diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-03-20 19:14:20 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-03-20 19:14:20 -0400 |
commit | d16a20fe14ed02e389ccde99d114e7335740e162 (patch) | |
tree | ce6aefd5c89fe33f6ee3af40c6f8a7dc3dd96f55 /cmd2/cmd2.py | |
parent | 7e850f828a899d5fcc5deb468857eaf9f3f2bbc5 (diff) | |
download | cmd2-git-d16a20fe14ed02e389ccde99d114e7335740e162.tar.gz |
Handled possible race condition
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 f470a76c..1226bc59 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1722,16 +1722,20 @@ class Cmd(cmd.Cmd): # Keep track of whether or not we were already redirecting before this command already_redirecting = self.redirecting - # Handle any redirection for this command - redir_error, saved_state = self._redirect_output(statement) + # When this isn't None, we know that _redirect_output completed. This prevents getting into + # a weird state if _redirect_output returns early because of a Ctrl-C event. + saved_state = None - # Do not continue if an error occurred while trying to redirect - if not redir_error: - # See if we need to update self.redirecting - if not already_redirecting: - self.redirecting = saved_state.redirecting + try: + # Handle any redirection for this command + redir_error, saved_state = self._redirect_output(statement) + + # Do not continue if an error occurred while trying to redirect + if not redir_error: + # See if we need to update self.redirecting + if not already_redirecting: + self.redirecting = saved_state.redirecting - try: timestart = datetime.datetime.now() if self._in_py: self._last_result = None @@ -1758,10 +1762,13 @@ class Cmd(cmd.Cmd): if self.timing: self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart)) - finally: + finally: + # Make sure _redirect_output completed + if saved_state is not None: self._restore_output(statement, saved_state) - if not already_redirecting: - self.redirecting = False + + if not already_redirecting: + self.redirecting = False except EmptyStatement: # don't do anything, but do allow command finalization hooks to run |