This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Mutable Sequence Type can work not only with iterable in slice[i:j] = t
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, daniel.urban, docs@python, ezio.melotti, georg.brandl, py.user, rhettinger
Priority: low Keywords:

Created on 2011-07-23 01:39 by py.user, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg140924 - (view) Author: py.user (py.user) * Date: 2011-07-23 01:39
1)
4.6.4 Mutable Sequence Types

| s[i:j] = t |  slice of s from i to j is replaced |
|            |  by the contents of the iterable t  |


>>> lst = list('abc')
>>> barr = bytearray(b'abc')
>>> lst[:1] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
>>> barr[:1] = 4
>>> barr
bytearray(b'\x00\x00\x00\x00bc')
>>>

there is no info about this feature in the documentation

2)
4.6.4 Mutable Sequence Types

| s.extend(x) |  same as s[len(s):len(s)] = x |

>>> lst = list('abc')
>>> barr = bytearray(b'abc')
>>> lst.extend(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> barr.extend(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> lst[len(lst):len(lst)] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
>>> barr[len(barr):len(barr)] = 4
>>> barr
bytearray(b'abc\x00\x00\x00\x00')
>>>

barr.extend(x)  !=  barr[len(barr):len(barr)] = x
msg140926 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-07-23 02:50
To my eye, this looks like a bytearray API bug in the bytearray implementation.  ISTM, the rhs of a slice assignment needs to be restricted to iterable inputs.

I'm marking this as low priority because the documented behaviors (i.e. normal slice assignment from an iterable) appear to be working fine.  So, there is no rush to take away the gratuitous API extension that accepts an integer input where a slice is usually expected.  The extension is not entirely harmless though -- we do lose the error-checking for the common mistake of writing s[i:j]=x instead of s[i:j]=[x].
msg140966 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-07-23 10:45
Yes, this is a bug in bytearray and should be fixed.
msg220550 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-14 12:32
This was fixed in the latest versions.

>>> b = bytearray(b'457')
>>> b[:1] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can assign only bytes, buffers, or iterables of ints in range(0, 256)
>>>
History
Date User Action Args
2022-04-11 14:57:20adminsetgithub: 56826
2014-06-14 12:32:08Claudiu.Popasetstatus: open -> closed

nosy: + Claudiu.Popa
messages: + msg220550

resolution: fixed
stage: resolved
2011-07-23 10:45:36georg.brandlsetversions: + Python 2.7, Python 3.2, - Python 3.1
nosy: + georg.brandl

messages: + msg140966

assignee: docs@python ->
components: - Documentation
2011-07-23 10:07:40daniel.urbansetnosy: + daniel.urban
2011-07-23 02:50:56rhettingersetpriority: normal -> low
nosy: + rhettinger
messages: + msg140926

2011-07-23 01:58:57ezio.melottisetnosy: + ezio.melotti
2011-07-23 01:39:58py.usercreate