Stéfan van der Walt skrev:
>
> As a side note, it may be safer to use the definitions of npy_intp and
> npy_uintp from the numpy header, rather than Py_ssize_t and Py_size_t.
>  In some earlier versions of Python (e.g., 2.4), these were not the
> same.
>   
No. You should use Py_ssize_t for indexing.

The reason is that the C standard does not guarantee that sizeof(void*) 
== sizeof(size_t), and there are platforms where this evaluates to 
false. That is why Python core uses Py_ssize_t instead of Py_intptr_t 
for indexing sequence types (cf. PEP 353). The Py_intptr_t/npy_intp is 
long enough to hold a void*; the indexer should have the same size as 
size_t, thus Py_ssize_t.

The use of npy_intp as indexer in NumPy is a C coding error. It 
accidentally works on computers with flat memory space, e.g. 32 bit x86 
and x64. Although platforms with flat memory are common, it should not 
be encouraged.

> Then, I also have a quick question: why is a person allowed to do
>
> cdef extern from something:
>     ctypedef int Py_ssize_t
>
> when Py_ssize_t is not an int?  Is this a Cython-specific idea?
>   
Because it "behaves like an int", the only difference being the size. 
But sizeof() is evaluated by the C compiler, which knows the true size 
of int and Py_ssize_t.

Sturla Molden

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to