13
13
Easy transcript-based testing of applications (see examples/example.py)
14
14
Bash-style ``select`` available
15
15
16
- Note that redirection with > and | will only work if `self.poutput()`
17
- is used in place of `print`.
16
+ Note, if self.stdout is different than sys.stdout, then redirection with > and |
17
+ will only work if `self.poutput()` is used in place of `print`.
18
18
19
19
- Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com
20
20
@@ -200,8 +200,6 @@ def __init__(self) -> None:
200
200
self .readline_settings = _SavedReadlineSettings ()
201
201
self .readline_module : Optional [ModuleType ] = None
202
202
self .history : list [str ] = []
203
- self .sys_stdout : Optional [TextIO ] = None
204
- self .sys_stdin : Optional [TextIO ] = None
205
203
206
204
207
205
# Contains data about a disabled command which is used to restore its original functions when the command is enabled
@@ -2854,9 +2852,12 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2854
2852
"""
2855
2853
import subprocess
2856
2854
2855
+ # Only redirect sys.stdout if it's the same as self.stdout
2856
+ stdouts_match = self .stdout == sys .stdout
2857
+
2857
2858
# Initialize the redirection saved state
2858
2859
redir_saved_state = utils .RedirectionSavedState (
2859
- cast (TextIO , self .stdout ), sys . stdout , self ._cur_pipe_proc_reader , self ._redirecting
2860
+ cast (TextIO , self .stdout ), stdouts_match , self ._cur_pipe_proc_reader , self ._redirecting
2860
2861
)
2861
2862
2862
2863
# The ProcReader for this command
@@ -2912,7 +2913,10 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2912
2913
raise RedirectionError (f'Pipe process exited with code { proc .returncode } before command could run' )
2913
2914
redir_saved_state .redirecting = True # type: ignore[unreachable]
2914
2915
cmd_pipe_proc_reader = utils .ProcReader (proc , cast (TextIO , self .stdout ), sys .stderr )
2915
- sys .stdout = self .stdout = new_stdout
2916
+
2917
+ self .stdout = new_stdout
2918
+ if stdouts_match :
2919
+ sys .stdout = self .stdout
2916
2920
2917
2921
elif statement .output :
2918
2922
if statement .output_to :
@@ -2926,7 +2930,10 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2926
2930
raise RedirectionError ('Failed to redirect output' ) from ex
2927
2931
2928
2932
redir_saved_state .redirecting = True
2929
- sys .stdout = self .stdout = new_stdout
2933
+
2934
+ self .stdout = new_stdout
2935
+ if stdouts_match :
2936
+ sys .stdout = self .stdout
2930
2937
2931
2938
else :
2932
2939
# Redirecting to a paste buffer
@@ -2944,7 +2951,10 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
2944
2951
# create a temporary file to store output
2945
2952
new_stdout = cast (TextIO , tempfile .TemporaryFile (mode = "w+" )) # noqa: SIM115
2946
2953
redir_saved_state .redirecting = True
2947
- sys .stdout = self .stdout = new_stdout
2954
+
2955
+ self .stdout = new_stdout
2956
+ if stdouts_match :
2957
+ sys .stdout = self .stdout
2948
2958
2949
2959
if statement .output == constants .REDIRECTION_APPEND :
2950
2960
self .stdout .write (current_paste_buffer )
@@ -2974,7 +2984,8 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
2974
2984
2975
2985
# Restore the stdout values
2976
2986
self .stdout = cast (TextIO , saved_redir_state .saved_self_stdout )
2977
- sys .stdout = cast (TextIO , saved_redir_state .saved_sys_stdout )
2987
+ if saved_redir_state .stdouts_match :
2988
+ sys .stdout = self .stdout
2978
2989
2979
2990
# Check if we need to wait for the process being piped to
2980
2991
if self ._cur_pipe_proc_reader is not None :
@@ -4449,22 +4460,13 @@ def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env:
4449
4460
# Set up sys module for the Python console
4450
4461
self ._reset_py_display ()
4451
4462
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
4463
return cmd2_env
4459
4464
4460
4465
def _restore_cmd2_env (self , cmd2_env : _SavedCmd2Env ) -> None :
4461
4466
"""Restore cmd2 environment after exiting an interactive Python shell.
4462
4467
4463
4468
:param cmd2_env: the environment settings to restore
4464
4469
"""
4465
- sys .stdout = cmd2_env .sys_stdout # type: ignore[assignment]
4466
- sys .stdin = cmd2_env .sys_stdin # type: ignore[assignment]
4467
-
4468
4470
# Set up readline for cmd2
4469
4471
if rl_type != RlType .NONE :
4470
4472
# Save py's history
0 commit comments