@@ -196,8 +196,10 @@ PyBytes_FromString(const char *str)
196196 return (PyObject * ) op ;
197197}
198198
199- PyObject *
200- PyBytes_FromFormatV (const char * format , va_list vargs )
199+
200+ static char *
201+ bytes_fromformat (PyBytesWriter * writer , Py_ssize_t writer_pos ,
202+ const char * format , va_list vargs )
201203{
202204 const char * f ;
203205 const char * p ;
@@ -213,12 +215,8 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
213215 "0xffffffffffffffff\0" (19 bytes). */
214216 char buffer [21 ];
215217
216- Py_ssize_t alloc = strlen (format );
217- PyBytesWriter * writer = PyBytesWriter_Create (alloc );
218- if (writer == NULL ) {
219- return NULL ;
220- }
221- char * s = PyBytesWriter_GetData (writer );
218+ char * s = PyBytesWriter_GetData (writer ) + writer_pos ;
219+ Py_ssize_t alloc = PyBytesWriter_GetSize (writer );
222220
223221#define WRITE_BYTES_LEN (str , len_expr ) \
224222 do { \
@@ -366,20 +364,39 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
366364 default :
367365 /* invalid format string: copy unformatted string and exit */
368366 WRITE_BYTES (p );
369- return PyBytesWriter_FinishWithEndPointer ( writer , s ) ;
367+ return s ;
370368 }
371369 }
372370
373371#undef WRITE_BYTES
374372#undef WRITE_BYTES_LEN
375373
376- return PyBytesWriter_FinishWithEndPointer ( writer , s ) ;
374+ return s ;
377375
378376 error :
379- PyBytesWriter_Discard (writer );
380377 return NULL ;
381378}
382379
380+
381+ PyObject *
382+ PyBytes_FromFormatV (const char * format , va_list vargs )
383+ {
384+ Py_ssize_t alloc = strlen (format );
385+ PyBytesWriter * writer = PyBytesWriter_Create (alloc );
386+ if (writer == NULL ) {
387+ return NULL ;
388+ }
389+
390+ char * s = bytes_fromformat (writer , 0 , format , vargs );
391+ if (s == NULL ) {
392+ PyBytesWriter_Discard (writer );
393+ return NULL ;
394+ }
395+
396+ return PyBytesWriter_FinishWithEndPointer (writer , s );
397+ }
398+
399+
383400PyObject *
384401PyBytes_FromFormat (const char * format , ...)
385402{
@@ -392,6 +409,7 @@ PyBytes_FromFormat(const char *format, ...)
392409 return ret ;
393410}
394411
412+
395413/* Helpers for formatstring */
396414
397415Py_LOCAL_INLINE (PyObject * )
@@ -3949,3 +3967,28 @@ PyBytesWriter_WriteBytes(PyBytesWriter *writer,
39493967 memcpy (buf + pos , bytes , size );
39503968 return 0 ;
39513969}
3970+
3971+
3972+ int
3973+ PyBytesWriter_Format (PyBytesWriter * writer , const char * format , ...)
3974+ {
3975+ Py_ssize_t pos = writer -> size ;
3976+ Py_ssize_t format_len = strlen (format );
3977+ if (format_len > PY_SSIZE_T_MAX - pos ) {
3978+ PyErr_NoMemory ();
3979+ return -1 ;
3980+ }
3981+ Py_ssize_t alloc = pos + format_len ;
3982+
3983+ if (PyBytesWriter_Resize (writer , alloc ) < 0 ) {
3984+ return -1 ;
3985+ }
3986+
3987+ va_list vargs ;
3988+ va_start (vargs , format );
3989+ char * buf = bytes_fromformat (writer , pos , format , vargs );
3990+ va_end (vargs );
3991+
3992+ Py_ssize_t size = buf - byteswriter_data (writer );
3993+ return PyBytesWriter_Resize (writer , size );
3994+ }
0 commit comments