Use a callable __tracebackhide__ for filtering

While this leads to slightly more complicated user code for the common
case (checking if the exception is of a given type) it's easier to
implement and more flexible.
This commit is contained in:
Florian Bruhin
2016-04-20 11:07:34 +02:00
parent 4c552d4ef7
commit 75160547f2
4 changed files with 27 additions and 32 deletions

View File

@@ -216,16 +216,18 @@ Let's run our little function::
test_checkconfig.py:8: Failed
1 failed in 0.12 seconds
If you only want to hide certain exception classes, you can also set
``__tracebackhide__`` to an exception type or a list of exception types::
If you only want to hide certain exceptions, you can set ``__tracebackhide__``
to a callable which gets the ``ExceptionInfo`` object. You can for example use
this to make sure unexpected exception types aren't hidden::
import operator
import pytest
class ConfigException(Exception):
pass
def checkconfig(x):
__tracebackhide__ = ConfigException
__tracebackhide__ = operator.methodcaller('errisinstance', ConfigException)
if not hasattr(x, "config"):
raise ConfigException("not configured: %s" %(x,))