diff options
author | Victor Stinner <vstinner@python.org> | 2022-04-21 14:52:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-21 14:52:54 (GMT) |
commit | 2a5f171759a31597032cfe52646929e6f8727243 (patch) | |
tree | 0ef894868fc9e6dec266dd9379c52610c60405bc | |
parent | 1b184c84082530f35c593cb7728752e258371c30 (diff) | |
download | cpython-2a5f171759a31597032cfe52646929e6f8727243.zip cpython-2a5f171759a31597032cfe52646929e6f8727243.tar.gz cpython-2a5f171759a31597032cfe52646929e6f8727243.tar.bz2 |
gh-89653: PEP 670: Convert tuple macros to functions (#91786)
Convert macros to static inline functions:
* PyTuple_GET_SIZE()
* PyTuple_SET_ITEM()
* PyList_GET_SIZE()
* PyList_SET_ITEM()
Add a macro converting arguments to PyTupleObject*, PyListObject* or
PyObject* to prevent emitting new compiler warnings.
According to PEP 670, PyTuple_GET_ITEM() and PyList_GET_ITEM() are
left as macros.
-rw-r--r-- | Include/cpython/listobject.h | 20 | ||||
-rw-r--r-- | Include/cpython/tupleobject.h | 20 |
2 files changed, 29 insertions, 11 deletions
diff --git a/Include/cpython/listobject.h b/Include/cpython/listobject.h index 51687d8..0cd6921 100644 --- a/Include/cpython/listobject.h +++ b/Include/cpython/listobject.h @@ -24,11 +24,21 @@ typedef struct { PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *); PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out); -/* Macro, trading safety for speed */ - /* Cast argument to PyListObject* type. */ #define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op)) -#define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i]) -#define PyList_SET_ITEM(op, i, v) _Py_RVALUE(_PyList_CAST(op)->ob_item[i] = (v)) -#define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op)) +// Macros and static inline functions, trading safety for speed + +static inline Py_ssize_t PyList_GET_SIZE(PyListObject *op) { + return Py_SIZE(op); +} +#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyList_CAST(op)) + +#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index]) + +static inline void +PyList_SET_ITEM(PyListObject *op, Py_ssize_t index, PyObject *value) { + op->ob_item[index] = value; +} +#define PyList_SET_ITEM(op, index, value) \ + PyList_SET_ITEM(_PyList_CAST(op), index, _PyObject_CAST(value)) diff --git a/Include/cpython/tupleobject.h b/Include/cpython/tupleobject.h index fc37c4e..fd15ecd 100644 --- a/Include/cpython/tupleobject.h +++ b/Include/cpython/tupleobject.h @@ -13,16 +13,24 @@ typedef struct { PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, Py_ssize_t); PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *); -/* Macros trading safety for speed */ - /* Cast argument to PyTupleObject* type. */ #define _PyTuple_CAST(op) (assert(PyTuple_Check(op)), (PyTupleObject *)(op)) -#define PyTuple_GET_SIZE(op) Py_SIZE(_PyTuple_CAST(op)) +// Macros and static inline functions, trading safety for speed + +static inline Py_ssize_t PyTuple_GET_SIZE(PyTupleObject *op) { + return Py_SIZE(op); +} +#define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyTuple_CAST(op)) -#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i]) +#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index]) -/* Macro, *only* to be used to fill in brand new tuples */ -#define PyTuple_SET_ITEM(op, i, v) _Py_RVALUE(_PyTuple_CAST(op)->ob_item[i] = (v)) +/* Function *only* to be used to fill in brand new tuples */ +static inline void +PyTuple_SET_ITEM(PyTupleObject *op, Py_ssize_t index, PyObject *value) { + op->ob_item[index] = value; +} +#define PyTuple_SET_ITEM(op, index, value) \ + PyTuple_SET_ITEM(_PyTuple_CAST(op), index, _PyObject_CAST(value)) PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out); |