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