From 0aefe038c9953c4ec868f023d13528121834877e Mon Sep 17 00:00:00 2001 From: Soyorin69 Date: Wed, 2 Dec 2026 09:36:43 +0800 Subject: [PATCH] GH-31597: Correct GIL declarations in PyArrow Move Python-object-dependent Cython declarations out of nogil blocks and update their call sites. Preserve nogil for pandas conversion functions that may execute work on helper threads. --- python/pyarrow/array.pxi | 12 ++-- python/pyarrow/includes/libarrow_python.pxd | 79 +++++++++++---------- python/pyarrow/scalar.pxi | 3 +- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index ac3b8733463..b0ab001a6ec 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -48,10 +48,9 @@ cdef _sequence_to_array(object sequence, object mask, object size, options.from_pandas = from_pandas options.ignore_timezone = os.environ.get('PYARROW_IGNORE_TIMEZONE', False) - with nogil: - chunked = GetResultValue( - ConvertPySequence(sequence, mask, options, pool) - ) + chunked = GetResultValue( + ConvertPySequence(sequence, mask, options, pool) + ) if chunked.get().num_chunks() == 1: return pyarrow_wrap_array(chunked.get().chunk(0)) @@ -93,9 +92,8 @@ cdef _ndarray_to_array(object values, object mask, DataType type, shared_ptr[CDataType] c_type = _ndarray_to_type(values, type) CCastOptions cast_options = CCastOptions(safe) - with nogil: - check_status(NdarrayToArrow(pool, values, mask, from_pandas, - c_type, cast_options, &chunked_out)) + check_status(NdarrayToArrow(pool, values, mask, from_pandas, + c_type, cast_options, &chunked_out)) if chunked_out.get().num_chunks() > 1: return pyarrow_wrap_chunked_array(chunked_out) diff --git a/python/pyarrow/includes/libarrow_python.pxd b/python/pyarrow/includes/libarrow_python.pxd index 41632f57481..0ff214159e7 100644 --- a/python/pyarrow/includes/libarrow_python.pxd +++ b/python/pyarrow/includes/libarrow_python.pxd @@ -65,8 +65,6 @@ cdef extern from "arrow/python/arrow_to_pandas.h" namespace "arrow::py::MapConve cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: shared_ptr[CDataType] GetPrimitiveType(Type type) - object PyFloat_FromHalf(uint16_t value) - cdef cppclass PyConversionOptions: PyConversionOptions() @@ -77,13 +75,26 @@ cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: c_bool ignore_timezone c_bool strict - # TODO Some functions below are not actually "nogil" + CStatus TensorToSparseCOOTensor(shared_ptr[CTensor], + shared_ptr[CSparseCOOTensor]* out) - CResult[shared_ptr[CChunkedArray]] ConvertPySequence( - object obj, object mask, const PyConversionOptions& options, - CMemoryPool* pool) + CStatus TensorToSparseCSRMatrix(shared_ptr[CTensor], + shared_ptr[CSparseCSRMatrix]* out) - CResult[shared_ptr[CDataType]] NumPyDtypeToArrow(object dtype) + CStatus TensorToSparseCSCMatrix(shared_ptr[CTensor], + shared_ptr[CSparseCSCMatrix]* out) + + CStatus TensorToSparseCSFTensor(shared_ptr[CTensor], + shared_ptr[CSparseCSFTensor]* out) + + void c_set_default_memory_pool \ + " arrow::py::set_default_memory_pool"(CMemoryPool* pool)\ + + CMemoryPool* c_get_memory_pool \ + " arrow::py::get_memory_pool"() + +cdef extern from "arrow/python/api.h" namespace "arrow::py": + object PyFloat_FromHalf(uint16_t value) CStatus NdarrayToArrow(CMemoryPool* pool, object ao, object mo, c_bool from_pandas, @@ -96,6 +107,12 @@ cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: const CCastOptions& cast_options, shared_ptr[CChunkedArray]* out) + CResult[shared_ptr[CChunkedArray]] ConvertPySequence( + object obj, object mask, const PyConversionOptions& options, + CMemoryPool* pool) + + CResult[shared_ptr[CDataType]] NumPyDtypeToArrow(object dtype) + CStatus NdarrayToTensor(CMemoryPool* pool, object ao, const vector[c_string]& dim_names, shared_ptr[CTensor]* out) @@ -144,36 +161,6 @@ cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: const vector[c_string]& dim_names, shared_ptr[CSparseCSFTensor]* out) - CStatus TensorToSparseCOOTensor(shared_ptr[CTensor], - shared_ptr[CSparseCOOTensor]* out) - - CStatus TensorToSparseCSRMatrix(shared_ptr[CTensor], - shared_ptr[CSparseCSRMatrix]* out) - - CStatus TensorToSparseCSCMatrix(shared_ptr[CTensor], - shared_ptr[CSparseCSCMatrix]* out) - - CStatus TensorToSparseCSFTensor(shared_ptr[CTensor], - shared_ptr[CSparseCSFTensor]* out) - - CStatus ConvertArrayToPandas(const PandasOptions& options, - shared_ptr[CArray] arr, - object py_ref, PyObject** out) - - CStatus ConvertChunkedArrayToPandas(const PandasOptions& options, - shared_ptr[CChunkedArray] arr, - object py_ref, PyObject** out) - - CStatus ConvertTableToPandas(const PandasOptions& options, - shared_ptr[CTable] table, - PyObject** out) - - void c_set_default_memory_pool \ - " arrow::py::set_default_memory_pool"(CMemoryPool* pool)\ - - CMemoryPool* c_get_memory_pool \ - " arrow::py::get_memory_pool"() - cdef cppclass PyBuffer(CBuffer): @staticmethod CResult[shared_ptr[CBuffer]] FromPyObject(object obj) @@ -210,15 +197,31 @@ cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: c_bool to_numpy +cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: + CStatus ConvertArrayToPandas(const PandasOptions& options, + shared_ptr[CArray] arr, + object py_ref, PyObject** out) + + CStatus ConvertChunkedArrayToPandas(const PandasOptions& options, + shared_ptr[CChunkedArray] arr, + object py_ref, PyObject** out) + + CStatus ConvertTableToPandas(const PandasOptions& options, + shared_ptr[CTable] table, + PyObject** out) + + cdef extern from "arrow/python/api.h" namespace "arrow::py::internal" nogil: cdef cppclass CTimePoint "arrow::py::internal::TimePoint": pass - CTimePoint PyDateTime_to_TimePoint(PyDateTime_DateTime* pydatetime) int64_t TimePoint_to_ns(CTimePoint val) CTimePoint TimePoint_from_s(double val) CTimePoint TimePoint_from_ns(int64_t val) + +cdef extern from "arrow/python/api.h" namespace "arrow::py::internal": + CTimePoint PyDateTime_to_TimePoint(PyDateTime_DateTime* pydatetime) CResult[c_string] TzinfoToString(PyObject* pytzinfo) CResult[PyObject*] StringToTzinfo(c_string, c_bool) diff --git a/python/pyarrow/scalar.pxi b/python/pyarrow/scalar.pxi index b07f6a5e054..e80763c6f97 100644 --- a/python/pyarrow/scalar.pxi +++ b/python/pyarrow/scalar.pxi @@ -1842,8 +1842,7 @@ def scalar(value, type=None, *, from_pandas=None, MemoryPool memory_pool=None): options.from_pandas = from_pandas value = [value] - with nogil: - chunked = GetResultValue(ConvertPySequence(value, None, options, pool)) + chunked = GetResultValue(ConvertPySequence(value, None, options, pool)) # get the first chunk assert chunked.get().num_chunks() == 1