472,952 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 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 1322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
8
by: Morpheus | last post by:
Hello, Say I have a class with a member... char mId; Whenever an object is created, I want to assign an incrementing character to this member, for instance the first would be A, then B, C,...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.