From 019461232fecf82d05240a79ff6ccd03ee31fe19 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 4 Dec 2020 19:51:09 +0100 Subject: [PATCH 1/2] bpo-30459: Convert PyTuple_SET_ITEM() to a function Convert the PyTuple_SET_ITEM(), PyList_SET_ITEM() and PyCell_SET() macros to static inline functions. --- Include/cellobject.h | 12 ++++++++++-- Include/cpython/listobject.h | 12 ++++++++++-- Include/cpython/tupleobject.h | 14 ++++++++++---- .../C API/2020-12-04-19-55-51.bpo-30459.3dmPs5.rst | 2 ++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2020-12-04-19-55-51.bpo-30459.3dmPs5.rst diff --git a/Include/cellobject.h b/Include/cellobject.h index f12aa90a42a8fe..dc5baa4fa4acaf 100644 --- a/Include/cellobject.h +++ b/Include/cellobject.h @@ -19,8 +19,16 @@ PyAPI_FUNC(PyObject *) PyCell_New(PyObject *); PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *); PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *); -#define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref) -#define PyCell_SET(op, v) (((PyCellObject *)(op))->ob_ref = v) +// Cast the argument to PyCellObject* type. +#define _PyCell_CAST(op) (assert(PyCell_Check(op)), (PyCellObject *)(op)) + +#define PyCell_GET(op) (_PyCell_CAST(op)->ob_ref) + +static inline void _PyCell_SET(PyCellObject *cell, PyObject *value) +{ + cell->ob_ref = value; +} +#define PyCell_SET(op, value) _PyCell_SET(_PyCell_CAST(op), value) #ifdef __cplusplus } diff --git a/Include/cpython/listobject.h b/Include/cpython/listobject.h index e1b9462d5b3612..8aacd6f5a921d7 100644 --- a/Include/cpython/listobject.h +++ b/Include/cpython/listobject.h @@ -29,6 +29,14 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out); /* 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) (_PyList_CAST(op)->ob_item[i] = (v)) +#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index]) + +static inline void +_PyList_SET_ITEM(PyListObject *list, Py_ssize_t index, PyObject *item) +{ + list->ob_item[index] = item; +} +#define PyList_SET_ITEM(op, index, item) \ + _PyList_SET_ITEM(_PyList_CAST(op), index, item) + #define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op)) diff --git a/Include/cpython/tupleobject.h b/Include/cpython/tupleobject.h index 51dcd4237be18c..877f0b1e067c94 100644 --- a/Include/cpython/tupleobject.h +++ b/Include/cpython/tupleobject.h @@ -20,9 +20,15 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *); #define PyTuple_GET_SIZE(op) Py_SIZE(_PyTuple_CAST(op)) -#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i]) - -/* Macro, *only* to be used to fill in brand new tuples */ -#define PyTuple_SET_ITEM(op, i, v) (_PyTuple_CAST(op)->ob_item[i] = v) +#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index]) + +// Function *only* to be used to fill in brand new tuples. +static inline void +_PyTuple_SET_ITEM(PyTupleObject *tuple, Py_ssize_t index, PyObject *item) +{ + tuple->ob_item[index] = item; +} +#define PyTuple_SET_ITEM(op, index, item) \ + _PyTuple_SET_ITEM(_PyTuple_CAST(op), index, item) PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out); diff --git a/Misc/NEWS.d/next/C API/2020-12-04-19-55-51.bpo-30459.3dmPs5.rst b/Misc/NEWS.d/next/C API/2020-12-04-19-55-51.bpo-30459.3dmPs5.rst new file mode 100644 index 00000000000000..5c78b2449835ea --- /dev/null +++ b/Misc/NEWS.d/next/C API/2020-12-04-19-55-51.bpo-30459.3dmPs5.rst @@ -0,0 +1,2 @@ +Convert the :c:func:`PyTuple_SET_ITEM()`, :c:func:`PyList_SET_ITEM()` and +:c:func:`PyCell_SET()` macros to static inline functions. From 9309a5932a4953829003dff4a3ee63559face3d4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 5 Dec 2020 02:30:10 +0100 Subject: [PATCH 2/2] Add a note in What's New in Python 3.10 --- Doc/whatsnew/3.10.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index a8f1080a504c70..083667a7c862c2 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -595,6 +595,10 @@ Porting to Python 3.10 :ref:`Python Path Configuration. `. (Contributed by Victor Stinner in :issue:`42260`.) +* Convert the :c:func:`PyTuple_SET_ITEM()`, :c:func:`PyList_SET_ITEM()` and + :c:func:`PyCell_SET()` macros to static inline functions. + (Contributed by Victor Stinner in :issue:`30459`.) + Deprecated ----------