Skip to content

Commit df22eec

Browse files
authored
gh-89653: PEP 670: Macros always cast arguments in cpython/ (#93766)
Header files in the Include/cpython/ are only included if the Py_LIMITED_API macro is not defined.
1 parent c200757 commit df22eec

9 files changed

+28
-92
lines changed

Include/cpython/bytearrayobject.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ static inline char* PyByteArray_AS_STRING(PyObject *op)
2525
}
2626
return _PyByteArray_empty_string;
2727
}
28-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
29-
# define PyByteArray_AS_STRING(self) PyByteArray_AS_STRING(_PyObject_CAST(self))
30-
#endif
28+
#define PyByteArray_AS_STRING(self) PyByteArray_AS_STRING(_PyObject_CAST(self))
3129

3230
static inline Py_ssize_t PyByteArray_GET_SIZE(PyObject *op) {
3331
PyByteArrayObject *self = _PyByteArray_CAST(op);
3432
return Py_SIZE(self);
3533
}
36-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
37-
# define PyByteArray_GET_SIZE(self) PyByteArray_GET_SIZE(_PyObject_CAST(self))
38-
#endif
34+
#define PyByteArray_GET_SIZE(self) PyByteArray_GET_SIZE(_PyObject_CAST(self))

Include/cpython/bytesobject.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ static inline char* PyBytes_AS_STRING(PyObject *op)
3636
{
3737
return _PyBytes_CAST(op)->ob_sval;
3838
}
39-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
40-
# define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))
41-
#endif
39+
#define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))
4240

4341
static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
4442
PyBytesObject *self = _PyBytes_CAST(op);
4543
return Py_SIZE(self);
4644
}
47-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
48-
# define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))
49-
#endif
45+
#define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))
5046

5147
/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
5248
x must be an iterable object. */

Include/cpython/cellobject.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ static inline PyObject* PyCell_GET(PyObject *op) {
2727
cell = _Py_CAST(PyCellObject*, op);
2828
return cell->ob_ref;
2929
}
30-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
31-
# define PyCell_GET(op) PyCell_GET(_PyObject_CAST(op))
32-
#endif
30+
#define PyCell_GET(op) PyCell_GET(_PyObject_CAST(op))
3331

3432
static inline void PyCell_SET(PyObject *op, PyObject *value) {
3533
PyCellObject *cell;
3634
assert(PyCell_Check(op));
3735
cell = _Py_CAST(PyCellObject*, op);
3836
cell->ob_ref = value;
3937
}
40-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
41-
# define PyCell_SET(op, value) PyCell_SET(_PyObject_CAST(op), (value))
42-
#endif
38+
#define PyCell_SET(op, value) PyCell_SET(_PyObject_CAST(op), (value))
4339

4440
#ifdef __cplusplus
4541
}

Include/cpython/dictobject.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
5252
mp = _Py_CAST(PyDictObject*, op);
5353
return mp->ma_used;
5454
}
55-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
56-
# define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
57-
#endif
55+
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
5856

5957
PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
6058
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);

Include/cpython/listobject.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
3434
PyListObject *list = _PyList_CAST(op);
3535
return Py_SIZE(list);
3636
}
37-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
38-
# define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
39-
#endif
37+
#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
4038

4139
#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
4240

@@ -45,7 +43,5 @@ PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
4543
PyListObject *list = _PyList_CAST(op);
4644
list->ob_item[index] = value;
4745
}
48-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
4946
#define PyList_SET_ITEM(op, index, value) \
5047
PyList_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
51-
#endif

Include/cpython/methodobject.h

+4-12
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ PyAPI_DATA(PyTypeObject) PyCMethod_Type;
4040
static inline PyCFunction PyCFunction_GET_FUNCTION(PyObject *func) {
4141
return _PyCFunctionObject_CAST(func)->m_ml->ml_meth;
4242
}
43-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
44-
# define PyCFunction_GET_FUNCTION(func) PyCFunction_GET_FUNCTION(_PyObject_CAST(func))
45-
#endif
43+
#define PyCFunction_GET_FUNCTION(func) PyCFunction_GET_FUNCTION(_PyObject_CAST(func))
4644

