From 1ea2e190d32078141bba78e8343b82197a307307 Mon Sep 17 00:00:00 2001 From: martclanor Date: Fri, 27 Dec 2024 19:23:53 +0100 Subject: [PATCH] prevent infinite recursion in removing/adding .exe suffix on windows --- flopy/mbase.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/flopy/mbase.py b/flopy/mbase.py index 1d541e128..f2d05e6b1 100644 --- a/flopy/mbase.py +++ b/flopy/mbase.py @@ -64,7 +64,12 @@ def resolve_exe(exe_name: Union[str, os.PathLike], forgive: bool = False) -> str str: absolute path to the executable """ - def _resolve(exe_name): + def _resolve(exe_name, checked=set()): + # Prevent infinite recursion by checking if exe_name has been checked + if exe_name in checked: + return None + checked.add(exe_name) + # exe_name is found (not None), ensure absolute path is returned if exe := which(exe_name): return which(Path(exe).resolve()) @@ -80,10 +85,10 @@ def _resolve(exe_name): return exe # try adding/removing .exe suffix - if on_windows and exe_name.lower().endswith(".exe"): - return _resolve(exe_name[:-4]) + if exe_name.lower().endswith(".exe"): + return _resolve(exe_name[:-4], checked) elif on_windows and "." not in Path(exe_name).stem: - return _resolve(f"{exe_name}.exe") + return _resolve(f"{exe_name}.exe", checked) exe_path = _resolve(exe_name)