Skip to content

Commit 49bedc2

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in pickle and struct
Replace the private _PyBytesWriter API with the new public PyBytesWriter API.
1 parent c3fca5d commit 49bedc2

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

Modules/_pickle.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,31 +2612,26 @@ save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
26122612
static PyObject *
26132613
raw_unicode_escape(PyObject *obj)
26142614
{
2615-
char *p;
2616-
Py_ssize_t i, size;
2617-
const void *data;
2618-
int kind;
2619-
_PyBytesWriter writer;
2615+
Py_ssize_t size = PyUnicode_GET_LENGTH(obj);
2616+
const void *data = PyUnicode_DATA(obj);
2617+
int kind = PyUnicode_KIND(obj);
26202618

2621-
_PyBytesWriter_Init(&writer);
2622-
2623-
size = PyUnicode_GET_LENGTH(obj);
2624-
data = PyUnicode_DATA(obj);
2625-
kind = PyUnicode_KIND(obj);
2626-
2627-
p = _PyBytesWriter_Alloc(&writer, size);
2628-
if (p == NULL)
2629-
goto error;
2630-
writer.overallocate = 1;
2619+
Py_ssize_t alloc = size;
2620+
PyBytesWriter *writer = PyBytesWriter_Create(alloc);
2621+
if (writer == NULL) {
2622+
return NULL;
2623+
}
2624+
char *p = PyBytesWriter_GetData(writer);
26312625

2632-
for (i=0; i < size; i++) {
2626+
for (Py_ssize_t i=0; i < size; i++) {
26332627
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
26342628
/* Map 32-bit characters to '\Uxxxxxxxx' */
26352629
if (ch >= 0x10000) {
26362630
/* -1: subtract 1 preallocated byte */
2637-
p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2638-
if (p == NULL)
2631+
p = PyBytesWriter_GrowAndUpdatePointer(writer, 10-1, p);
2632+
if (p == NULL) {
26392633
goto error;
2634+
}
26402635

26412636
*p++ = '\\';
26422637
*p++ = 'U';
@@ -2655,9 +2650,10 @@ raw_unicode_escape(PyObject *obj)
26552650
ch == 0x1a)
26562651
{
26572652
/* -1: subtract 1 preallocated byte */
2658-
p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2659-
if (p == NULL)
2653+
p = PyBytesWriter_GrowAndUpdatePointer(writer, 6-1, p);
2654+
if (p == NULL) {
26602655
goto error;
2656+
}
26612657

26622658
*p++ = '\\';
26632659
*p++ = 'u';
@@ -2671,10 +2667,10 @@ raw_unicode_escape(PyObject *obj)
26712667
*p++ = (char) ch;
26722668
}
26732669

2674-
return _PyBytesWriter_Finish(&writer, p);
2670+
return PyBytesWriter_FinishWithPointer(writer, p);
26752671

26762672
error:
2677-
_PyBytesWriter_Dealloc(&writer);
2673+
PyBytesWriter_Discard(writer);
26782674
return NULL;
26792675
}
26802676

Modules/_struct.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,6 @@ strings.");
21892189
static PyObject *
21902190
s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
21912191
{
2192-
char *buf;
21932192
PyStructObject *soself;
21942193
_structmodulestate *state = get_struct_state_structinst(self);
21952194

@@ -2205,21 +2204,19 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
22052204
}
22062205

22072206
/* Allocate a new string */
2208-
_PyBytesWriter writer;
2209-
_PyBytesWriter_Init(&writer);
2210-
buf = _PyBytesWriter_Alloc(&writer, soself->s_size);
2211-
if (buf == NULL) {
2212-
_PyBytesWriter_Dealloc(&writer);
2207+
PyBytesWriter *writer = PyBytesWriter_Create(soself->s_size);
2208+
if (writer == NULL) {
22132209
return NULL;
22142210
}
2211+
char *buf = PyBytesWriter_GetData(writer);
22152212

22162213
/* Call the guts */
22172214
if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) {
2218-
_PyBytesWriter_Dealloc(&writer);
2215+
PyBytesWriter_Discard(writer);
22192216
return NULL;
22202217
}
22212218

2222-
return _PyBytesWriter_Finish(&writer, buf + soself->s_size);
2219+
return PyBytesWriter_FinishWithSize(writer, soself->s_size);
22232220
}
22242221

22252222
PyDoc_STRVAR(s_pack_into__doc__,

0 commit comments

Comments
 (0)