From 704a33d2cf91f34580e4a4abfeb29ffcc2da5412 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 17 Nov 2018 23:47:32 +0100 Subject: [PATCH 1/2] Cmd.complete: use completedefault also with cmd=None Otherwise it will trigger a TypeError ("must be str, not NoneType") below with: compfunc = getattr(self, 'complete_' + cmd) The doc for `parseline` states: > 'command' and 'args' may be None if the line couldn't be parsed. --- Lib/cmd.py | 2 +- Lib/test/test_cmd.py | 23 +++++++++++++++++++ .../2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst diff --git a/Lib/cmd.py b/Lib/cmd.py index 859e91096d8f57..cf5952c52ce78a 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -263,7 +263,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 96e0c30da328cf..3e2e6351919edf 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -9,6 +9,13 @@ import unittest import io from test import support +from unittest.mock import patch + +try: + import readline +except ImportError: + readline = None + class samplecmdclass(cmd.Cmd): """ @@ -218,6 +225,22 @@ def test_input_reset_at_EOF(self): "(Cmd) \n" "(Cmd) *** Unknown syntax: EOF\n")) + @unittest.skipIf(readline is None, 'No readline module') + @patch('readline.get_line_buffer', return_value='foo') + @patch('readline.get_begidx', return_value=2) + @patch('readline.get_endidx', return_value=2) + def test_complete_with_None_from_parseline(self, *args): + + 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") + def test_main(verbose=None): from test import test_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..2800b407b48a74 --- /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``. From 28acdbae4fe0c4855fa4336bea97a0a7de3ae45f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 11 Mar 2019 15:07:17 +0100 Subject: [PATCH 2/2] update news --- .../next/Library/2019-01-11-12-20-48.bpo-35270.Vtm5iW.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 2800b407b48a74..9a4945e37c71bd 100644 --- 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 @@ -1 +1 @@ -Cmd.complete: use ``completedefault`` also with ``cmd=None`` returned from ``parseline``. +Cmd.complete: use ``completedefault`` also with ``cmd=None`` returned from ``parseline``. Patch contributed by Daniel Hahler.