@@ -263,7 +263,10 @@ def py_ini(self, content):
263263 @contextlib .contextmanager
264264 def script (self , content , encoding = "utf-8" ):
265265 file = Path (tempfile .mktemp (dir = os .getcwd ()) + ".py" )
266- file .write_text (content , encoding = encoding )
266+ if isinstance (content , bytes ):
267+ file .write_bytes (content )
268+ else :
269+ file .write_text (content , encoding = encoding )
267270 try :
268271 yield file
269272 finally :
@@ -608,6 +611,25 @@ def test_py_shebang_short_argv0(self):
608611 self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
609612 self .assertEqual (f'X.Y.exe -prearg "{ script } " -postarg' , data ["stdout" ].strip ())
610613
614+ def test_py_shebang_valid_bom (self ):
615+ with self .py_ini (TEST_PY_DEFAULTS ):
616+ content = "#! /usr/bin/python -prearg" .encode ("utf-8" )
617+ with self .script (b"\xEF \xBB \xBF " + content ) as script :
618+ data = self .run_py ([script , "-postarg" ])
619+ self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
620+ self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
621+ self .assertEqual (f"X.Y.exe -prearg { quote (script )} -postarg" , data ["stdout" ].strip ())
622+
623+ def test_py_shebang_invalid_bom (self ):
624+ with self .py_ini (TEST_PY_DEFAULTS ):
625+ content = "#! /usr/bin/python3 -prearg" .encode ("utf-8" )
626+ with self .script (b"\xEF \xAA \xBF " + content ) as script :
627+ data = self .run_py ([script , "-postarg" ])
628+ self .assertIn ("Invalid BOM" , data ["stderr" ])
629+ self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
630+ self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
631+ self .assertEqual (f"X.Y.exe { quote (script )} -postarg" , data ["stdout" ].strip ())
632+
611633 def test_py_handle_64_in_ini (self ):
612634 with self .py_ini ("\n " .join (["[defaults]" , "python=3.999-64" ])):
613635 # Expect this to fail, but should get oldStyleTag flipped on
0 commit comments