| 663 | } |
| 664 | |
| 665 | PyObject* dtype_mgb2np(mgb::DType dtype) { |
| 666 | PYTHON_GIL; |
| 667 | // According to |
| 668 | // https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_TypeObjectFromType |
| 669 | // the following is equivalent to PyArray_TypeObjectFromType for built-in |
| 670 | // types. |
| 671 | if (!dtype.valid()) { |
| 672 | Py_XINCREF(Py_None); |
| 673 | return Py_None; |
| 674 | } |
| 675 | auto descr = dtype_mgb2np_descr(dtype); |
| 676 | if (descr == nullptr) { |
| 677 | Py_XINCREF(Py_None); |
| 678 | return Py_None; |
| 679 | } |
| 680 | static bool use_typeobj_as_dtype = MGB_GETENV("MGE_USE_TYPEOBJ_AS_DTYPE"); |
| 681 | if (use_typeobj_as_dtype) { |
| 682 | if (dtype.has_param()) { |
| 683 | return reinterpret_cast<PyObject*>(descr.release()); |
| 684 | } |
| 685 | PyObject* typeobj = reinterpret_cast<PyObject*>(descr->typeobj); |
| 686 | Py_XINCREF(typeobj); |
| 687 | return typeobj; |
| 688 | } else { |
| 689 | return reinterpret_cast<PyObject*>(descr.release()); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | mgb::DType dtype_np2mgb(PyObject* obj) { |
| 694 | mgb_assert(obj && obj != Py_None, "can not convert null PyObject to numpy dtype"); |
no test coverage detected