Skip to content

Commit b040617

Browse files
gh-94772: Fix off-by-one error in Windows launcher (GH-94779)
(cherry picked from commit 407ff65) Co-authored-by: Paul Moore <[email protected]> Co-authored-by: Paul Moore <[email protected]>
1 parent 734c8b7 commit b040617

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Lib/test/test_launcher.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,30 @@ def test_py3_shebang(self):
516516
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
517517
self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
518518

519+
def test_py_shebang_nl(self):
520+
with self.py_ini(TEST_PY_COMMANDS):
521+
with self.script("#! /usr/bin/env python -prearg\n") as script:
522+
data = self.run_py([script, "-postarg"])
523+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
524+
self.assertEqual("3.100", data["SearchInfo.tag"])
525+
self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
526+
527+
def test_py2_shebang_nl(self):
528+
with self.py_ini(TEST_PY_COMMANDS):
529+
with self.script("#! /usr/bin/env python2 -prearg\n") as script:
530+
data = self.run_py([script, "-postarg"])
531+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
532+
self.assertEqual("3.100-32", data["SearchInfo.tag"])
533+
self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
534+
535+
def test_py3_shebang_nl(self):
536+
with self.py_ini(TEST_PY_COMMANDS):
537+
with self.script("#! /usr/bin/env python3 -prearg\n") as script:
538+
data = self.run_py([script, "-postarg"])
539+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
540+
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
541+
self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
542+
519543
def test_install(self):
520544
data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
521545
cmd = data["stdout"].strip()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect handling of shebang lines in py.exe launcher

PC/launcher2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,9 @@ checkShebang(SearchInfo *search)
874874
while (--bytesRead > 0 && *++b != '\r' && *b != '\n') { }
875875
wchar_t *shebang;
876876
int shebangLength;
877-
int exitCode = _decodeShebang(search, start, (int)(b - start + 1), onlyUtf8, &shebang, &shebangLength);
877+
// We add 1 when bytesRead==0, as in that case we hit EOF and b points
878+
// to the last character in the file, not the newline
879+
int exitCode = _decodeShebang(search, start, (int)(b - start + (bytesRead == 0)), onlyUtf8, &shebang, &shebangLength);
878880
if (exitCode) {
879881
return exitCode;
880882
}

0 commit comments

Comments
 (0)