]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119182: Use public PyUnicodeWriter in stringio.c (#129243)
authorVictor Stinner <vstinner@python.org>
Thu, 23 Jan 2025 23:31:49 +0000 (00:31 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Jan 2025 23:31:49 +0000 (23:31 +0000)
Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API.

Modules/_io/stringio.c

index 687f584ee6caff121251205e48d53b562fcc07e1..d1a71298a9087f5fc5ed0c2ceb9b4ec55c16b5d1 100644 (file)
@@ -30,7 +30,7 @@ typedef struct {
        _PyUnicodeWriter is destroyed.
     */
     int state;
-    _PyUnicodeWriter writer;
+    PyUnicodeWriter *writer;
 
     char ok; /* initialized? */
     char closed;
@@ -129,14 +129,18 @@ resize_buffer(stringio *self, size_t size)
 static PyObject *
 make_intermediate(stringio *self)
 {
-    PyObject *intermediate = _PyUnicodeWriter_Finish(&self->writer);
+    PyObject *intermediate = PyUnicodeWriter_Finish(self->writer);
+    self->writer = NULL;
     self->state = STATE_REALIZED;
     if (intermediate == NULL)
         return NULL;
 
-    _PyUnicodeWriter_Init(&self->writer);
-    self->writer.overallocate = 1;
-    if (_PyUnicodeWriter_WriteStr(&self->writer, intermediate)) {
+    self->writer = PyUnicodeWriter_Create(0);
+    if (self->writer == NULL) {
+        Py_DECREF(intermediate);
+        return NULL;
+    }
+    if (PyUnicodeWriter_WriteStr(self->writer, intermediate)) {
         Py_DECREF(intermediate);
         return NULL;
     }
@@ -155,7 +159,8 @@ realize(stringio *self)
     assert(self->state == STATE_ACCUMULATING);
     self->state = STATE_REALIZED;
 
-    intermediate = _PyUnicodeWriter_Finish(&self->writer);
+    intermediate = PyUnicodeWriter_Finish(self->writer);
+    self->writer = NULL;
     if (intermediate == NULL)
         return -1;
 
@@ -217,7 +222,7 @@ write_str(stringio *self, PyObject *obj)
 
     if (self->state == STATE_ACCUMULATING) {
         if (self->string_size == self->pos) {
-            if (_PyUnicodeWriter_WriteStr(&self->writer, decoded))
+            if (PyUnicodeWriter_WriteStr(self->writer, decoded))
                 goto fail;
             goto success;
         }
@@ -577,7 +582,8 @@ _io_StringIO_close_impl(stringio *self)
     /* Free up some memory */
     if (resize_buffer(self, 0) < 0)
         return NULL;
-    _PyUnicodeWriter_Dealloc(&self->writer);
+    PyUnicodeWriter_Discard(self->writer);
+    self->writer = NULL;
     Py_CLEAR(self->readnl);
     Py_CLEAR(self->writenl);
     Py_CLEAR(self->decoder);
@@ -615,7 +621,7 @@ stringio_dealloc(stringio *self)
         PyMem_Free(self->buf);
         self->buf = NULL;
     }
-    _PyUnicodeWriter_Dealloc(&self->writer);
+    PyUnicodeWriter_Discard(self->writer);
     (void)stringio_clear(self);
     if (self->weakreflist != NULL) {
         PyObject_ClearWeakRefs((PyObject *) self);
@@ -699,7 +705,8 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
 
     self->ok = 0;
 
-    _PyUnicodeWriter_Dealloc(&self->writer);
+    PyUnicodeWriter_Discard(self->writer);
+    self->writer = NULL;
     Py_CLEAR(self->readnl);
     Py_CLEAR(self->writenl);
     Py_CLEAR(self->decoder);
@@ -754,8 +761,10 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
         /* Empty stringio object, we can start by accumulating */
         if (resize_buffer(self, 0) < 0)
             return -1;
-        _PyUnicodeWriter_Init(&self->writer);
-        self->writer.overallocate = 1;
+        self->writer = PyUnicodeWriter_Create(0);
+        if (self->writer == NULL) {
+            return -1;
+        }
         self->state = STATE_ACCUMULATING;
     }
     self->pos = 0;