line with "with" should not be on the stacktrace for exceptions raised in __exit__ #126932
Labels
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
type-bug
An unexpected behavior, bug, or error
Bug report
Bug description:
When an exception happens in the
__exit__
part of a context manager, thewith ...
line is part of the stacktrace. This is surprising (in the badway), because that line is not a part of the stacktrace while you're in the
associated block of code.
In other words: at the end (either the premature end through an exception, or
when the end of the end of the block is reached) the frame for the
with
statements gets pushed onto the traceback. Even though at that time nothing
from that line is being executed.
What I find particularly confusing (wrong?) is that this approach is suggestive
of the fact that code from the line of the
with
statement is being executedat the time of of failure.
OTOH, I kinda understand why the line is there: it's to explain why one arrives
at
__exit__
. But that's not really through that line, it's more indirectlythrough something like "exiting the with statement from line xx". (in the clean
exit case, this would arguable be at the end of the block).
Some example code and tracebacks:
knockknock
Compare this with an exception inside a with-statement, where the
__exit__
itself is without problems.nobody
I'm going to call this a bug because I have no way of (concisely) explaining why
this would be the correct behavior. In general, this would be my explanation of
a stacktrace:
This explanation breaks down for the above. Could it be amended without (at least)
doubling in length?
CPython versions tested on:
3.10, 3.12
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: