Reputation: 11
I want to see the source code of numpy's dtype class.
"import numpy as np"
"i = np.dtype(int32)"
I see the doc of numpy:
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.dtype.html
I have search the numpy's source code:
https://github.com/numpy/numpy/tree/master/numpy/core
but I cann't find the dtype's source code.
I want to know the actual code to be run when we write np.dtype(int32)
.
I know a little of extend python with c code(swing Cython.etc),and I know the multiarray.pyd is the dll/so write with c.but I cann't find the interface of dtype method in the C source code of multiarray.
And I find some method interface via this:
https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/methods.c
But not find dtype's interface!
Can anyone show the way how to find the source code when we run np.dtype(int32)
?
thanks.
Upvotes: 1
Views: 413
Reputation: 111
The definition of dtype:
PyArray_Descr https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ndarraytypes.h :660
Where the code does np.dtype(int32):
PyArray_DescrConverter https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/descriptor.c :1353
Quote from the comments of PyArray_DescrConverter:
This is the central code that converts Python objects to Type-descriptor objects that are used throughout numpy.
Given an object determine dtype. ie np.array(something) PyArray_DTypeFromObject https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/common.c :104
How did I find this info? Cloned the repo, used egrep. Looked first for "dtype". Found it often with data type "PyArray_Descr". Searched for "PyArray_Descr" found definition in the header and lots of search hits. Refined search with "PyArray_DescrNew" which looked to be more interesting, I found the two functions of interest by looking at the more generic named files.
Upvotes: 1