Skip to content

Commit 2a5f171

Browse files
authored
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.
1 parent 1b184c8 commit 2a5f171

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

Diff for: Include/cpython/listobject.h

+15-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@ typedef struct {
2424
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
2525
PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
2626

27-
/* Macro, trading safety for speed */
28-
2927
/* Cast argument to PyListObject* type. */
3028
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
3129

32-
#define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i])
33-
#define PyList_SET_ITEM(op, i, v) _Py_RVALUE(_PyList_CAST(op)->ob_item[i] = (v))
34-
#define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op))
30+
// Macros and static inline functions, trading safety for speed
31+
32+
static inline Py_ssize_t PyList_GET_SIZE(PyListObject *op) {
33+
return Py_SIZE(op);
34+
}
35+
#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyList_CAST(op))
36+
37+
#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
38+
39+
static inline void
40+
PyList_SET_ITEM(PyListObject *op, Py_ssize_t index, PyObject *value) {
41+
op->ob_item[index] = value;
42+
}
43+
#define PyList_SET_ITEM(op, index, value) \
44+
PyList_SET_ITEM(_PyList_CAST(op), index, _PyObject_CAST(value))

Diff for: Include/cpython/tupleobject.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ typedef struct {
1313
PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, Py_ssize_t);
1414
PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
1515

16-
/* Macros trading safety for speed */
17-
1816
/* Cast argument to PyTupleObject* type. */
1917
#define _PyTuple_CAST(op) (assert(PyTuple_Check(op)), (PyTupleObject *)(op))
2018

21-
#define PyTuple_GET_SIZE(op) Py_SIZE(_PyTuple_CAST(op))
19+
// Macros and static inline functions, trading safety for speed
20+
21+
static inline Py_ssize_t PyTuple_GET_SIZE(PyTupleObject *op) {
22+
return Py_SIZE(op);
23+
}
24+
#define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyTuple_CAST(op))
2225

23-
#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
26+
#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index])
2427

25-
/* Macro, *only* to be used to fill in brand new tuples */
26-
#define PyTuple_SET_ITEM(op, i, v) _Py_RVALUE(_PyTuple_CAST(op)->ob_item[i] = (v))
28+
/* Function *only* to be used to fill in brand new tuples */
29+
static inline void
30+
PyTuple_SET_ITEM(PyTupleObject *op, Py_ssize_t index, PyObject *value) {
31+
op->ob_item[index] = value;
32+
}
33+
#define PyTuple_SET_ITEM(op, index, value) \
34+
PyTuple_SET_ITEM(_PyTuple_CAST(op), index, _PyObject_CAST(value))
2735

2836
PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);

0 commit comments

Comments
 (0)