test_compile_function.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """See https://github.com/numpy/numpy/pull/11937.
  2. """
  3. import sys
  4. import os
  5. import uuid
  6. from importlib import import_module
  7. import pytest
  8. import numpy.f2py
  9. from numpy.testing import assert_equal
  10. from . import util
  11. def setup_module():
  12. if not util.has_c_compiler():
  13. pytest.skip("Needs C compiler")
  14. if not util.has_f77_compiler():
  15. pytest.skip('Needs FORTRAN 77 compiler')
  16. # extra_args can be a list (since gh-11937) or string.
  17. # also test absence of extra_args
  18. @pytest.mark.parametrize(
  19. "extra_args", [['--noopt', '--debug'], '--noopt --debug', '']
  20. )
  21. @pytest.mark.leaks_references(reason="Imported module seems never deleted.")
  22. def test_f2py_init_compile(extra_args):
  23. # flush through the f2py __init__ compile() function code path as a
  24. # crude test for input handling following migration from
  25. # exec_command() to subprocess.check_output() in gh-11937
  26. # the Fortran 77 syntax requires 6 spaces before any commands, but
  27. # more space may be added/
  28. fsource = """
  29. integer function foo()
  30. foo = 10 + 5
  31. return
  32. end
  33. """
  34. # use various helper functions in util.py to enable robust build /
  35. # compile and reimport cycle in test suite
  36. moddir = util.get_module_dir()
  37. modname = util.get_temp_module_name()
  38. cwd = os.getcwd()
  39. target = os.path.join(moddir, str(uuid.uuid4()) + '.f')
  40. # try running compile() with and without a source_fn provided so
  41. # that the code path where a temporary file for writing Fortran
  42. # source is created is also explored
  43. for source_fn in [target, None]:
  44. # mimic the path changing behavior used by build_module() in
  45. # util.py, but don't actually use build_module() because it has
  46. # its own invocation of subprocess that circumvents the
  47. # f2py.compile code block under test
  48. try:
  49. os.chdir(moddir)
  50. ret_val = numpy.f2py.compile(
  51. fsource,
  52. modulename=modname,
  53. extra_args=extra_args,
  54. source_fn=source_fn
  55. )
  56. finally:
  57. os.chdir(cwd)
  58. # check for compile success return value
  59. assert_equal(ret_val, 0)
  60. # we are not currently able to import the Python-Fortran
  61. # interface module on Windows / Appveyor, even though we do get
  62. # successful compilation on that platform with Python 3.x
  63. if sys.platform != 'win32':
  64. # check for sensible result of Fortran function; that means
  65. # we can import the module name in Python and retrieve the
  66. # result of the sum operation
  67. return_check = import_module(modname)
  68. calc_result = return_check.foo()
  69. assert_equal(calc_result, 15)
  70. # Removal from sys.modules, is not as such necessary. Even with
  71. # removal, the module (dict) stays alive.
  72. del sys.modules[modname]
  73. def test_f2py_init_compile_failure():
  74. # verify an appropriate integer status value returned by
  75. # f2py.compile() when invalid Fortran is provided
  76. ret_val = numpy.f2py.compile(b"invalid")
  77. assert_equal(ret_val, 1)
  78. def test_f2py_init_compile_bad_cmd():
  79. # verify that usage of invalid command in f2py.compile() returns
  80. # status value of 127 for historic consistency with exec_command()
  81. # error handling
  82. # patch the sys Python exe path temporarily to induce an OSError
  83. # downstream NOTE: how bad of an idea is this patching?
  84. try:
  85. temp = sys.executable
  86. sys.executable = 'does not exist'
  87. # the OSError should take precedence over invalid Fortran
  88. ret_val = numpy.f2py.compile(b"invalid")
  89. assert_equal(ret_val, 127)
  90. finally:
  91. sys.executable = temp
  92. @pytest.mark.parametrize('fsource',
  93. ['program test_f2py\nend program test_f2py',
  94. b'program test_f2py\nend program test_f2py',])
  95. def test_compile_from_strings(tmpdir, fsource):
  96. # Make sure we can compile str and bytes gh-12796
  97. cwd = os.getcwd()
  98. try:
  99. os.chdir(str(tmpdir))
  100. ret_val = numpy.f2py.compile(
  101. fsource,
  102. modulename='test_compile_from_strings',
  103. extension='.f90')
  104. assert_equal(ret_val, 0)
  105. finally:
  106. os.chdir(cwd)