__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python3
  2. """Fortran to Python Interface Generator.
  3. """
  4. __all__ = ['run_main', 'compile', 'f2py_testing']
  5. import sys
  6. import subprocess
  7. import os
  8. from . import f2py2e
  9. from . import f2py_testing
  10. from . import diagnose
  11. run_main = f2py2e.run_main
  12. main = f2py2e.main
  13. def compile(source,
  14. modulename='untitled',
  15. extra_args='',
  16. verbose=True,
  17. source_fn=None,
  18. extension='.f'
  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. Returns
  46. -------
  47. result : int
  48. 0 on success
  49. Examples
  50. --------
  51. .. include:: compile_session.dat
  52. :literal:
  53. """
  54. import tempfile
  55. import shlex
  56. if source_fn is None:
  57. f, fname = tempfile.mkstemp(suffix=extension)
  58. # f is a file descriptor so need to close it
  59. # carefully -- not with .close() directly
  60. os.close(f)
  61. else:
  62. fname = source_fn
  63. if not isinstance(source, str):
  64. source = str(source, 'utf-8')
  65. try:
  66. with open(fname, 'w') as f:
  67. f.write(source)
  68. args = ['-c', '-m', modulename, f.name]
  69. if isinstance(extra_args, str):
  70. is_posix = (os.name == 'posix')
  71. extra_args = shlex.split(extra_args, posix=is_posix)
  72. args.extend(extra_args)
  73. c = [sys.executable,
  74. '-c',
  75. 'import numpy.f2py as f2py2e;f2py2e.main()'] + args
  76. try:
  77. output = subprocess.check_output(c)
  78. except subprocess.CalledProcessError as exc:
  79. status = exc.returncode
  80. output = ''
  81. except OSError:
  82. # preserve historic status code used by exec_command()
  83. status = 127
  84. output = ''
  85. else:
  86. status = 0
  87. output = output.decode()
  88. if verbose:
  89. print(output)
  90. finally:
  91. if source_fn is None:
  92. os.remove(fname)
  93. return status
  94. from numpy._pytesttester import PytestTester
  95. test = PytestTester(__name__)
  96. del PytestTester