Skip to content

Commit fd6ea21

Browse files
committed
pythongh-119070: Fix py.exe handling of /usr/bin/env commands missing extension (pythonGH-119426)
1 parent a62681c commit fd6ea21

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Lib/test/test_launcher.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,36 @@ def test_literal_shebang_invalid_template(self):
717717
f"{expect} arg1 {script}",
718718
data["stdout"].strip(),
719719
)
720+
721+
def test_shebang_command_in_venv(self):
722+
stem = "python-that-is-not-on-path"
723+
724+
# First ensure that our test name doesn't exist, and the launcher does
725+
# not match any installed env
726+
with self.script(f'#! /usr/bin/env {stem} arg1') as script:
727+
data = self.run_py([script], expect_returncode=103)
728+
729+
with self.fake_venv() as (venv_exe, env):
730+
# Put a "normal" Python on PATH as a distraction.
731+
# The active VIRTUAL_ENV should be preferred when the name isn't an
732+
# exact match.
733+
exe = Path(Path(venv_exe).name).absolute()
734+
exe.touch()
735+
self.addCleanup(exe.unlink)
736+
env["PATH"] = f"{exe.parent};{os.environ['PATH']}"
737+
738+
with self.script(f'#! /usr/bin/env {stem} arg1') as script:
739+
data = self.run_py([script], env=env)
740+
self.assertEqual(data["stdout"].strip(), f"{quote(venv_exe)} arg1 {quote(script)}")
741+
742+
with self.script(f'#! /usr/bin/env {exe.stem} arg1') as script:
743+
data = self.run_py([script], env=env)
744+
self.assertEqual(data["stdout"].strip(), f"{quote(exe)} arg1 {quote(script)}")
745+
746+
def test_shebang_executable_extension(self):
747+
with self.script('#! /usr/bin/env python3.12') as script:
748+
data = self.run_py([script])
749+
expect = "# Search PATH for python3.12.exe"
750+
actual = [line.strip() for line in data["stderr"].splitlines()
751+
if line.startswith("# Search PATH")]
752+
self.assertEqual([expect], actual)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes ``py.exe`` handling of shebangs like ``/usr/bin/env python3.12``,
2+
which were previously interpreted as ``python3.exe`` instead of
3+
``python3.12.exe``.

PC/launcher2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ searchPath(SearchInfo *search, const wchar_t *shebang, int shebangLength)
846846
}
847847

848848
wchar_t filename[MAXLEN];
849-
if (wcsncpy_s(filename, MAXLEN, command, lastDot)) {
849+
if (wcsncpy_s(filename, MAXLEN, command, commandLength)) {
850850
return RC_BAD_VIRTUAL_PATH;
851851
}
852852

0 commit comments

Comments
 (0)