]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94772: Fix off-by-one error in Windows launcher (GH-94779)
authorPaul Moore <p.f.moore@gmail.com>
Sat, 16 Jul 2022 09:02:22 +0000 (10:02 +0100)
committerGitHub <noreply@github.com>
Sat, 16 Jul 2022 09:02:22 +0000 (10:02 +0100)
Lib/test/test_launcher.py
Misc/NEWS.d/next/Windows/2022-07-12-20-45-43.gh-issue-94772.uNMmdG.rst [new file with mode: 0644]
PC/launcher2.c

index cd7b944f98f586dd349396d3377b45b6b7804996..50a2e8c03d6473f655a7a362b12baa321bb70031 100644 (file)
@@ -515,6 +515,30 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
         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_nl(self):
+        with self.py_ini(TEST_PY_COMMANDS):
+            with self.script("#! /usr/bin/env python -prearg\n") as script:
+                data = self.run_py([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_py2_shebang_nl(self):
+        with self.py_ini(TEST_PY_COMMANDS):
+            with self.script("#! /usr/bin/env python2 -prearg\n") as script:
+                data = self.run_py([script, "-postarg"])
+        self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
+        self.assertEqual("3.100-32", data["SearchInfo.tag"])
+        self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
+
+    def test_py3_shebang_nl(self):
+        with self.py_ini(TEST_PY_COMMANDS):
+            with self.script("#! /usr/bin/env python3 -prearg\n") as script:
+                data = self.run_py([script, "-postarg"])
+        self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
+        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_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-12-20-45-43.gh-issue-94772.uNMmdG.rst b/Misc/NEWS.d/next/Windows/2022-07-12-20-45-43.gh-issue-94772.uNMmdG.rst
new file mode 100644 (file)
index 0000000..bb5ab75
--- /dev/null
@@ -0,0 +1 @@
+Fix incorrect handling of shebang lines in py.exe launcher
index ae11f4f024a9040ad991c90e0f136fdef1ad14c3..c8ed1b0f7c8a6d1f2f10b7928a924729a961b796 100644 (file)
@@ -874,7 +874,9 @@ checkShebang(SearchInfo *search)
     while (--bytesRead > 0 && *++b != '\r' && *b != '\n') { }
     wchar_t *shebang;
     int shebangLength;
-    int exitCode = _decodeShebang(search, start, (int)(b - start + 1), onlyUtf8, &shebang, &shebangLength);
+    // We add 1 when bytesRead==0, as in that case we hit EOF and b points
+    // to the last character in the file, not the newline
+    int exitCode = _decodeShebang(search, start, (int)(b - start + (bytesRead == 0)), onlyUtf8, &shebang, &shebangLength);
     if (exitCode) {
         return exitCode;
     }