__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python
  2. """Fortran to Python Interface Generator.
  3. """
  4. from __future__ import division, absolute_import, print_function
  5. __all__ = ['run_main', 'compile', 'f2py_testing']
  6. import sys
  7. import subprocess
  8. import os
  9. import numpy as np
  10. from . import f2py2e
  11. from . import f2py_testing
  12. from . import diagnose
  13. run_main = f2py2e.run_main
  14. main = f2py2e.main
  15. def compile(source,
  16. modulename='untitled',
  17. extra_args='',
  18. verbose=True,
  19. source_fn=None,
  20. extension='.f'
  21. ):
  22. """
  23. Build extension module from a Fortran 77 source string with f2py.
  24. Parameters
  25. ----------
  26. source : str or bytes
  27. Fortran source of module / subroutine to compile
  28. .. versionchanged:: 1.16.0
  29. Accept str as well as bytes
  30. modulename : str, optional
  31. The name of the compiled python module
  32. extra_args : str or list, optional
  33. Additional parameters passed to f2py
  34. .. versionchanged:: 1.16.0
  35. A list of args may also be provided.
  36. verbose : bool, optional
  37. Print f2py output to screen
  38. source_fn : str, optional
  39. Name of the file where the fortran source is written.
  40. The default is to use a temporary file with the extension
  41. provided by the `extension` parameter
  42. extension : {'.f', '.f90'}, optional
  43. Filename extension if `source_fn` is not provided.
  44. The extension tells which fortran standard is used.
  45. The default is `.f`, which implies F77 standard.
  46. .. versionadded:: 1.11.0
  47. Returns
  48. -------
  49. result : int
  50. 0 on success
  51. Examples
  52. --------
  53. .. include:: compile_session.dat
  54. :literal:
  55. """
  56. import tempfile
  57. import shlex
  58. if source_fn is None:
  59. f, fname = tempfile.mkstemp(suffix=extension)
  60. # f is a file descriptor so need to close it
  61. # carefully -- not with .close() directly
  62. os.close(f)
  63. else:
  64. fname = source_fn
  65. if not isinstance(source, str):
  66. source = str(source, 'utf-8')
  67. try:
  68. with open(fname, 'w') as f:
  69. f.write(source)
  70. args = ['-c', '-m', modulename, f.name]
  71. if isinstance(extra_args, np.compat.basestring):
  72. is_posix = (os.name == 'posix')
  73. extra_args = shlex.split(extra_args, posix=is_posix)
  74. args.extend(extra_args)
  75. c = [sys.executable,
  76. '-c',
  77. 'import numpy.f2py as f2py2e;f2py2e.main()'] + args
  78. try:
  79. output = subprocess.check_output(c)
  80. except subprocess.CalledProcessError as exc:
  81. status = exc.returncode
  82. output = ''
  83. except OSError:
  84. # preserve historic status code used by exec_command()
  85. status = 127
  86. output = ''
  87. else:
  88. status = 0
  89. if verbose:
  90. print(output)
  91. finally:
  92. if source_fn is None:
  93. os.remove(fname)
  94. return status
  95. from numpy._pytesttester import PytestTester
  96. test = PytestTester(__name__)
  97. del PytestTester