POSIX Windows , , - ( , , , , , ), . , POSIX - , , .
- termios. , .
, stty:
import subprocess
def echooff():
subprocess.run(['stty', '-echo'], check=True)
def echoon():
subprocess.run(['stty', 'echo'], check=True)
, , echoon , . , ( ) reset stty echo.
, :
try:
echooff()
# do stuff
finally:
echoon()
, , contextlib:
@contextlib.contextmanager
def echo_disabled():
try:
echooff()
yield
finally:
echoon()
:
with echo_disabled():
tty , , , termios:
@contextlib.contextmanager
def echo_disabled():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ECHO # lflags
try:
yield
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)