From a912085cfd10d1a409c9bb7dacbc74713c92b0bf Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 28 Nov 2023 18:07:01 -0800 Subject: [PATCH 1/7] Add readline.backend for the backend it uses --- Doc/library/readline.rst | 13 ++++++++----- Lib/test/test_pdb.py | 2 +- Lib/test/test_readline.py | 4 ++-- Modules/readline.c | 10 +++++++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst index 8fb0eca8df74d8..4549d462c30bec 100644 --- a/Doc/library/readline.rst +++ b/Doc/library/readline.rst @@ -27,16 +27,15 @@ Readline library in general. .. note:: The underlying Readline library API may be implemented by - the ``libedit`` library instead of GNU readline. + the ``editline`` (``libedit``) library instead of GNU readline. On macOS the :mod:`readline` module detects which library is being used at run time. - The configuration file for ``libedit`` is different from that + The configuration file for ``editline`` is different from that of GNU readline. If you programmatically load configuration strings - you can check for the text "libedit" in :const:`readline.__doc__` - to differentiate between GNU readline and libedit. + you can use :data:`backend` to determine which library is being used. - If you use *editline*/``libedit`` readline emulation on macOS, the + If you use ``editline``/``libedit`` readline emulation on macOS, the initialization file located in your home directory is named ``.editrc``. For example, the following content in ``~/.editrc`` will turn ON *vi* keybindings and TAB completion:: @@ -44,6 +43,10 @@ Readline library in general. python:bind -v python:bind ^I rl_complete +.. data:: backend + + The name of the underlying Readline library being used, either + ``"readline"`` or ``"editline"``. Init file --------- diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 2a279ca869e9c7..50d8c8f52a909d 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -3293,7 +3293,7 @@ def setUpClass(): # Ensure that the readline module is loaded # If this fails, the test is skipped because SkipTest will be raised readline = import_module('readline') - if readline.__doc__ and "libedit" in readline.__doc__: + if readline.backend == "editline": raise unittest.SkipTest("libedit readline is not supported for pdb") def test_basic_completion(self): diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 6c2726d3209ecf..c7b4ef3485d209 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -19,7 +19,7 @@ if hasattr(readline, "_READLINE_LIBRARY_VERSION"): is_editline = ("EditLine wrapper" in readline._READLINE_LIBRARY_VERSION) else: - is_editline = (readline.__doc__ and "libedit" in readline.__doc__) + is_editline = readline.backend == "editline" def setUpModule(): @@ -198,7 +198,7 @@ def test_nonascii(self): script = r"""import readline -is_editline = readline.__doc__ and "libedit" in readline.__doc__ +is_editline = readline.backend == "editline" inserted = "[\xEFnserted]" macro = "|t\xEB[after]" set_pre_input_hook = getattr(readline, "set_pre_input_hook", None) diff --git a/Modules/readline.c b/Modules/readline.c index eb9a3d4693ee90..1d538eb066cc7e 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1538,6 +1538,7 @@ static struct PyModuleDef readlinemodule = { PyMODINIT_FUNC PyInit_readline(void) { + const char *backend = "readline"; PyObject *m; readlinestate *mod_state; @@ -1545,8 +1546,10 @@ PyInit_readline(void) using_libedit_emulation = 1; } - if (using_libedit_emulation) + if (using_libedit_emulation) { readlinemodule.m_doc = doc_module_le; + backend = "editline"; + } m = PyModule_Create(&readlinemodule); @@ -1568,6 +1571,11 @@ PyInit_readline(void) goto error; } + if (PyModule_AddStringConstant(m, "backend", backend) < 0) + { + goto error; + } + mod_state = (readlinestate *) PyModule_GetState(m); if (mod_state == NULL){ goto error; From bc70220f689bd12305ee9876ccac239b03679a84 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 29 Nov 2023 02:26:34 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-11-29-02-26-32.gh-issue-112510.j-zXGc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-11-29-02-26-32.gh-issue-112510.j-zXGc.rst diff --git a/Misc/NEWS.d/next/Library/2023-11-29-02-26-32.gh-issue-112510.j-zXGc.rst b/Misc/NEWS.d/next/Library/2023-11-29-02-26-32.gh-issue-112510.j-zXGc.rst new file mode 100644 index 00000000000000..02de6fa80c1b3e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-29-02-26-32.gh-issue-112510.j-zXGc.rst @@ -0,0 +1 @@ +Add :data:`readline.backend` for the backend readline uses (``editline`` or ``readline``) From f707ddb61632e38fe0c7bea5234c58ba5022fb9d Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 28 Nov 2023 19:53:38 -0800 Subject: [PATCH 3/7] Update Modules/readline.c Co-authored-by: Donghee Na --- Modules/readline.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/readline.c b/Modules/readline.c index 1d538eb066cc7e..afbb7f8f0ec18f 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -1571,8 +1571,7 @@ PyInit_readline(void) goto error; } - if (PyModule_AddStringConstant(m, "backend", backend) < 0) - { + if (PyModule_AddStringConstant(m, "backend", backend) < 0) { goto error; } From 95d0e57cefcb534fdd7eec566d760ae95874bb72 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 28 Nov 2023 18:13:23 -0800 Subject: [PATCH 4/7] Update editline check --- Lib/site.py | 3 +-- Lib/test/test_readline.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/site.py b/Lib/site.py index 672fa7b000ad02..2517b7e5f1d22a 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -444,8 +444,7 @@ def register_readline(): # Reading the initialization (config) file may not be enough to set a # completion key, so we set one first and then read the file. - readline_doc = getattr(readline, '__doc__', '') - if readline_doc is not None and 'libedit' in readline_doc: + if readline.backend == 'editline': readline.parse_and_bind('bind ^I rl_complete') else: readline.parse_and_bind('tab: complete') diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index c7b4ef3485d209..270505a2b9b042 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -171,7 +171,7 @@ def complete(text, state): if state == 0 and text == "$": return "$complete" return None - if "libedit" in getattr(readline, "__doc__", ""): + if readline.backend == "editline": readline.parse_and_bind(r'bind "\\t" rl_complete') else: readline.parse_and_bind(r'"\\t": complete') From 4e595f8826f7bdb4240bb97be84f4f895c5d1e40 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 30 Nov 2023 13:21:57 -0800 Subject: [PATCH 5/7] Add test for backend --- Lib/test/test_readline.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 270505a2b9b042..5e0e6f8dfac651 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -145,6 +145,9 @@ def test_init(self): TERM='xterm-256color') self.assertEqual(stdout, b'') + def test_backend(self): + self.assertIn(readline.backend, ("readline", "editline")) + auto_history_script = """\ import readline readline.set_auto_history({}) From 3f78b4f7ad7f88803af1158e5b6cadede407e519 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 1 Dec 2023 14:23:37 +0100 Subject: [PATCH 6/7] Add versionadded note --- Doc/library/readline.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst index 4549d462c30bec..3a8d398c823dca 100644 --- a/Doc/library/readline.rst +++ b/Doc/library/readline.rst @@ -47,6 +47,8 @@ Readline library in general. The name of the underlying Readline library being used, either ``"readline"`` or ``"editline"``. + + .. versionadded:: 3.13 Init file --------- From 8cc11ae87fdd4c7ad5bc0eb6cf03b0903f0f9289 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 1 Dec 2023 14:27:15 +0100 Subject: [PATCH 7/7] Fix whitespace --- Doc/library/readline.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst index 3a8d398c823dca..2e0f45ced30b9c 100644 --- a/Doc/library/readline.rst +++ b/Doc/library/readline.rst @@ -47,7 +47,7 @@ Readline library in general. The name of the underlying Readline library being used, either ``"readline"`` or ``"editline"``. - + .. versionadded:: 3.13 Init file