[Python-Dev] Evil reference cycles caused Exception.__traceback__

Nick Coghlan ncoghlan at gmail.com
Mon Sep 18 06:35:02 EDT 2017

On 18 September 2017 at 20:18, Victor Stinner <victor.stinner at gmail.com> wrote:
> 2017-09-18 12:07 GMT+02:00 Nick Coghlan <ncoghlan at gmail.com>:
>> I wonder if it might be reasonable to have tracebacks only hold a weak
>> reference to their frame objects when "__debug__ == False".
> Please don't change the Python behaviour depending on __debug__, or it
> will be a nightmare to debug it :-( ("Why does it work on my
> computer?")

Yeah, that's always a challenge :)

Rather than being thread local or context local state, whether or not
to keep the full frames in the traceback could be a yet another
setting on the *exception* object, whereby we tweaked the logic that
drops the reference at the end of an except clause as follows:

1. If the exception ref count == 1, just drop the reference and let
the traceback die with it
2. If the exception ref count > 1, check for a new "__keep_locals__" attribute
3. If it's true, just drop the reference and otherwise leave the exception alone
4. If it's false, recursively clear all the already terminated frames
in __traceback__, __context__, and __cause__

For now, we'd default to "__keep_locals__ = True", but we'd attempt to
figure out some way to migrate to the default being "__keep_locals__ =


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list