diff options
-rw-r--r-- | cmd2/cmd2.py | 22 | ||||
-rw-r--r-- | cmd2/utils.py | 13 |
2 files changed, 17 insertions, 18 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index c33e2927..d6eb121e 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2941,19 +2941,15 @@ class Cmd(cmd.Cmd): expanded_command = ' '.join(tokens) - # For any output that is a StdSim, we will use a pipe so we can save the output - is_out_sim = isinstance(self.stdout, utils.StdSim) - is_err_sim = isinstance(sys.stderr, utils.StdSim) - - proc_stdout = subprocess.PIPE if is_out_sim else self.stdout - proc_stderr = subprocess.PIPE if is_err_sim else sys.stderr - - proc = subprocess.Popen(expanded_command, stdout=proc_stdout, stderr=proc_stderr, shell=True) - if is_out_sim or is_err_sim: - proc_reader = utils.ProcReader(proc, self.stdout, sys.stderr) - proc_reader.wait() - else: - proc.communicate() + # For any stream that is a StdSim, we will use a pipe so we can capture its output + proc = subprocess.Popen(expanded_command, + stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout, + stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, + shell=True) + + # Use a ProcReader in all cases since the process will run normally even if no output is being captured + proc_reader = utils.ProcReader(proc, self.stdout, sys.stderr) + proc_reader.wait() @staticmethod def _reset_py_display() -> None: diff --git a/cmd2/utils.py b/cmd2/utils.py index 45c7ca6a..d14ef90f 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -371,14 +371,17 @@ class ByteBuf(object): class ProcReader(object): - """Used to read stdout and stderr from a Popen process if any of those were set to subprocess.PIPE""" + """ + Used to captured stdout and stderr from a Popen process if any of those were set to subprocess.PIPE. + If neither are pipes, then the process will run normally and no output will be captured. + """ def __init__(self, proc: subprocess.Popen, stdout: Union[StdSim, BinaryIO, TextIO], stderr: Union[StdSim, BinaryIO, TextIO]) -> None: """ ProcReader initializer :param proc: the Popen process being read from - :param stdout: the stdout stream being written to - :param stderr: the stderr stream being written to + :param stdout: the stream to write captured stdout + :param stderr: the stream to write captured stderr """ self._proc = proc self._stdout = stdout @@ -407,8 +410,8 @@ class ProcReader(object): if self._err_thread.is_alive(): self._err_thread.join() - # Handle case where the process ended before the last read could be done - # This will return None for the streams that weren't pipes so it is safe. + # Handle case where the process ended before the last read could be done. + # This will return None for the streams that weren't pipes. out, err = self._proc.communicate() if out: self._write_bytes(self._stdout, out) |