Skip to content

Commit

Permalink
Fix warning about non-ascii warnings even when they are ascii
Browse files Browse the repository at this point in the history
Fix #2809
  • Loading branch information
nicoddemus committed Oct 3, 2017
1 parent d132c50 commit fbb9e93
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions _pytest/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ 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.safe_str(m) for m in warn_msg.args]
unicode_warning = warn_msg.args != new_args
new_args = [m.encode('ascii', 'replace') for m in warn_msg.args]
unicode_warning = list(warn_msg.args) != new_args
warn_msg.args = new_args

msg = warnings.formatwarning(
Expand Down
1 change: 1 addition & 0 deletions changelog/2809.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pytest no longer complains about warnings with unicode messages being non-ascii compatible even for ascii-compatible messages. As a result of this, warnings with unicode messages are converted first to an ascii representation for safety.
22 changes: 21 additions & 1 deletion testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,33 @@ def test_func(fix):
result.stdout.fnmatch_lines([
'*== %s ==*' % WARNINGS_SUMMARY_HEADER,

'*test_py2_unicode.py:8: UserWarning: \u6d4b\u8bd5',
'*test_py2_unicode.py:8: UserWarning: ??',
'*warnings.warn(u"\u6d4b\u8bd5")',
'*warnings.py:*: UnicodeWarning: Warning is using unicode non*',
'* 1 passed, 2 warnings*',
])


def test_py2_unicode_ascii(testdir):
"""Ensure that our warning about 'unicode warnings containing non-ascii messages'
does not trigger with ascii-convertible messages"""
testdir.makeini('[pytest]')
testdir.makepyfile('''
import pytest
import warnings
@pytest.mark.filterwarnings('always')
def test_func():
warnings.warn(u"hello")
''')
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*== %s ==*' % WARNINGS_SUMMARY_HEADER,
'*warnings.warn(u"hello")',
'* 1 passed, 1 warnings in*'
])


def test_works_with_filterwarnings(testdir):
"""Ensure our warnings capture does not mess with pre-installed filters (#2430)."""
testdir.makepyfile('''
Expand Down

0 comments on commit fbb9e93

Please sign in to comment.