[Numpy-discussion] PyInt and Numpy's int64 conversion
Travis Oliphant
travis@continuum...
Fri Jan 6 15:31:45 CST 2012
>>>
>>> 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
>>
>> Ok many thanks !
>>
>> One last thing, do you happen to know how to actually convert an np int64 to
>> a C int ?
>>
>> - x.
>
> Not sure off-hand. You'll have to look at the NumPy scalar API in the C code
What is it you want to do? Do you want to get the C int out of the np.int64 *Python* object?
If so, you do:
npy_int64 val
PyArray_ScalarAsCtype(obj, &val);
If you want to get the C int of a *different* type out of the scalar Python object, you do:
npy_int32 val
PyArray_Descr * outcode = PyArray_DescrFromType(NPY_INT32);
PyArray_CastScalarToCtype(obj, &val, outcode);
Py_DECREF(outcode);
-Travis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120106/4118e67e/attachment.html
More information about the NumPy-Discussion
mailing list