diff options
-rw-r--r-- | cmd2/cmd2.py | 11 | ||||
-rw-r--r-- | cmd2/pyscript_bridge.py | 4 | ||||
-rw-r--r-- | tests/test_cmd2.py | 5 |
3 files changed, 16 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index ddc3945b..aeaa1543 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1718,6 +1718,11 @@ class Cmd(cmd.Cmd): try: # Get sigint protection while we set up redirection with self.sigint_protection: + if self._in_py: + # Start saving the command's output at this point to match what redirection captures + 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 @@ -1763,6 +1768,11 @@ 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 + self.stdout.pause_storage = True + sys.stderr.pause_storage = True + except EmptyStatement: # don't do anything, but do allow command finalization hooks to run pass @@ -3026,7 +3036,6 @@ class Cmd(cmd.Cmd): if self._in_py: err = "Recursively entering interactive Python consoles is not allowed." self.perror(err, traceback_war=False) - self._last_result = CommandResult('', err) return False try: diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index a4eaf308..8f0aaf52 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -92,6 +92,10 @@ class PyscriptBridge(object): # 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: diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index d33477f2..300e3ed9 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -268,9 +268,8 @@ def test_recursive_pyscript_not_allowed(base_app, request): python_script = os.path.join(test_dir, 'scripts', 'recursive.py') expected = 'Recursively entering interactive Python consoles is not allowed.' - run_cmd(base_app, "pyscript {}".format(python_script)) - err = base_app._last_result.stderr - assert err == expected + out, err = run_cmd(base_app, "pyscript {}".format(python_script)) + assert err[0] == expected def test_pyscript_with_nonexist_file(base_app): python_script = 'does_not_exist.py' |