common_rules.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. elsize = capi_maps.get_elsize(vars[n])
  82. at = capi_maps.c2capi_map[ct]
  83. dm = capi_maps.getarrdims(n, vars[n])
  84. if dm['dims']:
  85. idims.append('(%s)' % (dm['dims']))
  86. else:
  87. idims.append('')
  88. dms = dm['dims'].strip()
  89. if not dms:
  90. dms = '-1'
  91. cadd('\t{\"%s\",%s,{{%s}},%s, %s},'
  92. % (n, dm['rank'], dms, at, elsize))
  93. cadd('\t{NULL}\n};')
  94. inames1 = rmbadname(inames)
  95. inames1_tps = ','.join(['char *' + s for s in inames1])
  96. cadd('static void f2py_setup_%s(%s) {' % (name, inames1_tps))
  97. cadd('\tint i_f2py=0;')
  98. for n in inames1:
  99. cadd('\tf2py_%s_def[i_f2py++].data = %s;' % (name, n))
  100. cadd('}')
  101. if '_' in lower_name:
  102. F_FUNC = 'F_FUNC_US'
  103. else:
  104. F_FUNC = 'F_FUNC'
  105. cadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void(*)(%s));'
  106. % (F_FUNC, lower_name, name.upper(),
  107. ','.join(['char*'] * len(inames1))))
  108. cadd('static void f2py_init_%s(void) {' % name)
  109. cadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'
  110. % (F_FUNC, lower_name, name.upper(), name))
  111. cadd('}\n')
  112. iadd('\ttmp = PyFortranObject_New(f2py_%s_def,f2py_init_%s);' % (name, name))
  113. iadd('\tif (tmp == NULL) return NULL;')
  114. iadd('\tif (F2PyDict_SetItemString(d, \"%s\", tmp) == -1) return NULL;'
  115. % name)
  116. iadd('\tPy_DECREF(tmp);')
  117. tname = name.replace('_', '\\_')
  118. dadd('\\subsection{Common block \\texttt{%s}}\n' % (tname))
  119. dadd('\\begin{description}')
  120. for n in inames:
  121. dadd('\\item[]{{}\\verb@%s@{}}' %
  122. (capi_maps.getarrdocsign(n, vars[n])))
  123. if hasnote(vars[n]):
  124. note = vars[n]['note']
  125. if isinstance(note, list):
  126. note = '\n'.join(note)
  127. dadd('--- %s' % (note))
  128. dadd('\\end{description}')
  129. ret['docs'].append(
  130. '"\t/%s/ %s\\n"' % (name, ','.join(map(lambda v, d: v + d, inames, idims))))
  131. ret['commonhooks'] = chooks
  132. ret['initcommonhooks'] = ihooks
  133. ret['latexdoc'] = doc[0]
  134. if len(ret['docs']) <= 1:
  135. ret['docs'] = ''
  136. return ret, fwrap[0]