diff --git a/Lib/cmd.py b/Lib/cmd.py index 2e358d6cd5a02d..22f9f15abc83fd 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -270,7 +270,7 @@ def complete(self, text, state): endidx = readline.get_endidx() - stripped if begidx>0: cmd, args, foo = self.parseline(line) - if cmd == '': + if not cmd: compfunc = self.completedefault else: try: diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py index 46ec82b704963d..6789b2fcf55b5c 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -11,6 +11,7 @@ import io import textwrap from test import support +from unittest.mock import patch from test.support.import_helper import import_module from test.support.pty_helper import run_pty @@ -246,6 +247,22 @@ def test_input_reset_at_EOF(self): "(Cmd) \n" "(Cmd) *** Unknown syntax: EOF\n")) + def test_complete_with_None_from_parseline(self, *args): + readline = import_module('readline') + + with (patch('readline.get_line_buffer', return_value='foo'), + patch('readline.get_begidx', return_value=2), + patch('readline.get_endidx', return_value=2)): + class CustomCmd(cmd.Cmd): + def parseline(self, line): + return None, None, line + + def completedefault(self, *args): + return ['via_completedefault'] + + c = CustomCmd() + self.assertEqual(c.complete("", 0), "via_completedefault") + class CmdPrintExceptionClass(cmd.Cmd): """ diff --git a/Misc/NEWS.d/next/Library/2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst b/Misc/NEWS.d/next/Library/2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst new file mode 100644 index 00000000000000..9a4945e37c71bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst @@ -0,0 +1 @@ +Cmd.complete: use ``completedefault`` also with ``cmd=None`` returned from ``parseline``. Patch contributed by Daniel Hahler.