Skip to content

Commit bab245d

Browse files
committed
Use unique symbol for ufunc API
1 parent b6edc01 commit bab245d

File tree

30 files changed

+93
-95
lines changed

30 files changed

+93
-95
lines changed

asciidtype/asciidtype/src/asciidtype_main.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#include "numpy/ndarraytypes.h"
78
#include "numpy/arrayobject.h"
9+
#include "numpy/ufuncobject.h"
810
#include "numpy/dtype_api.h"
911

1012
#include "dtype.h"
@@ -21,6 +23,7 @@ PyMODINIT_FUNC
2123
PyInit__asciidtype_main(void)
2224
{
2325
import_array();
26+
import_umath();
2427

2528
PyObject *m = PyModule_Create(&moduledef);
2629
if (m == NULL) {
@@ -47,14 +50,10 @@ PyInit__asciidtype_main(void)
4750
goto error;
4851
}
4952

50-
PyObject *numpy = init_ufuncs();
51-
52-
if (numpy == NULL) {
53+
if (init_ufuncs() == -1) {
5354
goto error;
5455
}
5556

56-
Py_DECREF(numpy);
57-
5857
return m;
5958

6059
error:

asciidtype/asciidtype/src/casts.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79
#include "numpy/arrayobject.h"
810
#include "numpy/dtype_api.h"
911
#include "numpy/ndarraytypes.h"

asciidtype/asciidtype/src/dtype.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
// clang-format on
55

66
#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
7+
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
78
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
89
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
910
#define NO_IMPORT_ARRAY
11+
#define NO_IMPORT_UFUNC
1012
#include "numpy/ndarraytypes.h"
1113
#include "numpy/arrayobject.h"
1214
#include "numpy/ufuncobject.h"

asciidtype/asciidtype/src/umath.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL asciidtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL asciidtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79
#include "numpy/ndarraytypes.h"
810
#include "numpy/arrayobject.h"
911
#include "numpy/ufuncobject.h"
@@ -219,14 +221,12 @@ init_equal_ufunc(PyObject *numpy)
219221
return 0;
220222
}
221223

222-
PyObject *
224+
int
223225
init_ufuncs(void)
224226
{
225-
import_umath();
226-
227227
PyObject *numpy = PyImport_ImportModule("numpy");
228228
if (numpy == NULL) {
229-
return NULL;
229+
return -1;
230230
}
231231

232232
if (init_add_ufunc(numpy) < 0) {
@@ -237,9 +237,9 @@ init_ufuncs(void)
237237
goto error;
238238
}
239239

240-
return numpy;
240+
return 0;
241241

242242
error:
243243
Py_DECREF(numpy);
244-
return NULL;
244+
return -1;
245245
}

asciidtype/asciidtype/src/umath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _NPY_UFUNC_H
22
#define _NPY_UFUNC_H
33

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

77
#endif /*_NPY_UFUNC_H */

metadatadtype/metadatadtype/src/casts.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79
#include "numpy/arrayobject.h"
810
#include "numpy/dtype_api.h"
911
#include "numpy/ndarraytypes.h"

metadatadtype/metadatadtype/src/dtype.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
// clang-format off
2+
#include <Python.h>
3+
#include "structmember.h"
4+
// clang-format on
5+
16
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
7+
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
28
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
39
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
410
#define NO_IMPORT_ARRAY
11+
#define NO_IMPORT_UFUNC
512
#include "numpy/arrayobject.h"
613
#include "numpy/dtype_api.h"
714
#include "numpy/ndarraytypes.h"

metadatadtype/metadatadtype/src/dtype.h

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

4-
// clang-format off
5-
#include <Python.h>
6-
#include "structmember.h"
7-
// clang-format on
8-
94
typedef struct {
105
PyArray_Descr base;
116
PyObject *metadata;

metadatadtype/metadatadtype/src/metadatadtype_main.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#include "numpy/arrayobject.h"
8+
#include "numpy/ufuncobject.h"
79
#include "numpy/dtype_api.h"
810

911
#include "umath.h"
@@ -20,6 +22,7 @@ PyMODINIT_FUNC
2022
PyInit__metadatadtype_main(void)
2123
{
2224
import_array();
25+
import_umath();
2326

2427
PyObject *m = PyModule_Create(&moduledef);
2528
if (m == NULL) {
@@ -47,7 +50,7 @@ PyInit__metadatadtype_main(void)
4750
goto error;
4851
}
4952

50-
if (init_ufuncs(m) == NULL) {
53+
if (init_ufuncs() == -1) {
5154
goto error;
5255
}
5356

metadatadtype/metadatadtype/src/umath.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL metadatadtype_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL metadatadtype_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79
#include "numpy/ndarraytypes.h"
810
#include "numpy/arrayobject.h"
911
#include "numpy/ufuncobject.h"
@@ -103,11 +105,9 @@ add_wrapping_loop(const char *ufunc_name, PyArray_DTypeMeta **dtypes,
103105
return res;
104106
}
105107

106-
PyObject *
107-
init_ufuncs(PyObject *module)
108+
int
109+
init_ufuncs(void)
108110
{
109-
import_umath();
110-
111111
PyArray_DTypeMeta *binary_orig_dtypes[3] = {&MetadataDType, &MetadataDType,
112112
&MetadataDType};
113113
PyArray_DTypeMeta *binary_wrapped_dtypes[3] = {
@@ -126,9 +126,8 @@ init_ufuncs(PyObject *module)
126126
goto error;
127127
}
128128

129-
return module;
129+
return 0;
130130
error:
131131

132-
Py_DECREF(module);
133-
return NULL;
132+
return -1;
134133
}
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-
PyObject *
5-
init_ufuncs(PyObject *module);
4+
int
5+
init_ufuncs(void);
66

77
#endif /*_NPY_UFUNC_H */

mpfdtype/mpfdtype/src/casts.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

22
#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
3+
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
34
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
45
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
56
#define NO_IMPORT_ARRAY
7+
#define NO_IMPORT_UFUNC
68

79
extern "C" {
810
#include <Python.h>

mpfdtype/mpfdtype/src/dtype.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79
#include "numpy/arrayobject.h"
810
#include "numpy/ndarraytypes.h"
911
#include "numpy/dtype_api.h"

mpfdtype/mpfdtype/src/mpfdtype_main.c

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

33
#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#include "numpy/arrayobject.h"
7+
#include "numpy/ufuncobject.h"
68
#include "numpy/dtype_api.h"
79

810
#include "dtype.h"
@@ -19,9 +21,8 @@ static struct PyModuleDef moduledef = {
1921
PyMODINIT_FUNC
2022
PyInit__mpfdtype_main(void)
2123
{
22-
if (_import_array() < 0) {
23-
return NULL;
24-
}
24+
import_array();
25+
import_umath();
2526

2627
PyObject *m = PyModule_Create(&moduledef);
2728
if (m == NULL) {
@@ -44,14 +45,10 @@ PyInit__mpfdtype_main(void)
4445
goto error;
4546
}
4647

47-
PyObject *numpy = init_mpf_umath();
48-
49-
if (numpy == NULL) {
48+
if (init_mpf_umath() == -1) {
5049
goto error;
5150
}
5251

53-
Py_DECREF(numpy);
54-
5552
if (init_terrible_hacks() < 0) {
5653
goto error;
5754
}

mpfdtype/mpfdtype/src/umath.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
#include "scalar.h"
33
#define PY_ARRAY_UNIQUE_SYMBOL MPFDType_ARRAY_API
4+
#define PY_UFUNC_UNIQUE_SYMBOL MPFDType_UFUNC_API
45
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
56
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
67
#define NO_IMPORT_ARRAY
8+
#define NO_IMPORT_UFUNC
79

810
extern "C" {
911
#include <Python.h>
@@ -592,17 +594,15 @@ init_comps(PyObject *numpy)
592594
/*
593595
* Function that adds our multiply loop to NumPy's multiply ufunc.
594596
*/
595-
PyObject *
597+
int
596598
init_mpf_umath(void)
597599
{
598-
import_umath();
599-
600600
/*
601601
* Get the multiply ufunc:
602602
*/
603603
PyObject *numpy = PyImport_ImportModule("numpy");
604604
if (numpy == NULL) {
605-
return NULL;
605+
return -1;
606606
}
607607

608608
if (init_unary_ops(numpy) < 0) {
@@ -615,10 +615,11 @@ init_mpf_umath(void)
615615
goto err;
616616
}
617617

618-
return numpy;
618+
Py_DECREF(numpy);
619+
return 0;
619620

620621
err:
621622
Py_DECREF(numpy);
622-
return NULL;
623+
return -1;
623624
}
624625

mpfdtype/mpfdtype/src/umath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern "C" {
66
#endif
77

8-
PyObject *
8+
int
99
init_mpf_umath(void);
1010

1111
#ifdef __cplusplus

quaddtype/quaddtype/src/quaddtype_main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ PyInit__quaddtype_main(void)
5050
goto error;
5151
}
5252

53-
PyObject *numpy = init_multiply_ufunc();
54-
55-
if (numpy == NULL) {
53+
if (init_multiply_ufunc() == -1) {
5654
PyErr_SetString(PyExc_TypeError, "Failed to initialize the quadscalar multiply ufunc.");
5755
goto error;
5856
}
5957

60-
Py_DECREF(numpy);
61-
6258
return m;
6359

6460
error:

0 commit comments

Comments
 (0)