~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/f2py/cb_rules.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

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