From 23ea56ef2088aa86b4f45d2d079b82d3485d7ac6 Mon Sep 17 00:00:00 2001 From: Mathieu Dubois-Briand Date: Wed, 7 May 2025 17:26:02 +0200 Subject: [PATCH 1/3] gh-46927: Prevent readline from overriding environment Readline library will set the LINES and COLUMNS environment variables during initialization. One might expect these variables to be updated later on SIGWINCH, but this is not the case with cpython. As a consequence, when the readline module is imported, any process launched from cpython with the default environment will have LINES and COLUMNS variables set, potentially to a wrong value. Use the rl_change_environment global variable to disable this initial setup of the environment variables. --- .../2025-05-07-18-31-31.gh-issue-46927.sF02gj.rst | 2 ++ Modules/readline.c | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-05-07-18-31-31.gh-issue-46927.sF02gj.rst 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..46a59da5b5466b 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"; +#ifndef WITH_EDITLINE + /* 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 */ From cd7a8502aa8188984762be997df74f174906df48 Mon Sep 17 00:00:00 2001 From: Mathieu Dubois-Briand Date: Thu, 8 May 2025 08:55:36 +0200 Subject: [PATCH 2/3] Disable rl_change_environment on apple --- Modules/readline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/readline.c b/Modules/readline.c index 46a59da5b5466b..0cc714ab6cfef6 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1349,7 +1349,7 @@ setup_readline(readlinestate *mod_state) /* The name must be defined before initialization */ rl_readline_name = "python"; -#ifndef WITH_EDITLINE +#if !defined(__APPLE__) /* Prevent readline from changing environment variables such as LINES and * COLUMNS. */ From 4d18a9cc50db0f5cfd6d9f767708405527c1f525 Mon Sep 17 00:00:00 2001 From: Mathieu Dubois-Briand Date: Mon, 12 May 2025 17:49:20 +0200 Subject: [PATCH 3/3] Add test --- Lib/test/test_readline.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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):