FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add PyUnicodeWriter_WriteUCS4() function by vstinner · Pull Request #194 · python/pythoncapi-compat · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .rst  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 9 additions & 4 deletions docs/api.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
pythoncapi_compat.h API
+++++++++++++++++++++++

The ``pythoncapi_compat.h`` header file provides implementations of recent
functions for old Python versions.
The ``pythoncapi_compat.h`` header file provides implementations of around 150
recent functions for old Python versions.

Supported Python versions:

* Python 3.6 - 3.15
* Python 3.6 - 3.16
* PyPy 3.6 - 3.12

C++03 and C++11 are supported on Python 3.6 and newer.
Expand Down Expand Up @@ -193,6 +193,12 @@ Python 3.14

Not available on the limited C API.

.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, const Py_UCS4 *ucs4, Py_ssize_t size)

See `PyUnicodeWriter_WriteUCS4() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteUCS4>`__.

Not available on the limited C API.

.. c:function:: int PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)

See `PyUnicodeWriter_WriteUTF8() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteUTF8>`__.
Expand Down Expand Up @@ -338,7 +344,6 @@ Not supported:
* ``PyInitConfig_SetStrList()``
* ``PyType_GetBaseByToken()``
* ``PyUnicodeWriter_DecodeUTF8Stateful()``
* ``PyUnicodeWriter_WriteUCS4()``
* ``Py_InitializeFromInitConfig()``


Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* 2026-10-10: Add ``PyUnicodeWriter_WriteUCS4()`` function.
* 2026-10-08: Add support for the limited C API 3.2.
* 2026-10-08: Add functions:

Expand Down
15 changes: 15 additions & 0 deletions pythoncapi_compat.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,21 @@ PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str,
start, end);
}

static inline int
PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer,
const Py_UCS4 *ucs4, Py_ssize_t size)
{
PyObject *str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
ucs4, size);
if (str == NULL) {
return -1;
}

int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
Py_DECREF(str);
return res;
}

static inline int
PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
{
Expand Down
11 changes: 10 additions & 1 deletion tests/test_pythoncapi_compat_cext.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,7 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
return NULL;
}
int ret;
const Py_UCS4 ucs4[] = {'u', 'c', 's', '4'};

// test PyUnicodeWriter_WriteStr()
PyObject *str = PyUnicode_FromString("var");
Expand Down Expand Up @@ -1940,6 +1941,14 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
goto error;
}

// test PyUnicodeWriter_WriteUCS4()
if (PyUnicodeWriter_WriteUCS4(writer, ucs4, Py_ARRAY_LENGTH(ucs4)) < 0) {
goto error;
}
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
goto error;
}

// test PyUnicodeWriter_WriteRepr()
str = PyUnicode_FromString("repr");
if (str == NULL) {
Expand All @@ -1958,7 +1967,7 @@ test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
if (result == NULL) {
return NULL;
}
const char *expected = "var=long non-ASCII valu\xC3\xA9 'repr'<NULL>";
const char *expected = "var=long non-ASCII valu\xC3\xA9 ucs4 'repr'<NULL>";
assert(PyUnicode_EqualToUTF8(result, expected));
Py_DECREF(result);
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL