Skip to content

Commit

Permalink
pythongh-112105: Make completer delims work on libedit (pythongh-112106)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Nov 28, 2023
1 parent ac4b442 commit 2df26d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Lib/test/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import tempfile
import textwrap
import unittest
from test.support import verbose
from test.support.import_helper import import_module
Expand Down Expand Up @@ -163,6 +164,25 @@ def test_auto_history_disabled(self):
# end, so don't expect it in the output.
self.assertIn(b"History length: 0", output)

def test_set_complete_delims(self):
script = textwrap.dedent("""
import readline
def complete(text, state):
if state == 0 and text == "$":
return "$complete"
return None
if "libedit" in getattr(readline, "__doc__", ""):
readline.parse_and_bind(r'bind "\\t" rl_complete')
else:
readline.parse_and_bind(r'"\\t": complete')
readline.set_completer_delims(" \\t\\n")
readline.set_completer(complete)
print(input())
""")

output = run_pty(script, input=b"$\t\n")
self.assertIn(b"$complete", output)

def test_nonascii(self):
loc = locale.setlocale(locale.LC_CTYPE, None)
if loc in ('C', 'POSIX'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :func:`readline.set_completer_delims` work with libedit
16 changes: 16 additions & 0 deletions Modules/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ readline_set_completer_delims(PyObject *module, PyObject *string)
if (break_chars) {
free(completer_word_break_characters);
completer_word_break_characters = break_chars;
#ifdef WITH_EDITLINE
rl_basic_word_break_characters = break_chars;
#else
if (using_libedit_emulation) {
rl_basic_word_break_characters = break_chars;
}
#endif
rl_completer_word_break_characters = break_chars;
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -1309,6 +1316,15 @@ setup_readline(readlinestate *mod_state)
completer_word_break_characters =
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
/* All nonalphanums except '.' */
#ifdef WITH_EDITLINE
// libedit uses rl_basic_word_break_characters instead of
// rl_completer_word_break_characters as complete delimiter
rl_basic_word_break_characters = completer_word_break_characters;
#else
if (using_libedit_emulation) {
rl_basic_word_break_characters = completer_word_break_characters;
}
#endif
rl_completer_word_break_characters = completer_word_break_characters;

mod_state->begidx = PyLong_FromLong(0L);
Expand Down

0 comments on commit 2df26d8

Please sign in to comment.