test_crackfortran.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import numpy as np
  2. from numpy.testing import assert_array_equal
  3. from . import util
  4. from numpy.f2py import crackfortran
  5. import tempfile
  6. import textwrap
  7. class TestNoSpace(util.F2PyTest):
  8. # issue gh-15035: add handling for endsubroutine, endfunction with no space
  9. # between "end" and the block name
  10. code = """
  11. subroutine subb(k)
  12. real(8), intent(inout) :: k(:)
  13. k=k+1
  14. endsubroutine
  15. subroutine subc(w,k)
  16. real(8), intent(in) :: w(:)
  17. real(8), intent(out) :: k(size(w))
  18. k=w+1
  19. endsubroutine
  20. function t0(value)
  21. character value
  22. character t0
  23. t0 = value
  24. endfunction
  25. """
  26. def test_module(self):
  27. k = np.array([1, 2, 3], dtype=np.float64)
  28. w = np.array([1, 2, 3], dtype=np.float64)
  29. self.module.subb(k)
  30. assert_array_equal(k, w + 1)
  31. self.module.subc([w, k])
  32. assert_array_equal(k, w + 1)
  33. assert self.module.t0(23) == b'2'
  34. class TestPublicPrivate():
  35. def test_defaultPrivate(self, tmp_path):
  36. f_path = tmp_path / "mod.f90"
  37. with f_path.open('w') as ff:
  38. ff.write(textwrap.dedent("""\
  39. module foo
  40. private
  41. integer :: a
  42. public :: setA
  43. integer :: b
  44. contains
  45. subroutine setA(v)
  46. integer, intent(in) :: v
  47. a = v
  48. end subroutine setA
  49. end module foo
  50. """))
  51. mod = crackfortran.crackfortran([str(f_path)])
  52. assert len(mod) == 1
  53. mod = mod[0]
  54. assert 'private' in mod['vars']['a']['attrspec']
  55. assert 'public' not in mod['vars']['a']['attrspec']
  56. assert 'private' in mod['vars']['b']['attrspec']
  57. assert 'public' not in mod['vars']['b']['attrspec']
  58. assert 'private' not in mod['vars']['seta']['attrspec']
  59. assert 'public' in mod['vars']['seta']['attrspec']
  60. def test_defaultPublic(self, tmp_path):
  61. f_path = tmp_path / "mod.f90"
  62. with f_path.open('w') as ff:
  63. ff.write(textwrap.dedent("""\
  64. module foo
  65. public
  66. integer, private :: a
  67. public :: setA
  68. contains
  69. subroutine setA(v)
  70. integer, intent(in) :: v
  71. a = v
  72. end subroutine setA
  73. end module foo
  74. """))
  75. mod = crackfortran.crackfortran([str(f_path)])
  76. assert len(mod) == 1
  77. mod = mod[0]
  78. assert 'private' in mod['vars']['a']['attrspec']
  79. assert 'public' not in mod['vars']['a']['attrspec']
  80. assert 'private' not in mod['vars']['seta']['attrspec']
  81. assert 'public' in mod['vars']['seta']['attrspec']