Skip to content

Commit 3a8fdb2

Browse files
authored
bpo-41784: make PyUnicode_AsUTF8AndSize part of the limited API (GH-22252)
1 parent 4dfb190 commit 3a8fdb2

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

Doc/c-api/unicode.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,9 @@ These are the UTF-8 codec APIs:
10981098
.. versionchanged:: 3.7
10991099
The return type is now ``const char *`` rather of ``char *``.
11001100
1101+
.. versionchanged:: 3.10
1102+
This function is a part of the :ref:`limited API <stable>`.
1103+
11011104
11021105
.. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode)
11031106

Doc/whatsnew/3.10.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ New Features
323323
sending value into iterator without raising ``StopIteration`` exception.
324324
(Contributed by Vladimir Matveev in :issue:`41756`.)
325325

326+
* Added :c:func:`PyUnicode_AsUTF8AndSize` to the limited C API.
327+
(Contributed by Alex Gaynor in :issue:`41784`.)
328+
329+
326330
Porting to Python 3.10
327331
----------------------
328332

Include/cpython/unicodeobject.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -726,26 +726,6 @@ PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
726726

727727
/* --- Manage the default encoding ---------------------------------------- */
728728

729-
/* Returns a pointer to the default encoding (UTF-8) of the
730-
Unicode object unicode and the size of the encoded representation
731-
in bytes stored in *size.
732-
733-
In case of an error, no *size is set.
734-
735-
This function caches the UTF-8 encoded string in the unicodeobject
736-
and subsequent calls will return the same string. The memory is released
737-
when the unicodeobject is deallocated.
738-
739-
_PyUnicode_AsStringAndSize is a #define for PyUnicode_AsUTF8AndSize to
740-
support the previous internal function with the same behaviour.
741-
*/
742-
743-
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
744-
PyObject *unicode,
745-
Py_ssize_t *size);
746-
747-
#define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
748-
749729
/* Returns a pointer to the default encoding (UTF-8) of the
750730
Unicode object unicode.
751731

Include/unicodeobject.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,23 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
468468
PyObject *unicode /* Unicode object */
469469
);
470470

471+
/* Returns a pointer to the default encoding (UTF-8) of the
472+
Unicode object unicode and the size of the encoded representation
473+
in bytes stored in *size.
474+
475+
In case of an error, no *size is set.
476+
477+
This function caches the UTF-8 encoded string in the unicodeobject
478+
and subsequent calls will return the same string. The memory is released
479+
when the unicodeobject is deallocated.
480+
*/
481+
482+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
483+
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
484+
PyObject *unicode,
485+
Py_ssize_t *size);
486+
#endif
487+
471488
/* --- UTF-32 Codecs ------------------------------------------------------ */
472489

473490
/* Decodes length bytes from a UTF-32 encoded buffer string and returns
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``PyUnicode_AsUTF8AndSize`` to the limited C API.

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ EXPORT_FUNC(PyUnicode_AsUCS4Copy)
568568
EXPORT_FUNC(PyUnicode_AsUnicodeEscapeString)
569569
EXPORT_FUNC(PyUnicode_AsUTF16String)
570570
EXPORT_FUNC(PyUnicode_AsUTF32String)
571+
EXPORT_FUNC(PyUnicode_AsUTF8AndSize)
571572
EXPORT_FUNC(PyUnicode_AsUTF8String)
572573
EXPORT_FUNC(PyUnicode_AsWideChar)
573574
EXPORT_FUNC(PyUnicode_AsWideCharString)

0 commit comments

Comments
 (0)