@@ -302,16 +302,13 @@ static PyObject *
302302binascii_b2a_uu_impl (PyObject * module , Py_buffer * data , int backtick )
303303/*[clinic end generated code: output=b1b99de62d9bbeb8 input=beb27822241095cd]*/
304304{
305- unsigned char * ascii_data ;
306305 const unsigned char * bin_data ;
307306 int leftbits = 0 ;
308307 unsigned char this_ch ;
309308 unsigned int leftchar = 0 ;
310309 binascii_state * state ;
311- Py_ssize_t bin_len , out_len ;
312- _PyBytesWriter writer ;
310+ Py_ssize_t bin_len ;
313311
314- _PyBytesWriter_Init (& writer );
315312 bin_data = data -> buf ;
316313 bin_len = data -> len ;
317314 if ( bin_len > 45 ) {
@@ -325,10 +322,12 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
325322 }
326323
327324 /* We're lazy and allocate to much (fixed up later) */
328- out_len = 2 + (bin_len + 2 ) / 3 * 4 ;
329- ascii_data = _PyBytesWriter_Alloc ( & writer , out_len );
330- if (ascii_data == NULL )
325+ Py_ssize_t out_len = 2 + (bin_len + 2 ) / 3 * 4 ;
326+ PyBytesWriter * writer = PyBytesWriter_Create ( out_len );
327+ if (writer == NULL ) {
331328 return NULL ;
329+ }
330+ unsigned char * ascii_data = PyBytesWriter_Data (writer );
332331
333332 /* Store the length */
334333 if (backtick && !bin_len )
@@ -356,7 +355,7 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
356355 }
357356 * ascii_data ++ = '\n' ; /* Append a courtesy newline */
358357
359- return _PyBytesWriter_Finish ( & writer , ascii_data );
358+ return PyBytesWriter_FinishWithEndPointer ( writer , ascii_data );
360359}
361360
362361/*[clinic input]
@@ -387,12 +386,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
387386
388387 /* Allocate the buffer */
389388 Py_ssize_t bin_len = ((ascii_len + 3 )/4 )* 3 ; /* Upper bound, corrected later */
390- _PyBytesWriter writer ;
391- _PyBytesWriter_Init (& writer );
392- unsigned char * bin_data = _PyBytesWriter_Alloc (& writer , bin_len );
393- if (bin_data == NULL )
389+ PyBytesWriter * writer = PyBytesWriter_Create (bin_len );
390+ if (writer == NULL ) {
394391 return NULL ;
395- unsigned char * bin_data_start = bin_data ;
392+ }
393+ unsigned char * bin_data = PyBytesWriter_Data (writer );
396394
397395 if (strict_mode && ascii_len > 0 && ascii_data [0 ] == '=' ) {
398396 state = get_binascii_state (module );
@@ -488,12 +486,14 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
488486 state = get_binascii_state (module );
489487 if (state == NULL ) {
490488 /* error already set, from get_binascii_state */
489+ assert (PyErr_Occurred ());
491490 } else if (quad_pos == 1 ) {
492491 /*
493492 ** There is exactly one extra valid, non-padding, base64 character.
494493 ** This is an invalid length, as there is no possible input that
495494 ** could encoded into such a base64 string.
496495 */
496+ unsigned char * bin_data_start = PyBytesWriter_Data (writer );
497497 PyErr_Format (state -> Error ,
498498 "Invalid base64-encoded string: "
499499 "number of data characters (%zd) cannot be 1 more "
@@ -502,13 +502,15 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
502502 } else {
503503 PyErr_SetString (state -> Error , "Incorrect padding" );
504504 }
505- error_end :
506- _PyBytesWriter_Dealloc (& writer );
507- return NULL ;
505+ goto error_end ;
508506 }
509507
510508done :
511- return _PyBytesWriter_Finish (& writer , bin_data );
509+ return PyBytesWriter_FinishWithEndPointer (writer , bin_data );
510+
511+ error_end :
512+ PyBytesWriter_Discard (writer );
513+ return NULL ;
512514}
513515
514516
@@ -527,18 +529,15 @@ static PyObject *
527529binascii_b2a_base64_impl (PyObject * module , Py_buffer * data , int newline )
528530/*[clinic end generated code: output=4ad62c8e8485d3b3 input=0e20ff59c5f2e3e1]*/
529531{
530- unsigned char * ascii_data ;
531532 const unsigned char * bin_data ;
532533 int leftbits = 0 ;
533534 unsigned char this_ch ;
534535 unsigned int leftchar = 0 ;
535- Py_ssize_t bin_len , out_len ;
536- _PyBytesWriter writer ;
536+ Py_ssize_t bin_len ;
537537 binascii_state * state ;
538538
539539 bin_data = data -> buf ;
540540 bin_len = data -> len ;
541- _PyBytesWriter_Init (& writer );
542541
543542 assert (bin_len >= 0 );
544543
@@ -554,12 +553,15 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
554553 /* We're lazy and allocate too much (fixed up later).
555554 "+2" leaves room for up to two pad characters.
556555 Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
557- out_len = bin_len * 2 + 2 ;
558- if (newline )
556+ Py_ssize_t out_len = bin_len * 2 + 2 ;
557+ if (newline ) {
559558 out_len ++ ;
560- ascii_data = _PyBytesWriter_Alloc (& writer , out_len );
561- if (ascii_data == NULL )
559+ }
560+ PyBytesWriter * writer = PyBytesWriter_Create (out_len );
561+ if (writer == NULL ) {
562562 return NULL ;
563+ }
564+ unsigned char * ascii_data = PyBytesWriter_Data (writer );
563565
564566 for ( ; bin_len > 0 ; bin_len -- , bin_data ++ ) {
565567 /* Shift the data into our buffer */
@@ -584,7 +586,7 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
584586 if (newline )
585587 * ascii_data ++ = '\n' ; /* Append a courtesy newline */
586588
587- return _PyBytesWriter_Finish ( & writer , ascii_data );
589+ return PyBytesWriter_FinishWithEndPointer ( writer , ascii_data );
588590}
589591
590592
0 commit comments