-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-133363: Fix Cmd completion for lines beginning with !
#133364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
J 8000 ump to
Diff view
Diff view
!
When a line begins with `!` and there's no `do_shell` method defined, `parsecmd` returns `None` as the `cmd`, which incorrectly leads to `None` being concatenated to `complete_` and triggering a `TypeError`. Instead, recognize `None` as a sentinel that means we should call `completedefault`, as an empty string already is.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,31 @@ def do_tab_completion_test(self, args): | |
self.assertIn(b'ab_completion_test', output) | ||
self.assertIn(b'tab completion success', output) | ||
|
||
def test_bang_completion_without_do_shell(self): | ||
script = textwrap.dedent(""" | ||
import cmd | ||
class simplecmd(cmd.Cmd): | ||
def completedefault(self, text, line, begidx, endidx): | ||
return ["hello"] | ||
|
||
def default(self, line): | ||
if line == "! hello": | ||
print('tab completion success') | ||
else: | ||
print('tab completion failure') | ||
return True | ||
|
||
simplecmd().cmdloop() | ||
""") | ||
|
||
# '! h' and complete 'ello' to '! hello' | ||
input = b"! h\t\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do add check here for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does need to be another method, because the assertions are: self.assertIn(b'hello', output)
self.assertIn(b'tab completion success', output) and those would pass if either of the two inputs succeeded, not only if both of them did. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe we could use a subtest for this? I know how to avoid duplication with pytest, but I'm not really familiar with unittest. Does this work? # '! h' or '!h' and complete 'ello' to 'hello'
for input in [b"! h\t\n", b"!h\t\n"]:
with self.subTest(input=input):
output = run_pty(script, input)
self.assertIn(b'hello', output)
self.assertIn(b'tab completion success', output) Seems to... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean just use the same script and do two |
||
|
||
output = run_pty(script, input) | ||
|
||
self.assertIn(b'ello', output) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, because that's what is used in the previous test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The tests do both pass if I adjust them to check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized I co-authored that test. Ha. |
||
self.assertIn(b'tab completion success', output) | ||
|
||
def load_tests(loader, tests, pattern): | ||
tests.addTest(doctest.DocTestSuite()) | ||
return tests | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The :class:`cmd.Cmd` class has been fixed to call the ``completedefault`` | ||
method whenever the ``do_shell`` method is not defined and tab completion is | ||
requested for a line beginning with ``!``. Previously ``completedefault`` | ||
was called only if there were no spaces between the ``!`` and the cursor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is that even true? I thought the completion never works: elif line[0] == '!':
if hasattr(self, 'do_shell'):
line = 'shell ' + line[1:]
else:
return None, None, line Anyway, normally in the news we only need to say what is fixed, we don't need to do a complete description of what's the previous behavior. News entry should be a concise sentence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, because
OK, I've shortened it and just say that it's fixed to reliably call the method, since previously it was inconsistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay but it's still not good, because that's not really a name. It still won't give us the correct completion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not worry about that, we can do that in pdb I believe. I don't want to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
True, though that seems to need a bigger change if you want to fix that... Do you want to special case if begidx>0:
...
elif line.startswith("!"):
compfunc = self.completedefault
else:
compfunc = self.completenames Even that's not enough to make PDB's completion work without the space, though, because that'll give a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wrong. |
||
position where tab completion was attempted. |
Uh oh!
There was an error while loading. Please reload this page.