diff options
-rw-r--r-- | cmd2/cmd2.py | 14 | ||||
-rw-r--r-- | cmd2/utils.py | 21 |
2 files changed, 15 insertions, 20 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 4522a876..ae6e8de1 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1924,9 +1924,9 @@ class Cmd(cmd.Cmd): # Create a pipe with read and write sides read_fd, write_fd = os.pipe() - # Open each side of the pipe and set stdout accordingly - pipe_read = io.open(read_fd, 'r') - pipe_write = io.open(write_fd, 'w') + # Open each side of the pipe + subproc_stdin = io.open(read_fd, 'r') + new_stdout = io.open(write_fd, 'w') # We want Popen to raise an exception if it fails to open the process. Thus we don't set shell to True. try: @@ -1943,7 +1943,7 @@ class Cmd(cmd.Cmd): # For any stream that is a StdSim, we will use a pipe so we can capture its output proc = \ subprocess.Popen(statement.pipe_to, - stdin=pipe_read, + stdin=subproc_stdin, stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout, stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr, creationflags=creationflags, @@ -1951,11 +1951,11 @@ class Cmd(cmd.Cmd): saved_state.redirecting = True saved_state.pipe_proc_reader = utils.ProcReader(proc, self.stdout, sys.stderr) - sys.stdout = self.stdout = pipe_write + sys.stdout = self.stdout = new_stdout except Exception as ex: self.perror('Failed to open pipe because - {}'.format(ex), traceback_war=False) - pipe_read.close() - pipe_write.close() + subproc_stdin.close() + new_stdout.close() redir_error = True elif statement.output: diff --git a/cmd2/utils.py b/cmd2/utils.py index 5bb08edd..4172e362 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -414,19 +414,14 @@ class ProcReader(object): def wait(self) -> None: """Wait for the process to finish""" - while True: - try: - if self._out_thread.is_alive(): - self._out_thread.join() - 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. - out, err = self._proc.communicate() - break - except KeyboardInterrupt: - pass + if self._out_thread.is_alive(): + self._out_thread.join() + 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. + out, err = self._proc.communicate() if out: self._write_bytes(self._stdout, out) |