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:
@@ -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,))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user