Enhance running system commands
Fixes and
@@ -22,6 +22,7 @@ import types
import warnings
from collections import namedtuple
from importlib.machinery import PathFinder, SourceFileLoader
+from threading import Thread
import __main__ # @UnresolvedImport
import _ast
@@ -456,17 +457,33 @@ class VM:
)
if sys.version_info >= (3,6):
- popen_kw["errors"] = "backslashreplace"
+ popen_kw["errors"] = "replace"
popen_kw["encoding"] = encoding
assert cmd.cmd_line.startswith("!")
cmd_line = cmd.cmd_line[1:]
proc = subprocess.Popen(cmd_line, **popen_kw)
- # TODO: how to publish stdout as it arrives?
- out, err = proc.communicate(input="")
- print(out, end="")
- print(err, file=sys.stderr, end="")
+ def copy_stream(source, target):
+ while True:
+ c = source.readline()
+ if c == "":
+ break
+ else:
+ target.write(c)
+
+ copy_out = Thread(target=lambda: copy_stream(proc.stdout, sys.stdout), daemon=True)
+ copy_err = Thread(target=lambda: copy_stream(proc.stderr, sys.stderr), daemon=True)
+
+ copy_out.start()
+ copy_err.start()
+ try:
+ proc.wait()
+ except KeyboardInterrupt as e:
+ print(str(e), file=sys.stderr)
+
+ copy_out.join()
+ copy_err.join()
def _cmd_process_gui_events(self, cmd):
# advance the event loop
