Skip to content

Commit 38eb898

Browse files
saschanazkripken
authored andcommitted
Use subprocess.run + universal_newlines=True (#5843)
* explicit utf-8 environment ...as the Ubuntu images on Docker defaults to LANG=C (ascii) * python 3 style run_process utility * split by \n when universal_newlines * use run_process in many places
1 parent 1238631 commit 38eb898

14 files changed

+85
-57
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# For travis
22
FROM buildpack-deps:xenial
33
SHELL ["/bin/bash", "-c"]
4-
ENV DEBIAN_FRONTEND noninteractive
4+
ENV DEBIAN_FRONTEND=noninteractive LANG=C.UTF-8
55
RUN mkdir -p /root/emscripten/
66
COPY . /root/emscripten/
77

emcc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import os, sys, shutil, tempfile, subprocess, shlex, time, re, logging
3232
from subprocess import PIPE
3333
from tools import shared, jsrun, system_libs
34-
from tools.shared import execute, suffix, unsuffixed, unsuffixed_basename, WINDOWS, safe_move
34+
from tools.shared import execute, suffix, unsuffixed, unsuffixed_basename, WINDOWS, safe_move, run_process
3535
from tools.response_file import read_response_file
3636
import tools.line_endings
3737

@@ -386,8 +386,8 @@ def run():
386386
args = [x for x in sys.argv if x != '--cflags']
387387
with misc_temp_files.get_file(suffix='.o') as temp_target:
388388
input_file = 'hello_world.c'
389-
out, err = subprocess.Popen([shared.PYTHON] + args + [shared.path_from_root('tests', input_file), '-c', '-o', temp_target], stderr=subprocess.PIPE, env=debug_env).communicate()
390-
lines = [x for x in err.split(os.linesep) if shared.CLANG_CC in x and input_file in x]
389+
err = run_process([shared.PYTHON] + args + [shared.path_from_root('tests', input_file), '-c', '-o', temp_target], stderr=subprocess.PIPE, env=debug_env).stderr
390+
lines = [x for x in err.split('\n') if shared.CLANG_CC in x and input_file in x]
391391
line = re.search('running: (.*)', lines[0]).group(1)
392392
parts = shlex.split(line.replace('\\', '\\\\'))
393393
parts = [x for x in parts if x != '-c' and x != '-o' and input_file not in x and temp_target not in x and '-emit-llvm' not in x]

emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def compile_js(infile, settings, temp_files, DEBUG):
120120
logging.debug('emscript: llvm backend: ' + ' '.join(backend_args))
121121
t = time.time()
122122
with ToolchainProfiler.profile_block('emscript_llvm_backend'):
123-
shared.jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE), note_args=backend_args)
123+
shared.jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE, universal_newlines=True), note_args=backend_args)
124124
if DEBUG:
125125
logging.debug(' emscript: llvm backend took %s seconds' % (time.time() - t))
126126

tools/bisect_pair.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def run_code(name):
3737

3838
# Calculate diff chunks
3939
print('diffing')
40-
diff = Popen(['diff', '-U', '5', 'left', 'right'], stdout=PIPE).communicate()[0].split('\n')
40+
diff = run_process(['diff', '-U', '5', 'left', 'right'], stdout=PIPE).stdout.split('\n')
4141
pre_diff = diff[:2]
4242
diff = diff[2:]
4343

@@ -81,7 +81,7 @@ def run_code(name):
8181
found = mid
8282
break
8383

84-
critical = Popen(['diff', '-U', '5', 'middle' + str(mid-1), 'middle' + str(mid)], stdout=PIPE).communicate()[0]
84+
critical = run_process(['diff', '-U', '5', 'middle' + str(mid-1), 'middle' + str(mid)], stdout=PIPE).stdout
8585
c = open('critical.diff', 'w')
8686
c.write(critical)
8787
c.close()

tools/bisect_pair_wast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def run_code(name):
3838

