This function cannot return NULL, but it can fail, So call PyErr_Occurred to check if it failed after calling it.
| 94 | // So call PyErr_Occurred to check if it failed after |
| 95 | // calling it. |
| 96 | static DLDevice |
| 97 | array_get_dl_device(PyArrayObject *self) { |
| 98 | DLDevice ret; |
| 99 | ret.device_type = kDLCPU; |
| 100 | ret.device_id = 0; |
| 101 | PyObject *base = PyArray_BASE(self); |
| 102 | |
| 103 | // walk the bases (see gh-20340) |
| 104 | while (base != NULL && PyArray_Check(base)) { |
| 105 | base = PyArray_BASE((PyArrayObject *)base); |
| 106 | } |
| 107 | |
| 108 | // The outer if is due to the fact that NumPy arrays are on the CPU |
| 109 | // by default (if not created from DLPack). |
| 110 | if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { |
| 111 | DLManagedTensor *managed = PyCapsule_GetPointer( |
| 112 | base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); |
| 113 | if (managed == NULL) { |
| 114 | return ret; |
| 115 | } |
| 116 | return managed->dl_tensor.device; |
| 117 | } |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | PyObject * |
no test coverage detected