[Cython] Problems accessing ndarray attributes
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Sat Jan 17 16:01:05 CET 2009
Hi Ilmar,
Ilmar Wilbers wrote:
> Hi list,
>
> While trying to wrap an external C library, I have stumbled upon some
> question. A lot of them were answered by going through the mailing list
> archive, however a few remain.
>
> I have the following function with arguments:
> def tridag(np.ndarray[DTYPE_t, ndim=1, negative_indices=False] a)
>
> I want to do some assertions on the array a:
> assert a.dtype == np.float32
> assert a.flags['C_CONTIGUOUS']
> but keep getting an error "Attempting to index non-array type 'int'".
> Checking the type of a.flags indicates that it is in int. What am I
> missing?
You have encountered one of the less polished aspects of Cython I'm
afraid. The flags field is typed as int to give efficient access for
or-style flags checking, which precludes the object behaviour you use.
To make a long story short, here's your current options:
- assert (<object>a).flags[....]
- The assertion for datatype is already made if DTYPE_t is float32_t. Use
mode="c" as a flag to ndarray to do the other assertion automatically (on
entering the function).
This is definitely something that should be more done something about
(i.e. this should Just Work), I'll try to file an enhancement ticket for
it.
Dag Sverre
More information about the Cython-dev
mailing list