rules.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. #!/usr/bin/env python
  2. """
  3. Rules for building C/API module with f2py2e.
  4. Here is a skeleton of a new wrapper function (13Dec2001):
  5. wrapper_function(args)
  6. declarations
  7. get_python_arguments, say, `a' and `b'
  8. get_a_from_python
  9. if (successful) {
  10. get_b_from_python
  11. if (successful) {
  12. callfortran
  13. if (succesful) {
  14. put_a_to_python
  15. if (succesful) {
  16. put_b_to_python
  17. if (succesful) {
  18. buildvalue = ...
  19. }
  20. }
  21. }
  22. }
  23. cleanup_b
  24. }
  25. cleanup_a
  26. return buildvalue
  27. Copyright 1999,2000 Pearu Peterson all rights reserved,
  28. Pearu Peterson <pearu@ioc.ee>
  29. Permission to use, modify, and distribute this software is given under the
  30. terms of the NumPy License.
  31. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  32. $Date: 2005/08/30 08:58:42 $
  33. Pearu Peterson
  34. """
  35. from __future__ import division, absolute_import, print_function
  36. __version__ = "$Revision: 1.129 $"[10:-1]
  37. from . import __version__
  38. f2py_version = __version__.version
  39. import os
  40. import time
  41. import copy
  42. from .auxfuncs import (
  43. applyrules, debugcapi, dictappend, errmess, gentitle, getargs2,
  44. hascallstatement, hasexternals, hasinitvalue, hasnote, hasresultnote,
  45. isarray, isarrayofstrings, iscomplex, iscomplexarray,
  46. iscomplexfunction, iscomplexfunction_warn, isdummyroutine, isexternal,
  47. isfunction, isfunction_wrap, isint1array, isintent_aux, isintent_c,
  48. isintent_callback, isintent_copy, isintent_hide, isintent_inout,
  49. isintent_nothide, isintent_out, isintent_overwrite, islogical,
  50. islong_complex, islong_double, islong_doublefunction, islong_long,
  51. islong_longfunction, ismoduleroutine, isoptional, isrequired, isscalar,
  52. issigned_long_longarray, isstring, isstringarray, isstringfunction,
  53. issubroutine, issubroutine_wrap, isthreadsafe, isunsigned,
  54. isunsigned_char, isunsigned_chararray, isunsigned_long_long,
  55. isunsigned_long_longarray, isunsigned_short, isunsigned_shortarray,
  56. l_and, l_not, l_or, outmess, replace, stripcomma,
  57. )
  58. from . import capi_maps
  59. from . import cfuncs
  60. from . import common_rules
  61. from . import use_rules
  62. from . import f90mod_rules
  63. from . import func2subr
  64. options = {}
  65. sepdict = {}
  66. #for k in ['need_cfuncs']: sepdict[k]=','
  67. for k in ['decl',
  68. 'frompyobj',
  69. 'cleanupfrompyobj',
  70. 'topyarr', 'method',
  71. 'pyobjfrom', 'closepyobjfrom',
  72. 'freemem',
  73. 'userincludes',
  74. 'includes0', 'includes', 'typedefs', 'typedefs_generated',
  75. 'cppmacros', 'cfuncs', 'callbacks',
  76. 'latexdoc',
  77. 'restdoc',
  78. 'routine_defs', 'externroutines',
  79. 'initf2pywraphooks',
  80. 'commonhooks', 'initcommonhooks',
  81. 'f90modhooks', 'initf90modhooks']:
  82. sepdict[k] = '\n'
  83. #################### Rules for C/API module #################
  84. module_rules = {
  85. 'modulebody': """\
  86. /* File: #modulename#module.c
  87. * This file is auto-generated with f2py (version:#f2py_version#).
  88. * f2py is a Fortran to Python Interface Generator (FPIG), Second Edition,
  89. * written by Pearu Peterson <pearu@cens.ioc.ee>.
  90. * See http://cens.ioc.ee/projects/f2py2e/
  91. * Generation date: """ + time.asctime(time.localtime(time.time())) + """
  92. * $R""" + """evision:$
  93. * $D""" + """ate:$
  94. * Do not edit this file directly unless you know what you are doing!!!
  95. */
  96. #ifdef __cplusplus
  97. extern \"C\" {
  98. #endif
  99. """ + gentitle("See f2py2e/cfuncs.py: includes") + """
  100. #includes#
  101. #includes0#
  102. """ + gentitle("See f2py2e/rules.py: mod_rules['modulebody']") + """
  103. static PyObject *#modulename#_error;
  104. static PyObject *#modulename#_module;
  105. """ + gentitle("See f2py2e/cfuncs.py: typedefs") + """
  106. #typedefs#
  107. """ + gentitle("See f2py2e/cfuncs.py: typedefs_generated") + """
  108. #typedefs_generated#
  109. """ + gentitle("See f2py2e/cfuncs.py: cppmacros") + """
  110. #cppmacros#
  111. """ + gentitle("See f2py2e/cfuncs.py: cfuncs") + """
  112. #cfuncs#
  113. """ + gentitle("See f2py2e/cfuncs.py: userincludes") + """
  114. #userincludes#
  115. """ + gentitle("See f2py2e/capi_rules.py: usercode") + """
  116. #usercode#
  117. /* See f2py2e/rules.py */
  118. #externroutines#
  119. """ + gentitle("See f2py2e/capi_rules.py: usercode1") + """
  120. #usercode1#
  121. """ + gentitle("See f2py2e/cb_rules.py: buildcallback") + """
  122. #callbacks#
  123. """ + gentitle("See f2py2e/rules.py: buildapi") + """
  124. #body#
  125. """ + gentitle("See f2py2e/f90mod_rules.py: buildhooks") + """
  126. #f90modhooks#
  127. """ + gentitle("See f2py2e/rules.py: module_rules['modulebody']") + """
  128. """ + gentitle("See f2py2e/common_rules.py: buildhooks") + """
  129. #commonhooks#
  130. """ + gentitle("See f2py2e/rules.py") + """
  131. static FortranDataDef f2py_routine_defs[] = {
  132. #routine_defs#
  133. \t{NULL}
  134. };
  135. static PyMethodDef f2py_module_methods[] = {
  136. #pymethoddef#
  137. \t{NULL,NULL}
  138. };
  139. #if PY_VERSION_HEX >= 0x03000000
  140. static struct PyModuleDef moduledef = {
  141. \tPyModuleDef_HEAD_INIT,
  142. \t"#modulename#",
  143. \tNULL,
  144. \t-1,
  145. \tf2py_module_methods,
  146. \tNULL,
  147. \tNULL,
  148. \tNULL,
  149. \tNULL
  150. };
  151. #endif
  152. #if PY_VERSION_HEX >= 0x03000000
  153. #define RETVAL m
  154. PyMODINIT_FUNC PyInit_#modulename#(void) {
  155. #else
  156. #define RETVAL
  157. PyMODINIT_FUNC init#modulename#(void) {
  158. #endif
  159. \tint i;
  160. \tPyObject *m,*d, *s;
  161. #if PY_VERSION_HEX >= 0x03000000
  162. \tm = #modulename#_module = PyModule_Create(&moduledef);
  163. #else
  164. \tm = #modulename#_module = Py_InitModule(\"#modulename#\", f2py_module_methods);
  165. #endif
  166. \tPy_TYPE(&PyFortran_Type) = &PyType_Type;
  167. \timport_array();
  168. \tif (PyErr_Occurred())
  169. \t\t{PyErr_SetString(PyExc_ImportError, \"can't initialize module #modulename# (failed to import numpy)\"); return RETVAL;}
  170. \td = PyModule_GetDict(m);
  171. \ts = PyString_FromString(\"$R""" + """evision: $\");
  172. \tPyDict_SetItemString(d, \"__version__\", s);
  173. #if PY_VERSION_HEX >= 0x03000000
  174. \ts = PyUnicode_FromString(
  175. #else
  176. \ts = PyString_FromString(
  177. #endif
  178. \t\t\"This module '#modulename#' is auto-generated with f2py (version:#f2py_version#).\\nFunctions:\\n\"\n#docs#\".\");
  179. \tPyDict_SetItemString(d, \"__doc__\", s);
  180. \t#modulename#_error = PyErr_NewException (\"#modulename#.error\", NULL, NULL);
  181. \tPy_DECREF(s);
  182. \tfor(i=0;f2py_routine_defs[i].name!=NULL;i++)
  183. \t\tPyDict_SetItemString(d, f2py_routine_defs[i].name,PyFortranObject_NewAsAttr(&f2py_routine_defs[i]));
  184. #initf2pywraphooks#
  185. #initf90modhooks#
  186. #initcommonhooks#
  187. #interface_usercode#
  188. #ifdef F2PY_REPORT_ATEXIT
  189. \tif (! PyErr_Occurred())
  190. \t\ton_exit(f2py_report_on_exit,(void*)\"#modulename#\");
  191. #endif
  192. \treturn RETVAL;
  193. }
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. """,
  198. 'separatorsfor': {'latexdoc': '\n\n',
  199. 'restdoc': '\n\n'},
  200. 'latexdoc': ['\\section{Module \\texttt{#texmodulename#}}\n',
  201. '#modnote#\n',
  202. '#latexdoc#'],
  203. 'restdoc': ['Module #modulename#\n' + '=' * 80,
  204. '\n#restdoc#']
  205. }
  206. defmod_rules = [
  207. {'body': '/*eof body*/',
  208. 'method': '/*eof method*/',
  209. 'externroutines': '/*eof externroutines*/',
  210. 'routine_defs': '/*eof routine_defs*/',
  211. 'initf90modhooks': '/*eof initf90modhooks*/',
  212. 'initf2pywraphooks': '/*eof initf2pywraphooks*/',
  213. 'initcommonhooks': '/*eof initcommonhooks*/',
  214. 'latexdoc': '',
  215. 'restdoc': '',
  216. 'modnote': {hasnote: '#note#', l_not(hasnote): ''},
  217. }
  218. ]
  219. routine_rules = {
  220. 'separatorsfor': sepdict,
  221. 'body': """
  222. #begintitle#
  223. static char doc_#apiname#[] = \"\\\n#docreturn##name#(#docsignatureshort#)\\n\\nWrapper for ``#name#``.\\\n\\n#docstrsigns#\";
  224. /* #declfortranroutine# */
  225. static PyObject *#apiname#(const PyObject *capi_self,
  226. PyObject *capi_args,
  227. PyObject *capi_keywds,
  228. #functype# (*f2py_func)(#callprotoargument#)) {
  229. \tPyObject * volatile capi_buildvalue = NULL;
  230. \tvolatile int f2py_success = 1;
  231. #decl#
  232. \tstatic char *capi_kwlist[] = {#kwlist##kwlistopt##kwlistxa#NULL};
  233. #usercode#
  234. #routdebugenter#
  235. #ifdef F2PY_REPORT_ATEXIT
  236. f2py_start_clock();
  237. #endif
  238. \tif (!PyArg_ParseTupleAndKeywords(capi_args,capi_keywds,\\
  239. \t\t\"#argformat##keyformat##xaformat#:#pyname#\",\\
  240. \t\tcapi_kwlist#args_capi##keys_capi##keys_xa#))\n\t\treturn NULL;
  241. #frompyobj#
  242. /*end of frompyobj*/
  243. #ifdef F2PY_REPORT_ATEXIT
  244. f2py_start_call_clock();
  245. #endif
  246. #callfortranroutine#
  247. if (PyErr_Occurred())
  248. f2py_success = 0;
  249. #ifdef F2PY_REPORT_ATEXIT
  250. f2py_stop_call_clock();
  251. #endif
  252. /*end of callfortranroutine*/
  253. \t\tif (f2py_success) {
  254. #pyobjfrom#
  255. /*end of pyobjfrom*/
  256. \t\tCFUNCSMESS(\"Building return value.\\n\");
  257. \t\tcapi_buildvalue = Py_BuildValue(\"#returnformat#\"#return#);
  258. /*closepyobjfrom*/
  259. #closepyobjfrom#
  260. \t\t} /*if (f2py_success) after callfortranroutine*/
  261. /*cleanupfrompyobj*/
  262. #cleanupfrompyobj#
  263. \tif (capi_buildvalue == NULL) {
  264. #routdebugfailure#
  265. \t} else {
  266. #routdebugleave#
  267. \t}
  268. \tCFUNCSMESS(\"Freeing memory.\\n\");
  269. #freemem#
  270. #ifdef F2PY_REPORT_ATEXIT
  271. f2py_stop_clock();
  272. #endif
  273. \treturn capi_buildvalue;
  274. }
  275. #endtitle#
  276. """,
  277. 'routine_defs': '#routine_def#',
  278. 'initf2pywraphooks': '#initf2pywraphook#',
  279. 'externroutines': '#declfortranroutine#',
  280. 'doc': '#docreturn##name#(#docsignature#)',
  281. 'docshort': '#docreturn##name#(#docsignatureshort#)',
  282. 'docs': '"\t#docreturn##name#(#docsignature#)\\n"\n',
  283. 'need': ['arrayobject.h', 'CFUNCSMESS', 'MINMAX'],
  284. 'cppmacros': {debugcapi: '#define DEBUGCFUNCS'},
  285. 'latexdoc': ['\\subsection{Wrapper function \\texttt{#texname#}}\n',
  286. """
  287. \\noindent{{}\\verb@#docreturn##name#@{}}\\texttt{(#latexdocsignatureshort#)}
  288. #routnote#
  289. #latexdocstrsigns#
  290. """],
  291. 'restdoc': ['Wrapped function ``#name#``\n' + '-' * 80,
  292. ]
  293. }
  294. ################## Rules for C/API function ##############
  295. rout_rules = [
  296. { # Init
  297. 'separatorsfor': {'callfortranroutine': '\n', 'routdebugenter': '\n', 'decl': '\n',
  298. 'routdebugleave': '\n', 'routdebugfailure': '\n',
  299. 'setjmpbuf': ' || ',
  300. 'docstrreq': '\n', 'docstropt': '\n', 'docstrout': '\n',
  301. 'docstrcbs': '\n', 'docstrsigns': '\\n"\n"',
  302. 'latexdocstrsigns': '\n',
  303. 'latexdocstrreq': '\n', 'latexdocstropt': '\n',
  304. 'latexdocstrout': '\n', 'latexdocstrcbs': '\n',
  305. },
  306. 'kwlist': '', 'kwlistopt': '', 'callfortran': '', 'callfortranappend': '',
  307. 'docsign': '', 'docsignopt': '', 'decl': '/*decl*/',
  308. 'freemem': '/*freemem*/',
  309. 'docsignshort': '', 'docsignoptshort': '',
  310. 'docstrsigns': '', 'latexdocstrsigns': '',
  311. 'docstrreq': '\\nParameters\\n----------',
  312. 'docstropt': '\\nOther Parameters\\n----------------',
  313. 'docstrout': '\\nReturns\\n-------',
  314. 'docstrcbs': '\\nNotes\\n-----\\nCall-back functions::\\n',
  315. 'latexdocstrreq': '\\noindent Required arguments:',
  316. 'latexdocstropt': '\\noindent Optional arguments:',
  317. 'latexdocstrout': '\\noindent Return objects:',
  318. 'latexdocstrcbs': '\\noindent Call-back functions:',
  319. 'args_capi': '', 'keys_capi': '', 'functype': '',
  320. 'frompyobj': '/*frompyobj*/',
  321. # this list will be reversed
  322. 'cleanupfrompyobj': ['/*end of cleanupfrompyobj*/'],
  323. 'pyobjfrom': '/*pyobjfrom*/',
  324. # this list will be reversed
  325. 'closepyobjfrom': ['/*end of closepyobjfrom*/'],
  326. 'topyarr': '/*topyarr*/', 'routdebugleave': '/*routdebugleave*/',
  327. 'routdebugenter': '/*routdebugenter*/',
  328. 'routdebugfailure': '/*routdebugfailure*/',
  329. 'callfortranroutine': '/*callfortranroutine*/',
  330. 'argformat': '', 'keyformat': '', 'need_cfuncs': '',
  331. 'docreturn': '', 'return': '', 'returnformat': '', 'rformat': '',
  332. 'kwlistxa': '', 'keys_xa': '', 'xaformat': '', 'docsignxa': '', 'docsignxashort': '',
  333. 'initf2pywraphook': '',
  334. 'routnote': {hasnote: '--- #note#', l_not(hasnote): ''},
  335. }, {
  336. 'apiname': 'f2py_rout_#modulename#_#name#',
  337. 'pyname': '#modulename#.#name#',
  338. 'decl': '',
  339. '_check': l_not(ismoduleroutine)
  340. }, {
  341. 'apiname': 'f2py_rout_#modulename#_#f90modulename#_#name#',
  342. 'pyname': '#modulename#.#f90modulename#.#name#',
  343. 'decl': '',
  344. '_check': ismoduleroutine
  345. }, { # Subroutine
  346. 'functype': 'void',
  347. 'declfortranroutine': {l_and(l_not(l_or(ismoduleroutine, isintent_c)), l_not(isdummyroutine)): 'extern void #F_FUNC#(#fortranname#,#FORTRANNAME#)(#callprotoargument#);',
  348. l_and(l_not(ismoduleroutine), isintent_c, l_not(isdummyroutine)): 'extern void #fortranname#(#callprotoargument#);',
  349. ismoduleroutine: '',
  350. isdummyroutine: ''
  351. },
  352. 'routine_def': {l_not(l_or(ismoduleroutine, isintent_c, isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#F_FUNC#(#fortranname#,#FORTRANNAME#),(f2py_init_func)#apiname#,doc_#apiname#},',
  353. l_and(l_not(ismoduleroutine), isintent_c, l_not(isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#fortranname#,(f2py_init_func)#apiname#,doc_#apiname#},',
  354. l_and(l_not(ismoduleroutine), isdummyroutine): '\t{\"#name#\",-1,{{-1}},0,NULL,(f2py_init_func)#apiname#,doc_#apiname#},',
  355. },
  356. 'need': {l_and(l_not(l_or(ismoduleroutine, isintent_c)), l_not(isdummyroutine)): 'F_FUNC'},
  357. 'callfortranroutine': [
  358. {debugcapi: [
  359. """\tfprintf(stderr,\"debug-capi:Fortran subroutine `#fortranname#(#callfortran#)\'\\n\");"""]},
  360. {hasexternals: """\
  361. \t\tif (#setjmpbuf#) {
  362. \t\t\tf2py_success = 0;
  363. \t\t} else {"""},
  364. {isthreadsafe: '\t\t\tPy_BEGIN_ALLOW_THREADS'},
  365. {hascallstatement: '''\t\t\t\t#callstatement#;
  366. \t\t\t\t/*(*f2py_func)(#callfortran#);*/'''},
  367. {l_not(l_or(hascallstatement, isdummyroutine))
  368. : '\t\t\t\t(*f2py_func)(#callfortran#);'},
  369. {isthreadsafe: '\t\t\tPy_END_ALLOW_THREADS'},
  370. {hasexternals: """\t\t}"""}
  371. ],
  372. '_check': l_and(issubroutine, l_not(issubroutine_wrap)),
  373. }, { # Wrapped function
  374. 'functype': 'void',
  375. 'declfortranroutine': {l_not(l_or(ismoduleroutine, isdummyroutine)): 'extern void #F_WRAPPEDFUNC#(#name_lower#,#NAME#)(#callprotoargument#);',
  376. isdummyroutine: '',
  377. },
  378. 'routine_def': {l_not(l_or(ismoduleroutine, isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#F_WRAPPEDFUNC#(#name_lower#,#NAME#),(f2py_init_func)#apiname#,doc_#apiname#},',
  379. isdummyroutine: '\t{\"#name#\",-1,{{-1}},0,NULL,(f2py_init_func)#apiname#,doc_#apiname#},',
  380. },
  381. 'initf2pywraphook': {l_not(l_or(ismoduleroutine, isdummyroutine)): '''
  382. {
  383. extern #ctype# #F_FUNC#(#name_lower#,#NAME#)(void);
  384. PyObject* o = PyDict_GetItemString(d,"#name#");
  385. PyObject_SetAttrString(o,"_cpointer", F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL));
  386. #if PY_VERSION_HEX >= 0x03000000
  387. PyObject_SetAttrString(o,"__name__", PyUnicode_FromString("#name#"));
  388. #else
  389. PyObject_SetAttrString(o,"__name__", PyString_FromString("#name#"));
  390. #endif
  391. }
  392. '''},
  393. 'need': {l_not(l_or(ismoduleroutine, isdummyroutine)): ['F_WRAPPEDFUNC', 'F_FUNC']},
  394. 'callfortranroutine': [
  395. {debugcapi: [
  396. """\tfprintf(stderr,\"debug-capi:Fortran subroutine `f2pywrap#name_lower#(#callfortran#)\'\\n\");"""]},
  397. {hasexternals: """\
  398. \tif (#setjmpbuf#) {
  399. \t\tf2py_success = 0;
  400. \t} else {"""},
  401. {isthreadsafe: '\tPy_BEGIN_ALLOW_THREADS'},
  402. {l_not(l_or(hascallstatement, isdummyroutine))
  403. : '\t(*f2py_func)(#callfortran#);'},
  404. {hascallstatement:
  405. '\t#callstatement#;\n\t/*(*f2py_func)(#callfortran#);*/'},
  406. {isthreadsafe: '\tPy_END_ALLOW_THREADS'},
  407. {hasexternals: '\t}'}
  408. ],
  409. '_check': isfunction_wrap,
  410. }, { # Wrapped subroutine
  411. 'functype': 'void',
  412. 'declfortranroutine': {l_not(l_or(ismoduleroutine, isdummyroutine)): 'extern void #F_WRAPPEDFUNC#(#name_lower#,#NAME#)(#callprotoargument#);',
  413. isdummyroutine: '',
  414. },
  415. 'routine_def': {l_not(l_or(ismoduleroutine, isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#F_WRAPPEDFUNC#(#name_lower#,#NAME#),(f2py_init_func)#apiname#,doc_#apiname#},',
  416. isdummyroutine: '\t{\"#name#\",-1,{{-1}},0,NULL,(f2py_init_func)#apiname#,doc_#apiname#},',
  417. },
  418. 'initf2pywraphook': {l_not(l_or(ismoduleroutine, isdummyroutine)): '''
  419. {
  420. extern void #F_FUNC#(#name_lower#,#NAME#)(void);
  421. PyObject* o = PyDict_GetItemString(d,"#name#");
  422. PyObject_SetAttrString(o,"_cpointer", F2PyCapsule_FromVoidPtr((void*)#F_FUNC#(#name_lower#,#NAME#),NULL));
  423. #if PY_VERSION_HEX >= 0x03000000
  424. PyObject_SetAttrString(o,"__name__", PyUnicode_FromString("#name#"));
  425. #else
  426. PyObject_SetAttrString(o,"__name__", PyString_FromString("#name#"));
  427. #endif
  428. }
  429. '''},
  430. 'need': {l_not(l_or(ismoduleroutine, isdummyroutine)): ['F_WRAPPEDFUNC', 'F_FUNC']},
  431. 'callfortranroutine': [
  432. {debugcapi: [
  433. """\tfprintf(stderr,\"debug-capi:Fortran subroutine `f2pywrap#name_lower#(#callfortran#)\'\\n\");"""]},
  434. {hasexternals: """\
  435. \tif (#setjmpbuf#) {
  436. \t\tf2py_success = 0;
  437. \t} else {"""},
  438. {isthreadsafe: '\tPy_BEGIN_ALLOW_THREADS'},
  439. {l_not(l_or(hascallstatement, isdummyroutine))
  440. : '\t(*f2py_func)(#callfortran#);'},
  441. {hascallstatement:
  442. '\t#callstatement#;\n\t/*(*f2py_func)(#callfortran#);*/'},
  443. {isthreadsafe: '\tPy_END_ALLOW_THREADS'},
  444. {hasexternals: '\t}'}
  445. ],
  446. '_check': issubroutine_wrap,
  447. }, { # Function
  448. 'functype': '#ctype#',
  449. 'docreturn': {l_not(isintent_hide): '#rname#,'},
  450. 'docstrout': '#pydocsignout#',
  451. 'latexdocstrout': ['\\item[]{{}\\verb@#pydocsignout#@{}}',
  452. {hasresultnote: '--- #resultnote#'}],
  453. 'callfortranroutine': [{l_and(debugcapi, isstringfunction): """\
  454. #ifdef USESCOMPAQFORTRAN
  455. \tfprintf(stderr,\"debug-capi:Fortran function #ctype# #fortranname#(#callcompaqfortran#)\\n\");
  456. #else
  457. \tfprintf(stderr,\"debug-capi:Fortran function #ctype# #fortranname#(#callfortran#)\\n\");
  458. #endif
  459. """},
  460. {l_and(debugcapi, l_not(isstringfunction)): """\
  461. \tfprintf(stderr,\"debug-capi:Fortran function #ctype# #fortranname#(#callfortran#)\\n\");
  462. """}
  463. ],
  464. '_check': l_and(isfunction, l_not(isfunction_wrap))
  465. }, { # Scalar function
  466. 'declfortranroutine': {l_and(l_not(l_or(ismoduleroutine, isintent_c)), l_not(isdummyroutine)): 'extern #ctype# #F_FUNC#(#fortranname#,#FORTRANNAME#)(#callprotoargument#);',
  467. l_and(l_not(ismoduleroutine), isintent_c, l_not(isdummyroutine)): 'extern #ctype# #fortranname#(#callprotoargument#);',
  468. isdummyroutine: ''
  469. },
  470. 'routine_def': {l_and(l_not(l_or(ismoduleroutine, isintent_c)), l_not(isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#F_FUNC#(#fortranname#,#FORTRANNAME#),(f2py_init_func)#apiname#,doc_#apiname#},',
  471. l_and(l_not(ismoduleroutine), isintent_c, l_not(isdummyroutine)): '\t{\"#name#\",-1,{{-1}},0,(char *)#fortranname#,(f2py_init_func)#apiname#,doc_#apiname#},',
  472. isdummyroutine: '\t{\"#name#\",-1,{{-1}},0,NULL,(f2py_init_func)#apiname#,doc_#apiname#},',
  473. },
  474. 'decl': [{iscomplexfunction_warn: '\t#ctype# #name#_return_value={0,0};',
  475. l_not(iscomplexfunction): '\t#ctype# #name#_return_value=0;'},
  476. {iscomplexfunction:
  477. '\tPyObject *#name#_return_value_capi = Py_None;'}
  478. ],
  479. 'callfortranroutine': [
  480. {hasexternals: """\
  481. \tif (#setjmpbuf#) {
  482. \t\tf2py_success = 0;
  483. \t} else {"""},
  484. {isthreadsafe: '\tPy_BEGIN_ALLOW_THREADS'},
  485. {hascallstatement: '''\t#callstatement#;
  486. /*\t#name#_return_value = (*f2py_func)(#callfortran#);*/
  487. '''},
  488. {l_not(l_or(hascallstatement, isdummyroutine))
  489. : '\t#name#_return_value = (*f2py_func)(#callfortran#);'},
  490. {isthreadsafe: '\tPy_END_ALLOW_THREADS'},
  491. {hasexternals: '\t}'},
  492. {l_and(debugcapi, iscomplexfunction)
  493. : '\tfprintf(stderr,"#routdebugshowvalue#\\n",#name#_return_value.r,#name#_return_value.i);'},
  494. {l_and(debugcapi, l_not(iscomplexfunction)): '\tfprintf(stderr,"#routdebugshowvalue#\\n",#name#_return_value);'}],
  495. 'pyobjfrom': {iscomplexfunction: '\t#name#_return_value_capi = pyobj_from_#ctype#1(#name#_return_value);'},
  496. 'need': [{l_not(isdummyroutine): 'F_FUNC'},
  497. {iscomplexfunction: 'pyobj_from_#ctype#1'},
  498. {islong_longfunction: 'long_long'},
  499. {islong_doublefunction: 'long_double'}],
  500. 'returnformat': {l_not(isintent_hide): '#rformat#'},
  501. 'return': {iscomplexfunction: ',#name#_return_value_capi',
  502. l_not(l_or(iscomplexfunction, isintent_hide)): ',#name#_return_value'},
  503. '_check': l_and(isfunction, l_not(isstringfunction), l_not(isfunction_wrap))
  504. }, { # String function # in use for --no-wrap
  505. 'declfortranroutine': 'extern void #F_FUNC#(#fortranname#,#FORTRANNAME#)(#callprotoargument#);',
  506. 'routine_def': {l_not(l_or(ismoduleroutine, isintent_c)):
  507. '\t{\"#name#\",-1,{{-1}},0,(char *)#F_FUNC#(#fortranname#,#FORTRANNAME#),(f2py_init_func)#apiname#,doc_#apiname#},',
  508. l_and(l_not(ismoduleroutine), isintent_c):
  509. '\t{\"#name#\",-1,{{-1}},0,(char *)#fortranname#,(f2py_init_func)#apiname#,doc_#apiname#},'
  510. },
  511. 'decl': ['\t#ctype# #name#_return_value = NULL;',
  512. '\tint #name#_return_value_len = 0;'],
  513. 'callfortran':'#name#_return_value,#name#_return_value_len,',
  514. 'callfortranroutine':['\t#name#_return_value_len = #rlength#;',
  515. '\tif ((#name#_return_value = (string)malloc(sizeof(char)*(#name#_return_value_len+1))) == NULL) {',
  516. '\t\tPyErr_SetString(PyExc_MemoryError, \"out of memory\");',
  517. '\t\tf2py_success = 0;',
  518. '\t} else {',
  519. "\t\t(#name#_return_value)[#name#_return_value_len] = '\\0';",
  520. '\t}',
  521. '\tif (f2py_success) {',
  522. {hasexternals: """\
  523. \t\tif (#setjmpbuf#) {
  524. \t\t\tf2py_success = 0;
  525. \t\t} else {"""},
  526. {isthreadsafe: '\t\tPy_BEGIN_ALLOW_THREADS'},
  527. """\
  528. #ifdef USESCOMPAQFORTRAN
  529. \t\t(*f2py_func)(#callcompaqfortran#);
  530. #else
  531. \t\t(*f2py_func)(#callfortran#);
  532. #endif
  533. """,
  534. {isthreadsafe: '\t\tPy_END_ALLOW_THREADS'},
  535. {hasexternals: '\t\t}'},
  536. {debugcapi:
  537. '\t\tfprintf(stderr,"#routdebugshowvalue#\\n",#name#_return_value_len,#name#_return_value);'},
  538. '\t} /* if (f2py_success) after (string)malloc */',
  539. ],
  540. 'returnformat': '#rformat#',
  541. 'return': ',#name#_return_value',
  542. 'freemem': '\tSTRINGFREE(#name#_return_value);',
  543. 'need': ['F_FUNC', '#ctype#', 'STRINGFREE'],
  544. '_check':l_and(isstringfunction, l_not(isfunction_wrap)) # ???obsolete
  545. },
  546. { # Debugging
  547. 'routdebugenter': '\tfprintf(stderr,"debug-capi:Python C/API function #modulename#.#name#(#docsignature#)\\n");',
  548. 'routdebugleave': '\tfprintf(stderr,"debug-capi:Python C/API function #modulename#.#name#: successful.\\n");',
  549. 'routdebugfailure': '\tfprintf(stderr,"debug-capi:Python C/API function #modulename#.#name#: failure.\\n");',
  550. '_check': debugcapi
  551. }
  552. ]
  553. ################ Rules for arguments ##################
  554. typedef_need_dict = {islong_long: 'long_long',
  555. islong_double: 'long_double',
  556. islong_complex: 'complex_long_double',
  557. isunsigned_char: 'unsigned_char',
  558. isunsigned_short: 'unsigned_short',
  559. isunsigned: 'unsigned',
  560. isunsigned_long_long: 'unsigned_long_long',
  561. isunsigned_chararray: 'unsigned_char',
  562. isunsigned_shortarray: 'unsigned_short',
  563. isunsigned_long_longarray: 'unsigned_long_long',
  564. issigned_long_longarray: 'long_long',
  565. }
  566. aux_rules = [
  567. {
  568. 'separatorsfor': sepdict
  569. },
  570. { # Common
  571. 'frompyobj': ['\t/* Processing auxiliary variable #varname# */',
  572. {debugcapi: '\tfprintf(stderr,"#vardebuginfo#\\n");'}, ],
  573. 'cleanupfrompyobj': '\t/* End of cleaning variable #varname# */',
  574. 'need': typedef_need_dict,
  575. },
  576. # Scalars (not complex)
  577. { # Common
  578. 'decl': '\t#ctype# #varname# = 0;',
  579. 'need': {hasinitvalue: 'math.h'},
  580. 'frompyobj': {hasinitvalue: '\t#varname# = #init#;'},
  581. '_check': l_and(isscalar, l_not(iscomplex)),
  582. },
  583. {
  584. 'return': ',#varname#',
  585. 'docstrout': '#pydocsignout#',
  586. 'docreturn': '#outvarname#,',
  587. 'returnformat': '#varrformat#',
  588. '_check': l_and(isscalar, l_not(iscomplex), isintent_out),
  589. },
  590. # Complex scalars
  591. { # Common
  592. 'decl': '\t#ctype# #varname#;',
  593. 'frompyobj': {hasinitvalue: '\t#varname#.r = #init.r#, #varname#.i = #init.i#;'},
  594. '_check': iscomplex
  595. },
  596. # String
  597. { # Common
  598. 'decl': ['\t#ctype# #varname# = NULL;',
  599. '\tint slen(#varname#);',
  600. ],
  601. 'need':['len..'],
  602. '_check':isstring
  603. },
  604. # Array
  605. { # Common
  606. 'decl': ['\t#ctype# *#varname# = NULL;',
  607. '\tnpy_intp #varname#_Dims[#rank#] = {#rank*[-1]#};',
  608. '\tconst int #varname#_Rank = #rank#;',
  609. ],
  610. 'need':['len..', {hasinitvalue: 'forcomb'}, {hasinitvalue: 'CFUNCSMESS'}],
  611. '_check': isarray
  612. },
  613. # Scalararray
  614. { # Common
  615. '_check': l_and(isarray, l_not(iscomplexarray))
  616. }, { # Not hidden
  617. '_check': l_and(isarray, l_not(iscomplexarray), isintent_nothide)
  618. },
  619. # Integer*1 array
  620. {'need': '#ctype#',
  621. '_check': isint1array,
  622. '_depend': ''
  623. },
  624. # Integer*-1 array
  625. {'need': '#ctype#',
  626. '_check': isunsigned_chararray,
  627. '_depend': ''
  628. },
  629. # Integer*-2 array
  630. {'need': '#ctype#',
  631. '_check': isunsigned_shortarray,
  632. '_depend': ''
  633. },
  634. # Integer*-8 array
  635. {'need': '#ctype#',
  636. '_check': isunsigned_long_longarray,
  637. '_depend': ''
  638. },
  639. # Complexarray
  640. {'need': '#ctype#',
  641. '_check': iscomplexarray,
  642. '_depend': ''
  643. },
  644. # Stringarray
  645. {
  646. 'callfortranappend': {isarrayofstrings: 'flen(#varname#),'},
  647. 'need': 'string',
  648. '_check': isstringarray
  649. }
  650. ]
  651. arg_rules = [
  652. {
  653. 'separatorsfor': sepdict
  654. },
  655. { # Common
  656. 'frompyobj': ['\t/* Processing variable #varname# */',
  657. {debugcapi: '\tfprintf(stderr,"#vardebuginfo#\\n");'}, ],
  658. 'cleanupfrompyobj': '\t/* End of cleaning variable #varname# */',
  659. '_depend': '',
  660. 'need': typedef_need_dict,
  661. },
  662. # Doc signatures
  663. {
  664. 'docstropt': {l_and(isoptional, isintent_nothide): '#pydocsign#'},
  665. 'docstrreq': {l_and(isrequired, isintent_nothide): '#pydocsign#'},
  666. 'docstrout': {isintent_out: '#pydocsignout#'},
  667. 'latexdocstropt': {l_and(isoptional, isintent_nothide): ['\\item[]{{}\\verb@#pydocsign#@{}}',
  668. {hasnote: '--- #note#'}]},
  669. 'latexdocstrreq': {l_and(isrequired, isintent_nothide): ['\\item[]{{}\\verb@#pydocsign#@{}}',
  670. {hasnote: '--- #note#'}]},
  671. 'latexdocstrout': {isintent_out: ['\\item[]{{}\\verb@#pydocsignout#@{}}',
  672. {l_and(hasnote, isintent_hide): '--- #note#',
  673. l_and(hasnote, isintent_nothide): '--- See above.'}]},
  674. 'depend': ''
  675. },
  676. # Required/Optional arguments
  677. {
  678. 'kwlist': '"#varname#",',
  679. 'docsign': '#varname#,',
  680. '_check': l_and(isintent_nothide, l_not(isoptional))
  681. },
  682. {
  683. 'kwlistopt': '"#varname#",',
  684. 'docsignopt': '#varname#=#showinit#,',
  685. 'docsignoptshort': '#varname#,',
  686. '_check': l_and(isintent_nothide, isoptional)
  687. },
  688. # Docstring/BuildValue
  689. {
  690. 'docreturn': '#outvarname#,',
  691. 'returnformat': '#varrformat#',
  692. '_check': isintent_out
  693. },
  694. # Externals (call-back functions)
  695. { # Common
  696. 'docsignxa': {isintent_nothide: '#varname#_extra_args=(),'},
  697. 'docsignxashort': {isintent_nothide: '#varname#_extra_args,'},
  698. 'docstropt': {isintent_nothide: '#varname#_extra_args : input tuple, optional\\n Default: ()'},
  699. 'docstrcbs': '#cbdocstr#',
  700. 'latexdocstrcbs': '\\item[] #cblatexdocstr#',
  701. 'latexdocstropt': {isintent_nothide: '\\item[]{{}\\verb@#varname#_extra_args := () input tuple@{}} --- Extra arguments for call-back function {{}\\verb@#varname#@{}}.'},
  702. 'decl': ['\tPyObject *#varname#_capi = Py_None;',
  703. '\tPyTupleObject *#varname#_xa_capi = NULL;',
  704. '\tPyTupleObject *#varname#_args_capi = NULL;',
  705. '\tint #varname#_nofargs_capi = 0;',
  706. {l_not(isintent_callback):
  707. '\t#cbname#_typedef #varname#_cptr;'}
  708. ],
  709. 'kwlistxa': {isintent_nothide: '"#varname#_extra_args",'},
  710. 'argformat': {isrequired: 'O'},
  711. 'keyformat': {isoptional: 'O'},
  712. 'xaformat': {isintent_nothide: 'O!'},
  713. 'args_capi': {isrequired: ',&#varname#_capi'},
  714. 'keys_capi': {isoptional: ',&#varname#_capi'},
  715. 'keys_xa': ',&PyTuple_Type,&#varname#_xa_capi',
  716. 'setjmpbuf': '(setjmp(#cbname#_jmpbuf))',
  717. 'callfortran': {l_not(isintent_callback): '#varname#_cptr,'},
  718. 'need': ['#cbname#', 'setjmp.h'],
  719. '_check':isexternal
  720. },
  721. {
  722. 'frompyobj': [{l_not(isintent_callback): """\
  723. if(F2PyCapsule_Check(#varname#_capi)) {
  724. #varname#_cptr = F2PyCapsule_AsVoidPtr(#varname#_capi);
  725. } else {
  726. #varname#_cptr = #cbname#;
  727. }
  728. """}, {isintent_callback: """\
  729. if (#varname#_capi==Py_None) {
  730. #varname#_capi = PyObject_GetAttrString(#modulename#_module,\"#varname#\");
  731. if (#varname#_capi) {
  732. if (#varname#_xa_capi==NULL) {
  733. if (PyObject_HasAttrString(#modulename#_module,\"#varname#_extra_args\")) {
  734. PyObject* capi_tmp = PyObject_GetAttrString(#modulename#_module,\"#varname#_extra_args\");
  735. if (capi_tmp)
  736. #varname#_xa_capi = (PyTupleObject *)PySequence_Tuple(capi_tmp);
  737. else
  738. #varname#_xa_capi = (PyTupleObject *)Py_BuildValue(\"()\");
  739. if (#varname#_xa_capi==NULL) {
  740. PyErr_SetString(#modulename#_error,\"Failed to convert #modulename#.#varname#_extra_args to tuple.\\n\");
  741. return NULL;
  742. }
  743. }
  744. }
  745. }
  746. if (#varname#_capi==NULL) {
  747. PyErr_SetString(#modulename#_error,\"Callback #varname# not defined (as an argument or module #modulename# attribute).\\n\");
  748. return NULL;
  749. }
  750. }
  751. """},
  752. """\
  753. \t#varname#_nofargs_capi = #cbname#_nofargs;
  754. \tif (create_cb_arglist(#varname#_capi,#varname#_xa_capi,#maxnofargs#,#nofoptargs#,&#cbname#_nofargs,&#varname#_args_capi,\"failed in processing argument list for call-back #varname#.\")) {
  755. \t\tjmp_buf #varname#_jmpbuf;""",
  756. {debugcapi: ["""\
  757. \t\tfprintf(stderr,\"debug-capi:Assuming %d arguments; at most #maxnofargs#(-#nofoptargs#) is expected.\\n\",#cbname#_nofargs);
  758. \t\tCFUNCSMESSPY(\"for #varname#=\",#cbname#_capi);""",
  759. {l_not(isintent_callback): """\t\tfprintf(stderr,\"#vardebugshowvalue# (call-back in C).\\n\",#cbname#);"""}]},
  760. """\
  761. \t\tCFUNCSMESS(\"Saving jmpbuf for `#varname#`.\\n\");
  762. \t\tSWAP(#varname#_capi,#cbname#_capi,PyObject);
  763. \t\tSWAP(#varname#_args_capi,#cbname#_args_capi,PyTupleObject);
  764. \t\tmemcpy(&#varname#_jmpbuf,&#cbname#_jmpbuf,sizeof(jmp_buf));""",
  765. ],
  766. 'cleanupfrompyobj':
  767. """\
  768. \t\tCFUNCSMESS(\"Restoring jmpbuf for `#varname#`.\\n\");
  769. \t\t#cbname#_capi = #varname#_capi;
  770. \t\tPy_DECREF(#cbname#_args_capi);
  771. \t\t#cbname#_args_capi = #varname#_args_capi;
  772. \t\t#cbname#_nofargs = #varname#_nofargs_capi;
  773. \t\tmemcpy(&#cbname#_jmpbuf,&#varname#_jmpbuf,sizeof(jmp_buf));
  774. \t}""",
  775. 'need': ['SWAP', 'create_cb_arglist'],
  776. '_check':isexternal,
  777. '_depend':''
  778. },
  779. # Scalars (not complex)
  780. { # Common
  781. 'decl': '\t#ctype# #varname# = 0;',
  782. 'pyobjfrom': {debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",#varname#);'},
  783. 'callfortran': {isintent_c: '#varname#,', l_not(isintent_c): '&#varname#,'},
  784. 'return': {isintent_out: ',#varname#'},
  785. '_check': l_and(isscalar, l_not(iscomplex))
  786. }, {
  787. 'need': {hasinitvalue: 'math.h'},
  788. '_check': l_and(isscalar, l_not(iscomplex)),
  789. }, { # Not hidden
  790. 'decl': '\tPyObject *#varname#_capi = Py_None;',
  791. 'argformat': {isrequired: 'O'},
  792. 'keyformat': {isoptional: 'O'},
  793. 'args_capi': {isrequired: ',&#varname#_capi'},
  794. 'keys_capi': {isoptional: ',&#varname#_capi'},
  795. 'pyobjfrom': {isintent_inout: """\
  796. \tf2py_success = try_pyarr_from_#ctype#(#varname#_capi,&#varname#);
  797. \tif (f2py_success) {"""},
  798. 'closepyobjfrom': {isintent_inout: "\t} /*if (f2py_success) of #varname# pyobjfrom*/"},
  799. 'need': {isintent_inout: 'try_pyarr_from_#ctype#'},
  800. '_check': l_and(isscalar, l_not(iscomplex), isintent_nothide)
  801. }, {
  802. 'frompyobj': [
  803. # hasinitvalue...
  804. # if pyobj is None:
  805. # varname = init
  806. # else
  807. # from_pyobj(varname)
  808. #
  809. # isoptional and noinitvalue...
  810. # if pyobj is not None:
  811. # from_pyobj(varname)
  812. # else:
  813. # varname is uninitialized
  814. #
  815. # ...
  816. # from_pyobj(varname)
  817. #
  818. {hasinitvalue: '\tif (#varname#_capi == Py_None) #varname# = #init#; else',
  819. '_depend': ''},
  820. {l_and(isoptional, l_not(hasinitvalue)): '\tif (#varname#_capi != Py_None)',
  821. '_depend': ''},
  822. {l_not(islogical): '''\
  823. \t\tf2py_success = #ctype#_from_pyobj(&#varname#,#varname#_capi,"#pyname#() #nth# (#varname#) can\'t be converted to #ctype#");
  824. \tif (f2py_success) {'''},
  825. {islogical: '''\
  826. \t\t#varname# = (#ctype#)PyObject_IsTrue(#varname#_capi);
  827. \t\tf2py_success = 1;
  828. \tif (f2py_success) {'''},
  829. ],
  830. 'cleanupfrompyobj': '\t} /*if (f2py_success) of #varname#*/',
  831. 'need': {l_not(islogical): '#ctype#_from_pyobj'},
  832. '_check': l_and(isscalar, l_not(iscomplex), isintent_nothide),
  833. '_depend': ''
  834. }, { # Hidden
  835. 'frompyobj': {hasinitvalue: '\t#varname# = #init#;'},
  836. 'need': typedef_need_dict,
  837. '_check': l_and(isscalar, l_not(iscomplex), isintent_hide),
  838. '_depend': ''
  839. }, { # Common
  840. 'frompyobj': {debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",#varname#);'},
  841. '_check': l_and(isscalar, l_not(iscomplex)),
  842. '_depend': ''
  843. },
  844. # Complex scalars
  845. { # Common
  846. 'decl': '\t#ctype# #varname#;',
  847. 'callfortran': {isintent_c: '#varname#,', l_not(isintent_c): '&#varname#,'},
  848. 'pyobjfrom': {debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",#varname#.r,#varname#.i);'},
  849. 'return': {isintent_out: ',#varname#_capi'},
  850. '_check': iscomplex
  851. }, { # Not hidden
  852. 'decl': '\tPyObject *#varname#_capi = Py_None;',
  853. 'argformat': {isrequired: 'O'},
  854. 'keyformat': {isoptional: 'O'},
  855. 'args_capi': {isrequired: ',&#varname#_capi'},
  856. 'keys_capi': {isoptional: ',&#varname#_capi'},
  857. 'need': {isintent_inout: 'try_pyarr_from_#ctype#'},
  858. 'pyobjfrom': {isintent_inout: """\
  859. \t\tf2py_success = try_pyarr_from_#ctype#(#varname#_capi,&#varname#);
  860. \t\tif (f2py_success) {"""},
  861. 'closepyobjfrom': {isintent_inout: "\t\t} /*if (f2py_success) of #varname# pyobjfrom*/"},
  862. '_check': l_and(iscomplex, isintent_nothide)
  863. }, {
  864. 'frompyobj': [{hasinitvalue: '\tif (#varname#_capi==Py_None) {#varname#.r = #init.r#, #varname#.i = #init.i#;} else'},
  865. {l_and(isoptional, l_not(hasinitvalue))
  866. : '\tif (#varname#_capi != Py_None)'},
  867. '\t\tf2py_success = #ctype#_from_pyobj(&#varname#,#varname#_capi,"#pyname#() #nth# (#varname#) can\'t be converted to #ctype#");'
  868. '\n\tif (f2py_success) {'],
  869. 'cleanupfrompyobj': '\t} /*if (f2py_success) of #varname# frompyobj*/',
  870. 'need': ['#ctype#_from_pyobj'],
  871. '_check': l_and(iscomplex, isintent_nothide),
  872. '_depend': ''
  873. }, { # Hidden
  874. 'decl': {isintent_out: '\tPyObject *#varname#_capi = Py_None;'},
  875. '_check': l_and(iscomplex, isintent_hide)
  876. }, {
  877. 'frompyobj': {hasinitvalue: '\t#varname#.r = #init.r#, #varname#.i = #init.i#;'},
  878. '_check': l_and(iscomplex, isintent_hide),
  879. '_depend': ''
  880. }, { # Common
  881. 'pyobjfrom': {isintent_out: '\t#varname#_capi = pyobj_from_#ctype#1(#varname#);'},
  882. 'need': ['pyobj_from_#ctype#1'],
  883. '_check': iscomplex
  884. }, {
  885. 'frompyobj': {debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",#varname#.r,#varname#.i);'},
  886. '_check': iscomplex,
  887. '_depend': ''
  888. },
  889. # String
  890. { # Common
  891. 'decl': ['\t#ctype# #varname# = NULL;',
  892. '\tint slen(#varname#);',
  893. '\tPyObject *#varname#_capi = Py_None;'],
  894. 'callfortran':'#varname#,',
  895. 'callfortranappend':'slen(#varname#),',
  896. 'pyobjfrom':{debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",slen(#varname#),#varname#);'},
  897. 'return': {isintent_out: ',#varname#'},
  898. 'need': ['len..'], # 'STRINGFREE'],
  899. '_check':isstring
  900. }, { # Common
  901. 'frompyobj': """\
  902. \tslen(#varname#) = #length#;
  903. \tf2py_success = #ctype#_from_pyobj(&#varname#,&slen(#varname#),#init#,#varname#_capi,\"#ctype#_from_pyobj failed in converting #nth# `#varname#\' of #pyname# to C #ctype#\");
  904. \tif (f2py_success) {""",
  905. 'cleanupfrompyobj': """\
  906. \t\tSTRINGFREE(#varname#);
  907. \t} /*if (f2py_success) of #varname#*/""",
  908. 'need': ['#ctype#_from_pyobj', 'len..', 'STRINGFREE'],
  909. '_check':isstring,
  910. '_depend':''
  911. }, { # Not hidden
  912. 'argformat': {isrequired: 'O'},
  913. 'keyformat': {isoptional: 'O'},
  914. 'args_capi': {isrequired: ',&#varname#_capi'},
  915. 'keys_capi': {isoptional: ',&#varname#_capi'},
  916. 'pyobjfrom': {isintent_inout: '''\
  917. \tf2py_success = try_pyarr_from_#ctype#(#varname#_capi,#varname#);
  918. \tif (f2py_success) {'''},
  919. 'closepyobjfrom': {isintent_inout: '\t} /*if (f2py_success) of #varname# pyobjfrom*/'},
  920. 'need': {isintent_inout: 'try_pyarr_from_#ctype#'},
  921. '_check': l_and(isstring, isintent_nothide)
  922. }, { # Hidden
  923. '_check': l_and(isstring, isintent_hide)
  924. }, {
  925. 'frompyobj': {debugcapi: '\tfprintf(stderr,"#vardebugshowvalue#\\n",slen(#varname#),#varname#);'},
  926. '_check': isstring,
  927. '_depend': ''
  928. },
  929. # Array
  930. { # Common
  931. 'decl': ['\t#ctype# *#varname# = NULL;',
  932. '\tnpy_intp #varname#_Dims[#rank#] = {#rank*[-1]#};',
  933. '\tconst int #varname#_Rank = #rank#;',
  934. '\tPyArrayObject *capi_#varname#_tmp = NULL;',
  935. '\tint capi_#varname#_intent = 0;',
  936. ],
  937. 'callfortran':'#varname#,',
  938. 'return':{isintent_out: ',capi_#varname#_tmp'},
  939. 'need': 'len..',
  940. '_check': isarray
  941. }, { # intent(overwrite) array
  942. 'decl': '\tint capi_overwrite_#varname# = 1;',
  943. 'kwlistxa': '"overwrite_#varname#",',
  944. 'xaformat': 'i',
  945. 'keys_xa': ',&capi_overwrite_#varname#',
  946. 'docsignxa': 'overwrite_#varname#=1,',
  947. 'docsignxashort': 'overwrite_#varname#,',
  948. 'docstropt': 'overwrite_#varname# : input int, optional\\n Default: 1',
  949. '_check': l_and(isarray, isintent_overwrite),
  950. }, {
  951. 'frompyobj': '\tcapi_#varname#_intent |= (capi_overwrite_#varname#?0:F2PY_INTENT_COPY);',
  952. '_check': l_and(isarray, isintent_overwrite),
  953. '_depend': '',
  954. },
  955. { # intent(copy) array
  956. 'decl': '\tint capi_overwrite_#varname# = 0;',
  957. 'kwlistxa': '"overwrite_#varname#",',
  958. 'xaformat': 'i',
  959. 'keys_xa': ',&capi_overwrite_#varname#',
  960. 'docsignxa': 'overwrite_#varname#=0,',
  961. 'docsignxashort': 'overwrite_#varname#,',
  962. 'docstropt': 'overwrite_#varname# : input int, optional\\n Default: 0',
  963. '_check': l_and(isarray, isintent_copy),
  964. }, {
  965. 'frompyobj': '\tcapi_#varname#_intent |= (capi_overwrite_#varname#?0:F2PY_INTENT_COPY);',
  966. '_check': l_and(isarray, isintent_copy),
  967. '_depend': '',
  968. }, {
  969. 'need': [{hasinitvalue: 'forcomb'}, {hasinitvalue: 'CFUNCSMESS'}],
  970. '_check': isarray,
  971. '_depend': ''
  972. }, { # Not hidden
  973. 'decl': '\tPyObject *#varname#_capi = Py_None;',
  974. 'argformat': {isrequired: 'O'},
  975. 'keyformat': {isoptional: 'O'},
  976. 'args_capi': {isrequired: ',&#varname#_capi'},
  977. 'keys_capi': {isoptional: ',&#varname#_capi'},
  978. '_check': l_and(isarray, isintent_nothide)
  979. }, {
  980. 'frompyobj': ['\t#setdims#;',
  981. '\tcapi_#varname#_intent |= #intent#;',
  982. {isintent_hide:
  983. '\tcapi_#varname#_tmp = array_from_pyobj(#atype#,#varname#_Dims,#varname#_Rank,capi_#varname#_intent,Py_None);'},
  984. {isintent_nothide:
  985. '\tcapi_#varname#_tmp = array_from_pyobj(#atype#,#varname#_Dims,#varname#_Rank,capi_#varname#_intent,#varname#_capi);'},
  986. """\
  987. \tif (capi_#varname#_tmp == NULL) {
  988. \t\tif (!PyErr_Occurred())
  989. \t\t\tPyErr_SetString(#modulename#_error,\"failed in converting #nth# `#varname#\' of #pyname# to C/Fortran array\" );
  990. \t} else {
  991. \t\t#varname# = (#ctype# *)(PyArray_DATA(capi_#varname#_tmp));
  992. """,
  993. {hasinitvalue: [
  994. {isintent_nothide:
  995. '\tif (#varname#_capi == Py_None) {'},
  996. {isintent_hide: '\t{'},
  997. {iscomplexarray: '\t\t#ctype# capi_c;'},
  998. """\
  999. \t\tint *_i,capi_i=0;
  1000. \t\tCFUNCSMESS(\"#name#: Initializing #varname#=#init#\\n\");
  1001. \t\tif (initforcomb(PyArray_DIMS(capi_#varname#_tmp),PyArray_NDIM(capi_#varname#_tmp),1)) {
  1002. \t\t\twhile ((_i = nextforcomb()))
  1003. \t\t\t\t#varname#[capi_i++] = #init#; /* fortran way */
  1004. \t\t} else {
  1005. \t\t\tif (!PyErr_Occurred())
  1006. \t\t\t\tPyErr_SetString(#modulename#_error,\"Initialization of #nth# #varname# failed (initforcomb).\");
  1007. \t\t\tf2py_success = 0;
  1008. \t\t}
  1009. \t}
  1010. \tif (f2py_success) {"""]},
  1011. ],
  1012. 'cleanupfrompyobj': [ # note that this list will be reversed
  1013. '\t} /*if (capi_#varname#_tmp == NULL) ... else of #varname#*/',
  1014. {l_not(l_or(isintent_out, isintent_hide)): """\
  1015. \tif((PyObject *)capi_#varname#_tmp!=#varname#_capi) {
  1016. \t\tPy_XDECREF(capi_#varname#_tmp); }"""},
  1017. {l_and(isintent_hide, l_not(isintent_out))
  1018. : """\t\tPy_XDECREF(capi_#varname#_tmp);"""},
  1019. {hasinitvalue: '\t} /*if (f2py_success) of #varname# init*/'},
  1020. ],
  1021. '_check': isarray,
  1022. '_depend': ''
  1023. },
  1024. # Scalararray
  1025. { # Common
  1026. '_check': l_and(isarray, l_not(iscomplexarray))
  1027. }, { # Not hidden
  1028. '_check': l_and(isarray, l_not(iscomplexarray), isintent_nothide)
  1029. },
  1030. # Integer*1 array
  1031. {'need': '#ctype#',
  1032. '_check': isint1array,
  1033. '_depend': ''
  1034. },
  1035. # Integer*-1 array
  1036. {'need': '#ctype#',
  1037. '_check': isunsigned_chararray,
  1038. '_depend': ''
  1039. },
  1040. # Integer*-2 array
  1041. {'need': '#ctype#',
  1042. '_check': isunsigned_shortarray,
  1043. '_depend': ''
  1044. },
  1045. # Integer*-8 array
  1046. {'need': '#ctype#',
  1047. '_check': isunsigned_long_longarray,
  1048. '_depend': ''
  1049. },
  1050. # Complexarray
  1051. {'need': '#ctype#',
  1052. '_check': iscomplexarray,
  1053. '_depend': ''
  1054. },
  1055. # Stringarray
  1056. {
  1057. 'callfortranappend': {isarrayofstrings: 'flen(#varname#),'},
  1058. 'need': 'string',
  1059. '_check': isstringarray
  1060. }
  1061. ]
  1062. ################# Rules for checking ###############
  1063. check_rules = [
  1064. {
  1065. 'frompyobj': {debugcapi: '\tfprintf(stderr,\"debug-capi:Checking `#check#\'\\n\");'},
  1066. 'need': 'len..'
  1067. }, {
  1068. 'frompyobj': '\tCHECKSCALAR(#check#,\"#check#\",\"#nth# #varname#\",\"#varshowvalue#\",#varname#) {',
  1069. 'cleanupfrompyobj': '\t} /*CHECKSCALAR(#check#)*/',
  1070. 'need': 'CHECKSCALAR',
  1071. '_check': l_and(isscalar, l_not(iscomplex)),
  1072. '_break': ''
  1073. }, {
  1074. 'frompyobj': '\tCHECKSTRING(#check#,\"#check#\",\"#nth# #varname#\",\"#varshowvalue#\",#varname#) {',
  1075. 'cleanupfrompyobj': '\t} /*CHECKSTRING(#check#)*/',
  1076. 'need': 'CHECKSTRING',
  1077. '_check': isstring,
  1078. '_break': ''
  1079. }, {
  1080. 'need': 'CHECKARRAY',
  1081. 'frompyobj': '\tCHECKARRAY(#check#,\"#check#\",\"#nth# #varname#\") {',
  1082. 'cleanupfrompyobj': '\t} /*CHECKARRAY(#check#)*/',
  1083. '_check': isarray,
  1084. '_break': ''
  1085. }, {
  1086. 'need': 'CHECKGENERIC',
  1087. 'frompyobj': '\tCHECKGENERIC(#check#,\"#check#\",\"#nth# #varname#\") {',
  1088. 'cleanupfrompyobj': '\t} /*CHECKGENERIC(#check#)*/',
  1089. }
  1090. ]
  1091. ########## Applying the rules. No need to modify what follows #############
  1092. #################### Build C/API module #######################
  1093. def buildmodule(m, um):
  1094. """
  1095. Return
  1096. """
  1097. global f2py_version, options
  1098. outmess('\tBuilding module "%s"...\n' % (m['name']))
  1099. ret = {}
  1100. mod_rules = defmod_rules[:]
  1101. vrd = capi_maps.modsign2map(m)
  1102. rd = dictappend({'f2py_version': f2py_version}, vrd)
  1103. funcwrappers = []
  1104. funcwrappers2 = [] # F90 codes
  1105. for n in m['interfaced']:
  1106. nb = None
  1107. for bi in m['body']:
  1108. if not bi['block'] == 'interface':
  1109. errmess('buildmodule: Expected interface block. Skipping.\n')
  1110. continue
  1111. for b in bi['body']:
  1112. if b['name'] == n:
  1113. nb = b
  1114. break
  1115. if not nb:
  1116. errmess(
  1117. 'buildmodule: Could not found the body of interfaced routine "%s". Skipping.\n' % (n))
  1118. continue
  1119. nb_list = [nb]
  1120. if 'entry' in nb:
  1121. for k, a in nb['entry'].items():
  1122. nb1 = copy.deepcopy(nb)
  1123. del nb1['entry']
  1124. nb1['name'] = k
  1125. nb1['args'] = a
  1126. nb_list.append(nb1)
  1127. for nb in nb_list:
  1128. api, wrap = buildapi(nb)
  1129. if wrap:
  1130. if ismoduleroutine(nb):
  1131. funcwrappers2.append(wrap)
  1132. else:
  1133. funcwrappers.append(wrap)
  1134. ar = applyrules(api, vrd)
  1135. rd = dictappend(rd, ar)
  1136. # Construct COMMON block support
  1137. cr, wrap = common_rules.buildhooks(m)
  1138. if wrap:
  1139. funcwrappers.append(wrap)
  1140. ar = applyrules(cr, vrd)
  1141. rd = dictappend(rd, ar)
  1142. # Construct F90 module support
  1143. mr, wrap = f90mod_rules.buildhooks(m)
  1144. if wrap:
  1145. funcwrappers2.append(wrap)
  1146. ar = applyrules(mr, vrd)
  1147. rd = dictappend(rd, ar)
  1148. for u in um:
  1149. ar = use_rules.buildusevars(u, m['use'][u['name']])
  1150. rd = dictappend(rd, ar)
  1151. needs = cfuncs.get_needs()
  1152. code = {}
  1153. for n in needs.keys():
  1154. code[n] = []
  1155. for k in needs[n]:
  1156. c = ''
  1157. if k in cfuncs.includes0:
  1158. c = cfuncs.includes0[k]
  1159. elif k in cfuncs.includes:
  1160. c = cfuncs.includes[k]
  1161. elif k in cfuncs.userincludes:
  1162. c = cfuncs.userincludes[k]
  1163. elif k in cfuncs.typedefs:
  1164. c = cfuncs.typedefs[k]
  1165. elif k in cfuncs.typedefs_generated:
  1166. c = cfuncs.typedefs_generated[k]
  1167. elif k in cfuncs.cppmacros:
  1168. c = cfuncs.cppmacros[k]
  1169. elif k in cfuncs.cfuncs:
  1170. c = cfuncs.cfuncs[k]
  1171. elif k in cfuncs.callbacks:
  1172. c = cfuncs.callbacks[k]
  1173. elif k in cfuncs.f90modhooks:
  1174. c = cfuncs.f90modhooks[k]
  1175. elif k in cfuncs.commonhooks:
  1176. c = cfuncs.commonhooks[k]
  1177. else:
  1178. errmess('buildmodule: unknown need %s.\n' % (repr(k)))
  1179. continue
  1180. code[n].append(c)
  1181. mod_rules.append(code)
  1182. for r in mod_rules:
  1183. if ('_check' in r and r['_check'](m)) or ('_check' not in r):
  1184. ar = applyrules(r, vrd, m)
  1185. rd = dictappend(rd, ar)
  1186. ar = applyrules(module_rules, rd)
  1187. fn = os.path.join(options['buildpath'], vrd['coutput'])
  1188. ret['csrc'] = fn
  1189. f = open(fn, 'w')
  1190. f.write(ar['modulebody'].replace('\t', 2 * ' '))
  1191. f.close()
  1192. outmess('\tWrote C/API module "%s" to file "%s"\n' % (m['name'], fn))
  1193. if options['dorestdoc']:
  1194. fn = os.path.join(
  1195. options['buildpath'], vrd['modulename'] + 'module.rest')
  1196. f = open(fn, 'w')
  1197. f.write('.. -*- rest -*-\n')
  1198. f.write('\n'.join(ar['restdoc']))
  1199. f.close()
  1200. outmess('\tReST Documentation is saved to file "%s/%smodule.rest"\n' %
  1201. (options['buildpath'], vrd['modulename']))
  1202. if options['dolatexdoc']:
  1203. fn = os.path.join(
  1204. options['buildpath'], vrd['modulename'] + 'module.tex')
  1205. ret['ltx'] = fn
  1206. f = open(fn, 'w')
  1207. f.write(
  1208. '%% This file is auto-generated with f2py (version:%s)\n' % (f2py_version))
  1209. if 'shortlatex' not in options:
  1210. f.write(
  1211. '\\documentclass{article}\n\\usepackage{a4wide}\n\\begin{document}\n\\tableofcontents\n\n')
  1212. f.write('\n'.join(ar['latexdoc']))
  1213. if 'shortlatex' not in options:
  1214. f.write('\\end{document}')
  1215. f.close()
  1216. outmess('\tDocumentation is saved to file "%s/%smodule.tex"\n' %
  1217. (options['buildpath'], vrd['modulename']))
  1218. if funcwrappers:
  1219. wn = os.path.join(options['buildpath'], vrd['f2py_wrapper_output'])
  1220. ret['fsrc'] = wn
  1221. f = open(wn, 'w')
  1222. f.write('C -*- fortran -*-\n')
  1223. f.write(
  1224. 'C This file is autogenerated with f2py (version:%s)\n' % (f2py_version))
  1225. f.write(
  1226. 'C It contains Fortran 77 wrappers to fortran functions.\n')
  1227. lines = []
  1228. for l in ('\n\n'.join(funcwrappers) + '\n').split('\n'):
  1229. if l and l[0] == ' ':
  1230. while len(l) >= 66:
  1231. lines.append(l[:66] + '\n &')
  1232. l = l[66:]
  1233. lines.append(l + '\n')
  1234. else:
  1235. lines.append(l + '\n')
  1236. lines = ''.join(lines).replace('\n &\n', '\n')
  1237. f.write(lines)
  1238. f.close()
  1239. outmess('\tFortran 77 wrappers are saved to "%s"\n' % (wn))
  1240. if funcwrappers2:
  1241. wn = os.path.join(
  1242. options['buildpath'], '%s-f2pywrappers2.f90' % (vrd['modulename']))
  1243. ret['fsrc'] = wn
  1244. f = open(wn, 'w')
  1245. f.write('! -*- f90 -*-\n')
  1246. f.write(
  1247. '! This file is autogenerated with f2py (version:%s)\n' % (f2py_version))
  1248. f.write(
  1249. '! It contains Fortran 90 wrappers to fortran functions.\n')
  1250. lines = []
  1251. for l in ('\n\n'.join(funcwrappers2) + '\n').split('\n'):
  1252. if len(l) > 72 and l[0] == ' ':
  1253. lines.append(l[:72] + '&\n &')
  1254. l = l[72:]
  1255. while len(l) > 66:
  1256. lines.append(l[:66] + '&\n &')
  1257. l = l[66:]
  1258. lines.append(l + '\n')
  1259. else:
  1260. lines.append(l + '\n')
  1261. lines = ''.join(lines).replace('\n &\n', '\n')
  1262. f.write(lines)
  1263. f.close()
  1264. outmess('\tFortran 90 wrappers are saved to "%s"\n' % (wn))
  1265. return ret
  1266. ################## Build C/API function #############
  1267. stnd = {1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th',
  1268. 6: 'th', 7: 'th', 8: 'th', 9: 'th', 0: 'th'}
  1269. def buildapi(rout):
  1270. rout, wrap = func2subr.assubr(rout)
  1271. args, depargs = getargs2(rout)
  1272. capi_maps.depargs = depargs
  1273. var = rout['vars']
  1274. if ismoduleroutine(rout):
  1275. outmess('\t\t\tConstructing wrapper function "%s.%s"...\n' %
  1276. (rout['modulename'], rout['name']))
  1277. else:
  1278. outmess('\t\tConstructing wrapper function "%s"...\n' % (rout['name']))
  1279. # Routine
  1280. vrd = capi_maps.routsign2map(rout)
  1281. rd = dictappend({}, vrd)
  1282. for r in rout_rules:
  1283. if ('_check' in r and r['_check'](rout)) or ('_check' not in r):
  1284. ar = applyrules(r, vrd, rout)
  1285. rd = dictappend(rd, ar)
  1286. # Args
  1287. nth, nthk = 0, 0
  1288. savevrd = {}
  1289. for a in args:
  1290. vrd = capi_maps.sign2map(a, var[a])
  1291. if isintent_aux(var[a]):
  1292. _rules = aux_rules
  1293. else:
  1294. _rules = arg_rules
  1295. if not isintent_hide(var[a]):
  1296. if not isoptional(var[a]):
  1297. nth = nth + 1
  1298. vrd['nth'] = repr(nth) + stnd[nth % 10] + ' argument'
  1299. else:
  1300. nthk = nthk + 1
  1301. vrd['nth'] = repr(nthk) + stnd[nthk % 10] + ' keyword'
  1302. else:
  1303. vrd['nth'] = 'hidden'
  1304. savevrd[a] = vrd
  1305. for r in _rules:
  1306. if '_depend' in r:
  1307. continue
  1308. if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
  1309. ar = applyrules(r, vrd, var[a])
  1310. rd = dictappend(rd, ar)
  1311. if '_break' in r:
  1312. break
  1313. for a in depargs:
  1314. if isintent_aux(var[a]):
  1315. _rules = aux_rules
  1316. else:
  1317. _rules = arg_rules
  1318. vrd = savevrd[a]
  1319. for r in _rules:
  1320. if '_depend' not in r:
  1321. continue
  1322. if ('_check' in r and r['_check'](var[a])) or ('_check' not in r):
  1323. ar = applyrules(r, vrd, var[a])
  1324. rd = dictappend(rd, ar)
  1325. if '_break' in r:
  1326. break
  1327. if 'check' in var[a]:
  1328. for c in var[a]['check']:
  1329. vrd['check'] = c
  1330. ar = applyrules(check_rules, vrd, var[a])
  1331. rd = dictappend(rd, ar)
  1332. if isinstance(rd['cleanupfrompyobj'], list):
  1333. rd['cleanupfrompyobj'].reverse()
  1334. if isinstance(rd['closepyobjfrom'], list):
  1335. rd['closepyobjfrom'].reverse()
  1336. rd['docsignature'] = stripcomma(replace('#docsign##docsignopt##docsignxa#',
  1337. {'docsign': rd['docsign'],
  1338. 'docsignopt': rd['docsignopt'],
  1339. 'docsignxa': rd['docsignxa']}))
  1340. optargs = stripcomma(replace('#docsignopt##docsignxa#',
  1341. {'docsignxa': rd['docsignxashort'],
  1342. 'docsignopt': rd['docsignoptshort']}
  1343. ))
  1344. if optargs == '':
  1345. rd['docsignatureshort'] = stripcomma(
  1346. replace('#docsign#', {'docsign': rd['docsign']}))
  1347. else:
  1348. rd['docsignatureshort'] = replace('#docsign#[#docsignopt#]',
  1349. {'docsign': rd['docsign'],
  1350. 'docsignopt': optargs,
  1351. })
  1352. rd['latexdocsignatureshort'] = rd['docsignatureshort'].replace('_', '\\_')
  1353. rd['latexdocsignatureshort'] = rd[
  1354. 'latexdocsignatureshort'].replace(',', ', ')
  1355. cfs = stripcomma(replace('#callfortran##callfortranappend#', {
  1356. 'callfortran': rd['callfortran'], 'callfortranappend': rd['callfortranappend']}))
  1357. if len(rd['callfortranappend']) > 1:
  1358. rd['callcompaqfortran'] = stripcomma(replace('#callfortran# 0,#callfortranappend#', {
  1359. 'callfortran': rd['callfortran'], 'callfortranappend': rd['callfortranappend']}))
  1360. else:
  1361. rd['callcompaqfortran'] = cfs
  1362. rd['callfortran'] = cfs
  1363. if isinstance(rd['docreturn'], list):
  1364. rd['docreturn'] = stripcomma(
  1365. replace('#docreturn#', {'docreturn': rd['docreturn']})) + ' = '
  1366. rd['docstrsigns'] = []
  1367. rd['latexdocstrsigns'] = []
  1368. for k in ['docstrreq', 'docstropt', 'docstrout', 'docstrcbs']:
  1369. if k in rd and isinstance(rd[k], list):
  1370. rd['docstrsigns'] = rd['docstrsigns'] + rd[k]
  1371. k = 'latex' + k
  1372. if k in rd and isinstance(rd[k], list):
  1373. rd['latexdocstrsigns'] = rd['latexdocstrsigns'] + rd[k][0:1] +\
  1374. ['\\begin{description}'] + rd[k][1:] +\
  1375. ['\\end{description}']
  1376. # Workaround for Python 2.6, 2.6.1 bug: http://bugs.python.org/issue4720
  1377. if rd['keyformat'] or rd['xaformat']:
  1378. argformat = rd['argformat']
  1379. if isinstance(argformat, list):
  1380. argformat.append('|')
  1381. else:
  1382. assert isinstance(argformat, str), repr(
  1383. (argformat, type(argformat)))
  1384. rd['argformat'] += '|'
  1385. ar = applyrules(routine_rules, rd)
  1386. if ismoduleroutine(rout):
  1387. outmess('\t\t\t %s\n' % (ar['docshort']))
  1388. else:
  1389. outmess('\t\t %s\n' % (ar['docshort']))
  1390. return ar, wrap
  1391. #################### EOF rules.py #######################