[Numpy-discussion] numpy, Py_ssize_t, cython and 64 bits python 2.4

Dag Sverre Seljebotn dagss@student.matnat.uio...
Mon Nov 10 04:22:47 CST 2008

Charles R Harris wrote:
> On Sun, Nov 9, 2008 at 4:37 PM, Dag Sverre Seljebotn 
> <dagss@student.matnat.uio.no <mailto:dagss@student.matnat.uio.no>> wrote:
>     Charles R Harris wrote:
>     > But mixing python types and numpy types is not a good idea, they
>     exist
>     > separately and apply to different software. This may be
>     different for
>     > the buffer interface, which is likely to cross the boundary, but
>     that is
>     > an argument for being very careful on how the buffer interface
>     is dealt
>     > with in cython.
>     >
>     Which is why an exception is thrown in this mismatch circumstance
>     -- if
>     we weren't careful, the cast would happen, which would be very bad.
> I think the arguments should be copied and explicitly cast. It's 
> nasty, but crossing boundaries always is. There are some old 
> compatibility functions in numpy that do the same thing.
> Isn't this a bit conservative? It looks to me like long and double 
> would be good bets for a lot of these. The boolean type, '?', is also 
> missing.
> 186
> 187 ctypedef signed int npy_byte
> 188 ctypedef signed int npy_short
> 189 ctypedef signed int npy_int

As Robert mentioned, Cython is in many ways type size agnostic -- it 
simply needs to know that npy_longlong is a signed int for the purposes 
of coercion etc, but it does not need to know the size. The whole point 
of this is to as far as possible delegate this to the C compiler. By the 
time one has to be "conservative" or "optimistic" rather than exact one 
has already failed in this area IMO.

(This is because the ctypedefs are "external", so Cython just uses the 
name directly for the C code (i.e. "npy_byte" is inserted as a string in 
the C source). The syntax is unfortunately a bit confusing though; 
suggestions for improvements welcome.)

The one area you do get problems with this is if you try to assign from 
a npy_byte* to a npy_short*, which Cython will allow. But the C compiler 
will still raise an error in that case and so it is only an issue of 
user-friendliness (though it should probably be fixed somehow, perhaps 
by making all external typedefs size-incompatible from a Cython 
perspective and require casts, but that must be run by the Cython 

This is done so that one doesn't have to duplicate all the 
#ifdef-definitions (for which there wouldn't even by any Cython 
mechanism, though we could add one).

Dag Sverre

More information about the Numpy-discussion mailing list