1- from textwrap import dedent
2-
31import numpy as np
42
53
@@ -22,227 +20,3 @@ def old_np_unique(
2220 outs [inv_idx ] = outs [inv_idx ].reshape (inv_shape )
2321
2422 return tuple (outs )
25-
26-
27- # compatibility header for C code
28- def npy_2_compat_header () -> str :
29- """Compatibility header that Numpy suggests is vendored with code that uses Numpy < 2.0 and Numpy 2.x"""
30- return dedent ("""
31- #ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_
32- #define NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_
33-
34-
35- /*
36- * This header is meant to be included by downstream directly for 1.x compat.
37- * In that case we need to ensure that users first included the full headers
38- * and not just `ndarraytypes.h`.
39- */
40-
41- #ifndef NPY_FEATURE_VERSION
42- #error "The NumPy 2 compat header requires `import_array()` for which " \\
43- "the `ndarraytypes.h` header include is not sufficient. Please " \\
44- "include it after `numpy/ndarrayobject.h` or similar." \\
45- "" \\
46- "To simplify inclusion, you may use `PyArray_ImportNumPy()` " \\
47- "which is defined in the compat header and is lightweight (can be)."
48- #endif
49-
50- #if NPY_ABI_VERSION < 0x02000000
51- /*
52- * Define 2.0 feature version as it is needed below to decide whether we
53- * compile for both 1.x and 2.x (defining it gaurantees 1.x only).
54- */
55- #define NPY_2_0_API_VERSION 0x00000012
56- /*
57- * If we are compiling with NumPy 1.x, PyArray_RUNTIME_VERSION so we
58- * pretend the `PyArray_RUNTIME_VERSION` is `NPY_FEATURE_VERSION`.
59- * This allows downstream to use `PyArray_RUNTIME_VERSION` if they need to.
60- */
61- #define PyArray_RUNTIME_VERSION NPY_FEATURE_VERSION
62- /* Compiling on NumPy 1.x where these are the same: */
63- #define PyArray_DescrProto PyArray_Descr
64- #endif
65-
66-
67- /*
68- * Define a better way to call `_import_array()` to simplify backporting as
69- * we now require imports more often (necessary to make ABI flexible).
70- */
71- #ifdef import_array1
72-
73- static inline int
74- PyArray_ImportNumPyAPI()
75- {
76- if (NPY_UNLIKELY(PyArray_API == NULL)) {
77- import_array1(-1);
78- }
79- return 0;
80- }
81-
82- #endif /* import_array1 */
83-
84-
85- /*
86- * NPY_DEFAULT_INT
87- *
88- * The default integer has changed, `NPY_DEFAULT_INT` is available at runtime
89- * for use as type number, e.g. `PyArray_DescrFromType(NPY_DEFAULT_INT)`.
90- *
91- * NPY_RAVEL_AXIS
92- *
93- * This was introduced in NumPy 2.0 to allow indicating that an axis should be
94- * raveled in an operation. Before NumPy 2.0, NPY_MAXDIMS was used for this purpose.
95- *
96- * NPY_MAXDIMS
97- *
98- * A constant indicating the maximum number dimensions allowed when creating
99- * an ndarray.
100- *
101- * NPY_NTYPES_LEGACY
102- *
103- * The number of built-in NumPy dtypes.
104- */
105- #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
106- #define NPY_DEFAULT_INT NPY_INTP
107- #define NPY_RAVEL_AXIS NPY_MIN_INT
108- #define NPY_MAXARGS 64
109-
110- #elif NPY_ABI_VERSION < 0x02000000
111- #define NPY_DEFAULT_INT NPY_LONG
112- #define NPY_RAVEL_AXIS 32
113- #define NPY_MAXARGS 32
114-
115- /* Aliases of 2.x names to 1.x only equivalent names */
116- #define NPY_NTYPES NPY_NTYPES_LEGACY
117- #define PyArray_DescrProto PyArray_Descr
118- #define _PyArray_LegacyDescr PyArray_Descr
119- /* NumPy 2 definition always works, but add it for 1.x only */
120- #define PyDataType_ISLEGACY(dtype) (1)
121- #else
122- #define NPY_DEFAULT_INT \\
123- (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? NPY_INTP : NPY_LONG)
124- #define NPY_RAVEL_AXIS \\
125- (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? -1 : 32)
126- #define NPY_MAXARGS \\
127- (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? 64 : 32)
128- #endif
129-
130-
131- /*
132- * Access inline functions for descriptor fields. Except for the first
133- * few fields, these needed to be moved (elsize, alignment) for
134- * additional space. Or they are descriptor specific and are not generally
135- * available anymore (metadata, c_metadata, subarray, names, fields).
136- *
137- * Most of these are defined via the `DESCR_ACCESSOR` macro helper.
138- */
139- #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION || NPY_ABI_VERSION < 0x02000000
140- /* Compiling for 1.x or 2.x only, direct field access is OK: */
141-
142- static inline void
143- PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
144- {
145- dtype->elsize = size;
146- }
147-
148- static inline npy_uint64
149- PyDataType_FLAGS(const PyArray_Descr *dtype)
150- {
151- #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
152- return dtype->flags;
153- #else
154- return (unsigned char)dtype->flags; /* Need unsigned cast on 1.x */
155- #endif
156- }
157-
158- #define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \\
159- static inline type \\
160- PyDataType_##FIELD(const PyArray_Descr *dtype) { \\
161- if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \\
162- return (type)0; \\
163- } \\
164- return ((_PyArray_LegacyDescr *)dtype)->field; \\
165- }
166- #else /* compiling for both 1.x and 2.x */
167-
168- static inline void
169- PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
170- {
171- if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
172- ((_PyArray_DescrNumPy2 *)dtype)->elsize = size;
173- }
174- else {
175- ((PyArray_DescrProto *)dtype)->elsize = (int)size;
176- }
177- }
178-
179- static inline npy_uint64
180- PyDataType_FLAGS(const PyArray_Descr *dtype)
181- {
182- if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
183- return ((_PyArray_DescrNumPy2 *)dtype)->flags;
184- }
185- else {
186- return (unsigned char)((PyArray_DescrProto *)dtype)->flags;
187- }
188- }
189-
190- /* Cast to LegacyDescr always fine but needed when `legacy_only` */
191- #define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \\
192- static inline type \\
193- PyDataType_##FIELD(const PyArray_Descr *dtype) { \\
194- if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \\
195- return (type)0; \\
196- } \\
197- if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) { \\
198- return ((_PyArray_LegacyDescr *)dtype)->field; \\
199- } \\
200- else { \\
201- return ((PyArray_DescrProto *)dtype)->field; \\
202- } \\
203- }
204- #endif
205-
206- DESCR_ACCESSOR(ELSIZE, elsize, npy_intp, 0)
207- DESCR_ACCESSOR(ALIGNMENT, alignment, npy_intp, 0)
208- DESCR_ACCESSOR(METADATA, metadata, PyObject *, 1)
209- DESCR_ACCESSOR(SUBARRAY, subarray, PyArray_ArrayDescr *, 1)
210- DESCR_ACCESSOR(NAMES, names, PyObject *, 1)
211- DESCR_ACCESSOR(FIELDS, fields, PyObject *, 1)
212- DESCR_ACCESSOR(C_METADATA, c_metadata, NpyAuxData *, 1)
213-
214- #undef DESCR_ACCESSOR
215-
216-
217- #if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
218- #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
219- static inline PyArray_ArrFuncs *
220- PyDataType_GetArrFuncs(const PyArray_Descr *descr)
221- {
222- return _PyDataType_GetArrFuncs(descr);
223- }
224- #elif NPY_ABI_VERSION < 0x02000000
225- static inline PyArray_ArrFuncs *
226- PyDataType_GetArrFuncs(const PyArray_Descr *descr)
227- {
228- return descr->f;
229- }
230- #else
231- static inline PyArray_ArrFuncs *
232- PyDataType_GetArrFuncs(const PyArray_Descr *descr)
233- {
234- if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
235- return _PyDataType_GetArrFuncs(descr);
236- }
237- else {
238- return ((PyArray_DescrProto *)descr)->f;
239- }
240- }
241- #endif
242-
243-
244- #endif /* not internal build */
245-
246- #endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_ */
247-
248- """ )
0 commit comments