Skip to content

Commit 93993ee

Browse files
committed
Add support of numpy 2.0.0b1
1 parent 37ad9a1 commit 93993ee

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Added
1010
- Allow use of installed JRL-cmakemodule ([#446](https://github.com/stack-of-tasks/eigenpy/pull/446)
11+
- Support of Numpy 2.0.0b1 ([#448](https://github.com/stack-of-tasks/eigenpy/pull/448))
1112

1213
### Fixed
1314
- Fix unit test build in C++11 ([#442](https://github.com/stack-of-tasks/eigenpy/pull/442))

include/eigenpy/numpy-allocator.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ struct numpy_allocator_impl_matrix<Eigen::Ref<MatType, Options, Stride> > {
138138
outer_stride = reverse_strides ? mat.innerStride()
139139
: mat.outerStride();
140140

141+
#if NPY_ABI_VERSION < 0x02000000
141142
const int elsize = call_PyArray_DescrFromType(Scalar_type_code)->elsize;
143+
#else
144+
const int elsize = PyDataType_ELSIZE(call_PyArray_DescrFromType(Scalar_type_code));
145+
#endif
142146
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
143147

144148
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(
@@ -204,7 +208,11 @@ struct numpy_allocator_impl_matrix<
204208
outer_stride = reverse_strides ? mat.innerStride()
205209
: mat.outerStride();
206210

211+
#if NPY_ABI_VERSION < 0x02000000
207212
const int elsize = call_PyArray_DescrFromType(Scalar_type_code)->elsize;
213+
#else
214+
const int elsize = PyDataType_ELSIZE(call_PyArray_DescrFromType(Scalar_type_code));
215+
#endif
208216
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
209217

210218
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(

include/eigenpy/numpy.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,34 @@
1616
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
1717
#endif
1818

19+
/* Allow compiling against NumPy 1.x and 2.x
20+
see: https://github.com/numpy/numpy/blob/afea8fd66f6bdbde855f5aff0b4e73eb0213c646/doc/source/reference/c-api/array.rst#L1224
21+
*/
22+
#if NPY_ABI_VERSION < 0x02000000
23+
#define PyArray_DescrProto PyArray_Descr
24+
#endif
25+
1926
#include <numpy/ndarrayobject.h>
2027
#include <numpy/ufuncobject.h>
2128

29+
#if NPY_ABI_VERSION < 0x02000000
30+
static inline PyArray_ArrFuncs *
31+
PyDataType_GetArrFuncs(PyArray_Descr *descr)
32+
{
33+
return descr->f;
34+
}
35+
#endif
36+
37+
/* PEP 674 disallow using macros as l-values
38+
see : https://peps.python.org/pep-0674/
39+
*/
40+
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
41+
static inline void _Py_SET_TYPE(PyObject *o, PyTypeObject *type) {
42+
Py_TYPE(o) = type;
43+
}
44+
#define Py_SET_TYPE(o, type) _Py_SET_TYPE((PyObject*)(o), type)
45+
#endif
46+
2247
#if defined _WIN32 || defined __CYGWIN__
2348
#define EIGENPY_GET_PY_ARRAY_TYPE(array) \
2449
call_PyArray_MinScalarType(array)->type_num
@@ -170,7 +195,7 @@ inline void call_PyArray_InitArrFuncs(PyArray_ArrFuncs* funcs) {
170195
PyArray_InitArrFuncs(funcs);
171196
}
172197

173-
inline int call_PyArray_RegisterDataType(PyArray_Descr* dtype) {
198+
inline int call_PyArray_RegisterDataType(PyArray_DescrProto* dtype) {
174199
return PyArray_RegisterDataType(dtype);
175200
}
176201

include/eigenpy/user-type.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
171171
char* srcptr = static_cast<char*>(src);
172172

173173
PyArrayObject* py_array = static_cast<PyArrayObject*>(array);
174-
PyArray_CopySwapFunc* copyswap = PyArray_DESCR(py_array)->f->copyswap;
174+
PyArray_CopySwapFunc* copyswap = PyDataType_GetArrFuncs(PyArray_DESCR(py_array))->copyswap;
175175

176176
for (npy_intp i = 0; i < n; i++) {
177177
copyswap(dstptr, srcptr, swap, array);
@@ -189,7 +189,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
189189
return (npy_bool)(value != ZeroValue);
190190
} else {
191191
T tmp_value;
192-
PyArray_DESCR(py_array)->f->copyswap(
192+
PyDataType_GetArrFuncs(PyArray_DESCR(py_array))->copyswap(
193193
&tmp_value, ip, PyArray_ISBYTESWAPPED(py_array), array);
194194
return (npy_bool)(tmp_value != ZeroValue);
195195
}

src/numpy.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ void import_numpy() {
1414
}
1515

1616
int PyArray_TypeNum(PyTypeObject* type) {
17-
return PyArray_TypeNumFromName(const_cast<char*>(type->tp_name));
17+
PyArray_Descr * descr = PyArray_DescrFromTypeObject(reinterpret_cast<PyObject*>(type));
18+
if (descr == NULL) {
19+
return NPY_NOTYPE;
20+
}
21+
return descr->type_num;
1822
}
1923

2024
#if defined _WIN32 || defined __CYGWIN__

src/register.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ int Register::registerNewType(
6363
throw std::invalid_argument("PyType_Ready fails to initialize input type.");
6464
}
6565

66-
PyArray_Descr* descr_ptr =
67-
new PyArray_Descr(*call_PyArray_DescrFromType(NPY_OBJECT));
68-
PyArray_Descr& descr = *descr_ptr;
66+
PyArray_DescrProto* descr_ptr = new PyArray_DescrProto();
67+
Py_SET_TYPE(descr_ptr, &PyArrayDescr_Type);
68+
PyArray_DescrProto& descr = *descr_ptr;
6969
descr.typeobj = py_type_ptr;
7070
descr.kind = 'V';
7171
descr.byteorder = '=';
@@ -98,7 +98,7 @@ int Register::registerNewType(
9898
PyArray_Descr* new_descr = call_PyArray_DescrFromType(code);
9999

100100
if (PyDict_SetItemString(py_type_ptr->tp_dict, "dtype",
101-
(PyObject*)descr_ptr) < 0) {
101+
(PyObject*)new_descr) < 0) {
102102
throw std::invalid_argument("PyDict_SetItemString fails.");
103103
}
104104

0 commit comments

Comments
 (0)