@@ -271,7 +271,10 @@ def py_ini(self, content):
271271 @contextlib .contextmanager
272272 def script (self , content , encoding = "utf-8" ):
273273 file = Path (tempfile .mktemp (dir = os .getcwd ()) + ".py" )
274- file .write_text (content , encoding = encoding )
274+ if isinstance (content , bytes ):
275+ file .write_bytes (content )
276+ else :
277+ file .write_text (content , encoding = encoding )
275278 try :
276279 yield file
277280 finally :
@@ -624,6 +627,25 @@ def test_py_shebang_short_argv0(self):
624627 self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
625628 self .assertEqual (f'X.Y.exe -prearg "{ script } " -postarg' , data ["stdout" ].strip ())
626629
630+ def test_py_shebang_valid_bom (self ):
631+ with self .py_ini (TEST_PY_DEFAULTS ):
632+ content = "#! /usr/bin/python -prearg" .encode ("utf-8" )
633+ with self .script (b"\xEF \xBB \xBF " + content ) as script :
634+ data = self .run_py ([script , "-postarg" ])
635+ self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
636+ self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
637+ self .assertEqual (f"X.Y.exe -prearg { quote (script )} -postarg" , data ["stdout" ].strip ())
638+
639+ def test_py_shebang_invalid_bom (self ):
640+ with self .py_ini (TEST_PY_DEFAULTS ):
641+ content = "#! /usr/bin/python3 -prearg" .encode ("utf-8" )
642+ with self .script (b"\xEF \xAA \xBF " + content ) as script :
643+ data = self .run_py ([script , "-postarg" ])
644+ self .assertIn ("Invalid BOM" , data ["stderr" ])
645+ self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
646+ self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
647+ self .assertEqual (f"X.Y.exe { quote (script )} -postarg" , data ["stdout" ].strip ())
648+
627649 def test_py_handle_64_in_ini (self ):
628650 with self .py_ini ("\n " .join (["[defaults]" , "python=3.999-64" ])):
629651 # Expect this to fail, but should get oldStyleTag flipped on
0 commit comments