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