Skip to content

Commit 0a70d70

Browse files
committed
Convert _PyBytes_FormatEx()
1 parent 62a15be commit 0a70d70

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
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: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
431431

432432
static char*
433433
formatfloat(PyObject *v, int flags, int prec, int type,
434-
PyObject **p_result, _PyBytesWriter *writer, char *str)
434+
PyObject **p_result, PyBytesWriter *writer, char *str)
435435
{
436436
char *p;
437437
PyObject *result;
@@ -459,7 +459,8 @@ formatfloat(PyObject *v, int flags, int prec, int type,
459459

460460
len = strlen(p);
461461
if (writer != NULL) {
462-
str = _PyBytesWriter_Prepare(writer, str, len);
462+
Py_ssize_t resize = PyBytesWriter_GetSize(writer) + len;
463+
str = PyBytesWriter_ResizeAndUpdatePointer(writer, resize, str);
463464
if (str == NULL) {
464465
PyMem_Free(p);
465466
return NULL;
@@ -612,12 +613,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
612613
PyObject *args, int use_bytearray)
613614
{
614615
const char *fmt;
615-
char *res;
616616
Py_ssize_t arglen, argidx;
617617
Py_ssize_t fmtcnt;
618618
int args_owned = 0;
619619
PyObject *dict = NULL;
620-
_PyBytesWriter writer;
621620

622621
if (args == NULL) {
623622
PyErr_BadInternalCall();
@@ -626,14 +625,17 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
626625
fmt = format;
627626
fmtcnt = format_len;
628627

629-
_PyBytesWriter_Init(&writer);
630-
writer.use_bytearray = use_bytearray;
631-
632-
res = _PyBytesWriter_Alloc(&writer, fmtcnt);
633-
if (res == NULL)
628+
PyBytesWriter *writer;
629+
if (use_bytearray) {
630+
writer = _PyBytesWriter_CreateByteArray(fmtcnt);
631+
}
632+
else {
633+
writer = PyBytesWriter_Create(fmtcnt);
634+
}
635+
if (writer == NULL) {
634636
return NULL;
635-
if (!use_bytearray)
636-
writer.overallocate = 1;
637+
}
638+
char *res = PyBytesWriter_GetData(writer);
637639

638640
if (PyTuple_Check(args)) {
639641
arglen = PyTuple_GET_SIZE(args);
@@ -836,11 +838,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
836838
if (v == NULL)
837839
goto error;
838840

839-
if (fmtcnt == 0) {
840-
/* last write: disable writer overallocation */
841-
writer.overallocate = 0;
842-
}
843-
844841
sign = 0;
845842
fill = ' ';
846843
switch (c) {
@@ -901,8 +898,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
901898
}
902899

903900
/* Fast path */
904-
writer.min_size -= 2; /* size preallocated for "%d" */
905-
res = _PyLong_FormatBytesWriter(&writer, res,
901+
res = _PyLong_FormatBytesWriter(writer, res,
906902
v, base, alternate);
907903
if (res == NULL)
908904
goto error;
@@ -930,8 +926,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
930926
&& !(flags & (F_SIGN | F_BLANK)))
931927
{
932928
/* Fast path */
933-
writer.min_size -= 2; /* size preallocated for "%f" */
934-
res = formatfloat(v, flags, prec, c, NULL, &writer, res);
929+
res = formatfloat(v, flags, prec, c, NULL, writer, res);
935930
if (res == NULL)
936931
goto error;
937932
continue;
@@ -987,9 +982,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
987982
alloc++;
988983
/* 2: size preallocated for %s */
989984
if (alloc > 2) {
990-
res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
991-
if (res == NULL)
985+
Py_ssize_t resize = PyBytesWriter_GetSize(writer) + alloc - 2;
986+
res = PyBytesWriter_ResizeAndUpdatePointer(writer, resize, res);
987+
if (res == NULL) {
992988
goto error;
989+
}
993990
}
994991
#ifndef NDEBUG
995992
char *before = res;
@@ -1062,10 +1059,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10621059
assert((res - before) == alloc);
10631060
#endif
10641061
} /* '%' */
1065-
1066-
/* If overallocation was disabled, ensure that it was the last
1067-
write. Otherwise, we missed an optimization */
1068-
assert(writer.overallocate || fmtcnt == 0 || use_bytearray);
10691062
} /* until end */
10701063

10711064
if (argidx < arglen && !dict) {
@@ -1077,10 +1070,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10771070
if (args_owned) {
10781071
Py_DECREF(args);
10791072
}
1080-
return _PyBytesWriter_Finish(&writer, res);
1073+
return PyBytesWriter_FinishWithEndPointer(writer, res);
10811074

10821075
error:
1083-
_PyBytesWriter_Dealloc(&writer);
1076+
PyBytesWriter_Discard(writer);
10841077
if (args_owned) {
10851078
Py_DECREF(args);
10861079
}

Objects/longobject.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ static int
20162016
pylong_int_to_decimal_string(PyObject *aa,
20172017
PyObject **p_output,
20182018
_PyUnicodeWriter *writer,
2019-
_PyBytesWriter *bytes_writer,
2019+
PyBytesWriter *bytes_writer,
20202020
char **bytes_str)
20212021
{
20222022
PyObject *s = NULL;
@@ -2047,7 +2047,9 @@ pylong_int_to_decimal_string(PyObject *aa,
20472047
Py_ssize_t size = PyUnicode_GET_LENGTH(s);
20482048
const void *data = PyUnicode_DATA(s);
20492049
int kind = PyUnicode_KIND(s);
2050-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, size);
2050+
Py_ssize_t resize = PyBytesWriter_GetSize(bytes_writer) + size;
2051+
*bytes_str = PyBytesWriter_ResizeAndUpdatePointer(bytes_writer, resize,
2052+
*bytes_str);
20512053
if (*bytes_str == NULL) {
20522054
goto error;
20532055
}
@@ -2084,7 +2086,7 @@ static int
20842086
long_to_decimal_string_internal(PyObject *aa,
20852087
PyObject **p_output,
20862088
_PyUnicodeWriter *writer,
2087-
_PyBytesWriter *bytes_writer,
2089+
PyBytesWriter *bytes_writer,
20882090
char **bytes_str)
20892091
{
20902092
PyLongObject *scratch, *a;
@@ -2210,7 +2212,9 @@ long_to_decimal_string_internal(PyObject *aa,
22102212
}
22112213
}
22122214
else if (bytes_writer) {
2213-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
2215+
Py_ssize_t resize = PyBytesWriter_GetSize(bytes_writer) + strlen;
2216+
*bytes_str = PyBytesWriter_ResizeAndUpdatePointer(bytes_writer, resize,
2217+
*bytes_str);
22142218
if (*bytes_str == NULL) {
22152219
Py_DECREF(scratch);
22162220
return -1;
@@ -2320,7 +2324,7 @@ long_to_decimal_string(PyObject *aa)
23202324
static int
23212325
long_format_binary(PyObject *aa, int base, int alternate,
23222326
PyObject **p_output, _PyUnicodeWriter *writer,
2323-
_PyBytesWriter *bytes_writer, char **bytes_str)
2327+
PyBytesWriter *bytes_writer, char **bytes_str)
23242328
{
23252329
PyLongObject *a = (PyLongObject *)aa;
23262330
PyObject *v = NULL;
@@ -2381,7 +2385,9 @@ long_format_binary(PyObject *aa, int base, int alternate,
23812385
return -1;
23822386
}
23832387
else if (bytes_writer) {
2384-
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
2388+
Py_ssize_t resize = PyBytesWriter_GetSize(bytes_writer) + sz;
2389+
*bytes_str = PyBytesWriter_ResizeAndUpdatePointer(bytes_writer, resize,
2390+
*bytes_str);
23852391
if (*bytes_str == NULL)
23862392
return -1;
23872393
}
@@ -2510,7 +2516,7 @@ _PyLong_FormatWriter(_PyUnicodeWriter *writer,
25102516
}
25112517

25122518
char*
2513-
_PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str,
2519+
_PyLong_FormatBytesWriter(PyBytesWriter *writer, char *str,
25142520
PyObject *obj,
25152521
int base, int alternate)
25162522
{

0 commit comments

Comments
 (0)