[Zope-dev] Detecting object deletions

Jim Fulton jim@digicool.com
Sat, 15 May 1999 15:07:20 +0000


Robert Leftwich wrote:
> 
> How do I detect if an object is being deleted ?
> I need to detect when an object is being deleted in order to perform some clean up.
> 
> The deal is (excuse the highly imaginative names) :
> 
> Foo inherits from Bar which inherits from Folder
> Foo has a __del__ method which prints 'Foo is being deleted'
> When an instance of Foo is deleted (via manage_delObjects) I would expect __del__ to be called at some point but nothing happens.
> 
> Am I overlooking something obvious ?

No, you are unaware of something subtle. :)

This is a good question BTW.

The standard Python __del__ method is called whan an object
is removed from memory.  For non-persistent objects, this corresponds
to the object's "death".  Persistent objects may move in and out
of memory many times in their lifetimes.  (With ZODB3, they may
have many copies in memory simultaneously.)  In this case, __del__
has little to do with an object's life or it's connection to
it's container.

We just added a protocol for notifying an object when it is
being deleted from an Object Manager (e.g. Folder):

  _on_delete_object()

Object managers call this method just before deleting objects.
This protocol is present in the publich CVS source and will be 
in the upcoming Zope 2 alpha release. (The current default 
implementation actually extends this protcol in a way that
will certainly change, so don't pay attention to it.)

Note that this method is *not* a destructor.  It is only called
when an object is removed from a collection.  An object may be 
in multiple collections or referenced in other ways.  Even if 
an object is only referenced in a collection, it is not removed
from the database until the next pack. 

(When an object is removed from the database during packing, 
 it's record is simply removed.  No method gets called on the
 object.  Old objects don't die, they just fade away. ;)

Jim

--
Jim Fulton           mailto:jim@digicool.com
Technical Director   (540) 371-6909              Python Powered!
Digital Creations    http://www.digicool.com     http://www.python.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.