[Python-bugs-list] PRIVATE: Extra space bug in readline/rlcompleter/raw_input? (PR#45)

skip@mojam.com skip@mojam.com
Wed, 4 Aug 1999 11:55:06 -0400 (EDT)


Full_Name: Skip Montanaro
Version: 1.5.2
OS: Linux
Submission from: uwire5.uwire.nunet.net (199.249.165.174)


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])