Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Doc/c-api/structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :c:macro:`Py_SIZE` and :c:macro:`Py_SIZE` to use const object
casting. Patch by Dong-hee Na.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it deserves to be documented. It doesn't change the C API, no?

2 changes: 1 addition & 1 deletion Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down