[C++-sig] rfe: change of behaviour with return_internal_reference policy

gideon may gideon at computer.org
Mon Aug 19 16:27:25 CEST 2002


--On Monday, August 19, 2002 9:25 AM -0400 David Abrahams 
<dave at boost-consulting.com> wrote:

>
> -----------------------------------------------------------
>            David Abrahams * Boost Consulting
> dave at boost-consulting.com * http://www.boost-consulting.com
>
>
> ----- Original Message -----
> From: "gideon may" <gideon at computer.org>
> To: "pysig" <c++-sig at python.org>
> Sent: Monday, August 19, 2002 9:10 AM
> Subject: [C++-sig] rfe: change of behaviour with return_internal_reference
> policy
>
>
>> Dear Dave,
>>


>> Unfortunately it doesn't work, because of the weak references being used
>> with return_internal_reference.
>> It seems that within make_nurse_and_patient (life_support.cpp), a weak
>> reference is being
>> created between the result and the originator.
>
> No, not exactly. We're creating a weak reference from the result to a
> life_support object, which holds a reference to the originator and to the
> weak reference. It's a sick little trick which keeps the originator alive
> as long as the result is.

The internal working evaded me but now i understand :-)

>
>> In my case the result can be
>> a NULL pointer,
>
> Which gets translated to None.

Right.

>
>> failing
>> the PyWeakref_NewRef function.
>
> Ah yes; None doesn't support weak references.
>


> However, we /could/ check explicitly for None, since that's going to stay
> alive no matter what we do.
> I think in the case that's failing for you, both the nurse and patient
> parameters in make_nurse_and_patient are PyNone, right?

Actually, you're right, that's what I had before. But only the nurse needs
to be checked for PyNone, the patient is the 'self' parameter in toGroup 
afaik,
which is guaranteed to be not PyNone. Thus I guess that it suffices to 
change the
beginning of the function to :

if (nurse == Py_None) {
    Py_INCREF(Py_None);
    return Py_None;
}

>
>> This brings me to a second point, it seems that life_support_dealloc is
>> never called.
>> If it would,
>> the tp_free would be called for self, which is not initialized ie a NULL
>> function pointer.
>
> Really? Hmm, I'm not sure about that. Which fields in an extension type
> are filled in automatically by Python and when they're called remains a
> little mysterious to me, I have to admit.
>

I stumbled upon this because of my previous point. Without the mentioned 
change,
PyWeakref_Newref returns a NULL. This causes 'system' to be Py_XDEFREF'd -> 
crash in
life_support_dealloc (line 24). But somehow the the life_support_dealloc 
function is never
being called. I did some investigating in the functioning of weak 
references (esp the function
PyWeakref_NewRef) and it seems that the referent is being Py_XINCREF'd by 
default. Don'
know why but don't feel like spending too much time understanding.

BTW, am enjoying BPL more and more! Any news on enum_ <wink> ?





More information about the Cplusplus-sig mailing list