[Cython] Automatic buffer dtype conversion?
Magnus Lie Hetland
magnus at hetland.org
Mon Dec 15 17:09:24 CET 2008
Hi!
I've been trying to get the simple Cython+NumPy example from Robert
Bradshaw's slides to work:
> # footest.pyx
> cimport numpy
>
> def sum(x):
> cdef numpy.ndarray[int, ndim=1] arr = x
> cdef int i, s = 0
> for i in range(arr.shape[0]):
> s += arr[i]
> return s
I get it to compile/link, but then I try to actually use it, with the
following code:
> import footest
> import numpy as np
>
> a = np.arange(1000, dtype=np.int)
> print footest.sum(a)
The np.int shouldn't be necessary -- and I started without it -- but
even *with* it, I get the following error:
> File "footest.pyx", line 4, in footest.sum (footest.c:392)
> cdef numpy.ndarray[int, ndim=1] arr = x
> ValueError: Buffer dtype mismatch (expected int, got long)
In other words, there seems to have been a conversion from int to
long. If I try to print out repr(x), it seems to have a dtype of
int32. If I change the offending line from the pyx file to
> cdef numpy.ndarray[long, ndim=1] arr = x
it suddenly works.
I'm currently using Cython 0.10.2 with Python 2.5.2 and NumPy 1.2.1 in
Mac OS X (Leopard).
Am I missing something obvious? Any ideas?
--
Magnus Lie Hetland
http://hetland.org
More information about the Cython-dev
mailing list