Skip to content

Commit eaa85cb

Browse files
authored
gh-91768: C API no longer use "const PyObject*" type (#91769)
Py_REFCNT(), Py_TYPE(), Py_SIZE() and Py_IS_TYPE() functions argument type is now "PyObject*", rather than "const PyObject*". * Replace also "const PyObject*" with "PyObject*" in functions: * _Py_strhex_impl() * _Py_strhex_with_sep() * _Py_strhex_bytes_with_sep() * Remove _PyObject_CAST_CONST() and _PyVarObject_CAST_CONST() macros. * Py_IS_TYPE() can now use Py_TYPE() in its implementation.
1 parent 128d624 commit eaa85cb

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

Doc/c-api/structures.rst

+12-7
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,38 @@ the definition of all other Python objects.
6262
See documentation of :c:type:`PyVarObject` above.
6363

6464

65-
.. c:function:: int Py_Is(const PyObject *x, const PyObject *y)
65+
.. c:function:: int Py_Is(PyObject *x, PyObject *y)
6666
6767
Test if the *x* object is the *y* object, the same as ``x is y`` in Python.
6868
6969
.. versionadded:: 3.10
7070
7171
72-
.. c:function:: int Py_IsNone(const PyObject *x)
72+
.. c:function:: int Py_IsNone(PyObject *x)
7373
7474
Test if an object is the ``None`` singleton,
7575
the same as ``x is None`` in Python.
7676
7777
.. versionadded:: 3.10
7878
7979
80-
.. c:function:: int Py_IsTrue(const PyObject *x)
80+
.. c:function:: int Py_IsTrue(PyObject *x)
8181
8282
Test if an object is the ``True`` singleton,
8383
the same as ``x is True`` in Python.
8484
8585
.. versionadded:: 3.10
8686
8787
88-
.. c:function:: int Py_IsFalse(const PyObject *x)
88+
.. c:function:: int Py_IsFalse(PyObject *x)
8989
9090
Test if an object is the ``False`` singleton,
9191
the same as ``x is False`` in Python.
9292
9393
.. versionadded:: 3.10
9494
9595
96-
.. c:function:: PyTypeObject* Py_TYPE(const PyObject *o)
96+
.. c:function:: PyTypeObject* Py_TYPE(PyObject *o)
9797
9898
Get the type of the Python object *o*.
9999
@@ -103,6 +103,7 @@ the definition of all other Python objects.
103103
104104
.. versionchanged:: 3.11
105105
:c:func:`Py_TYPE()` is changed to an inline static function.
106+
The parameter type is no longer :c:type:`const PyObject*`.
106107
107108
108109
.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
@@ -120,12 +121,15 @@ the definition of all other Python objects.
120121
.. versionadded:: 3.9
121122
122123
123-
.. c:function:: Py_ssize_t Py_REFCNT(const PyObject *o)
124+
.. c:function:: Py_ssize_t Py_REFCNT(PyObject *o)
124125
125126
Get the reference count of the Python object *o*.
126127
127128
Use the :c:func:`Py_SET_REFCNT()` function to set an object reference count.
128129
130+
.. versionchanged:: 3.11
131+
The parameter type is no longer :c:type:`const PyObject*`.
132+
129133
.. versionchanged:: 3.10
130134
:c:func:`Py_REFCNT()` is changed to the inline static function.
131135
@@ -137,14 +141,15 @@ the definition of all other Python objects.
137141
.. versionadded:: 3.9
138142
139143
140-
.. c:function:: Py_ssize_t Py_SIZE(const PyVarObject *o)
144+
.. c:function:: Py_ssize_t Py_SIZE(PyVarObject *o)
141145
142146
Get the size of the Python object *o*.
143147
144148
Use the :c:func:`Py_SET_SIZE` function to set an object size.
145149
146150
.. versionchanged:: 3.11
147151
:c:func:`Py_SIZE()` is changed to an inline static function.
152+
The parameter type is no longer :c:type:`const PyVarObject*`.
148153
149154
150155
.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)

