Skip to content

Commit 9fda445

Browse files
author
Thomas Preud'homme
committed
[LNT] Python 3 support: subprocess IO as text
Summary: In Python 3, subprocess stdin, stdout and stderr are in binary mode by default. Trying to print or parse those as text as is done thoughought in LNT then causes byte/str errors. This commits sets the universal_newlines parameter to True when invoking subprocess methods involving input or output to request those to be in text mode. It also adapt _check_output to work whether universal_newlines is set to True in the caller or not. Finally, it cleans up 2 cases subprocess method call where the output file objects are stored in a variable but not used. Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls, leandron, PrzemekWirkus Reviewed By: PrzemekWirkus Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D68938
1 parent bf6972e commit 9fda445

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

lnt/server/db/rules/rule_update_profile_stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def update_profile_stats(session, ts, run_id):
2828

2929
dt = time.time()
3030
blocks = subprocess.check_output("du -s -k %s" % profile_path,
31-
shell=True).split('\t')[0]
31+
shell=True,
32+
universal_newlines=True).split('\t')[0]
3233
kb = float(blocks) # 1024 byte blocks.
3334

3435
history.append((dt, kb))

lnt/testing/util/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def capture_with_result(args, include_stderr=False):
6161
if include_stderr:
6262
stderr = subprocess.STDOUT
6363
try:
64-
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr)
64+
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr,
65+
universal_newlines=True)
6566
except OSError as e:
6667
if e.errno == errno.ENOENT:
6768
fatal('no such file or directory: %r when running %s.' %

lnt/tests/compile.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def runN(args, N, cwd, preprocess_cmd=None, env=None, sample_mem=False,
8181
stdout=subprocess.PIPE,
8282
stderr=subprocess.PIPE,
8383
env=env,
84-
cwd=cwd)
84+
cwd=cwd,
85+
universal_newlines=True)
8586
runn_stdout, runn_stderr = p.communicate()
8687
res = p.returncode
8788

@@ -332,13 +333,15 @@ def test_build(base_name, run_info, variables, project, build_config, num_jobs,
332333
stdin=None,
333334
stdout=subprocess.PIPE,
334335
stderr=subprocess.PIPE,
335-
cwd=source_path)
336+
cwd=source_path,
337+
universal_newlines=True)
336338
else:
337339
p = subprocess.Popen(args=['unzip', '-q', archive_path],
338340
stdin=None,
339341
stdout=subprocess.PIPE,
340342
stderr=subprocess.PIPE,
341-
cwd=source_path)
343+
cwd=source_path,
344+
universal_newlines=True)
342345
stdout, stderr = p.communicate()
343346
if p.wait() != 0:
344347
fatal(("unable to extract archive %r at %r\n"
@@ -356,7 +359,8 @@ def test_build(base_name, run_info, variables, project, build_config, num_jobs,
356359
stdin=None,
357360
stdout=subprocess.PIPE,
358361
stderr=subprocess.PIPE,
359-
cwd=source_path)
362+
cwd=source_path,
363+
universal_newlines=True)
360364
stdout, stderr = p.communicate()
361365
if p.wait() != 0:
362366
fatal(("unable to apply patch file %r in %r\n"

lnt/tests/nt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def execute_command(test_log, basedir, args, report_dir):
513513

514514
p = subprocess.Popen(args=args, stdin=None, stdout=logfile,
515515
stderr=subprocess.STDOUT, cwd=basedir,
516-
env=os.environ)
516+
env=os.environ, universal_newlines=True)
517517

518518
if report_dir is not None:
519519
while p.poll() is None:

lnt/tests/test_suite.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ def _check_output(self, *args, **kwargs):
424424
if 'cwd' in kwargs:
425425
logger.info(' (In %s)' % kwargs['cwd'])
426426
output = subprocess.check_output(*args, **kwargs)
427-
sys.stdout.write(output)
427+
if kwargs.get('universal_newlines', False):
428+
sys.stdout.write(output)
429+
else:
430+
sys.stdout.buffer.write(output)
428431
return output
429432

430433
def _clean(self, path):
@@ -652,7 +655,8 @@ def _get_lnt_code(self, code):
652655
def _extract_cmake_vars_from_cache(self):
653656
assert self.configured is True
654657
cmake_lah_output = self._check_output(
655-
[self.opts.cmake] + ['-LAH', '-N'] + [self._base_path])
658+
[self.opts.cmake] + ['-LAH', '-N'] + [self._base_path],
659+
universal_newlines=True)
656660
pattern2var = [
657661
(re.compile("^%s:[^=]*=(.*)$" % cmakevar), cmakevar)
658662
for cmakevar in (
@@ -875,13 +879,14 @@ def diagnose(self):
875879

876880
logger.info(' '.join(cmd_temps))
877881

878-
out = subprocess.check_output(cmd_temps)
882+
out = subprocess.check_output(cmd_temps, universal_newlines=True)
879883
logger.info(out)
880884

881885
# Figure out our test's target.
882886
make_cmd = [self.opts.make, "VERBOSE=1", 'help']
883887

884-
make_targets = subprocess.check_output(make_cmd)
888+
make_targets = subprocess.check_output(make_cmd,
889+
universal_newlines=True)
885890
matcher = re.compile(r"^\.\.\.\s{}$".format(short_name),
886891
re.MULTILINE | re.IGNORECASE)
887892
if not matcher.search(make_targets):
@@ -894,15 +899,17 @@ def diagnose(self):
894899
logger.info(" ".join(make_deps))
895900
p = subprocess.Popen(make_deps,
896901
stdout=subprocess.PIPE,
897-
stderr=subprocess.STDOUT)
902+
stderr=subprocess.STDOUT,
903+
universal_newlines=True)
898904
std_out, std_err = p.communicate()
899905
logger.info(std_out)
900906

901907
make_save_temps = [self.opts.make, "VERBOSE=1", short_name]
902908
logger.info(" ".join(make_save_temps))
903909
p = subprocess.Popen(make_save_temps,
904910
stdout=subprocess.PIPE,
905-
stderr=subprocess.STDOUT)
911+
stderr=subprocess.STDOUT,
912+
universal_newlines=True)
906913
std_out, std_err = p.communicate()
907914
logger.info(std_out)
908915
with open(report_path + "/build.log", 'w') as f:
@@ -926,13 +933,14 @@ def diagnose(self):
926933

927934
logger.info(' '.join(cmd_time_report))
928935

929-
out = subprocess.check_output(cmd_time_report)
936+
out = subprocess.check_output(cmd_time_report, universal_newlines=True)
930937
logger.info(out)
931938

932939
make_time_report = [self.opts.make, "VERBOSE=1", short_name]
933940
p = subprocess.Popen(make_time_report,
934941
stdout=subprocess.PIPE,
935-
stderr=subprocess.PIPE)
942+
stderr=subprocess.PIPE,
943+
universal_newlines=True)
936944
std_out, std_err = p.communicate()
937945

