[issue4580] slicing of memoryviews when itemsize != 1 is wrong

Nick Coghlan report at bugs.python.org
Wed Dec 10 12:07:38 CET 2008


Nick Coghlan <ncoghlan at gmail.com> added the comment:

The reason memoryview's current len() implementation is wrong is because
its indexing is per element in the original object, not per byte:

>>> a = array('i', range(10))
>>> m = memoryview(a)
>>> for i in range(len(m)):
...   print(m[i])
...
b'\x00\x00\x00\x00'
b'\x01\x00\x00\x00'
b'\x02\x00\x00\x00'
b'\x03\x00\x00\x00'
b'\x04\x00\x00\x00'
b'\x05\x00\x00\x00'
b'\x06\x00\x00\x00'
b'\x07\x00\x00\x00'
b'\x08\x00\x00\x00'
b'\t\x00\x00\x00'
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: index out of bounds

Oops.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4580>
_______________________________________


More information about the Python-bugs-list mailing list