gh-129813, PEP 782: Use PyBytesWriter in pickle and struct (#138833)

Replace the private _PyBytesWriter API with the new public
PyBytesWriter API.
This commit is contained in:
Victor Stinner
2025-09-13 18:26:49 +02:00
committed by GitHub
parent ca99af3c5e
commit a65236bb39
2 changed files with 23 additions and 30 deletions

View File

@@ -2612,31 +2612,26 @@ save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
static PyObject * static PyObject *
raw_unicode_escape(PyObject *obj) raw_unicode_escape(PyObject *obj)
{ {
char *p; Py_ssize_t size = PyUnicode_GET_LENGTH(obj);
Py_ssize_t i, size; const void *data = PyUnicode_DATA(obj);
const void *data; int kind = PyUnicode_KIND(obj);
int kind;
_PyBytesWriter writer;
_PyBytesWriter_Init(&writer); Py_ssize_t alloc = size;
PyBytesWriter *writer = PyBytesWriter_Create(alloc);
if (writer == NULL) {
return NULL;
}
char *p = PyBytesWriter_GetData(writer);
size = PyUnicode_GET_LENGTH(obj); for (Py_ssize_t i=0; i < size; i++) {
data = PyUnicode_DATA(obj);
kind = PyUnicode_KIND(obj);
p = _PyBytesWriter_Alloc(&writer, size);
if (p == NULL)
goto error;
writer.overallocate = 1;
for (i=0; i < size; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i); Py_UCS4 ch = PyUnicode_READ(kind, data, i);
/* Map 32-bit characters to '\Uxxxxxxxx' */ /* Map 32-bit characters to '\Uxxxxxxxx' */
if (ch >= 0x10000) { if (ch >= 0x10000) {
/* -1: subtract 1 preallocated byte */ /* -1: subtract 1 preallocated byte */
p = _PyBytesWriter_Prepare(&writer, p, 10-1); p = PyBytesWriter_GrowAndUpdatePointer(writer, 10-1, p);
if (p == NULL) if (p == NULL) {
goto error; goto error;
}
*p++ = '\\'; *p++ = '\\';
*p++ = 'U'; *p++ = 'U';
@@ -2655,9 +2650,10 @@ raw_unicode_escape(PyObject *obj)
ch == 0x1a) ch == 0x1a)
{ {
/* -1: subtract 1 preallocated byte */ /* -1: subtract 1 preallocated byte */
p = _PyBytesWriter_Prepare(&writer, p, 6-1); p = PyBytesWriter_GrowAndUpdatePointer(writer, 6-1, p);
if (p == NULL) if (p == NULL) {
goto error; goto error;
}
*p++ = '\\'; *p++ = '\\';
*p++ = 'u'; *p++ = 'u';
@@ -2671,10 +2667,10 @@ raw_unicode_escape(PyObject *obj)
*p++ = (char) ch; *p++ = (char) ch;
} }
return _PyBytesWriter_Finish(&writer, p); return PyBytesWriter_FinishWithPointer(writer, p);
error: error:
_PyBytesWriter_Dealloc(&writer); PyBytesWriter_Discard(writer);
return NULL; return NULL;
} }

View File

@@ -2189,7 +2189,6 @@ strings.");
static PyObject * static PyObject *
s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs) s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{ {
char *buf;
PyStructObject *soself; PyStructObject *soself;
_structmodulestate *state = get_struct_state_structinst(self); _structmodulestate *state = get_struct_state_structinst(self);
@@ -2205,21 +2204,19 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
} }
/* Allocate a new string */ /* Allocate a new string */
_PyBytesWriter writer; PyBytesWriter *writer = PyBytesWriter_Create(soself->s_size);
_PyBytesWriter_Init(&writer); if (writer == NULL) {
buf = _PyBytesWriter_Alloc(&writer, soself->s_size);
if (buf == NULL) {
_PyBytesWriter_Dealloc(&writer);
return NULL; return NULL;
} }
char *buf = PyBytesWriter_GetData(writer);
/* Call the guts */ /* Call the guts */
if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) { if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) {
_PyBytesWriter_Dealloc(&writer); PyBytesWriter_Discard(writer);
return NULL; return NULL;
} }
return _PyBytesWriter_Finish(&writer, buf + soself->s_size); return PyBytesWriter_FinishWithSize(writer, soself->s_size);
} }
PyDoc_STRVAR(s_pack_into__doc__, PyDoc_STRVAR(s_pack_into__doc__,