mirror of https://github.com/scrapy/scrapy.git
applied fix to deprecated decorator to warn only once (thanks Dan)
This commit is contained in:
parent
41036af643
commit
4f06f6ef41
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue