Skip to content

gh-89639: Revert Py_SIZE() static inline function #96119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions Doc/c-api/structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ the definition of all other Python objects.

Return a :term:`borrowed reference`.

Use the :c:func:`Py_SET_TYPE` function to set an object type.
The :c:func:`Py_SET_TYPE` function must be used to set an object type.

.. versionchanged:: 3.11
:c:func:`Py_TYPE()` is changed to an inline static function.
Expand All @@ -125,7 +125,8 @@ the definition of all other Python objects.

Get the reference count of the Python object *o*.

Use the :c:func:`Py_SET_REFCNT()` function to set an object reference count.
The :c:func:`Py_SET_REFCNT()` function must be used to set an object
reference count.

.. versionchanged:: 3.11
The parameter type is no longer :c:type:`const PyObject*`.
Expand All @@ -145,10 +146,9 @@ the definition of all other Python objects.

Get the size of the Python object *o*.

Use the :c:func:`Py_SET_SIZE` function to set an object size.
The :c:func:`Py_SET_SIZE` function must be used to set an object size.

.. versionchanged:: 3.11
:c:func:`Py_SIZE()` is changed to an inline static function.
The parameter type is no longer :c:type:`const PyVarObject*`.


Expand Down
14 changes: 0 additions & 14 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1870,20 +1870,6 @@ Porting to Python 3.11

(Contributed by Victor Stinner in :issue:`39573`.)

* Since :c:func:`Py_SIZE()` is changed to a inline static function,
``Py_SIZE(obj) = new_size`` must be replaced with
``Py_SET_SIZE(obj, new_size)``: see the :c:func:`Py_SET_SIZE()` function
(available since Python 3.9). For backward compatibility, this macro can be
used::

#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
{ ob->ob_size = size; }
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
#endif

(Contributed by Victor Stinner in :issue:`39573`.)

* ``<Python.h>`` no longer includes the header files ``<stdlib.h>``,
``<stdio.h>``, ``<errno.h>`` and ``<string.h>`` when the ``Py_LIMITED_API``
macro is set to ``0x030b0000`` (Python 3.11) or higher. C extensions should
Expand Down
9 changes: 2 additions & 7 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,9 @@ static inline PyTypeObject* Py_TYPE(PyObject *ob) {
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
#endif


// bpo-39573: The Py_SET_SIZE() function must be used to set an object 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(_PyObject_CAST(ob))
#endif
#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)


static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow again to use :c:func:`Py_SIZE` to set an object size:
``Py_SIZE(obj) = size``. Patch by Victor Stinner.
6 changes: 5 additions & 1 deletion Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4950,7 +4950,11 @@ test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
assert(Py_TYPE(obj) == &PyList_Type);
assert(Py_SIZE(obj) == 0);

// bpo-39573: Test Py_SET_TYPE() and Py_SET_SIZE() functions.
// gh-89639: Check that Py_SIZE() can be used as l-value
// to set an object size.
Py_SIZE(obj) = 0;

// gh-89639: Test Py_SET_TYPE() and Py_SET_SIZE() functions.
Py_SET_TYPE(obj, &PyList_Type);
Py_SET_SIZE(obj, 0);

Expand Down