2017-03-21 07:06:01 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import warnings
|
2017-03-05 03:07:37 +08:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2016-06-22 18:21:51 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-05-25 17:59:42 +08:00
|
|
|
from _pytest import compat
|
|
|
|
|
2016-06-22 18:21:51 +08:00
|
|
|
|
2016-10-18 02:34:30 +08:00
|
|
|
def _setoption(wmod, arg):
|
|
|
|
"""
|
|
|
|
Copy of the warning._setoption function but does not escape arguments.
|
|
|
|
"""
|
|
|
|
parts = arg.split(':')
|
|
|
|
if len(parts) > 5:
|
|
|
|
raise wmod._OptionError("too many fields (max 5): %r" % (arg,))
|
|
|
|
while len(parts) < 5:
|
|
|
|
parts.append('')
|
|
|
|
action, message, category, module, lineno = [s.strip()
|
|
|
|
for s in parts]
|
|
|
|
action = wmod._getaction(action)
|
|
|
|
category = wmod._getcategory(category)
|
|
|
|
if lineno:
|
|
|
|
try:
|
|
|
|
lineno = int(lineno)
|
|
|
|
if lineno < 0:
|
|
|
|
raise ValueError
|
|
|
|
except (ValueError, OverflowError):
|
|
|
|
raise wmod._OptionError("invalid lineno %r" % (lineno,))
|
|
|
|
else:
|
|
|
|
lineno = 0
|
|
|
|
wmod.filterwarnings(action, message, category, module, lineno)
|
|
|
|
|
|
|
|
|
2016-06-22 18:21:51 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("pytest-warnings")
|
|
|
|
group.addoption(
|
|
|
|
'-W', '--pythonwarnings', action='append',
|
2016-10-18 02:34:30 +08:00
|
|
|
help="set which warnings to report, see -W option of python itself.")
|
|
|
|
parser.addini("filterwarnings", type="linelist",
|
2017-07-21 20:18:45 +08:00
|
|
|
help="Each line specifies a pattern for "
|
|
|
|
"warnings.filterwarnings. "
|
|
|
|
"Processed after -W and --pythonwarnings.")
|
2016-06-22 18:21:51 +08:00
|
|
|
|
|
|
|
|
2017-03-05 03:07:37 +08:00
|
|
|
@contextmanager
|
|
|
|
def catch_warnings_for_item(item):
|
|
|
|
"""
|
|
|
|
catches the warnings generated during setup/call/teardown execution
|
|
|
|
of the given item and after it is done posts them as warnings to this
|
|
|
|
item.
|
|
|
|
"""
|
2016-06-22 18:21:51 +08:00
|
|
|
args = item.config.getoption('pythonwarnings') or []
|
2016-10-18 02:34:30 +08:00
|
|
|
inifilters = item.config.getini("filterwarnings")
|
2017-02-18 22:57:26 +08:00
|
|
|
with warnings.catch_warnings(record=True) as log:
|
2016-06-22 18:21:51 +08:00
|
|
|
for arg in args:
|
2017-02-18 22:57:26 +08:00
|
|
|
warnings._setoption(arg)
|
2016-10-18 02:34:30 +08:00
|
|
|
|
|
|
|
for arg in inifilters:
|
2017-02-18 22:57:26 +08:00
|
|
|
_setoption(warnings, arg)
|
2016-10-18 02:34:30 +08:00
|
|
|
|
2017-07-21 09:02:21 +08:00
|
|
|
mark = item.get_marker('filterwarnings')
|
|
|
|
if mark:
|
|
|
|
for arg in mark.args:
|
|
|
|
warnings._setoption(arg)
|
|
|
|
|
2016-06-22 18:21:51 +08:00
|
|
|
yield
|
|
|
|
|
2017-03-17 08:54:41 +08:00
|
|
|
for warning in log:
|
2017-05-26 13:12:02 +08:00
|
|
|
warn_msg = warning.message
|
|
|
|
unicode_warning = False
|
|
|
|
|
|
|
|
if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args):
|
2017-06-05 21:43:15 +08:00
|
|
|
new_args = [compat.safe_str(m) for m in warn_msg.args]
|
|
|
|
unicode_warning = warn_msg.args != new_args
|
|
|
|
warn_msg.args = new_args
|
2017-05-26 13:12:02 +08:00
|
|
|
|
2017-03-17 08:54:41 +08:00
|
|
|
msg = warnings.formatwarning(
|
2017-05-26 13:12:02 +08:00
|
|
|
warn_msg, warning.category,
|
2017-03-17 08:54:41 +08:00
|
|
|
warning.filename, warning.lineno, warning.line)
|
|
|
|
item.warn("unused", msg)
|
2017-03-05 03:07:37 +08:00
|
|
|
|
2017-05-26 13:12:02 +08:00
|
|
|
if unicode_warning:
|
|
|
|
warnings.warn(
|
2017-06-05 21:43:15 +08:00
|
|
|
"Warning is using unicode non convertible to ascii, "
|
2017-07-17 07:25:08 +08:00
|
|
|
"converting to a safe representation:\n %s" % msg,
|
2017-05-26 13:12:02 +08:00
|
|
|
UnicodeWarning)
|
|
|
|
|
2017-03-05 03:07:37 +08:00
|
|
|
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2017-03-17 08:54:41 +08:00
|
|
|
def pytest_runtest_protocol(item):
|
2017-03-05 03:07:37 +08:00
|
|
|
with catch_warnings_for_item(item):
|
|
|
|
yield
|