8000 gh-95005: Replace PyAccu with PyUnicodeWriter by aivarsk · Pull Request #95006 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-95005: Replace PyAccu with PyUnicodeWriter #95006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions Include/internal/pycore_accu.h

This file was deleted.

2 changes: 0 additions & 2 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ PYTHON_OBJS= \
# Objects
OBJECT_OBJS= \
Objects/abstract.o \
Objects/accu.o \
Objects/boolobject.o \
Objects/bytes_methods.o \
Objects/bytearrayobject.o \
Expand Down Expand Up @@ -1565,7 +1564,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/cpython/weakrefobject.h \
\
$(srcdir)/Include/internal/pycore_abstract.h \
$(srcdir)/Include/internal/pycore_accu.h \
$(srcdir)/Include/internal/pycore_asdl.h \
$(srcdir)/Include/internal/pycore_ast.h \
$(srcdir)/Include/internal/pycore_ast_state.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Replace :c:expr:`_PyAccu` with :c:expr:`_PyUnicodeWriter` in JSON encoder
and StringIO and remove the :c:expr:`_PyAccu` implementation.
29 changes: 15 additions & 14 deletions Modules/_io/stringio.c
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Loading
0