@@ -205,11 +205,9 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
205205/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
206206{
207207 const unsigned char * ascii_data ;
208- unsigned char * bin_data ;
209208 int leftbits = 0 ;
210209 unsigned char this_ch ;
211210 unsigned int leftchar = 0 ;
212- PyObject * rv ;
213211 Py_ssize_t ascii_len , bin_len ;
214212 binascii_state * state ;
215213
@@ -223,9 +221,11 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
223221 ascii_len -- ;
224222
225223 /* Allocate the buffer */
226- if ( (rv = PyBytes_FromStringAndSize (NULL , bin_len )) == NULL )
224+ PyBytesWriter * writer = PyBytesWriter_Create (bin_len );
225+ if (writer == NULL ) {
227226 return NULL ;
228- bin_data = (unsigned char * )PyBytes_AS_STRING (rv );
227+ }
228+ unsigned char * bin_data = PyBytesWriter_GetData (writer );
229229
230230 for ( ; bin_len > 0 ; ascii_len -- , ascii_data ++ ) {
231231 /* XXX is it really best to add NULs if there's no more data */
@@ -248,8 +248,7 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
248248 return NULL ;
249249 }
250250 PyErr_SetString (state -> Error , "Illegal char" );
251- Py_DECREF (rv );
252- return NULL ;
251+ goto error ;
253252 }
254253 this_ch = (this_ch - ' ' ) & 077 ;
255254 }
@@ -280,11 +279,14 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
280279 return NULL ;
281280 }
282281 PyErr_SetString (state -> Error , "Trailing garbage" );
283- Py_DECREF (rv );
284- return NULL ;
282+ goto error ;
285283 }
286284 }
287- return rv ;
285+ return PyBytesWriter_Finish (writer );
286+
287+ error :
288+ PyBytesWriter_Discard (writer );
289+ return NULL ;
288290}
289291
290292/*[clinic input]
@@ -888,8 +890,6 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
888890{
889891 const char * argbuf ;
890892 Py_ssize_t arglen ;
891- PyObject * retval ;
892- char * retbuf ;
893893 Py_ssize_t i , j ;
894894 binascii_state * state ;
895895
@@ -911,10 +911,11 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
911911 return NULL ;
912912 }
913913
914- retval = PyBytes_FromStringAndSize ( NULL , ( arglen /2 ) );
915- if (! retval )
914+ PyBytesWriter * writer = PyBytesWriter_Create ( arglen /2 );
915+ if (writer == NULL ) {
916916 return NULL ;
917- retbuf = PyBytes_AS_STRING (retval );
917+ }
918+ char * retbuf = PyBytesWriter_GetData (writer );
918919
919920 for (i = j = 0 ; i < arglen ; i += 2 ) {
920921 unsigned int top = _PyLong_DigitValue [Py_CHARMASK (argbuf [i ])];
@@ -926,14 +927,14 @@ binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
926927 }
927928 PyErr_SetString (state -> Error ,
928929 "Non-hexadecimal digit found" );
929- goto finally ;
930+ goto error ;
930931 }
931932 retbuf [j ++ ] = (top << 4 ) + bot ;
932933 }
933- return retval ;
934+ return PyBytesWriter_Finish ( writer ) ;
934935
935- finally :
936- Py_DECREF ( retval );
936+ error :
937+ PyBytesWriter_Discard ( writer );
937938 return NULL ;
938939}
939940
0 commit comments