global destructor not called?

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Sun Jun 17 17:05:13 EDT 2007


On Jun 15, 7:07 pm, Neal Becker <ndbeck... at gmail.com> wrote:
> Bruno Desthuilliers wrote:
> > Neal Becker a écrit :
> >> To implement logging, I'm using a class:
>
> > If I may ask : any reason not to use the logging module in the stdlib ?
>
> Don't exactly recall, but needed some specific behavior and it was just
> easier this way.
>

Ok, that's was just in case you didn't know about this module...

>
>
>
> >> class logger (object):
> >>     def __init__ (self, name):
> >>         self.name = name
> >>         self.f = open (self.name, 'w')
> >>     def write (self, stuff):
> >>         self.f.write (stuff)
> >>     def close (self):
> >>         self.f.close()
> >>     def flush (self):
> >>         self.f.flush()
> >>     def reopen (self):
> >>         self.f.flush()
> >>         self.f.close()
> >>         os.rename (self.name, self.name + '.old')
> >>         self.f = open (self.name, 'w')
> >>     def __del__ (self):
> >>         try:
> >>             os.remove (self.name + '.old')
> >>         except:
> >>             pass
>
> >> And setting:
> >> sys.stderr = logger(...)
>
> >> It seems my cleanup (__del__) is never called,
>
> > What makes you think so ?
>
> Cleanup should remove file file '.old',
> and it wasn't removed.
>  Adding
> atexit.register (self.__del__) to the logger constructor DID fix it.
>

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.)




More information about the Python-list mailing list