Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-102130: Support tab completion in cmd for Libedit.#107748

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 29 commits intopython:mainfromConstantin1489:fix_libedit
Dec 5, 2023

Conversation

@Constantin1489
Copy link
Contributor

@Constantin1489Constantin1489 commentedAug 7, 2023
edited
Loading

After I dug the rabbit hole, I wanted to share something and commit it to CPython.

My commit allows tab completion in cmd for Libedit. (EDIT AFTER MERGE: this allows tab completion for pdb with libedit)

tab ofreadline.parse_and_bind("tab: complete") is a symbolic character name. Libedit doesn't support a number of symbolic character names(like TAB, ESC, DEL).

FYI, for better compatibility, Python Documents should suggest using backslashed escape sequences(e.g.\t,\a) or the ASCII character corresponding to the octal number(or octal excape sequences).(e.g.\011), It's because the only thing GNU readline(man bash and/Readline Key Bindings to search the part.) and Libedit(man editrc) have in common.

Also, unlike GNU emacs software, GNU readline doesn't support control characters of the form '^character' (e.g. '^I' it's a tab).
Both GNU readline(man bash and/Readline Key Bindings to search the part.) and Libedit(man editrc) support the following backslashed escape sequences and octal escape sequences.

\a     bell\b     backspace\f     form feed\n     newline\r     carriage return\t     horizontal tab\v     vertical tab\nnn   octal escape sequences

GNU readline's predefined commands are in the '/Readline Command Names' ofman bash.
Libedit's predefined commands are in the section "EDITOR COMMANDS" ofman editrc. But there are other undocumented commands in its source code.
Or you can get a list byimport readline; readline.parse_and_bind('bind -l') (Libedit only. This shows undocumented commands too).


📚 Documentation preview 📚:https://cpython-previews--107748.org.readthedocs.build/

keepworking and yan12125 reacted with thumbs up emoji
@bedevere-bot
Copy link

Most changes to Pythonrequire a NEWS entry.

Please add it using theblurb_it web app or theblurb command-line tool.

@Constantin1489
Copy link
ContributorAuthor

Constantin1489 commentedAug 7, 2023
edited
Loading

There is something that Python core developers decide.

  1. Supporting "TAB" symbolic character name for libedit in "cmd.py".
readline_doc = getattr(readline, '__doc__', '')if readline_doc is not None and 'libedit' in readline_doc:     if self.completekey is 'tab':        self.completekey = r'"\t"'

OR just

if self.completekey is 'tab':    self.completekey = r'"\t"'

If this is supported, then other third-party libraries can support Libedit tab completion without changing their code.
I don't want this support because it will give a false impression of common key sequences and the requirement to support other symbolic character names.

  1. Removing editrc examples in readline.rst.

Because of the absence of the GNU inputrc example inreadline.rst, the editrc example may be just for hotfix to do a tab complete in cmd or pdb.

For Vim keybinding in the editrc example(python:bind -v), I think it's valid. For this reason, Vim keybinding for GNU inputrc example is needed.

  1. Changing keybindings in related test files.
    There are^I andtab in related test files. If you need me to change them, I will do it.

Just so you know, if you want to debug this problem, please check what I found below.

Use one of these lines to enable tab completion in Python using~/.editrc for libedit.

python:bind "\t" rl_completepython:bind "\011" rl_completepython:bind \\t rl_completepython:bind "" rl_complete# "" is a tab.

To use them withreadline.parse_and_bind() in Python, remove'python:' prefix. To make clear, If you didimport readline; readline.parse_and_bind('bind "\t" rl_complete') in pdb interactively (e.g.,$python -m pdb somefile.py), it will autocomplete file names of the current directory. It's normal because the binding statement wasn't called in the pdb library.

The key sequences("\t","\011") of the first two lines in the code block above are documented inman editrc. The first one is called a backslashed escape sequence. The second one is called the ASCII character corresponding to the octal number(or octal escape character).

But the last two lines are not. I stumble across them.

To do another complete command in Libedit, this is possible.
import readline; readline.parse_and_bind('bind -s "\011" ee')
In this case, If you press a tab, 'ee' will be typed.
Other possible options are appeared inman editrc.

I found several ways to debug it. (these are examples.)

  • In Python interpreter,import readline; readline.parse_and_bind('bind -s "\011" ee')
  • Replace'tab' with'' in the pdb.py and cmd.py of your python directory in$PATH. e.g./usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pdb.py.

How to check the keybinding setting applied with Libedit.

import readlinereadline.parse_and_bind('bind')

This shows the user's current applied libedit readline setting.

@gaogaotiantian
Copy link
Member

I don't think we should change the default and hope the completekey passed in is correct. It could break users code, for example, if they compareself.completekey == tab. Also, if the user subclassed thecmd.Cmd class and did somethingpdb does like

classMyCmd(cmd.Cmd):def__init__(self,completekey='tab'):cmd.Cmd.__init__(self,completekey)

They will still have the issue.

We should convert the completekey internally and just do a different binding command withlibedit. Minimal code changes and fix things for good.

Also, why do you want to change thesite.py file? It works for now I believe. Python REPL has the complete feature withlibedit.

I believe the right way to do this is just add an extra check incmd.Cmd forlibedit, and do the correct thing (converttab to"\t" or the equivalent inlibedit, then use a different binding command).

Also, if you want to finish this, you'd need a regression test on at leastcmd.Cmd, it would be easier after#110945 is merged so you'll have a helper function for pty.

