2010-11-06 18:38:53 +08:00
|
|
|
""" recording warnings during test function execution. """
|
2009-06-18 23:26:40 +08:00
|
|
|
|
2009-05-06 03:24:47 +08:00
|
|
|
import py
|
2010-07-05 04:13:12 +08:00
|
|
|
import sys, os
|
2009-05-06 03:24:47 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__recwarn(request):
|
2009-07-22 22:09:49 +08:00
|
|
|
"""Return a WarningsRecorder instance that provides these methods:
|
|
|
|
|
|
|
|
* ``pop(category=None)``: return last warning matching the category.
|
2010-07-27 03:15:15 +08:00
|
|
|
* ``clear()``: clear list of warnings
|
2009-07-22 22:09:49 +08:00
|
|
|
"""
|
2010-07-05 04:13:12 +08:00
|
|
|
if sys.version_info >= (2,7):
|
2010-07-27 03:15:15 +08:00
|
|
|
import warnings
|
2010-07-05 04:13:12 +08:00
|
|
|
oldfilters = warnings.filters[:]
|
|
|
|
warnings.simplefilter('default')
|
|
|
|
def reset_filters():
|
|
|
|
warnings.filters[:] = oldfilters
|
|
|
|
request.addfinalizer(reset_filters)
|
|
|
|
wrec = WarningsRecorder()
|
|
|
|
request.addfinalizer(wrec.finalize)
|
|
|
|
return wrec
|
2009-05-06 03:24:47 +08:00
|
|
|
|
2009-07-18 00:07:37 +08:00
|
|
|
def pytest_namespace():
|
2009-06-18 23:26:40 +08:00
|
|
|
return {'deprecated_call': deprecated_call}
|
|
|
|
|
|
|
|
def deprecated_call(func, *args, **kwargs):
|
2010-11-18 21:56:16 +08:00
|
|
|
""" assert that calling ``func(*args, **kwargs)``
|
|
|
|
triggers a DeprecationWarning.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2009-06-18 23:26:40 +08:00
|
|
|
warningmodule = py.std.warnings
|
|
|
|
l = []
|
|
|
|
oldwarn_explicit = getattr(warningmodule, 'warn_explicit')
|
2010-07-27 03:15:15 +08:00
|
|
|
def warn_explicit(*args, **kwargs):
|
|
|
|
l.append(args)
|
2009-06-18 23:26:40 +08:00
|
|
|
oldwarn_explicit(*args, **kwargs)
|
|
|
|
oldwarn = getattr(warningmodule, 'warn')
|
2010-07-27 03:15:15 +08:00
|
|
|
def warn(*args, **kwargs):
|
|
|
|
l.append(args)
|
2009-06-18 23:26:40 +08:00
|
|
|
oldwarn(*args, **kwargs)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-06-18 23:26:40 +08:00
|
|
|
warningmodule.warn_explicit = warn_explicit
|
|
|
|
warningmodule.warn = warn
|
|
|
|
try:
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
warningmodule.warn_explicit = warn_explicit
|
|
|
|
warningmodule.warn = warn
|
|
|
|
if not l:
|
|
|
|
#print warningmodule
|
2009-12-30 19:13:38 +08:00
|
|
|
__tracebackhide__ = True
|
2009-06-18 23:26:40 +08:00
|
|
|
raise AssertionError("%r did not produce DeprecationWarning" %(func,))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2009-05-06 03:24:47 +08:00
|
|
|
class RecordedWarning:
|
|
|
|
def __init__(self, message, category, filename, lineno, line):
|
|
|
|
self.message = message
|
|
|
|
self.category = category
|
|
|
|
self.filename = filename
|
|
|
|
self.lineno = lineno
|
|
|
|
self.line = line
|
|
|
|
|
|
|
|
class WarningsRecorder:
|
|
|
|
def __init__(self):
|
|
|
|
warningmodule = py.std.warnings
|
|
|
|
self.list = []
|
|
|
|
def showwarning(message, category, filename, lineno, line=0):
|
|
|
|
self.list.append(RecordedWarning(
|
|
|
|
message, category, filename, lineno, line))
|
|
|
|
try:
|
2010-07-27 03:15:15 +08:00
|
|
|
self.old_showwarning(message, category,
|
2009-05-06 03:24:47 +08:00
|
|
|
filename, lineno, line=line)
|
|
|
|
except TypeError:
|
2010-07-27 03:15:15 +08:00
|
|
|
# < python2.6
|
2009-05-06 03:24:47 +08:00
|
|
|
self.old_showwarning(message, category, filename, lineno)
|
|
|
|
self.old_showwarning = warningmodule.showwarning
|
|
|
|
warningmodule.showwarning = showwarning
|
|
|
|
|
|
|
|
def pop(self, cls=Warning):
|
|
|
|
""" pop the first recorded warning, raise exception if not exists."""
|
2009-08-28 22:25:29 +08:00
|
|
|
for i, w in enumerate(self.list):
|
2009-05-06 03:24:47 +08:00
|
|
|
if issubclass(w.category, cls):
|
|
|
|
return self.list.pop(i)
|
2009-05-06 04:31:18 +08:00
|
|
|
__tracebackhide__ = True
|
2009-05-06 03:24:47 +08:00
|
|
|
assert 0, "%r not found in %r" %(cls, self.list)
|
|
|
|
|
|
|
|
#def resetregistry(self):
|
|
|
|
# import warnings
|
|
|
|
# warnings.onceregistry.clear()
|
|
|
|
# warnings.__warningregistry__.clear()
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def clear(self):
|
2009-05-06 03:24:47 +08:00
|
|
|
self.list[:] = []
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
py.std.warnings.showwarning = self.old_showwarning
|