Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 575fe39

Browse files
authored
gh-155742: Use PyBytesWriter in bytes.translate() (#157346)
Replace soft deprecated _PyBytes_Resize() with PyBytesWriter. Enhance test_bytes.test_translate().
1 parent 23eda00 commit 575fe39

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,13 +1130,14 @@ def test_translate(self):
11301130
self.assertRaises(ValueError, b.translate, bytes(range(255)))
11311131

11321132
c = b.translate(rosetta, b'hello')
1133-
self.assertEqual(b, b'hello')
1134-
self.assertIsInstance(c, self.type2test)
1133+
self.assertEqual(c, b'')
1134+
self.assertEqual(type(c), self.type2test)
11351135

11361136
c = b.translate(rosetta)
11371137
d = b.translate(rosetta, b'')
1138-
self.assertEqual(c, d)
11391138
self.assertEqual(c, b'helle')
1139+
self.assertEqual(type(c), self.type2test)
1140+
self.assertEqual(d, b'helle')
11401141

11411142
c = b.translate(rosetta, b'l')
11421143
self.assertEqual(c, b'hee')

Objects/bytesobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)