Skip to content

Commit c298238

Browse files
gaogaotiantianblurb-it[bot]corona10
authored
pythongh-112510: Add readline.backend for the backend readline uses (pythonGH-112511)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Donghee Na <[email protected]>
1 parent f8ff80f commit c298238

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

Doc/library/readline.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@ Readline library in general.
2727
.. note::
2828

2929
The underlying Readline library API may be implemented by
30-
the ``libedit`` library instead of GNU readline.
30+
the ``editline`` (``libedit``) library instead of GNU readline.
3131
On macOS the :mod:`readline` module detects which library is being used
3232
at run time.
3333

34-
The configuration file for ``libedit`` is different from that
34+
The configuration file for ``editline`` is different from that
3535
of GNU readline. If you programmatically load configuration strings
36-
you can check for the text "libedit" in :const:`readline.__doc__`
37-
to differentiate between GNU readline and libedit.
36+
you can use :data:`backend` to determine which library is being used.
3837

39-
If you use *editline*/``libedit`` readline emulation on macOS, the
38+
If you use ``editline``/``libedit`` readline emulation on macOS, the
4039
initialization file located in your home directory is named
4140
``.editrc``. For example, the following content in ``~/.editrc`` will
4241
turn ON *vi* keybindings and TAB completion::
4342

4443
python:bind -v
4544
python:bind ^I rl_complete
4645

46+
.. data:: backend
47+
48+
The name of the underlying Readline library being used, either
49+
``"readline"`` or ``"editline"``.
50+
51+
.. versionadded:: 3.13
4752

4853
Init file
4954
---------

Lib/site.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ def register_readline():
444444

445445
# Reading the initialization (config) file may not be enough to set a
446446
# completion key, so we set one first and then read the file.
447-
readline_doc = getattr(readline, '__doc__', '')
448-
if readline_doc is not None and 'libedit' in readline_doc:
447+
if readline.backend == 'editline':
449448
readline.parse_and_bind('bind ^I rl_complete')
450449
else:
451450
readline.parse_and_bind('tab: complete')

Lib/test/test_pdb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3293,7 +3293,7 @@ def setUpClass():
32933293
# Ensure that the readline module is loaded
32943294
# If this fails, the test is skipped because SkipTest will be raised
32953295
readline = import_module('readline')
3296-
if readline.__doc__ and "libedit" in readline.__doc__:
3296+
if readline.backend == "editline":
32973297
raise unittest.SkipTest("libedit readline is not supported for pdb")
32983298

32993299
def test_basic_completion(self):

Lib/test/test_readline.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if hasattr(readline, "_READLINE_LIBRARY_VERSION"):
2020
is_editline = ("EditLine wrapper" in readline._READLINE_LIBRARY_VERSION)
2121
else:
22-
is_editline = (readline.__doc__ and "libedit" in readline.__doc__)
22+
is_editline = readline.backend == "editline"
2323

2424

2525
def setUpModule():
@@ -145,6 +145,9 @@ def test_init(self):
145145
TERM='xterm-256color')
146146
self.assertEqual(stdout, b'')
147147

148+
def test_backend(self):
149+
self.assertIn(readline.backend, ("readline", "editline"))
150+
148151
auto_history_script = """\
149152
import readline
150153
readline.set_auto_history({})
@@ -171,7 +174,7 @@ def complete(text, state):
171174
if state == 0 and text == "$":
172175
return "$complete"
173176
return None
174-
if "libedit" in getattr(readline, "__doc__", ""):
177+
if readline.backend == "editline":
175178
readline.parse_and_bind(r'bind "\\t" rl_complete')
176179
else:
177180
readline.parse_and_bind(r'"\\t": complete')
@@ -198,7 +201,7 @@ def test_nonascii(self):
198201

199202
script = r"""import readline
200203
201-
is_editline = readline.__doc__ and "libedit" in readline.__doc__
204+
is_editline = readline.backend == "editline"
202205
inserted = "[\xEFnserted]"
203206
macro = "|t\xEB[after]"
204207
set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :data:`readline.backend` for the backend readline uses (``editline`` or ``readline``)

Modules/readline.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -1538,15 +1538,18 @@ static struct PyModuleDef readlinemodule = {
15381538
PyMODINIT_FUNC
15391539
PyInit_readline(void)
15401540
{
1541+
const char *backend = "readline";
15411542
PyObject *m;
15421543
readlinestate *mod_state;
15431544

15441545
if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
15451546
using_libedit_emulation = 1;
15461547
}
15471548

1548-
if (using_libedit_emulation)
1549+
if (using_libedit_emulation) {
15491550
readlinemodule.m_doc = doc_module_le;
1551+
backend = "editline";
1552+
}
15501553

15511554

15521555
m = PyModule_Create(&readlinemodule);
@@ -1568,6 +1571,10 @@ PyInit_readline(void)
15681571
goto error;
15691572
}
15701573

1574+
if (PyModule_AddStringConstant(m, "backend", backend) < 0) {
1575+
goto error;
1576+
}
1577+
15711578
mod_state = (readlinestate *) PyModule_GetState(m);
15721579
if (mod_state == NULL){
15731580
goto error;

0 commit comments

Comments
 (0)