Skip to content

Commit 144d909

Browse files
Merge pull request #2337 from nicoddemus/2336-unicode-tb
Fix exception formatting while importing test modules
2 parents 6cfe087 + a542ed4 commit 144d909

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
than ValueErrors in the ``fileno`` method (`#2276`_).
66
Thanks `@metasyn`_ for the PR.
77

8-
*
8+
* Fix exception formatting while importing modules when the exception message
9+
contains non-ascii characters (`#2336`_).
10+
Thanks `@fabioz`_ for the report and `@nicoddemus`_ for the PR.
11+
912

1013
*
1114

@@ -14,10 +17,12 @@
1417
*
1518

1619

20+
.. _@fabioz: https://github.com/fabioz
1721
.. _@metasyn: https://github.com/metasyn
1822

1923

2024
.. _#2276: https://github.com/pytest-dev/pytest/issues/2276
25+
.. _#2336: https://github.com/pytest-dev/pytest/issues/2336
2126

2227

2328
3.0.7 (2017-03-14)

_pytest/compat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,7 @@ def safe_str(v):
237237
try:
238238
return str(v)
239239
except UnicodeError:
240+
if not isinstance(v, unicode):
241+
v = unicode(v)
240242
errors = 'replace'
241-
return v.encode('ascii', errors)
243+
return v.encode('utf-8', errors)

_pytest/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
isclass, isfunction, is_generator, _escape_strings,
2020
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
2121
get_real_func, getfslineno, safe_getattr,
22-
getlocation, enum,
22+
safe_str, getlocation, enum,
2323
)
2424

2525
cutdir1 = py.path.local(pluggy.__file__.rstrip("oc"))
@@ -437,7 +437,7 @@ def _importtestmodule(self):
437437
if self.config.getoption('verbose') < 2:
438438
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
439439
exc_repr = exc_info.getrepr(style='short') if exc_info.traceback else exc_info.exconly()
440-
formatted_tb = py._builtin._totext(exc_repr)
440+
formatted_tb = safe_str(exc_repr)
441441
raise self.CollectError(
442442
"ImportError while importing test module '{fspath}'.\n"
443443
"Hint: make sure your test modules/packages have valid Python names.\n"

testing/python/collect.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ def test_show_traceback_import_error(self, testdir, verbose):
105105
assert name not in stdout
106106

107107

108+
def test_show_traceback_import_error_unicode(self, testdir):
109+
"""Check test modules collected which raise ImportError with unicode messages
110+
are handled properly (#2336).
111+
"""
112+
testdir.makepyfile(u"""
113+
# -*- coding: utf-8 -*-
114+
raise ImportError(u'Something bad happened ☺')
115+
""")
116+
result = testdir.runpytest()
117+
result.stdout.fnmatch_lines([
118+
"ImportError while importing test module*",
119+
"Traceback:",
120+
"*raise ImportError*Something bad happened*",
121+
])
122+
assert result.ret == 2
123+
124+
108125
class TestClass:
109126
def test_class_with_init_warning(self, testdir):
110127
testdir.makepyfile("""

0 commit comments

Comments
 (0)