Skip to content

Commit 7bee101

Browse files
committed
pythongh-140482: Restore stty echo as a test environment
1 parent f0291c3 commit 7bee101

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Lib/test/libregrtest/save_env.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import threading
66

7+
from importlib import import_module
78
from test import support
89
from test.support import os_helper
910

@@ -65,17 +66,23 @@ def __init__(self, test_name, verbose, quiet, *, pgo):
6566
'shutil_archive_formats', 'shutil_unpack_formats',
6667
'asyncio.events._event_loop_policy',
6768
'urllib.requests._url_tempfiles', 'urllib.requests._opener',
69+
'stty_echo',
6870
)
6971

7072
def get_module(self, name):
7173
# function for restore() methods
7274
return sys.modules[name]
7375

74-
def try_get_module(self, name):
76+
def try_get_module(self, name, *, demand=False):
7577
# function for get() methods
7678
try:
7779
return self.get_module(name)
7880
except KeyError:
81+
if demand:
82+
try:
83+
return import_module(name)
84+
except ModuleNotFoundError:
85+
pass
7986
raise SkipTestEnvironment
8087

8188
def get_urllib_requests__url_tempfiles(self):
@@ -292,6 +299,25 @@ def restore_warnings_showwarning(self, fxn):
292299
warnings = self.get_module('warnings')
293300
warnings.showwarning = fxn
294301

302+
def get_stty_echo(self):
303+
termios = self.try_get_module('termios', demand=True)
304+
if not os.isatty(fd := sys.__stdin__.fileno()):
305+
return None
306+
attrs = termios.tcgetattr(fd)
307+
lflags = attrs[3]
308+
return bool(lflags & termios.ECHO)
309+
310+
def restore_stty_echo(self, echo):
311+
termios = self.get_module('termios')
312+
attrs = termios.tcgetattr(fd := sys.__stdin__.fileno())
313+
if echo:
314+
# Turn echo on.
315+
attrs[3] |= termios.ECHO
316+
else:
317+
# Turn echo off.
318+
attrs[3] &= ~termios.ECHO
319+
termios.tcsetattr(fd, termios.TCSADRAIN, attrs)
320+
295321
def resource_info(self):
296322
for name in self.resources:
297323
method_suffix = name.replace('.', '_')

0 commit comments

Comments
 (0)