applied fix to deprecated decorator to warn only once (thanks Dan)

This commit is contained in:
Pablo Hoffman 2009-08-17 18:13:45 -03:00
parent 41036af643
commit 4f06f6ef41
1 changed files with 9 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import warnings import warnings
from functools import wraps
def deprecated(use_instead=None): def deprecated(use_instead=None):
@ -6,19 +7,20 @@ def deprecated(use_instead=None):
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."""
def wraps(func): def wrapped(func):
@wraps(func)
def new_func(*args, **kwargs): def new_func(*args, **kwargs):
message = "Call to deprecated function %s." % func.__name__ message = "Call to deprecated function %s." % func.__name__
if use_instead: if use_instead:
message += " Use %s instead." % use_instead message += " Use %s instead." % use_instead
warnings.warn_explicit( warnings.warn_explicit(message,
message,
category=DeprecationWarning, category=DeprecationWarning,
filename=func.func_code.co_filename, filename=func.func_code.co_filename,
lineno=func.func_code.co_firstlineno + 1 lineno=func.func_code.co_firstlineno + 1,
) registry=func.func_globals.setdefault('__warningregistry__', \
{}),
module_globals=func.func_globals)
return func(*args, **kwargs) return func(*args, **kwargs)
return new_func return new_func
return wraps return wrapped