diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-06-07 16:51:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-07 16:51:16 -0700 |
commit | d0e71c85190b81bb269cc18bf9380a142e18d707 (patch) | |
tree | 7b1a8d83d35417cf886cd6586b78fd727b4b8859 /cmd2/cmd2.py | |
parent | 794414745f7c28c4c96300117ce9a57c38d4d5b5 (diff) | |
parent | c09e99341d5ba8aa0775cd312b359da084013a29 (diff) | |
download | cmd2-git-d0e71c85190b81bb269cc18bf9380a142e18d707.tar.gz |
Merge pull request #432 from python-cmd2/file_crashes
Fixed a couple potential crashes on opening files
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2d8766f4..289c44ae 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 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 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): |