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

Skip to content

Commit 0f3641c

Browse files
committed
Merged revisions 67291 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r67291 | benjamin.peterson | 2008-11-19 15:49:09 -0600 (Wed, 19 Nov 2008) | 5 lines make sure that bytearray methods return a new bytearray even if there is no change Fixes #4348 Reviewed by Brett ........
1 parent 9c22aee commit 0f3641c

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,16 @@ def test_insert(self):
738738
b.insert(0, Indexable(ord('A')))
739739
self.assertEqual(b, b'A')
740740

741+
def test_copied(self):
742+
# Issue 4348. Make sure that operations that don't mutate the array
743+
# copy the bytes.
744+
b = bytearray(b'abc')
745+
#self.assertFalse(b is b.replace(b'abc', b'cde', 0))
746+
747+
t = bytearray([i for i in range(256)])
748+
x = bytearray(b'')
749+
self.assertFalse(x is x.translate(t))
750+
741751
def test_partition_bytearray_doesnt_share_nullstring(self):
742752
a, b, c = bytearray(b"x").partition(b"y")
743753
self.assertEqual(b, b"")

Objects/bytearrayobject.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
13511351
{
13521352
register char *input, *output;
13531353
register const char *table;
1354-
register Py_ssize_t i, c, changed = 0;
1354+
register Py_ssize_t i, c;
13551355
PyObject *input_obj = (PyObject*)self;
13561356
const char *output_start;
13571357
Py_ssize_t inlen;
@@ -1397,14 +1397,8 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
13971397
/* If no deletions are required, use faster code */
13981398
for (i = inlen; --i >= 0; ) {
13991399
c = Py_CHARMASK(*input++);
1400-
if (Py_CHARMASK((*output++ = table[c])) != c)
1401-
changed = 1;
1400+
*output++ = table[c];
14021401
}
1403-
if (changed || !PyByteArray_CheckExact(input_obj))
1404-
goto done;
1405-
Py_DECREF(result);
1406-
Py_INCREF(input_obj);
1407-
result = input_obj;
14081402
goto done;
14091403
}
14101404

@@ -1419,13 +1413,6 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
14191413
if (trans_table[c] != -1)
14201414
if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
14211415
continue;
1422-
changed = 1;
1423-
}
1424-
if (!changed && PyByteArray_CheckExact(input_obj)) {
1425-
Py_DECREF(result);
1426-
Py_INCREF(input_obj);
1427-
result = input_obj;
1428-
goto done;
14291416
}
14301417
/* Fix the size of the resulting string */
14311418
if (inlen > 0)
@@ -1454,8 +1441,7 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
14541441
!memcmp(target+offset+1, pattern+1, length-2) )
14551442

14561443

1457-
/* Bytes ops must return a string. */
1458-
/* If the object is subclass of bytes, create a copy */
1444+
/* Bytes ops must return a string, create a copy */
14591445
Py_LOCAL(PyByteArrayObject *)
14601446
return_self(PyByteArrayObject *self)
14611447
{

0 commit comments

Comments
 (0)