Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ndarray does not accept memoryview for buffer= under python2 #5935

Closed
navytux opened this issue Jun 2, 2015 · 4 comments
Closed

ndarray does not accept memoryview for buffer= under python2 #5935

navytux opened this issue Jun 2, 2015 · 4 comments

Comments

@navytux
Copy link
Contributor

navytux commented Jun 2, 2015

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

@charris
Copy link
Member

charris commented Jun 4, 2015

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.

@navytux
Copy link
Contributor Author

navytux commented Jun 4, 2015

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.

jerome-nexedi pushed a commit to Nexedi/wendelin.core that referenced this issue Jan 21, 2016
Because numpy.ndarray does not accept it as buffer= argument

    numpy/numpy#5935

and our memcpy crashes.

NOTE if we'll need to use memoryview, we can adapt our memcpy to use
array() directly which works with memoryview, as outlined in the above
numpy issue.
@akochepasov
Copy link

akochepasov commented Oct 19, 2016

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

@rgommers
Copy link
Member

rgommers commented Aug 1, 2018

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

@rgommers rgommers closed this as completed Aug 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants