Skip to content
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

Bugfix unprintable assertion errors #2284

Merged

Conversation

omerhadari
Copy link
Contributor

@omerhadari omerhadari commented Mar 4, 2017

Thanks for submitting a PR, your contribution is really appreciated!

Here's a quick checklist that should be present in PRs:

  • Target: for bug or doc fixes, target master; for new features, target features;

Unless your change is trivial documentation fix (e.g., a typo or reword of a small section) please:

  • Make sure to include one or more tests for your change;
  • Add yourself to AUTHORS;
  • Add a new entry to CHANGELOG.rst
    • Choose any open position to avoid merge conflicts with other PRs.
    • Add a link to the issue you are fixing (if any) using RST syntax.
    • The pytest team likes to have people to acknowledged in the CHANGELOG, so please add a thank note to yourself ("Thanks @user for the PR") and a link to your GitHub profile. It may sound weird thanking yourself, but otherwise a maintainer would have to do it manually before or after merging instead of just using GitHub's merge button. This makes it easier on the maintainers to merge PRs.

The error I am fixing is one that I encountered when using a rather badly written python package I was forced to use as - is. One of the classes defined in the package decided to return unprintable strings as the result of its' __repr__ method. This caused py._builtin._totext to throw an error in python2.7, since it's actually the function unicode which cannot decode strings outside of range 128. Also using
_totext was unsafe in python3 as well, since classes with throwing __repr__ methods should not cause internal errors and stop all other tests from running.

I know that the event of both raising an AssertionError (instead of using assert) and the __repr__ method being unsafe is rather rare and points to horrific code, but it happened to me nonetheless, using code that is out of my control, and it should not cause internal errors imo.

Error reproduction:

$ cat test_throwing_repr.py
class BadRepr(object):
    def __repr__(self):
        raise NotImplementedError()

def test_raising_unprintable_assertion_error():
    raise AssertionError(BadRepr())

$ cat test_non_text_assertion_error_arg.py
def test_non_text_assertion_error_arg():
    # this causes internal errors only prior to python3
    raise AssertionError('\xff')

$ pytest test_throwing_repr.py
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /REDUCED, inifile: 
collected 1 items

test_throwing_repr.py
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 163, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 151, in <lambda>
INTERNALERROR>     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 613, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 254, in _wrapped_call
INTERNALERROR>     return call_outcome.get_result()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 279, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 265, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 104, in pytest_runtest_call
INTERNALERROR>     item.runtest()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/python.py", line 1574, in runtest
INTERNALERROR>     self.ihook.pytest_pyfunc_call(pyfuncitem=self)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 613, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 254, in _wrapped_call
INTERNALERROR>     return call_outcome.get_result()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 279, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 265, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/python.py", line 154, in pytest_pyfunc_call
INTERNALERROR>     testfunction(**testargs)
INTERNALERROR>   File "/REDUCED/test_throwing_repr.py", line 6, in test_raising_unprintable_assertion_error
INTERNALERROR>     raise AssertionError(BadRepr())
INTERNALERROR> AssertionError: <unprintable AssertionError object>
INTERNALERROR> 
INTERNALERROR> During handling of the above exception, another exception occurred:
INTERNALERROR> 
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/main.py", line 98, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/main.py", line 133, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/main.py", line 154, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 613, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 254, in _wrapped_call
INTERNALERROR>     return call_outcome.get_result()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 279, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 265, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 66, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 79, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 133, in call_and_report
INTERNALERROR>     call = call_runtest_hook(item, when, **kwds)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 151, in call_runtest_hook
INTERNALERROR>     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/runner.py", line 168, in __init__
INTERNALERROR>     self.excinfo = ExceptionInfo()
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/_pytest/_code/code.py", line 362, in __init__
INTERNALERROR>     exprinfo = py._builtin._totext(tup[1])
INTERNALERROR>   File "/REDUCED/.local/lib/python3.5/site-packages/py/_builtin.py", line 129, in _totext
INTERNALERROR>     obj = str(obj)
INTERNALERROR>   File "/REDUCED/test_throwing_repr.py", line 3, in __repr__
INTERNALERROR>     raise NotImplementedError()
INTERNALERROR> NotImplementedError

========================= no tests ran in 0.01 seconds =========================

$ pytest test_non_text_assertion_error_arg.py
============================= test session starts ==============================
platform linux2 -- Python 2.7.12, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /REDUCED, inifile: 
collected 1 items

test_non_text_assertion_error_arg.py
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/main.py", line 98, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/main.py", line 133, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/main.py", line 154, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 613, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 254, in _wrapped_call
INTERNALERROR>     return call_outcome.get_result()
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 280, in get_result
INTERNALERROR>     _reraise(*ex)  # noqa
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 265, in __init__
INTERNALERROR>     self.result = func()
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/runner.py", line 66, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/runner.py", line 79, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/runner.py", line 133, in call_and_report
INTERNALERROR>     call = call_runtest_hook(item, when, **kwds)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/runner.py", line 151, in call_runtest_hook
INTERNALERROR>     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/runner.py", line 168, in __init__
INTERNALERROR>     self.excinfo = ExceptionInfo()
INTERNALERROR>   File "/REDUCED/.local/lib/python2.7/site-packages/_pytest/_code/code.py", line 362, in __init__
INTERNALERROR>     exprinfo = py._builtin._totext(tup[1])
INTERNALERROR> UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

========================= no tests ran in 0.00 seconds =========================

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.71% when pulling f71467f on omerhadari:bugfix-unprintable-assertion-errors into 5721d8a on pytest-dev:master.

@nicoddemus
Copy link
Member

Thanks a lot for the PR, looks great!

@nicoddemus
Copy link
Member

(py37 failure on Travis is unrelated and can be ignored: #2285)

@omerhadari
Copy link
Contributor Author

Sorry for the mess, I put my name in the AUTHORS list in the wrong place, changed it now :)
Thanks for the super quick reply! Waiting patiently for a merge now :)

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.71% when pulling dd25ae7 on omerhadari:bugfix-unprintable-assertion-errors into 5721d8a on pytest-dev:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.71% when pulling dd25ae7 on omerhadari:bugfix-unprintable-assertion-errors into 5721d8a on pytest-dev:master.

@flub flub merged commit b28749e into pytest-dev:master Mar 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants