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.

Author mariocj89
Recipients mariocj89, pablogsal, twouters
Date 2019-05-06.19:32:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
There are multiple places in the standard library where the code captures an exception and reraises it later (outside of the original except).

This is known to cause a cycle as saving the exception has a traceback that eventually points back to the exception, but it is especially problematic as it keeps alive the objects that the user has as well.

This can be reproduced with the following example:

import weakref

class A: pass

ref = None

def x():
    global ref
    cool_var = A()
    ref = weakref.ref(cool_var)
    assert ref()
    except Exception as e:
        ee = e

except Exception:

assert ref() is None

There are places in the standard library that try to get around this. See as an example. This change did not include the exception path though, when the `err` variable is raised, the issue is still present.

This issue is to fix the instances found in, and By either setting the variable to None to remove the name or if it needs to be reraised by using a finally to delete it. This is basically the same that a try except would do, which automagically adds a finally to delete the local name used in the `except X as y`.
There was a discussion with twouters,pablogsal,barry about potentially adding something extra to the exception handling to try to clean this up but it was suggested the best approach is for the user to handle it and maybe a linter to potentially flag it, as there might also be multiple references to the exception being captured. It was also proposed to just change the situations where this happens in the standard library to remove the name if possible.

Note that eventually those objects will just be collected by the gc, it is just an improvement.

I'm preparing a PR for those three modules.
Date User Action Args
2019-05-06 19:32:15mariocj89setrecipients: + mariocj89, twouters, pablogsal
2019-05-06 19:32:15mariocj89setmessageid: <>
2019-05-06 19:32:15mariocj89linkissue36820 messages
2019-05-06 19:32:15mariocj89create