Skip to content

Commit 1d9406e

Browse files
authored
gh-133363: Fix Cmd completion for lines beginning with ! (#133364)
1 parent 3b43335 commit 1d9406e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Lib/cmd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def complete(self, text, state):
273273
endidx = readline.get_endidx() - stripped
274274
if begidx>0:
275275
cmd, args, foo = self.parseline(line)
276-
if cmd == '':
276+
if not cmd:
277277
compfunc = self.completedefault
278278
else:
279279
try:

Lib/test/test_cmd.py

+24
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ def do_tab_completion_test(self, args):
289289
self.assertIn(b'ab_completion_test', output)
290290
self.assertIn(b'tab completion success', output)
291291

292+
def test_bang_completion_without_do_shell(self):
293+
script = textwrap.dedent("""
294+
import cmd
295+
class simplecmd(cmd.Cmd):
296+
def completedefault(self, text, line, begidx, endidx):
297+
return ["hello"]
298+
299+
def default(self, line):
300+
if line.replace(" ", "") == "!hello":
301+
print('tab completion success')
302+
else:
303+
print('tab completion failure')
304+
return True
305+
306+
simplecmd().cmdloop()
307+
""")
308+
309+
# '! h' or '!h' and complete 'ello' to 'hello'
310+
for input in [b"! h\t\n", b"!h\t\n"]:
311+
with self.subTest(input=input):
312+
output = run_pty(script, input)
313+
self.assertIn(b'hello', output)
314+
self.assertIn(b'tab completion success', output)
315+
292316
def load_tests(loader, tests, pattern):
293317
tests.addTest(doctest.DocTestSuite())
294318
return tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :class:`cmd.Cmd` class has been fixed to reliably call the ``completedefault``
2+
method whenever the ``do_shell`` method is not defined and tab completion is
3+
requested for a line beginning with ``!``.

0 commit comments

Comments
 (0)