global destructor not called?

Duncan Booth duncan.booth at invalid.invalid
Mon Jun 18 04:00:57 EDT 2007


"bruno.desthuilliers at gmail.com" <bruno.desthuilliers at gmail.com> wrote:

>> >>     def __del__ (self):
>> >>         try:
>> >>             os.remove (self.name + '.old')
>> >>         except:
>> >>             pass
>>
>> >> And setting:
>> >> sys.stderr = logger(...)
>>
>> >> It seems my cleanup (__del__) is never called,
>>
...
> 
> Mmm... If I read the language's references, I see this:
> 
> """
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
> """
> http://docs.python.org/ref/customization.html
> 
> Hopefully you fuond the right way to ensure correct clean-up !-)
> 
> (damn, I knew I rembered something special about destructors... but I
> couldn't remember exactly what.)
> 
I don't think you can tell from this description whether the __del__ method 
was called or not.

Even if sys.stderr is destroyed before Python exits and the __del__ method 
is called the OP will still get the effect described: the global variables 
in the module defining logger will have been cleared before stderr is 
destroyed, so __del__ will simply throw an NameError when trying to access 
os.



More information about the Python-list mailing list