8000 TST: Use `meson` for testing ``f2py`` (#25111) · numpy/numpy@774e0a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 774e0a8

Browse files
HaoZekecharris
authored andcommitted
TST: Use meson for testing f2py (#25111)
* MAINT,TST: Use meson for compiler checks [f2py] * MAINT: Cache the compiler checks [f2py] * MAINT,TST: Always use meson [f2py] * TST: Rework to have a build_meson [f2py] * MAINT,TST: Simplify the meson backend [f2py] * TST: Use the build_meson function [f2py] * MAINT,TST: Minor cleanup [f2py] * TST: Ensure TestDocAdvanced runs with spin [f2py] * TST: Use cleanup meson backend [f2py] * MAINT,TST: Generalize build_meson [f2py] * TST: Use a helper for spin tests [f2py] * MAINT: Simplify meson backend [f2py] * TST: Handle unsupported compilers [f2py] * TST: Fix gibberish in [f2py] documentation test * CI: Add meson for cygwin runs * TST: Skips for 32-bit errors [f2py] * TST: Skip for cygwin since meson is old [f2py] * CI: Revert grabbing meson on cygwin The version is too old to be of use * TST: Cleanup old distutils builder [f2py] * TST: Skip cygwin better [f2py] * TST: Don't touch distutils * MAINT: Vendor in distutils testing requirement * TST: Try removing cygwin restrictions * MAINT: Cleanup some tests [f2py] * TST: Try to use concurrency for i/o bounds [f2py] * TST: Mark slow tests [f2py] Should finish in around a minute now
1 parent 9eca8ce commit 774e0a8

25 files changed

+284
-208
lines changed

numpy/distutils/tests/test_build_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_multi_fortran_libs_link(tmp_path):
1818
# We need to make sure we actually have an f77 compiler.
1919
# This is nontrivial, so we'll borrow the utilities
2020
# from f2py tests:
21-
from numpy.f2py.tests.util import has_f77_compiler
21+
from numpy.distutils.tests.utilities import has_f77_compiler
2222
if not has_f77_compiler():
2323
pytest.skip('No F77 compiler found')
2424

numpy/distutils/tests/utilities.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Kanged out of numpy.f2py.tests.util for test_build_ext
2+
from numpy.testing import IS_WASM
3+
import textwrap
4+
import shutil
5+
import tempfile
6+
import os
7+
import re
8+
import subprocess
9+
import sys
10+
11+
#
12+
# Check if compilers are available at all...
13+
#
14+
15+
_compiler_status = None
16+
17+
18+
def _get_compiler_status():
19+
global _compiler_status
20+
if _compiler_status is not None:
21+
return _compiler_status
22+
23+
_compiler_status = (False, False, False)
24+
if IS_WASM:
25+
# Can't run compiler from inside WASM.
26+
return _compiler_status
27+
28+
# XXX: this is really ugly. But I don't know how to invoke Distutils
29+
# in a safer way...
30+
code = textwrap.dedent(
31+
f"""\
32+
import os
33+
import sys
34+
sys.path = {repr(sys.path)}
35+
36+
def configuration(parent_name='',top_path=None):
37+
global config
38+
from numpy.distutils.misc_util import Configuration
39+
config = Configuration('', parent_name, top_path)
40+
return config
41+
42+
from numpy.distutils.core import setup
43+
setup(configuration=configuration)
44+
45+
config_cmd = config.get_config_cmd()
46+
have_c = config_cmd.try_compile('void foo() {{}}')
47+
print('COMPILERS:%%d,%%d,%%d' %% (have_c,
48+
config.have_f77c(),
49+
config.have_f90c()))
50+
sys.exit(99)
51+
"""
52+
)
53+
code = code % dict(syspath=repr(sys.path))
54+
55+
tmpdir = tempfile.mkdtemp()
56+
try:
57+
script = os.path.join(tmpdir, "setup.py")
58+
59+
with open(script, "w") as f:
60+
f.write(code)
61+
62+
cmd = [sys.executable, "setup.py", "config"]
63+
p = subprocess.Popen(
64+
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=tmpdir
65+
)
66+
out, err = p.communicate()
67+
finally:
68+
shutil.rmtree(tmpdir)
69+
70+
m = re.search(rb"COMPILERS:(\d+),(\d+),(\d+)", out)
71+
if m:
72+
_compiler_status = (
73+
bool(int(m.group(1))),
< 10000 /td>
74+
bool(int(m.group(2))),
75+
bool(int(m.group(3))),
76+
)
77+
# Finished
78+
return _compiler_status
79+
80+
81+
def has_c_compiler():
82+
return _get_compiler_status()[0]
83+
84+
85+
def has_f77_compiler():
86+
return _get_compiler_status()[1]
87+
88+
89+
def has_f90_compiler():
90+
return _get_compiler_status()[2]

numpy/f2py/_backends/_meson.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,6 @@ def _move_exec_to_root(self, build_dir: Path):
9191
for path_object in path_objects:
9292
shutil.move(path_object, Path.cwd())
9393

94-
def _get_build_command(self):
95-
return [
96-
"meson",
97-
"setup",
98-
self.meson_build_dir,
99-
]
100-
10194
def write_meson_build(self, build_dir: Path) -> None:
10295
"""Writes the meson build file at specified location"""
10396
meson_template = MesonTemplate(
@@ -115,19 +108,14 @@ def write_meson_build(self, build_dir: Path) -> None:
115108
meson_build_file.write_text(src)
116109
return meson_build_file
117110

111+
def _run_subprocess_command(self, command, cwd):
112+
subprocess.run(command, cwd=cwd, check=True)
113+
118114
def run_meson(self, build_dir: Path):
119-
completed_process = subprocess.run(self._get_build_command(), cwd=build_dir)
120-
if completed_process.returncode != 0:
121-
raise subprocess.CalledProcessError(
122-
completed_process.returncode, completed_process.args
123-
)
124-
completed_process = subprocess.run(
125-
["meson", "compile", "-C", self.meson_build_dir], cwd=build_dir
126-
)
127-
if completed_process.returncode != 0:
128-
raise subprocess.CalledProcessError(
129-
completed_process.returncode, completed_process.args
130-
)
115+
setup_command = ["meson", "setup", self.meson_build_dir]
116+
self._run_subprocess_command(setup_command, build_dir)
117+
compile_command = ["meson", "compile", "-C", self.meson_build_dir]
118+
self._run_subprocess_command(compile_command, build_dir)
131119

132120
def compile(self) -> None:
133121
self.sources = _prepare_sources(self.modulename, self.sources, self.build_dir)

numpy/f2py/tests/test_abstract_interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
@pytest.mark.skipif(IS_WASM, reason="Cannot start subprocess")
10+
@pytest.mark.slow
1011
class TestAbstractInterface(util.F2PyTest):
1112
sources = [util.getpath("tests", "src", "abstract_interface", "foo.f90")]
1213

numpy/f2py/tests/test_array_from_pyobj.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import platform
55
import pytest
6+
from pathlib import Path
67

78
import numpy as np
89

@@ -19,31 +20,22 @@
1920
**_typeinfo)
2021

2122

23+
def get_testdir():
24+
testroot = Path(__file__).resolve().parent / "src"
25+
return testroot / "array_from_pyobj"
26+
2227
def setup_module():
2328
"""
2429
Build the required testing extension module
2530
2631
"""
2732
global wrap
2833

29-
# Check compiler availability first
30-
if not util.has_c_compiler():
31-
pytest.skip("No C compiler available")
32-
3334
if wrap is None:
34-
config_code = """
35-
config.add_extension('test_array_from_pyobj_ext',
36-
sources=['wrapmodule.c', 'fortranobject.c'],
37-
define_macros=[])
38-
"""
39-
d = os.path.dirname(__file__)
4035
src = [
41-
util.getpath("tests", "src", "array_from_pyobj", "wrapmodule.c"),
42-
util.getpath("src", "fortranobject.c"),
43-
util.getpath("src", "fortranobject.h"),
36+
get_testdir() / "wrapmodule.c",
4437
]
45-
wrap = util.build_module_distutils(src, config_code,
46-
"test_array_from_pyobj_ext")
38+
wrap = util.build_meson(src, module_name = "test_array_from_pyobj_ext")
4739

4840

4941
def flags_info(arr):

numpy/f2py/tests/test_block_docstring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from numpy.testing import IS_PYPY
66

77

8+
@pytest.mark.slow
89
class TestBlockDocString(util.F2PyTest):
910
sources = [util.getpath("tests", "src", "block_docstring", "foo.f")]
1011

numpy/f2py/tests/test_callback.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TestF77Callback(util.F2PyTest):
1515
sources = [util.getpath("tests", "src", "callback", "foo.f")]
1616

1717
@pytest.mark.parametrize("name", "t,t2".split(","))
18+
@pytest.mark.slow
1819
def test_all(self, name):
1920
self.check_function(name)
2021

@@ -205,6 +206,7 @@ class TestF77CallbackPythonTLS(TestF77Callback):
205206
class TestF90Callback(util.F2PyTest):
206207
sources = [util.getpath("tests", "src", "callback", "gh17797.f90")]
207208

209+
@pytest.mark.slow
208210
def test_gh17797(self):
209211
def incr(x):
210212
return x + 123
@@ -222,6 +224,7 @@ class TestGH18335(util.F2PyTest):
222224
"""
223225
sources = [util.getpath("tests", "src", "callback", "gh18335.f90")]
224226

227+
@pytest.mark.slow
225228
def test_gh18335(self):
226229
def foo(x):
227230
x[0] += 1
@@ -235,7 +238,7 @@ class TestGH25211(util.F2PyTest):
235238
util.getpath("tests", "src", "callback", "gh25211.pyf")]
236239
module_name = "callback2"
237240

238-
def test_gh18335(self):
241+
def test_gh25211(self):
239242
def bar(x):
240243
return x*x
241244

numpy/f2py/tests/test_character.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from numpy.f2py.tests import util
66

77

8+
@pytest.mark.slow
89
class TestCharacterString(util.F2PyTest):
910
# options = ['--debug-capi', '--build-dir', '/tmp/test-build-f2py']
1011
suffix = '.f90'
@@ -512,6 +513,7 @@ class TestMiscCharacter(util.F2PyTest):
512513
end subroutine {fprefix}_character_bc_old
513514
""")
514515

516+
@pytest.mark.slow
515517
def test_gh18684(self):
516518
# Test character(len=5) and character*5 usages
517519
f = getattr(self.module, self.fprefix + '_gh18684')
@@ -596,6 +598,7 @@ class TestStringAssumedLength(util.F2PyTest):
596598
def test_gh24008(self):
597599
self.module.greet("joe", "bob")
598600

601+
@pytest.mark.slow
599602
class TestStringOptionalInOut(util.F2PyTest):
600603
sources = [util.getpath("tests", "src", "string", "gh24662.f90")]
601604

numpy/f2py/tests/test_common.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import os
2-
import sys
31
import pytest
4-
52
import numpy as np
63
from . import util
74

8-
5+
@pytest.mark.slow
96
class TestCommonBlock(util.F2PyTest):
107
sources = [util.getpath("tests", "src", "common", "block.f")]
118

12-
@pytest.mark.skipif(sys.platform == "win32",
13-
reason="Fails with MinGW64 Gfortran (Issue #9673)")
149
def test_common_block(self):
1510
self.module.initcb()
1611
assert self.module.block.long_bn == np.array(1.0, dtype=np.float64)
@@ -21,7 +16,5 @@ def test_common_block(self):
2116
class TestCommonWithUse(util.F2PyTest):
2217
sources = [util.getpath("tests", "src", "common", "gh19161.f90")]
2318

24-
@pytest.mark.skipif(sys.platform == "win32",
25-
reason="Fails with MinGW64 Gfortran (Issue #9673)")
2619
def test_common_gh19161(self):
2720
assert self.module.data.x == 0

numpy/f2py/tests/test_crackfortran.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class TestDimSpec(util.F2PyTest):
210210
)
211211

212212
@pytest.mark.parametrize("dimspec", all_dimspecs)
213+
@pytest.mark.slow
213214
def test_array_size(self, dimspec):
214215

215216
count = self.all_dimspecs.index(dimspec)
@@ -276,6 +277,7 @@ def test_input_encoding(self, tmp_path, encoding):
276277
assert mod[0]['name'] == 'foo'
277278

278279

280+
@pytest.mark.slow
279281
class TestUnicodeComment(util.F2PyTest):
280282
sources = [util.getpath("tests", "src", "crackfortran", "unicode_comment.f90")]
281283

@@ -327,6 +329,7 @@ def test_nameargspattern_backtracking(self, adversary):
327329
class TestFunctionReturn(util.F2PyTest):
328330
sources = [util.getpath("tests", "src", "crackfortran", "gh23598.f90")]
329331

332+
@pytest.mark.slow
330333
def test_function_rettype(self):
331334
# gh-23598
332335
assert self.module.intproduct(3, 4) == 12

0 commit comments

Comments
 (0)
0