[Python-bugs-list] [Bug #110689] PRIVATE: Extra space bug in readline/rlcompleter/raw_input? (PR#45)
noreply@sourceforge.net
noreply@sourceforge.net
Sun, 6 Aug 2000 13:34:28 -0700
Bug #110689, was updated on 2000-Jul-31 14:15
Here is a current snapshot of the bug.
Project: Python
Category: Modules
Status: Closed
Resolution: Wont Fix
Bug Group: Not a Bug
Priority: 5
Summary: PRIVATE: Extra space bug in readline/rlcompleter/raw_input? (PR#45)
Details: Jitterbug-Id: 45
Submitted-By: skip@mojam.com
Date: Wed, 4 Aug 1999 11:55:04 -0400 (EDT)
Version: 1.5.2
OS: Linux
I just tried using the readline/rlcompleter stuff in my own
program for the first time. I noticed that when using the
completion key to complete an input from a set of choices it
tacks on an extra space to the end of the string. This seems
like a bug to me. There's no way for the program to know if the
enter typed the space explicitly or if it was appended by
raw_input(). Is this perhaps a readline bug? I haven't delved
into the source code at this point.
Here's a script that demonstrates the problem. Try running it
three times, once entering
s TAB RET
once entering
spam RET
and once entering
spam SPC RET
You will see that the first and third cases are
indistinguishable.
#!/usr/bin/env python
import string, readline, rlcompleter, sys
class Completer:
def __init__(self):
self.list = []
def complete(self, text, state):
if state == 0:
self.matches = self.get_matches(text)
try:
return self.matches[state]
except IndexError:
return None
def set_choices(self, list):
self.list = list
def get_matches(self, text):
matches = []
for elt in self.list:
if string.find(elt, text) == 0:
matches.append(elt)
return matches
completer = Completer()
def select_from_list(list):
completer.set_choices(list)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
sys.stderr.write("Select from [%s]: " % string.join(list, "|"))
result = raw_input()
return result
result = select_from_list(["spam", "ham", "eggs", "juice"])
print `result`, ord(result[-1])
====================================================================
Audit trail:
Wed Aug 04 12:12:40 1999 guido moved from incoming to open
Follow-Ups:
Date: 2000-Aug-01 14:02
By: none
Comment:
From: Guido van Rossum <guido@CNRI.Reston.VA.US>
Subject: Re: [Python-bugs-list] PRIVATE: Extra space bug in readline/rlcompleter/raw_input? (PR#45)
Date: Wed, 04 Aug 1999 12:09:13 -0400
> I just tried using the readline/rlcompleter stuff in my own
> program for the first time. I noticed that when using the
> completion key to complete an input from a set of choices it
> tacks on an extra space to the end of the string. This seems
> like a bug to me. There's no way for the program to know if the
> enter typed the space explicitly or if it was appended by
> raw_input(). Is this perhaps a readline bug? I haven't delved
> into the source code at this point.
As far as I know this is a problem with the completion strategy
hardcoded in the GNU readline code. I don't know of a fix.
--Guido van Rossum (home page: http://www.python.org/~guido/)
-------------------------------------------------------
Date: 2000-Aug-06 13:34
By: twouters
Comment:
I agree with Guido: the extra space is added by Readline, for the simple purpose of distinguishing between a successful, unambiguous completion, and a half-finished completion that stopped at the first 'junction'. In other words, in the 'namespace' containing 'eggs', 'spamandham' and 'spamandeggs',
eg<TAB>
will complete to 'eggs<SPACE>' because it's a finished completion, but
spa<TAB>
will complete to 'spamand', (without a space), so you know you have more possible completions. If you disambiguate by continuing it with 'h', and then press TAB again, it gets expanded into 'spamandham<SPACE>'.
One possible solution is to never return a single possibility, but always a dummy possibility as well. Not very pretty, and not very nice if you use TABTAB (list all possible completions) but it should work.
The other solutions is to provide a replacement for readline :-)
-------------------------------------------------------
For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=110689&group_id=5470