Skip to content

Commit aee6552

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in _PyBytes_FormatEx()
Replace the private _PyBytesWriter API with the new public PyBytesWriter API.
1 parent c3fca5d commit aee6552

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

Include/internal/pycore_long.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ extern int _PyLong_FormatWriter(
135135
int alternate);
136136

137137
extern char* _PyLong_FormatBytesWriter(
138-
_PyBytesWriter *writer,
138+
PyBytesWriter *writer,
139139
char *str,
140140
PyObject *obj,
141141
int base,

Objects/bytesobject.c

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
429429

430430
static char*
431431
formatfloat(PyObject *v, int flags, int prec, int type,
432-
PyObject **p_result, _PyBytesWriter *writer, char *str)
432+
PyObject **p_result, PyBytesWriter *writer, char *str)
433433
{
434434
char *p;
435435
PyObject *result;
@@ -457,7 +457,7 @@ formatfloat(PyObject *v, int flags, int prec, int type,
457457

458458
len = strlen(p);
459459
if (writer != NULL) {
460-
str = _PyBytesWriter_Prepare(writer, str, len);
460+
str = PyBytesWriter_GrowAndUpdatePointer(writer, len, str);
461461
if (str == NULL) {
462462
PyMem_Free(p);
463463
return NULL;
@@ -608,12 +608,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
608608
PyObject *args, int use_bytearray)
609609
{
610610
const char *fmt;
611-
char *res;
612611
Py_ssize_t arglen, argidx;
613612
Py_ssize_t fmtcnt;
614613
int args_owned = 0;
615614
PyObject *dict = NULL;
616-
_PyBytesWriter writer;
617615

618616
if (args == NULL) {
619617
PyErr_BadInternalCall();
@@ -622,14 +620,17 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
622620
fmt = format;
623621
fmtcnt = format_len;
624622

625-
_PyBytesWriter_Init(&writer);
626-
writer.use_bytearray = use_bytearray;
627-
628-
res = _PyBytesWriter_Alloc(&writer, fmtcnt);
629-
if (res == NULL)
623+
PyBytesWriter *writer;
624+
if (use_bytearray) {
625+
writer = _PyBytesWriter_CreateByteArray(fmtcnt);
626+
}
627+
else {
628+
writer = PyBytesWriter_Create(fmtcnt);
629+
}
630+
if (writer == NULL) {
630631
return NULL;
631-
if (!use_bytearray)
632-
writer.overallocate = 1;
632+
}
633+
char *res = PyBytesWriter_GetData(writer);
633634

634635
if (PyTuple_Check(args)) {
635636
arglen = PyTuple_GET_SIZE(args);
@@ -832,11 +833,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
832833
if (v == NULL)
833834
goto error;
834835

835-
if (fmtcnt == 0) {
836-
/* last write: disable writer overallocation */
837-
writer.overallocate = 0;
838-
}
839-
840836
sign = 0;
841837
fill = ' ';
842838
switch (c) {
@@ -897,8 +893,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
897893
}
898894

899895
/* Fast path */
900-
writer.min_size -= 2; /* size preallocated for "%d" */
901-
res = _PyLong_FormatBytesWriter(&writer, res,
896+
res = _PyLong_FormatBytesWriter(writer, res,
902897
v, base, alternate);
903898
if (res == NULL)
904899
goto error;
@@ -926,8 +921,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
926921
&& !(flags & (F_SIGN | F_BLANK)))
927922
{
928923
/* Fast path */
929-
writer.min_size -= 2; /* size preallocated for "%f" */
930-
res = formatfloat(v, flags, prec, c, NULL, &writer, res);
924+
res = formatfloat(v, flags, prec, c, NULL, writer, res);
931925
if (res == NULL)
932926
goto error;
933927
continue;
@@ -983,9 +977,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
983977
alloc++;
984978
/* 2: size preallocated for %s */
985979
if (alloc > 2) {
986-
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
987-
if (res == NULL)
980+
res = PyBytesWriter_GrowAndUpdatePointer(writer, alloc - 2, res);
981+
if (res == NULL) {
988982
goto error;
983+
}
989984
}
990985
#ifndef NDEBUG
991986
char *before = res;
@@ -1058,10 +1053,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10581053
assert((res - before) == alloc);
10591054
#endif
10601055
} /* '%' */
1061-
1062-
/* If overallocation was disabled, ensure that it was the last
1063-
write. Otherwise, we missed an optimization */
1064-
assert(writer.overallocate || fmtcnt == 0 || use_bytearray);
10651056
} /* until end */
10661057

10671058
if (argidx < arglen && !dict) {
@@ -1073,10 +1064,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10731064
if (args_owned) {
10741065
Py_DECREF(args);
10751066
}
1076-
return _PyBytesWriter_Finish(&writer, res);
1067+
return PyBytesWriter_FinishWithPointer(writer, res);
10771068

10781069
error:
1079-
_PyBytesWriter_Dealloc(&writer);
1070+
PyBytesWriter_Discard(writer);
10801071
if (args_owned) {
10811072
Py_DECREF(args);
10821073
}

Objects/longobject.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ static int
20142014
pylong_int_to_decimal_string(PyObject *aa,
20152015
PyObject **p_output,
20162016
_PyUnicodeWriter *writer,
2017-
_PyBytesWriter *bytes_writer,
2017+
PyBytesWriter *bytes_writer,
20182018
char **bytes_str)
20192019
{
20202020
PyObject *s = NULL;
@@ -2045,7 +2045,8 @@ pylong_int_to_decimal_string(PyObject *aa,
20452045
Py_ssize_t size = PyUnicode_GET_LENGTH(s);
20462046
const void *data = PyUnicode_DATA(s);
20472047
int kind = PyUnicode_KIND(s);
2048-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, size);
2048+
*bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, size,
2049+
*bytes_str);
20492050
if (*bytes_str == NULL) {
20502051
goto error;
20512052
}
@@ -2082,7 +2083,7 @@ static int
20822083
long_to_decimal_string_internal(PyObject *aa,
20832084
PyObject **p_output,
20842085
_PyUnicodeWriter *writer,
2085-
_PyBytesWriter *bytes_writer,
2086+
PyBytesWriter *bytes_writer,
20862087
char **bytes_str)
20872088
{
20882089
PyLongObject *scratch, *a;
@@ -2208,7 +2209,8 @@ long_to_decimal_string_internal(PyObject *aa,
22082209
}
22092210
}
22102211
else if (bytes_writer) {
2211-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
2212+
*bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, strlen,
2213+
*bytes_str);
22122214
if (*bytes_str == NULL) {
22132215
Py_DECREF(scratch);
22142216
return -1;
@@ -2318,7 +2320,7 @@ long_to_decimal_string(PyObject *aa)
23182320
static int
23192321
long_format_binary(PyObject *aa, int base, int alternate,
23202322
PyObject **p_output, _PyUnicodeWriter *writer,
2321-
_PyBytesWriter *bytes_writer, char **bytes_str)
2323+
PyBytesWriter *bytes_writer, char **bytes_str)
23222324
{
23232325
PyLongObject *a = (PyLongObject *)aa;
23242326
PyObject *v = NULL;
@@ -2379,7 +2381,8 @@ long_format_binary(PyObject *aa, int base, int alternate,
23792381
return -1;
23802382
}
23812383
else if (bytes_writer) {
2382-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
2384+
*bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, sz,
2385+
*bytes_str);
23832386
if (*bytes_str == NULL)
23842387
return -1;
23852388
}
@@ -2508,7 +2511,7 @@ _PyLong_FormatWriter(_PyUnicodeWriter *writer,
25082511
}
25092512

25102513
char*
2511-
_PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str,
2514+
_PyLong_FormatBytesWriter(PyBytesWriter *writer, char *str,
25122515
PyObject *obj,
25132516
int base, int alternate)
25142517
{
@@ -6397,8 +6400,6 @@ int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
63976400
/*[clinic end generated code: output=89c801df114050a3 input=66f9d0c20529b44f]*/
63986401
{
63996402
int little_endian;
6400-
PyObject *bytes;
6401-
64026403
if (byteorder == NULL)
64036404
little_endian = 0;
64046405
else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
@@ -6411,18 +6412,19 @@ int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
64116412
return NULL;
64126413
}
64136414

6414-
bytes = PyBytes_FromStringAndSize(NULL, length);
6415-
if (bytes == NULL)
6415+
PyBytesWriter *writer = PyBytesWriter_Create(length);
6416+
if (writer == NULL) {
64166417
return NULL;
6418+
}
64176419

64186420
if (_PyLong_AsByteArray((PyLongObject *)self,
6419-
(unsigned char *)PyBytes_AS_STRING(bytes),
6421+
PyBytesWriter_GetData(writer),
64206422
length, little_endian, is_signed, 1) < 0) {
6421-
Py_DECREF(bytes);
6423+
PyBytesWriter_Discard(writer);
64226424
return NULL;
64236425
}
64246426

6425-
return bytes;
6427+
return PyBytesWriter_Finish(writer);
64266428
}
64276429

64286430
/*[clinic input]

0 commit comments

Comments
 (0)