You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C API] Add Py_NewRef() function to get a new strong reference to an object #86428
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2020-11-05.14:11:51.499>created_at=<Date2020-11-04.16:20:34.933>labels= ['expert-C-API', '3.10']
title='[C API] Add Py_NewRef() function to get a new strong reference to an object'updated_at=<Date2020-12-03.13:01:17.892>user='https://github.com/vstinner'
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:
(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:
Activity
vstinner commentedon Nov 4, 2020
In C, the following pattern (A) is very common:
Similar pattern (B) using return:
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:
(The actual implementation is just a little bit more complex, to also export it as a regular function.)
Pattern (A) becomes:
Pattern (B) becomes:
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)
vstinner commentedon Nov 4, 2020
I'm talking about the variable name which has to be repeated (written twice) in the source code.
vstinner commentedon Nov 5, 2020
New changeset 53a03aa by Victor Stinner in branch 'master':
bpo-42262: Add Py_NewRef() and Py_XNewRef() (GH-23152)
53a03aa
vstinner commentedon Dec 3, 2020
New changeset 8b6c4a9 by Victor Stinner in branch 'master':
bpo-42262: Py_NewRef() casts its argument to PyObject* (GH-23626)
8b6c4a9