Add UnicodeWarning for unicode warnings in Python2

This commit is contained in:
wanghui
2017-05-26 13:12:02 +08:00
parent c39689da41
commit d7a5c5716f
3 changed files with 47 additions and 1 deletions

View File

@@ -126,6 +126,7 @@ if _PY3:
import codecs
imap = map
STRING_TYPES = bytes, str
UNICODE_TYPES = str,
def _escape_strings(val):
"""If val is pure ascii, returns it as a str(). Otherwise, escapes
@@ -157,6 +158,7 @@ if _PY3:
return val.encode('unicode_escape').decode('ascii')
else:
STRING_TYPES = bytes, str, unicode
UNICODE_TYPES = unicode,
from itertools import imap # NOQA

View File

@@ -63,11 +63,24 @@ def catch_warnings_for_item(item):
yield
for warning in log:
warn_msg = warning.message
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
msg = warnings.formatwarning(
compat.safe_str(warning.message), warning.category,
warn_msg, warning.category,
warning.filename, warning.lineno, warning.line)
item.warn("unused", msg)
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,
UnicodeWarning)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_protocol(item):