[Python-Dev] Weak references: dereference notification

Jim Fulton jim at zope.com
Wed Nov 9 17:50:44 CET 2005


Gustavo J. A. M. Carneiro wrote:
>   Hello,
> 
>   I have come across a situation where I find the current weak
> references interface for extension types insufficient.
> 
>   Currently you only have a tp_weaklistoffset slot, pointing to a
> PyObject with weak references.  However, in my case[1] I _really_ need
> to be notified when a weak reference is dereferenced.  What happens now
> is that, when you call a weakref object, a simple Py_INCREF is done on
> the referenced object.  It would be easy to implement a new slot to
> contain a function that should be called when a weak reference is
> dereferenced.  Or, alternatively, a slot or class attribute that
> indicates an alternative type that should be used to create weak
> references: instead of the builtin weakref object, a subtype of it, so
> you can override tp_call.
> 
>   Does this sounds acceptable?

Since you can now (as of 2.4) subclass the weakref.ref class, you should be able to
do this yourself in Python.  See for example, weakref.KeyedRef.

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Python-Dev mailing list