@@ -2856,7 +2856,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2856
2856
2857
2857
# Initialize the redirection saved state
2858
2858
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
2860
2860
)
2861
2861
2862
2862
# The ProcReader for this command
@@ -2912,7 +2912,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2912
2912
raise RedirectionError (f'Pipe process exited with code { proc .returncode } before command could run' )
2913
2913
redir_saved_state .redirecting = True # type: ignore[unreachable]
2914
2914
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
2916
2916
2917
2917
elif statement .output :
2918
2918
if statement .output_to :
@@ -2926,7 +2926,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2926
2926
raise RedirectionError ('Failed to redirect output' ) from ex
2927
2927
2928
2928
redir_saved_state .redirecting = True
2929
- sys . stdout = self .stdout = new_stdout
2929
+ self .stdout = new_stdout
2930
2930
2931
2931
else :
2932
2932
# Redirecting to a paste buffer
@@ -2944,7 +2944,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2944
2944
# create a temporary file to store output
2945
2945
new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" )) # noqa: SIM115
2946
2946
redir_saved_state .redirecting = True
2947
- sys . stdout = self .stdout = new_stdout
2947
+ self .stdout = new_stdout
2948
2948
2949
2949
if statement .output == constants .REDIRECTION_APPEND :
2950
2950
self .stdout .write (current_paste_buffer )
@@ -2972,9 +2972,8 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
2972
2972
# Close the file or pipe that stdout was redirected to
2973
2973
self .stdout .close ()
2974
2974
2975
- # Restore the stdout values
2975
+ # Restore self. stdout
2976
2976
self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
2977
- sys .stdout = cast (TextIO , saved_redir_state .saved_sys_stdout )
2978
2977
2979
2978
# Check if we need to wait for the process being piped to
2980
2979
if self ._cur_pipe_proc_reader is not None :
@@ -4449,22 +4448,13 @@ def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env:
4449
4448
# Set up sys module for the Python console
4450
4449
self ._reset_py_display ()
4451
4450
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
-
4458
4451
return cmd2_env
4459
4452
4460
4453
def _restore_cmd2_env (self , cmd2_env : _SavedCmd2Env ) -> None :
4461
4454
"""Restore cmd2 environment after exiting an interactive Python shell.
4462
4455
4463
4456
:param cmd2_env: the environment settings to restore
4464
4457
"""
4465
- sys .stdout = cmd2_env .sys_stdout # type: ignore[assignment]
4466
- sys .stdin = cmd2_env .sys_stdin # type: ignore[assignment]
4467
-
4468
4458
# Set up readline for cmd2
4469
4459
if rl_type != RlType .NONE :
4470
4460
# Save py's history
@@ -4539,6 +4529,11 @@ def py_quit() -> None:
4539
4529
if self .self_in_py :
4540
4530
local_vars ['self' ] = self
4541
4531
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
+
4542
4537
# Handle case where we were called by do_run_pyscript()
4543
4538
if pyscript is not None :
4544
4539
# Read the script file
0 commit comments