[Python-Dev] del unexpectedly being called twice (original) (raw)

Duncan Booth duncan.booth at suttoncourtenay.org.uk
Fri Aug 18 10:41:19 CEST 2006


There's a thread on comp.lang.python at the moment under the subject "It is del calling twice for some instances?" which seems to show that when releasing a long chain of old-style classes every 50th approximately has its finaliser called twice. I've verified that this happens on both Python 1.4 and 1.5.

My guess is that there's a bug in the trashcan mechanism: calling the del method means creating a descriptor, and if that descriptor gets queued in the trashcan then releasing it calls the del method a second time. I'm not sure if there is going to be a particularly easy fix for that.

Would someone who knows this code (instance_dealloc in classobject.c) like to have a look at it, should I just submit a bug report, or isn't it worth bothering about?

The code which exhibits the problem:

#!/usr/local/bin/python -d

-- coding: koi8-u --

import sys

class foo: def init(self, other): self.other = other self._deleted = False

    global ini_cnt
    ini_cnt +=1

def __del__(self):
    if self._deleted:
        print "aargh!"
    self._deleted = True
    global del_cnt
    del_cnt +=1
    print "del",del_cnt,"at",id(self)

def stat(): print "-"*20 print "ini_cnt = %d" % ini_cnt print "del_cnt = %d" % del_cnt print "difference = %d" % (ini_cnt-del_cnt)

ini_cnt = 0 del_cnt = 0 loop_cnt = 55

a = foo(None)

for i in xrange(loop_cnt): a = foo(a)

stat() a = None stat()

The original thread is at: http://groups.google.com/group/comp.lang.python/browse_thread/thread/293acf433a39583b/bfd4af9c6008a34e



More information about the Python-Dev mailing list