This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: traceback.clear_frames() doesn't clear *all* frames
Type: Stage: resolved
Components: Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords:

Created on 2017-09-01 14:50 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
clear_frames_bug.py vstinner, 2017-09-01 14:54
Pull Requests
URL Status Linked Edit
PR 3262 closed vstinner, 2017-09-01 14:51
Messages (4)
msg301111 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-01 14:50
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 {}

---
msg301113 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-01 14:52
Would it be ok to backport such change to Python 3.6? It's a bugfix, no?
msg301114 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-01 14:53
traceback.clear_frames() was added by bpo-1565525: commit 173a157e725579eec1f28f8c9d53d6761ba6c79f.
msg301117 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-01 14:59
See bpo-31323 where I wanted to use traceback.clear_frames() to manually break a reference cycle.
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75502
2018-09-19 23:09:12vstinnersetstatus: open -> closed
resolution: out of date
stage: resolved
2017-09-01 14:59:45vstinnersetmessages: + msg301117
2017-09-01 14:54:31vstinnersetversions: + Python 3.6
2017-09-01 14:54:07vstinnersetfiles: + clear_frames_bug.py
2017-09-01 14:53:07vstinnersetmessages: + msg301114
2017-09-01 14:52:12vstinnersetmessages: + msg301113
2017-09-01 14:51:21vstinnersetpull_requests: + pull_request3305
2017-09-01 14:50:17vstinnercreate