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

Berthold Hoellmann berthold.hoellmann@gl-group....
Fri Jun 18 10:22:10 CDT 2010


Robert Kern <robert.kern@gmail.com> writes:

> On Fri, Jun 18, 2010 at 09:43, Berthold Hoellmann
> <berthold.hoellmann@gl-group.com> wrote:
>> 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.
>
> It is referring to the Python int type, not the C int type. Python
> ints are actually C longs underneath.

So it should be called "Platform python int" instead of "Platform int"
in the documetation. 

I now use 'sizeof' and 'c_int' from ctypes tp construct the dtype
description: 

  dtype('i%d' % sizeof(c_int))

Is there a way to avoid the usage of "ctypes" by using information
provided by numpy?

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