Skip to content

Commit 963b587

Browse files
authored
Replace subprocess.Popen usage. NFC (#8125)
These days we can just use `subprocess.run`. Also `universal_newlines` can now just be written as `text=True`.
1 parent 04a376a commit 963b587

File tree

7 files changed

+30
-33
lines changed

7 files changed

+30
-33
lines changed

check.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ def run_version_tests():
6060
changelog_version = get_changelog_version()
6161
for e in executables:
6262
print('.. %s --version' % e)
63-
out, err = subprocess.Popen([e, '--version'],
64-
stdout=subprocess.PIPE,
65-
stderr=subprocess.PIPE).communicate()
66-
out = out.decode('utf-8')
67-
err = err.decode('utf-8')
68-
assert len(err) == 0, 'Expected no stderr, got:\n%s' % err
63+
proc = subprocess.run([e, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
64+
assert len(proc.stderr) == 0, 'Expected no stderr, got:\n%s' % proc.stderr
65+
out = proc.stdout
6966
assert os.path.basename(e).replace('.exe', '') in out, 'Expected version to contain program name, got:\n%s' % out
7067
assert len(out.strip().splitlines()) == 1, 'Expected only version info, got:\n%s' % out
7168
parts = out.split()

scripts/auto_update_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def update_example_tests():
6060
print('link: ', ' '.join(cmd))
6161
subprocess.check_call(cmd)
6262
print('run...', output_file)
63-
proc = subprocess.Popen([output_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64-
actual, err = proc.communicate()
65-
assert proc.returncode == 0, [proc.returncode, actual, err]
63+
proc = subprocess.run([output_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64+
assert proc.returncode == 0, [proc.returncode, proc.stderror, proc.stdout]
65+
actual = proc.stdout
6666
with open(expected, 'wb') as o:
6767
o.write(actual)
6868
finally:

scripts/fuzz_opt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from test import support
4747

4848

49-
assert sys.version_info.major == 3, 'requires Python 3!'
49+
assert sys.version_info >= (3, 10), 'requires Python 3.10'
5050

5151
# parameters
5252

@@ -102,7 +102,7 @@ def run(cmd, stderr=None, silent=False):
102102

103103
def run_unchecked(cmd):
104104
print(' '.join(cmd))
105-
return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).communicate()[0]
105+
return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout
106106

107107

108108
def randomize_pass_debug():

scripts/fuzz_relooper.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ def get_phi(j):
370370
print('^')
371371
subprocess.check_call(['./fuzz'], stdout=open('fuzz.wast', 'w'))
372372
print('*')
373-
fast_out = subprocess.Popen(['bin/wasm-shell', 'fuzz.wast'],
374-
stdout=subprocess.PIPE,
375-
stderr=subprocess.PIPE).communicate()[0]
373+
fast_out = subprocess.run(['bin/wasm-shell', 'fuzz.wast'],
374+
stdout=subprocess.PIPE,
375+
stderr=subprocess.PIPE).stdout
376376
print('-')
377377
node = os.getenv('NODE', 'nodejs')
378-
slow_out = subprocess.Popen([node, 'fuzz.slow.js'],
379-
stdout=subprocess.PIPE,
380-
stderr=subprocess.PIPE).communicate()[0]
378+
slow_out = subprocess.run([node, 'fuzz.slow.js'],
379+
stdout=subprocess.PIPE,
380+
stderr=subprocess.PIPE).stdout
381381
print('_')
382382

383383
if slow_out != fast_out:

scripts/gen-s-parser.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
import sys
1818

19+
if sys.version_info < (3, 10):
20+
print("python 3.10 required")
21+
sys.exit(1)
22+
23+
1924
instructions = [
2025
("unreachable", "makeUnreachable()"),
2126
("nop", "makeNop()"),
@@ -831,10 +836,6 @@ def print_footer():
831836

832837

833838
def main():
834-
if sys.version_info.major != 3:
835-
import datetime
836-
print("It's " + str(datetime.datetime.now().year) + "! Use Python 3!")
837-
sys.exit(1)
838839
print_header()
839840
instruction_parser()
840841
print_footer()

scripts/test/support.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ def write_wast(filename, wast, asserts=[]):
186186
o.write(wast + '\n'.join(asserts))
187187

188188

189-
# Hack to allow subprocess.Popen with stdout/stderr to StringIO, which doesn't have a fileno and doesn't work otherwise
190-
def _process_communicate(*args, **kwargs):
189+
# Hack to allow subprocess with stdout/stderr to StringIO, which doesn't have a fileno and doesn't work otherwise
190+
def _subprocess_run(*args, **kwargs):
191191
overwrite_stderr = "stderr" in kwargs and isinstance(kwargs["stderr"], io.StringIO)
192192
overwrite_stdout = "stdout" in kwargs and isinstance(kwargs["stdout"], io.StringIO)
193193

@@ -198,15 +198,14 @@ def _process_communicate(*args, **kwargs):
198198
stderr_fd = kwargs["stderr"]
199199
kwargs["stderr"] = subprocess.PIPE
200200

201-
proc = subprocess.Popen(*args, **kwargs)
202-
out, err = proc.communicate()
201+
proc = subprocess.run(*args, **kwargs)
203202

204203
if overwrite_stdout:
205-
stdout_fd.write(out)
204+
stdout_fd.write(proc.stdout)
206205
if overwrite_stderr:
207-
stderr_fd.write(err)
206+
stderr_fd.write(proc.stderr)
208207

209-
return out, err, proc.returncode
208+
return proc.stdout, proc.stderr, proc.returncode
210209

211210

212211
def run_command(cmd, expected_status=0, stdout=None, stderr=None,
@@ -222,7 +221,7 @@ def run_command(cmd, expected_status=0, stdout=None, stderr=None,
222221
stderr = subprocess.PIPE
223222
print('executing: ', ' '.join(cmd), file=stdout)
224223

225-
out, err, code = _process_communicate(cmd, stdout=subprocess.PIPE, stderr=stderr, universal_newlines=True, encoding='UTF-8')
224+
out, err, code = _subprocess_run(cmd, stdout=subprocess.PIPE, stderr=stderr, encoding='UTF-8')
226225

227226
if expected_status is not None and code != expected_status:
228227
raise Exception(f"run_command `{' '.join(cmd)}` failed ({code}) {err or ''}")

scripts/test/wasm_opt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ def check():
9696
wasm = os.path.basename(t).replace('.wast', '')
9797
cmd = shared.WASM_OPT + [t, '--print', '-all']
9898
print(' ', ' '.join(cmd))
99-
actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
99+
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
100100
expected_file = os.path.join(shared.get_test_dir('print'), wasm + '.txt')
101-
shared.fail_if_not_identical_to_file(actual, expected_file)
101+
shared.fail_if_not_identical_to_file(proc.stdout, expected_file)
102102
cmd = shared.WASM_OPT + [os.path.join(shared.get_test_dir('print'), t), '--print-minified', '-all']
103103
print(' ', ' '.join(cmd))
104-
actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
105-
shared.fail_if_not_identical(actual.strip(), open(os.path.join(shared.get_test_dir('print'), wasm + '.minified.txt')).read().strip())
104+
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
105+
shared.fail_if_not_identical(proc.stdout.strip(), open(os.path.join(shared.get_test_dir('print'), wasm + '.minified.txt')).read().strip())
106106

107107

108108
def update_wasm_opt_tests():

0 commit comments

Comments
 (0)