[issue39841] "as" variable in except block deletes local variables with same name

Rémi Lapeyre report at bugs.python.org
Tue Mar 3 16:24:05 EST 2020


Rémi Lapeyre <remi.lapeyre at henki.fr> added the comment:

Hi Alan, this is documented at 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 is because the exception keeps a reference to the code frame that would make a cycle with the locals which would not be destroyed until the next garbage collection.

I think you will need to use a different name in the except clause.

----------
nosy: +remi.lapeyre

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39841>
_______________________________________


More information about the Python-bugs-list mailing list