diff options
author | Constantin Hong <hongconstantin@gmail.com> | 2023-12-05 07:24:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-05 07:24:56 (GMT) |
commit | aa5bee30abb28d73a838399f4c3a8bcdc5108fe3 (patch) | |
tree | 98d459e7dcd3e011709aa7f82d2e5919cce697f7 /Lib | |
parent | dc824c5dc120ffed84bafd23f95e95a99678ed6a (diff) | |
download | cpython-aa5bee30abb28d73a838399f4c3a8bcdc5108fe3.zip cpython-aa5bee30abb28d73a838399f4c3a8bcdc5108fe3.tar.gz cpython-aa5bee30abb28d73a838399f4c3a8bcdc5108fe3.tar.bz2 |
gh-102130: Support tab completion in cmd for Libedit. (GH-107748)
---
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/cmd.py | 10 | ||||
-rw-r--r-- | Lib/test/test_cmd.py | 30 |
2 files changed, 39 insertions, 1 deletions
@@ -108,7 +108,15 @@ class Cmd: import readline self.old_completer = readline.get_completer() readline.set_completer(self.complete) - readline.parse_and_bind(self.completekey+": complete") + if readline.backend == "editline": + if self.completekey == 'tab': + # libedit uses "^I" instead of "tab" + command_string = "bind ^I rl_complete" + else: + command_string = f"bind {self.completekey} rl_complete" + else: + command_string = f"{self.completekey}: complete" + readline.parse_and_bind(command_string) except ImportError: pass try: diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py index 951336f..46ec82b 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -9,7 +9,10 @@ import sys import doctest import unittest import io +import textwrap from test import support +from test.support.import_helper import import_module +from test.support.pty_helper import run_pty class samplecmdclass(cmd.Cmd): """ @@ -259,6 +262,33 @@ class CmdPrintExceptionClass(cmd.Cmd): def default(self, line): print(sys.exc_info()[:2]) + +@support.requires_subprocess() +class CmdTestReadline(unittest.TestCase): + 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') + + def test_basic_completion(self): + script = textwrap.dedent(""" + import cmd + class simplecmd(cmd.Cmd): + def do_tab_completion_test(self, args): + print('tab completion success') + return True + + simplecmd().cmdloop() + """) + + # 't' and complete 'ab_completion_test' to 'tab_completion_test' + input = b"t\t\n" + + output = run_pty(script, input) + + self.assertIn(b'ab_completion_test', output) + self.assertIn(b'tab completion success', output) + def load_tests(loader, tests, pattern): tests.addTest(doctest.DocTestSuite()) return tests |