Merge pull request #345 from BrandonHoffman/master

add exception handeling for warning serialization issues
This commit is contained in:
Bruno Oliveira
2018-09-28 06:38:14 -03:00
committed by GitHub
4 changed files with 40 additions and 2 deletions

1
changelog/344.bugfix Normal file
View File

@@ -0,0 +1 @@
Fix issue where Warnings could cause pytest to fail if they do not set the args attribute correctly.

View File

@@ -429,6 +429,35 @@ class TestTerminalReporting:
result = testdir.runpytest(n) result = testdir.runpytest(n)
result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warnings*"]) result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warnings*"])
@pytest.mark.parametrize("n", ["-n0", "-n1"])
def test_custom_subclass(self, testdir, n):
"""Check that warning subclasses that don't honor the args attribute don't break
pytest-xdist (#344)
"""
from pkg_resources import parse_version
if parse_version(pytest.__version__) < parse_version("3.1"):
pytest.skip("pytest warnings requires >= 3.1")
testdir.makepyfile(
"""
import warnings, py, pytest
class MyWarning(UserWarning):
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
self.args = ()
def test_func(request):
warnings.warn(MyWarning("foo", 1))
"""
)
testdir.syspathinsert()
result = testdir.runpytest(n)
result.stdout.fnmatch_lines(["*MyWarning*", "*1 passed, 1 warnings*"])
def test_logfinish_hook(self, testdir): def test_logfinish_hook(self, testdir):
"""Ensure the pytest_runtest_logfinish hook is being properly handled""" """Ensure the pytest_runtest_logfinish hook is being properly handled"""
from _pytest import hookspec from _pytest import hookspec

View File

@@ -182,7 +182,7 @@ def serialize_warning_message(warning_message):
message_module = type(warning_message.message).__module__ message_module = type(warning_message.message).__module__
message_class_name = type(warning_message.message).__name__ message_class_name = type(warning_message.message).__name__
message_args = warning_message.message.args message_args = warning_message.message.args
message_str = None message_str = str(warning_message.message)
else: else:
message_str = warning_message.message message_str = warning_message.message
message_module = None message_module = None

View File

@@ -426,7 +426,15 @@ def unserialize_warning_message(data):
if data["message_module"]: if data["message_module"]:
mod = importlib.import_module(data["message_module"]) mod = importlib.import_module(data["message_module"])
cls = getattr(mod, data["message_class_name"]) cls = getattr(mod, data["message_class_name"])
message = cls(*data["message_args"]) try:
message = cls(*data["message_args"])
except TypeError:
message_text = "{mod}.{cls}: {msg}".format(
mod=data["message_module"],
cls=data["message_class_name"],
msg=data["message_str"],
)
message = Warning(message_text)
else: else:
message = data["message_str"] message = data["message_str"]