Skip to content

Commit f487b9a

Browse files
committed
[numpy] merge numpy compat unit
1 parent 84ebcb6 commit f487b9a

File tree

4 files changed

+42
-58
lines changed

4 files changed

+42
-58
lines changed

src/decoder.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "markers.h"
2626
#include "decoder.h"
2727
#include "python_funcs.h"
28-
#include "numpy_compat.h"
2928

3029
/******************************************************************************/
3130

src/encoder.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "markers.h"
2727
#include "encoder.h"
2828
#include "python_funcs.h"
29-
#include "numpy_compat.h"
3029

3130
/******************************************************************************/
3231

src/numpy_compat.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/numpyapi.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define PY_ARRAY_UNIQUE_SYMBOL bjdata_numpy_array
2121
#define NPY_NO_DEPRECATED_API 0
2222
#include <numpy/arrayobject.h>
23+
#include <numpy/ndarraytypes.h>
2324

2425
#if defined (__cplusplus)
2526
extern "C" {
@@ -86,6 +87,47 @@ extern "C" {
8687

8788
#endif
8889

90+
#if NPY_ABI_VERSION < 0x02000000
91+
/*
92+
* NumPy 1.x: Direct struct member access
93+
*/
94+
#define DESCR_ELSIZE(d) (((PyArray_Descr*)(d))->elsize)
95+
#define DESCR_TYPE_NUM(d) (((PyArray_Descr*)(d))->type_num)
96+
#else
97+
/*
98+
* NumPy 2.x: Use Python attribute access to avoid API linkage issues
99+
* This is slightly slower but guaranteed to work.
100+
*/
101+
static inline npy_intp _descr_elsize_compat(PyArray_Descr* d) {
102+
npy_intp result = 0;
103+
PyObject* val = PyObject_GetAttrString((PyObject*)d, "itemsize");
104+
105+
if (val) {
106+
result = PyLong_AsSsize_t(val);
107+
Py_DECREF(val);
108+
}
109+
110+
PyErr_Clear();
111+
return result;
112+
}
113+
114+
static inline int _descr_type_num_compat(PyArray_Descr* d) {
115+
int result = 0;
116+
PyObject* val = PyObject_GetAttrString((PyObject*)d, "num");
117+
118+
if (val) {
119+
result = (int)PyLong_AsLong(val);
120+
Py_DECREF(val);
121+
}
122+
123+
PyErr_Clear();
124+
return result;
125+
}
126+
127+
#define DESCR_ELSIZE(d) _descr_elsize_compat((PyArray_Descr*)(d))
128+
#define DESCR_TYPE_NUM(d) _descr_type_num_compat((PyArray_Descr*)(d))
129+
#endif
130+
89131
#if defined (__cplusplus)
90132
}
91133
#endif

0 commit comments

Comments
 (0)