@@ -513,12 +513,13 @@ def __init__(
513513 self ._initial_termios_settings = None
514514 if not sys .platform .startswith ('win' ) and self .stdin .isatty ():
515515 try :
516+ import io
516517 import termios
517518
518519 self ._initial_termios_settings = termios .tcgetattr (self .stdin .fileno ())
519- except (ImportError , termios .error ):
520+ except (ImportError , io . UnsupportedOperation , termios .error ):
520521 # This can happen if termios isn't available or stdin is a pseudo-TTY
521- pass
522+ self . _initial_termios_settings = None
522523
523524 # If a startup script is provided and exists, then execute it in the startup commands
524525 if startup_script :
@@ -2834,14 +2835,15 @@ def onecmd_plus_hooks(
28342835
28352836 def _run_cmdfinalization_hooks (self , stop : bool , statement : Statement | None ) -> bool :
28362837 """Run the command finalization hooks."""
2837- with self .sigint_protection :
2838- if not sys .platform .startswith ('win' ) and self .stdin .isatty ():
2839- # Before the next command runs, fix any terminal problems like those
2840- # caused by certain binary characters having been printed to it.
2841- import subprocess
2842-
2843- proc = subprocess .Popen (['stty' , 'sane' ]) # noqa: S607
2844- proc .communicate ()
2838+ if self ._initial_termios_settings is not None and self .stdin .isatty ():
2839+ import io
2840+ import termios
2841+
2842+ # Before the next command runs, fix any terminal problems like those
2843+ # caused by certain binary characters having been printed to it.
2844+ with self .sigint_protection , contextlib .suppress (io .UnsupportedOperation , termios .error ):
2845+ # This can fail if stdin is a pseudo-TTY, in which case we just ignore it
2846+ termios .tcsetattr (self .stdin .fileno (), termios .TCSANOW , self ._initial_termios_settings )
28452847
28462848 data = plugin .CommandFinalizationData (stop , statement )
28472849 for func in self ._cmdfinalization_hooks :
0 commit comments