@@ -2276,7 +2276,6 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
22762276 PyObject * input_obj = (PyObject * )self ;
22772277 const char * output_start , * del_table_chars = NULL ;
22782278 Py_ssize_t inlen , tablen , dellen = 0 ;
2279- PyObject * result ;
22802279 int trans_table [256 ];
22812280
22822281 if (PyBytes_Check (table )) {
@@ -2321,13 +2320,13 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
23212320 }
23222321
23232322 inlen = PyBytes_GET_SIZE (input_obj );
2324- result = PyBytes_FromStringAndSize (( char * ) NULL , inlen );
2325- if (result == NULL ) {
2323+ PyBytesWriter * writer = PyBytesWriter_Create ( inlen );
2324+ if (writer == NULL ) {
23262325 PyBuffer_Release (& del_table_view );
23272326 PyBuffer_Release (& table_view );
23282327 return NULL ;
23292328 }
2330- output_start = output = PyBytes_AS_STRING ( result );
2329+ output_start = output = PyBytesWriter_GetData ( writer );
23312330 input = PyBytes_AS_STRING (input_obj );
23322331
23332332 if (dellen == 0 && table_chars != NULL ) {
@@ -2336,14 +2335,17 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
23362335 c = Py_CHARMASK (* input ++ );
23372336 * output ++ = table_chars [c ];
23382337 }
2338+ PyObject * result = PyBytesWriter_Finish (writer );
2339+
23392340 /* Check if anything changed (for returning original object) */
23402341 /* We save this check until the end so that the compiler will */
23412342 /* unroll the loop above leading to MUCH faster code. */
2342- if (PyBytes_CheckExact (input_obj )) {
2343+ if (result != NULL && PyBytes_CheckExact (input_obj )) {
23432344 if (memcmp (PyBytes_AS_STRING (input_obj ), output_start , inlen ) == 0 ) {
23442345 Py_SETREF (result , Py_NewRef (input_obj ));
23452346 }
23462347 }
2348+
23472349 PyBuffer_Release (& del_table_view );
23482350 PyBuffer_Release (& table_view );
23492351 return result ;
@@ -2370,13 +2372,11 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
23702372 changed = 1 ;
23712373 }
23722374 if (!changed && PyBytes_CheckExact (input_obj )) {
2373- Py_DECREF ( result );
2375+ PyBytesWriter_Discard ( writer );
23742376 return Py_NewRef (input_obj );
23752377 }
23762378 /* Fix the size of the resulting byte string */
2377- if (inlen > 0 )
2378- _PyBytes_Resize (& result , output - output_start );
2379- return result ;
2379+ return PyBytesWriter_FinishWithPointer (writer , output );
23802380}
23812381
23822382
0 commit comments