Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
vstinner committed Dec 4, 2020
commit 019461232fecf82d05240a79ff6ccd03ee31fe19
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.