Package dpkt :: Module decorators
[hide private]
[frames] | no frames]

Source Code for Module dpkt.decorators

 1  # -*- coding: utf-8 -*- 
 2  from __future__ import print_function 
 3  from __future__ import absolute_import 
 4   
 5  import warnings 
6 7 8 -def decorator_with_args(decorator_to_enhance):
9 """ 10 This is decorator for decorator. It allows any decorator to get additional arguments 11 """ 12 def decorator_maker(*args, **kwargs): 13 def decorator_wrapper(func): 14 return decorator_to_enhance(func, *args, **kwargs)
15 16 return decorator_wrapper 17 18 return decorator_maker 19
20 21 @decorator_with_args 22 -def deprecated(deprecated_method, func_name=None):
23 def _deprecated(*args, **kwargs): 24 # Print only the first occurrence of the DeprecationWarning, regardless of location 25 warnings.simplefilter('once', DeprecationWarning) 26 # Display the deprecation warning message 27 if func_name: # If the function, should be used instead, is received 28 warnings.warn("Call to deprecated method %s; use %s instead" % (deprecated_method.__name__, func_name), 29 category=DeprecationWarning, stacklevel=2) 30 else: 31 warnings.warn("Call to deprecated method %s" % deprecated_method.__name__, 32 category=DeprecationWarning, stacklevel=2) 33 return deprecated_method(*args, **kwargs) # actually call the method
34 35 return _deprecated 36
37 38 -class TestDeprecatedDecorator(object):
39 - def new_method(self):
40 return
41 42 @deprecated('new_method')
43 - def old_method(self):
44 return
45 46 @deprecated()
47 - def deprecated_decorator(self):
48 return
49
51 import sys 52 from .compat import StringIO 53 54 saved_stderr = sys.stderr 55 try: 56 out = StringIO() 57 sys.stderr = out 58 self.deprecated_decorator() 59 assert ('DeprecationWarning: Call to deprecated method deprecated_decorator' in out.getvalue()) 60 out.truncate(0) # clean the buffer 61 self.old_method() 62 assert ('DeprecationWarning: Call to deprecated method old_method; use new_method instead' in out.getvalue()) 63 out.truncate(0) # clean the buffer 64 self.new_method() 65 assert ('DeprecationWarning' not in out.getvalue()) 66 finally: 67 sys.stderr = saved_stderr
68 69 70 if __name__ == '__main__': 71 a = TestDeprecatedDecorator() 72 a.test_deprecated_decorator() 73 print('Tests Successful...') 74