473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1335
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.