Mercurial > cpython-lru_cache-weakref
annotate Lib/rlcompleter.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 37751d1cb4a8 |
| children | e07f104133d5 8d28d44f3a9a |
| rev | line source |
|---|---|
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
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 |
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
3 The completer completes keywords, built-ins and globals in a selectable |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
4 namespace (which defaults to __main__); when completing NAME.NAME..., it |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
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 |
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
7 It's very cool to do "import sys" type "sys.", hit the completion key (twice), |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
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 |
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
16 - Exceptions raised by the completer function are *ignored* (and generally cause |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
17 the completion to fail). This is a feature -- since readline sets the tty |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
18 device in raw (or cbreak) mode, printing a traceback wouldn't work well |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
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 |
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
21 - The evaluation of the NAME.NAME... form may cause arbitrary application |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
22 defined code to be executed if an object with a __getattr__ hook is found. |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
23 Since it is the responsibility of the application (or the user) to enable this |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
24 feature, I consider this an acceptable risk. More complicated expressions |
|
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
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 - When the original stdin is not a tty device, GNU readline is never |
|
75965
37751d1cb4a8
Closes #14411: remove outdated comment in rlcompleter docstring.
Georg Brandl <georg@python.org>
parents:
73165
diff
changeset
|
28 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
|
29 |
|
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 |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43020
diff
changeset
|
32 import builtins |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 import __main__ |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
| 16613 | 35 __all__ = ["Completer"] |
| 36 | |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 class Completer: |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
38 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
|
39 """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
|
40 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
41 Completer([namespace]) -> completer instance. |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
42 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
43 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
|
44 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
|
45 given as dictionaries. |
|
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 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
|
48 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
|
49 |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
50 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
|
51 """ |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22485
diff
changeset
|
52 |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
53 if namespace and not isinstance(namespace, dict): |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
38453
diff
changeset
|
54 raise TypeError('namespace must be a dictionary') |
|
22485
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 # 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
|
57 # 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
|
58 # 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
|
59 if namespace is None: |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
60 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
|
61 else: |
|
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
62 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
|
63 self.namespace = namespace |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 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
|
66 """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
|
67 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
68 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
|
69 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
|
70 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
71 """ |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
72 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
|
73 self.namespace = __main__.__dict__ |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22485
diff
changeset
|
74 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
75 if state == 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
76 if "." in text: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
77 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
|
78 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
79 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
|
80 try: |
|
69c9bbb69aab
In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
81 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
|
82 except IndexError: |
|
69c9bbb69aab
In completer(), return None instead of raising an IndexError when
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
83 return None |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
48412
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
85 def _callable_postfix(self, val, word): |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
48550
diff
changeset
|
86 if callable(val): |
|
48412
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
87 word = word + "(" |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
88 return word |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
89 |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 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
|
91 """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
|
92 |
|
22485
b360a650a0db
Add namespace selection for rlcompleter. Closes SF patch 490026.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16613
diff
changeset
|
93 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
|
94 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
|
95 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
96 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
97 import keyword |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
98 matches = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
99 n = len(text) |
|
48412
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
100 for word in keyword.kwlist: |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
101 if word[:n] == text: |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
102 matches.append(word) |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
103 for nspace in [builtins.__dict__, self.namespace]: |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
104 for word, val in nspace.items(): |
|
48434
4ba3d07b871f
fix regression from merge error in rlcompletor
Benjamin Peterson <benjamin@python.org>
parents:
48412
diff
changeset
|
105 if word[:n] == text and word != "__builtins__": |
|
48412
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
106 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
|
107 return matches |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 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
|
110 """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
|
111 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
112 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
|
113 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
|
114 (as revealed by dir()) are used as possible completions. (For class |
| 30723 | 115 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
|
116 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
117 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
|
118 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
|
119 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
120 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
121 import re |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
122 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
|
123 if not m: |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44098
diff
changeset
|
124 return [] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
125 expr, attr = m.group(1, 3) |
|
47072
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45773
diff
changeset
|
126 try: |
|
48550
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
127 thisobject = eval(expr, self.namespace) |
|
47072
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45773
diff
changeset
|
128 except Exception: |
|
0c7142c56d7e
Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45773
diff
changeset
|
129 return [] |
|
48550
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
130 |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
131 # get the content of the object, except __builtins__ |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
132 words = dir(thisobject) |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
133 if "__builtins__" in words: |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
134 words.remove("__builtins__") |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
135 |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
136 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
|
137 words.append('__class__') |
|
48550
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
138 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
|
139 matches = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
140 n = len(attr) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6516
diff
changeset
|
141 for word in words: |
|
48550
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
142 if word[:n] == attr and hasattr(thisobject, word): |
|
0a741c13fc81
Merged revisions 65168 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48435
diff
changeset
|
143 val = getattr(thisobject, word) |
|
48412
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
144 word = self._callable_postfix(val, "%s.%s" % (expr, word)) |
|
5f4a8980c1ac
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47072
diff
changeset
|
145 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
|
146 return matches |
|
6336
a42b31fe976f
Word completion for the new readline.set_completer() function.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
11410
0606682c7ebc
Patch by Michael Hudson: when the object of attribute expansion is a
Guido van Rossum <guido@python.org>
parents:
8498
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
155 try: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
156 import readline |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
157 except ImportError: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
158 pass |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
159 else: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
160 readline.set_completer(Completer().complete) |
