Skip to content

Commit f85cc8c

Browse files
authored
Merge pull request #448 from duburcqa/numpy_2
Add support of numpy 2.0.0b1
2 parents 37ad9a1 + 98717f0 commit f85cc8c

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ 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 =
145+
PyDataType_ELSIZE(call_PyArray_DescrFromType(Scalar_type_code));
146+
#endif
142147
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
143148

144149
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(
@@ -204,7 +209,12 @@ struct numpy_allocator_impl_matrix<
204209
outer_stride = reverse_strides ? mat.innerStride()
205210
: mat.outerStride();
206211

212+
#if NPY_ABI_VERSION < 0x02000000
207213
const int elsize = call_PyArray_DescrFromType(Scalar_type_code)->elsize;
214+
#else
215+
const int elsize =
216+
PyDataType_ELSIZE(call_PyArray_DescrFromType(Scalar_type_code));
217+
#endif
208218
npy_intp strides[2] = {elsize * inner_stride, elsize * outer_stride};
209219

210220
PyArrayObject *pyArray = (PyArrayObject *)call_PyArray_New(

include/eigenpy/numpy.hpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,33 @@
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:
21+
https://github.com/numpy/numpy/blob/afea8fd66f6bdbde855f5aff0b4e73eb0213c646/doc/source/reference/c-api/array.rst#L1224
22+
*/
23+
#if NPY_ABI_VERSION < 0x02000000
24+
#define PyArray_DescrProto PyArray_Descr
25+
#endif
26+
1927
#include <numpy/ndarrayobject.h>
2028
#include <numpy/ufuncobject.h>
2129

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

173-
inline int call_PyArray_RegisterDataType(PyArray_Descr* dtype) {
197+
inline int call_PyArray_RegisterDataType(PyArray_DescrProto* dtype) {
174198
return PyArray_RegisterDataType(dtype);
175199
}
176200

include/eigenpy/user-type.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ 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 =
175+
PyDataType_GetArrFuncs(PyArray_DESCR(py_array))->copyswap;
175176

176177
for (npy_intp i = 0; i < n; i++) {
177178
copyswap(dstptr, srcptr, swap, array);
@@ -189,8 +190,8 @@ struct SpecialMethods<T, NPY_USERDEF> {
189190
return (npy_bool)(value != ZeroValue);
190191
} else {
191192
T tmp_value;
192-
PyArray_DESCR(py_array)->f->copyswap(
193-
&tmp_value, ip, PyArray_ISBYTESWAPPED(py_array), array);
193+
PyDataType_GetArrFuncs(PyArray_DESCR(py_array))
194+
->copyswap(&tmp_value, ip, PyArray_ISBYTESWAPPED(py_array), array);
194195
return (npy_bool)(tmp_value != ZeroValue);
195196
}
196197
}

src/numpy.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ 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 =
18+
PyArray_DescrFromTypeObject(reinterpret_cast<PyObject*>(type));
19+
if (descr == NULL) {
20+
return NPY_NOTYPE;
21+
}
22+
return descr->type_num;
1823
}
1924

2025
#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)