Numpy: ndarray não aceita memoryview para buffer= em python2

Criado em 2 jun. 2015  ·  4Comentários  ·  Fonte: numpy/numpy

Olá lá em cima.

Eu descobri que ndarray(buffer=memoryview) falha no 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>

A criação de ndarray com suporte de memoryview falha:

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

embora tenha sucesso se passarmos memoryview para 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] funciona em python 3, é claro.

Obrigado de antemão,
Kirill

01 - Enhancement

Todos 4 comentários

IIRC, decidimos não oferecer suporte ao memoryview no Python 2 porque estava disponível apenas para 2.7. No entanto, ele foi retroportado para 2.6 e não suportamos mais versões anteriores, então provavelmente devemos rever essa decisão.

sim. Observe que já é suportado no Python 2 - array() aceita memoryview ([8-12] acima). Apenas o construtor ndarray não aceita memoryview para buffer= argumento.

Adoraríamos ter um melhor suporte para memoryview também

Fechando isso, os problemas somente do Python 2.x não são mais relevantes, já que estamos descartando o suporte para 2.7 em breve.

Esta página foi útil?
0 / 5 - 0 avaliações

Questões relacionadas

Levstyle picture Levstyle  ·  3Comentários

'
Pezhvuk picture Pezhvuk  ·  4Comentários

MorBilly picture MorBilly  ·  4Comentários

MareinK picture MareinK  ·  3Comentários

keithbriggs picture keithbriggs  ·  3Comentários