Skip to content

Commit a54aeb2

Browse files
authored
Merge pull request #32710 from compnerd/3-is-neigh
test: make `test_util` more Python 3 friendly
2 parents 1802ec9 + c1ce8b6 commit a54aeb2

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

test/lit.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ def append_to_env_path(directory):
183183
config.environment['PATH'] = \
184184
os.path.pathsep.join((directory, config.environment['PATH']))
185185

186+
if kIsWindows:
187+
config.environment['PYTHONIOENCODING'] = 'UTF8'
188+
186189
# Tweak the PATH to include the tools dir and the scripts dir.
187190
if swift_obj_root is not None:
188191
llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)

utils/incrparse/test_util.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import print_function
44

55
import argparse
6+
import io
67
import os
78
import re
89
import subprocess
@@ -21,6 +22,7 @@ def escapeCmdArg(arg):
2122

2223

2324
def run_command(cmd):
25+
cmd = list(map(lambda s: s.encode('utf-8'), cmd))
2426
print(' '.join([escapeCmdArg(arg) for arg in cmd]))
2527
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
2628

@@ -56,7 +58,7 @@ def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args,
5658
# Compute the -incremental-edit argument for swift-syntax-test
5759
column = len(pre_edit_line) + len(prefix) + 1
5860
edit_arg = '%d:%d-%d:%d=%s' % \
59-
(line_no, column, line_no, column + len(pre_edit),
61+
(line_no, column, line_no, column + len(pre_edit.encode('utf-8')),
6062
post_edit)
6163
incremental_edit_args.append('-incremental-edit')
6264
incremental_edit_args.append(edit_arg)
@@ -102,14 +104,19 @@ def parseLine(line, line_no, test_case, incremental_edit_args, reparse_args,
102104
# Nothing more to do
103105
line = ''
104106

105-
return (pre_edit_line, post_edit_line, current_reparse_start)
107+
return (pre_edit_line.encode('utf-8'),
108+
post_edit_line.encode('utf-8'),
109+
current_reparse_start)
106110

107111

108112
def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
109113
incremental_edit_args, reparse_args):
110-
with open(test_file, mode='r') as test_file_handle, \
111-
open(pre_edit_file, mode='w+b') as pre_edit_file_handle, \
112-
open(post_edit_file, mode='w+b') as post_edit_file_handle:
114+
with io.open(test_file, mode='r', encoding='utf-8',
115+
newline='\n') as test_file_handle, \
116+
io.open(pre_edit_file, mode='w+', encoding='utf-8',
117+
newline='\n') as pre_edit_file_handle, \
118+
io.open(post_edit_file, mode='w+', encoding='utf-8',
119+
newline='\n') as post_edit_file_handle:
113120

114121
current_reparse_start = None
115122

@@ -121,8 +128,8 @@ def prepareForIncrParse(test_file, test_case, pre_edit_file, post_edit_file,
121128
(pre_edit_line, post_edit_line, current_reparse_start) = \
122129
parseLineRes
123130

124-
pre_edit_file_handle.write(pre_edit_line)
125-
post_edit_file_handle.write(post_edit_line)
131+
pre_edit_file_handle.write(pre_edit_line.decode('utf-8'))
132+
post_edit_file_handle.write(post_edit_line.decode('utf-8'))
126133

127134
line_no += 1
128135

@@ -231,7 +238,7 @@ def serializeIncrParseMarkupFile(test_file, test_case, mode,
231238
if print_visual_reuse_info:
232239
print(output)
233240
except subprocess.CalledProcessError as e:
234-
raise TestFailedError(e.output)
241+
raise TestFailedError(e.output.decode('utf-8'))
235242

236243

237244
def main():

0 commit comments

Comments
 (0)