Puzzling behaviour of Py_IncRef

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Jan 20 17:33:19 EST 2022


On 20/01/22 12:09 am, Chris Angelico wrote:
> At this point, the refcount has indeed been increased.
> 
>>           return self;
>>      }
> 
> And then you say "my return value is this object".
> 
> So you're incrementing the refcount, then returning it without
> incrementing the refcount. Your code is actually equivalent to "return
> self".

Chris, you're not making any sense. This is C code, so there's no
way that "return x" can change the reference count of x.

 > The normal thing to do is to add a reference to whatever you're
 > returning. For instance, Py_RETURN_NONE will incref None and then
 > return it.
 >

The OP understands that this is not a normal thing to do. He's
trying to deliberately leak a reference for the purpose of diagnosing
a problem.

It would be interesting to see what the actual refcount is after
calling this function.

-- 
Greg


More information about the Python-list mailing list