~ubuntu-branches/ubuntu/precise/python-numpy/precise

« back to all changes in this revision

Viewing changes to numpy/f2py/func2subr.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2012-02-11 12:55:21 UTC
  • mfrom: (7.1.9 experimental) (7.2.3 sid)
  • Revision ID: package-import@ubuntu.com-20120211125521-31q3am7pp3mvt1ho
Tags: 1:1.6.1-5ubuntu1
* debian/patches/search-multiarch-paths.patch: (LP: #818867)
  - add multiarch libdirs to numpy.distutils.system_info
* Merge from Debian unstable, remaining changes:
  - debian/patches/20_disable-plot-extension.patch
     Disable plot_directive extension, and catch ImportErrors when
     matplotlib cannot be imported, which allows us to remove
     python-matplotlib from dependencies.  This is required because
     python-numpy is in main, while python-matplotlib is in universe.
  - Build using dh_python2
    add bin/f2py* to .install files
  - keep Replaces: python-numpy (<< 1:1.3.0-4) in python-numpy-dbg
    for lucid upgrades

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
 
76
76
def createfuncwrapper(rout,signature=0):
77
77
    assert isfunction(rout)
 
78
 
 
79
    extra_args = []
 
80
    vars = rout['vars']
 
81
    for a in rout['args']:
 
82
        v = rout['vars'][a]
 
83
        for i,d in enumerate(v.get('dimension',[])):
 
84
            if d==':':
 
85
                dn = 'f2py_%s_d%s' % (a, i)
 
86
                dv = dict(typespec='integer', intent=['hide'])
 
87
                dv['='] = 'shape(%s, %s)' % (a, i)
 
88
                extra_args.append(dn)
 
89
                vars[dn] = dv
 
90
                v['dimension'][i] = dn
 
91
    rout['args'].extend(extra_args)
 
92
    need_interface = bool(extra_args)
 
93
    
78
94
    ret = ['']
79
95
    def add(line,ret=ret):
80
96
        ret[0] = '%s\n      %s'%(ret[0],line)
82
98
    fortranname = getfortranname(rout)
83
99
    f90mode = ismoduleroutine(rout)
84
100
    newname = '%sf2pywrap'%(name)
85
 
    vars = rout['vars']
 
101
 
86
102
    if newname not in vars:
87
103
        vars[newname] = vars[name]
88
104
        args = [newname]+rout['args'][1:]
98
114
        charselect = vars[name]['charselector']
99
115
        if charselect.get('*','')=='(*)':
100
116
            charselect['*'] = '10'
 
117
    sargs = ', '.join(args)
101
118
    if f90mode:
102
 
        sargs = ', '.join(args)
103
119
        add('subroutine f2pywrap_%s_%s (%s)'%(rout['modulename'],name,sargs))
104
120
        if not signature:
105
121
            add('use %s, only : %s'%(rout['modulename'],fortranname))
106
122
    else:
107
 
        add('subroutine f2pywrap%s (%s)'%(name,', '.join(args)))
108
 
        add('external %s'%(fortranname))
109
 
        #if not return_char_star:
110
 
        l = l + ', '+fortranname
 
123
        add('subroutine f2pywrap%s (%s)'%(name,sargs))
 
124
        if not need_interface:
 
125
            add('external %s'%(fortranname))
 
126
            l = l + ', '+fortranname
 
127
    if need_interface:
 
128
        for line in rout['saved_interface'].split('\n'):
 
129
            if line.lstrip().startswith('use '):
 
130
                add(line)
 
131
 
111
132
    args = args[1:]
112
133
    dumped_args = []
113
134
    for a in args:
121
142
            dumped_args.append(a)
122
143
    for a in args:
123
144
        if a in dumped_args: continue
 
145
        if isintent_in(vars[a]):
 
146
            add(var2fixfortran(vars,a,f90mode=f90mode))
 
147
            dumped_args.append(a)
 
148
    for a in args:
 
149
        if a in dumped_args: continue
124
150
        add(var2fixfortran(vars,a,f90mode=f90mode))
125
 
 
 
151
            
126
152
    add(l)
127
153
 
 
154
    if need_interface:
 
155
        if f90mode:
 
156
            # f90 module already defines needed interface
 
157
            pass
 
158
        else:
 
159
            add('interface')
 
160
            add(rout['saved_interface'].lstrip())
 
161
            add('end interface')
 
162
 
 
163
    sargs = ', '.join([a for a in args if a not in extra_args])
 
164
 
128
165
    if not signature:
129
166
        if islogicalfunction(rout):
130
 
            add('%s = .not.(.not.%s(%s))'%(newname,fortranname,', '.join(args)))
131
 
        else:
132
 
            add('%s = %s(%s)'%(newname,fortranname,', '.join(args)))
133
 
    if f90mode:
134
 
        add('end subroutine f2pywrap_%s_%s'%(rout['modulename'],name))
135
 
    else:
136
 
        add('end')
137
 
    #print '**'*10
138
 
    #print ret[0]
139
 
    #print '**'*10
140
 
    return ret[0]
 
167
            add('%s = .not.(.not.%s(%s))'%(newname,fortranname,sargs))
 
168
        else:
 
169
            add('%s = %s(%s)'%(newname,fortranname,sargs))
 
170
    if f90mode:
 
171
        add('end subroutine f2pywrap_%s_%s'%(rout['modulename'],name))
 
172
    else:
 
173
        add('end')
 
174
    #print '**'*10
 
175
    #print ret[0]
 
176
    #print '**'*10
 
177
    return ret[0]
 
178
 
 
179
def createsubrwrapper(rout,signature=0):
 
180
    assert issubroutine(rout)
 
181
 
 
182
    extra_args = []
 
183
    vars = rout['vars']
 
184
    for a in rout['args']:
 
185
        v = rout['vars'][a]
 
186
        for i,d in enumerate(v.get('dimension',[])):
 
187
            if d==':':
 
188
                dn = 'f2py_%s_d%s' % (a, i)
 
189
                dv = dict(typespec='integer', intent=['hide'])
 
190
                dv['='] = 'shape(%s, %s)' % (a, i)
 
191
                extra_args.append(dn)
 
192
                vars[dn] = dv
 
193
                v['dimension'][i] = dn
 
194
    rout['args'].extend(extra_args)
 
195
    need_interface = bool(extra_args)
 
196
 
 
197
    ret = ['']
 
198
    def add(line,ret=ret):
 
199
        ret[0] = '%s\n      %s'%(ret[0],line)
 
200
    name = rout['name']
 
201
    fortranname = getfortranname(rout)
 
202
    f90mode = ismoduleroutine(rout)
 
203
 
 
204
    args = rout['args']
 
205
 
 
206
    sargs = ', '.join(args)
 
207
    if f90mode:
 
208
        add('subroutine f2pywrap_%s_%s (%s)'%(rout['modulename'],name,sargs))
 
209
        if not signature:
 
210
            add('use %s, only : %s'%(rout['modulename'],fortranname))
 
211
    else:
 
212
        add('subroutine f2pywrap%s (%s)'%(name,sargs))
 
213
        if not need_interface:
 
214
            add('external %s'%(fortranname))
 
215
 
 
216
    if need_interface:
 
217
        for line in rout['saved_interface'].split('\n'):
 
218
            if line.lstrip().startswith('use '):
 
219
                add(line)
 
220
 
 
221
    dumped_args = []
 
222
    for a in args:
 
223
        if isexternal(vars[a]):
 
224
            add('external %s'%(a))
 
225
            dumped_args.append(a)
 
226
    for a in args:
 
227
        if a in dumped_args: continue
 
228
        if isscalar(vars[a]):
 
229
            add(var2fixfortran(vars,a,f90mode=f90mode))
 
230
            dumped_args.append(a)
 
231
    for a in args:
 
232
        if a in dumped_args: continue
 
233
        add(var2fixfortran(vars,a,f90mode=f90mode))
 
234
 
 
235
    if need_interface:
 
236
        if f90mode:
 
237
            # f90 module already defines needed interface
 
238
            pass
 
239
        else:
 
240
            add('interface')
 
241
            add(rout['saved_interface'].lstrip())
 
242
            add('end interface')
 
243
 
 
244
    sargs = ', '.join([a for a in args if a not in extra_args])
 
245
 
 
246
    if not signature:
 
247
        add('call %s(%s)'%(fortranname,sargs))
 
248
    if f90mode:
 
249
        add('end subroutine f2pywrap_%s_%s'%(rout['modulename'],name))
 
250
    else:
 
251
        add('end')
 
252
    #print '**'*10
 
253
    #print ret[0]
 
254
    #print '**'*10
 
255
    return ret[0]
 
256
 
141
257
 
142
258
def assubr(rout):
143
 
    if not isfunction_wrap(rout): return rout,''
144
 
    fortranname = getfortranname(rout)
145
 
    name = rout['name']
146
 
    outmess('\t\tCreating wrapper for Fortran function "%s"("%s")...\n'%(name,fortranname))
147
 
    rout = copy.copy(rout)
148
 
    fname = name
149
 
    rname = fname
150
 
    if 'result' in rout:
151
 
        rname = rout['result']
152
 
        rout['vars'][fname]=rout['vars'][rname]
153
 
    fvar = rout['vars'][fname]
154
 
    if not isintent_out(fvar):
155
 
        if 'intent' not in fvar:
156
 
            fvar['intent']=[]
157
 
        fvar['intent'].append('out')
158
 
        flag=1
159
 
        for i in fvar['intent']:
160
 
            if i.startswith('out='):
161
 
                flag = 0
162
 
                break
163
 
        if flag:
164
 
            fvar['intent'].append('out=%s' % (rname))
 
259
    if isfunction_wrap(rout):
 
260
        fortranname = getfortranname(rout)
 
261
        name = rout['name']
 
262
        outmess('\t\tCreating wrapper for Fortran function "%s"("%s")...\n'%(name,fortranname))
 
263
        rout = copy.copy(rout)
 
264
        fname = name
 
265
        rname = fname
 
266
        if 'result' in rout:
 
267
            rname = rout['result']
 
268
            rout['vars'][fname]=rout['vars'][rname]
 
269
        fvar = rout['vars'][fname]
 
270
        if not isintent_out(fvar):
 
271
            if 'intent' not in fvar:
 
272
                fvar['intent']=[]
 
273
            fvar['intent'].append('out')
 
274
            flag=1
 
275
            for i in fvar['intent']:
 
276
                if i.startswith('out='):
 
277
                    flag = 0
 
278
                    break
 
279
            if flag:
 
280
                fvar['intent'].append('out=%s' % (rname))
 
281
        rout['args'][:] = [fname] + rout['args']
 
282
        return rout,createfuncwrapper(rout)
 
283
    if issubroutine_wrap(rout):
 
284
        fortranname = getfortranname(rout)
 
285
        name = rout['name']
 
286
        outmess('\t\tCreating wrapper for Fortran subroutine "%s"("%s")...\n'%(name,fortranname))
 
287
        rout = copy.copy(rout)
 
288
        return rout,createsubrwrapper(rout)
 
289
    return rout,''
165
290
 
166
 
    rout['args'] = [fname] + rout['args']
167
 
    return rout,createfuncwrapper(rout)