4745
static inline PyObject* PyCFunction_GET_SELF(PyObject *func_obj) {
4846
PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
@@ -51,16 +49,12 @@ static inline PyObject* PyCFunction_GET_SELF(PyObject *func_obj) {
5149
}
5250
return func->m_self;
5351
}
54-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
55-
# define PyCFunction_GET_SELF(func) PyCFunction_GET_SELF(_PyObject_CAST(func))
56-
#endif
52+
#define PyCFunction_GET_SELF(func) PyCFunction_GET_SELF(_PyObject_CAST(func))
5753

5854
static inline int PyCFunction_GET_FLAGS(PyObject *func) {
5955
return _PyCFunctionObject_CAST(func)->m_ml->ml_flags;
6056
}
61-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
62-
# define PyCFunction_GET_FLAGS(func) PyCFunction_GET_FLAGS(_PyObject_CAST(func))
63-
#endif
57+
#define PyCFunction_GET_FLAGS(func) PyCFunction_GET_FLAGS(_PyObject_CAST(func))
6458

6559
static inline PyTypeObject* PyCFunction_GET_CLASS(PyObject *func_obj) {
6660
PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
@@ -69,6 +63,4 @@ static inline PyTypeObject* PyCFunction_GET_CLASS(PyObject *func_obj) {
6963
}
7064
return _Py_NULL;
7165
}
72-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
73-
# define PyCFunction_GET_CLASS(func) PyCFunction_GET_CLASS(_PyObject_CAST(func))
74-
#endif
66+
#define PyCFunction_GET_CLASS(func) PyCFunction_GET_CLASS(_PyObject_CAST(func))

Include/cpython/tupleobject.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
2323
PyTupleObject *tuple = _PyTuple_CAST(op);
2424
return Py_SIZE(tuple);
2525
}
26-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
27-
# define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
28-
#endif
26+
#define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
2927

3028
#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index])
3129

@@ -35,9 +33,7 @@ PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
3533
PyTupleObject *tuple = _PyTuple_CAST(op);
3634
tuple->ob_item[index] = value;
3735
}
38-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
3936
#define PyTuple_SET_ITEM(op, index, value) \
4037
PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
41-
#endif
4238

4339
PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);

Include/cpython/unicodeobject.h

+14-46
Original file line numberDiff line numberDiff line change
@@ -188,45 +188,35 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
188188
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
189189
return _PyASCIIObject_CAST(op)->state.interned;
190190
}
191-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
192-
# define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
193-
#endif
191+
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
194192

195193
/* For backward compatibility */
196194
static inline unsigned int PyUnicode_IS_READY(PyObject* Py_UNUSED(op)) {
197195
return 1;
198196
}
199-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
200-
# define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
201-
#endif
197+
#define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
202198

203199
/* Return true if the string contains only ASCII characters, or 0 if not. The
204200
string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
205201
ready. */
206202
static inline unsigned int PyUnicode_IS_ASCII(PyObject *op) {
207203
return _PyASCIIObject_CAST(op)->state.ascii;
208204
}
209-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
210-
# define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
211-
#endif
205+
#define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
212206

213207
/* Return true if the string is compact or 0 if not.
214208
No type checks or Ready calls are performed. */
215209
static inline unsigned int PyUnicode_IS_COMPACT(PyObject *op) {
216210
return _PyASCIIObject_CAST(op)->state.compact;
217211
}
218-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
219-
# define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
220-
#endif
212+
#define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
221213

222214
/* Return true if the string is a compact ASCII string (use PyASCIIObject
223215
structure), or 0 if not. No type checks or Ready calls are performed. */
224216
static inline int PyUnicode_IS_COMPACT_ASCII(PyObject *op) {
225217
return (_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op));
226218
}
227-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
228-
# define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
229-
#endif
219+
#define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
230220

