Skip to content

Commit

Permalink
Only escape str-like arguments passed to warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Nov 28, 2017
1 parent d95c8a2 commit 4bc3a71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion _pytest/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,17 @@ def test_show_warning():
""")
result = testdir.runpytest('-W always' if default_config == 'cmdline' else '')
result.stdout.fnmatch_lines(['*= 1 failed, 2 passed, 1 warnings in *'])


@pytest.mark.skipif(sys.version_info[0] >= 3, reason='Python 2 only')
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 *'])

0 comments on commit 4bc3a71

Please sign in to comment.