[deprecated] On affiche les DeprecatedWarting dans python 2.7

fonction pour déclarer un module déprécié
This commit is contained in:
Valentin Samir 2013-04-25 16:27:31 +02:00
parent 9c809f9235
commit e82a0fac35

View file

@ -3,12 +3,16 @@
import warnings import warnings
import functools import functools
import inspect
import sys
def deprecated(replace=None): def deprecated(replace=None):
'''This is a decorator which can be used to mark functions '''This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted as deprecated. It will result in a warning being emitted
when the function is used.''' when the function is used.'''
warnings.resetwarnings()
if replace == None: if replace == None:
instead = "" instead = ""
elif isinstance(replace, str) or isinstance(replace, unicode): elif isinstance(replace, str) or isinstance(replace, unicode):
@ -29,3 +33,31 @@ def deprecated(replace=None):
return func(*args, **kwargs) return func(*args, **kwargs)
return new_func return new_func
return real_decorator return real_decorator
def module(replace=None):
"""À appeler dans un module déprécié"""
warnings.resetwarnings()
# On récupère le nom du module appelant la fonction
frm = inspect.stack()[1]
mod = inspect.getmodule(frm[0])
if mod and mod.__name__ != '__main__':
module_name = mod.__name__
else:
module_name = sys.argv[0]
if replace == None:
instead = ""
elif isinstance(replace, str) or isinstance(replace, unicode):
instead = " " + replace
else:
instead = " Use %s instead." % (replace.__name__,)
warnings.warn(
"Call to deprecated module %s.%s" % (module_name, instead),
category=DeprecationWarning,
stacklevel=3
)