Commit 5af866e 1 parent 3301a1c commit 5af866e Copy full SHA for 5af866e
File tree 4 files changed +30
-6
lines changed
4 files changed +30
-6
lines changed Original file line number Diff line number Diff line change 6
6
* Import errors when collecting test modules now display the full traceback (`#1976 `_).
7
7
Thanks `@cwitty `_ for the report and `@nicoddemus `_ for the PR.
8
8
9
- *
9
+ * When loading plugins, import errors which contain non-ascii messages are now properly handled in Python 2 (`#1998 `_).
10
+ Thanks `@nicoddemus `_ for the PR.
10
11
11
12
*
12
13
13
14
14
15
.. _@cwitty : https://github.com/cwitty
15
16
16
17
.. _#1976 : https://github.com/pytest-dev/pytest/issues/1976
18
+ .. _#1998 : https://github.com/pytest-dev/pytest/issues/1998
17
19
18
20
19
21
Original file line number Diff line number Diff line change @@ -213,4 +213,18 @@ def _is_unittest_unexpected_success_a_failure():
213
213
Changed in version 3.4: Returns False if there were any
214
214
unexpectedSuccesses from tests marked with the expectedFailure() decorator.
215
215
"""
216
- return sys .version_info >= (3 , 4 )
216
+ return sys .version_info >= (3 , 4 )
217
+
218
+
219
+ if _PY3 :
220
+ def safe_str (v ):
221
+ """returns v as string"""
222
+ return str (v )
223
+ else :
224
+ def safe_str (v ):
225
+ """returns v as string, converting to ascii if necessary"""
226
+ try :
227
+ return str (v )
228
+ except UnicodeError :
229
+ errors = 'replace'
230
+ return v .encode ('ascii' , errors )
Original file line number Diff line number Diff line change 12
12
import _pytest .hookspec # the extension point definitions
13
13
import _pytest .assertion
14
14
from _pytest ._pluggy import PluginManager , HookimplMarker , HookspecMarker
15
+ from _pytest .compat import safe_str
15
16
16
17
hookimpl = HookimplMarker ("pytest" )
17
18
hookspec = HookspecMarker ("pytest" )
@@ -405,7 +406,8 @@ def import_plugin(self, modname):
405
406
try :
406
407
__import__ (importspec )
407
408
except ImportError as e :
408
- new_exc = ImportError ('Error importing plugin "%s": %s' % (modname , e ))
409
+ msg = safe_str (e .args [0 ])
410
+ new_exc = ImportError ('Error importing plugin "%s": %s' % (modname , msg ))
409
411
# copy over name and path attributes
410
412
for attr in ('name' , 'path' ):
411
413
if hasattr (e , attr ):
Original file line number Diff line number Diff line change
1
+ # encoding: UTF-8
1
2
import pytest
2
3
import py
3
4
import os
@@ -179,15 +180,20 @@ def test_default_markers(testdir):
179
180
])
180
181
181
182
182
- def test_importplugin_issue375 (testdir , pytestpm ):
183
+ def test_importplugin_error_message (testdir , pytestpm ):
183
184
"""Don't hide import errors when importing plugins and provide
184
185
an easy to debug message.
186
+
187
+ See #375 and #1998.
185
188
"""
186
189
testdir .syspathinsert (testdir .tmpdir )
187
- testdir .makepyfile (qwe = "import aaaa" )
190
+ testdir .makepyfile (qwe = """
191
+ # encoding: UTF-8
192
+ raise ImportError(u'Not possible to import: ☺')
193
+ """ )
188
194
with pytest .raises (ImportError ) as excinfo :
189
195
pytestpm .import_plugin ("qwe" )
190
- expected = '.*Error importing plugin "qwe": No module named \' ?aaaa \' ? '
196
+ expected = '.*Error importing plugin "qwe": Not possible to import: . '
191
197
assert py .std .re .match (expected , str (excinfo .value ))
192
198
193
199
You can’t perform that action at this time.
0 commit comments