2121import lit .ShUtil as ShUtil
2222import lit .Test as Test
2323import lit .util
24- from lit .util import to_bytes , to_string
24+ from lit .util import to_bytes
2525from lit .BooleanExpression import BooleanExpression
2626
2727
@@ -391,18 +391,14 @@ def executeBuiltinEcho(cmd, shenv):
391391 # Some tests have un-redirected echo commands to help debug test failures.
392392 # Buffer our output and return it to the caller.
393393 is_redirected = True
394- encode = lambda x : x
395394 if stdout == subprocess .PIPE :
396395 is_redirected = False
397396 stdout = StringIO ()
398397 elif kIsWindows :
399- # Reopen stdout in binary mode to avoid CRLF translation. The versions
400- # of echo we are replacing on Windows all emit plain LF, and the LLVM
401- # tests now depend on this.
402- # When we open as binary, however, this also means that we have to write
403- # 'bytes' objects to stdout instead of 'str' objects.
404- encode = lit .util .to_bytes
405- stdout = open (stdout .name , stdout .mode + "b" )
398+ # Reopen stdout with specifying `newline` to avoid CRLF translation.
399+ # The versions of echo we are replacing on Windows all emit plain LF,
400+ # and the LLVM tests now depend on this.
401+ stdout = open (stdout .name , stdout .mode , newline = '' )
406402 opened_files .append ((None , None , stdout , None ))
407403
408404 # Implement echo flags. We only support -e and -n, and not yet in
@@ -423,16 +419,15 @@ def maybeUnescape(arg):
423419 if not interpret_escapes :
424420 return arg
425421
426- arg = lit .util .to_bytes (arg )
427- return arg .decode ("unicode_escape" )
422+ return arg .encode ("utf-8" ).decode ("unicode_escape" )
428423
429424 if args :
430425 for arg in args [:- 1 ]:
431- stdout .write (encode ( maybeUnescape (arg ) ))
432- stdout .write (encode ( " " ) )
433- stdout .write (encode ( maybeUnescape (args [- 1 ]) ))
426+ stdout .write (maybeUnescape (arg ))
427+ stdout .write (" " )
428+ stdout .write (maybeUnescape (args [- 1 ]))
434429 if write_newline :
435- stdout .write (encode ( "\n " ) )
430+ stdout .write ("\n " )
436431
437432 for (name , mode , f , path ) in opened_files :
438433 f .close ()
@@ -1062,14 +1057,14 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
10621057 if out is None :
10631058 out = ""
10641059 else :
1065- out = to_string ( out .decode ("utf-8" , errors = "replace" ) )
1060+ out = out .decode ("utf-8" , errors = "replace" )
10661061 except :
10671062 out = str (out )
10681063 try :
10691064 if err is None :
10701065 err = ""
10711066 else :
1072- err = to_string ( err .decode ("utf-8" , errors = "replace" ) )
1067+ err = err .decode ("utf-8" , errors = "replace" )
10731068 except :
10741069 err = str (err )
10751070
@@ -1261,7 +1256,7 @@ def executeScriptInternal(
12611256
12621257 # Add the command output, if redirected.
12631258 for (name , path , data ) in result .outputFiles :
1264- data = to_string ( data .decode ("utf-8" , errors = "replace" ) )
1259+ data = data .decode ("utf-8" , errors = "replace" )
12651260 out += formatOutput (f"redirected output from '{ name } '" , data , limit = 1024 )
12661261 if result .stdout .strip ():
12671262 out += formatOutput ("command stdout" , result .stdout )
@@ -1471,8 +1466,8 @@ def parseIntegratedTestScriptCommands(source_path, keywords):
14711466 keyword , ln = match .groups ()
14721467 yield (
14731468 line_number ,
1474- to_string ( keyword .decode ("utf-8" ) ),
1475- to_string ( ln .decode ("utf-8" ).rstrip ("\r " ) ),
1469+ keyword .decode ("utf-8" ),
1470+ ln .decode ("utf-8" ).rstrip ("\r " ),
14761471 )
14771472 finally :
14781473 f .close ()
0 commit comments