From 023acec19eb516dcb3b57ae2b5197e1f80af97d7 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 20 Mar 2019 14:24:47 -0400 Subject: Handled issue where nested pipe processes were not being closed in the right order upon SIGINT events --- cmd2/utils.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index 2db6a267..29ae332a 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -345,7 +345,8 @@ class StdSim(object): """Clear the internal contents""" self.buffer.byte_buf = b'' - def isatty(self) -> bool: + @staticmethod + def isatty() -> bool: """StdSim will never be considered an interactive stream""" return False @@ -403,9 +404,10 @@ class ProcReader(object): if self._proc.stderr is not None: self._err_thread.start() - def terminate(self) -> None: - """Terminates the process being run""" - self._proc.terminate() + def send_sigint(self) -> None: + """Send a SIGINT to the process""" + import signal + self._proc.send_signal(signal.SIGINT) def wait(self) -> None: """Wait for the process to finish""" @@ -452,7 +454,11 @@ class ProcReader(object): :param stream: the stream being written to :param to_write: the bytes being written """ - if 'b' in stream.mode: - stream.write(to_write) - else: - stream.buffer.write(to_write) + try: + if 'b' in stream.mode: + stream.write(to_write) + else: + stream.buffer.write(to_write) + except BrokenPipeError: + # This occurs if output is being piped to a process that closed + pass -- cgit v1.2.1