Skip to content

Commit db388df

Browse files
authored
gh-89653: PEP 670: Convert PyUnicode_KIND() macro to function (#92705)
In the limited C API version 3.12, PyUnicode_KIND() is now implemented as a static inline function. Keep the macro for the regular C API and for the limited C API version 3.11 and older to prevent introducing new compiler warnings. Update _decimal.c and stringlib/eq.h for PyUnicode_KIND().
1 parent d81d57e commit db388df

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

Include/cpython/unicodeobject.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,23 @@ enum PyUnicode_Kind {
242242
PyUnicode_4BYTE_KIND = 4
243243
};
244244

245-
/* Return one of the PyUnicode_*_KIND values defined above. */
245+
// PyUnicode_KIND(): Return one of the PyUnicode_*_KIND values defined above.
246+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
247+
// gh-89653: Converting this macro to a static inline function would introduce
248+
// new compiler warnings on "kind < PyUnicode_KIND(str)" (compare signed and
249+
// unsigned numbers) where kind type is an int or on
250+
// "unsigned int kind = PyUnicode_KIND(str)" (cast signed to unsigned).
251+
// Only declare the function as static inline function in the limited C API
252+
// version 3.12 which is stricter.
246253
#define PyUnicode_KIND(op) \
247254
(_PyASCIIObject_CAST(op)->state.kind)
255+
#else
256+
// Limited C API 3.12 and newer
257+
static inline int PyUnicode_KIND(PyObject *op) {
258+
assert(PyUnicode_IS_READY(op));
259+
return _PyASCIIObject_CAST(op)->state.kind;
260+
}
261+
#endif
248262

249263
/* Return a void pointer to the raw unicode buffer. */
250264
static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {

Modules/_decimal/_decimal.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ is_space(enum PyUnicode_Kind kind, const void *data, Py_ssize_t pos)
19331933
Return NULL if malloc fails and an empty string if invalid characters
19341934
are found. */
19351935
static char *
1936-
numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores)
1936+
numeric_as_ascii(PyObject *u, int strip_ws, int ignore_underscores)
19371937
{
19381938
enum PyUnicode_Kind kind;
19391939
const void *data;
@@ -2047,7 +2047,7 @@ PyDecType_FromCStringExact(PyTypeObject *type, const char *s,
20472047

20482048
/* Return a new PyDecObject or a subtype from a PyUnicodeObject. */
20492049
static PyObject *
2050-
PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u,
2050+
PyDecType_FromUnicode(PyTypeObject *type, PyObject *u,
20512051
PyObject *context)
20522052
{
20532053
PyObject *dec;
@@ -2067,7 +2067,7 @@ PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u,
20672067
* conversion. If the conversion is not exact, fail with InvalidOperation.
20682068
* Allow leading and trailing whitespace in the input operand. */
20692069
static PyObject *
2070-
PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u,
2070+
PyDecType_FromUnicodeExactWS(PyTypeObject *type, PyObject *u,
20712071
PyObject *context)
20722072
{
20732073
PyObject *dec;
@@ -2150,7 +2150,7 @@ PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
21502150
/* Convert from a PyLongObject. The context is not modified; flags set
21512151
during conversion are accumulated in the status parameter. */
21522152
static PyObject *
2153-
dec_from_long(PyTypeObject *type, const PyObject *v,
2153+
dec_from_long(PyTypeObject *type, PyObject *v,
21542154
const mpd_context_t *ctx, uint32_t *status)
21552155
{
21562156
PyObject *dec;
@@ -2201,7 +2201,7 @@ dec_from_long(PyTypeObject *type, const PyObject *v,
22012201
/* Return a new PyDecObject from a PyLongObject. Use the context for
22022202
conversion. */
22032203
static PyObject *
2204-
PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context)
2204+
PyDecType_FromLong(PyTypeObject *type, PyObject *v, PyObject *context)
22052205
{
22062206
PyObject *dec;
22072207
uint32_t status = 0;
@@ -2227,7 +2227,7 @@ PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context)
22272227
/* Return a new PyDecObject from a PyLongObject. Use a maximum context
22282228
for conversion. If the conversion is not exact, set InvalidOperation. */
22292229
static PyObject *
2230-
PyDecType_FromLongExact(PyTypeObject *type, const PyObject *v,
2230+
PyDecType_FromLongExact(PyTypeObject *type, PyObject *v,
22312231
PyObject *context)
22322232
{
22332233
PyObject *dec;

Objects/stringlib/eq.h

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
Py_LOCAL_INLINE(int)
77
unicode_eq(PyObject *a, PyObject *b)
88
{
9-
assert(PyUnicode_Check(a));
10-
assert(PyUnicode_Check(b));
11-
129
if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
1310
return 0;
1411
if (PyUnicode_GET_LENGTH(a) == 0)

0 commit comments

Comments
 (0)