Skip to content

Commit 407ff65

Browse files
authored
gh-94772: Fix off-by-one error in Windows launcher (GH-94779)
1 parent bbb2ab7 commit 407ff65

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
@@ -515,6 +515,30 @@ def test_py3_shebang(self):
515515
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
516516
self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
517517

518+
def test_py_shebang_nl(self):
519+
with self.py_ini(TEST_PY_COMMANDS):
520+
with self.script("#! /usr/bin/env python -prearg\n") as script:
521+
data = self.run_py([script, "-postarg"])
522+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
523+
self.assertEqual("3.100", data["SearchInfo.tag"])
524+
self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
525+
526+
def test_py2_shebang_nl(self):
527+
with self.py_ini(TEST_PY_COMMANDS):
528+
with self.script("#! /usr/bin/env python2 -prearg\n") as script:
529+
data = self.run_py([script, "-postarg"])
530+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
531+
self.assertEqual("3.100-32", data["SearchInfo.tag"])
532+
self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
533+
534+
def test_py3_shebang_nl(self):
535+
with self.py_ini(TEST_PY_COMMANDS):
536+
with self.script("#! /usr/bin/env python3 -prearg\n") as script:
537+
data = self.run_py([script, "-postarg"])
538+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
539+
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
540+
self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
541+
518542
def test_install(self):
519543
data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
520544
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)