Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate output on Python tab completion (OsX 10.8)

Using the snippet below, I have added tab completion to the python interpreter.

import readline
import rlcompleter
if 'libedit' in readline.__doc__:
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")

However, I have encountered a weird behaviour where after hitting TAB, the interpreter would give duplicates as such:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d=dict()
>>> d.
Display all 108 possibilities? (y or n)
d.__class__(         d.__class__(         d.__class__(
d.__class__(         d.__cmp__(           d.__cmp__(
d.__contains__(      d.__contains__(      d.__delattr__(
d.__delattr__(       d.__delattr__(       d.__delitem__(
d.__delitem__(       d.__doc__            d.__doc__
d.__doc__            d.__eq__(            d.__eq__(
d.__format__(        d.__format__(        d.__format__(
d.__ge__(            d.__ge__(            d.__getattribute__(
d.__getattribute__(  d.__getattribute__(  d.__getitem__(
d.__getitem__(       d.__gt__(            d.__gt__(
d.__hash__           d.__hash__           d.__hash__
d.__init__(          d.__init__(          d.__init__(
d.__iter__(          d.__iter__(          d.__le__(
d.__le__(            d.__len__(           d.__len__(
d.__lt__(            d.__lt__(            d.__ne__(
d.__ne__(            d.__new__(           d.__new__(
d.__new__(           d.__reduce__(        d.__reduce__(
d.__reduce__(        d.__reduce_ex__(     d.__reduce_ex__(
d.__reduce_ex__(     d.__repr__(          d.__repr__(
d.__repr__(          d.__setattr__(       d.__setattr__(
d.__setattr__(       d.__setitem__(       d.__setitem__(
d.__sizeof__(        d.__sizeof__(        d.__sizeof__(
d.__str__(           d.__str__(           d.__str__(
d.__subclasshook__(  d.__subclasshook__(  d.__subclasshook__(
d.clear(             d.clear(             d.copy(
d.copy(              d.fromkeys(          d.fromkeys(
d.get(               d.get(               d.has_key(
d.has_key(           d.items(             d.items(
d.iteritems(         d.iteritems(         d.iterkeys(
d.iterkeys(          d.itervalues(        d.itervalues(
d.keys(              d.keys(              d.pop(
d.pop(               d.popitem(           d.popitem(
d.setdefault(        d.setdefault(        d.update(
d.update(            d.values(            d.values(
d.viewitems(         d.viewitems(         d.viewkeys(
d.viewkeys(          d.viewvalues(        d.viewvalues(

108 possibilities! min 2, max 4 duplicates.

Furthermore,

  • if I subclass dict the total number of possibilities increases to 160 (and then to 209, 258).
  • if I type enough characters for TAB to nail the command, it does so without displaying duplicates.

Any pointers or debugging tips are appreciated.

like image 282
Eren Güven Avatar asked Mar 21 '13 09:03

Eren Güven


1 Answers

I have solved this issue by adding a custom completer. It now works as expected.

import readline
import rlcompleter

c = rlcompleter.Completer()

def complete(text, state):
    buffer = readline.get_line_buffer()
    first = c.complete(buffer, 0)
    if state == 0:
        return first
    current = c.complete(buffer, state)
    if current != first:
        return current

readline.parse_and_bind('bind ^I rl_complete')
readline.set_completer(complete)
like image 192
Eren Güven Avatar answered Oct 19 '22 01:10

Eren Güven