You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of clear_frames traverses the tb_next linked list. In effect, it clears frames from the frame where the exception is caught to the top of the stack (the top half of the stack).
But it does not clear the bottom half of the stack, which is what this issue is about. To clear the bottom half, I think what clear_frames needs to do is to traverse the f_back linked list from the exception-catching frame, which goes all the way to to bottom of the stack.
Basically the new implementation of clear_frames would look like this:
def clear_frames(tb):
if tb is None:
return
frame = tb.tb_frame
while frame is not None:
try:
frame.clear()
except RuntimeError:
pass
frame = frame.f_back
tb = tb.tb_next
while tb is not None:
try:
tb.tb_frame.clear()
except RuntimeError:
pass
tb = tb.tb_next
If you think this change is a good starting point, I will create a PR for review. What do you think? @vstinner@pitrou
Activity
vstinner commentedon Sep 1, 2017
traceback.clear_frames():
"Clears the local variables of all the stack frames in a traceback tb by calling the clear() method of each frame object."
https://docs.python.org/dev/library/traceback.html#traceback.clear_frames
That's wrong: it only calls frame.clear() on the top frame of each traceback object.
Output of attached clear_frames_bug.py script:
---
locals:
frame locals {'z': 3}
frame locals {'y': 2}
frame locals {'x': 1}
clear_frames()locals:
frame locals {}
frame locals {'y': 2}
frame locals {'x': 1}
---
As you can see, only the locals of the func3() frame, where the exception was raised, is clear. Locals of other frames are not cleared.
Attached PR fixes the issue.
Output with the fix:
---
locals:
frame locals {'z': 3}
frame locals {'y': 2}
frame locals {'x': 1}
clear_frames()locals:
frame locals {}
frame locals {}
frame locals {}
---
vstinner commentedon Sep 1, 2017
Would it be ok to backport such change to Python 3.6? It's a bugfix, no?
vstinner commentedon Sep 1, 2017
traceback.clear_frames() was added by bpo-1565525: commit 173a157.
vstinner commentedon Sep 1, 2017
See bpo-31323 where I wanted to use traceback.clear_frames() to manually break a reference cycle.
clchiou commentedon Sep 29, 2022
Hi, I stumbled upon this bug, and gave it a look.
The current implementation of
clear_framestraverses thetb_nextlinked list. In effect, it clears frames from the frame where the exception is caught to the top of the stack (the top half of the stack).But it does not clear the bottom half of the stack, which is what this issue is about. To clear the bottom half, I think what
clear_framesneeds to do is to traverse thef_backlinked list from the exception-catching frame, which goes all the way to to bottom of the stack.Basically the new implementation of
clear_frameswould look like this:If you think this change is a good starting point, I will create a PR for review. What do you think? @vstinner @pitrou