Skip to content

bpo-30459: Convert PyTuple_SET_ITEM() to a function #23645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ Porting to Python 3.10
:ref:`Python Path Configuration. <init-path-config>`.
(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
----------

Expand Down
12 changes: 10 additions & 2 deletions Include/cellobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions Include/cpython/listobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 10 additions & 4 deletions Include/cpython/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
@@ -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.