Skip to content

Commit eea1cb7

Browse files
authored
🐛 fix(execute): keep spaces in quoted shebang interpreter paths (#4064)
1 parent cff659a commit eea1cb7

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

docs/changelog/4064.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Shebang parsing now uses ``shlex``, so a quoted interpreter path that contains spaces is kept as one argument when
2+
``TOX_LIMITED_SHEBANG`` rewrites the invocation - by :user:`r3wretrhy`.

src/tox/execute/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import shlex
34
from pathlib import Path
45

56

@@ -25,7 +26,11 @@ def shebang(exe: str) -> list[str] | None:
2526
decoded = shebang_line.decode("UTF-8")
2627
except UnicodeDecodeError:
2728
return None
28-
return [i.strip() for i in decoded.strip().split() if i.strip()]
29+
try:
30+
parts = shlex.split(decoded.strip(), posix=True)
31+
except ValueError:
32+
return None
33+
return parts or None
2934

3035

3136
__all__ = [

tests/execute/local_subprocess/test_execute_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def test_shebang_found(tmp_path: Path) -> None:
1414
assert shebang(str(script_path)) == ["/bin/python", "-c"]
1515

1616

17+
def test_shebang_quoted_interpreter_path(tmp_path: Path) -> None:
18+
script_path = tmp_path / "a"
19+
script_path.write_text('#! "/Program Files/Python/python.exe" -c\n', encoding="utf-8")
20+
assert shebang(str(script_path)) == ["/Program Files/Python/python.exe", "-c"]
21+
22+
1723
def test_shebang_file_missing(tmp_path: Path) -> None:
1824
script_path = tmp_path / "a"
1925
assert shebang(str(script_path)) is None

0 commit comments

Comments
 (0)