annotate DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/f2py/capi_maps.py @ 118:770eb830ec19 emscripten

Typo fix
author Chris Cannam
date Wed, 18 May 2016 16:14:08 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 #!/usr/bin/env python
Chris@87 2 """
Chris@87 3
Chris@87 4 Copyright 1999,2000 Pearu Peterson all rights reserved,
Chris@87 5 Pearu Peterson <pearu@ioc.ee>
Chris@87 6 Permission to use, modify, and distribute this software is given under the
Chris@87 7 terms of the NumPy License.
Chris@87 8
Chris@87 9 NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Chris@87 10 $Date: 2005/05/06 10:57:33 $
Chris@87 11 Pearu Peterson
Chris@87 12
Chris@87 13 """
Chris@87 14 from __future__ import division, absolute_import, print_function
Chris@87 15
Chris@87 16 __version__ = "$Revision: 1.60 $"[10:-1]
Chris@87 17
Chris@87 18 from . import __version__
Chris@87 19 f2py_version = __version__.version
Chris@87 20
Chris@87 21 import copy
Chris@87 22 import re
Chris@87 23 import os
Chris@87 24 import sys
Chris@87 25 from .auxfuncs import *
Chris@87 26 from .crackfortran import markoutercomma
Chris@87 27 from . import cb_rules
Chris@87 28
Chris@87 29 # Numarray and Numeric users should set this False
Chris@87 30 using_newcore = True
Chris@87 31
Chris@87 32 depargs=[]
Chris@87 33 lcb_map={}
Chris@87 34 lcb2_map={}
Chris@87 35 # forced casting: mainly caused by the fact that Python or Numeric
Chris@87 36 # C/APIs do not support the corresponding C types.
Chris@87 37 c2py_map={'double': 'float',
Chris@87 38 'float': 'float', # forced casting
Chris@87 39 'long_double': 'float', # forced casting
Chris@87 40 'char': 'int', # forced casting
Chris@87 41 'signed_char': 'int', # forced casting
Chris@87 42 'unsigned_char': 'int', # forced casting
Chris@87 43 'short': 'int', # forced casting
Chris@87 44 'unsigned_short': 'int', # forced casting
Chris@87 45 'int': 'int', # (forced casting)
Chris@87 46 'long': 'int',
Chris@87 47 'long_long': 'long',
Chris@87 48 'unsigned': 'int', # forced casting
Chris@87 49 'complex_float': 'complex', # forced casting
Chris@87 50 'complex_double': 'complex',
Chris@87 51 'complex_long_double': 'complex', # forced casting
Chris@87 52 'string': 'string',
Chris@87 53 }
Chris@87 54 c2capi_map={'double':'NPY_DOUBLE',
Chris@87 55 'float':'NPY_FLOAT',
Chris@87 56 'long_double':'NPY_DOUBLE', # forced casting
Chris@87 57 'char':'NPY_CHAR',
Chris@87 58 'unsigned_char':'NPY_UBYTE',
Chris@87 59 'signed_char':'NPY_BYTE',
Chris@87 60 'short':'NPY_SHORT',
Chris@87 61 'unsigned_short':'NPY_USHORT',
Chris@87 62 'int':'NPY_INT',
Chris@87 63 'unsigned':'NPY_UINT',
Chris@87 64 'long':'NPY_LONG',
Chris@87 65 'long_long':'NPY_LONG', # forced casting
Chris@87 66 'complex_float':'NPY_CFLOAT',
Chris@87 67 'complex_double':'NPY_CDOUBLE',
Chris@87 68 'complex_long_double':'NPY_CDOUBLE', # forced casting
Chris@87 69 'string':'NPY_CHAR'}
Chris@87 70
Chris@87 71 #These new maps aren't used anyhere yet, but should be by default
Chris@87 72 # unless building numeric or numarray extensions.
Chris@87 73 if using_newcore:
Chris@87 74 c2capi_map={'double': 'NPY_DOUBLE',
Chris@87 75 'float': 'NPY_FLOAT',
Chris@87 76 'long_double': 'NPY_LONGDOUBLE',
Chris@87 77 'char': 'NPY_BYTE',
Chris@87 78 'unsigned_char': 'NPY_UBYTE',
Chris@87 79 'signed_char': 'NPY_BYTE',
Chris@87 80 'short': 'NPY_SHORT',
Chris@87 81 'unsigned_short': 'NPY_USHORT',
Chris@87 82 'int': 'NPY_INT',
Chris@87 83 'unsigned': 'NPY_UINT',
Chris@87 84 'long': 'NPY_LONG',
Chris@87 85 'unsigned_long': 'NPY_ULONG',
Chris@87 86 'long_long': 'NPY_LONGLONG',
Chris@87 87 'unsigned_long_long': 'NPY_ULONGLONG',
Chris@87 88 'complex_float': 'NPY_CFLOAT',
Chris@87 89 'complex_double': 'NPY_CDOUBLE',
Chris@87 90 'complex_long_double': 'NPY_CDOUBLE',
Chris@87 91 'string': 'NPY_CHAR', # f2py 2e is not ready for NPY_STRING (must set itemisize etc)
Chris@87 92 #'string':'NPY_STRING'
Chris@87 93
Chris@87 94 }
Chris@87 95 c2pycode_map={'double':'d',
Chris@87 96 'float':'f',
Chris@87 97 'long_double':'d', # forced casting
Chris@87 98 'char':'1',
Chris@87 99 'signed_char':'1',
Chris@87 100 'unsigned_char':'b',
Chris@87 101 'short':'s',
Chris@87 102 'unsigned_short':'w',
Chris@87 103 'int':'i',
Chris@87 104 'unsigned':'u',
Chris@87 105 'long':'l',
Chris@87 106 'long_long':'L',
Chris@87 107 'complex_float':'F',
Chris@87 108 'complex_double':'D',
Chris@87 109 'complex_long_double':'D', # forced casting
Chris@87 110 'string':'c'
Chris@87 111 }
Chris@87 112 if using_newcore:
Chris@87 113 c2pycode_map={'double':'d',
Chris@87 114 'float':'f',
Chris@87 115 'long_double':'g',
Chris@87 116 'char':'b',
Chris@87 117 'unsigned_char':'B',
Chris@87 118 'signed_char':'b',
Chris@87 119 'short':'h',
Chris@87 120 'unsigned_short':'H',
Chris@87 121 'int':'i',
Chris@87 122 'unsigned':'I',
Chris@87 123 'long':'l',
Chris@87 124 'unsigned_long':'L',
Chris@87 125 'long_long':'q',
Chris@87 126 'unsigned_long_long':'Q',
Chris@87 127 'complex_float':'F',
Chris@87 128 'complex_double':'D',
Chris@87 129 'complex_long_double':'G',
Chris@87 130 'string':'S'}
Chris@87 131 c2buildvalue_map={'double':'d',
Chris@87 132 'float':'f',
Chris@87 133 'char':'b',
Chris@87 134 'signed_char':'b',
Chris@87 135 'short':'h',
Chris@87 136 'int':'i',
Chris@87 137 'long':'l',
Chris@87 138 'long_long':'L',
Chris@87 139 'complex_float':'N',
Chris@87 140 'complex_double':'N',
Chris@87 141 'complex_long_double':'N',
Chris@87 142 'string':'z'}
Chris@87 143
Chris@87 144 if sys.version_info[0] >= 3:
Chris@87 145 # Bytes, not Unicode strings
Chris@87 146 c2buildvalue_map['string'] = 'y'
Chris@87 147
Chris@87 148 if using_newcore:
Chris@87 149 #c2buildvalue_map=???
Chris@87 150 pass
Chris@87 151
Chris@87 152 f2cmap_all={'real':{'':'float','4':'float','8':'double','12':'long_double','16':'long_double'},
Chris@87 153 'integer':{'':'int','1':'signed_char','2':'short','4':'int','8':'long_long',
Chris@87 154 '-1':'unsigned_char','-2':'unsigned_short','-4':'unsigned',
Chris@87 155 '-8':'unsigned_long_long'},
Chris@87 156 'complex':{'':'complex_float','8':'complex_float',
Chris@87 157 '16':'complex_double','24':'complex_long_double',
Chris@87 158 '32':'complex_long_double'},
Chris@87 159 'complexkind':{'':'complex_float','4':'complex_float',
Chris@87 160 '8':'complex_double','12':'complex_long_double',
Chris@87 161 '16':'complex_long_double'},
Chris@87 162 'logical':{'':'int','1':'char','2':'short','4':'int','8':'long_long'},
Chris@87 163 'double complex':{'':'complex_double'},
Chris@87 164 'double precision':{'':'double'},
Chris@87 165 'byte':{'':'char'},
Chris@87 166 'character':{'':'string'}
Chris@87 167 }
Chris@87 168
Chris@87 169 if os.path.isfile('.f2py_f2cmap'):
Chris@87 170 # User defined additions to f2cmap_all.
Chris@87 171 # .f2py_f2cmap must contain a dictionary of dictionaries, only.
Chris@87 172 # For example, {'real':{'low':'float'}} means that Fortran 'real(low)' is
Chris@87 173 # interpreted as C 'float'.
Chris@87 174 # This feature is useful for F90/95 users if they use PARAMETERSs
Chris@87 175 # in type specifications.
Chris@87 176 try:
Chris@87 177 outmess('Reading .f2py_f2cmap ...\n')
Chris@87 178 f = open('.f2py_f2cmap', 'r')
Chris@87 179 d = eval(f.read(), {}, {})
Chris@87 180 f.close()
Chris@87 181 for k, d1 in d.items():
Chris@87 182 for k1 in d1.keys():
Chris@87 183 d1[k1.lower()] = d1[k1]
Chris@87 184 d[k.lower()] = d[k]
Chris@87 185 for k in d.keys():
Chris@87 186 if k not in f2cmap_all:
Chris@87 187 f2cmap_all[k]={}
Chris@87 188 for k1 in d[k].keys():
Chris@87 189 if d[k][k1] in c2py_map:
Chris@87 190 if k1 in f2cmap_all[k]:
Chris@87 191 outmess("\tWarning: redefinition of {'%s':{'%s':'%s'->'%s'}}\n"%(k, k1, f2cmap_all[k][k1], d[k][k1]))
Chris@87 192 f2cmap_all[k][k1] = d[k][k1]
Chris@87 193 outmess('\tMapping "%s(kind=%s)" to "%s"\n' % (k, k1, d[k][k1]))
Chris@87 194 else:
Chris@87 195 errmess("\tIgnoring map {'%s':{'%s':'%s'}}: '%s' must be in %s\n"%(k, k1, d[k][k1], d[k][k1], list(c2py_map.keys())))
Chris@87 196 outmess('Succesfully applied user defined changes from .f2py_f2cmap\n')
Chris@87 197 except Exception as msg:
Chris@87 198 errmess('Failed to apply user defined changes from .f2py_f2cmap: %s. Skipping.\n' % (msg))
Chris@87 199 cformat_map={'double': '%g',
Chris@87 200 'float': '%g',
Chris@87 201 'long_double': '%Lg',
Chris@87 202 'char': '%d',
Chris@87 203 'signed_char': '%d',
Chris@87 204 'unsigned_char': '%hhu',
Chris@87 205 'short': '%hd',
Chris@87 206 'unsigned_short': '%hu',
Chris@87 207 'int': '%d',
Chris@87 208 'unsigned': '%u',
Chris@87 209 'long': '%ld',
Chris@87 210 'unsigned_long': '%lu',
Chris@87 211 'long_long': '%ld',
Chris@87 212 'complex_float': '(%g,%g)',
Chris@87 213 'complex_double': '(%g,%g)',
Chris@87 214 'complex_long_double': '(%Lg,%Lg)',
Chris@87 215 'string': '%s',
Chris@87 216 }
Chris@87 217
Chris@87 218 ############### Auxiliary functions
Chris@87 219 def getctype(var):
Chris@87 220 """
Chris@87 221 Determines C type
Chris@87 222 """
Chris@87 223 ctype='void'
Chris@87 224 if isfunction(var):
Chris@87 225 if 'result' in var:
Chris@87 226 a=var['result']
Chris@87 227 else:
Chris@87 228 a=var['name']
Chris@87 229 if a in var['vars']:
Chris@87 230 return getctype(var['vars'][a])
Chris@87 231 else:
Chris@87 232 errmess('getctype: function %s has no return value?!\n'%a)
Chris@87 233 elif issubroutine(var):
Chris@87 234 return ctype
Chris@87 235 elif 'typespec' in var and var['typespec'].lower() in f2cmap_all:
Chris@87 236 typespec = var['typespec'].lower()
Chris@87 237 f2cmap=f2cmap_all[typespec]
Chris@87 238 ctype=f2cmap[''] # default type
Chris@87 239 if 'kindselector' in var:
Chris@87 240 if '*' in var['kindselector']:
Chris@87 241 try:
Chris@87 242 ctype=f2cmap[var['kindselector']['*']]
Chris@87 243 except KeyError:
Chris@87 244 errmess('getctype: "%s %s %s" not supported.\n'%(var['typespec'], '*', var['kindselector']['*']))
Chris@87 245 elif 'kind' in var['kindselector']:
Chris@87 246 if typespec+'kind' in f2cmap_all:
Chris@87 247 f2cmap=f2cmap_all[typespec+'kind']
Chris@87 248 try:
Chris@87 249 ctype=f2cmap[var['kindselector']['kind']]
Chris@87 250 except KeyError:
Chris@87 251 if typespec in f2cmap_all:
Chris@87 252 f2cmap=f2cmap_all[typespec]
Chris@87 253 try:
Chris@87 254 ctype=f2cmap[str(var['kindselector']['kind'])]
Chris@87 255 except KeyError:
Chris@87 256 errmess('getctype: "%s(kind=%s)" is mapped to C "%s" (to override define dict(%s = dict(%s="<C typespec>")) in %s/.f2py_f2cmap file).\n'\
Chris@87 257 %(typespec, var['kindselector']['kind'], ctype,
Chris@87 258 typespec, var['kindselector']['kind'], os.getcwd()))
Chris@87 259
Chris@87 260 else:
Chris@87 261 if not isexternal(var):
Chris@87 262 errmess('getctype: No C-type found in "%s", assuming void.\n'%var)
Chris@87 263 return ctype
Chris@87 264
Chris@87 265 def getstrlength(var):
Chris@87 266 if isstringfunction(var):
Chris@87 267 if 'result' in var:
Chris@87 268 a=var['result']
Chris@87 269 else:
Chris@87 270 a=var['name']
Chris@87 271 if a in var['vars']:
Chris@87 272 return getstrlength(var['vars'][a])
Chris@87 273 else:
Chris@87 274 errmess('getstrlength: function %s has no return value?!\n'%a)
Chris@87 275 if not isstring(var):
Chris@87 276 errmess('getstrlength: expected a signature of a string but got: %s\n'%(repr(var)))
Chris@87 277 len='1'
Chris@87 278 if 'charselector' in var:
Chris@87 279 a=var['charselector']
Chris@87 280 if '*' in a:
Chris@87 281 len=a['*']
Chris@87 282 elif 'len' in a:
Chris@87 283 len=a['len']
Chris@87 284 if re.match(r'\(\s*([*]|[:])\s*\)', len) or re.match(r'([*]|[:])', len):
Chris@87 285 #if len in ['(*)','*','(:)',':']:
Chris@87 286 if isintent_hide(var):
Chris@87 287 errmess('getstrlength:intent(hide): expected a string with defined length but got: %s\n'%(repr(var)))
Chris@87 288 len='-1'
Chris@87 289 return len
Chris@87 290
Chris@87 291 def getarrdims(a,var,verbose=0):
Chris@87 292 global depargs
Chris@87 293 ret={}
Chris@87 294 if isstring(var) and not isarray(var):
Chris@87 295 ret['dims']=getstrlength(var)
Chris@87 296 ret['size']=ret['dims']
Chris@87 297 ret['rank']='1'
Chris@87 298 elif isscalar(var):
Chris@87 299 ret['size']='1'
Chris@87 300 ret['rank']='0'
Chris@87 301 ret['dims']=''
Chris@87 302 elif isarray(var):
Chris@87 303 # if not isintent_c(var):
Chris@87 304 # var['dimension'].reverse()
Chris@87 305 dim=copy.copy(var['dimension'])
Chris@87 306 ret['size']='*'.join(dim)
Chris@87 307 try: ret['size']=repr(eval(ret['size']))
Chris@87 308 except: pass
Chris@87 309 ret['dims']=','.join(dim)
Chris@87 310 ret['rank']=repr(len(dim))
Chris@87 311 ret['rank*[-1]']=repr(len(dim)*[-1])[1:-1]
Chris@87 312 for i in range(len(dim)): # solve dim for dependecies
Chris@87 313 v=[]
Chris@87 314 if dim[i] in depargs: v=[dim[i]]
Chris@87 315 else:
Chris@87 316 for va in depargs:
Chris@87 317 if re.match(r'.*?\b%s\b.*'%va, dim[i]):
Chris@87 318 v.append(va)
Chris@87 319 for va in v:
Chris@87 320 if depargs.index(va)>depargs.index(a):
Chris@87 321 dim[i]='*'
Chris@87 322 break
Chris@87 323 ret['setdims'], i='', -1
Chris@87 324 for d in dim:
Chris@87 325 i=i+1
Chris@87 326 if d not in ['*', ':', '(*)', '(:)']:
Chris@87 327 ret['setdims']='%s#varname#_Dims[%d]=%s,'%(ret['setdims'], i, d)
Chris@87 328 if ret['setdims']: ret['setdims']=ret['setdims'][:-1]
Chris@87 329 ret['cbsetdims'], i='', -1
Chris@87 330 for d in var['dimension']:
Chris@87 331 i=i+1
Chris@87 332 if d not in ['*', ':', '(*)', '(:)']:
Chris@87 333 ret['cbsetdims']='%s#varname#_Dims[%d]=%s,'%(ret['cbsetdims'], i, d)
Chris@87 334 elif isintent_in(var):
Chris@87 335 outmess('getarrdims:warning: assumed shape array, using 0 instead of %r\n' \
Chris@87 336 % (d))
Chris@87 337 ret['cbsetdims']='%s#varname#_Dims[%d]=%s,'%(ret['cbsetdims'], i, 0)
Chris@87 338 elif verbose :
Chris@87 339 errmess('getarrdims: If in call-back function: array argument %s must have bounded dimensions: got %s\n'%(repr(a), repr(d)))
Chris@87 340 if ret['cbsetdims']: ret['cbsetdims']=ret['cbsetdims'][:-1]
Chris@87 341 # if not isintent_c(var):
Chris@87 342 # var['dimension'].reverse()
Chris@87 343 return ret
Chris@87 344
Chris@87 345 def getpydocsign(a, var):
Chris@87 346 global lcb_map
Chris@87 347 if isfunction(var):
Chris@87 348 if 'result' in var:
Chris@87 349 af=var['result']
Chris@87 350 else:
Chris@87 351 af=var['name']
Chris@87 352 if af in var['vars']:
Chris@87 353 return getpydocsign(af, var['vars'][af])
Chris@87 354 else:
Chris@87 355 errmess('getctype: function %s has no return value?!\n'%af)
Chris@87 356 return '', ''
Chris@87 357 sig, sigout=a, a
Chris@87 358 opt=''
Chris@87 359 if isintent_in(var): opt='input'
Chris@87 360 elif isintent_inout(var): opt='in/output'
Chris@87 361 out_a = a
Chris@87 362 if isintent_out(var):
Chris@87 363 for k in var['intent']:
Chris@87 364 if k[:4]=='out=':
Chris@87 365 out_a = k[4:]
Chris@87 366 break
Chris@87 367 init=''
Chris@87 368 ctype=getctype(var)
Chris@87 369
Chris@87 370 if hasinitvalue(var):
Chris@87 371 init, showinit=getinit(a, var)
Chris@87 372 init = ', optional\\n Default: %s' % showinit
Chris@87 373 if isscalar(var):
Chris@87 374 if isintent_inout(var):
Chris@87 375 sig='%s : %s rank-0 array(%s,\'%s\')%s'%(a, opt, c2py_map[ctype],
Chris@87 376 c2pycode_map[ctype], init)
Chris@87 377 else:
Chris@87 378 sig='%s : %s %s%s'%(a, opt, c2py_map[ctype], init)
Chris@87 379 sigout='%s : %s'%(out_a, c2py_map[ctype])
Chris@87 380 elif isstring(var):
Chris@87 381 if isintent_inout(var):
Chris@87 382 sig='%s : %s rank-0 array(string(len=%s),\'c\')%s'%(a, opt, getstrlength(var), init)
Chris@87 383 else:
Chris@87 384 sig='%s : %s string(len=%s)%s'%(a, opt, getstrlength(var), init)
Chris@87 385 sigout='%s : string(len=%s)'%(out_a, getstrlength(var))
Chris@87 386 elif isarray(var):
Chris@87 387 dim=var['dimension']
Chris@87 388 rank=repr(len(dim))
Chris@87 389 sig='%s : %s rank-%s array(\'%s\') with bounds (%s)%s'%(a, opt, rank,
Chris@87 390 c2pycode_map[ctype],
Chris@87 391 ','.join(dim), init)
Chris@87 392 if a==out_a:
Chris@87 393 sigout='%s : rank-%s array(\'%s\') with bounds (%s)'\
Chris@87 394 %(a, rank, c2pycode_map[ctype], ','.join(dim))
Chris@87 395 else:
Chris@87 396 sigout='%s : rank-%s array(\'%s\') with bounds (%s) and %s storage'\
Chris@87 397 %(out_a, rank, c2pycode_map[ctype], ','.join(dim), a)
Chris@87 398 elif isexternal(var):
Chris@87 399 ua=''
Chris@87 400 if a in lcb_map and lcb_map[a] in lcb2_map and 'argname' in lcb2_map[lcb_map[a]]:
Chris@87 401 ua=lcb2_map[lcb_map[a]]['argname']
Chris@87 402 if not ua==a: ua=' => %s'%ua
Chris@87 403 else: ua=''
Chris@87 404 sig='%s : call-back function%s'%(a, ua)
Chris@87 405 sigout=sig
Chris@87 406 else:
Chris@87 407 errmess('getpydocsign: Could not resolve docsignature for "%s".\\n'%a)
Chris@87 408 return sig, sigout
Chris@87 409
Chris@87 410 def getarrdocsign(a, var):
Chris@87 411 ctype=getctype(var)
Chris@87 412 if isstring(var) and (not isarray(var)):
Chris@87 413 sig='%s : rank-0 array(string(len=%s),\'c\')'%(a, getstrlength(var))
Chris@87 414 elif isscalar(var):
Chris@87 415 sig='%s : rank-0 array(%s,\'%s\')'%(a, c2py_map[ctype],
Chris@87 416 c2pycode_map[ctype],)
Chris@87 417 elif isarray(var):
Chris@87 418 dim=var['dimension']
Chris@87 419 rank=repr(len(dim))
Chris@87 420 sig='%s : rank-%s array(\'%s\') with bounds (%s)'%(a, rank,
Chris@87 421 c2pycode_map[ctype],
Chris@87 422 ','.join(dim))
Chris@87 423 return sig
Chris@87 424
Chris@87 425 def getinit(a, var):
Chris@87 426 if isstring(var): init, showinit='""', "''"
Chris@87 427 else: init, showinit='', ''
Chris@87 428 if hasinitvalue(var):
Chris@87 429 init=var['=']
Chris@87 430 showinit=init
Chris@87 431 if iscomplex(var) or iscomplexarray(var):
Chris@87 432 ret={}
Chris@87 433
Chris@87 434 try:
Chris@87 435 v = var["="]
Chris@87 436 if ',' in v:
Chris@87 437 ret['init.r'], ret['init.i']=markoutercomma(v[1:-1]).split('@,@')
Chris@87 438 else:
Chris@87 439 v = eval(v, {}, {})
Chris@87 440 ret['init.r'], ret['init.i']=str(v.real), str(v.imag)
Chris@87 441 except:
Chris@87 442 raise ValueError('getinit: expected complex number `(r,i)\' but got `%s\' as initial value of %r.' % (init, a))
Chris@87 443 if isarray(var):
Chris@87 444 init='(capi_c.r=%s,capi_c.i=%s,capi_c)'%(ret['init.r'], ret['init.i'])
Chris@87 445 elif isstring(var):
Chris@87 446 if not init: init, showinit='""', "''"
Chris@87 447 if init[0]=="'":
Chris@87 448 init='"%s"'%(init[1:-1].replace('"', '\\"'))
Chris@87 449 if init[0]=='"': showinit="'%s'"%(init[1:-1])
Chris@87 450 return init, showinit
Chris@87 451
Chris@87 452 def sign2map(a, var):
Chris@87 453 """
Chris@87 454 varname,ctype,atype
Chris@87 455 init,init.r,init.i,pytype
Chris@87 456 vardebuginfo,vardebugshowvalue,varshowvalue
Chris@87 457 varrfromat
Chris@87 458 intent
Chris@87 459 """
Chris@87 460 global lcb_map, cb_map
Chris@87 461 out_a = a
Chris@87 462 if isintent_out(var):
Chris@87 463 for k in var['intent']:
Chris@87 464 if k[:4]=='out=':
Chris@87 465 out_a = k[4:]
Chris@87 466 break
Chris@87 467 ret={'varname':a,'outvarname':out_a}
Chris@87 468 ret['ctype']=getctype(var)
Chris@87 469 intent_flags = []
Chris@87 470 for f, s in isintent_dict.items():
Chris@87 471 if f(var): intent_flags.append('F2PY_%s'%s)
Chris@87 472 if intent_flags:
Chris@87 473 #XXX: Evaluate intent_flags here.
Chris@87 474 ret['intent'] = '|'.join(intent_flags)
Chris@87 475 else:
Chris@87 476 ret['intent'] = 'F2PY_INTENT_IN'
Chris@87 477 if isarray(var): ret['varrformat']='N'
Chris@87 478 elif ret['ctype'] in c2buildvalue_map:
Chris@87 479 ret['varrformat']=c2buildvalue_map[ret['ctype']]
Chris@87 480 else: ret['varrformat']='O'
Chris@87 481 ret['init'], ret['showinit']=getinit(a, var)
Chris@87 482 if hasinitvalue(var) and iscomplex(var) and not isarray(var):
Chris@87 483 ret['init.r'], ret['init.i'] = markoutercomma(ret['init'][1:-1]).split('@,@')
Chris@87 484 if isexternal(var):
Chris@87 485 ret['cbnamekey']=a
Chris@87 486 if a in lcb_map:
Chris@87 487 ret['cbname']=lcb_map[a]
Chris@87 488 ret['maxnofargs']=lcb2_map[lcb_map[a]]['maxnofargs']
Chris@87 489 ret['nofoptargs']=lcb2_map[lcb_map[a]]['nofoptargs']
Chris@87 490 ret['cbdocstr']=lcb2_map[lcb_map[a]]['docstr']
Chris@87 491 ret['cblatexdocstr']=lcb2_map[lcb_map[a]]['latexdocstr']
Chris@87 492 else:
Chris@87 493 ret['cbname']=a
Chris@87 494 errmess('sign2map: Confused: external %s is not in lcb_map%s.\n'%(a, list(lcb_map.keys())))
Chris@87 495 if isstring(var):
Chris@87 496 ret['length']=getstrlength(var)
Chris@87 497 if isarray(var):
Chris@87 498 ret=dictappend(ret, getarrdims(a, var))
Chris@87 499 dim=copy.copy(var['dimension'])
Chris@87 500 if ret['ctype'] in c2capi_map:
Chris@87 501 ret['atype']=c2capi_map[ret['ctype']]
Chris@87 502 # Debug info
Chris@87 503 if debugcapi(var):
Chris@87 504 il=[isintent_in, 'input', isintent_out, 'output',
Chris@87 505 isintent_inout, 'inoutput', isrequired, 'required',
Chris@87 506 isoptional, 'optional', isintent_hide, 'hidden',
Chris@87 507 iscomplex, 'complex scalar',
Chris@87 508 l_and(isscalar, l_not(iscomplex)), 'scalar',
Chris@87 509 isstring, 'string', isarray, 'array',
Chris@87 510 iscomplexarray, 'complex array', isstringarray, 'string array',
Chris@87 511 iscomplexfunction, 'complex function',
Chris@87 512 l_and(isfunction, l_not(iscomplexfunction)), 'function',
Chris@87 513 isexternal, 'callback',
Chris@87 514 isintent_callback, 'callback',
Chris@87 515 isintent_aux, 'auxiliary',
Chris@87 516 #ismutable,'mutable',l_not(ismutable),'immutable',
Chris@87 517 ]
Chris@87 518 rl=[]
Chris@87 519 for i in range(0, len(il), 2):
Chris@87 520 if il[i](var): rl.append(il[i+1])
Chris@87 521 if isstring(var):
Chris@87 522 rl.append('slen(%s)=%s'%(a, ret['length']))
Chris@87 523 if isarray(var):
Chris@87 524 # if not isintent_c(var):
Chris@87 525 # var['dimension'].reverse()
Chris@87 526 ddim=','.join(map(lambda x, y:'%s|%s'%(x, y), var['dimension'], dim))
Chris@87 527 rl.append('dims(%s)'%ddim)
Chris@87 528 # if not isintent_c(var):
Chris@87 529 # var['dimension'].reverse()
Chris@87 530 if isexternal(var):
Chris@87 531 ret['vardebuginfo']='debug-capi:%s=>%s:%s'%(a, ret['cbname'], ','.join(rl))
Chris@87 532 else:
Chris@87 533 ret['vardebuginfo']='debug-capi:%s %s=%s:%s'%(ret['ctype'], a, ret['showinit'], ','.join(rl))
Chris@87 534 if isscalar(var):
Chris@87 535 if ret['ctype'] in cformat_map:
Chris@87 536 ret['vardebugshowvalue']='debug-capi:%s=%s'%(a, cformat_map[ret['ctype']])
Chris@87 537 if isstring(var):
Chris@87 538 ret['vardebugshowvalue']='debug-capi:slen(%s)=%%d %s=\\"%%s\\"'%(a, a)
Chris@87 539 if isexternal(var):
Chris@87 540 ret['vardebugshowvalue']='debug-capi:%s=%%p'%(a)
Chris@87 541 if ret['ctype'] in cformat_map:
Chris@87 542 ret['varshowvalue']='#name#:%s=%s'%(a, cformat_map[ret['ctype']])
Chris@87 543 ret['showvalueformat']='%s'%(cformat_map[ret['ctype']])
Chris@87 544 if isstring(var):
Chris@87 545 ret['varshowvalue']='#name#:slen(%s)=%%d %s=\\"%%s\\"'%(a, a)
Chris@87 546 ret['pydocsign'], ret['pydocsignout']=getpydocsign(a, var)
Chris@87 547 if hasnote(var):
Chris@87 548 ret['note']=var['note']
Chris@87 549 return ret
Chris@87 550
Chris@87 551 def routsign2map(rout):
Chris@87 552 """
Chris@87 553 name,NAME,begintitle,endtitle
Chris@87 554 rname,ctype,rformat
Chris@87 555 routdebugshowvalue
Chris@87 556 """
Chris@87 557 global lcb_map
Chris@87 558 name = rout['name']
Chris@87 559 fname = getfortranname(rout)
Chris@87 560 ret={'name': name,
Chris@87 561 'texname': name.replace('_', '\\_'),
Chris@87 562 'name_lower': name.lower(),
Chris@87 563 'NAME': name.upper(),
Chris@87 564 'begintitle': gentitle(name),
Chris@87 565 'endtitle': gentitle('end of %s'%name),
Chris@87 566 'fortranname': fname,
Chris@87 567 'FORTRANNAME': fname.upper(),
Chris@87 568 'callstatement': getcallstatement(rout) or '',
Chris@87 569 'usercode': getusercode(rout) or '',
Chris@87 570 'usercode1': getusercode1(rout) or '',
Chris@87 571 }
Chris@87 572 if '_' in fname:
Chris@87 573 ret['F_FUNC'] = 'F_FUNC_US'
Chris@87 574 else:
Chris@87 575 ret['F_FUNC'] = 'F_FUNC'
Chris@87 576 if '_' in name:
Chris@87 577 ret['F_WRAPPEDFUNC'] = 'F_WRAPPEDFUNC_US'
Chris@87 578 else:
Chris@87 579 ret['F_WRAPPEDFUNC'] = 'F_WRAPPEDFUNC'
Chris@87 580 lcb_map={}
Chris@87 581 if 'use' in rout:
Chris@87 582 for u in rout['use'].keys():
Chris@87 583 if u in cb_rules.cb_map:
Chris@87 584 for un in cb_rules.cb_map[u]:
Chris@87 585 ln=un[0]
Chris@87 586 if 'map' in rout['use'][u]:
Chris@87 587 for k in rout['use'][u]['map'].keys():
Chris@87 588 if rout['use'][u]['map'][k]==un[0]: ln=k;break
Chris@87 589 lcb_map[ln]=un[1]
Chris@87 590 #else:
Chris@87 591 # errmess('routsign2map: cb_map does not contain module "%s" used in "use" statement.\n'%(u))
Chris@87 592 elif 'externals' in rout and rout['externals']:
Chris@87 593 errmess('routsign2map: Confused: function %s has externals %s but no "use" statement.\n'%(ret['name'], repr(rout['externals'])))
Chris@87 594 ret['callprotoargument'] = getcallprotoargument(rout, lcb_map) or ''
Chris@87 595 if isfunction(rout):
Chris@87 596 if 'result' in rout:
Chris@87 597 a=rout['result']
Chris@87 598 else:
Chris@87 599 a=rout['name']
Chris@87 600 ret['rname']=a
Chris@87 601 ret['pydocsign'], ret['pydocsignout']=getpydocsign(a, rout)
Chris@87 602 ret['ctype']=getctype(rout['vars'][a])
Chris@87 603 if hasresultnote(rout):
Chris@87 604 ret['resultnote']=rout['vars'][a]['note']
Chris@87 605 rout['vars'][a]['note']=['See elsewhere.']
Chris@87 606 if ret['ctype'] in c2buildvalue_map:
Chris@87 607 ret['rformat']=c2buildvalue_map[ret['ctype']]
Chris@87 608 else:
Chris@87 609 ret['rformat']='O'
Chris@87 610 errmess('routsign2map: no c2buildvalue key for type %s\n'%(repr(ret['ctype'])))
Chris@87 611 if debugcapi(rout):
Chris@87 612 if ret['ctype'] in cformat_map:
Chris@87 613 ret['routdebugshowvalue']='debug-capi:%s=%s'%(a, cformat_map[ret['ctype']])
Chris@87 614 if isstringfunction(rout):
Chris@87 615 ret['routdebugshowvalue']='debug-capi:slen(%s)=%%d %s=\\"%%s\\"'%(a, a)
Chris@87 616 if isstringfunction(rout):
Chris@87 617 ret['rlength']=getstrlength(rout['vars'][a])
Chris@87 618 if ret['rlength']=='-1':
Chris@87 619 errmess('routsign2map: expected explicit specification of the length of the string returned by the fortran function %s; taking 10.\n'%(repr(rout['name'])))
Chris@87 620 ret['rlength']='10'
Chris@87 621 if hasnote(rout):
Chris@87 622 ret['note']=rout['note']
Chris@87 623 rout['note']=['See elsewhere.']
Chris@87 624 return ret
Chris@87 625
Chris@87 626 def modsign2map(m):
Chris@87 627 """
Chris@87 628 modulename
Chris@87 629 """
Chris@87 630 if ismodule(m):
Chris@87 631 ret={'f90modulename':m['name'],
Chris@87 632 'F90MODULENAME':m['name'].upper(),
Chris@87 633 'texf90modulename':m['name'].replace('_', '\\_')}
Chris@87 634 else:
Chris@87 635 ret={'modulename':m['name'],
Chris@87 636 'MODULENAME':m['name'].upper(),
Chris@87 637 'texmodulename':m['name'].replace('_', '\\_')}
Chris@87 638 ret['restdoc'] = getrestdoc(m) or []
Chris@87 639 if hasnote(m):
Chris@87 640 ret['note']=m['note']
Chris@87 641 #m['note']=['See elsewhere.']
Chris@87 642 ret['usercode'] = getusercode(m) or ''
Chris@87 643 ret['usercode1'] = getusercode1(m) or ''
Chris@87 644 if m['body']:
Chris@87 645 ret['interface_usercode'] = getusercode(m['body'][0]) or ''
Chris@87 646 else:
Chris@87 647 ret['interface_usercode'] = ''
Chris@87 648 ret['pymethoddef'] = getpymethoddef(m) or ''
Chris@87 649 if 'coutput' in m:
Chris@87 650 ret['coutput'] = m['coutput']
Chris@87 651 if 'f2py_wrapper_output' in m:
Chris@87 652 ret['f2py_wrapper_output'] = m['f2py_wrapper_output']
Chris@87 653 return ret
Chris@87 654
Chris@87 655 def cb_sign2map(a,var,index=None):
Chris@87 656 ret={'varname':a}
Chris@87 657 if index is None or 1: # disable 7712 patch
Chris@87 658 ret['varname_i'] = ret['varname']
Chris@87 659 else:
Chris@87 660 ret['varname_i'] = ret['varname'] + '_' + str(index)
Chris@87 661 ret['ctype']=getctype(var)
Chris@87 662 if ret['ctype'] in c2capi_map:
Chris@87 663 ret['atype']=c2capi_map[ret['ctype']]
Chris@87 664 if ret['ctype'] in cformat_map:
Chris@87 665 ret['showvalueformat']='%s'%(cformat_map[ret['ctype']])
Chris@87 666 if isarray(var):
Chris@87 667 ret=dictappend(ret, getarrdims(a, var))
Chris@87 668 ret['pydocsign'], ret['pydocsignout']=getpydocsign(a, var)
Chris@87 669 if hasnote(var):
Chris@87 670 ret['note']=var['note']
Chris@87 671 var['note']=['See elsewhere.']
Chris@87 672 return ret
Chris@87 673
Chris@87 674 def cb_routsign2map(rout, um):
Chris@87 675 """
Chris@87 676 name,begintitle,endtitle,argname
Chris@87 677 ctype,rctype,maxnofargs,nofoptargs,returncptr
Chris@87 678 """
Chris@87 679 ret={'name':'cb_%s_in_%s'%(rout['name'], um),
Chris@87 680 'returncptr':''}
Chris@87 681 if isintent_callback(rout):
Chris@87 682 if '_' in rout['name']:
Chris@87 683 F_FUNC='F_FUNC_US'
Chris@87 684 else:
Chris@87 685 F_FUNC='F_FUNC'
Chris@87 686 ret['callbackname'] = '%s(%s,%s)' \
Chris@87 687 % (F_FUNC,
Chris@87 688 rout['name'].lower(),
Chris@87 689 rout['name'].upper(),
Chris@87 690 )
Chris@87 691 ret['static'] = 'extern'
Chris@87 692 else:
Chris@87 693 ret['callbackname'] = ret['name']
Chris@87 694 ret['static'] = 'static'
Chris@87 695 ret['argname']=rout['name']
Chris@87 696 ret['begintitle']=gentitle(ret['name'])
Chris@87 697 ret['endtitle']=gentitle('end of %s'%ret['name'])
Chris@87 698 ret['ctype']=getctype(rout)
Chris@87 699 ret['rctype']='void'
Chris@87 700 if ret['ctype']=='string': ret['rctype']='void'
Chris@87 701 else:
Chris@87 702 ret['rctype']=ret['ctype']
Chris@87 703 if ret['rctype']!='void':
Chris@87 704 if iscomplexfunction(rout):
Chris@87 705 ret['returncptr'] = """
Chris@87 706 #ifdef F2PY_CB_RETURNCOMPLEX
Chris@87 707 return_value=
Chris@87 708 #endif
Chris@87 709 """
Chris@87 710 else:
Chris@87 711 ret['returncptr'] = 'return_value='
Chris@87 712 if ret['ctype'] in cformat_map:
Chris@87 713 ret['showvalueformat']='%s'%(cformat_map[ret['ctype']])
Chris@87 714 if isstringfunction(rout):
Chris@87 715 ret['strlength']=getstrlength(rout)
Chris@87 716 if isfunction(rout):
Chris@87 717 if 'result' in rout:
Chris@87 718 a=rout['result']
Chris@87 719 else:
Chris@87 720 a=rout['name']
Chris@87 721 if hasnote(rout['vars'][a]):
Chris@87 722 ret['note']=rout['vars'][a]['note']
Chris@87 723 rout['vars'][a]['note']=['See elsewhere.']
Chris@87 724 ret['rname']=a
Chris@87 725 ret['pydocsign'], ret['pydocsignout']=getpydocsign(a, rout)
Chris@87 726 if iscomplexfunction(rout):
Chris@87 727 ret['rctype']="""
Chris@87 728 #ifdef F2PY_CB_RETURNCOMPLEX
Chris@87 729 #ctype#
Chris@87 730 #else
Chris@87 731 void
Chris@87 732 #endif
Chris@87 733 """
Chris@87 734 else:
Chris@87 735 if hasnote(rout):
Chris@87 736 ret['note']=rout['note']
Chris@87 737 rout['note']=['See elsewhere.']
Chris@87 738 nofargs=0
Chris@87 739 nofoptargs=0
Chris@87 740 if 'args' in rout and 'vars' in rout:
Chris@87 741 for a in rout['args']:
Chris@87 742 var=rout['vars'][a]
Chris@87 743 if l_or(isintent_in, isintent_inout)(var):
Chris@87 744 nofargs=nofargs+1
Chris@87 745 if isoptional(var):
Chris@87 746 nofoptargs=nofoptargs+1
Chris@87 747 ret['maxnofargs']=repr(nofargs)
Chris@87 748 ret['nofoptargs']=repr(nofoptargs)
Chris@87 749 if hasnote(rout) and isfunction(rout) and 'result' in rout:
Chris@87 750 ret['routnote']=rout['note']
Chris@87 751 rout['note']=['See elsewhere.']
Chris@87 752 return ret
Chris@87 753
Chris@87 754 def common_sign2map(a, var): # obsolute
Chris@87 755 ret={'varname':a}
Chris@87 756 ret['ctype']=getctype(var)
Chris@87 757 if isstringarray(var):
Chris@87 758 ret['ctype']='char'
Chris@87 759 if ret['ctype'] in c2capi_map:
Chris@87 760 ret['atype']=c2capi_map[ret['ctype']]
Chris@87 761 if ret['ctype'] in cformat_map:
Chris@87 762 ret['showvalueformat']='%s'%(cformat_map[ret['ctype']])
Chris@87 763 if isarray(var):
Chris@87 764 ret=dictappend(ret, getarrdims(a, var))
Chris@87 765 elif isstring(var):
Chris@87 766 ret['size']=getstrlength(var)
Chris@87 767 ret['rank']='1'
Chris@87 768 ret['pydocsign'], ret['pydocsignout']=getpydocsign(a, var)
Chris@87 769 if hasnote(var):
Chris@87 770 ret['note']=var['note']
Chris@87 771 var['note']=['See elsewhere.']
Chris@87 772 ret['arrdocstr']=getarrdocsign(a, var) # for strings this returns 0-rank but actually is 1-rank
Chris@87 773 return ret