Skip to content

Remove UnicodeWarning from pytest warnings #2466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions _pytest/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,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):
warn_msg.args = [compat.safe_str(m) for m in warn_msg.args]
unicode_warning = True
new_args = [compat.safe_str(m) for m in warn_msg.args]
unicode_warning = warn_msg.args != new_args
warn_msg.args = new_args

msg = warnings.formatwarning(
warn_msg, warning.category,
Expand All @@ -76,8 +77,8 @@ def catch_warnings_for_item(item):

if unicode_warning:
warnings.warn(
"This warning %s is broken as it's message is not a str instance"
"(after all this is a stdlib problem workaround)" % msg,
"Warning is using unicode non convertible to ascii, "
"converting to a safe representation:\n %s" % msg,
UnicodeWarning)


Expand Down
2 changes: 2 additions & 0 deletions changelog/2463.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``UnicodeWarning`` is issued from the internal pytest warnings plugin only when the message contains non-ascii
unicode (Python 2 only).
2 changes: 1 addition & 1 deletion testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_func(fix):

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

Expand Down