Merge pull request #979 from OlaiFG7/carry/old-9856-mc-command-pathext

mc_command: PATHEXT fallback for extensionless paths with spaces (OLD-9856)
This commit is contained in:
Aslan2142
2026-12-01 16:14:09 +01:00
committed by GitHub
2 changed files with 19 additions and 2 deletions

View File

@ -86,6 +86,16 @@ class SplitCommandTest(unittest.TestCase):
self.assertEqual(mc_command.split_command(raw), [raw])
which.assert_called_once_with(raw)
def test_pathext_fallback_when_which_misses(self):
raw = "C:\\Program Files\\LLVM\\bin\\llvm-mc"
with mock.patch.object(mc_command.os, "name", "nt"), \
mock.patch.object(mc_command.os, "pathsep", ";"), \
mock.patch.dict(mc_command.os.environ, {"PATHEXT": ".COM;.EXE"}, clear=False), \
mock.patch.object(mc_command.shutil, "which", return_value=None) as which, \
mock.patch.object(mc_command.os.path, "isfile", side_effect=lambda p: p == raw + ".EXE"):
self.assertEqual(mc_command.split_command(raw), [raw])
which.assert_called_once_with(raw)
@unittest.skipUnless(os.name == "nt", "Windows resolves a missing .exe")
def test_real_path_with_spaces_and_no_extension(self):
with tempfile.TemporaryDirectory() as tmp:

View File

@ -71,10 +71,17 @@ def split_command(raw: str) -> list[str]:
return []
if os.path.isfile(raw):
return [raw] # a plain path, spaces and all
if ("/" in raw or "\\" in raw) and shutil.which(raw):
if "/" in raw or "\\" in raw:
# A path whose extension is left out (C:\Program Files\LLVM\bin\llvm-mc): Windows finds llvm-mc.exe
# for it, but it is no file under that exact name, so it used to be split at its spaces.
return [raw]
# Fallback for interpreters before Python 3.12 on Windows, where shutil.which() does not apply PATHEXT
# when a directory component is present.
if shutil.which(raw):
return [raw]
if os.name == "nt":
for ext in os.environ.get("PATHEXT", ".COM;.EXE;.BAT;.CMD").split(os.pathsep):
if ext and os.path.isfile(raw + ext):
return [raw]
try:
return split_windows_command(raw) if os.name == "nt" else shlex.split(raw)
except ValueError as e: