From e7367b692bad892f27c0dd7ad04e6aa3c9f56230 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 6 Jun 2018 23:35:28 -0400 Subject: Fixed a couple potential crashes on opening files Fixed crashes that occur when attempting to open a file in a non-existent directory or a when the filename is too long. Specifically fixed this when redirecting output to a file and when saving a transcript based on the history. Also added a couple unit tests related to the fixes. --- cmd2/cmd2.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'cmd2/cmd2.py') diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2d8766f4..3f000318 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1696,7 +1696,7 @@ class Cmd(cmd.Cmd): if self.timing: self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart)) finally: - if self.allow_redirection: + if self.allow_redirection and self.redirecting: self._restore_output(statement) except EmptyStatement: pass @@ -1840,7 +1840,11 @@ class Cmd(cmd.Cmd): # REDIRECTION_APPEND or REDIRECTION_OUTPUT if statement.output == constants.REDIRECTION_APPEND: mode = 'a' - sys.stdout = self.stdout = open(os.path.expanduser(statement.output_to), mode) + try: + sys.stdout = self.stdout = open(os.path.expanduser(statement.output_to), mode) + except (FileNotFoundError, OSError) as ex: + self.perror('Not Redirecting because - {}'.format(ex), traceback_war=False) + self.redirecting = False else: # going to a paste buffer sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+") @@ -2878,16 +2882,19 @@ a..b, a:b, a:, ..b items by indices (inclusive) self.echo = saved_echo # finally, we can write the transcript out to the file - with open(transcript_file, 'w') as fout: - fout.write(transcript) - - # and let the user know what we did - if len(history) > 1: - plural = 'commands and their outputs' + try: + with open(transcript_file, 'w') as fout: + fout.write(transcript) + except (FileNotFoundError, OSError) as ex: + self.perror('Failed to save transcript: {}'.format(ex), traceback_war=False) else: - plural = 'command and its output' - msg = '{} {} saved to transcript file {!r}' - self.pfeedback(msg.format(len(history), plural, transcript_file)) + # and let the user know what we did + if len(history) > 1: + plural = 'commands and their outputs' + else: + plural = 'command and its output' + msg = '{} {} saved to transcript file {!r}' + self.pfeedback(msg.format(len(history), plural, transcript_file)) @with_argument_list def do_edit(self, arglist): -- cgit v1.2.1 From c09e99341d5ba8aa0775cd312b359da084013a29 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 7 Jun 2018 10:42:40 -0400 Subject: OSError is enough to catch the errors we are concerned with --- cmd2/cmd2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cmd2/cmd2.py') diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 3f000318..289c44ae 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1842,7 +1842,7 @@ class Cmd(cmd.Cmd): mode = 'a' try: sys.stdout = self.stdout = open(os.path.expanduser(statement.output_to), mode) - except (FileNotFoundError, OSError) as ex: + except OSError as ex: self.perror('Not Redirecting because - {}'.format(ex), traceback_war=False) self.redirecting = False else: @@ -2885,7 +2885,7 @@ a..b, a:b, a:, ..b items by indices (inclusive) try: with open(transcript_file, 'w') as fout: fout.write(transcript) - except (FileNotFoundError, OSError) as ex: + except OSError as ex: self.perror('Failed to save transcript: {}'.format(ex), traceback_war=False) else: # and let the user know what we did -- cgit v1.2.1