]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-89653: PEP 670: Use PyObject* type for parameters (#92694)
authorVictor Stinner <vstinner@python.org>
Wed, 11 May 2022 22:49:03 +0000 (00:49 +0200)
committerGitHub <noreply@github.com>
Wed, 11 May 2022 22:49:03 +0000 (00:49 +0200)
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().

Include/cpython/listobject.h
Include/cpython/tupleobject.h
Include/object.h

index 4989cccef9f8045cb1c15388f297f03ccd9e3cac..1add8213e0c092fbd63514ef96cf75a26a21ea08 100644 (file)
@@ -30,20 +30,22 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
 
 // Macros and static inline functions, trading safety for speed
 
-static inline Py_ssize_t PyList_GET_SIZE(PyListObject *op) {
-    return Py_SIZE(op);
+static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
+    PyListObject *list = _PyList_CAST(op);
+    return Py_SIZE(list);
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyList_CAST(op))
+#  define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
 #endif
 
 #define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
 
 static inline void
-PyList_SET_ITEM(PyListObject *op, Py_ssize_t index, PyObject *value) {
-    op->ob_item[index] = value;
+PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
+    PyListObject *list = _PyList_CAST(op);
+    list->ob_item[index] = value;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
 #define PyList_SET_ITEM(op, index, value) \
-    PyList_SET_ITEM(_PyList_CAST(op), index, _PyObject_CAST(value))
+    PyList_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
 #endif
index a41cee1759d484362e1e7558dff406b70f2e90fb..3d9c1aff588634e9b1ab1d215131cd65272cc96f 100644 (file)
@@ -19,23 +19,25 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
 
 // Macros and static inline functions, trading safety for speed
 
-static inline Py_ssize_t PyTuple_GET_SIZE(PyTupleObject *op) {
-    return Py_SIZE(op);
+static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
+    PyTupleObject *tuple = _PyTuple_CAST(op);
+    return Py_SIZE(tuple);
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyTuple_CAST(op))
+#  define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
 #endif
 
 #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 *op, Py_ssize_t index, PyObject *value) {
-    op->ob_item[index] = value;
+PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
+    PyTupleObject *tuple = _PyTuple_CAST(op);
+    tuple->ob_item[index] = value;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
 #define PyTuple_SET_ITEM(op, index, value) \
-    PyTuple_SET_ITEM(_PyTuple_CAST(op), index, _PyObject_CAST(value))
+    PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
 #endif
 
 PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
index fac8892f65552787c71471583f8b0bb2779af613..f2af428e2bb9730e4dd0e610d21b44c7610d3d2f 100644 (file)
@@ -137,11 +137,12 @@ static inline PyTypeObject* Py_TYPE(PyObject *ob) {
 #endif
 
 // bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
-static inline Py_ssize_t Py_SIZE(PyVarObject *ob) {
-    return ob->ob_size;
+static inline Py_ssize_t Py_SIZE(PyObject *ob) {
+    PyVarObject *var_ob = _PyVarObject_CAST(ob);
+    return var_ob->ob_size;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define Py_SIZE(ob) Py_SIZE(_PyVarObject_CAST(ob))
+#  define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
 #endif