Skip to content

Commit 42dd261

Browse files
[3.11] gh-112105: Make completer delims work on libedit (gh-112106) (gh-112488)
gh-112105: Make completer delims work on libedit (gh-112106) (cherry picked from commit 2df26d8) Co-authored-by: Tian Gao <[email protected]>
1 parent b85070c commit 42dd261

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Lib/test/test_readline.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import tempfile
8+
import textwrap
89
import unittest
910
from test.support import verbose
1011
from test.support.import_helper import import_module
@@ -163,6 +164,25 @@ def test_auto_history_disabled(self):
163164
# end, so don't expect it in the output.
164165
self.assertIn(b"History length: 0", output)
165166

167+
def test_set_complete_delims(self):
168+
script = textwrap.dedent("""
169+
import readline
170+
def complete(text, state):
171+
if state == 0 and text == "$":
172+
return "$complete"
173+
return None
174+
if "libedit" in getattr(readline, "__doc__", ""):
175+
readline.parse_and_bind(r'bind "\\t" rl_complete')
176+
else:
177+
readline.parse_and_bind(r'"\\t": complete')
178+
readline.set_completer_delims(" \\t\\n")
179+
readline.set_completer(complete)
180+
print(input())
181+
""")
182+
183+
output = run_pty(script, input=b"$\t\n")
184+
self.assertIn(b"$complete", output)
185+
166186
def test_nonascii(self):
167187
loc = locale.setlocale(locale.LC_CTYPE, None)
168188
if loc in ('C', 'POSIX'):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :func:`readline.set_completer_delims` work with libedit

Modules/readline.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,13 @@ readline_set_completer_delims(PyObject *module, PyObject *string)
572572
if (break_chars) {
573573
free(completer_word_break_characters);
574574
completer_word_break_characters = break_chars;
575+
#ifdef WITH_EDITLINE
576+
rl_basic_word_break_characters = break_chars;
577+
#else
578+
if (using_libedit_emulation) {
579+
rl_basic_word_break_characters = break_chars;
580+
}
581+
#endif
575582
rl_completer_word_break_characters = break_chars;
576583
Py_RETURN_NONE;
577584
}
@@ -1260,6 +1267,15 @@ setup_readline(readlinestate *mod_state)
12601267
completer_word_break_characters =
12611268
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
12621269
/* All nonalphanums except '.' */
1270+
#ifdef WITH_EDITLINE
1271+
// libedit uses rl_basic_word_break_characters instead of
1272+
// rl_completer_word_break_characters as complete delimiter
1273+
rl_basic_word_break_characters = completer_word_break_characters;
1274+
#else
1275+
if (using_libedit_emulation) {
1276+
rl_basic_word_break_characters = completer_word_break_characters;
1277+
}
1278+
#endif
12631279
rl_completer_word_break_characters = completer_word_break_characters;
12641280

12651281
mod_state->begidx = PyLong_FromLong(0L);

0 commit comments

Comments
 (0)