diff options
| author | Victor Stinner <vstinner@python.org> | 2020-02-05 13:12:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-05 13:12:19 +0100 |
| commit | f58bd7c1693fe041f7296a5778d0a11287895648 (patch) | |
| tree | 066423d4c91278ace5f1f9159436fc2917ed2af8 /Include/cpython | |
| parent | 0fa4f43db086ac3459811cca4ec5201ffbee694a (diff) | |
| download | cpython-git-f58bd7c1693fe041f7296a5778d0a11287895648.tar.gz | |
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()
Diffstat (limited to 'Include/cpython')
| -rw-r--r-- | Include/cpython/object.h | 16 | ||||
| -rw-r--r-- | Include/cpython/objimpl.h | 33 |
2 files changed, 49 insertions, 0 deletions
diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 3c4bf5bb59..e36f824eeb 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -6,6 +6,22 @@ extern "C" { #endif +PyAPI_FUNC(void) _Py_NewReference(PyObject *op); + +#ifdef Py_TRACE_REFS +/* Py_TRACE_REFS is such major surgery that we call external routines. */ +PyAPI_FUNC(void) _Py_ForgetReference(PyObject *); +#endif + +/* Update the Python traceback of an object. This function must be called + when a memory block is reused from a free list. */ +PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op); + +#ifdef Py_REF_DEBUG +PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void); +#endif + + /********************* String Literals ****************************************/ /* This structure helps managing static strings. The basic usage goes like this: Instead of doing diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h index f121922bc4..3f148146f6 100644 --- a/Include/cpython/objimpl.h +++ b/Include/cpython/objimpl.h @@ -6,6 +6,39 @@ extern "C" { #endif +/* 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(). + + These inline functions must not be called with op=NULL. */ +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)) + + /* This function returns the number of allocated memory blocks, regardless of size */ PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); |
