1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# doc is comment_documentation
# use list so order is preserved.
ufunc_api_list = [
(r"""
""",
'FromFuncAndData', 'PyUFuncGenericFunction *, void **, char *, int, int, int, int, char *, char *, int', 'PyObject *'),
(r"""
""",
'GenericFunction', 'PyUFuncObject *, PyObject *, PyArrayObject **', 'int'),
(r"""
""",
'f_f_As_d_d','char **, intp *, intp *, void *','void'),
(r"""
""",
'd_d','char **, intp *, intp *, void *','void'),
(r"""
""",
'f_f','char **, intp *, intp *, void *','void'),
(r"""
""",
'g_g','char **, intp *, intp *, void *','void'),
(r"""
""",
'F_F_As_D_D','char **, intp *, intp *, void *','void'),
(r"""
""",
'F_F','char **, intp *, intp *, void *','void'),
(r"""
""",
'D_D','char **, intp *, intp *, void *','void'),
(r"""
""",
'G_G','char **, intp *, intp *, void *','void'),
(r"""
""",
'O_O','char **, intp *, intp *, void *','void'),
(r"""
""",
'ff_f_As_dd_d','char **, intp *, intp *, void *','void'),
(r"""
""",
'ff_f','char **, intp *, intp *, void *','void'),
(r"""
""",
'dd_d','char **, intp *, intp *, void *','void'),
(r"""
""",
'gg_g','char **, intp *, intp *, void *','void'),
(r"""
""",
'FF_F_As_DD_D','char **, intp *, intp *, void *','void'),
(r"""
""",
'DD_D','char **, intp *, intp *, void *','void'),
(r"""
""",
'FF_F','char **, intp *, intp *, void *','void'),
(r"""
""",
'GG_G','char **, intp *, intp *, void *','void'),
(r"""
""",
'OO_O','char **, intp *, intp *, void *','void'),
(r"""
""",
'O_O_method','char **, intp *, intp *, void *','void'),
(r"""
""",
'checkfperr', 'int, PyObject *', 'int'),
(r"""
""",
'clearfperr', 'void', 'void')
]
# API fixes for __arrayobject_api.h
fixed = 1
nummulti = len(ufunc_api_list)
numtotal = fixed + nummulti
module_list = []
extension_list = []
init_list = []
#setup object API
for k, item in enumerate(ufunc_api_list):
num = fixed + k
astr = "static %s PyUFunc_%s \\\n (%s);" % \
(item[3],item[1],item[2])
module_list.append(astr)
astr = "#define PyUFunc_%s \\\n (*(%s (*)(%s)) \\\n"\
" PyUFunc_API[%d])" % (item[1],item[3],item[2],num)
extension_list.append(astr)
astr = " (void *) PyUFunc_%s," % item[1]
init_list.append(astr)
outstr = r"""
#ifdef _UMATHMODULE
static PyTypeObject PyUFunc_Type;
%s
#else
static void **PyUFunc_API;
#define PyUFunc_Type (*(PyTypeObject *)PyUFunc_API[0])
%s
static int
import_ufunc(void)
{
PyObject *numpy = PyImport_ImportModule("scipy.base.umath");
PyObject *c_api = NULL;
if (numpy == NULL) return -1;
c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
if (c_api == NULL) {Py_DECREF(numpy); return -1;}
if (PyCObject_Check(c_api)) {
PyUFunc_API = (void **)PyCObject_AsVoidPtr(c_api);
}
Py_DECREF(c_api);
Py_DECREF(numpy);
if (PyUFunc_API == NULL) return -1;
return 0;
}
#endif
""" % ('\n'.join(module_list),
'\n'.join(extension_list))
# Write to header
fid = open('__ufunc_api.h','w')
fid.write(outstr)
fid.close()
outstr = r"""
/* These pointers will be stored in the C-object for use in other
extension modules
*/
void *PyUFunc_API[] = {
(void *) &PyUFunc_Type,
%s
};
""" % '\n'.join(init_list)
# Write to c-code
fid = open('__ufunc_api.c','w')
fid.write(outstr)
fid.close()
|