test_crackfortran.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import importlib
  2. import codecs
  3. import unicodedata
  4. import pytest
  5. import numpy as np
  6. from numpy.f2py.crackfortran import markinnerspaces
  7. from . import util
  8. from numpy.f2py import crackfortran
  9. import textwrap
  10. class TestNoSpace(util.F2PyTest):
  11. # issue gh-15035: add handling for endsubroutine, endfunction with no space
  12. # between "end" and the block name
  13. sources = [util.getpath("tests", "src", "crackfortran", "gh15035.f")]
  14. def test_module(self):
  15. k = np.array([1, 2, 3], dtype=np.float64)
  16. w = np.array([1, 2, 3], dtype=np.float64)
  17. self.module.subb(k)
  18. assert np.allclose(k, w + 1)
  19. self.module.subc([w, k])
  20. assert np.allclose(k, w + 1)
  21. assert self.module.t0("23") == b"2"
  22. class TestPublicPrivate:
  23. def test_defaultPrivate(self):
  24. fpath = util.getpath("tests", "src", "crackfortran", "privatemod.f90")
  25. mod = crackfortran.crackfortran([str(fpath)])
  26. assert len(mod) == 1
  27. mod = mod[0]
  28. assert "private" in mod["vars"]["a"]["attrspec"]
  29. assert "public" not in mod["vars"]["a"]["attrspec"]
  30. assert "private" in mod["vars"]["b"]["attrspec"]
  31. assert "public" not in mod["vars"]["b"]["attrspec"]
  32. assert "private" not in mod["vars"]["seta"]["attrspec"]
  33. assert "public" in mod["vars"]["seta"]["attrspec"]
  34. def test_defaultPublic(self, tmp_path):
  35. fpath = util.getpath("tests", "src", "crackfortran", "publicmod.f90")
  36. mod = crackfortran.crackfortran([str(fpath)])
  37. assert len(mod) == 1
  38. mod = mod[0]
  39. assert "private" in mod["vars"]["a"]["attrspec"]
  40. assert "public" not in mod["vars"]["a"]["attrspec"]
  41. assert "private" not in mod["vars"]["seta"]["attrspec"]
  42. assert "public" in mod["vars"]["seta"]["attrspec"]
  43. def test_access_type(self, tmp_path):
  44. fpath = util.getpath("tests", "src", "crackfortran", "accesstype.f90")
  45. mod = crackfortran.crackfortran([str(fpath)])
  46. assert len(mod) == 1
  47. tt = mod[0]['vars']
  48. assert set(tt['a']['attrspec']) == {'private', 'bind(c)'}
  49. assert set(tt['b_']['attrspec']) == {'public', 'bind(c)'}
  50. assert set(tt['c']['attrspec']) == {'public'}
  51. class TestModuleProcedure():
  52. def test_moduleOperators(self, tmp_path):
  53. fpath = util.getpath("tests", "src", "crackfortran", "operators.f90")
  54. mod = crackfortran.crackfortran([str(fpath)])
  55. assert len(mod) == 1
  56. mod = mod[0]
  57. assert "body" in mod and len(mod["body"]) == 9
  58. assert mod["body"][1]["name"] == "operator(.item.)"
  59. assert "implementedby" in mod["body"][1]
  60. assert mod["body"][1]["implementedby"] == \
  61. ["item_int", "item_real"]
  62. assert mod["body"][2]["name"] == "operator(==)"
  63. assert "implementedby" in mod["body"][2]
  64. assert mod["body"][2]["implementedby"] == ["items_are_equal"]
  65. assert mod["body"][3]["name"] == "assignment(=)"
  66. assert "implementedby" in mod["body"][3]
  67. assert mod["body"][3]["implementedby"] == \
  68. ["get_int", "get_real"]
  69. def test_notPublicPrivate(self, tmp_path):
  70. fpath = util.getpath("tests", "src", "crackfortran", "pubprivmod.f90")
  71. mod = crackfortran.crackfortran([str(fpath)])
  72. assert len(mod) == 1
  73. mod = mod[0]
  74. assert mod['vars']['a']['attrspec'] == ['private', ]
  75. assert mod['vars']['b']['attrspec'] == ['public', ]
  76. assert mod['vars']['seta']['attrspec'] == ['public', ]
  77. class TestExternal(util.F2PyTest):
  78. # issue gh-17859: add external attribute support
  79. sources = [util.getpath("tests", "src", "crackfortran", "gh17859.f")]
  80. def test_external_as_statement(self):
  81. def incr(x):
  82. return x + 123
  83. r = self.module.external_as_statement(incr)
  84. assert r == 123
  85. def test_external_as_attribute(self):
  86. def incr(x):
  87. return x + 123
  88. r = self.module.external_as_attribute(incr)
  89. assert r == 123
  90. class TestCrackFortran(util.F2PyTest):
  91. # gh-2848: commented lines between parameters in subroutine parameter lists
  92. sources = [util.getpath("tests", "src", "crackfortran", "gh2848.f90")]
  93. def test_gh2848(self):
  94. r = self.module.gh2848(1, 2)
  95. assert r == (1, 2)
  96. class TestMarkinnerspaces:
  97. # gh-14118: markinnerspaces does not handle multiple quotations
  98. def test_do_not_touch_normal_spaces(self):
  99. test_list = ["a ", " a", "a b c", "'abcdefghij'"]
  100. for i in test_list:
  101. assert markinnerspaces(i) == i
  102. def test_one_relevant_space(self):
  103. assert markinnerspaces("a 'b c' \\' \\'") == "a 'b@_@c' \\' \\'"
  104. assert markinnerspaces(r'a "b c" \" \"') == r'a "b@_@c" \" \"'
  105. def test_ignore_inner_quotes(self):
  106. assert markinnerspaces("a 'b c\" \" d' e") == "a 'b@_@c\"@_@\"@_@d' e"
  107. assert markinnerspaces("a \"b c' ' d\" e") == "a \"b@_@c'@_@'@_@d\" e"
  108. def test_multiple_relevant_spaces(self):
  109. assert markinnerspaces("a 'b c' 'd e'") == "a 'b@_@c' 'd@_@e'"
  110. assert markinnerspaces(r'a "b c" "d e"') == r'a "b@_@c" "d@_@e"'
  111. class TestDimSpec(util.F2PyTest):
  112. """This test suite tests various expressions that are used as dimension
  113. specifications.
  114. There exists two usage cases where analyzing dimensions
  115. specifications are important.
  116. In the first case, the size of output arrays must be defined based
  117. on the inputs to a Fortran function. Because Fortran supports
  118. arbitrary bases for indexing, for instance, `arr(lower:upper)`,
  119. f2py has to evaluate an expression `upper - lower + 1` where
  120. `lower` and `upper` are arbitrary expressions of input parameters.
  121. The evaluation is performed in C, so f2py has to translate Fortran
  122. expressions to valid C expressions (an alternative approach is
  123. that a developer specifies the corresponding C expressions in a
  124. .pyf file).
  125. In the second case, when user provides an input array with a given
  126. size but some hidden parameters used in dimensions specifications
  127. need to be determined based on the input array size. This is a
  128. harder problem because f2py has to solve the inverse problem: find
  129. a parameter `p` such that `upper(p) - lower(p) + 1` equals to the
  130. size of input array. In the case when this equation cannot be
  131. solved (e.g. because the input array size is wrong), raise an
  132. error before calling the Fortran function (that otherwise would
  133. likely crash Python process when the size of input arrays is
  134. wrong). f2py currently supports this case only when the equation
  135. is linear with respect to unknown parameter.
  136. """
  137. suffix = ".f90"
  138. code_template = textwrap.dedent("""
  139. function get_arr_size_{count}(a, n) result (length)
  140. integer, intent(in) :: n
  141. integer, dimension({dimspec}), intent(out) :: a
  142. integer length
  143. length = size(a)
  144. end function
  145. subroutine get_inv_arr_size_{count}(a, n)
  146. integer :: n
  147. ! the value of n is computed in f2py wrapper
  148. !f2py intent(out) n
  149. integer, dimension({dimspec}), intent(in) :: a
  150. if (a({first}).gt.0) then
  151. print*, "a=", a
  152. endif
  153. end subroutine
  154. """)
  155. linear_dimspecs = [
  156. "n", "2*n", "2:n", "n/2", "5 - n/2", "3*n:20", "n*(n+1):n*(n+5)",
  157. "2*n, n"
  158. ]
  159. nonlinear_dimspecs = ["2*n:3*n*n+2*n"]
  160. all_dimspecs = linear_dimspecs + nonlinear_dimspecs
  161. code = ""
  162. for count, dimspec in enumerate(all_dimspecs):
  163. lst = [(d.split(":")[0] if ":" in d else "1") for d in dimspec.split(',')]
  164. code += code_template.format(
  165. count=count,
  166. dimspec=dimspec,
  167. first=", ".join(lst),
  168. )
  169. @pytest.mark.parametrize("dimspec", all_dimspecs)
  170. def test_array_size(self, dimspec):
  171. count = self.all_dimspecs.index(dimspec)
  172. get_arr_size = getattr(self.module, f"get_arr_size_{count}")
  173. for n in [1, 2, 3, 4, 5]:
  174. sz, a = get_arr_size(n)
  175. assert a.size == sz
  176. @pytest.mark.parametrize("dimspec", all_dimspecs)
  177. def test_inv_array_size(self, dimspec):
  178. count = self.all_dimspecs.index(dimspec)
  179. get_arr_size = getattr(self.module, f"get_arr_size_{count}")
  180. get_inv_arr_size = getattr(self.module, f"get_inv_arr_size_{count}")
  181. for n in [1, 2, 3, 4, 5]:
  182. sz, a = get_arr_size(n)
  183. if dimspec in self.nonlinear_dimspecs:
  184. # one must specify n as input, the call we'll ensure
  185. # that a and n are compatible:
  186. n1 = get_inv_arr_size(a, n)
  187. else:
  188. # in case of linear dependence, n can be determined
  189. # from the shape of a:
  190. n1 = get_inv_arr_size(a)
  191. # n1 may be different from n (for instance, when `a` size
  192. # is a function of some `n` fraction) but it must produce
  193. # the same sized array
  194. sz1, _ = get_arr_size(n1)
  195. assert sz == sz1, (n, n1, sz, sz1)
  196. class TestModuleDeclaration:
  197. def test_dependencies(self, tmp_path):
  198. fpath = util.getpath("tests", "src", "crackfortran", "foo_deps.f90")
  199. mod = crackfortran.crackfortran([str(fpath)])
  200. assert len(mod) == 1
  201. assert mod[0]["vars"]["abar"]["="] == "bar('abar')"
  202. class TestEval(util.F2PyTest):
  203. def test_eval_scalar(self):
  204. eval_scalar = crackfortran._eval_scalar
  205. assert eval_scalar('123', {}) == '123'
  206. assert eval_scalar('12 + 3', {}) == '15'
  207. assert eval_scalar('a + b', dict(a=1, b=2)) == '3'
  208. assert eval_scalar('"123"', {}) == "'123'"
  209. class TestFortranReader(util.F2PyTest):
  210. @pytest.mark.parametrize("encoding",
  211. ['ascii', 'utf-8', 'utf-16', 'utf-32'])
  212. def test_input_encoding(self, tmp_path, encoding):
  213. # gh-635
  214. f_path = tmp_path / f"input_with_{encoding}_encoding.f90"
  215. with f_path.open('w', encoding=encoding) as ff:
  216. ff.write("""
  217. subroutine foo()
  218. end subroutine foo
  219. """)
  220. mod = crackfortran.crackfortran([str(f_path)])
  221. assert mod[0]['name'] == 'foo'
  222. class TestUnicodeComment(util.F2PyTest):
  223. sources = [util.getpath("tests", "src", "crackfortran", "unicode_comment.f90")]
  224. @pytest.mark.skipif(
  225. (importlib.util.find_spec("charset_normalizer") is None),
  226. reason="test requires charset_normalizer which is not installed",
  227. )
  228. def test_encoding_comment(self):
  229. self.module.foo(3)