common_rules.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. """
  3. Build common block mechanism for f2py2e.
  4. Copyright 2000 Pearu Peterson all rights reserved,
  5. Pearu Peterson <pearu@ioc.ee>
  6. Permission to use, modify, and distribute this software is given under the
  7. terms of the NumPy License
  8. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  9. $Date: 2005/05/06 10:57:33 $
  10. Pearu Peterson
  11. """
  12. from . import __version__
  13. f2py_version = __version__.version
  14. from .auxfuncs import (
  15. hasbody, hascommon, hasnote, isintent_hide, outmess
  16. )
  17. from . import capi_maps
  18. from . import func2subr
  19. from .crackfortran import rmbadname
  20. def findcommonblocks(block, top=1):
  21. ret = []
  22. if hascommon(block):
  23. for key, value in block['common'].items():
  24. vars_ = {v: block['vars'][v] for v in value}
  25. ret.append((key, value, vars_))
  26. elif hasbody(block):
  27. for b in block['body']:
  28. ret = ret + findcommonblocks(b, 0)
  29. if top:
  30. tret = []
  31. names = []
  32. for t in ret:
  33. if t[0] not in names:
  34. names.append(t[0])
  35. tret.append(t)
  36. return tret
  37. return ret
  38. def buildhooks(m):
  39. ret = {'commonhooks': [], 'initcommonhooks': [],
  40. 'docs': ['"COMMON blocks:\\n"']}
  41. fwrap = ['']
  42. def fadd(line, s=fwrap):
  43. s[0] = '%s\n %s' % (s[0], line)
  44. chooks = ['']
  45. def cadd(line, s=chooks):
  46. s[0] = '%s\n%s' % (s[0], line)
  47. ihooks = ['']
  48. def iadd(line, s=ihooks):
  49. s[0] = '%s\n%s' % (s[0], line)
  50. doc = ['']
  51. def dadd(line, s=doc):
  52. s[0] = '%s\n%s' % (s[0], line)
  53. for (name, vnames, vars) in findcommonblocks(m):
  54. lower_name = name.lower()
  55. hnames, inames = [], []
  56. for n in vnames:
  57. if isintent_hide(vars[n]):
  58. hnames.append(n)
  59. else:
  60. inames.append(n)
  61. if hnames:
  62. outmess('\t\tConstructing COMMON block support for "%s"...\n\t\t %s\n\t\t Hidden: %s\n' % (
  63. name, ','.join(inames), ','.join(hnames)))
  64. else:
  65. outmess('\t\tConstructing COMMON block support for "%s"...\n\t\t %s\n' % (
  66. name, ','.join(inames)))
  67. fadd('subroutine f2pyinit%s(setupfunc)' % name)
  68. fadd('external setupfunc')
  69. for n in vnames:
  70. fadd(func2subr.var2fixfortran(vars, n))
  71. if name == '_BLNK_':
  72. fadd('common %s' % (','.join(vnames)))
  73. else:
  74. fadd('common /%s/ %s' % (name, ','.join(vnames)))
  75. fadd('call setupfunc(%s)' % (','.join(inames)))
  76. fadd('end\n')
  77. cadd('static FortranDataDef f2py_%s_def[] = {' % (name))
  78. idims = []
  79. for n in inames:
  80. ct = capi_maps.getctype(vars[n])
  81. at = capi_maps.c2capi_map[ct]
  82. dm = capi_maps.getarrdims(n, vars[n])
  83. if dm['dims']:
  84. idims.append('(%s)' % (dm['dims']))
  85. else:
  86. idims.append('')
  87. dms = dm['dims'].strip()
  88. if not dms:
  89. dms = '-1'
  90. cadd('\t{\"%s\",%s,{{%s}},%s},' % (n, dm['rank'], dms, at))
  91. cadd('\t{NULL}\n};')
  92. inames1 = rmbadname(inames)
  93. inames1_tps = ','.join(['char *' + s for s in inames1])
  94. cadd('static void f2py_setup_%s(%s) {' % (name, inames1_tps))
  95. cadd('\tint i_f2py=0;')
  96. for n in inames1:
  97. cadd('\tf2py_%s_def[i_f2py++].data = %s;' % (name, n))
  98. cadd('}')
  99. if '_' in lower_name:
  100. F_FUNC = 'F_FUNC_US'
  101. else:
  102. F_FUNC = 'F_FUNC'
  103. cadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void(*)(%s));'
  104. % (F_FUNC, lower_name, name.upper(),
  105. ','.join(['char*'] * len(inames1))))
  106. cadd('static void f2py_init_%s(void) {' % name)
  107. cadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'
  108. % (F_FUNC, lower_name, name.upper(), name))
  109. cadd('}\n')
  110. iadd('\ttmp = PyFortranObject_New(f2py_%s_def,f2py_init_%s);' % (name, name))
  111. iadd('\tF2PyDict_SetItemString(d, \"%s\", tmp);' % name)
  112. iadd('\tPy_DECREF(tmp);')
  113. tname = name.replace('_', '\\_')
  114. dadd('\\subsection{Common block \\texttt{%s}}\n' % (tname))
  115. dadd('\\begin{description}')
  116. for n in inames:
  117. dadd('\\item[]{{}\\verb@%s@{}}' %
  118. (capi_maps.getarrdocsign(n, vars[n])))
  119. if hasnote(vars[n]):
  120. note = vars[n]['note']
  121. if isinstance(note, list):
  122. note = '\n'.join(note)
  123. dadd('--- %s' % (note))
  124. dadd('\\end{description}')
  125. ret['docs'].append(
  126. '"\t/%s/ %s\\n"' % (name, ','.join(map(lambda v, d: v + d, inames, idims))))
  127. ret['commonhooks'] = chooks
  128. ret['initcommonhooks'] = ihooks
  129. ret['latexdoc'] = doc[0]
  130. if len(ret['docs']) <= 1:
  131. ret['docs'] = ''
  132. return ret, fwrap[0]