Merge pull request #2337 from nicoddemus/2336-unicode-tb

Fix exception formatting while importing test modules
This commit is contained in:
Ronny Pfannschmidt 2017-03-30 09:12:14 +02:00 committed by GitHub
commit 144d90932e
4 changed files with 29 additions and 5 deletions

View File

@ -5,8 +5,11 @@
than ValueErrors in the ``fileno`` method (`#2276`_). than ValueErrors in the ``fileno`` method (`#2276`_).
Thanks `@metasyn`_ for the PR. Thanks `@metasyn`_ for the PR.
* * Fix exception formatting while importing modules when the exception message
contains non-ascii characters (`#2336`_).
Thanks `@fabioz`_ for the report and `@nicoddemus`_ for the PR.
* *
* *
@ -14,10 +17,12 @@
* *
.. _@fabioz: https://github.com/fabioz
.. _@metasyn: https://github.com/metasyn .. _@metasyn: https://github.com/metasyn
.. _#2276: https://github.com/pytest-dev/pytest/issues/2276 .. _#2276: https://github.com/pytest-dev/pytest/issues/2276
.. _#2336: https://github.com/pytest-dev/pytest/issues/2336
3.0.7 (2017-03-14) 3.0.7 (2017-03-14)

View File

@ -237,5 +237,7 @@ else:
try: try:
return str(v) return str(v)
except UnicodeError: except UnicodeError:
if not isinstance(v, unicode):
v = unicode(v)
errors = 'replace' errors = 'replace'
return v.encode('ascii', errors) return v.encode('utf-8', errors)

View File

@ -19,7 +19,7 @@ from _pytest.compat import (
isclass, isfunction, is_generator, _escape_strings, isclass, isfunction, is_generator, _escape_strings,
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET, REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
get_real_func, getfslineno, safe_getattr, get_real_func, getfslineno, safe_getattr,
getlocation, enum, safe_str, getlocation, enum,
) )
cutdir1 = py.path.local(pluggy.__file__.rstrip("oc")) cutdir1 = py.path.local(pluggy.__file__.rstrip("oc"))
@ -437,7 +437,7 @@ class Module(pytest.File, PyCollector):
if self.config.getoption('verbose') < 2: if self.config.getoption('verbose') < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback) exc_info.traceback = exc_info.traceback.filter(filter_traceback)
exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly() exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly()
formatted_tb = py._builtin._totext(exc_repr) formatted_tb = safe_str(exc_repr)
raise self.CollectError( raise self.CollectError(
"ImportError while importing test module '{fspath}'.\n" "ImportError while importing test module '{fspath}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n" "Hint: make sure your test modules/packages have valid Python names.\n"

View File

@ -105,6 +105,23 @@ class TestModule:
assert name not in stdout assert name not in stdout
def test_show_traceback_import_error_unicode(self, testdir):
"""Check test modules collected which raise ImportError with unicode messages
are handled properly (#2336).
"""
testdir.makepyfile(u"""
# -*- coding: utf-8 -*-
raise ImportError(u'Something bad happened ☺')
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"ImportError while importing test module*",
"Traceback:",
"*raise ImportError*Something bad happened*",
])
assert result.ret == 2
class TestClass: class TestClass:
def test_class_with_init_warning(self, testdir): def test_class_with_init_warning(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""