| 309 | } |
| 310 | |
| 311 | Status MakeNumPyView(std::shared_ptr<Array> arr, PyObject* py_ref, int npy_type, int ndim, |
| 312 | npy_intp* dims, PyObject** out) { |
| 313 | PyAcquireGIL lock; |
| 314 | |
| 315 | PyArray_Descr* descr = internal::GetSafeNumPyDtype(npy_type); |
| 316 | set_numpy_metadata(npy_type, arr->type().get(), descr); |
| 317 | PyObject* result = PyArray_NewFromDescr( |
| 318 | &PyArray_Type, descr, ndim, dims, /*strides=*/nullptr, |
| 319 | const_cast<void*>(GetPrimitiveValues(*arr)), /*flags=*/0, nullptr); |
| 320 | PyArrayObject* np_arr = reinterpret_cast<PyArrayObject*>(result); |
| 321 | if (np_arr == nullptr) { |
| 322 | // Error occurred, trust that error set |
| 323 | return Status::OK(); |
| 324 | } |
| 325 | |
| 326 | PyObject* base; |
| 327 | if (py_ref == nullptr) { |
| 328 | // Capsule will be owned by the ndarray, no incref necessary. See |
| 329 | // ARROW-1973 |
| 330 | RETURN_NOT_OK(CapsulizeArray(arr, &base)); |
| 331 | } else { |
| 332 | Py_INCREF(py_ref); |
| 333 | base = py_ref; |
| 334 | } |
| 335 | RETURN_NOT_OK(SetNdarrayBase(np_arr, base)); |
| 336 | |
| 337 | // Do not allow Arrow data to be mutated |
| 338 | PyArray_CLEARFLAGS(np_arr, NPY_ARRAY_WRITEABLE); |
| 339 | *out = result; |
| 340 | return Status::OK(); |
| 341 | } |
| 342 | |
| 343 | class PandasWriter { |
| 344 | public: |
no test coverage detected