File Unlocking in __del__ does not work

S

schwerdy

Hello developers!

I'm using Python 2.3.4 under debian Sarge and want to write a small
logger class. My source code reads:

#***************************************************
import sys, time
from fcntl import *
class Log(object):
"""
Very Simple Logger Class
"""
def __init__(self, path):
self.logfile = open(path, 'a')
flock(self.logfile, LOCK_EX | LOCK_NB) # throw exept. if file
locked

def write(self, msg):
self.logfile.write(time.strftime('%Y-%m-%d %H:%M:%S : ') +
str(msg) + '\n')
self.logfile.flush()

def __del__(self):
flock(self.logfile, LOCK_UN)
self.logfile.close()


l = Log('/var/log/myagi.log')
log = l.write

log("bla")
#***************************************************

When I run the script, python says "Exception exceptions.TypeError:
"'NoneType' object is not callable" in <bound method Log.__del__ of
<__main__.Log object at 0x401e238c>> ignored"
It seems, as in the __del__ method, the flock function has gone (a
debug line as "print flock" in the __del__ method prints "None").

does anybody know what is going on?

best regards,
Sebastian 'Schwerdy' Schwerdhoefer
 
T

Terry Reedy

schwerdy said:
#***************************************************
import sys, time
from fcntl import *
class Log(object):
"""
Very Simple Logger Class
"""
def __init__(self, path):
self.logfile = open(path, 'a')
flock(self.logfile, LOCK_EX | LOCK_NB) # throw exept. if file
locked

def write(self, msg):
self.logfile.write(time.strftime('%Y-%m-%d %H:%M:%S : ') +
str(msg) + '\n')
self.logfile.flush()

def __del__(self):
flock(self.logfile, LOCK_UN)
self.logfile.close()


l = Log('/var/log/myagi.log')
log = l.write

log("bla")
#***************************************************

When I run the script, python says "Exception exceptions.TypeError:
"'NoneType' object is not callable" in <bound method Log.__del__ of
<__main__.Log object at 0x401e238c>> ignored"
It seems, as in the __del__ method, the flock function has gone (a
debug line as "print flock" in the __del__ method prints "None").

does anybody know what is going on?

A wild guess: your quoted script does not explicitly delete instance l and
method log (which contains an bound reference to the same object).
Therefore, the object will only be deleted and __del__ invoked as part of
the shutdown cleanup process. In the absence of explicitly registered
cleanup functions, cleanup happens in arbitrary implementation and
version-specific order. It just happens that flock is None'ed before l.
So what happens if you do do an explicit 'del l,log' at the end of your
script?

Note: on Jython, even explicit delete is not enough to trigger the __del__
method. Better to rename __del__ as 'close' and call l.close() to release
the resources.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top