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

https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

"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:
    foo

was translated to

except E as N:
    try:
        foo
    finally:
        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.
History
Date User Action Args
2020-02-29 01:26:00terry.reedysetrecipients: + terry.reedy, wangjie
2020-02-29 01:26:00terry.reedysetmessageid: <1582939560.94.0.360207315872.issue39782@roundup.psfhosted.org>
2020-02-29 01:26:00terry.reedylinkissue39782 messages
2020-02-29 01:26:00terry.reedycreate