Skip to content

Commit d0c9353

Browse files
authored
gh-89653: PEP 670: unicodeobject.h uses _Py_CAST() (#92696)
Use _Py_CAST() and _Py_STATIC_CAST() in macros wrapping static inline functions of unicodeobject.h. Change also the kind type from unsigned int to int: same parameter type than PyUnicode_FromKindAndData(). The limited API version 3.11 no longer casts arguments to expected types.
1 parent 92f0ed1 commit d0c9353

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Doc/c-api/unicode.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ access to internal read-only data of Unicode objects:
149149
``PyUnicode_WCHAR_KIND`` is deprecated.
150150
151151
152-
.. c:function:: unsigned int PyUnicode_KIND(PyObject *o)
152+
.. c:function:: int PyUnicode_KIND(PyObject *o)
153153
154154
Return one of the PyUnicode kind constants (see above) that indicate how many
155155
bytes per character this Unicode object uses to store its data. *o* has to
@@ -168,7 +168,7 @@ access to internal read-only data of Unicode objects:
168168
.. versionadded:: 3.3
169169
170170
171-
.. c:function:: void PyUnicode_WRITE(unsigned int kind, void *data, \
171+
.. c:function:: void PyUnicode_WRITE(int kind, void *data, \
172172
Py_ssize_t index, Py_UCS4 value)
173173
174174
Write into a canonical representation *data* (as obtained with
@@ -181,7 +181,7 @@ access to internal read-only data of Unicode objects:
181181
.. versionadded:: 3.3
182182
183183
184-
.. c:function:: Py_UCS4 PyUnicode_READ(unsigned int kind, void *data, \
184+
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, \
185185
Py_ssize_t index)
186186
187187
Read a code point from a canonical representation *data* (as obtained with

Include/cpython/unicodeobject.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
351351
kind and data pointers obtained from other function calls.
352352
index is the index in the string (starts at 0) and value is the new
353353
code point value which should be written to that location. */
354-
static inline void PyUnicode_WRITE(unsigned int kind, void *data,
354+
static inline void PyUnicode_WRITE(int kind, void *data,
355355
Py_ssize_t index, Py_UCS4 value)
356356
{
357357
if (kind == PyUnicode_1BYTE_KIND) {
@@ -368,12 +368,15 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
368368
_Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
369369
}
370370
}
371+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
371372
#define PyUnicode_WRITE(kind, data, index, value) \
372-
PyUnicode_WRITE((unsigned int)(kind), (void*)(data), (index), (Py_UCS4)(value))
373+
PyUnicode_WRITE(_Py_STATIC_CAST(int, kind), _Py_CAST(void*, data), \
374+
(index), _Py_STATIC_CAST(Py_UCS4, value))
375+
#endif
373376

374377
/* Read a code point from the string's canonical representation. No checks
375378
or ready calls are performed. */
376-
static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
379+
static inline Py_UCS4 PyUnicode_READ(int kind,
377380
const void *data, Py_ssize_t index)
378381
{
379382
if (kind == PyUnicode_1BYTE_KIND) {
@@ -385,8 +388,11 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
385388
assert(kind == PyUnicode_4BYTE_KIND);
386389
return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
387390
}
391+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
388392
#define PyUnicode_READ(kind, data, index) \
389-
PyUnicode_READ((unsigned int)(kind), (const void*)(data), (index))
393+
PyUnicode_READ(_Py_STATIC_CAST(int, kind), _Py_CAST(const void*, data), \
394+
(index))
395+
#endif
390396

391397
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
392398
calls PyUnicode_KIND() and might call it twice. For single reads, use
@@ -395,7 +401,7 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
395401
static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
396402
{
397403
assert(PyUnicode_IS_READY(unicode));
398-
unsigned int kind = PyUnicode_KIND(unicode);
404+
int kind = PyUnicode_KIND(unicode);
399405
if (kind == PyUnicode_1BYTE_KIND) {
400406
return PyUnicode_1BYTE_DATA(unicode)[index];
401407
}
@@ -420,7 +426,7 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
420426
return 0x7fU;
421427
}
422428

423-
unsigned int kind = PyUnicode_KIND(op);
429+
int kind = PyUnicode_KIND(op);
424430
if (kind == PyUnicode_1BYTE_KIND) {
425431
return 0xffU;
426432
}

0 commit comments

Comments
 (0)