@@ -302,16 +302,13 @@ static PyObject *
302
302
binascii_b2a_uu_impl (PyObject * module , Py_buffer * data , int backtick )
303
303
/*[clinic end generated code: output=b1b99de62d9bbeb8 input=beb27822241095cd]*/
304
304
{
305
- unsigned char * ascii_data ;
306
305
const unsigned char * bin_data ;
307
306
int leftbits = 0 ;
308
307
unsigned char this_ch ;
309
308
unsigned int leftchar = 0 ;
310
309
binascii_state * state ;
311
- Py_ssize_t bin_len , out_len ;
312
- _PyBytesWriter writer ;
310
+ Py_ssize_t bin_len ;
313
311
314
- _PyBytesWriter_Init (& writer );
315
312
bin_data = data -> buf ;
316
313
bin_len = data -> len ;
317
314
if ( bin_len > 45 ) {
@@ -325,10 +322,12 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
325
322
}
326
323
327
324
/* 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 ) {
331
328
return NULL ;
329
+ }
330
+ unsigned char * ascii_data = PyBytesWriter_Data (writer );
332
331
333
332
/* Store the length */
334
333
if (backtick && !bin_len )
@@ -356,7 +355,7 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
356
355
}
357
356
* ascii_data ++ = '\n' ; /* Append a courtesy newline */
358
357
359
- return _PyBytesWriter_Finish ( & writer , ascii_data );
358
+ return PyBytesWriter_FinishWithEndPointer ( writer , ascii_data );
360
359
}
361
360
362
361
/*[clinic input]
@@ -387,12 +386,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
387
386
388
387
/* Allocate the buffer */
389
388
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 ) {
394
391
return NULL ;
395
- unsigned char * bin_data_start = bin_data ;
392
+ }
393
+ unsigned char * bin_data = PyBytesWriter_Data (writer );
396
394
397
395
if (strict_mode && ascii_len > 0 && ascii_data [0 ] == '=' ) {
398
396
state = get_binascii_state (module );
@@ -488,12 +486,14 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
488
486
state = get_binascii_state (module );
489
487
if (state == NULL ) {
490
488
/* error already set, from get_binascii_state */
489
+ assert (PyErr_Occurred ());
491
490
} else if (quad_pos == 1 ) {
492
491
/*
493
492
** There is exactly one extra valid, non-padding, base64 character.
494
493
** This is an invalid length, as there is no possible input that
495
494
** could encoded into such a base64 string.
496
495
*/
496
+ unsigned char * bin_data_start = PyBytesWriter_Data (writer );
497
497
PyErr_Format (state -> Error ,
498
498
"Invalid base64-encoded string: "
499
499
"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)
502
502
} else {
503
503
PyErr_SetString (state -> Error , "Incorrect padding" );
504
504
}
505
- error_end :
506
- _PyBytesWriter_Dealloc (& writer );
507
- return NULL ;
505
+ goto error_end ;
508
506
}
509
507
510
508
done :
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 ;
512
514
}
513
515
514
516
@@ -527,18 +529,15 @@ static PyObject *
527
529
binascii_b2a_base64_impl (PyObject * module , Py_buffer * data , int newline )
528
530
/*[clinic end generated code: output=4ad62c8e8485d3b3 input=0e20ff59c5f2e3e1]*/
529
531
{
530
- unsigned char * ascii_data ;
531
532
const unsigned char * bin_data ;
532
533
int leftbits = 0 ;
533
534
unsigned char this_ch ;
534
535
unsigned int leftchar = 0 ;
535
- Py_ssize_t bin_len , out_len ;
536
- _PyBytesWriter writer ;
536
+ Py_ssize_t bin_len ;
537
537
binascii_state * state ;
538
538
539
539
bin_data = data -> buf ;
540
540
bin_len = data -> len ;
541
- _PyBytesWriter_Init (& writer );
542
541
543
542
assert (bin_len >= 0 );
544
543
@@ -554,12 +553,15 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
554
553
/* We're lazy and allocate too much (fixed up later).
555
554
"+2" leaves room for up to two pad characters.
556
555
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 ) {
559
558
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 ) {
562
562
return NULL ;
563
+ }
564
+ unsigned char * ascii_data = PyBytesWriter_Data (writer );
563
565
564
566
for ( ; bin_len > 0 ; bin_len -- , bin_data ++ ) {
565
567
/* Shift the data into our buffer */
@@ -584,7 +586,7 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
584
586
if (newline )
585
587
* ascii_data ++ = '\n' ; /* Append a courtesy newline */
586
588
587
- return _PyBytesWriter_Finish ( & writer , ascii_data );
589
+ return PyBytesWriter_FinishWithEndPointer ( writer , ascii_data );
588
590
}
589
591
590
592
0 commit comments