Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,13 +1116,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')
Expand Down
18 changes: 9 additions & 9 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,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)) {
Expand Down Expand Up @@ -2320,13 +2319,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) {
Expand All @@ -2335,14 +2334,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;
Expand All @@ -2369,13 +2371,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);
}


Expand Down
Loading