[Python-Dev] Cycle collection enhancement idea
"Martin v. Löwis"
martin at v.loewis.de
Sat Jun 28 17:32:08 CEST 2008
> import os
> class RunningFile(object):
> filename = '/tmp/running'
> def __init__(self):
> open(self.filename, 'wb')
> def __del__(self):
> running_file = RunningFile()
> The deller object is in a cycle as described above [as well as the
> Deller class itself].
I think you are mistaken here. The RunningFile instance in above code
is *not* part of a cycle. It doesn't have any instance variables (i.e.
its __dict__ is empty), and it only refers to its class, which (AFAICT)
doesn't refer back to the instance.
> When Python exits, it could call
> deller.__del__() and then collect the cycle. But Python does the wrong
> thing here, and gets rid of the globals before calling __del__:
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute 'unlink'" in <bound method RunningFile.__del__ of
> <__main__.RunningFile object at 0x7f9655eb92d0>> ignored
This is a different issue. For shutdown, Python doesn't rely on
cyclic garbage collection (only). Instead, all modules get forcefully
cleared, causing this problem.
> I believe applying the above enhancement would solve these problems.
No, they wouldn't.
To work around the real problem in your case, put everything that the
destructor uses into an instance or class attribute:
filename = '/tmp/running'
_unlink = os.unlink
More information about the Python-Dev