Skip to content

Commit 7df3a1c

Browse files
gh-95285: py.exe launcher fails with short argv0 (GH-95295) (GH-95298)
(cherry picked from commit 7ac5bb3) Co-authored-by: Steve Dower <[email protected]>
1 parent 0d812a5 commit 7df3a1c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Lib/test/test_launcher.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class RunPyMixin:
150150
@classmethod
151151
def find_py(cls):
152152
py_exe = None
153-
if sysconfig.is_python_build(True):
153+
if sysconfig.is_python_build():
154154
py_exe = Path(sys.executable).parent / PY_EXE
155155
else:
156156
for p in os.getenv("PATH").split(";"):
@@ -188,7 +188,7 @@ def find_py(cls):
188188
)
189189
return py_exe
190190

191-
def run_py(self, args, env=None, allow_fail=False, expect_returncode=0):
191+
def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=None):
192192
if not self.py_exe:
193193
self.py_exe = self.find_py()
194194

@@ -199,9 +199,12 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0):
199199
"PYLAUNCHER_DEBUG": "1",
200200
"PYLAUNCHER_DRYRUN": "1",
201201
}
202+
if not argv:
203+
argv = [self.py_exe, *args]
202204
with subprocess.Popen(
203-
[self.py_exe, *args],
205+
argv,
204206
env=env,
207+
executable=self.py_exe,
205208
stdin=subprocess.PIPE,
206209
stdout=subprocess.PIPE,
207210
stderr=subprocess.PIPE,
@@ -540,6 +543,15 @@ def test_py3_shebang_nl(self):
540543
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
541544
self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
542545

546+
def test_py_shebang_short_argv0(self):
547+
with self.py_ini(TEST_PY_COMMANDS):
548+
with self.script("#! /usr/bin/env python -prearg") as script:
549+
# Override argv to only pass "py.exe" as the command
550+
data = self.run_py([script, "-postarg"], argv=f'"py.exe" "{script}" -postarg')
551+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
552+
self.assertEqual("3.100", data["SearchInfo.tag"])
553+
self.assertEqual(f'X.Y.exe -prearg "{script}" -postarg', data["stdout"].strip())
554+
543555
def test_install(self):
544556
data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
545557
cmd = data["stdout"].strip()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :ref:`launcher` handling of command lines where it is only passed a
2+
short executable name.

PC/launcher2.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ parseCommandLine(SearchInfo *search)
580580
break;
581581
}
582582
}
583+
if (tail == search->originalCmdLine && tail[0] == L'"') {
584+
++tail;
585+
}
583586
// Without special cases, we can now fill in the search struct
584587
int tailLen = (int)(end ? (end - tail) : wcsnlen_s(tail, MAXLEN));
585588
search->executableLength = -1;

0 commit comments

Comments
 (0)