summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-20 11:16:15 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-20 11:16:15 -0400
commit04b66bb955a456c951b024fe7b1545442fa32c5c (patch)
tree83a78ae517576708589cba28639a45e1cdddbc00
parente42065ea805c813790ae91dd00e3677ed811c8cd (diff)
downloadcmd2-git-04b66bb955a456c951b024fe7b1545442fa32c5c.tar.gz
Simiplified code
-rw-r--r--cmd2/cmd2.py22
-rw-r--r--cmd2/utils.py13
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)