annotate Lib/rlcompleter.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 39d1c2c7bcf7
children d234dd21374a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
1 """Word completion for GNU readline.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
3 The completer completes keywords, built-ins and globals in a selectable
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
4 namespace (which defaults to __main__); when completing NAME.NAME..., it
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
5 evaluates (!) the expression up to the last dot and completes its attributes.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
7 It's very cool to do "import sys" type "sys.", hit the completion key (twice),
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
8 and see the list of names defined by the sys module!
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 Tip: to use the tab key as the completion key, call
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 readline.parse_and_bind("tab: complete")
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 Notes:
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
16 - Exceptions raised by the completer function are *ignored* (and generally cause
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
17 the completion to fail). This is a feature -- since readline sets the tty
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
18 device in raw (or cbreak) mode, printing a traceback wouldn't work well
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
19 without some complicated hoopla to save, reset and restore the tty state.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
21 - The evaluation of the NAME.NAME... form may cause arbitrary application
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
22 defined code to be executed if an object with a __getattr__ hook is found.
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
23 Since it is the responsibility of the application (or the user) to enable this
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
24 feature, I consider this an acceptable risk. More complicated expressions
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
25 (e.g. function calls or indexing operations) are *not* evaluated.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 - GNU readline is also used by the built-in functions input() and
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 raw_input(), and thus these also benefit/suffer from the completer
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 features. Clearly an interactive application can benefit by
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 specifying its own completer function and using raw_input() for all
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 its input.
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 - When the original stdin is not a tty device, GNU readline is never
75967
39d1c2c7bcf7 Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents: 48122
diff changeset
34 used, and this module (and the readline module) are silently inactive.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 """
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 import __builtin__
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 import __main__
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
41 __all__ = ["Completer"]
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
42
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 class Completer:
22485
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
44 def __init__(self, namespace = None):
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
45 """Create a new completer for the command line.
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
46
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
47 Completer([namespace]) -> completer instance.
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
48
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
49 If unspecified, the default namespace where completions are performed
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
50 is __main__ (technically, __main__.__dict__). Namespaces should be
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
51 given as dictionaries.
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
52
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
53 Completer instances should be used as the completion mechanism of
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
54 readline via the set_completer() call:
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
55
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
56 readline.set_completer(Completer(my_namespace).complete)
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
57 """
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22485
diff changeset
58
22485
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
59 if namespace and not isinstance(namespace, dict):
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
60 raise TypeError,'namespace must be a dictionary'
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
61
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
62 # Don't bind to namespace quite yet, but flag whether the user wants a
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
63 # specific namespace or to use __main__.__dict__. This will allow us
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
64 # to bind to __main__.__dict__ at completion time, not now.
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
65 if namespace is None:
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
66 self.use_main_ns = 1
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
67 else:
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
68 self.use_main_ns = 0
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
69 self.namespace = namespace
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 def complete(self, text, state):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
72 """Return the next possible completion for 'text'.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
74 This is called successively with state == 0, 1, 2, ... until it
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
75 returns None. The completion should begin with 'text'.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
77 """
22485
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
78 if self.use_main_ns:
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
79 self.namespace = __main__.__dict__
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22485
diff changeset
80
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
81 if state == 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
82 if "." in text:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
83 self.matches = self.attr_matches(text)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
84 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
85 self.matches = self.global_matches(text)
8498
69c9bbb69aab In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
86 try:
69c9bbb69aab In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
87 return self.matches[state]
69c9bbb69aab In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
88 except IndexError:
69c9bbb69aab In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
89 return None
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
47875
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
91 def _callable_postfix(self, val, word):
48122
5d2cd96a196b Remove a use of callable() to silence the warning triggered under -3.
Brett Cannon <bcannon@gmail.com>
parents: 48040
diff changeset
92 if hasattr(val, '__call__'):
47875
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
93 word = word + "("
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
94 return word
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
95
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def global_matches(self, text):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
97 """Compute matches when text is a simple name.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
22485
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
99 Return a list of all keywords, built-in functions and names currently
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
100 defined in self.namespace that match.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
102 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
103 import keyword
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
104 matches = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
105 n = len(text)
47875
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
106 for word in keyword.kwlist:
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
107 if word[:n] == text:
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
108 matches.append(word)
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
109 for nspace in [__builtin__.__dict__, self.namespace]:
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
110 for word, val in nspace.items():
12806
583def8cedf4 Do not expose __builtins__ name as a completion; this is an implementation
Fred Drake <fdrake@acm.org>
parents: 11448
diff changeset
111 if word[:n] == text and word != "__builtins__":
47875
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
112 matches.append(self._callable_postfix(val, word))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
113 return matches
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 def attr_matches(self, text):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
116 """Compute matches when text contains a dot.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
118 Assuming the text is of the form NAME.NAME....[NAME], and is
22485
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
119 evaluatable in self.namespace, it will be evaluated and its attributes
b360a650a0db Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16613
diff changeset
120 (as revealed by dir()) are used as possible completions. (For class
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 23645
diff changeset
121 instances, class members are also considered.)
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
123 WARNING: this can still invoke arbitrary C code, if an object
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
124 with a __getattr__ hook is evaluated.
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
126 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
127 import re
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
128 m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
129 if not m:
45691
e9be6aeb4df0 #2220: handle matching failure more gracefully.
Georg Brandl <georg@python.org>
parents: 38058
diff changeset
130 return []
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
131 expr, attr = m.group(1, 3)
46910
741b2667ac0b - #2250: Exceptions raised during evaluation of names in rlcompleter's
Georg Brandl <georg@python.org>
parents: 45691
diff changeset
132 try:
48040
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
133 thisobject = eval(expr, self.namespace)
46910
741b2667ac0b - #2250: Exceptions raised during evaluation of names in rlcompleter's
Georg Brandl <georg@python.org>
parents: 45691
diff changeset
134 except Exception:
741b2667ac0b - #2250: Exceptions raised during evaluation of names in rlcompleter's
Georg Brandl <georg@python.org>
parents: 45691
diff changeset
135 return []
48040
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
136
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
137 # get the content of the object, except __builtins__
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
138 words = dir(thisobject)
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
139 if "__builtins__" in words:
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
140 words.remove("__builtins__")
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
141
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
142 if hasattr(thisobject, '__class__'):
11410
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
143 words.append('__class__')
48040
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
144 words.extend(get_class_members(thisobject.__class__))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
145 matches = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
146 n = len(attr)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
147 for word in words:
48040
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
148 if word[:n] == attr and hasattr(thisobject, word):
068c00bf5a24 Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents: 47875
diff changeset
149 val = getattr(thisobject, word)
47875
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
150 word = self._callable_postfix(val, "%s.%s" % (expr, word))
e4bbb1d3dd14 Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents: 46910
diff changeset
151 matches.append(word)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6516
diff changeset
152 return matches
6336
a42b31fe976f Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
11410
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
154 def get_class_members(klass):
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
155 ret = dir(klass)
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
156 if hasattr(klass,'__bases__'):
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
157 for base in klass.__bases__:
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
158 ret = ret + get_class_members(base)
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
159 return ret
0606682c7ebc Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents: 8498
diff changeset
160
38058
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
161 try:
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
162 import readline
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
163 except ImportError:
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
164 pass
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
165 else:
9abeab96af57 Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents: 30723
diff changeset
166 readline.set_completer(Completer().complete)