I can help you with the review and find the core-dev for approval if you want to work on this.

@Constantin1489
Copy link
ContributorAuthor

Constantin1489 commentedNov 5, 2023
edited
Loading

Yes, what you pointed is valid.
So you want Python should change the tab internally for portability.

Minimal code changes and fix things for good.

Because I was unsure about the decision I made. This is what I asked in number 1#107748 (comment).
In that case, a user who uses libedit with your custom object will experience when press 't' will be ignored sometimes.

At the time I wrote this PR, I thought, that if a user did something wrong, the wrong result must be thrown. And the docs must give a pledge. (You know when learning the linux thing there must be some reason I don't know and some result is unintuitive or counterintuitive for newcomer.).

Thank you for giving me direction for what I consider. I will do that within a week as soon as possible.

The second thing you mention(site.py) is just for a tone and manner thing(second and third thing that I asked in#107748 (comment)). I will revert it.

Thank you for helping me.

@gaogaotiantian
Copy link
Member

I believe you only need to changecmd.py, which would make this PR compact and easy to review. I'd recommend you to wait for#110945 so you'll have a test framework for completion. Actually enabling the test in that PR on libedit would just do.

BTW, you should not useis to compare strings. In this very specific case it actually works because of the python internal cache for intern strings, but that's definitely not what you should rely on. Always use== to check the string equality.

Constantin1489 reacted with eyes emoji

@Constantin1489Constantin1489 changed the titlegh-102130: Support a tab completion for Libedit in cmd, site, and pdb.gh-102130: Support a tab completion in cmd for Libedit.Nov 6, 2023
Constantin1489and others added2 commitsNovember 7, 2023 06:20
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@Constantin1489Constantin1489 marked this pull request as draftNovember 6, 2023 22:17
@Constantin1489Constantin1489 marked this pull request as ready for reviewNovember 6, 2023 23:33
@Constantin1489

This comment was marked as resolved.

@gaogaotiantian
Copy link
Member

I have the hardest time believing that commit can cause the completion issue. The code should not be executed at all for completion. Are you sure you did the test correctly?

@Constantin1489
Copy link
ContributorAuthor

I'm sorry. You are right. Maybe my environment goes something wrong.

@gaogaotiantian
Copy link
Member

Hi@Constantin1489 , now that#111826 is merged, you have the utility to write tests for the feature. Could you:

@encukou
Copy link
Member

And one more thing I always forget to point out (sorry for that)!

At the end of theCmd class documentation (before.. _cmd-objects:), add a versionchanged note like

   .. versionchanged:: 3.11   ``completekey='tab'`` is replaced by ``'^I'`` for ``editline``.

Also, please ignore the free-threaded CI failures; they're not your fault and they're allowed to fail for now.

Constantin1489 reacted with rocket emoji

@encukou
Copy link
Member

encukou commentedDec 1, 2023 via email

change the NEWS text, the commit names don't matter
On December 1, 2023 5:02:04 PM GMT+01:00, Constantin Hong ***@***.***> wrote:@Constantin1489 commented on this pull request.> @@ -0,0 +1 @@+Support tab completion in :mod:`cmd` for Libedit.Should I change Libedit to editline of this and this commit post's name?```gh-102130: Support tab completion in cmd for Libedit.->gh-102130: Support tab completion in cmd for editline.```--Reply to this email directly or view it on GitHub:#107748 (review)You are receiving this because you were mentioned.Message ID: ***@***.***>
-- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Constantin1489 reacted with thumbs up emoji

Constantin1489and others added2 commitsDecember 4, 2023 22:25
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@encukouencukou added the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelDec 4, 2023
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@encukou for commit652100f 🤖

If you want to schedule another build, you need to add the🔨 test-with-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbotsTest PR w/ buildbots; report in status section labelDec 4, 2023
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@encukouencukou merged commitaa5bee3 intopython:mainDec 5, 2023
@encukou
Copy link
Member

Thank you for your contribution!

Constantin1489 reacted with hooray emoji

@Constantin1489Constantin1489 deleted the fix_libedit branchDecember 5, 2023 21:43
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
…-107748)---Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@encukouencukou mentioned this pull requestJun 17, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
…-107748)---Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@tanloong
Copy link
Contributor

tanloong commentedOct 25, 2025
edited
Loading

Hi@gaogaotiantian, as libedit is now supported, is this part intest_pdb.py not needed anymore?https://github.com/python/cpython/blob/main/Lib/test/test_pdb.py#L4780-L4781

Can I make a PR to remove it?

@gaogaotiantian
Copy link
Member

@tanloong, I don't believe you can. If you read the full discussion (to be more specific -#107748 (comment)), you'll find that we tried to do that but failed. The problem islibedit has some issues for inputing spaces so all the multiline tests would fail. Feel free to give it a try (you'll need libedit so make surereadline.backend returnseditline before you start working on it). If you can solve the problem, I'd be happy to review the PR.

@tanloong
Copy link
Contributor

@gaogaotiantian, thanks so much for clarifying, I totally missed it from not reading carefully.

I want to be honest I'm not sure if I can fix the libedit space issue, but I'll still give it a try. Thanks again!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@encukouencukouencukou left review comments

@gaogaotiantiangaogaotiantiangaogaotiantian approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@Constantin1489@bedevere-bot@gaogaotiantian@encukou@tanloong

[8]ページ先頭

©2009-2025 Movatter.jp