Skip to content

Commit 10de4e1

Browse files
committed
set encoding for open()
1 parent dc3f0c7 commit 10de4e1

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def executeBuiltinEcho(cmd, shenv):
397397
# Reopen stdout with specifying `newline` to avoid CRLF translation.
398398
# The versions of echo we are replacing on Windows all emit plain LF,
399399
# and the LLVM tests now depend on this.
400-
stdout = open(stdout.name, stdout.mode, newline="")
400+
stdout = open(stdout.name, stdout.mode, encoding="utf-8", newline="")
401401
opened_files.append((None, None, stdout, None))
402402

403403
# Implement echo flags. We only support -e and -n, and not yet in
@@ -695,7 +695,7 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
695695
else:
696696
# Make sure relative paths are relative to the cwd.
697697
redir_filename = os.path.join(cmd_shenv.cwd, name)
698-
fd = open(redir_filename, mode)
698+
fd = open(redir_filename, mode, encoding="utf-8")
699699
# Workaround a Win32 and/or subprocess bug when appending.
700700
#
701701
# FIXME: Actually, this is probably an instance of PR6753.
@@ -1311,13 +1311,6 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
13111311
script += ".bat"
13121312

13131313
# Write script file
1314-
mode = "w"
1315-
open_kwargs = {}
1316-
if litConfig.isWindows and not isWin32CMDEXE:
1317-
mode += "b" # Avoid CRLFs when writing bash scripts.
1318-
else:
1319-
open_kwargs["encoding"] = "utf-8"
1320-
f = open(script, mode, **open_kwargs)
13211314
if isWin32CMDEXE:
13221315
for i, ln in enumerate(commands):
13231316
match = re.fullmatch(kPdbgRegex, ln)
@@ -1326,8 +1319,9 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
13261319
commands[i] = match.expand(
13271320
"echo '\\1' > nul && " if command else "echo '\\1' > nul"
13281321
)
1329-
f.write("@echo on\n")
1330-
f.write("\n@if %ERRORLEVEL% NEQ 0 EXIT\n".join(commands))
1322+
with open(script, "w", encoding="utf-8") as f:
1323+
f.write("@echo on\n")
1324+
f.write("\n@if %ERRORLEVEL% NEQ 0 EXIT\n".join(commands))
13311325
else:
13321326
for i, ln in enumerate(commands):
13331327
match = re.fullmatch(kPdbgRegex, ln)
@@ -1366,8 +1360,6 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
13661360
# seen the latter manage to terminate the shell running lit.
13671361
if command:
13681362
commands[i] += f" && {{ {command}; }}"
1369-
if test.config.pipefail:
1370-
f.write(b"set -o pipefail;" if mode == "wb" else "set -o pipefail;")
13711363

13721364
# Manually export any DYLD_* variables used by dyld on macOS because
13731365
# otherwise they are lost when the shell executable is run, before the
@@ -1377,14 +1369,14 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
13771369
for k, v in test.config.environment.items()
13781370
if k.startswith("DYLD_")
13791371
)
1380-
f.write(bytes(env_str, "utf-8") if mode == "wb" else env_str)
1381-
f.write(b"set -x;" if mode == "wb" else "set -x;")
1382-
if mode == "wb":
1383-
f.write(bytes("{ " + "; } &&\n{ ".join(commands) + "; }", "utf-8"))
1384-
else:
1372+
1373+
with open(script, "w", encoding="utf-8", newline="") as f:
1374+
if test.config.pipefail:
1375+
f.write("set -o pipefail;")
1376+
f.write(env_str)
1377+
f.write("set -x;")
13851378
f.write("{ " + "; } &&\n{ ".join(commands) + "; }")
1386-
f.write(b"\n" if mode == "wb" else "\n")
1387-
f.close()
1379+
f.write("\n")
13881380

13891381
if isWin32CMDEXE:
13901382
command = ["cmd", "/c", script]

llvm/utils/lit/lit/reports.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def write_results(self, tests, elapsed):
2929
fd, _ = tempfile.mkstemp(
3030
suffix=ext, prefix=f"{filename}.", dir=os.path.dirname(self.output_file)
3131
)
32-
report_file = os.fdopen(fd, "w")
32+
report_file = os.fdopen(fd, "w", encoding="utf-8")
3333
else:
3434
# Overwrite if the results already exist.
35-
report_file = open(self.output_file, "w")
35+
report_file = open(self.output_file, "w", encoding="utf-8")
3636

3737
with report_file:
3838
self._write_results_to_file(tests, elapsed, report_file)

0 commit comments

Comments
 (0)