Skip to content

Fix exception formatting while importing test modules #2337

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 2 commits into from
Mar 30, 2017
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
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
than ValueErrors in the ``fileno`` method (`#2276`_).
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.


*

Expand All @@ -14,10 +17,12 @@
*


.. _@fabioz: https://github.com/fabioz
.. _@metasyn: https://github.com/metasyn


.. _#2276: https://github.com/pytest-dev/pytest/issues/2276
.. _#2336: https://github.com/pytest-dev/pytest/issues/2336


3.0.7 (2017-03-14)
Expand Down
4 changes: 3 additions & 1 deletion _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,7 @@ def safe_str(v):
try:
return str(v)
except UnicodeError:
if not isinstance(v, unicode):
v = unicode(v)
errors = 'replace'
return v.encode('ascii', errors)
return v.encode('utf-8', errors)
4 changes: 2 additions & 2 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
isclass, isfunction, is_generator, _escape_strings,
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
get_real_func, getfslineno, safe_getattr,
getlocation, enum,
safe_str, getlocation, enum,
)

cutdir1 = py.path.local(pluggy.__file__.rstrip("oc"))
Expand Down Expand Up @@ -437,7 +437,7 @@ def _importtestmodule(self):
if self.config.getoption('verbose') < 2:
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
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(
"ImportError while importing test module '{fspath}'.\n"
"Hint: make sure your test modules/packages have valid Python names.\n"
Expand Down
17 changes: 17 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ def test_show_traceback_import_error(self, testdir, verbose):
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:
def test_class_with_init_warning(self, testdir):
testdir.makepyfile("""
Expand Down