@@ -513,12 +513,13 @@ def __init__(
513
513
self ._initial_termios_settings = None
514
514
if not sys .platform .startswith ('win' ) and self .stdin .isatty ():
515
515
try :
516
+ import io
516
517
import termios
517
518
518
519
self ._initial_termios_settings = termios .tcgetattr (self .stdin .fileno ())
519
- except (ImportError , termios .error ):
520
+ except (ImportError , io . UnsupportedOperation , termios .error ):
520
521
# This can happen if termios isn't available or stdin is a pseudo-TTY
521
- pass
522
+ self . _initial_termios_settings = None
522
523
523
524
# If a startup script is provided and exists, then execute it in the startup commands
524
525
if startup_script :
@@ -2834,14 +2835,15 @@ def onecmd_plus_hooks(
2834
2835
2835
2836
def _run_cmdfinalization_hooks (self , stop : bool , statement : Statement | None ) -> bool :
2836
2837
"""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 )
2845
2847
2846
2848
data = plugin .CommandFinalizationData (stop , statement )
2847
2849
for func in self ._cmdfinalization_hooks :
0 commit comments