471,310 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

global destructor not called?

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?

Jun 15 '07 #1
4 1278
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...
Jun 15 '07 #2
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.
>
>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.
>
>even though I believe my
program exits normally. What's wrong?

Not enough data...

Jun 15 '07 #3
On Jun 15, 7:07 pm, Neal Becker <ndbeck...@gmail.comwrote:
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.)

Jun 17 '07 #4
"br*****************@gmail.com" <br*****************@gmail.comwrote:
> 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.
Jun 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Newsnet Customer | last post: by
11 posts views Thread by Stub | last post: by
1 post views Thread by Nimmi Srivastav | last post: by
7 posts views Thread by Emanuel Ziegler | last post: by
8 posts views Thread by Morpheus | last post: by
35 posts views Thread by Peter Oliphant | last post: by
14 posts views Thread by gurry | last post: by
5 posts views Thread by junw2000 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.