[Python-checkins] gh-95285: py.exe launcher fails with short argv0 (GH-95295) (GH-95298)

ambv webhook-mailer at python.org
Wed Jul 27 04:07:11 EDT 2022


https://github.com/python/cpython/commit/7df3a1ce4bf820fb783cc87f2e8ab2abe5ef43fa
commit: 7df3a1ce4bf820fb783cc87f2e8ab2abe5ef43fa
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2022-07-27T10:06:59+02:00
summary:

gh-95285: py.exe launcher fails with short argv0 (GH-95295) (GH-95298)

(cherry picked from commit 7ac5bb3e6a1cf780aea8164fdba09db993a21d6f)

Co-authored-by: Steve Dower <steve.dower at python.org>

files:
A Misc/NEWS.d/next/Windows/2022-07-26-20-33-12.gh-issue-95285.w6fa22.rst
M Lib/test/test_launcher.py
M PC/launcher2.c

diff --git a/Lib/test/test_launcher.py b/Lib/test/test_launcher.py
index 201aba660abee..0b7cbaad3f582 100644
--- a/Lib/test/test_launcher.py
+++ b/Lib/test/test_launcher.py
@@ -150,7 +150,7 @@ class RunPyMixin:
     @classmethod
     def find_py(cls):
         py_exe = None
-        if sysconfig.is_python_build(True):
+        if sysconfig.is_python_build():
             py_exe = Path(sys.executable).parent / PY_EXE
         else:
             for p in os.getenv("PATH").split(";"):
@@ -188,7 +188,7 @@ def find_py(cls):
             )
         return py_exe
 
-    def run_py(self, args, env=None, allow_fail=False, expect_returncode=0):
+    def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=None):
         if not self.py_exe:
             self.py_exe = self.find_py()
 
@@ -199,9 +199,12 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0):
             "PYLAUNCHER_DEBUG": "1",
             "PYLAUNCHER_DRYRUN": "1",
         }
+        if not argv:
+            argv = [self.py_exe, *args]
         with subprocess.Popen(
-            [self.py_exe, *args],
+            argv,
             env=env,
+            executable=self.py_exe,
             stdin=subprocess.PIPE,
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
@@ -540,6 +543,15 @@ def test_py3_shebang_nl(self):
         self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
         self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
 
+    def test_py_shebang_short_argv0(self):
+        with self.py_ini(TEST_PY_COMMANDS):
+            with self.script("#! /usr/bin/env python -prearg") as script:
+                # Override argv to only pass "py.exe" as the command
+                data = self.run_py([script, "-postarg"], argv=f'"py.exe" "{script}" -postarg')
+        self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
+        self.assertEqual("3.100", data["SearchInfo.tag"])
+        self.assertEqual(f'X.Y.exe -prearg "{script}" -postarg', data["stdout"].strip())
+
     def test_install(self):
         data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
         cmd = data["stdout"].strip()
diff --git a/Misc/NEWS.d/next/Windows/2022-07-26-20-33-12.gh-issue-95285.w6fa22.rst b/Misc/NEWS.d/next/Windows/2022-07-26-20-33-12.gh-issue-95285.w6fa22.rst
new file mode 100644
index 0000000000000..76b38e7803cca
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2022-07-26-20-33-12.gh-issue-95285.w6fa22.rst
@@ -0,0 +1,2 @@
+Fix :ref:`launcher` handling of command lines where it is only passed a
+short executable name.
diff --git a/PC/launcher2.c b/PC/launcher2.c
index c8ed1b0f7c8a6..31b5617771e9b 100644
--- a/PC/launcher2.c
+++ b/PC/launcher2.c
@@ -580,6 +580,9 @@ parseCommandLine(SearchInfo *search)
             break;
         }
     }
+    if (tail == search->originalCmdLine && tail[0] == L'"') {
+        ++tail;
+    }
     // Without special cases, we can now fill in the search struct
     int tailLen = (int)(end ? (end - tail) : wcsnlen_s(tail, MAXLEN));
     search->executableLength = -1;



More information about the Python-checkins mailing list