-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_extension_module.py
221 lines (193 loc) · 6.3 KB
/
build_extension_module.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'''build an extension module
'''
module_head = '''#define PY_SSIZE_T_CLEAN
#include "Python.h"
%s
'''
module_exception ='''static PyObject *%sError;
'''
method_obj = '''static PyObject *
%s_%s(%s)
%s
'''
method_doc = '''PyDoc_STRVAR(%s_%s_doc,
%s);
'''
method_def = '''
{"%s", %s_%s, %s, %s_%s_doc},'''
PyMethodDef = '''
static PyMethodDef %s_methods[] = {%s
{NULL, NULL} /* sentinel */
};
'''
struct_seq_field = '''
'''
struct_seq_desc = '''
'''
struct_seq_type = '''
static PyTypeObject %sType;
'''
PyStructSeqField = '''
static PyStructSequence_Field %s_type_fields[] = {%s
};
'''
PyStructSeqDesc = '''
static PyStructSequence_Desc %s_type_desc = {
"%s.%s",
%s,
%s_type_fields,
%d,
};
'''
struct_seq_init_type = ''' PyStructSequence_InitType(&%sType, &%s_type_desc);
'''
struct_seq_init_add = ''' Py_INCREF(&%sType);
PyModule_AddObject(m, "%s", (PyObject*) &%sType);
'''
PyStructSeqTail = '''
if (!initialized) {
%s
}
%s
initialized = 1;
'''
module_tail = '''
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef %smodule = {
PyModuleDef_HEAD_INIT,
"%s",
module_doc,
-1,
%s_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_%s(void)
{
PyObject *m;
m = PyModule_Create(&%smodule);
/*import_array();*/
if (m == NULL)
return NULL;
#else
PyMODINIT_FUNC
init%s(void)
{
PyObject *m;
m = Py_InitModule3("%s", %s_methods, module_doc);
/*import_array();*/
if (m == NULL)
goto finally;
#endif
%sError = PyErr_NewException("%s.error", NULL, NULL);
Py_INCREF(%sError);
PyModule_AddObject(m, "error", %sError);
'''
class Builder(object):
def __init__(self, module_name, include_files,
module_localprototype, module_localfunction,
module_add, module_methods,
module_doc, module_filename,
module_struct_seq, use_numpy=False):
## if use_numpy is True, should add "numpy/npy_3kcompat.h" and "numpy/arrayobject.h" in include files
self.use_numpy = use_numpy
self.module_name = module_name
self.module_doc = module_doc
self.module_filename = module_filename
self.include_files = ['#include %s'%i for i in include_files]
if self.use_numpy:
self.include_files.extend(['#include "numpy/npy_3kcompat.h"',
'#include "numpy/arrayobject.h"'])
self.include_files = '\n'.join(self.include_files)
self.localprototype = module_localprototype
self.localfunction = module_localfunction
self.module_add = module_add
self.methods = module_methods
self.module_method = ''
self.method_list = ''
self.m_t = {'METH_NOARGS':'PyObject *self',
'METH_O':'PyObject *self, PyObject *arg',
'METH_VARARGS':'PyObject *self, PyObject *args',
'METH_VARARGS|METH_KEYWORDS':'PyObject *self, PyObject *args, PyObject *kwds'
}
self.tail_struct_seq = ''
if module_struct_seq:
self.localprototype = 'static int initialized;\n' + self.localprototype
init_type = ''
init_add = ''
for s in module_struct_seq:
s_name = s[0]
s_type = '%s'%(s_name.capitalize())
self.localprototype += struct_seq_type%s_type
f = ''
for i in s[1]:
f += '''
{"%s", "%s"},'''%i
f += '''
{0}'''
self.localfunction += PyStructSeqField%(s_name, f)
self.localfunction += PyStructSeqDesc%(s_name, module_name, s_name, s[2], s_name, len(s[1]))
init_type += struct_seq_init_type%(s_type, s_name)
init_add += struct_seq_init_add%(s_type, s_name, s_type)
self.tail_struct_seq += PyStructSeqTail%(init_type, init_add)
def head(self):
return module_head%self.include_files
def exception(self):
return module_exception%self.module_name
def method_def(self):
for (m_name, m_type, m_doc, m_block) in self.methods:
self.module_method += method_obj%(self.module_name,
m_name,
self.m_t[m_type],
m_block)
self.module_method += method_doc%(self.module_name,
m_name,
m_doc)
if (m_type == 'METH_VARARGS|METH_KEYWORDS') or (m_type == 'METH_NOARGS'):
self.method_list += method_def%(m_name,
'(PyCFunction)'+self.module_name,
m_name,
m_type,
self.module_name,
m_name)
else:
self.method_list += method_def%(m_name,
self.module_name,
m_name,
m_type,
self.module_name,
m_name)
return self.module_method+PyMethodDef%(self.module_name,
self.method_list)
def tail(self):
m = '''PyDoc_STRVAR(module_doc,
"%s");
'''%self.module_doc
n12 = (self.module_name,)*12
m += module_tail%n12
for a in self.module_add:
m += ''' PyModule_AddObject(m, "%s", %s);\n'''%a
m += self.tail_struct_seq
m += '''
#if PY_VERSION_HEX >= 0x03000000
return m;
#else
finally:
return;
#endif
}
'''
if self.use_numpy:
m = m.replace('/*import_array();*/', 'import_array();')
return m
def create(self):
with open(self.module_filename, 'w') as w:
w.write(self.head())
w.write(self.exception())
w.write(self.localprototype)
w.write(self.localfunction)
w.write(self.method_def())
w.write(self.tail())