-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
rlcompleter should be enabled automatically #50095
Comments
An interactive prompt should offer working completion out-of-the-box, Note that it should work not only when Python is run without arguments, Safety: if 'rlcompleter' or 'readline' are not availiable (e.g. broken Portability: there won't be completion on windows (as long as pyreadline |
What would be the right place for this to happen? I first thought of But perhaps tab isn’t the right key to bind. I think inputrc could set Moreover, in my PYTHONSTARTUP I not only bind tab to complete, but also So, I don’t know the history and choices behind readline.so, but from a [1] XDG Base Directory Specification: Cheers, Merwok |
He, adding completion is also something I find myself adding on every |
for what it is worth, I am +1 on having completion and history file work by default. The sqlite3 command line does this, for example. I think it is what unix user expect nowadays, and I think it is reasonable. Looking at my home directory, it would appear that the de-facto standard history file name would be .python_history. This is based on .mysql_history, .psql_history, and .sqlite_history, none of which I configured explicitly to support history saving. |
Okay, that’s one question answered. Still to solve: How to bind the right key? (“But perhaps tab isn’t the right key to bind. I think inputrc could set it to something different, perhaps shell rc files too. Is there an API to get this setting, does readline.so already do the right thing, or does it have to be added?”) |
I think TAB is the key expected by most people, so let's make it the default. |
Keep in mind that the Python readline module may be linked to either GNU readline or the BSD editline (libedit) library and they have different command strings. Note the warning here: http://docs.python.org/dev/py3k/library/readline.html Here's a snippet of what I have today in my startup file: import rlcompleter
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete") See bpo-10666 for more details and some possible changes. |
Nosying Martin: any Windows installer concerns? |
There is no readline support on Windows at all, so I don't think the Windows installer can be affected. I'm uncertain what the proposed change to Python is at this point, though. |
Ned, does readline.read_init_file() with libedit? |
readline.read_init_file() does work with libedit. The directives read have to be in libedit format. |
Please ignore the generated patches, left here for Martin’s benefit, and review the manually attached fix-5845.diff. I have used read_init_file instead of hard-coding tab (it’s anyway the default for readline). I may have repeated the same info in too much doc files, but I preferred to have more than one place where people could learn about the new behavior. Advice welcomed. If the basic approach looks good, especially if site still feels the right place for this, I will add tests. |
Updated patch (not using Mercurial, looks like I haven’t enough bandwidth to push all those changesets). |
I think there's a bug in the way you are detecting interactive mode: $ ./python -c "import sys; print(sys.stdin.isatty())"
True |
Interestingly, there's already the following code in Modules/main.c: if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
isatty(fileno(stdin))) {
PyObject *v;
v = PyImport_ImportModule("readline");
if (v == NULL)
PyErr_Clear();
else
Py_DECREF(v);
} ...meaning readline already gets imported automatically when desired. And indeed: $ ./python -S
Python 3.3.0a0 (default:50f1922bc1d5, Aug 18 2011, 00:09:47)
[GCC 4.5.2] on linux2
>>> import sys
>>> sys.modules['readline']
<module 'readline' from '/home/antoine/cpython/default/build/lib.linux-x86_64-3.3-pydebug/readline.cpython-33dm.so'> So perhaps we could simply change this code to import another, private module (e.g. "_setupinteractive.py") which would setup readline and rlcompleter? |
We can’t just decide to enable completion by checking “'readline' in sys.modules” in site, because it always returns False. site is imported in Python/pythonrun.c, and I guess that Modules/main.c is run afterwards. More importantly, I think the import of readline in Modules/main.c is a CPython implementation detail, and I’d prefer to have code in site that is directly useful for other VMs. (I’d go as far as proposing to remove the import readline from main.c, when my patch makes it obsolete.) I re-read the docs for sys.argv and the -m switch and decided to translate to C checks (“command == NULL && filename == NULL && module == NULL”) to “if not sys.argv[0]”. Unfortunately, sys.argv is not available when site is imported, so this is a dead end :( So, I could try your _setupinteractive module idea, even though I think it’s a bit unclean, or drink the cool aid and set up completion in main.c. |
Easily detecting interactive mode is of general interest for customization.
BTW, drawback of doing any such setup in site.py: "python -S" would |
|
$PYTHONSTARTUP doesn't work with -i |
[sorry, html mail was bad idea] On Tue, Sep 6, 2011 at 17:54, Antoine Pitrou <report@bugs.python.org> wrote: Éric Araujo <merwok@netwok.org> added the comment:
Good point!
Perhaps it should? Point to watch out for: errors in $PYTHONSTARTUP.
python -S doesn't disable readline. What makes completions more of a "customization" than editing? |
|
FTR, I tried checking sys.ps1 instead of argv but it’s the same problem. |
In the spirit of pushing this forward, here is an updated patch using the sys.__interactivehook__ approach. I didn't add any tests since it doesn't seem very easy to write any. If nobody objects, I would like to commit this soon. |
New changeset d5ef330bac50 by Antoine Pitrou in branch 'default': |
End-users will hopefully rejoice :) |
Well, post it to python-dev and see what reaction you get :) I could be wrong, but I don't think I am (obviously). |
I expect that a lot of users use the tab key to indent in the repl (as well as in editors, smart enough editors can convert the tab presses to spaces) |
I'm not interested in python-dev reactions here. For this The commit adds an often-requested feature (to the point that many |
The interpreter prompt is not a text editor at all. You can misuse Really, everyone I've ever showed tab-completion to has always (probably because noone is crazy enough to type long chunks of |
I am in AP's camp on the tab issue, but I think we can preserve "tab inserts tab" behavior at the continuation prompt. I don't like "indent at beginning of line." I have rlcompleter enabled in Python 2.6 and i get the following when I press tab: Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> <tab>
Display all 173 possibilities? (y or n) y
ArithmeticError( abs( input(
AssertionError( all( int(
AttributeError( and intern(
BaseException( any( is
.. I find this rather useful. At the ... prompt, however, a tab (or better four spaces) is arguably the right completion at the beginning of the line. |
Take it with a large grain of Internet salt, but Ezio pointed me to the following reactions: |
Also made it to the front-page of Hacker News[1], with better quality comments than the reddit thread. |
This issue seems to have stalled. But it's still marked as a release blocker, which means I can't release Python 3.4a1 in two days if this issue is still open. One of three things will happen:
IMO the optimal solution is that tab preceded by only whitespace indents, and tab preceded by any non-whitespace character attempts to complete. Can we goad readline into behaving this way? |
On 31/07/13 17:14, Larry Hastings wrote:
Yes we can. Attached are a pair of lightweight modules I've used for this for the last 3-4 years, and it works fine in Python 2.4 through 3.3. I put something like this in my startup.py: import completer
import history
history = history.History()
completer = completer.Completer(
bindings=(r'"\C-xo": overwrite-mode',
r'"\C-xd": dump-functions',
)) Originally they were in a single class, but I was persuaded that it was cleaner to separate them. |
If R. David agrees (assuming no patch is coming forward), it could be degrated to deferred-blocker, and the alpha phases could be used to see how annoyed people are with "tab not working". |
Yeah, deferred blocker is fine with me. |
Do you know that the readline module first force rebind of TAB to insert-tab? |
There is a backward compatibility issue with changeset d5ef330bac50 When a user is not aware of this new feature and has been implementing http://docs.python.org/3/library/readline.html?highlight=readline#example then each new interactive python 3.4 session reads (appending them) |
Interesting. A way to avoid doing this would be to only load history if |
Attaching patch to only load history file if no history exists. |
New changeset 687dd81cee3b by Antoine Pitrou in branch 'default': |
The patch fixes the problem on my setup. A very minor glitch: after |
Yes, the only way to know if a history file has already been loaded with |
What is the status of this issue? |
I would say it's now closed. If there's some fine tuning needed, separate issues should be opened. |
Added bpo-20411, revealed in 3.4b2. |
New changeset 69c451851c71 by R David Murray in branch 'default': |
Funny thing, this feature breaks the interactive interpreter in the most basic way on OS X systems. For example, the tab key won't even work to indent. You can't even type the most basic programs into the interactive interpreter. For example: >>> for i in range(10):
... print(i) Oh sure, you can make it work by typing the space bar a bunch of times, but it's extremely annoying. The only way I was able to get a working interactive interpreter on my machine was to manually edit site.py and remove the call to enablerlcompleter() from main(). I hope someone reconsiders this feature and removes it as default behavior. |
This issue should have gone back to being a release blocker after the alpha release to fix the tab-as-indent issue, but obviously that didn't happen (I forgot about it myself). Please open a new issue requesting a fix for this bug (that tab doesn't work as indent at the ... prompt), referencing the discussion in this issue. |
Here is the new issue (putting the link here for reference): http://bugs.python.org/issue22086 |
The active issue for this is now 23441. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: