Re: Logging in __del__()

2008-07-16 Thread Marc 'BlackJack' Rintsch
On Wed, 16 Jul 2008 12:38:50 +0100, Robert Rawlins wrote:

 So, am I right to assume that python will still handle its garbage disposal
 if I implement __del__(), it just handles circular references in a slightly
 different way, but to the same effect. Right?

No.  Circular references in objects with a `__del__()` implementation are
not collected!  Why are you using `__del__()` anyway?  Are you aware of
the promises that are *not* made!  It's not guaranteed when the method
is called nor if it is called at all!

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Logging in __del__()

2008-07-15 Thread Robert Rawlins
Guys,

 

I'm trying to help trace when instances of particular classes are being
destroyed by the garbage collector and thought the cleanest way would be to
implement a logging call in __del__() on the class. However, I'm having an
issue.

 

I inject a logger instance into my class upon construction and set it to
self.logger, I then use this within the class to log actions going on
within, works fine.

 

Now, when I make the call in the __del__() method like so:

 

def __del__(self):

# Log the classes destruction.

self.logger.debug(Class Instance Destroyed)

 

I then get the following exception thrown when running my code:

 

Traceback (most recent call last):

  File /usr/lib/python2.5/logging/handlers.py, line 73, in emit

if self.shouldRollover(record):

  File /usr/lib/python2.5/logging/handlers.py, line 147, in shouldRollover

self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature

ValueError: I/O operation on closed file

 

Does anyone have any ideas as to what I'm doing wrong here? Is this a known
issue which has a neat little work around?

 

Cheers,

 

Robert

--
http://mail.python.org/mailman/listinfo/python-list

Re: Logging in __del__()

2008-07-15 Thread Fredrik Lundh

Robert Rawlins wrote:


I then get the following exception thrown when running my code:


When the application is running, or when it is shutting down?


Traceback (most recent call last):
  File /usr/lib/python2.5/logging/handlers.py, line 73, in emit
if self.shouldRollover(record):
  File /usr/lib/python2.5/logging/handlers.py, line 147, in shouldRollover
self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
ValueError: I/O operation on closed file

Does anyone have any ideas as to what I’m doing wrong here? Is this a 
known issue which has a neat little work around?


Python makes no guarantees that it will tear down your objects before it 
tears down the library's objects (or the library itself).  See e.g.


http://www.python.org/doc/essays/cleanup/

(old and probably somewhat outdated, but you get the idea)

I'd just put a try/except around it, and ignore any exceptions that may 
occur.


/F

--
http://mail.python.org/mailman/listinfo/python-list


Re: Logging in __del__()

2008-07-15 Thread Vinay Sajip
On Jul 15, 1:51 pm, Robert Rawlins
[EMAIL PROTECTED] wrote:

 Am I right in thinking that Python destroys instances of classes when it
 deems they are no longer needed? I shouldn't have to explicitly delete the
 classes, right?

Python uses reference counting with a cycle detector, but the
detector's behaviour is different if there are finalizers  (__del__) -
see

http://www.python.org/doc/ext/refcounts.html

Regards,

Vinay Sajip
--
http://mail.python.org/mailman/listinfo/python-list