Fix warning about non-ascii warnings even when they are ascii

Fix #2809
This commit is contained in:
Bruno Oliveira 2017-10-02 16:20:51 -03:00
parent d132c502e6
commit fbb9e9328b
3 changed files with 24 additions and 3 deletions

View File

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

1
changelog/2809.bugfix Normal file
View File

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

View File

@ -163,13 +163,33 @@ def test_py2_unicode(testdir, pyfile_with_warnings):
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('''