Skip to content

Commit 4a14388

Browse files
authored
[test] Handling buffering of subprocess output. NFC (#24766)
The python unittest framework has a stdout/stderr buffering mechanism that we don't take advantage of but that I would like to move towards using more. This change allows our tests to run in buffered mode without generating output on the underlying stdout and stderr file descriptors.
1 parent 006e6bf commit 4a14388

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

test/common.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import contextlib
1414
import difflib
1515
import hashlib
16+
import io
1617
import itertools
1718
import logging
1819
import multiprocessing
@@ -1763,19 +1764,36 @@ def clear(self):
17631764
if shared.EMSCRIPTEN_TEMP_DIR:
17641765
utils.delete_contents(shared.EMSCRIPTEN_TEMP_DIR)
17651766

1766-
def run_process(self, cmd, check=True, **args):
1767+
def run_process(self, cmd, check=True, **kwargs):
17671768
# Wrapper around shared.run_process. This is desirable so that the tests
17681769
# can fail (in the unittest sense) rather than error'ing.
17691770
# In the long run it would nice to completely remove the dependency on
17701771
# core emscripten code (shared.py) here.
1772+
1773+
# Handle buffering for subprocesses. The python unittest buffering mechanism
1774+
# will only buffer output from the current process (by overwriding sys.stdout
1775+
# and sys.stderr), not from sub-processes.
1776+
stdout_buffering = 'stdout' not in kwargs and isinstance(sys.stdout, io.StringIO)
1777+
stderr_buffering = 'stderr' not in kwargs and isinstance(sys.stderr, io.StringIO)
1778+
if stdout_buffering:
1779+
kwargs['stdout'] = PIPE
1780+
if stderr_buffering:
1781+
kwargs['stderr'] = PIPE
1782+
17711783
try:
1772-
return shared.run_process(cmd, check=check, **args)
1784+
rtn = shared.run_process(cmd, check=check, **kwargs)
17731785
except subprocess.CalledProcessError as e:
17741786
if check and e.returncode != 0:
17751787
print(e.stdout)
17761788
print(e.stderr)
17771789
self.fail(f'subprocess exited with non-zero return code({e.returncode}): `{shlex.join(cmd)}`')
17781790

1791+
if stdout_buffering:
1792+
sys.stdout.write(rtn.stdout)
1793+
if stderr_buffering:
1794+
sys.stderr.write(rtn.stderr)
1795+
return rtn
1796+
17791797
def emcc(self, filename, args=[], output_filename=None, **kwargs): # noqa
17801798
compile_only = '-c' in args or '-sSIDE_MODULE' in args
17811799
cmd = [compiler_for(filename), filename] + self.get_cflags(compile_only=compile_only) + args

0 commit comments

Comments
 (0)