[OS X 10.5] hitting TAB inserts ./ in interactive mode ?

N

Nik Krumm

Hi all,

Thanks for your help. I installed python 2.7 on my Mac OS X 10.5.8
machine:


nik$ python
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

and now, when I hit TAB or paste in a code snippet with tabs in them,
the interpreter tries to autocomplete a path, or inserts "./" if the
line is blank (just as readline would in the shell environment). It
does *not* try to autocomplete function (as readline would in python--
importing readline does turn on this functionality). And it does *not*
insert a tab, as I would like it to!

If i start my old python 2.5 which came with the OS, this is not a
problem.

I've tried setting a PYTHONIOENCODING, but that doesn't seem to be
doing the job.

Any ideas? Thanks in advance.
~Nik
 
N

Ned Deily

Thanks for your help. I installed python 2.7 on my Mac OS X 10.5.8
machine:


nik$ python
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

and now, when I hit TAB or paste in a code snippet with tabs in them,
the interpreter tries to autocomplete a path, or inserts "./" if the
line is blank (just as readline would in the shell environment). It
does *not* try to autocomplete function (as readline would in python--
importing readline does turn on this functionality). And it does *not*
insert a tab, as I would like it to!

See the rlcompleter module in the standard library:
http://docs.python.org/library/rlcompleter.html

In my .bashrc file, I have a line:
[ -f ~/.pythonrc ] && export PYTHONSTARTUP=~/.pythonrc

and in the .pythonrc file, I include:

try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")

Note the print() form which works with either Python 2 or 3.
 
L

Lawrence D'Oliveiro

Ned Deily said:
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")

Note the print() form which works with either Python 2 or 3.

You should be writing diagnostics to stderr, not stdout.
 
N

Ned Deily

Lawrence D'Oliveiro said:
You should be writing diagnostics to stderr, not stdout.

In general, sure. Statements in a PYTHONSTARTUP file, like here, are
only executed in interactive mode and it isn't likely that someone is
going to be redirecting stdout or stderr; that would kind of defeat the
purpose of readline completion functions which is what this is all
about. But, if you feel strongly about it, I'm sure a contributed patch
to improve the rlcompleter documentation would be welcome.
 
N

Nik Krumm

In general, sure.   Statements in a PYTHONSTARTUP file, like here, are
only executed in interactive mode and it isn't likely that someone is
going to be redirecting stdout or stderr; that would kind of defeat the
purpose ofreadlinecompletion functions which is what this is all
about.  But, if you feel strongly about it, I'm sure a contributed patch
to improve the rlcompleter documentation would be welcome.

Hi, Thanks for the replies.

The issue isn't with readline. The readline module or rlcompleter
module are both available, and loading them has no effect on the
behavior of tab:

import readline [Now i hit tab...]
./
File "<stdin>", line 1
./
^
SyntaxError: invalid syntax

[Hit tab twice...]../.bash_history ./.bash_profile ./.bash_profile.pysave ./.CFUserTextEncoding ./.cups ./.dropbox
../.DS_Store ./.fontconfig ./.ipython ./.lesshst ./.ssh ./.subversion
../.Trash ./.Xauthority ./
Desktop ./Documents ./
Downloads ./inputrc
../Library ./Movies ./
Music ./Pictures ./
Public ./python
../Sites
./
import rlcompleter
pri[TAB]--> completes
if testvar:
.... [one TAB: nothing], [two TABs: "Display all 179 possibilities y/
n?"] (Note: here i need it to insert a tab, just as it should!)

So this ostensibly makes it very hard to write or paste any code into
the command line!

any further ideas? Thanks again
~N
 
N

Ned Deily

The issue isn't with readline. The readline module or rlcompleter
module are both available, and loading them has no effect on the
behavior of tab:
import readline [Now i hit tab...]
./
File "<stdin>", line 1
./
^
SyntaxError: invalid syntax

[Hit tab twice...]./.bash_history ./.bash_profile ./.bash_profile.pysave [...]
So this ostensibly makes it very hard to write or paste any code into
the command line!

Ah, thanks, I get it now. It turns out this is a bug seen for the
first time with the new-style (32-/64-, 10.5+) python.org 2.7 installer
build because it links with the Apple-supplied editline library rather
than the GNU readline library as in other installers. I've opened an
issue for this:

http://bugs.python.org/issue9907

As noted there, two workarounds come to mind. Either switch to using
the old-style (32-only, 10.3+) 2.7 installer; or, add or modify a
PYTHONSTARTUP file to force the desired TAB behavior, so something like
this:

$ cat > $HOME/.pystartup
import readline
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I ed-insert")
^D
$ export PYTHONSTARTUP=$HOME/.pystartup

Thanks for reporting this!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top