Skip to content

Commit 37a630e

Browse files
committed
Fix exception formatting while importing test modules
Fix pytest-dev#2336
1 parent 6cfe087 commit 37a630e

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
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: 2 additions & 0 deletions
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'
241243
return v.encode('ascii', 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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ 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+
"""Import errors when collecting modules should display the traceback (#1976).
110+
111+
With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
112+
"""
113+
testdir.makepyfile(u"""
114+
# -*- coding: utf-8 -*-
115+
raise ImportError(u'Something bad happened ☺')
116+
""")
117+
result = testdir.runpytest()
118+
result.stdout.fnmatch_lines([
119+
"ImportError while importing test module*",
120+
"Traceback:",
121+
"*raise ImportError*Something bad happened*",
122+
])
123+
assert result.ret == 2
124+
125+
108126
class TestClass:
109127
def test_class_with_init_warning(self, testdir):
110128
testdir.makepyfile("""

0 commit comments

Comments
 (0)