[Python-Dev] Weak references: dereference notification

Gustavo J. A. M. Carneiro gjc at inescporto.pt
Wed Nov 9 18:14:59 CET 2005


Qua, 2005-11-09 às 11:50 -0500, Jim Fulton escreveu:
> 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.

 I know I can subclass it, but it doesn't change anything.  If people
keep writing code like weakref.ref(myobj) instead of myweakref(myobj),
it still won't work.

  I wouldn't want to have to teach users of the library that they need
to use an alternative type; that seldom doesn't work.

  Now, if there was a place in the type that contained information like 

	"for creating weak references of instances of this type, use this
weakref class"

and weakref.ref was smart enough to lookup this type and use it, only
_then_ it could work.

  Thanks,

-- 
Gustavo J. A. M. Carneiro
<gjc at inescporto.pt> <gustavo at users.sourceforge.net>
The universe is always one step beyond logic.



More information about the Python-Dev mailing list