231221
enum PyUnicode_Kind {
232222
/* Return values of the PyUnicode_KIND() function: */
@@ -236,22 +226,14 @@ enum PyUnicode_Kind {
236226
};
237227

238228
// PyUnicode_KIND(): Return one of the PyUnicode_*_KIND values defined above.
239-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
229+
//
240230
// gh-89653: Converting this macro to a static inline function would introduce
241231
// new compiler warnings on "kind < PyUnicode_KIND(str)" (compare signed and
242232
// unsigned numbers) where kind type is an int or on
243233
// "unsigned int kind = PyUnicode_KIND(str)" (cast signed to unsigned).
244234
// Only declare the function as static inline function in the limited C API
245235
// version 3.12 which is stricter.
246-
#define PyUnicode_KIND(op) \
247-
(_PyASCIIObject_CAST(op)->state.kind)
248-
#else
249-
// Limited C API 3.12 and newer
250-
static inline int PyUnicode_KIND(PyObject *op) {
251-
assert(PyUnicode_IS_READY(op));
252-
return _PyASCIIObject_CAST(op)->state.kind;
253-
}
254-
#endif
236+
#define PyUnicode_KIND(op) (_PyASCIIObject_CAST(op)->state.kind)
255237

256238
/* Return a void pointer to the raw unicode buffer. */
257239
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
@@ -275,9 +257,7 @@ static inline void* PyUnicode_DATA(PyObject *op) {
275257
}
276258
return _PyUnicode_NONCOMPACT_DATA(op);
277259
}
278-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
279-
# define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
280-
#endif
260+
#define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
281261

282262
/* Return pointers to the canonical representation cast to unsigned char,
283263
Py_UCS2, or Py_UCS4 for direct character access.
@@ -292,9 +272,7 @@ static inline void* PyUnicode_DATA(PyObject *op) {
292272
static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
293273
return _PyASCIIObject_CAST(op)->length;
294274
}
295-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
296-
# define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
297-
#endif
275+
#define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
298276

299277
/* Write into the canonical representation, this function does not do any sanity
300278
checks and is intended for usage in loops. The caller should cache the
@@ -319,11 +297,9 @@ static inline void PyUnicode_WRITE(int kind, void *data,
319297
_Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
320298
}
321299
}
322-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
323300
#define PyUnicode_WRITE(kind, data, index, value) \
324301
PyUnicode_WRITE(_Py_STATIC_CAST(int, kind), _Py_CAST(void*, data), \
325302
(index), _Py_STATIC_CAST(Py_UCS4, value))
326-
#endif
327303

328304
/* Read a code point from the string's canonical representation. No checks
329305
or ready calls are performed. */
@@ -340,12 +316,10 @@ static inline Py_UCS4 PyUnicode_READ(int kind,
340316
assert(kind == PyUnicode_4BYTE_KIND);
341317
return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
342318
}
343-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
344319
#define PyUnicode_READ(kind, data, index) \
345320
PyUnicode_READ(_Py_STATIC_CAST(int, kind), \
346321
_Py_STATIC_CAST(const void*, data), \
347322
(index))
348-
#endif
349323

350324
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
351325
calls PyUnicode_KIND() and might call it twice. For single reads, use
@@ -369,10 +343,8 @@ static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
369343
assert(kind == PyUnicode_4BYTE_KIND);
370344
return PyUnicode_4BYTE_DATA(unicode)[index];
371345
}
372-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
373-
# define PyUnicode_READ_CHAR(unicode, index) \
374-
PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
375-
#endif
346+
#define PyUnicode_READ_CHAR(unicode, index) \
347+
PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
376348

377349
/* Return a maximum character value which is suitable for creating another
378350
string based on op. This is always an approximation but more efficient
@@ -395,10 +367,8 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
395367
assert(kind == PyUnicode_4BYTE_KIND);
396368
return 0x10ffffU;
397369
}
398-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
399-
# define PyUnicode_MAX_CHAR_VALUE(op) \
400-
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
401-
#endif
370+
#define PyUnicode_MAX_CHAR_VALUE(op) \
371+
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
402372

403373
/* === Public API ========================================================= */
404374

@@ -417,9 +387,7 @@ static inline int PyUnicode_READY(PyObject* Py_UNUSED(op))
417387
{
418388
return 0;
419389
}
420-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
421-
# define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
422-
#endif
390+
#define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
423391

424392
/* Get a copy of a Unicode string. */
425393
PyAPI_FUNC(PyObject*) _PyUnicode_Copy(

Include/cpython/weakrefobject.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,4 @@ static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
5353
}
5454
return Py_None;
5555
}
56-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
57-
# define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))
58-
#endif
56+
#define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))

0 commit comments

Comments
 (0)