[Numpy-discussion] PyInt and Numpy's int64 conversion

Wes McKinney wesmckinn at gmail.com
Sat Dec 24 19:51:06 EST 2011


On Sat, Dec 24, 2011 at 3:11 AM, xantares 09 <xantares09 at hotmail.com> wrote:
>
>
>> From: wesmckinn at gmail.com
>> Date: Fri, 23 Dec 2011 12:31:45 -0500
>> To: numpy-discussion at scipy.org
>> Subject: Re: [Numpy-discussion] PyInt and Numpy's int64 conversion
>
>>
>> On Fri, Dec 23, 2011 at 4:37 AM, xantares 09 <xantares09 at hotmail.com>
>> wrote:
>> > Hi,
>> >
>> > I'm using Numpy from the C python api side while tweaking my SWIG
>> > interface
>> > to work with numpy array types.
>> > I want to convert a numpy array of integers (whose elements are numpy's
>> > 'int64')
>> > The problem is that it this int64 type is not compatible with the
>> > standard
>> > python integer type:
>> > I cannot use PyInt_Check, and PyInt_AsUnsignedLongMask to check and
>> > convert
>> > from int64: basically PyInt_Check returns false.
>> > I checked the numpy config header and npy_int64 does have a size of 8o,
>> > which should be the same as int on my x86_64.
>> > What is the correct way to do that ?
>> > I checked for a Int64_Check function and didn't find any in numpy
>> > headers.
>> >
>> > Regards,
>> >
>> > x.
>> >
>> > _______________________________________________
>> > NumPy-Discussion mailing list
>> > NumPy-Discussion at scipy.org
>> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> >
>>
>> hello,
>>
>> I think you'll want to use the C macro PyArray_IsIntegerScalar, e.g.
>> in pandas I have the following function exposed to my Cython code:
>>
>> PANDAS_INLINE int
>> is_integer_object(PyObject* obj) {
>> return PyArray_IsIntegerScalar(obj);
>> }
>>
>> last time I checked that macro detects Python int, long, and all of
>> the NumPy integer hierarchy (int8, 16, 32, 64). If you ONLY want to
>> check for int64 I am not 100% sure the best way.
>>
>> - Wes
>
> Hi,
>
> Thank you for your reply !
>
> That's the thing : I want to check/convert every type of integer, numpy's
> int64 and also python standard ints.
> Is there a way to avoid to use only the python api ? ( and avoid to depend
> on numpy's PyArray_* functions )
>
> Regards.
>
> x.
>
>
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

No. All of the PyTypeObject objects for the NumPy array scalars are
explicitly part of the NumPy C API so you have no choice but to depend
on that (to get the best performance). If you want to ONLY check for
int64 at the C API level, I did a bit of digging and the relevant type
definitions are in

https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/npy_common.h

so you'll want to do:

int is_int64(PyObject* obj){
  return PyObject_TypeCheck(obj, &PyInt64ArrType_Type);
}

and that will *only* detect np.int64

- Wes



More information about the NumPy-Discussion mailing list