diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst index 86d4536f8d28e7..4ad0666d065ed4 100644 --- a/Doc/c-api/structures.rst +++ b/Doc/c-api/structures.rst @@ -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. @@ -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*`. @@ -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*`. diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 2159a3175b91f1..cb5720f97a0f3f 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -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`.) - * ```` no longer includes the header files ````, ````, ```` and ```` when the ``Py_LIMITED_API`` macro is set to ``0x030b0000`` (Python 3.11) or higher. C extensions should diff --git a/Include/object.h b/Include/object.h index 21d3a75834eba5..04af5b2d75909e 100644 --- a/Include/object.h +++ b/Include/object.h @@ -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) { diff --git a/Misc/NEWS.d/next/C API/2022-08-19-12-06-57.gh-issue-89639.mKduYZ.rst b/Misc/NEWS.d/next/C API/2022-08-19-12-06-57.gh-issue-89639.mKduYZ.rst new file mode 100644 index 00000000000000..b05dfed4b64025 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-08-19-12-06-57.gh-issue-89639.mKduYZ.rst @@ -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. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 2d4c73cfe97097..639dd667feef20 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -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);