Skip to content

gh-106734: Disable tab completion in multiline mode of pdb #106735

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

Merged
merged 7 commits into from
Sep 12, 2023

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Jul 14, 2023

Lib/pdb.py Outdated
try:
if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
# Multi-line mode
try:
import readline
Copy link
Member

@brandtbucher brandtbucher Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's worth doing this just once at module level? I see we're already trying to import readline up in Pdb.__init__.

Lib/pdb.py Outdated
try:
import readline
# Disable tab-completion temporarily
readline.parse_and_bind('tab: self-insert')
Copy link
Member

@brandtbucher brandtbucher Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the Cmd base class is a bit more careful about moves like this. For example, maybe we should only do this if self.use_rawinput and self.completekey == "tab" and readline.get_completer() is self.complete? That way we don't change the global readline state in an unexpected way during debugging.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should protect this with use_rawinput and completekey, however, I'm not sure if checking the get_completer() is necessary. The completer should always be self.complete. In any case, we should not trigger the completer function in multiline mode - tab should always input tab.

As for importing readline globally, cmd does it twice in the code - I think it's because readline is only available on some systems. This is more explicit than having a readline is None check in the function.

I can move the readline reverting part into the mutiline if statement - so we can avoid changing the readline states in exec() - even though it's hard to imagine having useful code there to use readline.

Lib/pdb.py Outdated
Comment on lines 494 to 495
if readline:
readline.parse_and_bind('tab: complete')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@gaogaotiantian
Copy link
Member Author

So I switched to a context based solution - a couple of days ago a context manager was included in pdb so we already had that. Now the input part is contained in a "disable tab" context which checks for self.use_rawinput and self.completekey.

Co-authored-by: Brandt Bucher <[email protected]>
@brandtbucher brandtbucher merged commit 391f3e3 into python:main Sep 12, 2023
@gaogaotiantian gaogaotiantian deleted the pdb-multiline-tab branch October 7, 2023 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants