gh-95005: Replace PyAccu with PyUnicodeWriter (gh-95006)

This commit is contained in:
Aivars Kalvāns 2022-07-27 11:43:34 +03:00 committed by GitHub
parent 565403038b
commit 8c88e360e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 70 additions and 240 deletions

View file

@ -1,7 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include <stddef.h> // offsetof()
#include "pycore_accu.h"
#include "pycore_object.h"
#include "_iomodule.h"
@ -27,12 +26,12 @@ typedef struct {
/* The stringio object can be in two states: accumulating or realized.
In accumulating state, the internal buffer contains nothing and
the contents are given by the embedded _PyAccu structure.
the contents are given by the embedded _PyUnicodeWriter structure.
In realized state, the internal buffer is meaningful and the
_PyAccu is destroyed.
_PyUnicodeWriter is destroyed.
*/
int state;
_PyAccu accu;
_PyUnicodeWriter writer;
char ok; /* initialized? */
char closed;
@ -126,12 +125,14 @@ resize_buffer(stringio *self, size_t size)
static PyObject *
make_intermediate(stringio *self)
{
PyObject *intermediate = _PyAccu_Finish(&self->accu);
PyObject *intermediate = _PyUnicodeWriter_Finish(&self->writer);
self->state = STATE_REALIZED;
if (intermediate == NULL)
return NULL;
if (_PyAccu_Init(&self->accu) ||
_PyAccu_Accumulate(&self->accu, intermediate)) {
_PyUnicodeWriter_Init(&self->writer);
self->writer.overallocate = 1;
if (_PyUnicodeWriter_WriteStr(&self->writer, intermediate)) {
Py_DECREF(intermediate);
return NULL;
}
@ -150,7 +151,7 @@ realize(stringio *self)
assert(self->state == STATE_ACCUMULATING);
self->state = STATE_REALIZED;
intermediate = _PyAccu_Finish(&self->accu);
intermediate = _PyUnicodeWriter_Finish(&self->writer);
if (intermediate == NULL)
return -1;
@ -218,7 +219,7 @@ write_str(stringio *self, PyObject *obj)
if (self->state == STATE_ACCUMULATING) {
if (self->string_size == self->pos) {
if (_PyAccu_Accumulate(&self->accu, decoded))
if (_PyUnicodeWriter_WriteStr(&self->writer, decoded))
goto fail;
goto success;
}
@ -572,7 +573,7 @@ _io_StringIO_close_impl(stringio *self)
/* Free up some memory */
if (resize_buffer(self, 0) < 0)
return NULL;
_PyAccu_Destroy(&self->accu);
_PyUnicodeWriter_Dealloc(&self->writer);
Py_CLEAR(self->readnl);
Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder);
@ -602,7 +603,7 @@ stringio_dealloc(stringio *self)
PyMem_Free(self->buf);
self->buf = NULL;
}
_PyAccu_Destroy(&self->accu);
_PyUnicodeWriter_Dealloc(&self->writer);
Py_CLEAR(self->readnl);
Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder);
@ -687,7 +688,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
self->ok = 0;
_PyAccu_Destroy(&self->accu);
_PyUnicodeWriter_Dealloc(&self->writer);
Py_CLEAR(self->readnl);
Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder);
@ -742,8 +743,8 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
/* Empty stringio object, we can start by accumulating */
if (resize_buffer(self, 0) < 0)
return -1;
if (_PyAccu_Init(&self->accu))
return -1;
_PyUnicodeWriter_Init(&self->writer);
self->writer.overallocate = 1;
self->state = STATE_ACCUMULATING;
}
self->pos = 0;