Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-89188: add PyUnicode_Data and PyUnicode_GetKind #115211

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ static inline void* PyUnicode_DATA(PyObject *op) {
}
#define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))

/* Symbol to reexport PyUnicode_DATA without needing to read the contents
of the structure directly.
*/
PyAPI_FUNC(void *) PyUnicode_Data(PyObject *op);
PyAPI_FUNC(int) PyUnicode_GetKind(PyObject *op);

/* Return pointers to the canonical representation cast to unsigned char,
Py_UCS2, or Py_UCS4 for direct character access.
No checks are performed, use PyUnicode_KIND() before to ensure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``PyUnicode_Data`` and ``PyUnicode_GetKind`` as alternatives to ``PyUnicode_DATA`` and ``PyUnicode_KIND`` which don't rely on the internal structure of unicode objects.
16 changes: 16 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3863,6 +3863,22 @@ _PyUnicode_AsUTF8NoNUL(PyObject *unicode)
return s;
}

void* PyUnicode_Data(PyObject *unicode) {
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
return PyUnicode_DATA(unicode);
}

int PyUnicode_GetKind(PyObject *unicode) {
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return -1;
}
return PyUnicode_KIND(unicode);
}

/*
PyUnicode_GetSize() has been deprecated since Python 3.3
because it returned length of Py_UNICODE.
Expand Down
Loading