Message380337
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) |
|
Date |
User |
Action |
Args |
2020-11-04 16:20:34 | vstinner | set | recipients:
+ vstinner |
2020-11-04 16:20:34 | vstinner | set | messageid: <1604506834.94.0.230929451807.issue42262@roundup.psfhosted.org> |
2020-11-04 16:20:34 | vstinner | link | issue42262 messages |
2020-11-04 16:20:34 | vstinner | create | |
|