Merge escape if checks + include COMSPEC check on Windows

This commit is contained in:
Roy Nieterau 2025-02-04 15:20:28 +01:00
parent 516dd2d7ce
commit ed0f5c8d7f

View file

@ -108,31 +108,29 @@ def run_subprocess(*args, **kwargs):
| getattr(subprocess, "CREATE_NO_WINDOW", 0) | getattr(subprocess, "CREATE_NO_WINDOW", 0)
) )
# Escape parentheses for bash # Escape special characters in certain shells
if ( if (
kwargs.get("shell") is True kwargs.get("shell") is True
and len(args) == 1 and len(args) == 1
and isinstance(args[0], str) and isinstance(args[0], str)
and os.getenv("SHELL") in ("/bin/bash", "/bin/sh")
): ):
new_arg = ( # Escape parentheses for bash
args[0] if os.getenv("SHELL") in ("/bin/bash", "/bin/sh"):
.replace("(", "\\(") new_arg = (
.replace(")", "\\)") args[0]
) .replace("(", "\\(")
args = (new_arg, ) .replace(")", "\\)")
)
args = (new_arg,)
# Escape & on Windows in shell with `cmd.exe` using ^&
elif (
platform.system().lower() == "windows"
and os.getenv("COMSPEC").endswith("cmd.exe")
):
new_arg = args[0].replace("&", "^&")
args = (new_arg, )
# Escape & on Windows in shell using ^& # Get environments from kwarg or use current process environments if were
if (
kwargs.get("shell") is True
and len(args) == 1
and isinstance(args[0], str)
and platform.system().lower() == "windows"
):
new_arg = args[0].replace("&", "^&")
args = (new_arg, )
# Get environents from kwarg or use current process environments if were
# not passed. # not passed.
env = kwargs.get("env") or os.environ env = kwargs.get("env") or os.environ
# Make sure environment contains only strings # Make sure environment contains only strings