ginormous traceback for cached exceptions #10362
-
We have tests that are paremetrized with two values. Let's call them @pytest.mark.parametrize(("foo", "bar"), ...)
def test_foo_bar(foo, bar):
processed_foo = do_something_expensive_with_foo(foo)
do_something_with_foo_and_bar(processed_foo, bar) This means def cache(fn):
"""Similar to :func:`functools.cache` (Python >= 3.8) or :func:`functools.lru_cache` with infinite cache size,
but this also caches exceptions.
"""
sentinel = object()
out_cache = {}
exc_cache = {}
@functools.wraps(fn)
def wrapper(*args, **kwargs):
key = args + tuple(kwargs.values())
out = out_cache.get(key, sentinel)
if out is not sentinel:
return out
exc = exc_cache.get(key, sentinel)
if exc is not sentinel:
raise exc
try:
out = fn(*args, **kwargs)
except Exception as exc:
exc_cache[key] = exc
raise exc
out_cache[key] = out
return out
return wrapper It works exactly as intended except for one thing: if the call fails, it creates traceback abominations like this: Traceback
As you can see, the largest part of the traceback is coming from Traceback
I'm guessing Unfortunately, I'm unable to reproduce this outside of the highly parametrized setting detailed above. This is why I'm fishing for ideas here instead of opening an issue. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
at first glance it unclear whats happening there, this is most likely as side-effect of the way the exceptions get cached, however it probably should be considered a pytest bug that it happens |
Beta Was this translation helpful? Give feedback.
at first glance it unclear whats happening there,
this is most likely a problem with raising/re-raising the exception and a unexpected interaction of the traceback from the exception info vs the traceback of the exception object vs the way exceptions and exception causes are tracked in python
this is most likely as side-effect of the way the exceptions get cached, however it probably should be considered a pytest bug that it happens
right now im unaware of the extra details needed to ensure python+pytest gets this right - a research into exceptions vs exception sources vs exception causes is necessary