C API: Remove PyObject_Init() return value · Issue #105944 · python/cpython (original) (raw)
PyObject_Init(), PyObject_INIT(), PyObject_InitVar() and PyObject_INIT_VAR() return their argument which is technically a borrowed reference. For me, this API is really surprising. Why would someone like to rely on the return value, since the result is already known: it's the input argument?
I propose to remove the return value of these 4 PyObject "INIT" functions.
On PyPI top 5,000 projects (2023-04-13), I found 3 projects using these functions, but none are affected by proposed change:
- Cython (0.29.34)
- pyjson5 (1.6.2)
- boost-python copy by scipy 1.10.1
I searched for PyObject_INIT
regex.
Cython is not affected, since it already ignores the result explicitly:
code.putln("(void) PyObject_INIT(o, t);")
There is also boost-python (used by scipy 1.10.1) which defines PyObject_INIT() if it's not defined... for Python 1.x :-) Python 3 is not affected.
#if !defined(PY_MAJOR_VERSION) || PY_MAJOR_VERSION < 2
define PyObject_INIT(op, typeobj) \
( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
#endif
pyjson5 (1.6.2) declares PyObject_INIT()
and PyObject_InitVar()
functions in a Cython binding... without using them. So it's not affected neither.
cdef extern from 'Python.h': # ... object ObjectInit 'PyObject_INIT'(PyObject *obj, type cls) PyVarObject *ObjectInitVar 'PyObject_InitVar'(PyVarObject *obj, type cls, Py_ssize_t size) object PyLong_FromString(const char *str, char **pend, int base) # ...