diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-04-23 22:35:56 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-04-23 22:35:56 -0400 |
commit | ca7f24b238cdfd67adddec0146e7519352589484 (patch) | |
tree | e0208beaab3b491f4bc5e073c9085fe365ef63cd | |
parent | 47b531d0ecc41a0ae4182ed72445a1fd811e7d62 (diff) | |
download | cmd2-git-ca7f24b238cdfd67adddec0146e7519352589484.tar.gz |
Only enabling/disabling stdout storage in the call PyscriptBridge makes to onecmd_plus_hooks.
All nested calls to onecmd_plus_hooks keep capturing stdout to match the behavior of stdout
redirection being enabled during nested commands.
-rw-r--r-- | cmd2/cmd2.py | 21 | ||||
-rw-r--r-- | cmd2/pyscript_bridge.py | 9 |
2 files changed, 12 insertions, 18 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 3cac43f7..a7b60b1a 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1675,10 +1675,13 @@ class Cmd(cmd.Cmd): statement = self.statement_parser.parse_command_only(line) return statement.command, statement.args, statement.command_and_args - def onecmd_plus_hooks(self, line: str) -> bool: + def onecmd_plus_hooks(self, line: str, pyscript_bridge_call: bool = False) -> bool: """Top-level function called by cmdloop() to handle parsing a line and running the command and all of its hooks. :param line: line of text read from input + :param pyscript_bridge_call: This should only ever be set to True by PyscriptBridge to signify the beginning + of an app() call in a pyscript. It is used to enable/disable the storage of the + command's stdout. :return: True if cmdloop() should exit, False otherwise """ import datetime @@ -1718,10 +1721,9 @@ class Cmd(cmd.Cmd): try: # Get sigint protection while we set up redirection with self.sigint_protection: - if self._in_py: - # Start saving output at this point to match the same period output is redirected + if pyscript_bridge_call: + # Start saving command's stdout at this point self.stdout.pause_storage = False - sys.stderr.pause_storage = False redir_error, saved_state = self._redirect_output(statement) self.cur_pipe_proc_reader = saved_state.pipe_proc_reader @@ -1768,10 +1770,9 @@ class Cmd(cmd.Cmd): if not already_redirecting: self.redirecting = False - if self._in_py: - # Stop saving command's output before command finalization hooks run + if pyscript_bridge_call: + # Stop saving command's stdout before command finalization hooks run self.stdout.pause_storage = True - sys.stderr.pause_storage = True except EmptyStatement: # don't do anything, but do allow command finalization hooks to run @@ -1801,12 +1802,6 @@ class Cmd(cmd.Cmd): return data.stop except Exception as ex: self.perror(ex) - finally: - with self.sigint_protection: - if self._in_py: - # Restore ability to save output now that the command finalization hooks are done - self.stdout.pause_storage = False - sys.stderr.pause_storage = False def runcmds_plus_hooks(self, cmds: List[str]) -> bool: """Convenience method to run multiple commands by onecmd_plus_hooks. diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index 47ce29db..1c720cf9 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -87,20 +87,19 @@ class PyscriptBridge(object): # This will be used to capture _cmd2_app.stdout and sys.stdout copy_cmd_stdout = StdSim(self._cmd2_app.stdout, echo) + # Pause the storing of stdout until onecmd_plus_hooks enables it + copy_cmd_stdout.pause_storage = True + # This will be used to capture sys.stderr copy_stderr = StdSim(sys.stderr, echo) - # Pause the storing of any output until onecmd_plus_hooks enables it - copy_cmd_stdout.pause_storage = True - copy_stderr.pause_storage = True - self._cmd2_app._last_result = None try: self._cmd2_app.stdout = copy_cmd_stdout with redirect_stdout(copy_cmd_stdout): with redirect_stderr(copy_stderr): - self._cmd2_app.onecmd_plus_hooks(command) + self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True) finally: self._cmd2_app.stdout = copy_cmd_stdout.inner_stream |