a ;jgm@sdZddlZddlZddlZddlZddlmZddlmZddl Z ddl Z ddl m Z ddl m Z ddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZddlmZejZejZejZejZejdkZdededZddZddZ ddZ!ddZ"ddZ#d3ddZ$d d!Z%Gd"d#d#e j&Z'd$d%Z(d&d'Z)d(d)Z*d*d+Z+d,d-Z,d4d/d0Z-d1d2Z.dS)5ae f2py2e - Fortran to Python C/API generator. 2nd Edition. See __usage__ below. Copyright 1999 -- 2011 Pearu Peterson all rights reserved. Copyright 2011 -- present NumPy Developers. Permission to use, modify, and distribute this software is given under the terms of the NumPy License. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. N)Path) dropwhile) crackfortran)rules)cb_rules)auxfuncs)cfuncs) f90mod_rules) __version__) capi_maps)errmess)f2py_build_generator) ayUsage: 1) To construct extension module sources: f2py [] [[[only:]||[skip:]] \ ] \ [: ...] 2) To compile fortran files and build extension modules: f2py -c [, , ] 3) To generate signature files: f2py -h ...< same options as in (1) > Description: This program generates a Python C/API file (module.c) that contains wrappers for given fortran functions so that they can be called from Python. With the -c option the corresponding extension modules are built. Options: -h Write signatures of the fortran routines to file and exit. You can then edit and use it instead of . If ==stdout then the signatures are printed to stdout. Names of fortran routines for which Python C/API functions will be generated. Default is all that are found in . Paths to fortran/signature files that will be scanned for in order to determine their signatures. skip: Ignore fortran functions that follow until `:'. only: Use only fortran functions that follow until `:'. : Get back to mode. -m Name of the module; f2py generates a Python/C API file module.c or extension module . Default is 'untitled'. '-include
' Writes additional headers in the C wrapper, can be passed multiple times, generates #include
each time. --[no-]lower Do [not] lower the cases in . By default, --lower is assumed with -h key, and --no-lower without -h key. --build-dir All f2py generated files are created in . Default is tempfile.mkdtemp(). --overwrite-signature Overwrite existing signature file. --[no-]latex-doc Create (or not) module.tex. Default is --no-latex-doc. --short-latex Create 'incomplete' LaTeX document (without commands \documentclass, \tableofcontents, and \begin{document}, \end{document}). --[no-]rest-doc Create (or not) module.rst. Default is --no-rest-doc. --debug-capi Create C/API code that reports the state of the wrappers during runtime. Useful for debugging. --[no-]wrap-functions Create Fortran subroutine wrappers to Fortran 77 functions. --wrap-functions is default because it ensures maximum portability/compiler independence. --include-paths ::... Search include files from the given directories. --help-link [..] List system resources found by system_info.py. See also --link- switch below. [..] is optional list of resources names. E.g. try 'f2py --help-link lapack_opt'. --f2cmap Load Fortran-to-Python KIND specification from the given file. Default: .f2py_f2cmap in current directory. --quiet Run quietly. --verbose Run with extra verbosity. --skip-empty-wrappers Only generate wrapper files when needed. -v Print f2py version ID and exit. build backend options (only effective with -c) [NO_MESON] is used to indicate an option not meant to be used with the meson backend or above Python 3.12: --fcompiler= Specify Fortran compiler type by vendor [NO_MESON] --compiler= Specify distutils C compiler type [NO_MESON] --help-fcompiler List available Fortran compilers and exit [NO_MESON] --f77exec= Specify the path to F77 compiler [NO_MESON] --f90exec= Specify the path to F90 compiler [NO_MESON] --f77flags= Specify F77 compiler flags --f90flags= Specify F90 compiler flags --opt= Specify optimization flags [NO_MESON] --arch= Specify architecture specific optimization flags [NO_MESON] --noopt Compile without optimization [NO_MESON] --noarch Compile without arch-dependent optimization [NO_MESON] --debug Compile with debugging information --dep Specify a meson dependency for the module. This may be passed multiple times for multiple dependencies. Dependencies are stored in a list for further processing. Example: --dep lapack --dep scalapack This will identify "lapack" and "scalapack" as dependencies and remove them from argv, leaving a dependencies list containing ["lapack", "scalapack"]. --backend Specify the build backend for the compilation process. The supported backends are 'meson' and 'distutils'. If not specified, defaults to 'distutils'. On Python 3.12 or higher, the default is 'meson'. Extra options (only effective with -c): --link- Link extension module with as defined by numpy.distutils/system_info.py. E.g. to link with optimized LAPACK libraries (vecLib on MacOSX, ATLAS elsewhere), use --link-lapack_opt. See also --help-link switch. [NO_MESON] -L/path/to/lib/ -l -D -U -I/path/to/include/ .o .so .a Using the following macros may be required with non-gcc Fortran compilers: -DPREPEND_FORTRAN -DNO_APPEND_FORTRAN -DUPPERCASE_FORTRAN When using -DF2PY_REPORT_ATEXIT, a performance report of F2PY interface is printed out at exit (platforms: Linux). When using -DF2PY_REPORT_ON_ARRAY_COPY=, a message is sent to stderr whenever F2PY interface makes a copy of an array. Integer sets the threshold for array sizes when a message should be shown. Version: z numpy Version: z License: NumPy license (see LICENSE.txt in the NumPy source code) Copyright 1999 -- 2011 Pearu Peterson all rights reserved. Copyright 2011 -- present NumPy Developers. https://numpy.org/doc/stable/f2py/index.html c Csggggf\}}}}d\}}}}} } } } d} d}d}d}d}d}d}t|\}}d\}}|ddd}|D]P}|d krxqh|d krd}qh|d krd}qh|d krd}qh|dd dkr||d dqh|dkrd}qh|dkrd} qh|dkrd}qh|dkrd} qh|dkr| d7} qh|dkr&d}qh|dkr6d}qh|dkrFd}qh|dkrVd}qh|dkrfd}qh|dkrvd}qh|dkrd|d<qh|dkrd} qh|dkrd} qh|dkrd} qh|dkrd|d <qh|d!krd}qh|d"krd}qh|dd#d$krtttqh|d%kr"d}qh|dd d&krltjd'|d(dd)|d dtj|d(d<qh|d*kr|d+}qh|dd,krt d-t |tqh|rd}|}qh|rd}|}qh| rd} |}qh| rd} ||d.<qh| rd} ||d/<qh| rd} ||d0<qh|dkrz8t |Wdn1s<0Y||Wn<t y}z"t d1|d2|d3WYd}~n d}~00qh|dkr||qh|dkrh||qh|s|s|stt ttj|s| std4|t||rtj||}|rNtj|rNd |vrNt d5|t||d6<||d7<| |d8<|dkr|sd|d9<n||d9<|r||d:<|r||d;<|r||d<<|r||d=<||d><||d?<||d@<||dA<||dB<|d0d||fS)CN)rrrrrrrrrTr.)NN) buildpathcoutputf2py_wrapper_outputonly:skip::z--debug---lower --build-dirz --no-lower--quietz --verbosez --latex-docz--no-latex-docz --rest-docz --no-rest-docz--wrap-functionsz--no-wrap-functionsz --short-latexZ shortlatexz --coutputz--f2py-wrapper-output--f2cmapz--overwrite-signaturez h-overwrite-h-mz-vz--show-compilersz-include userincludes z #include z--skip-empty-wrappersF-zUnknown option %s rr f2cmap_filez OSError: z. Skipping file "z". zCreating build directory %s zFSignature file "%s" exists!!! Use --overwrite-signature to overwrite. emptygendebugverbosedo-lowermodule signsfile onlyfuncs skipfuncs dolatexdoc dorestdoc wrapfuncsr include_paths) get_includesappendprint f2py_versionsysexitr Zoutneedsr"r repropenOSError __usage__ospathisdiroutmessmkdirjoinisfile setdefault)Z inputlinefilesr-r,r'ff2Zf3Zf5Zf6Zf8Zf9Zf10r(r&Zdolcr.r/r0rr1r+ modulenameoptionsldetailrK;/usr/local/lib/python3.9/site-packages/numpy/f2py/f2py2e.py scaninputlines                                .       rMcCs~|t_|dt_|dt_d|vr,|dt_d|vr>|dt_d|vrP|dt_|dtjdd<|dt_ t|}d|vrt d |dt |}|dd dd krt j |n8t|dd }||Wdn1s0Y|d dur|D]}d|d|d <qn|D]}|d |d <q"|ddurb|D]}d|d|d<qHn|D]}|d|d<qf|S)Nr'r(r*r-r,r1r)r+zSaving signatures to file "%s" istdoutwrz %smodule.cnamerz%s-f2pywrappers.f)rrHrr'r(Z f77modulenamer-r,r1Z dolowercaser?Z crack2fortranr6rNwriter9)rDrHpostlistZpyfrEmodrKrKrLcallcrackfortranOs<        (rTc Cs^ttdggi}}}|D]p}d|dvr@t|q$d|vr||dD]&}||vrhg||<|||dqT||||dq$i}t||D]\}}||vrtd|ddd||Dfqg} d|vr:|dD]D}||vr"||vr"| || |qtd |d |d qi||<t ||t || q|S) NzBuilding modules... __user__rPusez+ Skipping module "%s" which is used by %s. ,css|]}d|VqdS)z"%s"NrK.0srKrKrL zbuildmodules..z Module "z" uses nonexisting "z" which will be ignored. ) r Z buildcfuncsr?rZbuildcallbackskeysr3ziprAindex dict_appendrZ buildmodule) lstmodulesZmnamesisusedbyitemuretr*rPZumrKrKrL buildmodulesss<     rgcCsP|D]B\}}||vr g||<t|tr<|||||<q|||qdSN)items isinstancelistr3)Zd_outZd_inkvrKrKrLr`s  r`c Cs<ttjtjtj}tj|dd}tj|dd}t }| |\}}t dd|\}}|j rd|vrx|j }n t ||j }|d|g7}t|\} } | t_t| dt| | } i} | D]@} d | vr| d D]&}|| vrg| |<| || d qq| D]b} | d d krd | d vr| d | vrtd| d dddd| | d Dqd| vr| ddkrtdtdtjtjd| dfdS| D]:} | d d krd | vrtdtdt| d q| dt_| t_| dt_ t!| }|D]}t"||||dq|S)au Equivalent to running:: f2py where ``=string.join(,' ')``, but in Python. Unless ``-h`` is used, this function returns a dictionary containing information on generated modules and their dependencies on source files. You cannot build extension modules with this function, that is, using ``-c`` is not allowed. Use the ``compile`` command instead. Examples -------- The command ``f2py -m scalar scalar.f`` can be executed from Python as follows. .. literalinclude:: ../../source/f2py/code/results/run_main_session.dat :language: python srczfortranobject.hzfortranobject.cr[.]pyf([.]src|)rr r%rVrPblockz python modulerUz$Skipping Makefile build for module "z" which is used by {} rWcss|]}d|dVqdS)"NrKrXrKrKrLr[r\zrun_main..r+r(rzKStopping. Edit the signature file and then run f2py on the signature file: z%s %s rNzJTip: If your original code is Fortran source then you must use -m option. z2All blocks must be python module blocks but got %sr'r0)Zcsrch)#rZreset_global_f2py_varsr<r=dirnameabspathr __file__rAmake_f2py_compile_parserparse_known_args filter_files module_namevalidate_modulenamerMrrHr Zload_f2cmap_filerTr]r3r?formatbasenamer6argvr TypeErrorr8Z debugoptionsr r0rgr`)Z comline_listZf2pydirZfobjhsrcZfobjcsrcparserargs pyf_files_modnamerDrHrRrcplistrerfZmnrKrKrLrun_mainsv         rc Csvgg}}t|d|dj}|r0t|}nd}dd|DD]*}||rb|||dqB||qB||fS)z, Filter files by prefix and suffix. z.*z\ZrcSsg|] }|qSrK)strip)rYxrKrKrL r\z filter_files..N)recompilematchlenr3) prefixsuffixrD remove_prefixfilteredrestrindfilerKrKrLrxs   rxcCstjtj|j}|Srh)r<r=rsru)r*prKrKrL get_prefix src@seZdZdddZdS)CombineIncludePathsNcCsdtt|dgpg}|dkr$td|dks4|dkrF||dn ||t|dt|dS)Nr1--include_pathszJUse --include-paths or -I instead of --include_paths which will be removed--include-pathsr)setgetattrr?updatesplitaddsetattrrk)selfr namespacevalues option_stringZinclude_paths_setrKrKrL__call__s zCombineIncludePaths.__call__)N)__name__ __module__ __qualname__rrKrKrKrLrsrcCs@tjdd}|jddtd|jddtd|jddtd|S)NFadd_helpz-Ir1)destactionrr)argparseArgumentParser add_argumentrrrKrKrLinclude_parsers  rcCs>d|}t}||\}}|j}|jdur6g}||fS)N )rArrrwr1)ZilinerrZremainZipathsrKrKrLr2!s r2cCsBtjdd}|jdddd|jddd gd d |jd d d |S)NFrz--depr3 dependencies)rrz --backendmeson distutils)choicesdefaultr ry)r)rrrrrKrKrLrv*s  rvcCsVt}|\}}tjdg|t_|j}trB|dkrBtdd}|jpJg||jdS)NrrzMCannot use distutils backend with Python>=3.12, using meson backend instead. r)rbackendrG) rvrwr6r}rMESON_ONLY_VERr?rry)rrZremaining_argv backend_keyrKrKrLpreparse_sysargv1s  rcsddl}t}|d}|dur"d}|d}|d}t|}tjd}tj|=d}ztjd}Wntyxd}Yn0|durtj|d }tj|d =tj|=n d }|}t d fd d tjd dD fd d tjDt_ rdd D t dfdd tjd dDfdd tjDt_gd} tjd dD]>} | dvrhd } n| dkrvd} | s| dkrT | qTrddkr d fdd tjDt_t dfdd tjd dDfdd tjDt_t d t d fdd tjd dD fdd tjd dDt st|dkst fdd tjDt_g} D]} d} | dt | | krt s|dkrtd ndd!lm}|t|j}| t | d}}||vr`i}z ||}Wn:tyZ||vrVtd"| t | dYn0|}| }d||<qq| D]} | }|=qt d#ksJtt d$fd%d tjd dD fd&d tjDt_d'vr d'tjd d}d(}|tjvrhtj|} tj||d#tj|d =tj|=tjd d}td)d*|\}}||}t||}td)d+|\}}td,d)|d d-\}}td.d)|d d-\}}td/d)|d d-\}}td0d)|d d-\}}tt |D]T}||d1d }t |d kr"| dt |d#kr>t|||<n td2|q|dkr|std3 d4td5d5 d6|d5d5 |n$td5d5 d5d5 |t!|\}}||||||||||  |d|i}| dS)7z Do it all in one call! rNrGuntitledrr-crrz--link-csg|]}|r|qSrKrrY_m)_reg1rKrLrer\zrun_compile..csg|]}|vr|qSrKrKr) sysinfo_flagsrKrLrfr\cSsg|]}|ddqS)NrK)rYrErKrKrLrhr\zN--((no-|)(wrap-functions|lower)|debug-capi|quiet|skip-empty-wrappers)|-includecsg|]}|r|qSrKrr)_reg2rKrLrlr\csg|]}|vr|qSrKrKr) f2py_flagsrKrLrmr\)rrrrcsg|]}|vr|qSrKrKr) f2py_flags2rKrLrzr\z4--((f(90)?compiler(-exec|)|compiler)=|help-compiler)csg|]}|r|qSrKrr)_reg3rKrLr}r\csg|]}|vr|qSrKrKr) flib_flagsrKrLr~r\z--f(77|90)flags=z@--((f(77|90)exec|opt|arch)=|(debug|noopt|noarch|help-fcompiler))csg|]}|r|qSrKrr)reg_f77_f90_flagsrKrLrr\csg|]}|r|qSrKrr)reg_distutils_flagsrKrLrr\rcsg|]}|vr|qSrKrKr)distutils_flagsfc_flagsrKrLrr\z --fcompiler=zT--fcompiler cannot be used with meson,set compiler with the FC environment variable ) fcompilerzUnknown vendor: "%s"r!z --(verbose)csg|]}|r|qSrKrr)_reg5rKrLrr\csg|]}|vr|qSrKrKr) setup_flagsrKrLrr\rrrroz[.](o|a|so|dylib)z-L)rz-lz-Uz-D=zInvalid use of -D:zjUsing meson backend Will pass --lower to f2py See https://numpy.org/doc/stable/f2py/buildtools/meson.html rrz -m )"tempfilerrr6r}r_ ValueErrormkdtemprrr3extendrrr?Znumpy.distutilsrZload_all_fcompiler_classesrkZfcompiler_classr]lowerKeyErrorrr4r8rxrzrangertuplerrAr2)rZargyrGrr build_backendiZremove_build_dir build_dirflaZdel_listrZrmrZ allowed_keysnvovZvmapsourcesZ f2cmapoptrZ_sources extra_objects library_dirs libraries undef_macros define_macros name_value include_dirsbuilderrK) rrrrrrrrrrrrrrL run_compileEs                             ,$ rrcCsVt|dkrtd|rR|d}t|}||krRtd|d|d|d|}|S)NrzOnly one .pyf file per callrz Ignoring -m z. z defines z to be the modulename. )rrrZget_f2py_modulenamer?)rrGZpyffZ pyf_modnamerKrKrLrzs  rzcCsrdtjddvrBtjdtr,tdnddlm}|dSdtjddvr\tnttjdddS)Nz --help-linkrzUse --dep for meson builds rshow_allr) r6r}removerr?Znumpy.distutils.system_inforrrrrKrKrLmains   r)N)r)/__doc__r6r<pprintrpathlibr itertoolsrrcopyrrrrrr r r r r Znumpy.f2py._backendsrversionr5Z numpy_versionshowr? version_inforr;rMrTrgr`rrxrActionrrr2rvrrrzrrKrKrKrLsf              $# X   %