From 3d6e5eeb3abaaaf91df6b328cc97613c63370dde Mon Sep 17 00:00:00 2001 From: "Korn, Uwe" Date: Fri, 15 Feb 2019 12:54:32 +0100 Subject: [PATCH 1/2] =?UTF-8?q?ARROW-4582:=20[Python/C++]=C2=A0Acquire=20t?= =?UTF-8?q?he=20GIL=20on=20Py=5FINCREF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/src/arrow/python/numpy_convert.cc | 32 +++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/cpp/src/arrow/python/numpy_convert.cc b/cpp/src/arrow/python/numpy_convert.cc index c73e0bc15c9..6cb3b8fab6f 100644 --- a/cpp/src/arrow/python/numpy_convert.cc +++ b/cpp/src/arrow/python/numpy_convert.cc @@ -46,6 +46,7 @@ bool is_contiguous(PyObject* array) { } NumPyBuffer::NumPyBuffer(PyObject* ao) : Buffer(nullptr, 0) { + PyAcquireGIL lock; arr_ = ao; Py_INCREF(ao); @@ -187,7 +188,6 @@ Status NumPyDtypeToArrow(PyArray_Descr* descr, std::shared_ptr* out) { #undef TO_ARROW_TYPE_CASE Status NdarrayToTensor(MemoryPool* pool, PyObject* ao, std::shared_ptr* out) { - PyAcquireGIL lock; if (!PyArray_Check(ao)) { return Status::TypeError("Did not pass ndarray object"); @@ -199,25 +199,29 @@ Status NdarrayToTensor(MemoryPool* pool, PyObject* ao, std::shared_ptr* int ndim = PyArray_NDIM(ndarray); + // This is also holding the GIL, so don't already draw it. std::shared_ptr data = std::make_shared(ao); std::vector shape(ndim); std::vector strides(ndim); - npy_intp* array_strides = PyArray_STRIDES(ndarray); - npy_intp* array_shape = PyArray_SHAPE(ndarray); - for (int i = 0; i < ndim; ++i) { - if (array_strides[i] < 0) { - return Status::Invalid("Negative ndarray strides not supported"); + { + PyAcquireGIL lock; + npy_intp* array_strides = PyArray_STRIDES(ndarray); + npy_intp* array_shape = PyArray_SHAPE(ndarray); + for (int i = 0; i < ndim; ++i) { + if (array_strides[i] < 0) { + return Status::Invalid("Negative ndarray strides not supported"); + } + shape[i] = array_shape[i]; + strides[i] = array_strides[i]; } - shape[i] = array_shape[i]; - strides[i] = array_strides[i]; - } - std::shared_ptr type; - RETURN_NOT_OK( - GetTensorType(reinterpret_cast(PyArray_DESCR(ndarray)), &type)); - *out = std::make_shared(type, data, shape, strides); - return Status::OK(); + std::shared_ptr type; + RETURN_NOT_OK( + GetTensorType(reinterpret_cast(PyArray_DESCR(ndarray)), &type)); + *out = std::make_shared(type, data, shape, strides); + return Status::OK(); + } } Status TensorToNdarray(const std::shared_ptr& tensor, PyObject* base, From 7f9838da5d41a8a6bdfa3cfec6a069cd12706090 Mon Sep 17 00:00:00 2001 From: "Korn, Uwe" Date: Fri, 15 Feb 2019 13:48:49 +0100 Subject: [PATCH 2/2] docker-compose run clang-format --- cpp/src/arrow/python/numpy_convert.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/cpp/src/arrow/python/numpy_convert.cc b/cpp/src/arrow/python/numpy_convert.cc index 6cb3b8fab6f..02ce0b661ed 100644 --- a/cpp/src/arrow/python/numpy_convert.cc +++ b/cpp/src/arrow/python/numpy_convert.cc @@ -188,7 +188,6 @@ Status NumPyDtypeToArrow(PyArray_Descr* descr, std::shared_ptr* out) { #undef TO_ARROW_TYPE_CASE Status NdarrayToTensor(MemoryPool* pool, PyObject* ao, std::shared_ptr* out) { - if (!PyArray_Check(ao)) { return Status::TypeError("Did not pass ndarray object"); }