Handle unserializable warning arguments

Fix #349
This commit is contained in:
Bruno Oliveira
2018-10-29 18:47:43 -03:00
parent 70688b7986
commit b151e56fbd
4 changed files with 37 additions and 4 deletions

1
changelog/349.bugfix.rst Normal file
View File

@@ -0,0 +1 @@
Correctly handle warnings created with arguments that can't be serialized during the transfer from workers to master node.

View File

@@ -766,6 +766,23 @@ class TestWarnings:
result = testdir.runpytest(n)
result.stdout.fnmatch_lines(["*MyWarning*", "*1 passed, 1 warnings*"])
@pytest.mark.parametrize("n", ["-n0", "-n1"])
def test_unserializable_arguments(self, testdir, n):
"""Check that warnings with unserializable arguments are handled correctly (#349)."""
testdir.makepyfile(
"""
import warnings, pytest
def test_func(tmpdir):
fn = (tmpdir / 'foo.txt').ensure(file=1)
with fn.open('r') as f:
warnings.warn(UserWarning("foo", f))
"""
)
testdir.syspathinsert()
result = testdir.runpytest(n)
result.stdout.fnmatch_lines(["*UserWarning*foo.txt*", "*1 passed, 1 warnings*"])
class TestNodeFailure:
def test_load_single(self, testdir):

View File

@@ -12,6 +12,7 @@ import time
import _pytest.hookspec
import pytest
from execnet.gateway_base import dumps, DumpError
class WorkerInteractor(object):
@@ -181,8 +182,15 @@ def serialize_warning_message(warning_message):
if isinstance(warning_message.message, Warning):
message_module = type(warning_message.message).__module__
message_class_name = type(warning_message.message).__name__
message_args = warning_message.message.args
message_str = str(warning_message.message)
# check now if we can serialize the warning arguments (#349)
# if not, we will just use the exception message on the master node
try:
dumps(warning_message.message.args)
except DumpError:
message_args = None
else:
message_args = warning_message.message.args
else:
message_str = warning_message.message
message_module = None

View File

@@ -426,9 +426,16 @@ def unserialize_warning_message(data):
if data["message_module"]:
mod = importlib.import_module(data["message_module"])
cls = getattr(mod, data["message_class_name"])
try:
message = cls(*data["message_args"])
except TypeError:
message = None
if data["message_args"] is not None:
try:
message = cls(*data["message_args"])
except TypeError:
pass
if message is None:
# could not recreate the original warning instance;
# create a generic Warning instance with the original
# message at least
message_text = "{mod}.{cls}: {msg}".format(
mod=data["message_module"],
cls=data["message_class_name"],