Skip to content

Commit

Permalink
pythongh-89188: add PyUnicode_Data and PyUnicode_GetKind
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 9, 2024
1 parent c968dc7 commit 3279b86
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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
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

0 comments on commit 3279b86

Please sign in to comment.