[issue45241] python REPL leaks local variables when an exception is thrown

Ronald Oussoren report at bugs.python.org
Sun Sep 19 04:36:23 EDT 2021


Ronald Oussoren <ronaldoussoren at mac.com> added the comment:

I'd expect that there'd be at least one instance of X alive due to sys.last_value and in particular sys.last_traceback (which has a reference to the stack frame which contains references to local variables).

Btw. I cannot reproduce this with Python 3.9.7 installed with the Python.org installer (on MacOS 11):

Python 3.9.7 (v3.9.7:1016ef3790, Aug 30 2021, 16:25:35) 
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin


When I introduce a reference loop the number of X() instances does grow, but only until the cyclic garbage collector runs (for example when gc.collect is called).

I used slightly tweaked code to investigate:


class Y:
    pass
class X:
    def __init__(self):
        self.y = Y()
        self.y.x = self
    pass

def dump():
    import gc
#    gc.collect()
    objs = gc.get_objects()
    for obj in objs:
        if isinstance(obj, X):
            print(obj)

def f():
    x = X()
    raise Exception()

f()

----------
nosy: +ronaldoussoren

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


More information about the Python-bugs-list mailing list