Skip to content

Commit a810cb8

Browse files
authored
pythongh-89188: Implement PyUnicode_KIND() as a function (python#129412)
Implement PyUnicode_KIND() and PyUnicode_DATA() as function, in addition to the macros with the same names. The macros rely on C bit fields which have compiler-specific layout.
1 parent e1c4ba9 commit a810cb8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Include/cpython/unicodeobject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ enum PyUnicode_Kind {
240240
PyUnicode_4BYTE_KIND = 4
241241
};
242242

243+
PyAPI_FUNC(int) PyUnicode_KIND(PyObject *op);
244+
243245
// PyUnicode_KIND(): Return one of the PyUnicode_*_KIND values defined above.
244246
//
245247
// gh-89653: Converting this macro to a static inline function would introduce
@@ -264,13 +266,15 @@ static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
264266
return data;
265267
}
266268

267-
static inline void* PyUnicode_DATA(PyObject *op) {
269+
PyAPI_FUNC(void*) PyUnicode_DATA(PyObject *op);
270+
271+
static inline void* _PyUnicode_DATA(PyObject *op) {
268272
if (PyUnicode_IS_COMPACT(op)) {
269273
return _PyUnicode_COMPACT_DATA(op);
270274
}
271275
return _PyUnicode_NONCOMPACT_DATA(op);
272276
}
273-
#define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
277+
#define PyUnicode_DATA(op) _PyUnicode_DATA(_PyObject_CAST(op))
274278

275279
/* Return pointers to the canonical representation cast to unsigned char,
276280
Py_UCS2, or Py_UCS4 for direct character access.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Implement :c:func:`PyUnicode_KIND` and :c:func:`PyUnicode_DATA` as function,
2+
in addition to the macros with the same names. The macros rely on C bit
3+
fields which have compiler-specific layout. Patch by Victor Stinner.

Objects/unicodeobject.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16486,3 +16486,24 @@ PyInit__string(void)
1648616486
{
1648716487
return PyModuleDef_Init(&_string_module);
1648816488
}
16489+
16490+
16491+
#undef PyUnicode_KIND
16492+
int PyUnicode_KIND(PyObject *op)
16493+
{
16494+
if (!PyUnicode_Check(op)) {
16495+
PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
16496+
return -1;
16497+
}
16498+
return _PyASCIIObject_CAST(op)->state.kind;
16499+
}
16500+
16501+
#undef PyUnicode_DATA
16502+
void* PyUnicode_DATA(PyObject *op)
16503+
{
16504+
if (!PyUnicode_Check(op)) {
16505+
PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
16506+
return NULL;
16507+
}
16508+
return _PyUnicode_DATA(op);
16509+
}

0 commit comments

Comments
 (0)