Skip to content

Commit

Permalink
MAINT,BUG: Never import distutils above 3.12 [f2py] (numpy#25123)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke authored and charris committed Dec 19, 2023
1 parent ba4cc02 commit 20fb4e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
51 changes: 31 additions & 20 deletions numpy/f2py/f2py2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# outmess=sys.stdout.write
show = pprint.pprint
outmess = auxfuncs.outmess
MESON_ONLY_VER = (sys.version_info >= (3, 12))

__usage__ =\
f"""Usage:
Expand Down Expand Up @@ -548,9 +549,10 @@ def preparse_sysargv():
sys.argv = [sys.argv[0]] + remaining_argv

backend_key = args.backend
if sys.version_info >= (3, 12) and backend_key == 'distutils':
outmess("Cannot use distutils backend with Python 3.12, using meson backend instead.\n")
backend_key = 'meson'
if MESON_ONLY_VER and backend_key == 'distutils':
outmess("Cannot use distutils backend with Python>=3.12,"
" using meson backend instead.\n")
backend_key = "meson"

return {
"dependencies": args.dependencies or [],
Expand Down Expand Up @@ -625,21 +627,27 @@ def run_compile():
for s in flib_flags:
v = '--fcompiler='
if s[:len(v)] == v:
from numpy.distutils import fcompiler
fcompiler.load_all_fcompiler_classes()
allowed_keys = list(fcompiler.fcompiler_class.keys())
nv = ov = s[len(v):].lower()
if ov not in allowed_keys:
vmap = {} # XXX
try:
nv = vmap[ov]
except KeyError:
if ov not in vmap.values():
print('Unknown vendor: "%s"' % (s[len(v):]))
nv = ov
i = flib_flags.index(s)
flib_flags[i] = '--fcompiler=' + nv
continue
if MESON_ONLY_VER or backend_key == 'meson':
outmess(
"--fcompiler cannot be used with meson,"
"set compiler with the FC environment variable\n"
)
else:
from numpy.distutils import fcompiler
fcompiler.load_all_fcompiler_classes()
allowed_keys = list(fcompiler.fcompiler_class.keys())
nv = ov = s[len(v):].lower()
if ov not in allowed_keys:
vmap = {} # XXX
try:
nv = vmap[ov]
except KeyError:
if ov not in vmap.values():
print('Unknown vendor: "%s"' % (s[len(v):]))
nv = ov
i = flib_flags.index(s)
flib_flags[i] = '--fcompiler=' + nv
continue
for s in del_list:
i = flib_flags.index(s)
del flib_flags[i]
Expand Down Expand Up @@ -727,8 +735,11 @@ def validate_modulename(pyf_files, modulename='untitled'):
def main():
if '--help-link' in sys.argv[1:]:
sys.argv.remove('--help-link')
from numpy.distutils.system_info import show_all
show_all()
if MESON_ONLY_VER:
outmess("Use --dep for meson builds\n")
else:
from numpy.distutils.system_info import show_all
show_all()
return

# Probably outdated options that were not working before 1.16
Expand Down
32 changes: 32 additions & 0 deletions numpy/f2py/tests/test_f2py2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ def test_untitled_cli(capfd, hello_world_f90, monkeypatch):
assert "untitledmodule.c" in out


@pytest.mark.skipif((platform.system() != 'Linux') or (sys.version_info <= (3, 12)), reason='Compiler and 3.12 required')
def test_no_py312_distutils_fcompiler(capfd, hello_world_f90, monkeypatch):
"""Check that no distutils imports are performed on 3.12
CLI :: --fcompiler --help-link --backend distutils
"""
MNAME = "hi"
foutl = get_io_paths(hello_world_f90, mname=MNAME)
ipath = foutl.f90inp
monkeypatch.setattr(
sys, "argv", f"f2py {ipath} -c --fcompiler=gfortran -m {MNAME}".split()
)
with util.switchdir(ipath.parent):
f2pycli()
out, _ = capfd.readouterr()
assert "--fcompiler cannot be used with meson" in out
monkeypatch.setattr(
sys, "argv", f"f2py --help-link".split()
)
with util.switchdir(ipath.parent):
f2pycli()
out, _ = capfd.readouterr()
assert "Use --dep for meson builds" in out
MNAME = "hi2" # Needs to be different for a new -c
monkeypatch.setattr(
sys, "argv", f"f2py {ipath} -c -m {MNAME} --backend distutils".split()
)
with util.switchdir(ipath.parent):
f2pycli()
out, _ = capfd.readouterr()
assert "Cannot use distutils backend with Python>=3.12" in out


@pytest.mark.xfail
def test_f2py_skip(capfd, retreal_f77, monkeypatch):
"""Tests that functions can be skipped
Expand Down

0 comments on commit 20fb4e4

Please sign in to comment.