gh-119182: Add PyUnicodeWriter C API (#119184) · python/cpython@5c4235c · GitHub
Skip to content

Commit 5c4235c

Browse files
authored
gh-119182: Add PyUnicodeWriter C API (#119184)
1 parent 2c7209a commit 5c4235c

6 files changed

Lines changed: 533 additions & 18 deletions

File tree

Doc/c-api/unicode.rst

Lines changed: 84 additions & 0 deletions

Doc/whatsnew/3.14.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ New Features
283283
* Add :c:func:`PyLong_GetSign` function to get the sign of :class:`int` objects.
284284
(Contributed by Sergey B Kirpichev in :gh:`116560`.)
285285

286+
* Add a new :c:type:`PyUnicodeWriter` API to create a Python :class:`str`
287+
object:
288+
289+
* :c:func:`PyUnicodeWriter_Create`.
290+
* :c:func:`PyUnicodeWriter_Discard`.
291+
* :c:func:`PyUnicodeWriter_Finish`.
292+
* :c:func:`PyUnicodeWriter_WriteChar`.
293+
* :c:func:`PyUnicodeWriter_WriteUTF8`.
294+
* :c:func:`PyUnicodeWriter_WriteStr`.
295+
* :c:func:`PyUnicodeWriter_WriteRepr`.
296+
* :c:func:`PyUnicodeWriter_WriteSubstring`.
297+
* :c:func:`PyUnicodeWriter_Format`.
298+
299+
(Contributed by Victor Stinner in :gh:`119182`.)
300+
286301
Porting to Python 3.14
287302
----------------------
288303

Include/cpython/unicodeobject.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,40 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
444444
Py_ssize_t size);
445445

446446

447-
/* --- _PyUnicodeWriter API ----------------------------------------------- */
447+
/* --- Public PyUnicodeWriter API ----------------------------------------- */
448+
449+
typedef struct PyUnicodeWriter PyUnicodeWriter;
450+
451+
PyAPI_FUNC(PyUnicodeWriter*) PyUnicodeWriter_Create(Py_ssize_t length);
452+
PyAPI_FUNC(void) PyUnicodeWriter_Discard(PyUnicodeWriter *writer);
453+
PyAPI_FUNC(PyObject*) PyUnicodeWriter_Finish(PyUnicodeWriter *writer);
454+
455+
PyAPI_FUNC(int) PyUnicodeWriter_WriteChar(
456+
PyUnicodeWriter *writer,
457+
Py_UCS4 ch);
458+
PyAPI_FUNC(int) PyUnicodeWriter_WriteUTF8(
459+
PyUnicodeWriter *writer,
460+
const char *str,
461+
Py_ssize_t size);
462+
463+
PyAPI_FUNC(int) PyUnicodeWriter_WriteStr(
464+
PyUnicodeWriter *writer,
465+
PyObject *obj);
466+
PyAPI_FUNC(int) PyUnicodeWriter_WriteRepr(
467+
PyUnicodeWriter *writer,
468+
PyObject *obj);
469+
PyAPI_FUNC(int) PyUnicodeWriter_WriteSubstring(
470+
PyUnicodeWriter *writer,
471+
PyObject *str,
472+
Py_ssize_t start,
473+
Py_ssize_t end);
474+
PyAPI_FUNC(int) PyUnicodeWriter_Format(
475+
PyUnicodeWriter *writer,
476+
const char *format,
477+
...);
478+
479+
480+
/* --- Private _PyUnicodeWriter API --------------------------------------- */
448481

449482
typedef struct {
450483
PyObject *buffer;
@@ -466,7 +499,7 @@ typedef struct {
466499
/* If readonly is 1, buffer is a shared string (cannot be modified)
467500
and size is set to 0. */
468501
unsigned char readonly;
469-
} _PyUnicodeWriter ;
502+
} _PyUnicodeWriter;
470503

471504
// Initialize a Unicode writer.
472505
//
Lines changed: 13 additions & 0 deletions

0 commit comments

Comments
 (0)