@@ -431,7 +431,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
431431
432432static char *
433433formatfloat (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 }
0 commit comments