Author terry.reedy
Recipients terry.reedy, wangjie
Date 2020-02-29.01:26:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
For beginners, 'is this a Python bug' questions should usually be directed elsewhere for initial review.

"When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

except E as N:

was translated to

except E as N:
        del N

This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs."

"l['e']" is the first new name.  "b" in each locals is the second.  Without "b = ..." there is no increase because l['e'] gets replaced on each call.  But inserting each exception into the next locals (see "keeping all locals in that frame alive") in effect makes a linked list of frames and locals.

If one wants to keep multiple exceptions around, best to copy just the info one want kept.
Date User Action Args
2020-02-29 01:26:00terry.reedysetrecipients: + terry.reedy, wangjie
2020-02-29 01:26:00terry.reedysetmessageid: <>
2020-02-29 01:26:00terry.reedylinkissue39782 messages
2020-02-29 01:26:00terry.reedycreate