Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
46cc6d11
Commit
46cc6d11
authored
Nov 19, 2008
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make sure that bytearray methods return a new bytearray even if there is no change
Fixes #4348 Reviewed by Brett
parent
3b30e2c8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
21 deletions
+16
-21
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+10
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/bytearrayobject.c
Objects/bytearrayobject.c
+3
-21
No files found.
Lib/test/test_bytes.py
View file @
46cc6d11
...
...
@@ -721,6 +721,16 @@ class ByteArrayTest(BaseBytesTest):
b.insert(0, Indexable(ord('A')))
self.assertEqual(b, b'A')
def test_copied(self):
# Issue 4348. Make sure that operations that don't mutate the array
# copy the bytes.
b = bytearray(b'abc')
#self.assertFalse(b is b.replace(b'abc', b'cde', 0))
t = bytearray([i for i in range(256)])
x = bytearray(b'')
self.assertFalse(x is x.translate(t))
def test_partition_bytearray_doesnt_share_nullstring(self):
a, b, c = bytearray(b"
x
").partition(b"
y
")
self.assertEqual(b, b"")
...
...
Misc/NEWS
View file @
46cc6d11
...
...
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins
-----------------
- Issue #4348: Some bytearray methods returned that didn't cause any change to
the bytearray, returned the same bytearray instead of a copy.
- Issue #4317: Fixed a crash in the imageop.rgb2rgb8() function.
- Issue #4230: If ``__getattr__`` is a descriptor, it now functions correctly.
...
...
Objects/bytearrayobject.c
View file @
46cc6d11
...
...
@@ -1423,7 +1423,7 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
{
register
char
*
input
,
*
output
;
register
const
char
*
table
;
register
Py_ssize_t
i
,
c
,
changed
=
0
;
register
Py_ssize_t
i
,
c
;
PyObject
*
input_obj
=
(
PyObject
*
)
self
;
const
char
*
output_start
;
Py_ssize_t
inlen
;
...
...
@@ -1469,14 +1469,8 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
/* If no deletions are required, use faster code */
for
(
i
=
inlen
;
--
i
>=
0
;
)
{
c
=
Py_CHARMASK
(
*
input
++
);
if
(
Py_CHARMASK
((
*
output
++
=
table
[
c
]))
!=
c
)
changed
=
1
;
*
output
++
=
table
[
c
];
}
if
(
changed
||
!
PyByteArray_CheckExact
(
input_obj
))
goto
done
;
Py_DECREF
(
result
);
Py_INCREF
(
input_obj
);
result
=
input_obj
;
goto
done
;
}
...
...
@@ -1491,13 +1485,6 @@ bytes_translate(PyByteArrayObject *self, PyObject *args)
if
(
trans_table
[
c
]
!=
-
1
)
if
(
Py_CHARMASK
(
*
output
++
=
(
char
)
trans_table
[
c
])
==
c
)
continue
;
changed
=
1
;
}
if
(
!
changed
&&
PyByteArray_CheckExact
(
input_obj
))
{
Py_DECREF
(
result
);
Py_INCREF
(
input_obj
);
result
=
input_obj
;
goto
done
;
}
/* Fix the size of the resulting string */
if
(
inlen
>
0
)
...
...
@@ -1526,15 +1513,10 @@ done:
!memcmp(target+offset+1, pattern+1, length-2) )
/* Bytes ops must return a string. */
/* If the object is subclass of bytes, create a copy */
/* Bytes ops must return a string, create a copy */
Py_LOCAL
(
PyByteArrayObject
*
)
return_self
(
PyByteArrayObject
*
self
)
{
if
(
PyByteArray_CheckExact
(
self
))
{
Py_INCREF
(
self
);
return
(
PyByteArrayObject
*
)
self
;
}
return
(
PyByteArrayObject
*
)
PyByteArray_FromStringAndSize
(
PyByteArray_AS_STRING
(
self
),
PyByteArray_GET_SIZE
(
self
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment