bpo-39542: Make PyObject_INIT() opaque in limited C API (GH-18363)

In the limited C API, PyObject_INIT() and PyObject_INIT_VAR() are now
defined as aliases to PyObject_Init() and PyObject_InitVar() to make
their implementation opaque. It avoids to leak implementation details
in the limited C API.

Exclude the following functions from the limited C API, move them
from object.h to cpython/object.h:

* _Py_NewReference()
* _Py_ForgetReference()
* _PyTraceMalloc_NewReference()
* _Py_GetRefTotal()
This commit is contained in:
Victor Stinner 2020-02-05 13:12:19 +01:00 committed by GitHub
parent 0fa4f43db0
commit f58bd7c169
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 51 deletions

View file

@ -127,41 +127,22 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
#define PyObject_NewVar(type, typeobj, n) \
( (type *) _PyObject_NewVar((typeobj), (n)) )
/* Inline functions trading binary compatibility for speed:
PyObject_INIT() is the fast version of PyObject_Init(), and
PyObject_INIT_VAR() is the fast version of PyObject_InitVar.
See also pymem.h.
These inline functions expect non-NULL object pointers. */
static inline PyObject*
_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
{
assert(op != NULL);
Py_TYPE(op) = typeobj;
if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(typeobj);
}
_Py_NewReference(op);
return op;
}
#define PyObject_INIT(op, typeobj) \
_PyObject_INIT(_PyObject_CAST(op), (typeobj))
static inline PyVarObject*
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
{
assert(op != NULL);
Py_SIZE(op) = size;
PyObject_INIT((PyObject *)op, typeobj);
return op;
}
#define PyObject_INIT_VAR(op, typeobj, size) \
_PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
#ifdef Py_LIMITED_API
/* Define PyObject_INIT() and PyObject_INIT_VAR() as aliases to PyObject_Init()
and PyObject_InitVar() in the limited C API for compatibility with the
CPython C API. */
# define PyObject_INIT(op, typeobj) \
PyObject_Init(_PyObject_CAST(op), (typeobj))
# define PyObject_INIT_VAR(op, typeobj, size) \
PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
#else
/* PyObject_INIT() and PyObject_INIT_VAR() are defined in cpython/objimpl.h */
#endif
/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
vrbl-size object with nitems items, exclusive of gc overhead (if any). The
value is rounded up to the closest multiple of sizeof(void *), in order to