Skip to content

Commit

Permalink
Prevent potential data loss in numpy's dtype::get_itemsize().
Browse files Browse the repository at this point in the history
The numpy 2.0 migration guide states [https://numpy.org/devdocs/numpy_2_0_migration_guide.html]:

"PyDataType_ELSIZE and PyDataType_SET_ELSIZE (note that the result is now npy_intp and not int)."

Even though it is rather unlikely to have have elements larger than 2GB each, prevent potential numerical overflows, which could occur when casting ssize_t to int on 64-bit systems.
  • Loading branch information
JohannesWilde committed Dec 14, 2024
1 parent ff0ae9b commit c641a80
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/boost/python/numpy/dtype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BOOST_NUMPY_DECL dtype : public object {
template <typename T> static dtype get_builtin();

/// @brief Return the size of the data type in bytes.
int get_itemsize() const;
Py_ssize_t get_itemsize() const;

/**
* @brief Compare two dtypes for equivalence.
Expand Down
2 changes: 1 addition & 1 deletion src/numpy/dtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ python::detail::new_reference dtype::convert(object const & arg, bool align)
return python::detail::new_reference(reinterpret_cast<PyObject*>(obj));
}

int dtype::get_itemsize() const {
Py_ssize_t dtype::get_itemsize() const {
#if NPY_ABI_VERSION < 0x02000000
return reinterpret_cast<PyArray_Descr*>(ptr())->elsize;
#else
Expand Down

0 comments on commit c641a80

Please sign in to comment.