This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rheise
Recipients rheise
Date 2010-12-30.13:08:06
SpamBayes Score 1.8152146e-14
Marked as misclassified No
Message-id <1293714489.48.0.717157988045.issue10796@psf.upfronthosting.co.za>
In-reply-to
Content
Python's readline library generates out of the choices provided by a custom completion function the wrong terminal input. Say, the completion function suggests 'foobar' and 'foobaz' as matching completion strings, readline should produce the characters 'fooba' to prompt and await further interaction by the user. 
This is the intended behaviour of readline and this works in Python as well. However this does nod work anymore as soon, as the suggestion 
list contains dashes `-' as input argument. 

A working as supposed example:

>>> import readline
>>> 
>>> def complete(text, state):
...   if (state == 0):
...     return "abc"
...   if (state == 1):
...     return "ade"
...   else:
...     return None
... 
>>> 
>>> readline.parse_and_bind("Tab: complete")
>>> readline.set_completer(complete)
>>> 
>>> raw_input()
a
abc  ade  
a
'a'
>>> 

remark: I entered a and hit tab. readline produces abc/ade as valid choices, stopping at the first ambiguous character. Now consider the following example:

>>> import readline
>>> 
>>> def complete(text, state):
...   if (state == 0):
...     return "a-bc"
...   if (state == 1):
...     return "a-de"
...   else:
...     return None
... 
>>> 
>>> readline.parse_and_bind("Tab: complete")
>>> readline.set_completer(complete)
>>> 
>>> raw_input()
a-a-a-
'a-a-a-'
>>> 

The intended behaviour is the very same as for the first example. Readline should produce 'a-' and offer a-bc and a-de as valid choices. Instead it produces an additional a- for every time I hit tab. 

Same for Python3.1:

$ python3.1
Python 3.1.2 (release31-maint, Sep 26 2010, 13:51:01) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
>>> 
>>> def complete(text, state):
...   if (state == 0):
...     return "a-bc"
...   if (state == 1):
...     return "a-de"
...   else:
...     return None
... 
>>> 
>>> readline.parse_and_bind("Tab: complete")
>>> readline.set_completer(complete)
>>> 
>>> input()
a-a-a-a-a-
'a-a-a-a-a-'
>>> 


Other programming languages falling back to the GNU C-readline library don't have this problem. Consider the roughly equivalent example in Perl which works as expected:

use Term::ReadLine;

sub complete
{
        my ($text, $state) = @_;
        if ($state == 0)
        {
                return "a-bc";
        }
        elsif ($state == 1)
        {
                return "a-cd";
        }
        else
        {
                return undef;
        }
}

my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;

$term->parse_and_bind("Tab: complete");
$attribs->{completion_entry_function} = \&complete;

$term->readline();



Running it:

$ perl rl 
a-
a-bc  a-cd  
a-
History
Date User Action Args
2010-12-30 13:08:09rheisesetrecipients: + rheise
2010-12-30 13:08:09rheisesetmessageid: <1293714489.48.0.717157988045.issue10796@psf.upfronthosting.co.za>
2010-12-30 13:08:07rheiselinkissue10796 messages
2010-12-30 13:08:06rheisecreate