[issue4580] slicing of memoryviews when itemsize != 1 is wrong
report at bugs.python.org
Tue Dec 9 14:37:51 CET 2008
Nick Coghlan <ncoghlan at gmail.com> added the comment:
Copy & paste from python-dev post:
The problem with memoryview appears to be related to the way it
calculates its own length (since that is the check that is failing when
the view blows up):
>>> a = array('i', range(10))
>>> m = memoryview(a)
>>> len(m) # This is the length in bytes, which is WRONG!
>>> m2 = memoryview(a)[2:8]
>>> len(m2) # This is correct
>>> a2 = array('i', range(6))
>>> m[:] = a # But this works
>>> m2[:] = a2 # and this does not
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot modify size of memoryview object
>>> len(memoryview(a2)) # Ah, 24 != 6 is our problem!
Looks to me like there are a couple of bugs here:
The first is that memoryview is treating the len field in the Py_buffer
struct as the number of objects in the view in a few places instead of
as the total number of bytes being exposed (it is actually the latter,
as defined in PEP 3118).
The second is that the getbuf implementation in array.array is broken.
It is ONLY OK for shape to be null when ndim=0 (i.e. a scalar value). An
array is NOT a scalar value, so the array objects should be setting the
shape pointer to point to an single item array (where shape is the
length of the array).
memoryview can then be fixed to use shape instead of len to get the
number of objects in the view.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list