@@ -434,7 +434,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
434434 self ._script_dir = []
435435
436436 # A flag used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt
437- self .sigint_protection = utils .ContextFlag (False )
437+ self .sigint_protection = utils .ContextFlag ()
438438
439439 # When this is not None, then it holds a ProcReader for the pipe process created by the current command
440440 self .cur_pipe_proc_reader = None
@@ -711,9 +711,7 @@ def ppaged(self, msg: str, end: str = '\n', chop: bool = False) -> None:
711711 # still receive the SIGINT since it is in the same process group as us.
712712 with self .sigint_protection :
713713 pipe_proc = subprocess .Popen (pager , shell = True , stdin = subprocess .PIPE )
714- pipe_proc .stdin .write (msg_str .encode ('utf-8' , 'replace' ))
715- pipe_proc .stdin .close ()
716- pipe_proc .communicate ()
714+ pipe_proc .communicate (msg_str .encode ('utf-8' , 'replace' ))
717715 else :
718716 self .decolorized_write (self .stdout , msg_str )
719717 except BrokenPipeError :
@@ -1728,14 +1726,11 @@ def onecmd_plus_hooks(self, line: str) -> bool:
17281726 saved_state = None
17291727
17301728 try :
1729+ # Get sigint protection while we set up redirection
17311730 with self .sigint_protection :
1732- # Set up our redirection state variables
17331731 redir_error , saved_state = self ._redirect_output (statement )
17341732 self .cur_pipe_proc_reader = saved_state .pipe_proc_reader
17351733
1736- if self ._in_py :
1737- self ._last_result = None
1738-
17391734 # Do not continue if an error occurred while trying to redirect
17401735 if not redir_error :
17411736 # See if we need to update self.redirecting
@@ -1789,9 +1784,9 @@ def onecmd_plus_hooks(self, line: str) -> bool:
17891784 def _run_cmdfinalization_hooks (self , stop : bool , statement : Optional [Statement ]) -> bool :
17901785 """Run the command finalization hooks"""
17911786
1792- if not sys . platform . startswith ( 'win' ) :
1793- # Fix those annoying problems that occur with terminal programs like "less" when you pipe to them
1794- if self . stdin . isatty ():
1787+ with self . sigint_protection :
1788+ if not sys . platform . startswith ( 'win' ) and self . stdin . isatty ():
1789+ # Fix those annoying problems that occur with terminal programs like "less" when you pipe to them
17951790 import subprocess
17961791 proc = subprocess .Popen (['stty' , 'sane' ])
17971792 proc .communicate ()
@@ -2015,12 +2010,12 @@ def _restore_output(self, statement: Statement, saved_state: RedirectionSavedSta
20152010 self .stdout = saved_state .saved_self_stdout
20162011 sys .stdout = saved_state .saved_sys_stdout
20172012
2018- # Check if we need to wait for the process being piped to
2019- if self .cur_pipe_proc_reader is not None :
2020- self .cur_pipe_proc_reader .wait ()
2013+ # Check if we need to wait for the process being piped to
2014+ if self .cur_pipe_proc_reader is not None :
2015+ self .cur_pipe_proc_reader .wait ()
20212016
2022- # Restore cur_pipe_proc_reader
2023- self .cur_pipe_proc_reader = saved_state .saved_pipe_proc_reader
2017+ # Restore cur_pipe_proc_reader
2018+ self .cur_pipe_proc_reader = saved_state .saved_pipe_proc_reader
20242019
20252020 def cmd_func (self , command : str ) -> Optional [Callable ]:
20262021 """
0 commit comments