Numpy: ndarray does not accept memoryview for buffer= under python2

Created on 2 Jun 2015  路  4Comments  路  Source: numpy/numpy

Hello up there.

I've discovered that ndarray(buffer=memoryview) fails on python 2:

In [1]: sys.version
Out[1]: '2.7.10 (default, May 26 2015, 13:16:40) \n[GCC 4.9.2]'

In [2]: numpy.__version__
Out[2]: '1.10.0.dev0+9e7a0b2'


In [3]: b = bytearray([1,2,3])

In [4]: b
Out[4]: bytearray(b'\x01\x02\x03')

In [5]: m = memoryview(b)

In [6]: m
Out[6]: <memory at 0x7fa1f4e59b50>

Creating ndarray backed by memoryview fails:

In [7]: a = ndarray((3,), buffer=m, dtype=uint8)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-1528204f5358> in <module>()
----> 1 a = ndarray((3,), buffer=m, dtype=uint8)

TypeError: expected a readable buffer object

though it succeeds if we pass memoryview to array():

In [8]: a = array(m, copy=False)

In [9]: a
Out[9]: array([1, 2, 3], dtype=uint8)

In [10]: a[0] = 5

In [11]: a
Out[11]: array([5, 2, 3], dtype=uint8)

In [12]: b
Out[12]: bytearray(b'\x05\x02\x03')

[7] works on python 3, of course.

Thanks beforehand,
Kirill

01 - Enhancement

All 4 comments

IIRC, we decided not to support memoryview in Python 2 because it was only available for 2.7. However, it has been backported to 2.6 and we no longer support earlier versions, so we should probably revisit that decision.

Yes. Please note it is already supported on Python 2 - array() accepts memoryview ([8-12] above). Only ndarray constructor does not accept memoryview for buffer= argument.

We'd love to have better support for memoryview as well

Closing this, Python 2.x only issues are no longer relevant given that we're dropping support for 2.7 soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thouis picture thouis  路  4Comments

qualiaa picture qualiaa  路  3Comments

kevinzhai80 picture kevinzhai80  路  4Comments

Foadsf picture Foadsf  路  3Comments

inducer picture inducer  路  3Comments