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? 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...
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...
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.)
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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"...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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() }
}
|
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) {...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |