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