[issue40440] allow array.array construction from memoryview w/o copy

Serhiy Storchaka report at bugs.python.org
Wed Apr 29 17:56:05 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

array.array should copy the content, to be able to modify it. It implements both the storage for data and the view of that storage.

What you want is already implemented as the memoryview object.

>>> import array
>>> x = array.array('b', [1,2,3,4])
>>> x
array('b', [1, 2, 3, 4])
>>> z = memoryview(x).cast('h')
>>> z
<memory at 0x7f31e79d2c80>
>>> list(z)
[513, 1027]
>>> z[0] = 42
>>> x
array('b', [42, 0, 3, 4])
>>> x.append(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BufferError: cannot resize an array that is exporting buffers

----------
nosy: +serhiy.storchaka, skrah

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40440>
_______________________________________


More information about the Python-bugs-list mailing list