[Python-Dev] Weak references: dereference notification

Ronald Oussoren ronaldoussoren at mac.com
Wed Nov 9 20:40:02 CET 2005


On 9-nov-2005, at 18:52, Gustavo J. A. M. Carneiro wrote:

> Qua, 2005-11-09 às 09:23 -0800, Guido van Rossum escreveu:
>>>> Gustavo J. A. M. Carneiro wrote:
>>>>>   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.
>>
>> I find reading through the bug discussion a bit difficult to
>> understand your use case. Could you explain it here? If you can't
>> explain it you certainly won't get your problem solved! :-)
>
>   This is a typical PyObject wrapping C object (GObject) problem.   
> Both
> PyObject and GObject have independent reference counts.  For each
> GObject there is at most one PyObject wrapper.
>
>   When the refcount on the wrapper drops to zero, tp_dealloc is  
> called.
> In tp_dealloc, and if the GObject refcount is > 1, I do something
> slightly evil: I 'resurect' the PyObject (calling PyObject_Init),  
> create
> a weak reference to the GObject, and drop the "strong" reference.  I
> call this a 'hibernation state'.

Why do you do that? The only reasons I can think of are that you hope  
to gain
some speed from this or that you want to support weak references to  
the GObject.

For what its worth, in PyObjC we don't support weak references to the  
underlying
Objective-C object and delete the proxy object when it is garbage  
collected.
Objective-C also has reference counts, we increase that in the  
constructor for
the proxy object and decrease it again in the destroctor.

Ronald


More information about the Python-Dev mailing list