[Numpy-discussion] checking for array type in C extension

Berthold Hoellmann berthold.hoellmann@gl-group....
Fri Jun 18 09:43:34 CDT 2010


Pauli Virtanen <pav@iki.fi> writes:

> pe, 2010-06-18 kello 12:49 +0200, Berthold Hoellmann kirjoitti:
> [clip]
>> tst.inttestfunc(np.array((1,2),dtype=np.int))
>> tst.inttestfunc(np.array((1,2),dtype=np.int8))
>> tst.inttestfunc(np.array((1,2),dtype=np.int16))
>> tst.inttestfunc(np.array((1,2),dtype=np.int32))
>> tst.inttestfunc(np.array((1,2),dtype=np.int64))
>> 
>> hoel@pc090498 ~/pytest $ PYTHONPATH=build/lib.win32-2.5/ python xx.py
>> 1.4.1 ['C:\\Python25\\lib\\site-packages\\numpy']
>> PyArray_TYPE(array): 7; NPY_INT: 5
>> NPY_INT not found
>> PyArray_TYPE(array): 1; NPY_INT: 5
>> NPY_INT not found
>> PyArray_TYPE(array): 3; NPY_INT: 5
>> NPY_INT not found
>> PyArray_TYPE(array): 7; NPY_INT: 5
>> NPY_INT not found
>> PyArray_TYPE(array): 9; NPY_INT: 5
>> NPY_INT not found
>>
>> NPY_INT32 is 7, but shouldn't NPY_INT correspond to numpy.int. And what
>> kind of int is NPY_INT in this case?
>
> I think the explanation is the following:
>
> - NPY_INT is a virtual type that is either int32 or int64, depending on
>   the native platform size.
>
> - It has its own type code, distinct from NPY_SHORT (= 3 = int32) and
>   NPY_LONG (= 7 = int64).
>
> - But the type specifier is replaced either by NPY_SHORT or NPY_LONG
>   on array creation, so no array is of this dtype.
>
> 	Pauli

The documentation (i refere to NumPy User Guide, Release 1.5.0.dev8106)
claims that numpy.int is platform int and that "NPY_INT" is a C based
name, thus referring to int also. So if I want to use "int" platform
independent in my extension module, what do I do to check whether the
correct data is proivided. I want to avoid one of the casting routines,
such as "PyArray_FROM_OTF" or "PyArray_FromAny"? Do I have to use
"PyArray_ITEMSIZE"? But "PyArray_ITEMSIZE" indicates that "numpy.int"
are not platform integers. It returns "8" on Linux64, where
"sizeof(int)==4".

So is this a bug in 1.4.1, in the 1.5 beta documentation or a
misunderstanding on my side?

Kind regards

Berthold Höllmann
-- 
Germanischer Lloyd AG
Berthold Höllmann
Project Engineer, CAE Development
Brooktorkai 18
20457 Hamburg
Germany
Phone: +49(0)40 36149-7374
Fax: +49(0)40 36149-7320
e-mail: berthold.hoellmann@gl-group.com
Internet: http://www.gl-group.com


More information about the NumPy-Discussion mailing list