Skip to content

Commit 8c88e36

Browse files
authored
gh-95005: Replace PyAccu with PyUnicodeWriter (gh-95006)
1 parent 5654030 commit 8c88e36

10 files changed

+69
-239
lines changed

Include/internal/pycore_accu.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

Makefile.pre.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ PYTHON_OBJS= \
435435
# Objects
436436
OBJECT_OBJS= \
437437
Objects/abstract.o \
438-
Objects/accu.o \
439438
Objects/boolobject.o \
440439
Objects/bytes_methods.o \
441440
Objects/bytearrayobject.o \
@@ -1565,7 +1564,6 @@ PYTHON_HEADERS= \
15651564
$(srcdir)/Include/cpython/weakrefobject.h \
15661565
\
15671566
$(srcdir)/Include/internal/pycore_abstract.h \
1568-
$(srcdir)/Include/internal/pycore_accu.h \
15691567
$(srcdir)/Include/internal/pycore_asdl.h \
15701568
$(srcdir)/Include/internal/pycore_ast.h \
15711569
$(srcdir)/Include/internal/pycore_ast_state.h \
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Replace :c:expr:`_PyAccu` with :c:expr:`_PyUnicodeWriter` in JSON encoder
2+
and StringIO and remove the :c:expr:`_PyAccu` implementation.

Modules/_io/stringio.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#define PY_SSIZE_T_CLEAN
22
#include "Python.h"
33
#include <stddef.h> // offsetof()
4-
#include "pycore_accu.h"
54
#include "pycore_object.h"
65
#include "_iomodule.h"
76

@@ -27,12 +26,12 @@ typedef struct {
2726

2827
/* The stringio object can be in two states: accumulating or realized.
2928
In accumulating state, the internal buffer contains nothing and
30-
the contents are given by the embedded _PyAccu structure.
29+
the contents are given by the embedded _PyUnicodeWriter structure.
3130
In realized state, the internal buffer is meaningful and the
32-
_PyAccu is destroyed.
31+
_PyUnicodeWriter is destroyed.
3332
*/
3433
int state;
35-
_PyAccu accu;
34+
_PyUnicodeWriter writer;
3635

3736
char ok; /* initialized? */
3837
char closed;
@@ -126,12 +125,14 @@ resize_buffer(stringio *self, size_t size)
126125
static PyObject *
127126
make_intermediate(stringio *self)
128127
{
129-
PyObject *intermediate = _PyAccu_Finish(&self->accu);
128+
PyObject *intermediate = _PyUnicodeWriter_Finish(&self->writer);
130129
self->state = STATE_REALIZED;
131130
if (intermediate == NULL)
132131
return NULL;
133-
if (_PyAccu_Init(&self->accu) ||
134-
_PyAccu_Accumulate(&self->accu, intermediate)) {
132+
133+
_PyUnicodeWriter_Init(&self->writer);
134+
self->writer.overallocate = 1;
135+
if (_PyUnicodeWriter_WriteStr(&self->writer, intermediate)) {
135136
Py_DECREF(intermediate);
136137
return NULL;
137138
}
@@ -150,7 +151,7 @@ realize(stringio *self)
150151
assert(self->state == STATE_ACCUMULATING);
151152
self->state = STATE_REALIZED;
152153

153-
intermediate = _PyAccu_Finish(&self->accu);
154+
intermediate = _PyUnicodeWriter_Finish(&self->writer);
154155
if (intermediate == NULL)
155156
return -1;
156157

@@ -218,7 +219,7 @@ write_str(stringio *self, PyObject *obj)
218219

219220
if (self->state == STATE_ACCUMULATING) {
220221
if (self->string_size == self->pos) {
221-
if (_PyAccu_Accumulate(&self->accu, decoded))
222+
if (_PyUnicodeWriter_WriteStr(&self->writer, decoded))
222223
goto fail;
223224
goto success;
224225
}
@@ -572,7 +573,7 @@ _io_StringIO_close_impl(stringio *self)
572573
/* Free up some memory */
573574
if (resize_buffer(self, 0) < 0)
574575
return NULL;
575-
_PyAccu_Destroy(&self->accu);
576+
_PyUnicodeWriter_Dealloc(&self->writer);
576577
Py_CLEAR(self->readnl);
577578
Py_CLEAR(self->writenl);
578579
Py_CLEAR(self->decoder);
@@ -602,7 +603,7 @@ stringio_dealloc(stringio *self)
602603
PyMem_Free(self->buf);
603604
self->buf = NULL;
604605
}
605-
_PyAccu_Destroy(&self->accu);
606+
_PyUnicodeWriter_Dealloc(&self->writer);
606607
Py_CLEAR(self->readnl);
607608
Py_CLEAR(self->writenl);
608609
Py_CLEAR(self->decoder);
@@ -687,7 +688,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
687688

688689
self->ok = 0;
689690

690-
_PyAccu_Destroy(&self->accu);
691+
_PyUnicodeWriter_Dealloc(&self->writer);
691692
Py_CLEAR(self->readnl);
692693
Py_CLEAR(self->writenl);
693694
Py_CLEAR(self->decoder);
@@ -742,8 +743,8 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
742743
/* Empty stringio object, we can start by accumulating */
743744
if (resize_buffer(self, 0) < 0)
744745
return -1;
745-
if (_PyAccu_Init(&self->accu))
746-
return -1;
746+
_PyUnicodeWriter_Init(&self->writer);
747+
self->writer.overallocate = 1;
747748
self->state = STATE_ACCUMULATING;
748749
}
749750
self->pos = 0;

0 commit comments

Comments
 (0)