From 796db80ca42a88defbbf1ff10ec59e9d128159fa Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 27 Nov 2017 23:07:29 -0200 Subject: [PATCH] Only escape str-like arguments passed to warnings Fix #2956 --- _pytest/warnings.py | 4 +++- changelog/2956.bugfix | 1 + testing/test_warnings.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelog/2956.bugfix diff --git a/_pytest/warnings.py b/_pytest/warnings.py index 847771daa..3c2b1914f 100644 --- a/_pytest/warnings.py +++ b/_pytest/warnings.py @@ -72,7 +72,9 @@ def catch_warnings_for_item(item): unicode_warning = False if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args): - new_args = [compat.ascii_escaped(m) for m in warn_msg.args] + new_args = [] + for m in warn_msg.args: + new_args.append(compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES) else m) unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args diff --git a/changelog/2956.bugfix b/changelog/2956.bugfix new file mode 100644 index 000000000..13717657b --- /dev/null +++ b/changelog/2956.bugfix @@ -0,0 +1 @@ +Fix regression with warnings that contained non-strings in their arguments in Python 2. diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 7d3262802..02400bd1d 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -243,3 +243,16 @@ def test_filterwarnings_mark(testdir, default_config): """) result = testdir.runpytest('-W always' if default_config == 'cmdline' else '') result.stdout.fnmatch_lines(['*= 1 failed, 2 passed, 1 warnings in *']) + + +def test_non_string_warning_argument(testdir): + """Non-str argument passed to warning breaks pytest (#2956)""" + testdir.makepyfile(""" + import warnings + import pytest + + def test(): + warnings.warn(UserWarning(1, u'foo')) + """) + result = testdir.runpytest('-W', 'always') + result.stdout.fnmatch_lines(['*= 1 passed, 1 warnings in *'])