Skip to content

Commit d1019a0

Browse files
committed
gh-119182: Add PyUnicodeWriter_WriteWideChar()
1 parent 52efb94 commit d1019a0

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

Include/cpython/unicodeobject.h

+4
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ PyAPI_FUNC(int) PyUnicodeWriter_WriteUTF8(
459459
PyUnicodeWriter *writer,
460460
const char *str,
461461
Py_ssize_t size);
462+
PyAPI_FUNC(int) PyUnicodeWriter_WriteWideChar(
463+
PyUnicodeWriter *writer,
464+
wchar_t *str,
465+
Py_ssize_t size);
462466

463467
PyAPI_FUNC(int) PyUnicodeWriter_WriteStr(
464468
PyUnicodeWriter *writer,

Modules/_testcapi/unicode.c

+37
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,42 @@ test_unicodewriter_format_recover_error(PyObject *self, PyObject *Py_UNUSED(args
436436
}
437437

438438

439+
static PyObject *
440+
test_unicodewriter_widechar(PyObject *self, PyObject *Py_UNUSED(args))
441+
{
442+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
443+
if (writer == NULL) {
444+
return NULL;
445+
}
446+
if (PyUnicodeWriter_WriteWideChar(writer, L"latin1=\xE9 IGNORED", 8) < 0) {
447+
goto error;
448+
}
449+
if (PyUnicodeWriter_WriteWideChar(writer, L"-", 1) < 0) {
450+
goto error;
451+
}
452+
if (PyUnicodeWriter_WriteWideChar(writer, L"euro=\u20AC", -1) < 0) {
453+
goto error;
454+
}
455+
if (PyUnicodeWriter_WriteChar(writer, '.') < 0) {
456+
goto error;
457+
}
458+
459+
PyObject *result = PyUnicodeWriter_Finish(writer);
460+
if (result == NULL) {
461+
return NULL;
462+
}
463+
assert(PyUnicode_EqualToUTF8(result,
464+
"latin1=\xC3\xA9-euro=\xE2\x82\xAC."));
465+
Py_DECREF(result);
466+
467+
Py_RETURN_NONE;
468+
469+
error:
470+
PyUnicodeWriter_Discard(writer);
471+
return NULL;
472+
}
473+
474+
439475
static PyMethodDef TestMethods[] = {
440476
{"unicode_new", unicode_new, METH_VARARGS},
441477
{"unicode_fill", unicode_fill, METH_VARARGS},
@@ -450,6 +486,7 @@ static PyMethodDef TestMethods[] = {
450486
{"test_unicodewriter_recover_error", test_unicodewriter_recover_error, METH_NOARGS},
451487
{"test_unicodewriter_format", test_unicodewriter_format, METH_NOARGS},
452488
{"test_unicodewriter_format_recover_error", test_unicodewriter_format_recover_error, METH_NOARGS},
489+
{"test_unicodewriter_widechar", test_unicodewriter_widechar, METH_NOARGS},
453490
{NULL},
454491
};
455492

Objects/unicodeobject.c

+21
Original file line numberDiff line numberDiff line change
@@ -13500,6 +13500,27 @@ PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer,
1350013500
return res;
1350113501
}
1350213502

13503+
13504+
int
13505+
PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer,
13506+
wchar_t *str,
13507+
Py_ssize_t size)
13508+
{
13509+
if (size < 0) {
13510+
size = wcslen(str);
13511+
}
13512+
PyObject *obj = PyUnicode_FromWideChar(str, size);
13513+
if (obj == NULL) {
13514+
return -1;
13515+
}
13516+
13517+
_PyUnicodeWriter *_writer = (_PyUnicodeWriter *)writer;
13518+
int res = _PyUnicodeWriter_WriteStr(_writer, obj);
13519+
Py_DECREF(obj);
13520+
return res;
13521+
}
13522+
13523+
1350313524
int
1350413525
_PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
1350513526
const char *str, Py_ssize_t len)

0 commit comments

Comments
 (0)