comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/f2py/cb_rules.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 #!/usr/bin/env python
2 """
3
4 Build call-back mechanism for f2py2e.
5
6 Copyright 2000 Pearu Peterson all rights reserved,
7 Pearu Peterson <pearu@ioc.ee>
8 Permission to use, modify, and distribute this software is given under the
9 terms of the NumPy License.
10
11 NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
12 $Date: 2005/07/20 11:27:58 $
13 Pearu Peterson
14
15 """
16 from __future__ import division, absolute_import, print_function
17
18 import pprint
19 import sys
20
21 from . import __version__
22 from .auxfuncs import *
23 from . import cfuncs
24
25 f2py_version = __version__.version
26
27 errmess=sys.stderr.write
28 outmess=sys.stdout.write
29 show=pprint.pprint
30
31
32 ################## Rules for callback function ##############
33
34 cb_routine_rules={
35 'cbtypedefs':'typedef #rctype#(*#name#_typedef)(#optargs_td##args_td##strarglens_td##noargs#);',
36 'body':"""
37 #begintitle#
38 PyObject *#name#_capi = NULL;/*was Py_None*/
39 PyTupleObject *#name#_args_capi = NULL;
40 int #name#_nofargs = 0;
41 jmp_buf #name#_jmpbuf;
42 /*typedef #rctype#(*#name#_typedef)(#optargs_td##args_td##strarglens_td##noargs#);*/
43 #static# #rctype# #callbackname# (#optargs##args##strarglens##noargs#) {
44 \tPyTupleObject *capi_arglist = #name#_args_capi;
45 \tPyObject *capi_return = NULL;
46 \tPyObject *capi_tmp = NULL;
47 \tint capi_j,capi_i = 0;
48 \tint capi_longjmp_ok = 1;
49 #decl#
50 #ifdef F2PY_REPORT_ATEXIT
51 f2py_cb_start_clock();
52 #endif
53 \tCFUNCSMESS(\"cb:Call-back function #name# (maxnofargs=#maxnofargs#(-#nofoptargs#))\\n\");
54 \tCFUNCSMESSPY(\"cb:#name#_capi=\",#name#_capi);
55 \tif (#name#_capi==NULL) {
56 \t\tcapi_longjmp_ok = 0;
57 \t\t#name#_capi = PyObject_GetAttrString(#modulename#_module,\"#argname#\");
58 \t}
59 \tif (#name#_capi==NULL) {
60 \t\tPyErr_SetString(#modulename#_error,\"cb: Callback #argname# not defined (as an argument or module #modulename# attribute).\\n\");
61 \t\tgoto capi_fail;
62 \t}
63 \tif (F2PyCapsule_Check(#name#_capi)) {
64 \t#name#_typedef #name#_cptr;
65 \t#name#_cptr = F2PyCapsule_AsVoidPtr(#name#_capi);
66 \t#returncptr#(*#name#_cptr)(#optargs_nm##args_nm##strarglens_nm#);
67 \t#return#
68 \t}
69 \tif (capi_arglist==NULL) {
70 \t\tcapi_longjmp_ok = 0;
71 \t\tcapi_tmp = PyObject_GetAttrString(#modulename#_module,\"#argname#_extra_args\");
72 \t\tif (capi_tmp) {
73 \t\t\tcapi_arglist = (PyTupleObject *)PySequence_Tuple(capi_tmp);
74 \t\t\tif (capi_arglist==NULL) {
75 \t\t\t\tPyErr_SetString(#modulename#_error,\"Failed to convert #modulename#.#argname#_extra_args to tuple.\\n\");
76 \t\t\t\tgoto capi_fail;
77 \t\t\t}
78 \t\t} else {
79 \t\t\tPyErr_Clear();
80 \t\t\tcapi_arglist = (PyTupleObject *)Py_BuildValue(\"()\");
81 \t\t}
82 \t}
83 \tif (capi_arglist == NULL) {
84 \t\tPyErr_SetString(#modulename#_error,\"Callback #argname# argument list is not set.\\n\");
85 \t\tgoto capi_fail;
86 \t}
87 #setdims#
88 #pyobjfrom#
89 \tCFUNCSMESSPY(\"cb:capi_arglist=\",capi_arglist);
90 \tCFUNCSMESS(\"cb:Call-back calling Python function #argname#.\\n\");
91 #ifdef F2PY_REPORT_ATEXIT
92 f2py_cb_start_call_clock();
93 #endif
94 \tcapi_return = PyObject_CallObject(#name#_capi,(PyObject *)capi_arglist);
95 #ifdef F2PY_REPORT_ATEXIT
96 f2py_cb_stop_call_clock();
97 #endif
98 \tCFUNCSMESSPY(\"cb:capi_return=\",capi_return);
99 \tif (capi_return == NULL) {
100 \t\tfprintf(stderr,\"capi_return is NULL\\n\");
101 \t\tgoto capi_fail;
102 \t}
103 \tif (capi_return == Py_None) {
104 \t\tPy_DECREF(capi_return);
105 \t\tcapi_return = Py_BuildValue(\"()\");
106 \t}
107 \telse if (!PyTuple_Check(capi_return)) {
108 \t\tcapi_return = Py_BuildValue(\"(N)\",capi_return);
109 \t}
110 \tcapi_j = PyTuple_Size(capi_return);
111 \tcapi_i = 0;
112 #frompyobj#
113 \tCFUNCSMESS(\"cb:#name#:successful\\n\");
114 \tPy_DECREF(capi_return);
115 #ifdef F2PY_REPORT_ATEXIT
116 f2py_cb_stop_clock();
117 #endif
118 \tgoto capi_return_pt;
119 capi_fail:
120 \tfprintf(stderr,\"Call-back #name# failed.\\n\");
121 \tPy_XDECREF(capi_return);
122 \tif (capi_longjmp_ok)
123 \t\tlongjmp(#name#_jmpbuf,-1);
124 capi_return_pt:
125 \t;
126 #return#
127 }
128 #endtitle#
129 """,
130 'need':['setjmp.h', 'CFUNCSMESS'],
131 'maxnofargs':'#maxnofargs#',
132 'nofoptargs':'#nofoptargs#',
133 'docstr':"""\
134 \tdef #argname#(#docsignature#): return #docreturn#\\n\\
135 #docstrsigns#""",
136 'latexdocstr':"""
137 {{}\\verb@def #argname#(#latexdocsignature#): return #docreturn#@{}}
138 #routnote#
139
140 #latexdocstrsigns#""",
141 'docstrshort':'def #argname#(#docsignature#): return #docreturn#'
142 }
143 cb_rout_rules=[
144 {# Init
145 'separatorsfor': {'decl': '\n',
146 'args': ',', 'optargs': '', 'pyobjfrom': '\n', 'freemem': '\n',
147 'args_td': ',', 'optargs_td': '',
148 'args_nm': ',', 'optargs_nm': '',
149 'frompyobj': '\n', 'setdims': '\n',
150 'docstrsigns': '\\n"\n"',
151 'latexdocstrsigns': '\n',
152 'latexdocstrreq': '\n', 'latexdocstropt': '\n',
153 'latexdocstrout': '\n', 'latexdocstrcbs': '\n',
154 },
155 'decl': '/*decl*/', 'pyobjfrom': '/*pyobjfrom*/', 'frompyobj': '/*frompyobj*/',
156 'args': [], 'optargs': '', 'return': '', 'strarglens': '', 'freemem': '/*freemem*/',
157 'args_td': [], 'optargs_td': '', 'strarglens_td': '',
158 'args_nm': [], 'optargs_nm': '', 'strarglens_nm': '',
159 'noargs': '',
160 'setdims': '/*setdims*/',
161 'docstrsigns': '', 'latexdocstrsigns': '',
162 'docstrreq': '\tRequired arguments:',
163 'docstropt': '\tOptional arguments:',
164 'docstrout': '\tReturn objects:',
165 'docstrcbs': '\tCall-back functions:',
166 'docreturn': '', 'docsign': '', 'docsignopt': '',
167 'latexdocstrreq': '\\noindent Required arguments:',
168 'latexdocstropt': '\\noindent Optional arguments:',
169 'latexdocstrout': '\\noindent Return objects:',
170 'latexdocstrcbs': '\\noindent Call-back functions:',
171 'routnote': {hasnote:'--- #note#',l_not(hasnote):''},
172 }, { # Function
173 'decl':'\t#ctype# return_value;',
174 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting return_value->");'},
175 '\tif (capi_j>capi_i)\n\t\tGETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n");',
176 {debugcapi:'\tfprintf(stderr,"#showvalueformat#.\\n",return_value);'}
177 ],
178 'need':['#ctype#_from_pyobj', {debugcapi:'CFUNCSMESS'}, 'GETSCALARFROMPYTUPLE'],
179 'return':'\treturn return_value;',
180 '_check':l_and(isfunction, l_not(isstringfunction), l_not(iscomplexfunction))
181 },
182 {# String function
183 'pyobjfrom':{debugcapi:'\tfprintf(stderr,"debug-capi:cb:#name#:%d:\\n",return_value_len);'},
184 'args':'#ctype# return_value,int return_value_len',
185 'args_nm':'return_value,&return_value_len',
186 'args_td':'#ctype# ,int',
187 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting return_value->\\"");'},
188 """\tif (capi_j>capi_i)
189 \t\tGETSTRFROMPYTUPLE(capi_return,capi_i++,return_value,return_value_len);""",
190 {debugcapi:'\tfprintf(stderr,"#showvalueformat#\\".\\n",return_value);'}
191 ],
192 'need':['#ctype#_from_pyobj', {debugcapi:'CFUNCSMESS'},
193 'string.h', 'GETSTRFROMPYTUPLE'],
194 'return':'return;',
195 '_check':isstringfunction
196 },
197 {# Complex function
198 'optargs':"""
199 #ifndef F2PY_CB_RETURNCOMPLEX
200 #ctype# *return_value
201 #endif
202 """,
203 'optargs_nm':"""
204 #ifndef F2PY_CB_RETURNCOMPLEX
205 return_value
206 #endif
207 """,
208 'optargs_td':"""
209 #ifndef F2PY_CB_RETURNCOMPLEX
210 #ctype# *
211 #endif
212 """,
213 'decl':"""
214 #ifdef F2PY_CB_RETURNCOMPLEX
215 \t#ctype# return_value;
216 #endif
217 """,
218 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting return_value->");'},
219 """\
220 \tif (capi_j>capi_i)
221 #ifdef F2PY_CB_RETURNCOMPLEX
222 \t\tGETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,\"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n\");
223 #else
224 \t\tGETSCALARFROMPYTUPLE(capi_return,capi_i++,return_value,#ctype#,\"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n\");
225 #endif
226 """,
227 {debugcapi:"""
228 #ifdef F2PY_CB_RETURNCOMPLEX
229 \tfprintf(stderr,\"#showvalueformat#.\\n\",(return_value).r,(return_value).i);
230 #else
231 \tfprintf(stderr,\"#showvalueformat#.\\n\",(*return_value).r,(*return_value).i);
232 #endif
233
234 """}
235 ],
236 'return':"""
237 #ifdef F2PY_CB_RETURNCOMPLEX
238 \treturn return_value;
239 #else
240 \treturn;
241 #endif
242 """,
243 'need':['#ctype#_from_pyobj', {debugcapi:'CFUNCSMESS'},
244 'string.h', 'GETSCALARFROMPYTUPLE', '#ctype#'],
245 '_check':iscomplexfunction
246 },
247 {'docstrout':'\t\t#pydocsignout#',
248 'latexdocstrout':['\\item[]{{}\\verb@#pydocsignout#@{}}',
249 {hasnote:'--- #note#'}],
250 'docreturn':'#rname#,',
251 '_check':isfunction},
252 {'_check':issubroutine,'return':'return;'}
253 ]
254
255 cb_arg_rules=[
256 { # Doc
257 'docstropt':{l_and(isoptional, isintent_nothide):'\t\t#pydocsign#'},
258 'docstrreq':{l_and(isrequired, isintent_nothide):'\t\t#pydocsign#'},
259 'docstrout':{isintent_out:'\t\t#pydocsignout#'},
260 'latexdocstropt':{l_and(isoptional, isintent_nothide):['\\item[]{{}\\verb@#pydocsign#@{}}',
261 {hasnote:'--- #note#'}]},
262 'latexdocstrreq':{l_and(isrequired, isintent_nothide):['\\item[]{{}\\verb@#pydocsign#@{}}',
263 {hasnote:'--- #note#'}]},
264 'latexdocstrout':{isintent_out:['\\item[]{{}\\verb@#pydocsignout#@{}}',
265 {l_and(hasnote, isintent_hide):'--- #note#',
266 l_and(hasnote, isintent_nothide):'--- See above.'}]},
267 'docsign':{l_and(isrequired, isintent_nothide):'#varname#,'},
268 'docsignopt':{l_and(isoptional, isintent_nothide):'#varname#,'},
269 'depend':''
270 },
271 {
272 'args': {
273 l_and (isscalar, isintent_c):'#ctype# #varname_i#',
274 l_and (isscalar, l_not(isintent_c)):'#ctype# *#varname_i#_cb_capi',
275 isarray:'#ctype# *#varname_i#',
276 isstring:'#ctype# #varname_i#'
277 },
278 'args_nm': {
279 l_and (isscalar, isintent_c):'#varname_i#',
280 l_and (isscalar, l_not(isintent_c)):'#varname_i#_cb_capi',
281 isarray:'#varname_i#',
282 isstring:'#varname_i#'
283 },
284 'args_td': {
285 l_and (isscalar, isintent_c):'#ctype#',
286 l_and (isscalar, l_not(isintent_c)):'#ctype# *',
287 isarray:'#ctype# *',
288 isstring:'#ctype#'
289 },
290 'strarglens': {isstring:',int #varname_i#_cb_len'}, # untested with multiple args
291 'strarglens_td': {isstring:',int'}, # untested with multiple args
292 'strarglens_nm': {isstring:',#varname_i#_cb_len'}, # untested with multiple args
293 },
294 { # Scalars
295 'decl':{l_not(isintent_c):'\t#ctype# #varname_i#=(*#varname_i#_cb_capi);'},
296 'error': {l_and(isintent_c, isintent_out,
297 throw_error('intent(c,out) is forbidden for callback scalar arguments')):\
298 ''},
299 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting #varname#->");'},
300 {isintent_out:'\tif (capi_j>capi_i)\n\t\tGETSCALARFROMPYTUPLE(capi_return,capi_i++,#varname_i#_cb_capi,#ctype#,"#ctype#_from_pyobj failed in converting argument #varname# of call-back function #name# to C #ctype#\\n");'},
301 {l_and(debugcapi, l_and(l_not(iscomplex), isintent_c)):'\tfprintf(stderr,"#showvalueformat#.\\n",#varname_i#);'},
302 {l_and(debugcapi, l_and(l_not(iscomplex), l_not(isintent_c))):'\tfprintf(stderr,"#showvalueformat#.\\n",*#varname_i#_cb_capi);'},
303 {l_and(debugcapi, l_and(iscomplex, isintent_c)):'\tfprintf(stderr,"#showvalueformat#.\\n",(#varname_i#).r,(#varname_i#).i);'},
304 {l_and(debugcapi, l_and(iscomplex, l_not(isintent_c))):'\tfprintf(stderr,"#showvalueformat#.\\n",(*#varname_i#_cb_capi).r,(*#varname_i#_cb_capi).i);'},
305 ],
306 'need':[{isintent_out:['#ctype#_from_pyobj', 'GETSCALARFROMPYTUPLE']},
307 {debugcapi:'CFUNCSMESS'}],
308 '_check':isscalar
309 }, {
310 'pyobjfrom':[{isintent_in:"""\
311 \tif (#name#_nofargs>capi_i)
312 \t\tif (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,pyobj_from_#ctype#1(#varname_i#)))
313 \t\t\tgoto capi_fail;"""},
314 {isintent_inout:"""\
315 \tif (#name#_nofargs>capi_i)
316 \t\tif (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,pyarr_from_p_#ctype#1(#varname_i#_cb_capi)))
317 \t\t\tgoto capi_fail;"""}],
318 'need':[{isintent_in:'pyobj_from_#ctype#1'},
319 {isintent_inout:'pyarr_from_p_#ctype#1'},
320 {iscomplex:'#ctype#'}],
321 '_check':l_and(isscalar, isintent_nothide),
322 '_optional':''
323 }, {# String
324 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting #varname#->\\"");'},
325 """\tif (capi_j>capi_i)
326 \t\tGETSTRFROMPYTUPLE(capi_return,capi_i++,#varname_i#,#varname_i#_cb_len);""",
327 {debugcapi:'\tfprintf(stderr,"#showvalueformat#\\":%d:.\\n",#varname_i#,#varname_i#_cb_len);'},
328 ],
329 'need':['#ctype#', 'GETSTRFROMPYTUPLE',
330 {debugcapi:'CFUNCSMESS'}, 'string.h'],
331 '_check':l_and(isstring, isintent_out)
332 }, {
333 'pyobjfrom':[{debugcapi:'\tfprintf(stderr,"debug-capi:cb:#varname#=\\"#showvalueformat#\\":%d:\\n",#varname_i#,#varname_i#_cb_len);'},
334 {isintent_in:"""\
335 \tif (#name#_nofargs>capi_i)
336 \t\tif (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,pyobj_from_#ctype#1size(#varname_i#,#varname_i#_cb_len)))
337 \t\t\tgoto capi_fail;"""},
338 {isintent_inout:"""\
339 \tif (#name#_nofargs>capi_i) {
340 \t\tint #varname_i#_cb_dims[] = {#varname_i#_cb_len};
341 \t\tif (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,pyarr_from_p_#ctype#1(#varname_i#,#varname_i#_cb_dims)))
342 \t\t\tgoto capi_fail;
343 \t}"""}],
344 'need':[{isintent_in:'pyobj_from_#ctype#1size'},
345 {isintent_inout:'pyarr_from_p_#ctype#1'}],
346 '_check':l_and(isstring, isintent_nothide),
347 '_optional':''
348 },
349 # Array ...
350 {
351 'decl':'\tnpy_intp #varname_i#_Dims[#rank#] = {#rank*[-1]#};',
352 'setdims':'\t#cbsetdims#;',
353 '_check':isarray,
354 '_depend':''
355 },
356 {
357 'pyobjfrom': [{debugcapi:'\tfprintf(stderr,"debug-capi:cb:#varname#\\n");'},
358 {isintent_c: """\
359 \tif (#name#_nofargs>capi_i) {
360 \t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,0,NPY_CARRAY,NULL); /*XXX: Hmm, what will destroy this array??? */
361 """,
362 l_not(isintent_c): """\
363 \tif (#name#_nofargs>capi_i) {
364 \t\tPyArrayObject *tmp_arr = (PyArrayObject *)PyArray_New(&PyArray_Type,#rank#,#varname_i#_Dims,#atype#,NULL,(char*)#varname_i#,0,NPY_FARRAY,NULL); /*XXX: Hmm, what will destroy this array??? */
365 """,
366 },
367 """
368 \t\tif (tmp_arr==NULL)
369 \t\t\tgoto capi_fail;
370 \t\tif (PyTuple_SetItem((PyObject *)capi_arglist,capi_i++,(PyObject *)tmp_arr))
371 \t\t\tgoto capi_fail;
372 }"""],
373 '_check': l_and(isarray, isintent_nothide, l_or(isintent_in, isintent_inout)),
374 '_optional': '',
375 }, {
376 'frompyobj':[{debugcapi:'\tCFUNCSMESS("cb:Getting #varname#->");'},
377 """\tif (capi_j>capi_i) {
378 \t\tPyArrayObject *rv_cb_arr = NULL;
379 \t\tif ((capi_tmp = PyTuple_GetItem(capi_return,capi_i++))==NULL) goto capi_fail;
380 \t\trv_cb_arr = array_from_pyobj(#atype#,#varname_i#_Dims,#rank#,F2PY_INTENT_IN""",
381 {isintent_c:'|F2PY_INTENT_C'},
382 """,capi_tmp);
383 \t\tif (rv_cb_arr == NULL) {
384 \t\t\tfprintf(stderr,\"rv_cb_arr is NULL\\n\");
385 \t\t\tgoto capi_fail;
386 \t\t}
387 \t\tMEMCOPY(#varname_i#,rv_cb_arr->data,PyArray_NBYTES(rv_cb_arr));
388 \t\tif (capi_tmp != (PyObject *)rv_cb_arr) {
389 \t\t\tPy_DECREF(rv_cb_arr);
390 \t\t}
391 \t}""",
392 {debugcapi:'\tfprintf(stderr,"<-.\\n");'},
393 ],
394 'need':['MEMCOPY', {iscomplexarray:'#ctype#'}],
395 '_check':l_and(isarray, isintent_out)
396 }, {
397 'docreturn':'#varname#,',
398 '_check':isintent_out
399 }
400 ]
401
402 ################## Build call-back module #############
403 cb_map={}
404 def buildcallbacks(m):
405 global cb_map
406 cb_map[m['name']]=[]
407 for bi in m['body']:
408 if bi['block']=='interface':
409 for b in bi['body']:
410 if b:
411 buildcallback(b, m['name'])
412 else:
413 errmess('warning: empty body for %s\n' % (m['name']))
414
415 def buildcallback(rout, um):
416 global cb_map
417 from . import capi_maps
418
419 outmess('\tConstructing call-back function "cb_%s_in_%s"\n'%(rout['name'], um))
420 args, depargs=getargs(rout)
421 capi_maps.depargs=depargs
422 var=rout['vars']
423 vrd=capi_maps.cb_routsign2map(rout, um)
424 rd=dictappend({}, vrd)
425 cb_map[um].append([rout['name'], rd['name']])
426 for r in cb_rout_rules:
427 if ('_check' in r and r['_check'](rout)) or ('_check' not in r):
428 ar=applyrules(r, vrd, rout)
429 rd=dictappend(rd, ar)
430 savevrd={}
431 for i, a in enumerate(args):
432 vrd=capi_maps.cb_sign2map(a, var[a], index=i)
433 savevrd[a]=vrd
434 for r in cb_arg_rules:
435 if '_depend' in r:
436 continue
437 if '_optional' in r and isoptional(var[a]):
438 continue
439 if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
440 ar=applyrules(r, vrd, var[a])
441 rd=dictappend(rd, ar)
442 if '_break' in r:
443 break
444 for a in args:
445 vrd=savevrd[a]
446 for r in cb_arg_rules:
447 if '_depend' in r:
448 continue
449 if ('_optional' not in r) or ('_optional' in r and isrequired(var[a])):
450 continue
451 if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
452 ar=applyrules(r, vrd, var[a])
453 rd=dictappend(rd, ar)
454 if '_break' in r:
455 break
456 for a in depargs:
457 vrd=savevrd[a]
458 for r in cb_arg_rules:
459 if '_depend' not in r:
460 continue
461 if '_optional' in r:
462 continue
463 if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
464 ar=applyrules(r, vrd, var[a])
465 rd=dictappend(rd, ar)
466 if '_break' in r:
467 break
468 if 'args' in rd and 'optargs' in rd:
469 if isinstance(rd['optargs'], list):
470 rd['optargs']=rd['optargs']+["""
471 #ifndef F2PY_CB_RETURNCOMPLEX
472 ,
473 #endif
474 """]
475 rd['optargs_nm']=rd['optargs_nm']+["""
476 #ifndef F2PY_CB_RETURNCOMPLEX
477 ,
478 #endif
479 """]
480 rd['optargs_td']=rd['optargs_td']+["""
481 #ifndef F2PY_CB_RETURNCOMPLEX
482 ,
483 #endif
484 """]
485 if isinstance(rd['docreturn'], list):
486 rd['docreturn']=stripcomma(replace('#docreturn#', {'docreturn':rd['docreturn']}))
487 optargs=stripcomma(replace('#docsignopt#',
488 {'docsignopt':rd['docsignopt']}
489 ))
490 if optargs=='':
491 rd['docsignature']=stripcomma(replace('#docsign#', {'docsign':rd['docsign']}))
492 else:
493 rd['docsignature']=replace('#docsign#[#docsignopt#]',
494 {'docsign': rd['docsign'],
495 'docsignopt': optargs,
496 })
497 rd['latexdocsignature']=rd['docsignature'].replace('_', '\\_')
498 rd['latexdocsignature']=rd['latexdocsignature'].replace(',', ', ')
499 rd['docstrsigns']=[]
500 rd['latexdocstrsigns']=[]
501 for k in ['docstrreq', 'docstropt', 'docstrout', 'docstrcbs']:
502 if k in rd and isinstance(rd[k], list):
503 rd['docstrsigns']=rd['docstrsigns']+rd[k]
504 k='latex'+k
505 if k in rd and isinstance(rd[k], list):
506 rd['latexdocstrsigns']=rd['latexdocstrsigns']+rd[k][0:1]+\
507 ['\\begin{description}']+rd[k][1:]+\
508 ['\\end{description}']
509 if 'args' not in rd:
510 rd['args']=''
511 rd['args_td']=''
512 rd['args_nm']=''
513 if not (rd.get('args') or rd.get('optargs') or rd.get('strarglens')):
514 rd['noargs'] = 'void'
515
516 ar=applyrules(cb_routine_rules, rd)
517 cfuncs.callbacks[rd['name']]=ar['body']
518 if isinstance(ar['need'], str):
519 ar['need']=[ar['need']]
520
521 if 'need' in rd:
522 for t in cfuncs.typedefs.keys():
523 if t in rd['need']:
524 ar['need'].append(t)
525
526 cfuncs.typedefs_generated[rd['name']+'_typedef'] = ar['cbtypedefs']
527 ar['need'].append(rd['name']+'_typedef')
528 cfuncs.needs[rd['name']]=ar['need']
529
530 capi_maps.lcb2_map[rd['name']]={'maxnofargs':ar['maxnofargs'],
531 'nofoptargs':ar['nofoptargs'],
532 'docstr':ar['docstr'],
533 'latexdocstr':ar['latexdocstr'],
534 'argname':rd['argname']
535 }
536 outmess('\t %s\n'%(ar['docstrshort']))
537 #print ar['body']
538 return
539 ################## Build call-back function #############