https://github.com/python/cpython/commit/575fe3914f466e684f13aed1cdd68347f8746944
commit: 575fe3914f466e684f13aed1cdd68347f8746944
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-12T17:49:29Z
summary:
gh-155742: Use PyBytesWriter in bytes.translate() (#157346)
Replace soft deprecated _PyBytes_Resize() with PyBytesWriter.
Enhance test_bytes.test_translate().
files:
M Lib/test/test_bytes.py
M Objects/bytesobject.c
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 4b2bed9a0a5644..3297a53dceff00 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1130,13 +1130,14 @@ def test_translate(self):
self.assertRaises(ValueError, b.translate, bytes(range(255)))
c = b.translate(rosetta, b'hello')
- self.assertEqual(b, b'hello')
- self.assertIsInstance(c, self.type2test)
+ self.assertEqual(c, b'')
+ self.assertEqual(type(c), self.type2test)
c = b.translate(rosetta)
d = b.translate(rosetta, b'')
- self.assertEqual(c, d)
self.assertEqual(c, b'helle')
+ self.assertEqual(type(c), self.type2test)
+ self.assertEqual(d, b'helle')
c = b.translate(rosetta, b'l')
self.assertEqual(c, b'hee')
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index a5ec05764b2b70..ace5fe9d86a7d3 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2276,7 +2276,6 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
PyObject *input_obj = (PyObject*)self;
const char *output_start, *del_table_chars=NULL;
Py_ssize_t inlen, tablen, dellen = 0;
- PyObject *result;
int trans_table[256];
if (PyBytes_Check(table)) {
@@ -2321,13 +2320,13 @@ bytes_translate_impl(PyBytesObject *self, PyObject
*table,
}
inlen = PyBytes_GET_SIZE(input_obj);
- result = PyBytes_FromStringAndSize((char *)NULL, inlen);
- if (result == NULL) {
+ PyBytesWriter *writer = PyBytesWriter_Create(inlen);
+ if (writer == NULL) {
PyBuffer_Release(&del_table_view);
PyBuffer_Release(&table_view);
return NULL;
}
- output_start = output = PyBytes_AS_STRING(result);
+ output_start = output = PyBytesWriter_GetData(writer);
input = PyBytes_AS_STRING(input_obj);
if (dellen == 0 && table_chars != NULL) {
@@ -2336,14 +2335,17 @@ bytes_translate_impl(PyBytesObject *self, PyObject
*table,
c = Py_CHARMASK(*input++);
*output++ = table_chars[c];
}
+ PyObject *result = PyBytesWriter_Finish(writer);
+
/* Check if anything changed (for returning original object) */
/* We save this check until the end so that the compiler will */
/* unroll the loop above leading to MUCH faster code. */
- if (PyBytes_CheckExact(input_obj)) {
+ if (result != NULL && PyBytes_CheckExact(input_obj)) {
if (memcmp(PyBytes_AS_STRING(input_obj), output_start, inlen) ==
0) {
Py_SETREF(result, Py_NewRef(input_obj));
}
}
+
PyBuffer_Release(&del_table_view);
PyBuffer_Release(&table_view);
return result;
@@ -2370,13 +2372,11 @@ bytes_translate_impl(PyBytesObject *self, PyObject
*table,
changed = 1;
}
if (!changed && PyBytes_CheckExact(input_obj)) {
- Py_DECREF(result);
+ PyBytesWriter_Discard(writer);
return Py_NewRef(input_obj);
}
/* Fix the size of the resulting byte string */
- if (inlen > 0)
- _PyBytes_Resize(&result, output - output_start);
- return result;
+ return PyBytesWriter_FinishWithPointer(writer, output);
}
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]