Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/f2py/f90mod_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 F90 module support 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/02/03 19:30:23 $ | |
13 Pearu Peterson | |
14 | |
15 """ | |
16 from __future__ import division, absolute_import, print_function | |
17 | |
18 __version__ = "$Revision: 1.27 $"[10:-1] | |
19 | |
20 f2py_version='See `f2py -v`' | |
21 | |
22 import pprint | |
23 import sys | |
24 errmess=sys.stderr.write | |
25 outmess=sys.stdout.write | |
26 show=pprint.pprint | |
27 | |
28 from .auxfuncs import * | |
29 import numpy as np | |
30 from . import capi_maps | |
31 from . import func2subr | |
32 from .crackfortran import undo_rmbadname, undo_rmbadname1 | |
33 | |
34 options={} | |
35 | |
36 def findf90modules(m): | |
37 if ismodule(m): return [m] | |
38 if not hasbody(m): return [] | |
39 ret = [] | |
40 for b in m['body']: | |
41 if ismodule(b): ret.append(b) | |
42 else: ret=ret+findf90modules(b) | |
43 return ret | |
44 | |
45 fgetdims1 = """\ | |
46 external f2pysetdata | |
47 logical ns | |
48 integer r,i,j | |
49 integer(%d) s(*) | |
50 ns = .FALSE. | |
51 if (allocated(d)) then | |
52 do i=1,r | |
53 if ((size(d,i).ne.s(i)).and.(s(i).ge.0)) then | |
54 ns = .TRUE. | |
55 end if | |
56 end do | |
57 if (ns) then | |
58 deallocate(d) | |
59 end if | |
60 end if | |
61 if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.intp().itemsize | |
62 | |
63 fgetdims2="""\ | |
64 end if | |
65 if (allocated(d)) then | |
66 do i=1,r | |
67 s(i) = size(d,i) | |
68 end do | |
69 end if | |
70 flag = 1 | |
71 call f2pysetdata(d,allocated(d))""" | |
72 | |
73 fgetdims2_sa="""\ | |
74 end if | |
75 if (allocated(d)) then | |
76 do i=1,r | |
77 s(i) = size(d,i) | |
78 end do | |
79 !s(r) must be equal to len(d(1)) | |
80 end if | |
81 flag = 2 | |
82 call f2pysetdata(d,allocated(d))""" | |
83 | |
84 | |
85 def buildhooks(pymod): | |
86 global fgetdims1, fgetdims2 | |
87 from . import rules | |
88 ret = {'f90modhooks':[],'initf90modhooks':[],'body':[], | |
89 'need':['F_FUNC', 'arrayobject.h'], | |
90 'separatorsfor':{'includes0':'\n','includes':'\n'}, | |
91 'docs':['"Fortran 90/95 modules:\\n"'], | |
92 'latexdoc':[]} | |
93 fhooks=[''] | |
94 def fadd(line,s=fhooks): s[0] = '%s\n %s'%(s[0], line) | |
95 doc = [''] | |
96 def dadd(line,s=doc): s[0] = '%s\n%s'%(s[0], line) | |
97 for m in findf90modules(pymod): | |
98 sargs, fargs, efargs, modobjs, notvars, onlyvars=[], [], [], [], [m['name']], [] | |
99 sargsp = [] | |
100 ifargs = [] | |
101 mfargs = [] | |
102 if hasbody(m): | |
103 for b in m['body']: notvars.append(b['name']) | |
104 for n in m['vars'].keys(): | |
105 var = m['vars'][n] | |
106 if (n not in notvars) and (not l_or(isintent_hide, isprivate)(var)): | |
107 onlyvars.append(n) | |
108 mfargs.append(n) | |
109 outmess('\t\tConstructing F90 module support for "%s"...\n'%(m['name'])) | |
110 if onlyvars: | |
111 outmess('\t\t Variables: %s\n'%(' '.join(onlyvars))) | |
112 chooks=[''] | |
113 def cadd(line,s=chooks): s[0] = '%s\n%s'%(s[0], line) | |
114 ihooks=[''] | |
115 def iadd(line,s=ihooks): s[0] = '%s\n%s'%(s[0], line) | |
116 | |
117 vrd=capi_maps.modsign2map(m) | |
118 cadd('static FortranDataDef f2py_%s_def[] = {'%(m['name'])) | |
119 dadd('\\subsection{Fortran 90/95 module \\texttt{%s}}\n'%(m['name'])) | |
120 if hasnote(m): | |
121 note = m['note'] | |
122 if isinstance(note, list): note='\n'.join(note) | |
123 dadd(note) | |
124 if onlyvars: | |
125 dadd('\\begin{description}') | |
126 for n in onlyvars: | |
127 var = m['vars'][n] | |
128 modobjs.append(n) | |
129 ct = capi_maps.getctype(var) | |
130 at = capi_maps.c2capi_map[ct] | |
131 dm = capi_maps.getarrdims(n, var) | |
132 dms = dm['dims'].replace('*', '-1').strip() | |
133 dms = dms.replace(':', '-1').strip() | |
134 if not dms: dms='-1' | |
135 use_fgetdims2 = fgetdims2 | |
136 if isstringarray(var): | |
137 if 'charselector' in var and 'len' in var['charselector']: | |
138 cadd('\t{"%s",%s,{{%s,%s}},%s},'\ | |
139 %(undo_rmbadname1(n), dm['rank'], dms, var['charselector']['len'], at)) | |
140 use_fgetdims2 = fgetdims2_sa | |
141 else: | |
142 cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n), dm['rank'], dms, at)) | |
143 else: | |
144 cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n), dm['rank'], dms, at)) | |
145 dadd('\\item[]{{}\\verb@%s@{}}'%(capi_maps.getarrdocsign(n, var))) | |
146 if hasnote(var): | |
147 note = var['note'] | |
148 if isinstance(note, list): note='\n'.join(note) | |
149 dadd('--- %s'%(note)) | |
150 if isallocatable(var): | |
151 fargs.append('f2py_%s_getdims_%s'%(m['name'], n)) | |
152 efargs.append(fargs[-1]) | |
153 sargs.append('void (*%s)(int*,int*,void(*)(char*,int*),int*)'%(n)) | |
154 sargsp.append('void (*)(int*,int*,void(*)(char*,int*),int*)') | |
155 iadd('\tf2py_%s_def[i_f2py++].func = %s;'%(m['name'], n)) | |
156 fadd('subroutine %s(r,s,f2pysetdata,flag)'%(fargs[-1])) | |
157 fadd('use %s, only: d => %s\n'%(m['name'], undo_rmbadname1(n))) | |
158 fadd('integer flag\n') | |
159 fhooks[0]=fhooks[0]+fgetdims1 | |
160 dms = eval('range(1,%s+1)'%(dm['rank'])) | |
161 fadd(' allocate(d(%s))\n'%(','.join(['s(%s)'%i for i in dms]))) | |
162 fhooks[0]=fhooks[0]+use_fgetdims2 | |
163 fadd('end subroutine %s'%(fargs[-1])) | |
164 else: | |
165 fargs.append(n) | |
166 sargs.append('char *%s'%(n)) | |
167 sargsp.append('char*') | |
168 iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'], n)) | |
169 if onlyvars: | |
170 dadd('\\end{description}') | |
171 if hasbody(m): | |
172 for b in m['body']: | |
173 if not isroutine(b): | |
174 print('Skipping', b['block'], b['name']) | |
175 continue | |
176 modobjs.append('%s()'%(b['name'])) | |
177 b['modulename'] = m['name'] | |
178 api, wrap=rules.buildapi(b) | |
179 if isfunction(b): | |
180 fhooks[0]=fhooks[0]+wrap | |
181 fargs.append('f2pywrap_%s_%s'%(m['name'], b['name'])) | |
182 #efargs.append(fargs[-1]) | |
183 ifargs.append(func2subr.createfuncwrapper(b, signature=1)) | |
184 else: | |
185 if wrap: | |
186 fhooks[0]=fhooks[0]+wrap | |
187 fargs.append('f2pywrap_%s_%s'%(m['name'], b['name'])) | |
188 ifargs.append(func2subr.createsubrwrapper(b, signature=1)) | |
189 else: | |
190 fargs.append(b['name']) | |
191 mfargs.append(fargs[-1]) | |
192 #if '--external-modroutines' in options and options['--external-modroutines']: | |
193 # outmess('\t\t\tapplying --external-modroutines for %s\n'%(b['name'])) | |
194 # efargs.append(fargs[-1]) | |
195 api['externroutines']=[] | |
196 ar=applyrules(api, vrd) | |
197 ar['docs']=[] | |
198 ar['docshort']=[] | |
199 ret=dictappend(ret, ar) | |
200 cadd('\t{"%s",-1,{{-1}},0,NULL,(void *)f2py_rout_#modulename#_%s_%s,doc_f2py_rout_#modulename#_%s_%s},'%(b['name'], m['name'], b['name'], m['name'], b['name'])) | |
201 sargs.append('char *%s'%(b['name'])) | |
202 sargsp.append('char *') | |
203 iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'], b['name'])) | |
204 cadd('\t{NULL}\n};\n') | |
205 iadd('}') | |
206 ihooks[0]='static void f2py_setup_%s(%s) {\n\tint i_f2py=0;%s'%(m['name'], ','.join(sargs), ihooks[0]) | |
207 if '_' in m['name']: | |
208 F_FUNC='F_FUNC_US' | |
209 else: | |
210 F_FUNC='F_FUNC' | |
211 iadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void (*)(%s));'\ | |
212 %(F_FUNC, m['name'], m['name'].upper(), ','.join(sargsp))) | |
213 iadd('static void f2py_init_%s(void) {'%(m['name'])) | |
214 iadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'\ | |
215 %(F_FUNC, m['name'], m['name'].upper(), m['name'])) | |
216 iadd('}\n') | |
217 ret['f90modhooks']=ret['f90modhooks']+chooks+ihooks | |
218 ret['initf90modhooks']=['\tPyDict_SetItemString(d, "%s", PyFortranObject_New(f2py_%s_def,f2py_init_%s));'%(m['name'], m['name'], m['name'])]+ret['initf90modhooks'] | |
219 fadd('') | |
220 fadd('subroutine f2pyinit%s(f2pysetupfunc)'%(m['name'])) | |
221 #fadd('use %s'%(m['name'])) | |
222 if mfargs: | |
223 for a in undo_rmbadname(mfargs): | |
224 fadd('use %s, only : %s'%(m['name'], a)) | |
225 if ifargs: | |
226 fadd(' '.join(['interface']+ifargs)) | |
227 fadd('end interface') | |
228 fadd('external f2pysetupfunc') | |
229 if efargs: | |
230 for a in undo_rmbadname(efargs): | |
231 fadd('external %s'%(a)) | |
232 fadd('call f2pysetupfunc(%s)'%(','.join(undo_rmbadname(fargs)))) | |
233 fadd('end subroutine f2pyinit%s\n'%(m['name'])) | |
234 | |
235 dadd('\n'.join(ret['latexdoc']).replace(r'\subsection{', r'\subsubsection{')) | |
236 | |
237 ret['latexdoc']=[] | |
238 ret['docs'].append('"\t%s --- %s"'%(m['name'], | |
239 ','.join(undo_rmbadname(modobjs)))) | |
240 | |
241 ret['routine_defs']='' | |
242 ret['doc']=[] | |
243 ret['docshort']=[] | |
244 ret['latexdoc']=doc[0] | |
245 if len(ret['docs'])<=1: ret['docs']='' | |
246 return ret, fhooks[0] |