Skip to content

Commit

Permalink
Fix exception formatting while importing test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Mar 29, 2017
1 parent 6cfe087 commit 37a630e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
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
2 changes: 2 additions & 0 deletions _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)
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
18 changes: 18 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ def test_show_traceback_import_error(self, testdir, verbose):
assert name not in stdout


def test_show_traceback_import_error_unicode(self, testdir):
"""Import errors when collecting modules should display the traceback (#1976).
With low verbosity we omit pytest and internal modules, otherwise show all traceback entries.
"""
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

0 comments on commit 37a630e

Please sign in to comment.