GitHub

Original file line numberDiff line numberDiff line change

@@ -99,7 +99,10 @@ the definition of all other Python objects.

9999
100100

Return a :term:`borrowed reference`.

101101
102-

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

102+

Use the :c:func:`Py_SET_TYPE` function to set an object type.

103+
104+

.. versionchanged:: 3.11

105+

:c:func:`Py_TYPE()` is changed to an inline static function.

103106
104107
105108

.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)

@@ -121,9 +124,10 @@ the definition of all other Python objects.

121124
122125

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

123126
127+

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

128+
124129

.. versionchanged:: 3.10

125130

:c:func:`Py_REFCNT()` is changed to the inline static function.

126-

Use :c:func:`Py_SET_REFCNT()` to set an object reference count.

127131
128132
129133

.. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt)

@@ -137,7 +141,10 @@ the definition of all other Python objects.

137141
138142

Get the size of the Python object *o*.

139143
140-

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

144+

Use the :c:func:`Py_SET_SIZE` function to set an object size.

145+
146+

.. versionchanged:: 3.11

147+

:c:func:`Py_SIZE()` is changed to an inline static function.

141148
142149
143150

.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)

Original file line numberDiff line numberDiff line change

@@ -402,6 +402,34 @@ Porting to Python 3.11

402402

:ref:`static types <static-types>`.

403403

(Contributed by Erlend E. Aasland in :issue:`43908`)

404404
405+

* Since :c:func:`Py_TYPE()` is changed to a inline static function,

406+

``Py_TYPE(obj) = new_type`` must be replaced with

407+

``Py_SET_TYPE(obj, new_type)``: see the :c:func:`Py_SET_TYPE()` function

408+

(available since Python 3.9). For backward compatibility, this macro can be

409+

used::

410+
411+

#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)

412+

static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)

413+

{ ob->ob_type = type; }

414+

#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)

415+

#endif

416+
417+

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

418+
419+

* Since :c:func:`Py_SIZE()` is changed to a inline static function,

420+

``Py_SIZE(obj) = new_size`` must be replaced with

421+

``Py_SET_SIZE(obj, new_size)``: see the :c:func:`Py_SET_SIZE()` function

422+

(available since Python 3.9). For backward compatibility, this macro can be

423+

used::

424+
425+

#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)

426+

static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)

427+

{ ob->ob_size = size; }

428+

#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)

429+

#endif

430+
431+

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

432+
405433

Deprecated

406434

----------

407435
Original file line numberDiff line numberDiff line change

@@ -134,10 +134,16 @@ static inline Py_ssize_t _Py_REFCNT(const PyObject *ob) {

134134
135135
136136

// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.

137-

#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)

137+

static inline PyTypeObject* _Py_TYPE(const PyObject *ob) {

138+

return ob->ob_type;

139+

}

140+

#define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST_CONST(ob))

138141
139142

// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.

140-

#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)

143+

static inline Py_ssize_t _Py_SIZE(const PyVarObject *ob) {

144+

return ob->ob_size;

145+

}

146+

#define Py_SIZE(ob) _Py_SIZE(_PyVarObject_CAST_CONST(ob))

141147
142148
143149

static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,3 @@

1+

Convert the :c:func:`Py_TYPE` and :c:func:`Py_SIZE` macros to static inline

2+

functions. The :c:func:`Py_SET_TYPE` and :c:func:`Py_SET_SIZE` functions

3+

must now be used to set an object type and size. Patch by Victor Stinner.

Original file line numberDiff line numberDiff line change

@@ -5511,10 +5511,9 @@ test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))

55115511

assert(Py_TYPE(obj) == &PyList_Type);

55125512

assert(Py_SIZE(obj) == 0);

55135513
5514-

// bpo-39573: Check that Py_TYPE() and Py_SIZE() can be used

5515-

// as l-values to set an object type and size.

5516-

Py_TYPE(obj) = &PyList_Type;

5517-

Py_SIZE(obj) = 0;

5514+

// bpo-39573: Test Py_SET_TYPE() and Py_SET_SIZE() functions.

5515+

Py_SET_TYPE(obj, &PyList_Type);

5516+

Py_SET_SIZE(obj, 0);

55185517
55195518

Py_DECREF(obj);

55205519

Py_RETURN_NONE;

Read the original on github.com ↗