Skip to content

gh-46927: Prevent readline from overriding environment #133585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions Lib/test/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ def test_write_read_limited_history(self):
# So, we've only tested that the read did not fail.
# See TestHistoryManipulation for the full test.

def test_environment_is_not_modified(self):
original_env = dict(os.environ)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to add the use of subprocess here? This test always passes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err sorry, I reworked the test and forgot to remove the subprocess requirement.

But I do confirm this test fails if I revert the initial commit of the PR.

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prevent :mod:`readline` from overriding the ``COLUMNS`` and ``LINES``
environment variables, as values are not updated on terminal resize.
7 changes: 7 additions & 0 deletions Modules/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,13 @@ setup_readline(readlinestate *mod_state)
/* The name must be defined before initialization */
rl_readline_name = "python";

#if !defined(__APPLE__)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised I had to rely on the __APPLE__ define here. As I understand apple OS is using libedit, so I would have thought relying on WITH_EDITLINE would be enough. But the macOS pipeline proved me wrong...

/* 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
*/
Expand Down
Loading