Restored 2.7 implentation of deprecated_call

This commit is contained in:
Bruno Oliveira 2015-11-26 15:54:57 -02:00
parent 320c95ca43
commit 6378cdf7a9
1 changed files with 20 additions and 9 deletions

View File

@ -29,18 +29,29 @@ def pytest_namespace():
def deprecated_call(func, *args, **kwargs): def deprecated_call(func, *args, **kwargs):
"""Assert that ``func(*args, **kwargs)`` triggers a DeprecationWarning. """ assert that calling ``func(*args, **kwargs)``
triggers a DeprecationWarning.
""" """
wrec = WarningsRecorder() l = []
with wrec: oldwarn_explicit = getattr(warnings, 'warn_explicit')
warnings.simplefilter('always') # ensure all warnings are triggered def warn_explicit(*args, **kwargs):
l.append(args)
oldwarn_explicit(*args, **kwargs)
oldwarn = getattr(warnings, 'warn')
def warn(*args, **kwargs):
l.append(args)
oldwarn(*args, **kwargs)
warnings.warn_explicit = warn_explicit
warnings.warn = warn
try:
ret = func(*args, **kwargs) ret = func(*args, **kwargs)
finally:
depwarnings = (DeprecationWarning, PendingDeprecationWarning) warnings.warn_explicit = oldwarn_explicit
if not any(r.category in depwarnings for r in wrec): warnings.warn = oldwarn
if not l:
__tracebackhide__ = True __tracebackhide__ = True
raise AssertionError("%r did not produce DeprecationWarning" % (func,)) raise AssertionError("%r did not produce DeprecationWarning" %(func,))
return ret return ret