Skip to content

Commit 6e1a214

Browse files
gh-89653: PEP 670: unicodeobject.h uses _Py_CAST() (GH-92696) (GH-92703)
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. (cherry picked from commit d0c9353) Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent a1bef8c commit 6e1a214

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
@@ -367,7 +367,7 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
367367
kind and data pointers obtained from other function calls.
368368
index is the index in the string (starts at 0) and value is the new
369369
code point value which should be written to that location. */
370-
static inline void PyUnicode_WRITE(unsigned int kind, void *data,
370+
static inline void PyUnicode_WRITE(int kind, void *data,
371371
Py_ssize_t index, Py_UCS4 value)
372372
{
373373
if (kind == PyUnicode_1BYTE_KIND) {
@@ -384,12 +384,15 @@ static inline void PyUnicode_WRITE(unsigned int kind, void *data,
384384
_Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
385385
}
386386
}
387+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
387388
#define PyUnicode_WRITE(kind, data, index, value) \
388-
PyUnicode_WRITE((unsigned int)(kind), (void*)(data), (index), (Py_UCS4)(value))
389+
PyUnicode_WRITE(_Py_STATIC_CAST(int, kind), _Py_CAST(void*, data), \
390+
(index), _Py_STATIC_CAST(Py_UCS4, value))
391+
#endif
389392

390393
/* Read a code point from the string's canonical representation. No checks
391394
or ready calls are performed. */
392-
static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
395+
static inline Py_UCS4 PyUnicode_READ(int kind,
393396
const void *data, Py_ssize_t index)
394397
{
395398
if (kind == PyUnicode_1BYTE_KIND) {
@@ -401,8 +404,11 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
401404
assert(kind == PyUnicode_4BYTE_KIND);
402405
return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
403406
}
407+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
404408
#define PyUnicode_READ(kind, data, index) \
405-
PyUnicode_READ((unsigned int)(kind), (const void*)(data), (index))
409+
PyUnicode_READ(_Py_STATIC_CAST(int, kind), _Py_CAST(const void*, data), \
410+
(index))
411+
#endif
406412

407413
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
408414
calls PyUnicode_KIND() and might call it twice. For single reads, use
@@ -411,7 +417,7 @@ static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
411417
static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
412418
{
413419
assert(PyUnicode_IS_READY(unicode));
414-
unsigned int kind = PyUnicode_KIND(unicode);
420+
int kind = PyUnicode_KIND(unicode);
415421
if (kind == PyUnicode_1BYTE_KIND) {
416422
return PyUnicode_1BYTE_DATA(unicode)[index];
417423
}
@@ -436,7 +442,7 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
436442
return 0x7fU;
437443
}
438444

439-
unsigned int kind = PyUnicode_KIND(op);
445+
int kind = PyUnicode_KIND(op);
440446
if (kind == PyUnicode_1BYTE_KIND) {
441447
return 0xffU;
442448
}

0 commit comments

Comments
 (0)