@@ -432,7 +432,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
432432
433433static char *
434434formatfloat (PyObject * v , int flags , int prec , int type ,
435- PyObject * * p_result , _PyBytesWriter * writer , char * str )
435+ PyObject * * p_result , PyBytesWriter * writer , char * str )
436436{
437437 char * p ;
438438 PyObject * result ;
@@ -460,7 +460,7 @@ formatfloat(PyObject *v, int flags, int prec, int type,
460460
461461 len = strlen (p );
462462 if (writer != NULL ) {
463- str = _PyBytesWriter_Prepare (writer , str , len );
463+ str = PyBytesWriter_GrowAndUpdatePointer (writer , len , str );
464464 if (str == NULL ) {
465465 PyMem_Free (p );
466466 return NULL ;
@@ -611,12 +611,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
611611 PyObject * args , int use_bytearray )
612612{
613613 const char * fmt ;
614- char * res ;
615614 Py_ssize_t arglen , argidx ;
616615 Py_ssize_t fmtcnt ;
617616 int args_owned = 0 ;
618617 PyObject * dict = NULL ;
619- _PyBytesWriter writer ;
620618
621619 if (args == NULL ) {
622620 PyErr_BadInternalCall ();
@@ -625,14 +623,17 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
625623 fmt = format ;
626624 fmtcnt = format_len ;
627625
628- _PyBytesWriter_Init (& writer );
629- writer .use_bytearray = use_bytearray ;
630-
631- res = _PyBytesWriter_Alloc (& writer , fmtcnt );
632- if (res == NULL )
626+ PyBytesWriter * writer ;
627+ if (use_bytearray ) {
628+ writer = _PyBytesWriter_CreateByteArray (fmtcnt );
629+ }
630+ else {
631+ writer = PyBytesWriter_Create (fmtcnt );
632+ }
633+ if (writer == NULL ) {
633634 return NULL ;
634- if (! use_bytearray )
635- writer . overallocate = 1 ;
635+ }
636+ char * res = PyBytesWriter_GetData ( writer ) ;
636637
637638 if (PyTuple_Check (args )) {
638639 arglen = PyTuple_GET_SIZE (args );
@@ -835,11 +836,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
835836 if (v == NULL )
836837 goto error ;
837838
838- if (fmtcnt == 0 ) {
839- /* last write: disable writer overallocation */
840- writer .overallocate = 0 ;
841- }
842-
843839 sign = 0 ;
844840 fill = ' ' ;
845841 switch (c ) {
@@ -900,8 +896,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
900896 }
901897
902898 /* Fast path */
903- writer .min_size -= 2 ; /* size preallocated for "%d" */
904- res = _PyLong_FormatBytesWriter (& writer , res ,
899+ res = _PyLong_FormatBytesWriter (writer , res ,
905900 v , base , alternate );
906901 if (res == NULL )
907902 goto error ;
@@ -929,8 +924,7 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
929924 && !(flags & (F_SIGN | F_BLANK )))
930925 {
931926 /* Fast path */
932- writer .min_size -= 2 ; /* size preallocated for "%f" */
933- res = formatfloat (v , flags , prec , c , NULL , & writer , res );
927+ res = formatfloat (v , flags , prec , c , NULL , writer , res );
934928 if (res == NULL )
935929 goto error ;
936930 continue ;
@@ -986,9 +980,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
986980 alloc ++ ;
987981 /* 2: size preallocated for %s */
988982 if (alloc > 2 ) {
989- res = _PyBytesWriter_Prepare ( & writer , res , alloc - 2 );
990- if (res == NULL )
983+ res = PyBytesWriter_GrowAndUpdatePointer ( writer , alloc - 2 , res );
984+ if (res == NULL ) {
991985 goto error ;
986+ }
992987 }
993988#ifndef NDEBUG
994989 char * before = res ;
@@ -1061,10 +1056,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10611056 assert ((res - before ) == alloc );
10621057#endif
10631058 } /* '%' */
1064-
1065- /* If overallocation was disabled, ensure that it was the last
1066- write. Otherwise, we missed an optimization */
1067- assert (writer .overallocate || fmtcnt == 0 || use_bytearray );
10681059 } /* until end */
10691060
10701061 if (argidx < arglen && !dict ) {
@@ -1076,10 +1067,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10761067 if (args_owned ) {
10771068 Py_DECREF (args );
10781069 }
1079- return _PyBytesWriter_Finish ( & writer , res );
1070+ return PyBytesWriter_FinishWithPointer ( writer , res );
10801071
10811072 error :
1082- _PyBytesWriter_Dealloc ( & writer );
1073+ PyBytesWriter_Discard ( writer );
10831074 if (args_owned ) {
10841075 Py_DECREF (args );
10851076 }
0 commit comments