[issue5062] Rlcompleter.Completer does not use __dir__ magic method

Gabriel Genellina report at bugs.python.org
Tue Jan 27 04:58:51 CET 2009


Gabriel Genellina <gagsl-py2 at yahoo.com.ar> added the comment:

This is not a bug in rlcompleter; __dir__ is returning bogus items, and 
rlcompleter checks whether there exist actually an attribute with such 
name.

Defining __getattr__ (or __getattribute__) and a matching __dir__ works 
fine:

>>> class B(object):
...   def __dir__(self):
...     return dir(object) + ["xa","xb","xc"]
...   def __getattr__(self, name):
...     if name in ["xa","xb","xc"]:
...       return None
...     raise AttributeError, name
...
>>> b = B()
>>> import rlcompleter
>>> c = rlcompleter.Completer()
>>> c.complete("b.", 0)
'b.__class__('
>>> c.matches
['b.__class__(', 'b.__delattr__(', 'b.__doc__', 'b.__format__(', 
'b.__getattribute__(', 
...
'b.xa', 'b.xb', 'b.xc', 'b.__class__(', 'b.__class__(', 
...]
>>> c.complete("b.x", 0)
'b.xa'
>>> c.matches
['b.xa', 'b.xb', 'b.xc']

Now, looking at this I saw there *is* a bug in rlcompleter, as it may 
return many duplicate items:

>>> c.complete("b.__c", 0)
'b.__class__('
>>> c.matches
['b.__class__(', 'b.__class__(', 'b.__class__(', 'b.__class__(']

The attached patch fixes that.

----------
keywords: +patch
nosy: +gagenellina
Added file: http://bugs.python.org/file12872/rlcompleter.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5062>
_______________________________________


More information about the Python-bugs-list mailing list