From 9ba5c4303e4369669983d0b514e5f1e47b4b5cd5 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 26 May 2020 23:07:06 +0900 Subject: [PATCH 1/2] bpo-40778: Update Py_REFCNT and Py_SIZE to const casting --- Doc/c-api/structures.rst | 9 +++++++-- Include/object.h | 5 +++-- .../next/C API/2020-05-26-23-06-59.bpo-40778.7cumE-.rst | 2 ++ Modules/arraymodule.c | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2020-05-26-23-06-59.bpo-40778.7cumE-.rst diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst index 5535f42ac120ac..d2797fe4006578 100644 --- a/Doc/c-api/structures.rst +++ b/Doc/c-api/structures.rst @@ -94,7 +94,10 @@ the definition of all other Python objects. object. It expands to:: - (((PyObject*)(o))->ob_refcnt) + (((const PyObject*)(o))->ob_refcnt) + + .. versionchanged:: 3.10 + :c:macro:`Py_SIZE` is expanded to use ``const PyVarObject*``. .. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt) @@ -109,8 +112,10 @@ the definition of all other Python objects. This macro is used to access the :attr:`ob_size` member of a Python object. It expands to:: - (((PyVarObject*)(o))->ob_size) + (((const PyVarObject*)(o))->ob_size) + .. versionchanged:: 3.10 + :c:macro:`Py_SIZE` is expanded to use ``const PyVarObject*``. .. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size) diff --git a/Include/object.h b/Include/object.h index 5ad05699bb45cd..0f4d2da37c0aa9 100644 --- a/Include/object.h +++ b/Include/object.h @@ -119,9 +119,10 @@ typedef struct { /* Cast argument to PyVarObject* type. */ #define _PyVarObject_CAST(op) ((PyVarObject*)(op)) +#define _PyVarObject_CAST_CONST(op) ((const PyVarObject*)(op)) -#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt) -#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size) +#define Py_REFCNT(ob) (_PyObject_CAST_CONST(ob)->ob_refcnt) +#define Py_SIZE(ob) (_PyVarObject_CAST_CONST(ob)->ob_size) static inline PyTypeObject* _Py_TYPE(const PyObject *ob) { return ob->ob_type; diff --git a/Misc/NEWS.d/next/C API/2020-05-26-23-06-59.bpo-40778.7cumE-.rst b/Misc/NEWS.d/next/C API/2020-05-26-23-06-59.bpo-40778.7cumE-.rst new file mode 100644 index 00000000000000..50ee4c38abc3cd --- /dev/null +++ b/Misc/NEWS.d/next/C API/2020-05-26-23-06-59.bpo-40778.7cumE-.rst @@ -0,0 +1,2 @@ +Update :c:macro:`Py_SIZE` and :c:macro:`Py_SIZE` to use const object +casting. Patch by Dong-hee Na. diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 732703e481adcd..d92b084ecbd335 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -2543,7 +2543,7 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) view->suboffsets = NULL; view->shape = NULL; if ((flags & PyBUF_ND)==PyBUF_ND) { - view->shape = &((Py_SIZE(self))); + view->shape = (Py_ssize_t *)&((Py_SIZE(self))); } view->strides = NULL; if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) From e3d62bd2265b8997efcfc3a27d70fca688a88010 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 26 May 2020 23:17:27 +0900 Subject: [PATCH 2/2] Update Modules/arraymodule.c Co-authored-by: Victor Stinner --- Modules/arraymodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index d92b084ecbd335..08aab8de4dafd2 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -2543,7 +2543,7 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) view->suboffsets = NULL; view->shape = NULL; if ((flags & PyBUF_ND)==PyBUF_ND) { - view->shape = (Py_ssize_t *)&((Py_SIZE(self))); + view->shape = &((PyVarObject *)self)->ob_size; } view->strides = NULL; if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)