Skip to content

Commit 910c027

Browse files
committed
update metadatadtype for numpy 2.0
1 parent 08214d1 commit 910c027

File tree

7 files changed

+40
-45
lines changed

7 files changed

+40
-45
lines changed

metadatadtype/metadatadtype/src/casts.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <Python.h>
22

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4-
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
4+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
5+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
56
#define NO_IMPORT_ARRAY
67
#include "numpy/arrayobject.h"
7-
#include "numpy/experimental_dtype_api.h"
8+
#include "numpy/dtype_api.h"
89
#include "numpy/ndarraytypes.h"
910

1011
#include "casts.h"
@@ -186,7 +187,7 @@ static PyArray_DTypeMeta *m2m_dtypes[2] = {NULL, NULL};
186187
static PyType_Slot m2m_slots[] = {
187188
{NPY_METH_resolve_descriptors,
188189
&metadata_to_metadata_resolve_descriptors},
189-
{_NPY_METH_get_loop, &metadata_to_metadata_get_loop},
190+
{NPY_METH_get_loop, &metadata_to_metadata_get_loop},
190191
{0, NULL}};
191192

192193
PyArrayMethod_Spec MetadataToMetadataCastSpec = {
@@ -200,7 +201,7 @@ PyArrayMethod_Spec MetadataToMetadataCastSpec = {
200201
};
201202

202203
static PyType_Slot m2f_slots[] = {
203-
{_NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}};
204+
{NPY_METH_get_loop, &metadata_to_float64_get_loop}, {0, NULL}};
204205

205206
static char *m2f_name = "cast_MetadataDType_to_Float64";
206207

metadatadtype/metadatadtype/src/casts.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
#ifndef _NPY_CASTS_H
22
#define _NPY_CASTS_H
33

4-
#include <Python.h>
5-
6-
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
7-
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
8-
#define NO_IMPORT_ARRAY
9-
#include "numpy/arrayobject.h"
10-
#include "numpy/experimental_dtype_api.h"
11-
#include "numpy/ndarraytypes.h"
12-
134
PyArrayMethod_Spec **
145
get_casts(void);
156

metadatadtype/metadatadtype/src/dtype.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
2+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
3+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
4+
#define NO_IMPORT_ARRAY
5+
#include "numpy/arrayobject.h"
6+
#include "numpy/dtype_api.h"
7+
#include "numpy/ndarraytypes.h"
8+
19
#include "dtype.h"
210

311
#include "casts.h"
@@ -74,8 +82,9 @@ new_metadatadtype_instance(PyObject *metadata)
7482
}
7583
Py_INCREF(metadata);
7684
new->metadata = metadata;
77-
new->base.elsize = sizeof(double);
78-
new->base.alignment = _Alignof(double); /* is there a better spelling? */
85+
PyArray_Descr *base = (PyArray_Descr *)new;
86+
base->elsize = sizeof(double);
87+
base->alignment = _Alignof(double); /* is there a better spelling? */
7988
/* do not support byte-order for now */
8089

8190
return new;

metadatadtype/metadatadtype/src/dtype.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
#include "structmember.h"
77
// clang-format on
88

9-
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
10-
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
11-
#define NO_IMPORT_ARRAY
12-
#include "numpy/arrayobject.h"
13-
#include "numpy/experimental_dtype_api.h"
14-
#include "numpy/ndarraytypes.h"
15-
169
typedef struct {
1710
PyArray_Descr base;
1811
PyObject *metadata;

metadatadtype/metadatadtype/src/metadatadtype_main.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include <Python.h>
22

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4-
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
4+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
5+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
56
#include "numpy/arrayobject.h"
6-
#include "numpy/experimental_dtype_api.h"
7+
#include "numpy/dtype_api.h"
78

8-
#include "dtype.h"
99
#include "umath.h"
10+
#include "dtype.h"
1011

1112
static struct PyModuleDef moduledef = {
1213
PyModuleDef_HEAD_INIT,
@@ -18,12 +19,7 @@ static struct PyModuleDef moduledef = {
1819
PyMODINIT_FUNC
1920
PyInit__metadatadtype_main(void)
2021
{
21-
if (_import_array() < 0) {
22-
return NULL;
23-
}
24-
if (import_experimental_dtype_api(15) < 0) {
25-
return NULL;
26-
}
22+
import_array();
2723

2824
PyObject *m = PyModule_Create(&moduledef);
2925
if (m == NULL) {
@@ -51,7 +47,7 @@ PyInit__metadatadtype_main(void)
5147
goto error;
5248
}
5349

54-
if (init_ufuncs() < 0) {
50+
if (init_ufuncs(m) == NULL) {
5551
goto error;
5652
}
5753

metadatadtype/metadatadtype/src/umath.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#include <Python.h>
22

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4-
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
4+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
5+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
56
#define NO_IMPORT_ARRAY
6-
#include "numpy/arrayobject.h"
7-
#include "numpy/experimental_dtype_api.h"
87
#include "numpy/ndarraytypes.h"
8+
#include "numpy/arrayobject.h"
99
#include "numpy/ufuncobject.h"
10+
#include "numpy/dtype_api.h"
1011

1112
#include "dtype.h"
1213
#include "umath.h"
1314

1415
static int
1516
translate_given_descrs(int nin, int nout,
16-
PyArray_DTypeMeta *NPY_UNUSED(wrapped_dtypes[]),
17-
PyArray_Descr *given_descrs[],
17+
PyArray_DTypeMeta *const NPY_UNUSED(wrapped_dtypes[]),
18+
PyArray_Descr *const given_descrs[],
1819
PyArray_Descr *new_descrs[])
1920
{
2021
for (int i = 0; i < nin + nout; i++) {
@@ -35,8 +36,8 @@ translate_given_descrs(int nin, int nout,
3536

3637
static int
3738
translate_loop_descrs(int nin, int NPY_UNUSED(nout),
38-
PyArray_DTypeMeta *NPY_UNUSED(new_dtypes[]),
39-
PyArray_Descr *given_descrs[],
39+
PyArray_DTypeMeta *const NPY_UNUSED(new_dtypes[]),
40+
PyArray_Descr *const given_descrs[],
4041
PyArray_Descr *original_descrs[],
4142
PyArray_Descr *loop_descrs[])
4243
{
@@ -102,9 +103,11 @@ add_wrapping_loop(const char *ufunc_name, PyArray_DTypeMeta **dtypes,
102103
return res;
103104
}
104105

105-
int
106-
init_ufuncs(void)
106+
PyObject *
107+
init_ufuncs(PyObject *module)
107108
{
109+
import_umath();
110+
108111
PyArray_DTypeMeta *binary_orig_dtypes[3] = {&MetadataDType, &MetadataDType,
109112
&MetadataDType};
110113
PyArray_DTypeMeta *binary_wrapped_dtypes[3] = {
@@ -123,7 +126,9 @@ init_ufuncs(void)
123126
goto error;
124127
}
125128

126-
return 0;
129+
return module;
127130
error:
128-
return -1;
131+
132+
Py_DECREF(module);
133+
return NULL;
129134
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _NPY_UFUNC_H
22
#define _NPY_UFUNC_H
33

4-
int
5-
init_ufuncs(void);
4+
PyObject *
5+
init_ufuncs(PyObject *module);
66

77
#endif /*_NPY_UFUNC_H */

0 commit comments

Comments
 (0)