Repository navigation
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): |
Replies: 7 comments
|
@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.
])
|
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. |
|
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? |
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. 😁 |
|
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? |
|
A note there might be worthwhile yes! 👍 |
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.