Only escape str-like arguments passed to warnings

Fix #2956
This commit is contained in:
Bruno Oliveira 2017-11-27 23:07:29 -02:00
parent d95c8a2204
commit 796db80ca4
3 changed files with 17 additions and 1 deletions

View File

@ -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

1
changelog/2956.bugfix Normal file
View File

@ -0,0 +1 @@
Fix regression with warnings that contained non-strings in their arguments in Python 2.

View File

@ -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 *'])