Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

from .utils import print_warning

# Import termios to save and restore terminal echo. This is only available on
# Unix, and it's fine if the module can't be found.
try:
import termios

Check failure on line 15 in Lib/test/libregrtest/save_env.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F401)

Lib/test/libregrtest/save_env.py:15:12: F401 `termios` imported but unused; consider using `importlib.util.find_spec` to test for availability
except ModuleNotFoundError:
pass


class SkipTestEnvironment(Exception):
pass
Expand Down Expand Up @@ -65,6 +72,7 @@
'shutil_archive_formats', 'shutil_unpack_formats',
'asyncio.events._event_loop_policy',
'urllib.requests._url_tempfiles', 'urllib.requests._opener',
'stty_echo',
)

def get_module(self, name):
Expand Down Expand Up @@ -292,6 +300,24 @@
warnings = self.get_module('warnings')
warnings.showwarning = fxn

def get_stty_echo(self):
termios = self.try_get_module('termios')
if not os.isatty(fd := sys.__stdin__.fileno()):
return None
attrs = termios.tcgetattr(fd)
lflags = attrs[3]
return bool(lflags & termios.ECHO)
def restore_stty_echo(self, echo):
termios = self.get_module('termios')
attrs = termios.tcgetattr(fd := sys.__stdin__.fileno())
if echo:
# Turn echo on.
attrs[3] |= termios.ECHO
else:
# Turn echo off.
attrs[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, attrs)

def resource_info(self):
for name in self.resources:
method_suffix = name.replace('.', '_')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve and restore the state of ``stty echo`` as part of the test environment.
Loading