Include/internal/pycore_strhex.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ PyAPI_FUNC(PyObject*) _Py_strhex_bytes(
2222
PyAPI_FUNC(PyObject*) _Py_strhex_with_sep(
2323
const char* argbuf,
2424
const Py_ssize_t arglen,
25-
const PyObject* sep,
25+
PyObject* sep,
2626
const int bytes_per_group);
2727
PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(
2828
const char* argbuf,
2929
const Py_ssize_t arglen,
30-
const PyObject* sep,
30+
PyObject* sep,
3131
const int bytes_per_group);
3232

3333
#ifdef __cplusplus

Include/object.h

+9-13
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ struct _object {
105105

106106
/* Cast argument to PyObject* type. */
107107
#define _PyObject_CAST(op) ((PyObject*)(op))
108-
#define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
109108

110109
typedef struct {
111110
PyObject ob_base;
@@ -114,39 +113,36 @@ typedef struct {
114113

115114
/* Cast argument to PyVarObject* type. */
116115
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
117-
#define _PyVarObject_CAST_CONST(op) ((const PyVarObject*)(op))
118116

119117

120118
// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
121119
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
122120
#define Py_Is(x, y) ((x) == (y))
123121

124122

125-
static inline Py_ssize_t Py_REFCNT(const PyObject *ob) {
123+
static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
126124
return ob->ob_refcnt;
127125
}
128-
#define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST_CONST(ob))
126+
#define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
129127

130128

131129
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
132-
static inline PyTypeObject* Py_TYPE(const PyObject *ob) {
130+
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
133131
return ob->ob_type;
134132
}
135-
#define Py_TYPE(ob) Py_TYPE(_PyObject_CAST_CONST(ob))
133+
#define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
136134

137135
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
138-
static inline Py_ssize_t Py_SIZE(const PyVarObject *ob) {
136+
static inline Py_ssize_t Py_SIZE(PyVarObject *ob) {
139137
return ob->ob_size;
140138
}
141-
#define Py_SIZE(ob) Py_SIZE(_PyVarObject_CAST_CONST(ob))
139+
#define Py_SIZE(ob) Py_SIZE(_PyVarObject_CAST(ob))
142140

143141

144-
static inline int Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
145-
// bpo-44378: Don't use Py_TYPE() since Py_TYPE() requires a non-const
146-
// object.
147-
return ob->ob_type == type;
142+
static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
143+
return Py_TYPE(ob) == type;
148144
}
149-
#define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST_CONST(ob), type)
145+
#define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), type)
150146

151147

152148
static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:c:func:`Py_REFCNT`, :c:func:`Py_TYPE`, :c:func:`Py_SIZE` and
2+
:c:func:`Py_IS_TYPE` functions argument type is now ``PyObject*``, rather
3+
than ``const PyObject*``. Patch by Victor Stinner.

Python/pystrhex.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <stdlib.h> // abs()
66

77
static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
8-
const PyObject* sep, int bytes_per_sep_group,
8+
PyObject* sep, int bytes_per_sep_group,
99
const int return_bytes)
1010
{
1111
assert(arglen >= 0);
@@ -152,21 +152,23 @@ PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen)
152152

153153
/* Same as above but returns a bytes() instead of str() to avoid the
154154
* need to decode the str() when bytes are needed. */
155-
PyObject * _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)
155+
PyObject* _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)
156156
{
157157
return _Py_strhex_impl(argbuf, arglen, NULL, 0, 1);
158158
}
159159

160160
/* These variants include support for a separator between every N bytes: */
161161

162-
PyObject * _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
162+
PyObject* _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen,
163+
PyObject* sep, const int bytes_per_group)
163164
{
164165
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 0);
165166
}
166167

167168
/* Same as above but returns a bytes() instead of str() to avoid the
168169
* need to decode the str() when bytes are needed. */
169-
PyObject * _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
170+
PyObject* _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen,
171+
PyObject* sep, const int bytes_per_group)
170172
{
171173
return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 1);
172174
}

0 commit comments

Comments
 (0)