public static void Clear(PyObject o) { while (PyObject.Size(o) != -1) { Py.DecRef(o); } }
public static void Initialize() { //PyObject* numpy = PyImport_ImportModule("numpy.core.multiarray"); _numpy = PyImport.ImportModule("numpy.core.multiarray"); if (_numpy == IntPtr.Zero) { //PyErr.SetString(PyExc.ImportError, "numpy.core.multiarray failed to import"); throw new Exception("numpy.core.multiarray failed to import"); } _cApi = _numpy["_ARRAY_API"]; if (_cApi == IntPtr.Zero) { //PyErr_SetString(PyExc_AttributeError, "_ARRAY_API not found"); throw new Exception("_ARRAY_API not found"); } _pyArrayAPI = PyCapsule.GetPointer(_cApi, null); Py.DecRef(_cApi); if (_pyArrayAPI == IntPtr.Zero) { //PyErr_SetString(PyExc_RuntimeError, "_ARRAY_API is NULL pointer"); throw new Exception("_ARRAY_API is NULL pointer"); } Marshal.Copy(_pyArrayAPI, _pyArrayAPIs, 0, _pyArrayAPIs.Length); PyArrayDescrFromType = (PyArrayDescrFromTypeDelegate)Marshal.GetDelegateForFunctionPointer(_pyArrayAPIs[MultiarrayFuncsAPI.PyArray_DescrFromType], typeof(PyArrayDescrFromTypeDelegate)); PyArrayFromBuffer = (PyArrayFromBufferDelegate)Marshal.GetDelegateForFunctionPointer(_pyArrayAPIs[MultiarrayFuncsAPI.PyArray_FromBuffer], typeof(PyArrayFromBufferDelegate)); PyArrayNewFromDescr = (PyArrayNewFromDescrDelegate)Marshal.GetDelegateForFunctionPointer(_pyArrayAPIs[MultiarrayFuncsAPI.PyArray_NewFromDescr], typeof(PyArrayNewFromDescrDelegate)); PyArrayGetPtr = (PyArrayGetPtrDelegate)Marshal.GetDelegateForFunctionPointer(_pyArrayAPIs[MultiarrayFuncsAPI.PyArray_GetPtr], typeof(PyArrayGetPtrDelegate)); PyArrayType = _pyArrayAPIs[MultiarrayTypesAPI.PyArray_Type]; }