From e82a0fac35f4c7e111db9d85978f7c6bac200db7 Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Thu, 25 Apr 2013 16:27:31 +0200 Subject: [PATCH] =?UTF-8?q?[deprecated]=20On=20affiche=20les=20DeprecatedW?= =?UTF-8?q?arting=20dans=20python=202.7=20fonction=20pour=20d=C3=A9clarer?= =?UTF-8?q?=20un=20module=20d=C3=A9pr=C3=A9ci=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/deprecated.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/deprecated.py b/lib/deprecated.py index 2aca5197..50ee8459 100644 --- a/lib/deprecated.py +++ b/lib/deprecated.py @@ -3,12 +3,16 @@ import warnings import functools +import inspect +import sys def deprecated(replace=None): '''This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.''' + warnings.resetwarnings() + if replace == None: instead = "" elif isinstance(replace, str) or isinstance(replace, unicode): @@ -29,3 +33,31 @@ def deprecated(replace=None): return func(*args, **kwargs) return new_func 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 + ) +