-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C API] Add Py_NewRef() function to get a new strong reference to an object #86428
Comments
In C, the following pattern (A) is very common: Py_INCREF(sysdict);
interp->sysdict = sysdict;
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:
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 |
I'm talking about the variable name which has to be repeated (written twice) in the source code. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: