[Python-Dev] Weak references

est@hyperreal.org est@hyperreal.org
Sun, 5 Nov 2000 20:27:52 -0800 (PST)

Guido van Rossum discourseth:
> [MAL]
> > These already exist... http://www.handshake.de/~dieter/weakdict.html
> > 
> > mx.Proxy also has an implementation which support weak references.
> Thanks.  For Fred to read...

He may want to also see my http://www.hyperreal.org/~est/python/weak
(also registered in the vaults), an implementation of weak dicts and
proxy maps that doesn't require the contained objects to be
subclassed.  It even has documentation in standard format (!)
including some words about motivation.

One simple implementation approach to make it work for all objects
might be to have:

int PyObject_PushWeakHandler(PyObject *o,
                             void (*handler)(PyObject *o, PyObject *data),
                             PyObject *data);

When an object is deallocated all its handlers would be called on the
object and the data that was registered with the handler.  I believe
this would make weak dicts a very simple extension module.  With this
approach I suspect DATA should be incref'd by the call to
PyObject_PushWeakHandler() and decref'd after the associated handler
is called.