gh-89188: Implement PyUnicode_KIND() as a function (#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.
This commit is contained in:
Victor Stinner 2025-01-30 12:27:27 +01:00 committed by GitHub
parent e1c4ba9288
commit a810cb89f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 2 deletions

View file

@ -16486,3 +16486,24 @@ PyInit__string(void)
{
return PyModuleDef_Init(&_string_module);
}
#undef PyUnicode_KIND
int PyUnicode_KIND(PyObject *op)
{
if (!PyUnicode_Check(op)) {
PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
return -1;
}
return _PyASCIIObject_CAST(op)->state.kind;
}
#undef PyUnicode_DATA
void* PyUnicode_DATA(PyObject *op)
{
if (!PyUnicode_Check(op)) {
PyErr_Format(PyExc_TypeError, "expect str, got %T", op);
return NULL;
}
return _PyUnicode_DATA(op);
}