Skip to content

Commit 704a33d

Browse files
committed
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.
1 parent d70a359 commit 704a33d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/cmd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def complete(self, text, state):
263263
endidx = readline.get_endidx() - stripped
264264
if begidx>0:
265265
cmd, args, foo = self.parseline(line)
266-
if cmd == '':
266+
if not cmd:
267267
compfunc = self.completedefault
268268
else:
269269
try:

Lib/test/test_cmd.py

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import unittest
1010
import io
1111
from test import support
12+
from unittest.mock import patch
13+
14+
try:
15+
import readline
16+
except ImportError:
17+
readline = None
18+
1219

1320
class samplecmdclass(cmd.Cmd):
1421
"""
@@ -218,6 +225,22 @@ def test_input_reset_at_EOF(self):
218225
"(Cmd) \n"
219226
"(Cmd) *** Unknown syntax: EOF\n"))
220227

228+
@unittest.skipIf(readline is None, 'No readline module')
229+
@patch('readline.get_line_buffer', return_value='foo')
230+
@patch('readline.get_begidx', return_value=2)
231+
@patch('readline.get_endidx', return_value=2)
232+
def test_complete_with_None_from_parseline(self, *args):
233+
234+
class CustomCmd(cmd.Cmd):
235+
def parseline(self, line):
236+
return None, None, line
237+
238+
def completedefault(self, *args):
239+
return ['via_completedefault']
240+
241+
c = CustomCmd()
242+
self.assertEqual(c.complete("", 0), "via_completedefault")
243+
221244

222245
def test_main(verbose=None):
223246
from test import test_cmd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cmd.complete: use ``completedefault`` also with ``cmd=None`` returned from ``parseline``.

0 commit comments

Comments
 (0)