Skip to content

Commit f6ac209

Browse files
authored
Merge pull request #345 from BrandonHoffman/master
add exception handeling for warning serialization issues
2 parents 8475876 + b176b37 commit f6ac209

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

changelog/344.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue where Warnings could cause pytest to fail if they do not set the args attribute correctly.

testing/acceptance_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,35 @@ def test_func(request):
429429
result = testdir.runpytest(n)
430430
result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warnings*"])
431431

432+
@pytest.mark.parametrize("n", ["-n0", "-n1"])
433+
def test_custom_subclass(self, testdir, n):
434+
"""Check that warning subclasses that don't honor the args attribute don't break
435+
pytest-xdist (#344)
436+
"""
437+
from pkg_resources import parse_version
438+
439+
if parse_version(pytest.__version__) < parse_version("3.1"):
440+
pytest.skip("pytest warnings requires >= 3.1")
441+
442+
testdir.makepyfile(
443+
"""
444+
import warnings, py, pytest
445+
446+
class MyWarning(UserWarning):
447+
448+
def __init__(self, p1, p2):
449+
self.p1 = p1
450+
self.p2 = p2
451+
self.args = ()
452+
453+
def test_func(request):
454+
warnings.warn(MyWarning("foo", 1))
455+
"""
456+
)
457+
testdir.syspathinsert()
458+
result = testdir.runpytest(n)
459+
result.stdout.fnmatch_lines(["*MyWarning*", "*1 passed, 1 warnings*"])
460+
432461
def test_logfinish_hook(self, testdir):
433462
"""Ensure the pytest_runtest_logfinish hook is being properly handled"""
434463
from _pytest import hookspec

xdist/remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def serialize_warning_message(warning_message):
182182
message_module = type(warning_message.message).__module__
183183
message_class_name = type(warning_message.message).__name__
184184
message_args = warning_message.message.args
185-
message_str = None
185+
message_str = str(warning_message.message)
186186
else:
187187
message_str = warning_message.message
188188
message_module = None

xdist/workermanage.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,15 @@ def unserialize_warning_message(data):
426426
if data["message_module"]:
427427
mod = importlib.import_module(data["message_module"])
428428
cls = getattr(mod, data["message_class_name"])
429-
message = cls(*data["message_args"])
429+
try:
430+
message = cls(*data["message_args"])
431+
except TypeError:
432+
message_text = "{mod}.{cls}: {msg}".format(
433+
mod=data["message_module"],
434+
cls=data["message_class_name"],
435+
msg=data["message_str"],
436+
)
437+
message = Warning(message_text)
430438
else:
431439
message = data["message_str"]
432440

0 commit comments

Comments
 (0)