[3.13] gh-131020: py.exe launcher does not correctly detect a BOM when searching for the shebang (GH-131021) (#131047)

gh-131020: py.exe launcher does not correctly detect a BOM when searching for the shebang (GH-131021)
(cherry picked from commit 36ef3bfe39)

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-03-10 20:54:10 +01:00 committed by GitHub
parent 30e8bd2a41
commit cf829f49de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View file

@ -271,7 +271,10 @@ class RunPyMixin:
@contextlib.contextmanager
def script(self, content, encoding="utf-8"):
file = Path(tempfile.mktemp(dir=os.getcwd()) + ".py")
file.write_text(content, encoding=encoding)
if isinstance(content, bytes):
file.write_bytes(content)
else:
file.write_text(content, encoding=encoding)
try:
yield file
finally:
@ -624,6 +627,25 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
self.assertEqual("3.100", data["SearchInfo.tag"])
self.assertEqual(f'X.Y.exe -prearg "{script}" -postarg', data["stdout"].strip())
def test_py_shebang_valid_bom(self):
with self.py_ini(TEST_PY_DEFAULTS):
content = "#! /usr/bin/python -prearg".encode("utf-8")
with self.script(b"\xEF\xBB\xBF" + content) 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 {quote(script)} -postarg", data["stdout"].strip())
def test_py_shebang_invalid_bom(self):
with self.py_ini(TEST_PY_DEFAULTS):
content = "#! /usr/bin/python3 -prearg".encode("utf-8")
with self.script(b"\xEF\xAA\xBF" + content) as script:
data = self.run_py([script, "-postarg"])
self.assertIn("Invalid BOM", data["stderr"])
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
self.assertEqual("3.100", data["SearchInfo.tag"])
self.assertEqual(f"X.Y.exe {quote(script)} -postarg", data["stdout"].strip())
def test_py_handle_64_in_ini(self):
with self.py_ini("\n".join(["[defaults]", "python=3.999-64"])):
# Expect this to fail, but should get oldStyleTag flipped on