From ba9a02833412e6a2c0f21ca73b39aa815cffead5 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 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/flopy/mbase.py b/flopy/mbase.py index 1d541e128..983e11f8c 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()) @@ -81,9 +86,9 @@ def _resolve(exe_name): # try adding/removing .exe suffix if on_windows and exe_name.lower().endswith(".exe"): - return _resolve(exe_name[:-4]) + 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)