Skip to content

Error importing module is not properly treated in pytest (fails with UnicodeEncodeError from pytest) #2336

@fabioz

Description

@fabioz

pytest version: 3.0.5, OS: Windows 10, python: 2.7.12

We have an intermittent issue when running pytest in that a module won't be properly loaded when imported by pytest. I still don't know the real reason for the issue (could be pytest related or not, still unsure as it's flaky on our ci)... anyways, the known problem on pytest is that pytest hides the real error by giving an UnicodeEncodeError instead of printing the original message (the error is below).

It seems that the problem is that it's mixing bytes and unicode on python 2:

formatted_tb = py._builtin._totext(exc_repr) # unicode on python 2, and the .format() call is made on a bytes array.

Exception given by pytest:

W:\py27-win64\lib\site-packages\_pytest\runner.py:163: in __init__
    self.result = func()
W:\py27-win64\lib\site-packages\_pytest\main.py:464: in _memocollect
    return self._memoizedcall('_collected', lambda: list(self.collect()))
W:\py27-win64\lib\site-packages\_pytest\main.py:335: in _memoizedcall
    res = function()
W:\py27-win64\lib\site-packages\_pytest\main.py:464: in <lambda>
    return self._memoizedcall('_collected', lambda: list(self.collect()))
W:\py27-win64\lib\site-packages\_pytest\python.py:411: in collect
    self.session._fixturemanager.parsefactories(self)
W:\py27-win64\lib\site-packages\_pytest\fixtures.py:1064: in parsefactories
    holderobj = node_or_obj.obj
W:\py27-win64\lib\site-packages\_pytest\python.py:215: in fget
    self._obj = obj = self._getobj()
W:\py27-win64\lib\site-packages\_pytest\python.py:408: in _getobj
    return self._memoizedcall('_obj', self._importtestmodule)
W:\py27-win64\lib\site-packages\_pytest\main.py:335: in _memoizedcall
    res = function()
W:\py27-win64\lib\site-packages\_pytest\python.py:445: in _importtestmodule
    "{traceback}".format(fspath=self.fspath, traceback=formatted_tb)
E   UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 110: ordinal not in range(128)

Metadata

Metadata

Assignees

Labels

status: criticalgrave problem or usability issue that affects lots of userstype: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions