Skip to content

Commit

Permalink
apacheGH-40376: [Python] Update for NumPy 2.0 ABI change in PyArray_D…
Browse files Browse the repository at this point in the history
…escr->elsize

Co-authored-by: Sebastian Berg <[email protected]>
  • Loading branch information
jorisvandenbossche and seberg committed Mar 8, 2024
1 parent bdd04c0 commit aa34b96
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python/pyarrow/src/arrow/python/arrow_to_pandas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Status PyArray_NewFromPool(int nd, npy_intp* dims, PyArray_Descr* descr, MemoryP
//
// * Track allocations
// * Get better performance through custom allocators
int64_t total_size = descr->elsize;
int64_t total_size = PyDataType_ELSIZE(descr);
for (int i = 0; i < nd; ++i) {
total_size *= dims[i];
}
Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/src/arrow/python/numpy_convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ NumPyBuffer::NumPyBuffer(PyObject* ao) : Buffer(nullptr, 0) {
PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(ao);
auto ptr = reinterpret_cast<uint8_t*>(PyArray_DATA(ndarray));
data_ = const_cast<const uint8_t*>(ptr);
size_ = PyArray_SIZE(ndarray) * PyArray_DESCR(ndarray)->elsize;
size_ = PyArray_NBYTES(ndarray);
capacity_ = size_;
is_mutable_ = !!(PyArray_FLAGS(ndarray) & NPY_ARRAY_WRITEABLE);
}
Expand Down
5 changes: 5 additions & 0 deletions python/pyarrow/src/arrow/python/numpy_interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
#define NPY_INT32_IS_INT 0
#endif

// Backported NumPy 2 API (can be removed if numpy 2 is required)
#if NPY_ABI_VERSION < 0x02000000
#define PyDataType_ELSIZE(descr) ((descr)->elsize)
#endif

namespace arrow {
namespace py {

Expand Down
4 changes: 2 additions & 2 deletions python/pyarrow/src/arrow/python/numpy_to_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class NumPyConverter {
mask_ = reinterpret_cast<PyArrayObject*>(mo);
}
length_ = static_cast<int64_t>(PyArray_SIZE(arr_));
itemsize_ = static_cast<int>(PyArray_DESCR(arr_)->elsize);
itemsize_ = static_cast<int64_t>(PyArray_ITEMSIZE(arr_));
stride_ = static_cast<int64_t>(PyArray_STRIDES(arr_)[0]);
}

Expand Down Expand Up @@ -296,7 +296,7 @@ class NumPyConverter {
PyArrayObject* mask_;
int64_t length_;
int64_t stride_;
int itemsize_;
int64_t itemsize_;

bool from_pandas_;
compute::CastOptions cast_options_;
Expand Down

0 comments on commit aa34b96

Please sign in to comment.