1
2 from __future__ import print_function
3 from __future__ import absolute_import
4
5 import warnings
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
25 warnings.simplefilter('once', DeprecationWarning)
26
27 if func_name:
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)
34
35 return _deprecated
36
41
42 @deprecated('new_method')
45
46 @deprecated()
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)
61 self.old_method()
62 assert ('DeprecationWarning: Call to deprecated method old_method; use new_method instead' in out.getvalue())
63 out.truncate(0)
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