[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-12-03 Thread STINNER Victor


STINNER Victor  added the comment:


New changeset 8b6c4a921af6d5d0a9640211ac93d7886a55a8f3 by Victor Stinner in 
branch 'master':
bpo-42262: Py_NewRef() casts its argument to PyObject* (GH-23626)
https://github.com/python/cpython/commit/8b6c4a921af6d5d0a9640211ac93d7886a55a8f3


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-12-03 Thread STINNER Victor


Change by STINNER Victor :


--
pull_requests: +22494
pull_request: https://github.com/python/cpython/pull/23626

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-05 Thread STINNER Victor


Change by STINNER Victor :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-05 Thread STINNER Victor


STINNER Victor  added the comment:


New changeset 53a03aafd5812018a3821a2e83063fd3d6cd2576 by Victor Stinner in 
branch 'master':
bpo-42262: Add Py_NewRef() and Py_XNewRef() (GH-23152)
https://github.com/python/cpython/commit/53a03aafd5812018a3821a2e83063fd3d6cd2576


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-04 Thread Erlend Egeberg Aasland


Change by Erlend Egeberg Aasland :


--
nosy: +erlendaasland

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-04 Thread Simon Cross


Change by Simon Cross :


--
nosy: +hodgestar

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-04 Thread STINNER Victor


STINNER Victor  added the comment:

> The problem in these patterns is that the object has to be written twice.

I'm talking about the variable name which has to be repeated (written twice) in 
the source code.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-04 Thread STINNER Victor


Change by STINNER Victor :


--
keywords: +patch
pull_requests: +22064
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/23152

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42262] [C API] Add Py_NewRef() function to get a new strong reference to an object

2020-11-04 Thread STINNER Victor


New submission from STINNER Victor :

In C, the following pattern (A) is very common:

Py_INCREF(sysdict);
interp->sysdict = sysdict;

* (1) Increment the reference counter
* (2) Store a reference to the object somewhere

Similar pattern (B) using return:

Py_INCREF(temp);
return temp;

The problem in these patterns is that the object has to be written twice. I 
propose to add a simple new Py_NewRef() function which returns a new strong 
reference. In short, it's just:

static inline PyObject* Py_NewRef(PyObject *obj)
{
Py_INCREF(obj);
return obj;
}

(The actual implementation is just a little bit more complex, to also export it 
as a regular function.)

Pattern (A) becomes:

interp->sysdict = Py_NewRef(sysdict);

Pattern (B) becomes:


return Py_NewRef(temp);

Py_NewRef() might help to prepare a C extension to be converted to HPy which 
uses a HPy_Dup() function. HPy_Dup() is different than "Py_INCREF(obj), obj" 
for subtle reasons. I let you dig into HPy documentation for the rationale:

https://hpy.readthedocs.io/en/latest/api.html#handles

Even if you ignore HPy, Py_NewRef() avoids to repeat the object variable, and 
so makes the code simpler. Simple example:

-#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#define Py_RETURN_NONE return Py_NewRef(Py_None)

--
components: C API
messages: 380337
nosy: vstinner
priority: normal
severity: normal
status: open
title: [C API] Add Py_NewRef() function to get a new strong reference to an 
object
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com