3939
# Calculate diff chunks
4040
print('diffing')
41-
diff = Popen(['diff', '-U', '5', 'left', 'right'], stdout=PIPE).communicate()[0].split('\n')
41+
diff = run_process(['diff', '-U', '5', 'left', 'right'], stdout=PIPE).stdout.split('\n')
4242
pre_diff = diff[:2]
4343
diff = diff[2:]
4444

@@ -82,7 +82,7 @@ def run_code(name):
8282
found = mid
8383
break
8484

85-
critical = Popen(['diff', '-U', '5', 'middle' + str(mid-1), 'middle' + str(mid)], stdout=PIPE).communicate()[0]
85+
critical = run_process(['diff', '-U', '5', 'middle' + str(mid-1), 'middle' + str(mid)], stdout=PIPE).stdout
8686
c = open('critical.diff', 'w')
8787
c.write(critical)
8888
c.close()

tools/ctor_evaller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def read_and_delete(filename):
247247
err_file = config.get_temp_files().get('.err').name
248248
out_file_handle = open(out_file, 'w')
249249
err_file_handle = open(err_file, 'w')
250-
proc = subprocess.Popen(shared.NODE_JS + [temp_file], stdout=out_file_handle, stderr=err_file_handle)
250+
proc = subprocess.Popen(shared.NODE_JS + [temp_file], stdout=out_file_handle, stderr=err_file_handle, universal_newlines=True)
251251
try:
252252
shared.jsrun.timeout_run(proc, timeout=10, full_output=True, throw_on_failure=False)
253253
except Exception as e:
@@ -289,7 +289,7 @@ def eval_ctors_wasm(js, wasm_file, num):
289289
if debug_info:
290290
cmd += ['-g']
291291
shared.logging.debug('wasm ctor cmd: ' + str(cmd))
292-
out, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
292+
err = shared.run_process(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stderr
293293
num_successful = err.count('success on')
294294
shared.logging.debug(err)
295295
if len(ctors) == num_successful:

tools/duplicate_function_eliminator.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def process_shell(js_engine, shell, equivalentfn_hash_info=None):
2020
f.write(equivalentfn_hash_info)
2121
f.close()
2222

23-
(output,error) = subprocess.Popen(js_engine +
23+
proc = shared.run_process(js_engine +
2424
[DUPLICATE_FUNCTION_ELIMINATOR, temp_file, '--use-hash-info', '--no-minimize-whitespace'],
25-
stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
26-
assert len(output) > 0
27-
assert len(error) == 0
25+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
26+
assert len(proc.stdout) > 0
27+
assert len(proc.stderr) == 0
2828

29-
return output
29+
return proc.stdout
3030

3131
def run_on_chunk(command):
3232
try:
@@ -45,15 +45,13 @@ def run_on_chunk(command):
4545

4646
if shared.EM_BUILD_VERBOSE_LEVEL >= 3: print('run_on_chunk: ' + str(command), file=sys.stderr)
4747

48-
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
49-
output = proc.communicate()[0]
48+
proc = shared.run_process(command, stdout=subprocess.PIPE)
49+
output = proc.stdout
5050
assert proc.returncode == 0, 'Error in optimizer (return code ' + str(proc.returncode) + '): ' + output
5151
assert len(output) > 0 and not output.startswith('Assertion failed'), 'Error in optimizer: ' + output
5252
filename = temp_files.get(os.path.basename(filename) + '.jo' + file_suffix).name
5353

54-
# Important to write out in binary mode, because the data we are writing contains Windows line endings '\r\n' because it was PIPED from console.
55-
# Otherwise writing \r\n to ascii mode file will result in Windows amplifying \n to \r\n, generating bad \r\r\n line endings.
56-
f = open(filename, 'wb')
54+
f = open(filename, 'w')
5755
f.write(output)
5856
f.close()
5957
if DEBUG and not shared.WINDOWS: print('.', file=sys.stderr) # Skip debug progress indicator on Windows, since it doesn't buffer well with multiple threads printing to console.

tools/ffdb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,18 @@ def print_applist(applist, running_app_manifests, print_removable):
176176

177177
def adb_devices():
178178
try:
179-
devices = subprocess.check_output([ADB, 'devices'])
179+
devices = run_process([ADB, 'devices'], check=True, stdout=subprocess.PIPE).stdout
180180
devices = devices.strip().split('\n')[1:]
181181
devices = [x.strip().split('\t') for x in devices]
182182
return devices
183183
except Exception as e:
184184
return []
185185

186186
def b2g_get_prefs_filename():
187-
return subprocess.check_output([ADB, 'shell', 'echo', '-n', '/data/b2g/mozilla/*.default/prefs.js'])
187+
return run_process([ADB, 'shell', 'echo', '-n', '/data/b2g/mozilla/*.default/prefs.js'], check=True, stdout=subprocess.PIPE).stdout
188188

189189
def b2g_get_prefs_data():
190-
return subprocess.check_output([ADB, 'shell', 'cat', '/data/b2g/mozilla/*.default/prefs.js'])
190+
return run_process([ADB, 'shell', 'cat', '/data/b2g/mozilla/*.default/prefs.js'], check=True, stdout=subprocess.PIPE).stdout
191191

192192
def b2g_get_pref(sub):
193193
prefs_data = b2g_get_prefs_data().split('\n')

tools/file_packager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def was_seen(name):
365365

366366
# guess at format. this lets us tell crunch to not try to be clever and use odd formats like DXT5_AGBR
367367
try:
368-
format = Popen(['file', file_['srcpath']], stdout=PIPE).communicate()[0]
368+
format = run_process(['file', file_['srcpath']], stdout=PIPE).stdout
369369
if 'DXT5' in format:
370370
format = ['-dxt5']
371371
elif 'DXT1' in format:

tools/js_optimizer.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ def minify_shell(self, shell, minify_whitespace, source_map=False):
246246
f.write('// EXTRA_INFO:' + json.dumps(self.serialize()))
247247
f.close()
248248

249-
output = subprocess.Popen(self.js_engine +
249+
output = shared.run_process(self.js_engine +
250250
[JS_OPTIMIZER, temp_file, 'minifyGlobals', 'noPrintMetadata'] +
251251
(['minifyWhitespace'] if minify_whitespace else []) +
252252
(['--debug'] if source_map else []),
253-
stdout=subprocess.PIPE).communicate()[0]
253+
stdout=subprocess.PIPE).stdout
254254

255255
assert len(output) > 0 and not output.startswith('Assertion failed'), 'Error in js optimizer: ' + output
256256
#print >> sys.stderr, "minified SHELL 3333333333333333", output, "\n44444444444444444444"
@@ -290,14 +290,12 @@ def run_on_chunk(command):
290290
print('running js optimizer command', ' '.join([c if c != filename else saved for c in command]), file=sys.stderr)
291291
shutil.copyfile(filename, os.path.join(shared.get_emscripten_temp_dir(), saved))
292292
if shared.EM_BUILD_VERBOSE_LEVEL >= 3: print('run_on_chunk: ' + str(command), file=sys.stderr)
293-
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
294-
output = proc.communicate()[0]
293+
proc = shared.run_process(command, stdout=subprocess.PIPE)
294+
output = proc.stdout
295295
assert proc.returncode == 0, 'Error in optimizer (return code ' + str(proc.returncode) + '): ' + output
296296
assert len(output) > 0 and not output.startswith('Assertion failed'), 'Error in optimizer: ' + output
297297
filename = temp_files.get(os.path.basename(filename) + '.jo.js').name
298-
# Important to write out in binary mode, because the data we are writing contains Windows line endings '\r\n' because it was PIPED from console.
299-
# Otherwise writing \r\n to ascii mode file will result in Windows amplifying \n to \r\n, generating bad \r\r\n line endings.
300-
f = open(filename, 'wb')
298+
f = open(filename, 'w')
301299
f.write(output)
302300
f.close()
303301
if DEBUG and not shared.WINDOWS: print('.', file=sys.stderr) # Skip debug progress indicator on Windows, since it doesn't buffer well with multiple threads printing to console.

0 commit comments

Comments
 (0)