33 PyTypeObject* py_type_ptr,
const std::type_info* type_info_ptr,
34 const int type_size,
const int alignement, PyArray_GetItemFunc* getitem,
35 PyArray_SetItemFunc* setitem, PyArray_NonzeroFunc* nonzero,
36 PyArray_CopySwapFunc* copyswap, PyArray_CopySwapNFunc* copyswapn,
37 PyArray_DotFunc* dotfunc, PyArray_FillFunc*
fill,
38 PyArray_FillWithScalarFunc* fillwithscalar) {
39 bp::tuple tp_bases_extended(
40 bp::make_tuple(bp::handle<>(bp::borrowed(&PyGenericArrType_Type))));
42 bp::tuple(bp::handle<>(bp::borrowed(py_type_ptr->tp_bases)));
44 Py_INCREF(tp_bases_extended.ptr());
45 py_type_ptr->tp_bases = tp_bases_extended.ptr();
47 py_type_ptr->tp_flags &= ~Py_TPFLAGS_READY;
49 if (PyType_Ready(py_type_ptr) <
52 throw std::invalid_argument(
"PyType_Ready fails to initialize input type.");
55 PyArray_Descr* descr_ptr =
57 PyArray_Descr& descr = *descr_ptr;
58 descr.typeobj = py_type_ptr;
60 descr.byteorder =
'=';
62 descr.elsize = type_size;
64 NPY_NEEDS_PYAPI | NPY_USE_GETITEM | NPY_USE_SETITEM | NPY_NEEDS_INIT;
71 PyArray_ArrFuncs* funcs_ptr =
new PyArray_ArrFuncs;
72 PyArray_ArrFuncs& funcs = *funcs_ptr;
75 funcs.getitem = getitem;
76 funcs.setitem = setitem;
77 funcs.nonzero = nonzero;
78 funcs.copyswap = copyswap;
79 funcs.copyswapn = copyswapn;
80 funcs.dotfunc = dotfunc;
82 funcs.fillwithscalar = fillwithscalar;
86 assert(code >= 0 &&
"The return code should be positive");
89 if (PyDict_SetItemString(py_type_ptr->tp_dict,
"dtype",
90 (PyObject*)descr_ptr) < 0) {
91 throw std::invalid_argument(
"PyDict_SetItemString fails.");
95 std::make_pair(type_info_ptr, py_type_ptr));
static PyArray_Descr * getPyArrayDescr()
MapCode py_array_code_bindings
Structure collecting all the types registers in Numpy via EigenPy.
MapDescr py_array_descr_bindings
static Register & instance()
static bool isRegistered()
int call_PyArray_RegisterDataType(PyArray_Descr *dtype)
MapInfo type_to_py_type_bindings
PyArray_Descr * call_PyArray_DescrFromType(int typenum)
static int registerNewType(PyTypeObject *py_type_ptr, const std::type_info *type_info_ptr, const int type_size, const int alignment, PyArray_GetItemFunc *getitem, PyArray_SetItemFunc *setitem, PyArray_NonzeroFunc *nonzero, PyArray_CopySwapFunc *copyswap, PyArray_CopySwapNFunc *copyswapn, PyArray_DotFunc *dotfunc, PyArray_FillFunc *fill, PyArray_FillWithScalarFunc *fillwithscalar)
void call_PyArray_InitArrFuncs(PyArray_ArrFuncs *funcs)
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)
int EIGENPY_DLLAPI PyArray_TypeNum(PyTypeObject *type)