938946
with open(report_path + "/time-report.txt", 'w') as f:
@@ -944,13 +952,15 @@ def diagnose(self):
944952

945953
logger.info(' '.join(cmd_stats_report))
946954

947-
out = subprocess.check_output(cmd_stats_report)
955+
out = subprocess.check_output(cmd_stats_report,
956+
universal_newlines=True)
948957
logger.info(out)
949958

950959
make_stats_report = [self.opts.make, "VERBOSE=1", short_name]
951960
p = subprocess.Popen(make_stats_report,
952961
stdout=subprocess.PIPE,
953-
stderr=subprocess.PIPE)
962+
stderr=subprocess.PIPE,
963+
universal_newlines=True)
954964
std_out, std_err = p.communicate()
955965

956966
with open(report_path + "/stats-report.txt", 'w') as f:
@@ -974,19 +984,20 @@ def diagnose(self):
974984
cmd_iprofiler = cmd + ['-DTEST_SUITE_RUN_UNDER=' + iprofiler]
975985
print(' '.join(cmd_iprofiler))
976986

977-
out = subprocess.check_output(cmd_iprofiler)
987+
subprocess.check_output(cmd_iprofiler, universal_newlines=True)
978988

979989
os.chdir(local_path)
980990
make_iprofiler_temps = [self.opts.make, "VERBOSE=1", short_name]
981991
p = subprocess.Popen(make_iprofiler_temps,
982992
stdout=subprocess.PIPE,
983993
stderr=subprocess.PIPE)
984-
std_out, std_err = p.communicate()
994+
p.communicate()
985995
logger.warning("Using sudo to collect execution trace.")
986996
make_save_temps = sudo + [self.opts.lit, short_name + ".test"]
987997
p = subprocess.Popen(make_save_temps,
988998
stdout=subprocess.PIPE,
989-
stderr=subprocess.PIPE)
999+
stderr=subprocess.PIPE,
1000+
universal_newlines=True)
9901001
std_out, std_err = p.communicate()
9911002
sys.stdout.write(std_out)
9921003
sys.stderr.write(std_err)

0 commit comments

Comments
 (0)