bpo-41784: make PyUnicode_AsUTF8AndSize part of the limited API (GH-22252)

This commit is contained in:
Alex Gaynor 2020-10-19 18:17:50 -04:00 committed by GitHub
parent 4dfb190a33
commit 3a8fdb2879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 20 deletions

View File

@ -1098,6 +1098,9 @@ These are the UTF-8 codec APIs:
.. versionchanged:: 3.7 .. versionchanged:: 3.7
The return type is now ``const char *`` rather of ``char *``. The return type is now ``const char *`` rather of ``char *``.
.. versionchanged:: 3.10
This function is a part of the :ref:`limited API <stable>`.
.. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode) .. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode)

View File

@ -323,6 +323,10 @@ New Features
sending value into iterator without raising ``StopIteration`` exception. sending value into iterator without raising ``StopIteration`` exception.
(Contributed by Vladimir Matveev in :issue:`41756`.) (Contributed by Vladimir Matveev in :issue:`41756`.)
* Added :c:func:`PyUnicode_AsUTF8AndSize` to the limited C API.
(Contributed by Alex Gaynor in :issue:`41784`.)
Porting to Python 3.10 Porting to Python 3.10
---------------------- ----------------------

View File

@ -726,26 +726,6 @@ PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
/* --- Manage the default encoding ---------------------------------------- */ /* --- Manage the default encoding ---------------------------------------- */
/* Returns a pointer to the default encoding (UTF-8) of the
Unicode object unicode and the size of the encoded representation
in bytes stored in *size.
In case of an error, no *size is set.
This function caches the UTF-8 encoded string in the unicodeobject
and subsequent calls will return the same string. The memory is released
when the unicodeobject is deallocated.
_PyUnicode_AsStringAndSize is a #define for PyUnicode_AsUTF8AndSize to
support the previous internal function with the same behaviour.
*/
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
PyObject *unicode,
Py_ssize_t *size);
#define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
/* Returns a pointer to the default encoding (UTF-8) of the /* Returns a pointer to the default encoding (UTF-8) of the
Unicode object unicode. Unicode object unicode.

View File

@ -468,6 +468,23 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
PyObject *unicode /* Unicode object */ PyObject *unicode /* Unicode object */
); );
/* Returns a pointer to the default encoding (UTF-8) of the
Unicode object unicode and the size of the encoded representation
in bytes stored in *size.
In case of an error, no *size is set.
This function caches the UTF-8 encoded string in the unicodeobject
and subsequent calls will return the same string. The memory is released
when the unicodeobject is deallocated.
*/
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
PyObject *unicode,
Py_ssize_t *size);
#endif
/* --- UTF-32 Codecs ------------------------------------------------------ */ /* --- UTF-32 Codecs ------------------------------------------------------ */
/* Decodes length bytes from a UTF-32 encoded buffer string and returns /* Decodes length bytes from a UTF-32 encoded buffer string and returns

View File

@ -0,0 +1 @@
Added ``PyUnicode_AsUTF8AndSize`` to the limited C API.

View File

@ -568,6 +568,7 @@ EXPORT_FUNC(PyUnicode_AsUCS4Copy)
EXPORT_FUNC(PyUnicode_AsUnicodeEscapeString) EXPORT_FUNC(PyUnicode_AsUnicodeEscapeString)
EXPORT_FUNC(PyUnicode_AsUTF16String) EXPORT_FUNC(PyUnicode_AsUTF16String)
EXPORT_FUNC(PyUnicode_AsUTF32String) EXPORT_FUNC(PyUnicode_AsUTF32String)
EXPORT_FUNC(PyUnicode_AsUTF8AndSize)
EXPORT_FUNC(PyUnicode_AsUTF8String) EXPORT_FUNC(PyUnicode_AsUTF8String)
EXPORT_FUNC(PyUnicode_AsWideChar) EXPORT_FUNC(PyUnicode_AsWideChar)
EXPORT_FUNC(PyUnicode_AsWideCharString) EXPORT_FUNC(PyUnicode_AsWideCharString)