diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 3982686dd10aec..9d4b10a463a5e6 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -431,6 +431,16 @@ def my_hook(): readline.set_pre_input_hook(my_hook) self.assertIs(readline.get_pre_input_hook(), my_hook) + def test_environment_is_not_modified(self): + # os.environ contains enviroment at the time "os" module was loaded, so + # before the "readline" module is loaded. + original_env = dict(os.environ) + + # Force refresh of os.environ and make sure it is the same as before the + # refresh. + os.reload_environ() + self.assertEqual(dict(os.environ), original_env) + @unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled') class FreeThreadingTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-18-31-31.gh-issue-46927.sF02gj.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-18-31-31.gh-issue-46927.sF02gj.rst new file mode 100644 index 00000000000000..3ba9757c8acb5d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-18-31-31.gh-issue-46927.sF02gj.rst @@ -0,0 +1,2 @@ +Prevent :mod:`readline` from overriding the ``COLUMNS`` and ``LINES`` +environment variables, as values are not updated on terminal resize. diff --git a/Modules/readline.c b/Modules/readline.c index 579a34b02ceb67..0cc714ab6cfef6 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1349,6 +1349,13 @@ setup_readline(readlinestate *mod_state) /* The name must be defined before initialization */ rl_readline_name = "python"; +#if !defined(__APPLE__) + /* Prevent readline from changing environment variables such as LINES and + * COLUMNS. + */ + rl_change_environment = 0; +#endif + /* the libedit readline emulation resets key bindings etc * when calling rl_initialize. So call it upfront */