Skip to content
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

[WIP] bpo-41078: Convert PyTuple_GET_ITEM() macro to static inline function #21059

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/whatsnew/3.10.rst
Expand Up @@ -202,5 +202,15 @@ Porting to Python 3.10
for historical reason. It is no longer allowed.
(Contributed by Victor Stinner in :issue:`40839`.)

* Convert ``PyTuple`` and ``PyList`` macros to static inline functions:

* :c:func:`PyTuple_GET_SIZE`, :c:func:`PyList_GET_SIZE`
* :c:func:`PyTuple_GET_ITEM`, :c:func:`PyList_GET_ITEM`
* :c:func:`PyTuple_SET_ITEM`, :c:func:`PyList_SET_ITEM`

It is no longer possible to write ``&PyTuple_GET_ITEM()`` or
``&PyList_GET_ITEM()``.
(Contributed by Victor Stinner in :issue:`41078`.)

Removed
-------
24 changes: 21 additions & 3 deletions Include/cpython/listobject.h
Expand Up @@ -29,6 +29,24 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
/* Cast argument to PyTupleObject* 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_SIZE(op) Py_SIZE(_PyList_CAST(op))

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))


static inline PyObject* _PyList_GET_ITEM(PyListObject *op, Py_ssize_t i)
{
return op->ob_item[i];
}
#define PyList_GET_ITEM(op, i) _PyList_GET_ITEM(_PyList_CAST(op), i)


static inline void _PyList_SET_ITEM(PyListObject *op, Py_ssize_t i, PyObject *item)
{
op->ob_item[i] = item;
}
#define PyList_SET_ITEM(op, i, item) \
_PyList_SET_ITEM(_PyList_CAST(op), i, item)
25 changes: 21 additions & 4 deletions Include/cpython/tupleobject.h
Expand Up @@ -18,11 +18,28 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
/* 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))

#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
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))


static inline PyObject* _PyTuple_GET_ITEM(PyTupleObject *op, Py_ssize_t i)
{
return op->ob_item[i];
}
#define PyTuple_GET_ITEM(op, i) _PyTuple_GET_ITEM(_PyTuple_CAST(op), i)


static inline void _PyTuple_SET_ITEM(PyTupleObject *op, Py_ssize_t i, PyObject *item)
{
op->ob_item[i] = item;
}
/* Function, *only* to be used to fill in brand new tuples */
#define PyTuple_SET_ITEM(op, i, item) \
_PyTuple_SET_ITEM(_PyTuple_CAST(op), i, item)

/* 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)

PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
@@ -0,0 +1,8 @@
Convert ``PyTuple`` and ``PyList`` macros to static inline functions:

* :c:func:`PyTuple_GET_SIZE`, :c:func:`PyList_GET_SIZE`
* :c:func:`PyTuple_GET_ITEM`, :c:func:`PyList_GET_ITEM`
* :c:func:`PyTuple_SET_ITEM`, :c:func:`PyList_SET_ITEM`

It is no longer possible to write ``&PyTuple_GET_ITEM()`` or
``&PyList_GET_ITEM()``.
3 changes: 2 additions & 1 deletion Objects/genericaliasobject.c
Expand Up @@ -2,6 +2,7 @@

#include "Python.h"
#include "pycore_object.h"
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "structmember.h" // PyMemberDef

typedef struct {
Expand Down Expand Up @@ -308,7 +309,7 @@ ga_getitem(PyObject *self, PyObject *item)
}
int is_tuple = PyTuple_Check(item);
Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE(item) : 1;
PyObject **argitems = is_tuple ? &PyTuple_GET_ITEM(item, 0) : &item;
PyObject **argitems = is_tuple ? _PyTuple_ITEMS(item) : &item;
if (nitems != nparams) {
return PyErr_Format(PyExc_TypeError,
"Too %s arguments for %R",
Expand Down