@@ -1630,29 +1630,26 @@ def ppaged(
16301630
16311631 # If the pager was killed (e.g. SIGKILL), the terminal might be in a bad state.
16321632 # Attempt to restore terminal settings and foreground process group.
1633- if self .stdin .isatty ():
1633+ if self ._initial_termios_settings is not None and self . stdin .isatty (): # type: ignore[unreachable]
16341634 try :
16351635 import signal
16361636 import termios
1637- except ImportError :
1637+
1638+ # Ensure we are in the foreground process group
1639+ if hasattr (os , 'tcsetpgrp' ) and hasattr (os , 'getpgrp' ):
1640+ # Ignore SIGTTOU to avoid getting stopped when calling tcsetpgrp from background
1641+ old_handler = signal .signal (signal .SIGTTOU , signal .SIG_IGN )
1642+ try :
1643+ os .tcsetpgrp (self .stdin .fileno (), os .getpgrp ())
1644+ finally :
1645+ signal .signal (signal .SIGTTOU , old_handler )
1646+
1647+ # Restore terminal attributes
1648+ if self ._initial_termios_settings is not None :
1649+ termios .tcsetattr (self .stdin .fileno (), termios .TCSANOW , self ._initial_termios_settings )
1650+
1651+ except (OSError , termios .error ):
16381652 pass
1639- else :
1640- try :
1641- # Ensure we are in the foreground process group
1642- if hasattr (os , 'tcsetpgrp' ) and hasattr (os , 'getpgrp' ):
1643- # Ignore SIGTTOU to avoid getting stopped when calling tcsetpgrp from background
1644- old_handler = signal .signal (signal .SIGTTOU , signal .SIG_IGN )
1645- try :
1646- os .tcsetpgrp (self .stdin .fileno (), os .getpgrp ())
1647- finally :
1648- signal .signal (signal .SIGTTOU , old_handler )
1649-
1650- # Restore terminal attributes
1651- if self ._initial_termios_settings is not None :
1652- termios .tcsetattr (self .stdin .fileno (), termios .TCSANOW , self ._initial_termios_settings )
1653-
1654- except (OSError , termios .error ):
1655- pass
16561653
16571654 else :
16581655 self .poutput (
@@ -4605,12 +4602,10 @@ def _set_up_py_shell_env(self, interp: InteractiveConsole) -> _SavedCmd2Env:
46054602 self ._reset_py_display ()
46064603
46074604 # Enable tab completion if readline is available
4608- try :
4605+ if not sys . platform . startswith ( 'win' ) :
46094606 import readline
46104607 import rlcompleter
4611- except ImportError :
4612- pass
4613- else :
4608+
46144609 # Save the current completer
46154610 cmd2_env .completer = readline .get_completer ()
46164611
@@ -4631,11 +4626,9 @@ def _restore_cmd2_env(self, cmd2_env: _SavedCmd2Env) -> None:
46314626 :param cmd2_env: the environment settings to restore
46324627 """
46334628 # Restore the readline completer
4634- try :
4629+ if not sys . platform . startswith ( 'win' ) :
46354630 import readline
4636- except ImportError :
4637- pass
4638- else :
4631+
46394632 readline .set_completer (cmd2_env .completer )
46404633
46414634 def _run_python (self , * , pyscript : str | None = None ) -> bool | None :
0 commit comments