global destructor not called?

N

Neal Becker

To implement logging, I'm using a class:
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, even though I believe my
program exits normally. What's wrong?
 
B

Bruno Desthuilliers

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 ?
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 ?
even though I believe my
program exits normally. What's wrong?

Not enough data...
 
N

Neal Becker

Bruno said:
Neal Becker a écrit :

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

bruno.desthuilliers

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

Duncan Booth

....

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top