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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
from base import Component
class PyCFunction(Component):
"""
PyCFunction(<name>, *components, provides=..,title=.., description=..)
>>> from __init__ import * #doctest: +ELLIPSIS
Ignoring...
>>> f = PyCFunction('hello', title='A function.', description='\\nFirst line.\\n2nd line.')
>>> a1_in_doc = '''First line.\\nSecond line.'''
>>> a1_out_doc = '''Single line.'''
>>> f += PyCArgument('a1',output_intent='return', input_title='anything',
... input_description=a1_in_doc, output_description=a1_out_doc)
>>> f += PyCArgument('c1',input_intent='extra')
>>> f += PyCArgument('b1',input_intent='optional')
>>> f += PyCArgument('d2',input_intent='hide', output_intent='return')
>>> f += PyCArgument('a2',input_intent='required')
>>> f += PyCArgument('c2',input_intent='extra')
>>> f += PyCArgument('b2',input_intent='optional')
>>> m = ExtensionModule('test_PyCFunction', f)
>>> foo = m.build() #doctest: +ELLIPSIS
exec_command...
>>> print foo.hello.__doc__
hello(a1, a2 [, b1, b2, c1, c2]) -> (a1, d2)
<BLANKLINE>
A function.
<BLANKLINE>
Required arguments:
a1 - a python object, anything
First line.
Second line.
a2 - a python object
<BLANKLINE>
Optional arguments:
b1 - a python object
b2 - a python object
<BLANKLINE>
Extra optional arguments:
c1 - a python object
c2 - a python object
<BLANKLINE>
Return values:
a1 - a python object
Single line.
d2 - a python object
<BLANKLINE>
Description:
<BLANKLINE>
First line.
2nd line.
>>> print foo.hello(1, 2)
(1, None)
"""
container_options = dict(\
Args = dict(separator=', '),
ReqArgs = dict(separator=', '),
OptArgs = dict(separator=', '),
ExtArgs = dict(separator=', '),
RetArgs = dict(separator=', ', prefix='(', suffix=')', default = 'None',
skip_prefix_when_empty=True, skip_suffix_when_empty=True),
OptExtArgs = dict(separator=', ', prefix=' [, ', skip_prefix_when_empty=True,
suffix=']', skip_suffix_when_empty=True),
FuncTitle = dict(default='<KILLLINE>',prefix='"\\n\\n',suffix='"',separator='\\n"\n" ',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
FuncDescr = dict(default='<KILLLINE>',prefix='"\\n\\nDescription:\\n"\n" ',
suffix='"',separator='\\n"\n" ',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
ReqArgsDoc = dict(default='<KILLLINE>', prefix='"\\n\\nRequired arguments:\\n"\n" ',
separator='\\n"\n" ', suffix='"',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
OptArgsDoc = dict(default='<KILLLINE>', prefix='"\\n\\nOptional arguments:\\n"\n" ',
separator='\\n"\n" ', suffix='"',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
ExtArgsDoc = dict(default='<KILLLINE>', prefix='"\\n\\nExtra optional arguments:\\n"\n" ',
separator='\\n"\n" ', suffix='"',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
RetDoc = dict(default='"Return value:\\n None\\n"', prefix='"\\n\\nReturn values:\\n"\n" ',
separator='\\n"\n" ', suffix='"',
skip_prefix_when_empty=True, skip_suffix_when_empty=True,
use_firstline_indent=True, replace_map={'\n':'\\n'}),
Decl = dict(default='<KILLLINE>', use_indent=True),
ReqKWList = dict(separator=', ', suffix=', ', skip_suffix_when_empty=True),
OptKWList = dict(separator=', ', suffix=', ', skip_suffix_when_empty=True),
ExtKWList = dict(separator=', ', suffix=', ', skip_suffix_when_empty=True),
ReqArgFmt = dict(separator=''),
OptArgFmt = dict(separator=''),
ExtArgFmt = dict(separator=''),
OptExtArgFmt = dict(separator='', prefix='|', skip_prefix_when_empty=True),
ReqArgObj = dict(separator=', ', prefix=', ', skip_prefix_when_empty=True),
OptArgObj = dict(separator=', ', prefix=', ', skip_prefix_when_empty=True),
ExtArgObj = dict(separator=', ', prefix=', ', skip_prefix_when_empty=True),
FromPyObj = dict(default='<KILLLINE>', use_indent=True),
Exec = dict(default='<KILLLINE>', use_indent=True),
PyObjFrom = dict(default='<KILLLINE>', use_indent=True),
RetFmt = dict(separator=''),
RetObj = dict(separator=', ', prefix=', ', skip_prefix_when_empty=True),
CleanPyObjFrom = dict(default='<KILLLINE>', reverse=True, use_indent=True),
CleanExec = dict(default='<KILLLINE>', reverse=True, use_indent=True),
CleanFromPyObj = dict(default='<KILLLINE>', reverse=True, use_indent=True),
)
component_container_map = dict(CCode = 'Exec',
PyCArgument = 'Args',
CDecl = 'Decl')
template = '''
static char %(pyc_name)s_doc[] =
" %(name)s(%(ReqArgs)s%(OptExtArgs)s) -> %(RetArgs)s"
%(FuncTitle)s
%(ReqArgsDoc)s
%(OptArgsDoc)s
%(ExtArgsDoc)s
%(RetDoc)s
%(FuncDescr)s
;
static PyObject*
%(pyc_name)s
(PyObject *pyc_self, PyObject *pyc_args, PyObject *pyc_keywds) {
PyObject * volatile pyc_buildvalue = NULL;
volatile int capi_success = 1;
%(Decl)s
static char *capi_kwlist[] = {%(ReqKWList)s%(OptKWList)s%(ExtKWList)sNULL};
if (PyArg_ParseTupleAndKeywords(pyc_args, pyc_keywds,"%(ReqArgFmt)s%(OptExtArgFmt)s",
capi_kwlist%(ReqArgObj)s%(OptArgObj)s%(ExtArgObj)s)) {
%(FromPyObj)s
%(Exec)s
capi_success = !PyErr_Occurred();
if (capi_success) {
%(PyObjFrom)s
pyc_buildvalue = Py_BuildValue("%(RetFmt)s"%(RetObj)s);
%(CleanPyObjFrom)s
}
%(CleanExec)s
%(CleanFromPyObj)s
}
return pyc_buildvalue;
}
'''
def initialize(self, name, *components, **options):
self.name = name
self.pyc_name = 'pyc_function_'+name
self._provides = options.pop('provides',
'%s_%s' % (self.__class__.__name__, name))
self.title = options.pop('title', None)
self.description = options.pop('description', None)
if options: self.warning('%s unused options: %s\n' % (self.__class__.__name__, options))
map(self.add, components)
return self
def init_containers(self):
return
def update_containers(self):
evaluate = self.evaluate
# update ExtensionModule containers:
t = '{"%(name)s", (PyCFunction)%(pyc_name)s,\n METH_VARARGS | METH_KEYWORDS, %(pyc_name)s_doc}'
self.container_ModuleMethod.add(evaluate(t), self.name)
# update local containers:
self.container_OptExtArgs += self.container_OptArgs + self.container_ExtArgs
self.container_OptExtArgFmt += self.container_OptArgFmt + self.container_ExtArgFmt
self.container_ModuleFuncDoc += evaluate('%(name)s(%(ReqArgs)s%(OptExtArgs)s) -> %(RetArgs)s')
if self.title is not None:
self.container_FuncTitle += self.title
self.container_ModuleFuncDoc += ' ' + self.title
if self.description is not None:
self.container_FuncDescr += self.description
# resolve dependencies
sorted_arguments = []
sorted_names = []
comp_map = {}
dep_map = {}
for (c,l) in self.components:
if not isinstance(c, Component.PyCArgument):
continue
d = [n for n in c.depends if n not in sorted_names]
if not d:
sorted_arguments.append((c,l))
sorted_names.append(c.name)
else:
comp_map[c.name] = (c,l)
dep_map[c.name] = d
while dep_map:
dep_map_copy = dep_map.copy()
for name, deps in dep_map.items():
d = [n for n in deps if dep_map.has_key(n)]
if not d:
sorted_arguments.append(comp_map[name])
del dep_map[name]
else:
dep_map[name] = d
if dep_map_copy==dep_map:
self.warnign('%s: detected cyclic dependencies in %r, incorrect behavior is expected.\n'\
% (self.provides, dep_map))
sorted_arguments += dep_map.values()
break
for c, l in sorted_arguments:
old_parent = c.parent
c.parent = self
c.ctype.set_converters(c)
c.parent = old_parent
return
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|