m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.6 KiB

6 months ago
  1. #!/usr/bin/env python3
  2. """Fortran to Python Interface Generator.
  3. """
  4. __all__ = ['run_main', 'compile', 'get_include', 'f2py_testing']
  5. import sys
  6. import subprocess
  7. import os
  8. from . import f2py2e
  9. from . import diagnose
  10. run_main = f2py2e.run_main
  11. main = f2py2e.main
  12. def compile(source,
  13. modulename='untitled',
  14. extra_args='',
  15. verbose=True,
  16. source_fn=None,
  17. extension='.f',
  18. full_output=False
  19. ):
  20. """
  21. Build extension module from a Fortran 77 source string with f2py.
  22. Parameters
  23. ----------
  24. source : str or bytes
  25. Fortran source of module / subroutine to compile
  26. .. versionchanged:: 1.16.0
  27. Accept str as well as bytes
  28. modulename : str, optional
  29. The name of the compiled python module
  30. extra_args : str or list, optional
  31. Additional parameters passed to f2py
  32. .. versionchanged:: 1.16.0
  33. A list of args may also be provided.
  34. verbose : bool, optional
  35. Print f2py output to screen
  36. source_fn : str, optional
  37. Name of the file where the fortran source is written.
  38. The default is to use a temporary file with the extension
  39. provided by the `extension` parameter
  40. extension : {'.f', '.f90'}, optional
  41. Filename extension if `source_fn` is not provided.
  42. The extension tells which fortran standard is used.
  43. The default is `.f`, which implies F77 standard.
  44. .. versionadded:: 1.11.0
  45. full_output : bool, optional
  46. If True, return a `subprocess.CompletedProcess` containing
  47. the stdout and stderr of the compile process, instead of just
  48. the status code.
  49. .. versionadded:: 1.20.0
  50. Returns
  51. -------
  52. result : int or `subprocess.CompletedProcess`
  53. 0 on success, or a `subprocess.CompletedProcess` if
  54. ``full_output=True``
  55. Examples
  56. --------
  57. .. include:: compile_session.dat
  58. :literal:
  59. """
  60. import tempfile
  61. import shlex
  62. if source_fn is None:
  63. f, fname = tempfile.mkstemp(suffix=extension)
  64. # f is a file descriptor so need to close it
  65. # carefully -- not with .close() directly
  66. os.close(f)
  67. else:
  68. fname = source_fn
  69. if not isinstance(source, str):
  70. source = str(source, 'utf-8')
  71. try:
  72. with open(fname, 'w') as f:
  73. f.write(source)
  74. args = ['-c', '-m', modulename, f.name]
  75. if isinstance(extra_args, str):
  76. is_posix = (os.name == 'posix')
  77. extra_args = shlex.split(extra_args, posix=is_posix)
  78. args.extend(extra_args)
  79. c = [sys.executable,
  80. '-c',
  81. 'import numpy.f2py as f2py2e;f2py2e.main()'] + args
  82. try:
  83. cp = subprocess.run(c, stdout=subprocess.PIPE,
  84. stderr=subprocess.PIPE)
  85. except OSError:
  86. # preserve historic status code used by exec_command()
  87. cp = subprocess.CompletedProcess(c, 127, stdout=b'', stderr=b'')
  88. else:
  89. if verbose:
  90. print(cp.stdout.decode())
  91. finally:
  92. if source_fn is None:
  93. os.remove(fname)
  94. if full_output:
  95. return cp
  96. else:
  97. return cp.returncode
  98. def get_include():
  99. """
  100. Return the directory that contains the fortranobject.c and .h files.
  101. .. note::
  102. This function is not needed when building an extension with
  103. `numpy.distutils` directly from ``.f`` and/or ``.pyf`` files
  104. in one go.
  105. Python extension modules built with f2py-generated code need to use
  106. ``fortranobject.c`` as a source file, and include the ``fortranobject.h``
  107. header. This function can be used to obtain the directory containing
  108. both of these files.
  109. Returns
  110. -------
  111. include_path : str
  112. Absolute path to the directory containing ``fortranobject.c`` and
  113. ``fortranobject.h``.
  114. Notes
  115. -----
  116. .. versionadded:: 1.22.0
  117. Unless the build system you are using has specific support for f2py,
  118. building a Python extension using a ``.pyf`` signature file is a two-step
  119. process. For a module ``mymod``:
  120. - Step 1: run ``python -m numpy.f2py mymod.pyf --quiet``. This
  121. generates ``_mymodmodule.c`` and (if needed)
  122. ``_fblas-f2pywrappers.f`` files next to ``mymod.pyf``.
  123. - Step 2: build your Python extension module. This requires the
  124. following source files:
  125. - ``_mymodmodule.c``
  126. - ``_mymod-f2pywrappers.f`` (if it was generated in step 1)
  127. - ``fortranobject.c``
  128. See Also
  129. --------
  130. numpy.get_include : function that returns the numpy include directory
  131. """
  132. return os.path.join(os.path.dirname(__file__), 'src')
  133. if sys.version_info[:2] >= (3, 7):
  134. # module level getattr is only supported in 3.7 onwards
  135. # https://www.python.org/dev/peps/pep-0562/
  136. def __getattr__(attr):
  137. # Avoid importing things that aren't needed for building
  138. # which might import the main numpy module
  139. if attr == "f2py_testing":
  140. import numpy.f2py.f2py_testing as f2py_testing
  141. return f2py_testing
  142. elif attr == "test":
  143. from numpy._pytesttester import PytestTester
  144. test = PytestTester(__name__)
  145. return test
  146. else:
  147. raise AttributeError("module {!r} has no attribute "
  148. "{!r}".format(__name__, attr))
  149. def __dir__():
  150. return list(globals().keys() | {"f2py_testing", "test"})
  151. else:
  152. from . import f2py_testing
  153. from numpy._pytesttester import PytestTester
  154. test = PytestTester(__name__)
  155. del PytestTester