Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does creating this memoryview raise a ValueError only when assigning to a variable?

Pythons memoryview does not support datetime64 or timedelta. Ok. But when I try to create a memoryview of a structured array that includes a datetime64 or timedelta, it appears to work... unless I assign it to a variable!

In [19]: memoryview(zeros(10, dtype=[("A", "m8[s]")]))
Out[19]: <memory at 0x7f1d455d6048>

In [20]: x = memoryview(zeros(10, dtype=[("A", "m8[s]")]))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: cannot include dtype 'm' in a buffer

In [21]: x = _19

In [22]: x
Out[22]: <memory at 0x7f1d455d6048>

This seriously challenges my understanding of the way Python fundamentally works. How can f() and x = f() be different, considering that (1) IPythons REPL assigns the output to _19 anyway, and (2) the function/class memoryview has no way of knowing what the caller is going to do with its output?

I am running the code on Python 3.4.1, numpy 1.10.0.dev+fbcc24f, on Linux 2.6.32-431.23.3.el6.x86_64, Scientific Linux release 6.6.


EDIT

On Python 3.5, numpy 1.10.4, I get:

In [50]: memoryview(numpy.zeros(10, dtype=[("A", "m8[s]")]))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: cannot include dtype 'm' in a buffer

During handling of the above exception, another exception occurred:

SystemError                               Traceback (most recent call last)
<ipython-input-50-5d5ac6c085fa> in <module>()
----> 1 memoryview(numpy.zeros(10, dtype=[("A", "m8[s]")]))

SystemError: <class 'memoryview'> returned a result with an error set

I have filed a bug with numpy, although I'm not quite sure that's where the problem lies.

like image 373
gerrit Avatar asked Feb 24 '15 23:02

gerrit


1 Answers

There is something very odd going on here.

>>> memoryview(zeros(10, dtype=[("A", "m8[s]")]))
<memory at 0x102654348>
>>> 
ValueError: cannot include dtype 'm' in a buffer

My conjecture is that this is related to https://bugs.python.org/issue23571. Some C function underlying memoryview is both returning a non-null result and setting an error flag. This apparently causes the error to be raised when the next statement is executed! In Python 3.5, the interpreter raises a SystemError instead when this condition occurs.

It seems like the real bug here is with the memoryview function, not with numpy.

like image 108
augurar Avatar answered Oct 13 '22 01:10

augurar