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
67dc4a87
Commit
67dc4a87
authored
Nov 03, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now raises an error.
parent
ef317387
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
0 deletions
+29
-0
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+20
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/bytearrayobject.c
Objects/bytearrayobject.c
+6
-0
No files found.
Lib/test/test_bytes.py
View file @
67dc4a87
...
...
@@ -635,6 +635,26 @@ class ByteArrayTest(BaseBytesTest):
b[3:0] = [42, 42, 42]
self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9]))
b[3:] = b'
foo
'
self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111]))
b[:3] = memoryview(b'
foo
')
self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111]))
b[3:4] = []
self.assertEqual(b, bytearray([102, 111, 111, 111, 111]))
b[1:] = list(b'
uuuu
') # this works only on Python2
self.assertEqual(b, bytearray([102, 117, 117, 117, 117]))
for elem in [5, -5, 0, long(10e20), u'
str
', 2.3, [u'
a
', u'b'], [[]]]:
with self.assertRaises(TypeError):
b[3:4] = elem
for elem in [[254, 255, 256], [-256, 9000]]:
with self.assertRaises(ValueError):
b[3:4] = elem
def test_extended_set_del_slice(self):
indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
for start in indices:
...
...
Misc/NEWS
View file @
67dc4a87
...
...
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
Core and Builtins
-----------------
- Issue #8401: assigning an int to a bytearray slice (e.g. b[3:4] = 5) now
raises an error.
- Issue #14700: Fix buggy overflow checks for large width and precision
in string formatting operations.
...
...
Objects/bytearrayobject.c
View file @
67dc4a87
...
...
@@ -636,6 +636,12 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
needed
=
0
;
}
else
if
(
values
==
(
PyObject
*
)
self
||
!
PyByteArray_Check
(
values
))
{
if
(
PyNumber_Check
(
values
)
||
PyUnicode_Check
(
values
))
{
PyErr_SetString
(
PyExc_TypeError
,
"can assign only bytes, buffers, or iterables "
"of ints in range(0, 256)"
);
return
-
1
;
}
/* Make a copy and call this function recursively */
int
err
;
values
=
PyByteArray_FromObject
(
values
);
...
...
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