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));