@@ -2856,7 +2856,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
28562856
28572857 # Initialize the redirection saved state
28582858 redir_saved_state = utils.RedirectionSavedState(
2859- cast (TextIO , self .stdout ), sys . stdout , self ._cur_pipe_proc_reader , self ._redirecting
2859+ cast(TextIO, self.stdout), self._cur_pipe_proc_reader, self._redirecting
28602860 )
28612861
28622862 # The ProcReader for this command
@@ -2912,7 +2912,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
29122912 raise RedirectionError(f'Pipe process exited with code {proc.returncode} before command could run')
29132913 redir_saved_state.redirecting = True # type: ignore[unreachable]
29142914 cmd_pipe_proc_reader = utils.ProcReader(proc, cast(TextIO, self.stdout), sys.stderr)
2915- sys . stdout = self .stdout = new_stdout
2915+ self.stdout = new_stdout
29162916
29172917 elif statement.output:
29182918 if statement.output_to:
@@ -2926,7 +2926,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
29262926 raise RedirectionError('Failed to redirect output') from ex
29272927
29282928 redir_saved_state.redirecting = True
2929- sys . stdout = self .stdout = new_stdout
2929+ self.stdout = new_stdout
29302930
29312931 else:
29322932 # Redirecting to a paste buffer
@@ -2944,7 +2944,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
29442944 # create a temporary file to store output
29452945 new_stdout = cast(TextIO, tempfile.TemporaryFile(mode="w+")) # noqa: SIM115
29462946 redir_saved_state.redirecting = True
2947- sys . stdout = self .stdout = new_stdout
2947+ self.stdout = new_stdout
29482948
29492949 if statement.output == constants.REDIRECTION_APPEND:
29502950 self.stdout.write(current_paste_buffer)
@@ -2972,9 +2972,8 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
29722972 # Close the file or pipe that stdout was redirected to
29732973 self.stdout.close()
29742974
2975- # Restore the stdout values
2975+ # Restore self. stdout
29762976 self.stdout = cast(TextIO, saved_redir_state.saved_self_stdout)
2977- sys .stdout = cast (TextIO , saved_redir_state .saved_sys_stdout )
29782977
29792978 # Check if we need to wait for the process being piped to
29802979 if self._cur_pipe_proc_reader is not None:
@@ -4449,22 +4448,13 @@ def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env:
44494448 # Set up sys module for the Python console
44504449 self._reset_py_display()
44514450
4452- cmd2_env .sys_stdout = sys .stdout
4453- sys .stdout = self .stdout # type: ignore[assignment]
4454-
4455- cmd2_env .sys_stdin = sys .stdin
4456- sys .stdin = self .stdin # type: ignore[assignment]
4457-
44584451 return cmd2_env
44594452
44604453 def _restore_cmd2_env(self, cmd2_env: _SavedCmd2Env) -> None:
44614454 """Restore cmd2 environment after exiting an interactive Python shell.
44624455
44634456 :param cmd2_env: the environment settings to restore
44644457 """
4465- sys .stdout = cmd2_env .sys_stdout # type: ignore[assignment]
4466- sys .stdin = cmd2_env .sys_stdin # type: ignore[assignment]
4467-
44684458 # Set up readline for cmd2
44694459 if rl_type != RlType.NONE:
44704460 # Save py's history
@@ -4539,6 +4529,11 @@ def py_quit() -> None:
45394529 if self.self_in_py:
45404530 local_vars['self'] = self
45414531
4532+ # Since poutput() may not be available in a pyscript, like in the case when self_in_py is False,
4533+ # provide a version of print() which writes to self.stdout. This way, print's output can be
4534+ # captured and redirected.
4535+ local_vars['print'] = functools.partial(print, file=self.stdout)
4536+
45424537 # Handle case where we were called by do_run_pyscript()
45434538 if pyscript is not None:
45444539 # Read the script file
0 commit comments