Py.test doesn't show local "caught exception" variables in detailed tracebacks #2919
-
Hello, I've noticed that when using showlocals ("-l" flag), pytest didnt display local exception variables created by an "except XXX as xxx" clause. It's however very useful to see the values of intermediate exceptions happening in frames of the traceback. Pytest modules installed: Example of traceback output with "py.test -vl" ("e" is missing in showlocals part):
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
Thanks @pakal for the report! |
Beta Was this translation helpful? Give feedback.
-
@nicoddemus any advice on where this is best fixed? I have mocked a stub test for now: def test_exceptions_are_displayed_when_showlocals_is_passed(pytester: Pytester) -> None:
pytester.makepyfile(
"""
def foobar():
try:
x = 1
y = 'foo'
10 / 0
except ZeroDivisionError as e:
c = 'bar'
raise RuntimeError(e)
def test_it():
foobar()
"""
)
result = pytester.runpytest("-v", "-l")
assert result.ret == ExitCode.TESTS_FAILED
result.stdout.fnmatch_lines([
"x*= 1",
"y*= 'foo'",
"c*= 'bar'"
#TODO: Ensure {'e'} is listed as in local scope.
])
|
Beta Was this translation helpful? Give feedback.
-
Hmm unfortunately no, the exception variable is automatically deleted once the stack is unwound to avoid cyclic references... there's a workaround, which is to assign the exception to a different variable: except recurly.errors.NotFoundError as e:
if "Account" in e.message:
error = e
raise RuntimeError("Abnormal NotFoundError encountered in refresh of user %s" % user) But I don't know how it could be fixed. |
Beta Was this translation helpful? Give feedback.
-
So it's the Python runtime itself which deletes the exception? I'm confused, isn't the GC able to deal with cyclic refererences except in rare cases? |
Beta Was this translation helpful? Give feedback.
-
Exactly. 👍
Yes but leaving that in there would create a cycle every time, and I guess that's something they want to avoid. Can't find more information about this right now, but I'm sure there are better explanations out there. 😁 |
Beta Was this translation helpful? Give feedback.
-
Allright, I guess it's not something Pytest can fix, it would modify Python behaviour too much ^^' Is there someplace where it would be worth documenting this? |
Beta Was this translation helpful? Give feedback.
-
A note there might be worthwhile yes! 👍 |
Beta Was this translation helpful? Give feedback.
Hmm unfortunately no, the exception variable is automatically deleted once the stack is unwound to avoid cyclic references...
there's a workaround, which is to assign the exception to a different variable:
But I don't know how it could be fixed.