summaryrefslogtreecommitdiffstats
path: root/Lib/rlcompleter.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-10-26 13:09:08 (GMT)
committerGuido van Rossum <guido@python.org>1999-10-26 13:09:08 (GMT)
commit4e20de59dd5a9058c0d99c10d773b9d0bcbbe07d (patch)
tree4ae40a869cbafb74125693b24dec331e119f2bb8 /Lib/rlcompleter.py
parentd65b53923ecefc96f24d9cbc4e3e83dd2194d6b4 (diff)
downloadcpython-4e20de59dd5a9058c0d99c10d773b9d0bcbbe07d.zip
cpython-4e20de59dd5a9058c0d99c10d773b9d0bcbbe07d.tar.gz
cpython-4e20de59dd5a9058c0d99c10d773b9d0bcbbe07d.tar.bz2
Patch by Michael Hudson: when the object of attribute expansion is a
class instance, include the class attributes in the list of possible expansions.
Diffstat (limited to 'Lib/rlcompleter.py')
-rw-r--r--Lib/rlcompleter.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py
index 92633ab..96abad0 100644
--- a/Lib/rlcompleter.py
+++ b/Lib/rlcompleter.py
@@ -1,13 +1,24 @@
"""Word completion for GNU readline 2.0.
This requires the latest extension to the readline module (the
-set_completer() function). When completing a simple identifier, it
+ object=eval(expr, __main__.__dict__)
+ words = dir(object)
+ if hasattr(object,'__class__'):
+ words.append('__class__')
+ words=words+get_class_members(object.__class__)
completes keywords, built-ins and globals in __main__; when completing
NAME.NAME..., it evaluates (!) the expression up to the last dot and
completes its attributes.
It's very cool to do "import string" type "string.", hit the
completion key (twice), and see the list of names defined by the
+
+def get_class_members(klass):
+ ret=dir(klass)
+ if hasattr(klass,'__bases__'):
+ for base in klass.__bases__:
+ ret=ret + get_class_members(base)
+ return ret
string module!
Tip: to use the tab key as the completion key, call
@@ -87,7 +98,8 @@ class Completer:
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluabable in the globals of __main__, it will be evaluated
and its attributes (as revealed by dir()) are used as possible
- completions.
+ completions. (For class instances, class members are are also
+ considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
@@ -98,7 +110,11 @@ class Completer:
if not m:
return
expr, attr = m.group(1, 3)
- words = dir(eval(expr, __main__.__dict__))
+ object = eval(expr, __main__.__dict__)
+ words = dir(object)
+ if hasattr(object,'__class__'):
+ words.append('__class__')
+ words = words + get_class_members(object.__class__)
matches = []
n = len(attr)
for word in words:
@@ -106,4 +122,11 @@ class Completer:
matches.append("%s.%s" % (expr, word))
return matches
+def get_class_members(klass):
+ ret = dir(klass)
+ if hasattr(klass,'__bases__'):
+ for base in klass.__bases__:
+ ret = ret + get_class_members(base)
+ return ret
+
readline.set_completer(Completer().complete)