Mercurial > cpython-lru_cache-weakref
annotate Lib/rlcompleter.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 5d2cd96a196b |
| children | 39d1c2c7bcf7 |
| rev | line source |
|---|---|
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Word completion for GNU readline 2.0. |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
3 This requires the latest extension to the readline module. The completer |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
4 completes keywords, built-ins and globals in a selectable namespace (which |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
5 defaults to __main__); when completing NAME.NAME..., it evaluates (!) the |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
6 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
|
7 |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23634
diff
changeset
|
8 It's very cool to do "import sys" type "sys.", hit the |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 completion key (twice), and see the list of names defined by the |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23634
diff
changeset
|
10 sys module! |
|
6336
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 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
|
13 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 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
|
15 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 Notes: |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 - Exceptions raised by the completer function are *ignored* (and |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 generally cause the completion to fail). This is a feature -- since |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 readline sets the tty device in raw (or cbreak) mode, printing a |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 traceback wouldn't work well without some complicated hoopla to save, |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 reset and restore the tty state. |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 - The evaluation of the NAME.NAME... form may cause arbitrary |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 application defined code to be executed if an object with a |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 __getattr__ hook is found. Since it is the responsibility of the |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 application (or the user) to enable this feature, I consider this an |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 acceptable risk. More complicated expressions (e.g. function calls or |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 indexing operations) are *not* evaluated. |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 - 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
|
32 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
|
33 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
|
34 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
|
35 its input. |
|
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 - When the original stdin is not a tty device, GNU readline is never |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 used, and this module (and the readline module) are silently inactive. |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 """ |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 import __builtin__ |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 import __main__ |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
| 16613 | 45 __all__ = ["Completer"] |
| 46 | |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 class Completer: |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
48 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
|
49 """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
|
50 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
51 Completer([namespace]) -> completer instance. |
|
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 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
|
54 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
|
55 given as dictionaries. |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
56 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
57 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
|
58 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
|
59 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
60 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
|
61 """ |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22485
diff
changeset
|
62 |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
63 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
|
64 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
|
65 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
66 # 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
|
67 # 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
|
68 # 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
|
69 if namespace is None: |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
70 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
|
71 else: |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
72 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
|
73 self.namespace = namespace |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 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
|
76 """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
|
77 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
78 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
|
79 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
|
80 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
81 """ |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
82 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
|
83 self.namespace = __main__.__dict__ |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22485
diff
changeset
|
84 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
85 if state == 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
86 if "." in text: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
87 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
|
88 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
89 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
|
90 try: |
|
69c9bbb69aab
In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
91 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
|
92 except IndexError: |
|
69c9bbb69aab
In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
93 return None |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
47875
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
95 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
|
96 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
|
97 word = word + "(" |
|
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
98 return word |
|
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
99 |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 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
|
101 """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
|
102 |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
103 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
|
104 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
|
105 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
106 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
107 import keyword |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
108 matches = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
109 n = len(text) |
|
47875
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
110 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
|
111 if word[:n] == text: |
|
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
112 matches.append(word) |
|
e4bbb1d3dd14
Issue #449227: Now with the rlcompleter module, callable objects are
Facundo Batista <facundobatista@gmail.com>
parents:
46910
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 return matches |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 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
|
120 """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
|
121 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
122 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
|
123 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
|
124 (as revealed by dir()) are used as possible completions. (For class |
| 30723 | 125 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
|
126 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
127 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
|
128 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
|
129 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
130 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
131 import re |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
132 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
|
133 if not m: |
|
45691
e9be6aeb4df0
#2220: handle matching failure more gracefully.
Georg Brandl <georg@python.org>
parents:
38058
diff
changeset
|
134 return [] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
135 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
|
136 try: |
|
48040
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
137 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
|
138 except Exception: |
|
741b2667ac0b
- #2250: Exceptions raised during evaluation of names in rlcompleter's
Georg Brandl <georg@python.org>
parents:
45691
diff
changeset
|
139 return [] |
|
48040
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
140 |
|
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
141 # 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
|
142 words = dir(thisobject) |
|
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
143 if "__builtins__" in words: |
|
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
144 words.remove("__builtins__") |
|
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
145 |
|
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
146 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
|
147 words.append('__class__') |
|
48040
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
148 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
|
149 matches = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
150 n = len(attr) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
151 for word in words: |
|
48040
068c00bf5a24
Issue 3396. Fixed the autocompletion of 'int.', and worked
Facundo Batista <facundobatista@gmail.com>
parents:
47875
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 return matches |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
|
11410
0606682c7ebc
Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents:
8498
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 |
|
38058
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
165 try: |
|
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
166 import readline |
|
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
167 except ImportError: |
|
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
168 pass |
|
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
169 else: |
|
9abeab96af57
Patch #1472854: make the rlcompleter.Completer class usable on non-
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
170 readline.set_completer(Completer().complete) |
