[Python-Dev] Cycle collection enhancement idea

eyal.lotem+pyutils@gmail.com eyal.lotem at gmail.com
Sun Jun 29 13:26:08 CEST 2008


On Jun 28, 6:21 pm, Nick Coghlan <ncogh... at gmail.com> wrote:
> Eyal Lotem wrote:
> > Example:
>
> > import os
> > class RunningFile(object):
> >     filename = '/tmp/running'
> >     def __init__(self):
> >         open(self.filename, 'wb')
> >     def __del__(self):
> >         os.unlink(self.filename)
> > running_file = RunningFile()
>
> > The deller object is in a cycle as described above [as well as the
> > Deller class itself].  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
>
> I don't know what you're trying to get at with this example. There isn't
> any cyclic GC involved at all, just referencing counting. And before the
> module globals are cleared, running_file is still referenced, so calling
> its __del__ method early would be an outright error in the interpreter
> (as far as I know, getting __del__ methods to run is one of the
> *reasons* for clearing the module globals).
>
> It's a fact of Python development: __del__ methods cannot safely
> reference module globals, because those globals may be gone by the time
> that method is invoked.
That's because globals are being cleaned up in an ad-hoc manner,
instead of just using cyclic GC that supports cycles.

>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncogh... at gmail.com   |   Brisbane, Australia
> ---------------------------------------------------------------
>              http://www.boredomandlaziness.org
> _______________________________________________
> Python-Dev mailing list
> Python-... at python.orghttp://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:http://mail.python.org/mailman/options/python-dev/python-dev2-garchiv...


More information about the Python-Dev mailing list