8000 gh-119182: Add PyUnicodeWriter_WriteUCS4() function by vstinner · Pull Request #120849 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

gh-119182: Add PyUnicodeWriter_WriteUCS4() function #120849

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

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,16 @@ object.
On success, return ``0``.
On error, set an exception, leave the writer unchanged, and return ``-1``.

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

Writer the UCS4 string *str* into *writer*.

*size* is a number of UCS4 characters. If *size* is equal to ``-1``, get the
string length (search the NUL character).

On success, return ``0``.
On error, set an exception, leave the writer unchanged, and return ``-1``.

.. c:function:: int PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)

Call :c:func:`PyObject_Str` on *obj* and write the output into *writer*.
Expand Down
1 change: 1 addition & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ New Features
* :c:func:`PyUnicodeWriter_Finish`.
* :c:func:`PyUnicodeWriter_WriteChar`.
* :c:func:`PyUnicodeWriter_WriteUTF8`.
* :c:func:`PyUnicodeWriter_WriteUCS4`.
* :c:func:`PyUnicodeWriter_WriteWideChar`.
* :c:func:`PyUnicodeWriter_WriteStr`.
* :c:func:`PyUnicodeWriter_WriteRepr`.
Expand Down
4 changes: 4 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ PyAPI_FUNC(int) PyUnicodeWriter_WriteWideChar(
PyUnicodeWriter *writer,
const wchar_t *str,
Py_ssize_t size);
PyAPI_FUNC(int) PyUnicodeWriter_WriteUCS4(
PyUnicodeWriter *writer,
Py_UCS4 *str,
Py_ssize_t size);

PyAPI_FUNC(int) PyUnicodeWriter_WriteStr(
PyUnicodeWriter *writer,
Expand Down
18 changes: 17 additions & 1 deletion Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,8 +1784,24 @@ def test_widechar(self):
writer.write_widechar("latin1=\xE9")
writer.write_widechar("-")
writer.write_widechar("euro=\u20AC")
writer.write_char("-")
writer.write_ucs4("max=\U0010ffff", -1)
writer.write_char('.')
self.assertEqual(writer.finish(), "latin1=\xE9-euro=\u20AC.")
self.assertEqual(writer.finish(),
"latin1=\xE9-euro=\u20AC-max=\U0010ffff.")

def test_ucs4(self):
writer = self.create_writer(0)
writer.write_ucs4("ascii", -1)
writer.write_char("-")
writer.write_ucs4("latin1=\xe9", -1)
writer.write_char("-")
writer.write_ucs4("euro=\u20ac", -1)
writer.write_char("-")
writer.write_ucs4("max=\U0010ffff", -1)
writer.write_char(".")
self.assertEqual(writer.finish(),
"ascii-latin1=\xE9-euro=\u20AC-max=\U0010ffff.")


@unittest.skipIf(ctypes is None, 'need ctypes')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Add a new :c:type:`PyUnicodeWriter` API to create a Python :class:`str` object:
* :c:func:`PyUnicodeWriter_ 8000 Finish`.
* :c:func:`PyUnicodeWriter_WriteChar`.
* :c:func:`PyUnicodeWriter_WriteUTF8`.
* :c:func:`PyUnicodeWriter_WriteUCS4`.
* :c:func:`PyUnicodeWriter_WriteWideChar`.
* :c:func:`PyUnicodeWriter_WriteStr`.
* :c:func:`PyUnicodeWriter_WriteRepr`.
* :c:func:`PyUnicodeWriter_WriteSubstring`.
* :c:func:`PyUnicodeWriter_Format`.
* :c:func:`PyUnicodeWriter_DecodeUTF8Stateful`.

Patch by Victor Stinner.
31 changes: 31 additions & 0 deletions Modules/_testcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,36 @@ writer_write_widechar(PyObject *self_raw, PyObject *args)
}


static PyObject*
writer_write_ucs4(PyObject *self_raw, PyObject *args)
{
WriterObject *self = (WriterObject *)self_raw;
if (writer_check(self) < 0) {
return NULL;
}

PyObject *str;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "Un", &str, &size)) {
return NULL;
}
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
size = Py_MIN(size, len);

Py_UCS4 *ucs4 = PyUnicode_AsUCS4Copy(str);
if (ucs4 == NULL) {
return NULL;
}

int res = PyUnicodeWriter_WriteUCS4(self->writer, ucs4, size);
PyMem_Free(ucs4);
if (res < 0) {
return NULL;
}
Py_RETURN_NONE;
}


static PyObject*
writer_write_str(PyObject *self_raw, PyObject *args)
{
Expand Down Expand Up @@ -484,6 +514,7 @@ static PyMethodDef writer_methods[] = {
{"write_char", _PyCFunction_CAST(writer_write_char), METH_VARARGS},
{"write_utf8", _PyCFunction_CAST(writer_write_utf8), METH_VARARGS},
{"write_widechar", _PyCFunction_CAST(writer_write_widechar), METH_VARARGS},
{"write_ucs4", _PyCFunction_CAST(writer_write_ucs4), METH_VARARGS},
{"write_str", _PyCFunction_CAST(writer_write_str), METH_VARARGS},
{"write_repr", _PyCFunction_CAST(writer_write_repr), METH_VARARGS},
{"write_substring", _PyCFunction_CAST(writer_write_substring), METH_VARARGS},
Expand Down
44 changes: 44 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,50 @@ _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
return res;
}


int
PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
Py_UCS4 *str,
Py_ssize_t size)
{
_PyUnicodeWriter *writer = (_PyUnicodeWriter*)pub_writer;

if (size < 0) {
size = 0;
for (; str[size] != '\0'; size++);
}

if (size == 0) {
return 0;
}

Py_UCS4 max_char = ucs4lib_find_max_char(str, str + size);

if (_PyUnicodeWriter_Prepare(writer, size, max_char) < 0) {
return -1;
}

int kind = writer->kind;
void *data = (Py_UCS1*)writer->data + writer->pos * kind;
if (kind == PyUnicode_1BYTE_KIND) {
_PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1,
str, str + size,
data);
}
else if (kind == PyUnicode_2BYTE_KIND) {
_PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2,
str, str + size,
data);
}
else {
memcpy(data, str, size * sizeof(Py_UCS4));
}
writer->pos += size;

return 0;
}


PyObject*
PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
{
Expand Down
Loading
0