From 0f0ca07d79203629b62afba02c9c9e472da9b0dc Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 24 Oct 2025 20:30:59 +0200 Subject: [PATCH] [3.13] gh-140482: Preserve and restore `stty echo` as a test environment (GH-140519) (#140563) gh-140482: Preserve and restore `stty echo` as a test environment (GH-140519) (cherry picked from commit b3c713a0af5f5c4b5704d8019a893a1b70eba941) gh-140482: Restore `stty echo` as a test environment Co-authored-by: Barry Warsaw --- Lib/test/libregrtest/save_env.py | 26 +++++++++++++++++++ ...-10-23-16-39-49.gh-issue-140482.ZMtyeD.rst | 1 + 2 files changed, 27 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2025-10-23-16-39-49.gh-issue-140482.ZMtyeD.rst diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index b2cc381344b2..02492124a612 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -9,6 +9,13 @@ from test.support import os_helper 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 # noqa: F401 +except ModuleNotFoundError: + pass + class SkipTestEnvironment(Exception): pass @@ -65,6 +72,7 @@ class saved_test_environment: '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): @@ -292,6 +300,24 @@ class saved_test_environment: 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('.', '_') diff --git a/Misc/NEWS.d/next/Tests/2025-10-23-16-39-49.gh-issue-140482.ZMtyeD.rst b/Misc/NEWS.d/next/Tests/2025-10-23-16-39-49.gh-issue-140482.ZMtyeD.rst new file mode 100644 index 000000000000..20747ad7f113 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2025-10-23-16-39-49.gh-issue-140482.ZMtyeD.rst @@ -0,0 +1 @@ +Preserve and restore the state of ``stty echo`` as part of the test environment. -- 2.47.3