[Python-Dev] Cycle collection enhancement idea

Nick Coghlan ncoghlan at gmail.com
Sat Jun 28 17:21:19 CEST 2008


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.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list