Re: Puzzling behaviour of Py_IncRef

2022-01-27 Thread Barry Scott
> On 27 Jan 2022, at 07:46, Tony Flury wrote: > > > On 26/01/2022 22:41, Barry wrote: >> >> >> Run python and your code under a debugger and check the ref count of the >> object as you step through the code. >> >> Don’t just step through your code but also step through the C python code.

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Tony Flury via Python-list
On 26/01/2022 22:41, Barry wrote: Run python and your code under a debugger and check the ref count of the object as you step through the code. Don’t just step through your code but also step through the C python code. That will allow you to see how this works at a low level. Setting a

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Barry
> On 25 Jan 2022, at 23:50, Tony Flury wrote: > >  > > >> On 25/01/2022 22:28, Barry wrote: >> On 25 Jan 2022, at 14:50, Tony Flury via Python-list wrote:  > On 20/01/2022 23:12, Chris Angelico wrote: >>> On Fri, 21 Jan 2022 at 10:10, Greg Ewing >>>

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Greg Ewing
The convention for refcounting in CPython is that a function takes borrowed references as arguments and returns a new reference. The 'self' argument passed in is a borrowed reference. If you want to return it, you need to create a new reference by increfing it. So what you have written is just

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Tony Flury via Python-list
On 26/01/2022 08:20, Chris Angelico wrote: On Wed, 26 Jan 2022 at 19:04, Tony Flury via Python-list wrote: So according to that I should increment twice if and only if the calling code is using the result - which you can't tell in the C code - which is very odd behaviour. No, the return

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Chris Angelico
On Wed, 26 Jan 2022 at 19:04, Tony Flury via Python-list wrote: > > So according to that I should increment twice if and only if the calling > code is using the result - which you can't tell in the C code - which is > very odd behaviour. No, the return value from your C function will *always*

Re: Puzzling behaviour of Py_IncRef

2022-01-26 Thread Tony Flury via Python-list
On 26/01/2022 01:29, MRAB wrote: On 2022-01-25 23:50, Tony Flury via Python-list wrote: On 25/01/2022 22:28, Barry wrote: On 25 Jan 2022, at 14:50, Tony Flury via Python-list  wrote:  On 20/01/2022 23:12, Chris Angelico wrote: On Fri, 21 Jan 2022 at 10:10, Greg Ewing  wrote: On

Re: Puzzling behaviour of Py_IncRef

2022-01-25 Thread MRAB
On 2022-01-25 23:50, Tony Flury via Python-list wrote: On 25/01/2022 22:28, Barry wrote: On 25 Jan 2022, at 14:50, Tony Flury via Python-list wrote:  On 20/01/2022 23:12, Chris Angelico wrote: On Fri, 21 Jan 2022 at 10:10, Greg Ewing wrote: On 20/01/22 12:09 am, Chris Angelico wrote:

Re: Puzzling behaviour of Py_IncRef

2022-01-25 Thread Tony Flury via Python-list
On 25/01/2022 22:28, Barry wrote: On 25 Jan 2022, at 14:50, Tony Flury via Python-list wrote:  On 20/01/2022 23:12, Chris Angelico wrote: On Fri, 21 Jan 2022 at 10:10, Greg Ewing wrote: On 20/01/22 12:09 am, Chris Angelico wrote: At this point, the refcount has indeed been increased.

Re: Puzzling behaviour of Py_IncRef

2022-01-25 Thread Barry
> On 25 Jan 2022, at 14:50, Tony Flury via Python-list > wrote: > >  >> On 20/01/2022 23:12, Chris Angelico wrote: >>> On Fri, 21 Jan 2022 at 10:10, Greg Ewing >>> wrote: >>> On 20/01/22 12:09 am, Chris Angelico wrote: At this point, the refcount has indeed been increased. >

Re: Puzzling behaviour of Py_IncRef

2022-01-25 Thread Tony Flury via Python-list
On 20/01/2022 23:12, Chris Angelico wrote: On Fri, 21 Jan 2022 at 10:10, Greg Ewing wrote: 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

Re: Puzzling behaviour of Py_IncRef

2022-01-20 Thread Chris Angelico
On Fri, 21 Jan 2022 at 10:10, Greg Ewing wrote: > > 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

Re: Puzzling behaviour of Py_IncRef

2022-01-20 Thread Greg Ewing
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

Re: Puzzling behaviour of Py_IncRef

2022-01-19 Thread Barry Scott
> On 19 Jan 2022, at 10:57, Tony Flury via Python-list > wrote: > > I am writing a C extension module for an AVL tree, and I am trying to ensure > reference counting is done correctly. I was having a problem with the > reference counting so I worked up this little POC of the problem, and I

Re: Puzzling behaviour of Py_IncRef

2022-01-19 Thread Chris Angelico
On Thu, Jan 20, 2022 at 1:22 AM Tony Flury wrote: > > > On 19/01/2022 11:09, Chris Angelico wrote: > > On Wed, Jan 19, 2022 at 10:00 PM Tony Flury via Python-list > > wrote: > >> Extension function : > >> > >> static PyObject *_Node_test_ref_count(PyObject *self) > >> { > >>

Re: Puzzling behaviour of Py_IncRef

2022-01-19 Thread Tony Flury via Python-list
On 19/01/2022 11:09, Chris Angelico wrote: On Wed, Jan 19, 2022 at 10:00 PM Tony Flury via Python-list wrote: Extension function : static PyObject *_Node_test_ref_count(PyObject *self) { printf("\nIncrementing ref count for self - just for the hell of it\n");

Re: Puzzling behaviour of Py_IncRef

2022-01-19 Thread Chris Angelico
On Wed, Jan 19, 2022 at 10:00 PM Tony Flury via Python-list wrote: > Extension function : > > static PyObject *_Node_test_ref_count(PyObject *self) > { > printf("\nIncrementing ref count for self - just for the hell > of it\n"); > printf("\n before self has a ref

Puzzling behaviour of Py_IncRef

2022-01-19 Thread Tony Flury via Python-list
I am writing a C extension module for an AVL tree, and I am trying to ensure reference counting is done correctly. I was having a problem with the reference counting so I worked up this little POC of the problem, and I hope someone can explain this. Extension function : static PyObject