Skip to content

Commit 6de78ef

Browse files
authored
pythongh-89653: PEP 670: Use PyObject* type for parameters (python#92694)
Use the PyObject* type for parameters of static inline functions: * Py_SIZE(): same parameter type than PyObject_Size() * PyList_GET_SIZE(), PyList_SET_ITEM(): same parameter type than PyList_Size() and PyList_SetItem() * PyTuple_GET_SIZE(), PyTuple_SET_ITEM(): same parameter type than PyTuple_Size() and PyTuple_SetItem().
1 parent d492f0a commit 6de78ef

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

Include/cpython/listobject.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
3030

3131
// Macros and static inline functions, trading safety for speed
3232

33-
static inline Py_ssize_t PyList_GET_SIZE(PyListObject *op) {
34-
return Py_SIZE(op);
33+
static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
34+
PyListObject *list = _PyList_CAST(op);
35+
return Py_SIZE(list);
3536
}
3637
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
37-
# define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyList_CAST(op))
38+
# define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
3839
#endif
3940

4041
#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
4142

4243
static inline void
43-
PyList_SET_ITEM(PyListObject *op, Py_ssize_t index, PyObject *value) {
44-
op->ob_item[index] = value;
44+
PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
45+
PyListObject *list = _PyList_CAST(op);
46+
list->ob_item[index] = value;
4547
}
4648
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
4749
#define PyList_SET_ITEM(op, index, value) \
48-
PyList_SET_ITEM(_PyList_CAST(op), index, _PyObject_CAST(value))
50+
PyList_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
4951
#endif

Include/cpython/tupleobject.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
1919

2020
// Macros and static inline functions, trading safety for speed
2121

22-
static inline Py_ssize_t PyTuple_GET_SIZE(PyTupleObject *op) {
23-
return Py_SIZE(op);
22+
static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
23+
PyTupleObject *tuple = _PyTuple_CAST(op);
24+
return Py_SIZE(tuple);
2425
}
2526
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
26-
# define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyTuple_CAST(op))
27+
# define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
2728
#endif
2829

2930
#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index])
3031

3132
/* Function *only* to be used to fill in brand new tuples */
3233
static inline void
33-
PyTuple_SET_ITEM(PyTupleObject *op, Py_ssize_t index, PyObject *value) {
34-
op->ob_item[index] = value;
34+
PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
35+
PyTupleObject *tuple = _PyTuple_CAST(op);
36+
tuple->ob_item[index] = value;
3537
}
3638
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
3739
#define PyTuple_SET_ITEM(op, index, value) \
38-
PyTuple_SET_ITEM(_PyTuple_CAST(op), index, _PyObject_CAST(value))
40+
PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
3941
#endif
4042

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

Include/object.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ static inline PyTypeObject* Py_TYPE(PyObject *ob) {
137137
#endif
138138

139139
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
140-
static inline Py_ssize_t Py_SIZE(PyVarObject *ob) {
141-
return ob->ob_size;
140+
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
141+
PyVarObject *var_ob = _PyVarObject_CAST(ob);
142+
return var_ob->ob_size;
142143
}
143144
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
144-
# define Py_SIZE(ob) Py_SIZE(_PyVarObject_CAST(ob))
145+
# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
145146
#endif
146147

147148

0 commit comments

Comments
 (0)