##// END OF EJS Templates
added __all__ to completer.py and added basic tests for test_dir2...
Tim Couper -
Show More
@@ -1,893 +1,915 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 Original rlcompleter documentation:
9 Original rlcompleter documentation:
10
10
11 This requires the latest extension to the readline module (the
11 This requires the latest extension to the readline module (the
12 completes keywords, built-ins and globals in __main__; when completing
12 completes keywords, built-ins and globals in __main__; when completing
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 completes its attributes.
14 completes its attributes.
15
15
16 It's very cool to do "import string" type "string.", hit the
16 It's very cool to do "import string" type "string.", hit the
17 completion key (twice), and see the list of names defined by the
17 completion key (twice), and see the list of names defined by the
18 string module!
18 string module!
19
19
20 Tip: to use the tab key as the completion key, call
20 Tip: to use the tab key as the completion key, call
21
21
22 readline.parse_and_bind("tab: complete")
22 readline.parse_and_bind("tab: complete")
23
23
24 Notes:
24 Notes:
25
25
26 - Exceptions raised by the completer function are *ignored* (and
26 - Exceptions raised by the completer function are *ignored* (and
27 generally cause the completion to fail). This is a feature -- since
27 generally cause the completion to fail). This is a feature -- since
28 readline sets the tty device in raw (or cbreak) mode, printing a
28 readline sets the tty device in raw (or cbreak) mode, printing a
29 traceback wouldn't work well without some complicated hoopla to save,
29 traceback wouldn't work well without some complicated hoopla to save,
30 reset and restore the tty state.
30 reset and restore the tty state.
31
31
32 - The evaluation of the NAME.NAME... form may cause arbitrary
32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 application defined code to be executed if an object with a
33 application defined code to be executed if an object with a
34 __getattr__ hook is found. Since it is the responsibility of the
34 __getattr__ hook is found. Since it is the responsibility of the
35 application (or the user) to enable this feature, I consider this an
35 application (or the user) to enable this feature, I consider this an
36 acceptable risk. More complicated expressions (e.g. function calls or
36 acceptable risk. More complicated expressions (e.g. function calls or
37 indexing operations) are *not* evaluated.
37 indexing operations) are *not* evaluated.
38
38
39 - GNU readline is also used by the built-in functions input() and
39 - GNU readline is also used by the built-in functions input() and
40 raw_input(), and thus these also benefit/suffer from the completer
40 raw_input(), and thus these also benefit/suffer from the completer
41 features. Clearly an interactive application can benefit by
41 features. Clearly an interactive application can benefit by
42 specifying its own completer function and using raw_input() for all
42 specifying its own completer function and using raw_input() for all
43 its input.
43 its input.
44
44
45 - When the original stdin is not a tty device, GNU readline is never
45 - When the original stdin is not a tty device, GNU readline is never
46 used, and this module (and the readline module) are silently inactive.
46 used, and this module (and the readline module) are silently inactive.
47 """
47 """
48
48
49 #*****************************************************************************
49 #*****************************************************************************
50 #
50 #
51 # Since this file is essentially a minimally modified copy of the rlcompleter
51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 # module which is part of the standard Python distribution, I assume that the
52 # module which is part of the standard Python distribution, I assume that the
53 # proper procedure is to maintain its copyright as belonging to the Python
53 # proper procedure is to maintain its copyright as belonging to the Python
54 # Software Foundation (in addition to my own, for all new code).
54 # Software Foundation (in addition to my own, for all new code).
55 #
55 #
56 # Copyright (C) 2008-2011 IPython Development Team
56 # Copyright (C) 2008-2011 IPython Development Team
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 #
59 #
60 # Distributed under the terms of the BSD License. The full license is in
60 # Distributed under the terms of the BSD License. The full license is in
61 # the file COPYING, distributed as part of this software.
61 # the file COPYING, distributed as part of this software.
62 #
62 #
63 #*****************************************************************************
63 #*****************************************************************************
64 from __future__ import print_function
64 from __future__ import print_function
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Imports
67 # Imports
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 import __builtin__
70 import __builtin__
71 import __main__
71 import __main__
72 import glob
72 import glob
73 import inspect
73 import inspect
74 import itertools
74 import itertools
75 import keyword
75 import keyword
76 import os
76 import os
77 import re
77 import re
78 import shlex
78 import shlex
79 import sys
79 import sys
80
80
81 from IPython.config.configurable import Configurable
81 from IPython.config.configurable import Configurable
82 from IPython.core.error import TryNext
82 from IPython.core.error import TryNext
83 from IPython.core.prefilter import ESC_MAGIC
83 from IPython.core.prefilter import ESC_MAGIC
84 from IPython.utils import generics
84 from IPython.utils import generics
85 from IPython.utils import io
85 from IPython.utils import io
86 from IPython.utils.dir2 import dir2
86 from IPython.utils.dir2 import dir2
87 from IPython.utils.process import arg_split
87 from IPython.utils.process import arg_split
88 from IPython.utils.traitlets import CBool, Enum
88 from IPython.utils.traitlets import CBool, Enum
89
89
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91 # Globals
91 # Globals
92 #-----------------------------------------------------------------------------
92 #-----------------------------------------------------------------------------
93
93
94 # Public API
94 # Public API
95 __all__ = ['Completer','IPCompleter']
95 __all__ = ['Completer','IPCompleter']
96
96
97 if sys.platform == 'win32':
97 if sys.platform == 'win32':
98 PROTECTABLES = ' '
98 PROTECTABLES = ' '
99 else:
99 else:
100 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
100 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
101
101
102 #-----------------------------------------------------------------------------
102 #-----------------------------------------------------------------------------
103 # Main functions and classes
103 # Main functions and classes
104 #-----------------------------------------------------------------------------
104 #-----------------------------------------------------------------------------
105
105
106 def has_open_quotes(s):
106 def has_open_quotes(s):
107 """Return whether a string has open quotes.
107 """Return whether a string has open quotes.
108
108
109 This simply counts whether the number of quote characters of either type in
109 This simply counts whether the number of quote characters of either type in
110 the string is odd.
110 the string is odd.
111
111
112 Returns
112 Returns
113 -------
113 -------
114 If there is an open quote, the quote character is returned. Else, return
114 If there is an open quote, the quote character is returned. Else, return
115 False.
115 False.
116 """
116 """
117 # We check " first, then ', so complex cases with nested quotes will get
117 # We check " first, then ', so complex cases with nested quotes will get
118 # the " to take precedence.
118 # the " to take precedence.
119 if s.count('"') % 2:
119 if s.count('"') % 2:
120 return '"'
120 return '"'
121 elif s.count("'") % 2:
121 elif s.count("'") % 2:
122 return "'"
122 return "'"
123 else:
123 else:
124 return False
124 return False
125
125
126
126
127 def protect_filename(s):
127 def protect_filename(s):
128 """Escape a string to protect certain characters."""
128 """Escape a string to protect certain characters."""
129
129
130 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
130 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
131 for ch in s])
131 for ch in s])
132
132
133 def expand_user(path):
133 def expand_user(path):
134 """Expand '~'-style usernames in strings.
134 """Expand '~'-style usernames in strings.
135
135
136 This is similar to :func:`os.path.expanduser`, but it computes and returns
136 This is similar to :func:`os.path.expanduser`, but it computes and returns
137 extra information that will be useful if the input was being used in
137 extra information that will be useful if the input was being used in
138 computing completions, and you wish to return the completions with the
138 computing completions, and you wish to return the completions with the
139 original '~' instead of its expanded value.
139 original '~' instead of its expanded value.
140
140
141 Parameters
141 Parameters
142 ----------
142 ----------
143 path : str
143 path : str
144 String to be expanded. If no ~ is present, the output is the same as the
144 String to be expanded. If no ~ is present, the output is the same as the
145 input.
145 input.
146
146
147 Returns
147 Returns
148 -------
148 -------
149 newpath : str
149 newpath : str
150 Result of ~ expansion in the input path.
150 Result of ~ expansion in the input path.
151 tilde_expand : bool
151 tilde_expand : bool
152 Whether any expansion was performed or not.
152 Whether any expansion was performed or not.
153 tilde_val : str
153 tilde_val : str
154 The value that ~ was replaced with.
154 The value that ~ was replaced with.
155 """
155 """
156 # Default values
156 # Default values
157 tilde_expand = False
157 tilde_expand = False
158 tilde_val = ''
158 tilde_val = ''
159 newpath = path
159 newpath = path
160
160
161 if path.startswith('~'):
161 if path.startswith('~'):
162 tilde_expand = True
162 tilde_expand = True
163 rest = len(path)-1
163 rest = len(path)-1
164 newpath = os.path.expanduser(path)
164 newpath = os.path.expanduser(path)
165 if rest:
165 if rest:
166 tilde_val = newpath[:-rest]
166 tilde_val = newpath[:-rest]
167 else:
167 else:
168 tilde_val = newpath
168 tilde_val = newpath
169
169
170 return newpath, tilde_expand, tilde_val
170 return newpath, tilde_expand, tilde_val
171
171
172
172
173 def compress_user(path, tilde_expand, tilde_val):
173 def compress_user(path, tilde_expand, tilde_val):
174 """Does the opposite of expand_user, with its outputs.
174 """Does the opposite of expand_user, with its outputs.
175 """
175 """
176 if tilde_expand:
176 if tilde_expand:
177 return path.replace(tilde_val, '~')
177 return path.replace(tilde_val, '~')
178 else:
178 else:
179 return path
179 return path
180
180
181 class Bunch(object): pass
181 class Bunch(object): pass
182
182
183 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
183 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
184 GREEDY_DELIMS = ' \r\n'
184 GREEDY_DELIMS = ' \r\n'
185
185
186 class CompletionSplitter(object):
186 class CompletionSplitter(object):
187 """An object to split an input line in a manner similar to readline.
187 """An object to split an input line in a manner similar to readline.
188
188
189 By having our own implementation, we can expose readline-like completion in
189 By having our own implementation, we can expose readline-like completion in
190 a uniform manner to all frontends. This object only needs to be given the
190 a uniform manner to all frontends. This object only needs to be given the
191 line of text to be split and the cursor position on said line, and it
191 line of text to be split and the cursor position on said line, and it
192 returns the 'word' to be completed on at the cursor after splitting the
192 returns the 'word' to be completed on at the cursor after splitting the
193 entire line.
193 entire line.
194
194
195 What characters are used as splitting delimiters can be controlled by
195 What characters are used as splitting delimiters can be controlled by
196 setting the `delims` attribute (this is a property that internally
196 setting the `delims` attribute (this is a property that internally
197 automatically builds the necessary """
197 automatically builds the necessary """
198
198
199 # Private interface
199 # Private interface
200
200
201 # A string of delimiter characters. The default value makes sense for
201 # A string of delimiter characters. The default value makes sense for
202 # IPython's most typical usage patterns.
202 # IPython's most typical usage patterns.
203 _delims = DELIMS
203 _delims = DELIMS
204
204
205 # The expression (a normal string) to be compiled into a regular expression
205 # The expression (a normal string) to be compiled into a regular expression
206 # for actual splitting. We store it as an attribute mostly for ease of
206 # for actual splitting. We store it as an attribute mostly for ease of
207 # debugging, since this type of code can be so tricky to debug.
207 # debugging, since this type of code can be so tricky to debug.
208 _delim_expr = None
208 _delim_expr = None
209
209
210 # The regular expression that does the actual splitting
210 # The regular expression that does the actual splitting
211 _delim_re = None
211 _delim_re = None
212
212
213 def __init__(self, delims=None):
213 def __init__(self, delims=None):
214 delims = CompletionSplitter._delims if delims is None else delims
214 delims = CompletionSplitter._delims if delims is None else delims
215 self.set_delims(delims)
215 self.set_delims(delims)
216
216
217 def set_delims(self, delims):
217 def set_delims(self, delims):
218 """Set the delimiters for line splitting."""
218 """Set the delimiters for line splitting."""
219 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
219 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
220 self._delim_re = re.compile(expr)
220 self._delim_re = re.compile(expr)
221 self._delims = delims
221 self._delims = delims
222 self._delim_expr = expr
222 self._delim_expr = expr
223
223
224 def get_delims(self):
224 def get_delims(self):
225 """Return the string of delimiter characters."""
225 """Return the string of delimiter characters."""
226 return self._delims
226 return self._delims
227
227
228 def split_line(self, line, cursor_pos=None):
228 def split_line(self, line, cursor_pos=None):
229 """Split a line of text with a cursor at the given position.
229 """Split a line of text with a cursor at the given position.
230 """
230 """
231 l = line if cursor_pos is None else line[:cursor_pos]
231 l = line if cursor_pos is None else line[:cursor_pos]
232 return self._delim_re.split(l)[-1]
232 return self._delim_re.split(l)[-1]
233
233
234
234
235 class Completer(Configurable):
235 class Completer(Configurable):
236
236
237 greedy = CBool(False, config=True,
237 greedy = CBool(False, config=True,
238 help="""Activate greedy completion
238 help="""Activate greedy completion
239
239
240 This will enable completion on elements of lists, results of function calls, etc.,
240 This will enable completion on elements of lists, results of function calls, etc.,
241 but can be unsafe because the code is actually evaluated on TAB.
241 but can be unsafe because the code is actually evaluated on TAB.
242 """
242 """
243 )
243 )
244
244
245
245
246 def __init__(self, namespace=None, global_namespace=None, config=None, **kwargs):
246 def __init__(self, namespace=None, global_namespace=None, config=None, **kwargs):
247 """Create a new completer for the command line.
247 """Create a new completer for the command line.
248
248
249 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
249 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
250
250
251 If unspecified, the default namespace where completions are performed
251 If unspecified, the default namespace where completions are performed
252 is __main__ (technically, __main__.__dict__). Namespaces should be
252 is __main__ (technically, __main__.__dict__). Namespaces should be
253 given as dictionaries.
253 given as dictionaries.
254
254
255 An optional second namespace can be given. This allows the completer
255 An optional second namespace can be given. This allows the completer
256 to handle cases where both the local and global scopes need to be
256 to handle cases where both the local and global scopes need to be
257 distinguished.
257 distinguished.
258
258
259 Completer instances should be used as the completion mechanism of
259 Completer instances should be used as the completion mechanism of
260 readline via the set_completer() call:
260 readline via the set_completer() call:
261
261
262 readline.set_completer(Completer(my_namespace).complete)
262 readline.set_completer(Completer(my_namespace).complete)
263 """
263 """
264
264
265 # Don't bind to namespace quite yet, but flag whether the user wants a
265 # Don't bind to namespace quite yet, but flag whether the user wants a
266 # specific namespace or to use __main__.__dict__. This will allow us
266 # specific namespace or to use __main__.__dict__. This will allow us
267 # to bind to __main__.__dict__ at completion time, not now.
267 # to bind to __main__.__dict__ at completion time, not now.
268 if namespace is None:
268 if namespace is None:
269 self.use_main_ns = 1
269 self.use_main_ns = 1
270 else:
270 else:
271 self.use_main_ns = 0
271 self.use_main_ns = 0
272 self.namespace = namespace
272 self.namespace = namespace
273
273
274 # The global namespace, if given, can be bound directly
274 # The global namespace, if given, can be bound directly
275 if global_namespace is None:
275 if global_namespace is None:
276 self.global_namespace = {}
276 self.global_namespace = {}
277 else:
277 else:
278 self.global_namespace = global_namespace
278 self.global_namespace = global_namespace
279
279
280 super(Completer, self).__init__(config=config, **kwargs)
280 super(Completer, self).__init__(config=config, **kwargs)
281
281
282 def complete(self, text, state):
282 def complete(self, text, state):
283 """Return the next possible completion for 'text'.
283 """Return the next possible completion for 'text'.
284
284
285 This is called successively with state == 0, 1, 2, ... until it
285 This is called successively with state == 0, 1, 2, ... until it
286 returns None. The completion should begin with 'text'.
286 returns None. The completion should begin with 'text'.
287
287
288 """
288 """
289 if self.use_main_ns:
289 if self.use_main_ns:
290 self.namespace = __main__.__dict__
290 self.namespace = __main__.__dict__
291
291
292 if state == 0:
292 if state == 0:
293 if "." in text:
293 if "." in text:
294 self.matches = self.attr_matches(text)
294 self.matches = self.attr_matches(text)
295 else:
295 else:
296 self.matches = self.global_matches(text)
296 self.matches = self.global_matches(text)
297 try:
297 try:
298 return self.matches[state]
298 return self.matches[state]
299 except IndexError:
299 except IndexError:
300 return None
300 return None
301
301
302 def global_matches(self, text):
302 def global_matches(self, text):
303 """Compute matches when text is a simple name.
303 """Compute matches when text is a simple name.
304
304
305 Return a list of all keywords, built-in functions and names currently
305 Return a list of all keywords, built-in functions and names currently
306 defined in self.namespace or self.global_namespace that match.
306 defined in self.namespace or self.global_namespace that match.
307
307
308 """
308 """
309 #print 'Completer->global_matches, txt=%r' % text # dbg
309 #print 'Completer->global_matches, txt=%r' % text # dbg
310 matches = []
310 matches = []
311 match_append = matches.append
311 match_append = matches.append
312 n = len(text)
312 n = len(text)
313 for lst in [keyword.kwlist,
313 for lst in [keyword.kwlist,
314 __builtin__.__dict__.keys(),
314 __builtin__.__dict__.keys(),
315 self.namespace.keys(),
315 self.namespace.keys(),
316 self.global_namespace.keys()]:
316 self.global_namespace.keys()]:
317 for word in lst:
317 for word in lst:
318 if word[:n] == text and word != "__builtins__":
318 if word[:n] == text and word != "__builtins__":
319 match_append(word)
319 match_append(word)
320 return matches
320 return matches
321
321
322 def attr_matches(self, text):
322 def attr_matches(self, text):
323 """Compute matches when text contains a dot.
323 """Compute matches when text contains a dot.
324
324
325 Assuming the text is of the form NAME.NAME....[NAME], and is
325 Assuming the text is of the form NAME.NAME....[NAME], and is
326 evaluatable in self.namespace or self.global_namespace, it will be
326 evaluatable in self.namespace or self.global_namespace, it will be
327 evaluated and its attributes (as revealed by dir()) are used as
327 evaluated and its attributes (as revealed by dir()) are used as
328 possible completions. (For class instances, class members are are
328 possible completions. (For class instances, class members are are
329 also considered.)
329 also considered.)
330
330
331 WARNING: this can still invoke arbitrary C code, if an object
331 WARNING: this can still invoke arbitrary C code, if an object
332 with a __getattr__ hook is evaluated.
332 with a __getattr__ hook is evaluated.
333
333
334 """
334 """
335
335
336 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
336 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
337 # Another option, seems to work great. Catches things like ''.<tab>
337 # Another option, seems to work great. Catches things like ''.<tab>
338 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
338 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
339
340 if m:
339 if m:
341 expr, attr = m.group(1, 3)
340 expr, attr = m.group(1, 3)
342 elif self.greedy:
341 elif self.greedy:
343 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
342 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
344 if not m2:
343 if not m2:
345 return []
344 return []
346 expr, attr = m2.group(1,2)
345 expr, attr = m2.group(1,2)
347 else:
346 else:
348 return []
347 return []
349
348
350 try:
349 try:
351 obj = eval(expr, self.namespace)
350 obj = eval(expr, self.namespace)
352 except:
351 except:
353 try:
352 try:
354 obj = eval(expr, self.global_namespace)
353 obj = eval(expr, self.global_namespace)
355 except:
354 except:
356 return []
355 return []
357
356
358 words = dir2(obj)
357 if self.limit_to__all__ and hasattr(obj, '__all__'):
358 words = get__all__entries(obj)
359 else:
360 words = dir2(obj)
359
361
360 try:
362 try:
361 words = generics.complete_object(obj, words)
363 words = generics.complete_object(obj, words)
362 except TryNext:
364 except TryNext:
363 pass
365 pass
364 except Exception:
366 except Exception:
365 # Silence errors from completion function
367 # Silence errors from completion function
366 #raise # dbg
368 #raise # dbg
367 pass
369 pass
368 # Build match list to return
370 # Build match list to return
369 n = len(attr)
371 n = len(attr)
370 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
372 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
371 return res
373 return res
372
374
373
375
376 def get__all__entries(obj):
377 """returns the strings in the __all__ attribute"""
378 try:
379 words = getattr(obj,'__all__')
380 except:
381 return []
382
383 return [w for w in words if isinstance(w, basestring)]
384
385
374 class IPCompleter(Completer):
386 class IPCompleter(Completer):
375 """Extension of the completer class with IPython-specific features"""
387 """Extension of the completer class with IPython-specific features"""
376
388
377 def _greedy_changed(self, name, old, new):
389 def _greedy_changed(self, name, old, new):
378 """update the splitter and readline delims when greedy is changed"""
390 """update the splitter and readline delims when greedy is changed"""
379 if new:
391 if new:
380 self.splitter.set_delims(GREEDY_DELIMS)
392 self.splitter.set_delims(GREEDY_DELIMS)
381 else:
393 else:
382 self.splitter.set_delims(DELIMS)
394 self.splitter.set_delims(DELIMS)
383
395
384 if self.readline:
396 if self.readline:
385 self.readline.set_completer_delims(self.splitter.get_delims())
397 self.readline.set_completer_delims(self.splitter.get_delims())
386
398
387 merge_completions = CBool(True, config=True,
399 merge_completions = CBool(True, config=True,
388 help="""Whether to merge completion results into a single list
400 help="""Whether to merge completion results into a single list
389
401
390 If False, only the completion results from the first non-empty
402 If False, only the completion results from the first non-empty
391 completer will be returned.
403 completer will be returned.
392 """
404 """
393 )
405 )
394 omit__names = Enum((0,1,2), default_value=2, config=True,
406 omit__names = Enum((0,1,2), default_value=2, config=True,
395 help="""Instruct the completer to omit private method names
407 help="""Instruct the completer to omit private method names
396
408
397 Specifically, when completing on ``object.<tab>``.
409 Specifically, when completing on ``object.<tab>``.
398
410
399 When 2 [default]: all names that start with '_' will be excluded.
411 When 2 [default]: all names that start with '_' will be excluded.
400
412
401 When 1: all 'magic' names (``__foo__``) will be excluded.
413 When 1: all 'magic' names (``__foo__``) will be excluded.
402
414
403 When 0: nothing will be excluded.
415 When 0: nothing will be excluded.
404 """
416 """
405 )
417 )
418 limit_to__all__ = Enum((0,1), default_value=1, config=True,
419 help="""Instruct the completer to use __all__ for the completion
420
421 Specifically, when completing on ``object.<tab>``.
422
423 When 1: only those names in obj.__all__ will be included.
424
425 When 0 [default]: the values in the __all__ attribute are ignored
426 """
427 )
406
428
407 def __init__(self, shell=None, namespace=None, global_namespace=None,
429 def __init__(self, shell=None, namespace=None, global_namespace=None,
408 alias_table=None, use_readline=True,
430 alias_table=None, use_readline=True,
409 config=None, **kwargs):
431 config=None, **kwargs):
410 """IPCompleter() -> completer
432 """IPCompleter() -> completer
411
433
412 Return a completer object suitable for use by the readline library
434 Return a completer object suitable for use by the readline library
413 via readline.set_completer().
435 via readline.set_completer().
414
436
415 Inputs:
437 Inputs:
416
438
417 - shell: a pointer to the ipython shell itself. This is needed
439 - shell: a pointer to the ipython shell itself. This is needed
418 because this completer knows about magic functions, and those can
440 because this completer knows about magic functions, and those can
419 only be accessed via the ipython instance.
441 only be accessed via the ipython instance.
420
442
421 - namespace: an optional dict where completions are performed.
443 - namespace: an optional dict where completions are performed.
422
444
423 - global_namespace: secondary optional dict for completions, to
445 - global_namespace: secondary optional dict for completions, to
424 handle cases (such as IPython embedded inside functions) where
446 handle cases (such as IPython embedded inside functions) where
425 both Python scopes are visible.
447 both Python scopes are visible.
426
448
427 - If alias_table is supplied, it should be a dictionary of aliases
449 - If alias_table is supplied, it should be a dictionary of aliases
428 to complete.
450 to complete.
429
451
430 use_readline : bool, optional
452 use_readline : bool, optional
431 If true, use the readline library. This completer can still function
453 If true, use the readline library. This completer can still function
432 without readline, though in that case callers must provide some extra
454 without readline, though in that case callers must provide some extra
433 information on each call about the current line."""
455 information on each call about the current line."""
434
456
435 self.magic_escape = ESC_MAGIC
457 self.magic_escape = ESC_MAGIC
436 self.splitter = CompletionSplitter()
458 self.splitter = CompletionSplitter()
437
459
438 # Readline configuration, only used by the rlcompleter method.
460 # Readline configuration, only used by the rlcompleter method.
439 if use_readline:
461 if use_readline:
440 # We store the right version of readline so that later code
462 # We store the right version of readline so that later code
441 import IPython.utils.rlineimpl as readline
463 import IPython.utils.rlineimpl as readline
442 self.readline = readline
464 self.readline = readline
443 else:
465 else:
444 self.readline = None
466 self.readline = None
445
467
446 # _greedy_changed() depends on splitter and readline being defined:
468 # _greedy_changed() depends on splitter and readline being defined:
447 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
469 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
448 config=config, **kwargs)
470 config=config, **kwargs)
449
471
450 # List where completion matches will be stored
472 # List where completion matches will be stored
451 self.matches = []
473 self.matches = []
452 self.shell = shell.shell
474 self.shell = shell.shell
453 if alias_table is None:
475 if alias_table is None:
454 alias_table = {}
476 alias_table = {}
455 self.alias_table = alias_table
477 self.alias_table = alias_table
456 # Regexp to split filenames with spaces in them
478 # Regexp to split filenames with spaces in them
457 self.space_name_re = re.compile(r'([^\\] )')
479 self.space_name_re = re.compile(r'([^\\] )')
458 # Hold a local ref. to glob.glob for speed
480 # Hold a local ref. to glob.glob for speed
459 self.glob = glob.glob
481 self.glob = glob.glob
460
482
461 # Determine if we are running on 'dumb' terminals, like (X)Emacs
483 # Determine if we are running on 'dumb' terminals, like (X)Emacs
462 # buffers, to avoid completion problems.
484 # buffers, to avoid completion problems.
463 term = os.environ.get('TERM','xterm')
485 term = os.environ.get('TERM','xterm')
464 self.dumb_terminal = term in ['dumb','emacs']
486 self.dumb_terminal = term in ['dumb','emacs']
465
487
466 # Special handling of backslashes needed in win32 platforms
488 # Special handling of backslashes needed in win32 platforms
467 if sys.platform == "win32":
489 if sys.platform == "win32":
468 self.clean_glob = self._clean_glob_win32
490 self.clean_glob = self._clean_glob_win32
469 else:
491 else:
470 self.clean_glob = self._clean_glob
492 self.clean_glob = self._clean_glob
471
493
472 # All active matcher routines for completion
494 # All active matcher routines for completion
473 self.matchers = [self.python_matches,
495 self.matchers = [self.python_matches,
474 self.file_matches,
496 self.file_matches,
475 self.magic_matches,
497 self.magic_matches,
476 self.alias_matches,
498 self.alias_matches,
477 self.python_func_kw_matches,
499 self.python_func_kw_matches,
478 ]
500 ]
479
501
480 def all_completions(self, text):
502 def all_completions(self, text):
481 """
503 """
482 Wrapper around the complete method for the benefit of emacs
504 Wrapper around the complete method for the benefit of emacs
483 and pydb.
505 and pydb.
484 """
506 """
485 return self.complete(text)[1]
507 return self.complete(text)[1]
486
508
487 def _clean_glob(self,text):
509 def _clean_glob(self,text):
488 return self.glob("%s*" % text)
510 return self.glob("%s*" % text)
489
511
490 def _clean_glob_win32(self,text):
512 def _clean_glob_win32(self,text):
491 return [f.replace("\\","/")
513 return [f.replace("\\","/")
492 for f in self.glob("%s*" % text)]
514 for f in self.glob("%s*" % text)]
493
515
494 def file_matches(self, text):
516 def file_matches(self, text):
495 """Match filenames, expanding ~USER type strings.
517 """Match filenames, expanding ~USER type strings.
496
518
497 Most of the seemingly convoluted logic in this completer is an
519 Most of the seemingly convoluted logic in this completer is an
498 attempt to handle filenames with spaces in them. And yet it's not
520 attempt to handle filenames with spaces in them. And yet it's not
499 quite perfect, because Python's readline doesn't expose all of the
521 quite perfect, because Python's readline doesn't expose all of the
500 GNU readline details needed for this to be done correctly.
522 GNU readline details needed for this to be done correctly.
501
523
502 For a filename with a space in it, the printed completions will be
524 For a filename with a space in it, the printed completions will be
503 only the parts after what's already been typed (instead of the
525 only the parts after what's already been typed (instead of the
504 full completions, as is normally done). I don't think with the
526 full completions, as is normally done). I don't think with the
505 current (as of Python 2.3) Python readline it's possible to do
527 current (as of Python 2.3) Python readline it's possible to do
506 better."""
528 better."""
507
529
508 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
530 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
509
531
510 # chars that require escaping with backslash - i.e. chars
532 # chars that require escaping with backslash - i.e. chars
511 # that readline treats incorrectly as delimiters, but we
533 # that readline treats incorrectly as delimiters, but we
512 # don't want to treat as delimiters in filename matching
534 # don't want to treat as delimiters in filename matching
513 # when escaped with backslash
535 # when escaped with backslash
514 if text.startswith('!'):
536 if text.startswith('!'):
515 text = text[1:]
537 text = text[1:]
516 text_prefix = '!'
538 text_prefix = '!'
517 else:
539 else:
518 text_prefix = ''
540 text_prefix = ''
519
541
520 text_until_cursor = self.text_until_cursor
542 text_until_cursor = self.text_until_cursor
521 # track strings with open quotes
543 # track strings with open quotes
522 open_quotes = has_open_quotes(text_until_cursor)
544 open_quotes = has_open_quotes(text_until_cursor)
523
545
524 if '(' in text_until_cursor or '[' in text_until_cursor:
546 if '(' in text_until_cursor or '[' in text_until_cursor:
525 lsplit = text
547 lsplit = text
526 else:
548 else:
527 try:
549 try:
528 # arg_split ~ shlex.split, but with unicode bugs fixed by us
550 # arg_split ~ shlex.split, but with unicode bugs fixed by us
529 lsplit = arg_split(text_until_cursor)[-1]
551 lsplit = arg_split(text_until_cursor)[-1]
530 except ValueError:
552 except ValueError:
531 # typically an unmatched ", or backslash without escaped char.
553 # typically an unmatched ", or backslash without escaped char.
532 if open_quotes:
554 if open_quotes:
533 lsplit = text_until_cursor.split(open_quotes)[-1]
555 lsplit = text_until_cursor.split(open_quotes)[-1]
534 else:
556 else:
535 return []
557 return []
536 except IndexError:
558 except IndexError:
537 # tab pressed on empty line
559 # tab pressed on empty line
538 lsplit = ""
560 lsplit = ""
539
561
540 if not open_quotes and lsplit != protect_filename(lsplit):
562 if not open_quotes and lsplit != protect_filename(lsplit):
541 # if protectables are found, do matching on the whole escaped name
563 # if protectables are found, do matching on the whole escaped name
542 has_protectables = True
564 has_protectables = True
543 text0,text = text,lsplit
565 text0,text = text,lsplit
544 else:
566 else:
545 has_protectables = False
567 has_protectables = False
546 text = os.path.expanduser(text)
568 text = os.path.expanduser(text)
547
569
548 if text == "":
570 if text == "":
549 return [text_prefix + protect_filename(f) for f in self.glob("*")]
571 return [text_prefix + protect_filename(f) for f in self.glob("*")]
550
572
551 # Compute the matches from the filesystem
573 # Compute the matches from the filesystem
552 m0 = self.clean_glob(text.replace('\\',''))
574 m0 = self.clean_glob(text.replace('\\',''))
553
575
554 if has_protectables:
576 if has_protectables:
555 # If we had protectables, we need to revert our changes to the
577 # If we had protectables, we need to revert our changes to the
556 # beginning of filename so that we don't double-write the part
578 # beginning of filename so that we don't double-write the part
557 # of the filename we have so far
579 # of the filename we have so far
558 len_lsplit = len(lsplit)
580 len_lsplit = len(lsplit)
559 matches = [text_prefix + text0 +
581 matches = [text_prefix + text0 +
560 protect_filename(f[len_lsplit:]) for f in m0]
582 protect_filename(f[len_lsplit:]) for f in m0]
561 else:
583 else:
562 if open_quotes:
584 if open_quotes:
563 # if we have a string with an open quote, we don't need to
585 # if we have a string with an open quote, we don't need to
564 # protect the names at all (and we _shouldn't_, as it
586 # protect the names at all (and we _shouldn't_, as it
565 # would cause bugs when the filesystem call is made).
587 # would cause bugs when the filesystem call is made).
566 matches = m0
588 matches = m0
567 else:
589 else:
568 matches = [text_prefix +
590 matches = [text_prefix +
569 protect_filename(f) for f in m0]
591 protect_filename(f) for f in m0]
570
592
571 #io.rprint('mm', matches) # dbg
593 #io.rprint('mm', matches) # dbg
572
594
573 # Mark directories in input list by appending '/' to their names.
595 # Mark directories in input list by appending '/' to their names.
574 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
596 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
575 return matches
597 return matches
576
598
577 def magic_matches(self, text):
599 def magic_matches(self, text):
578 """Match magics"""
600 """Match magics"""
579 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
601 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
580 # Get all shell magics now rather than statically, so magics loaded at
602 # Get all shell magics now rather than statically, so magics loaded at
581 # runtime show up too
603 # runtime show up too
582 magics = self.shell.lsmagic()
604 magics = self.shell.lsmagic()
583 pre = self.magic_escape
605 pre = self.magic_escape
584 baretext = text.lstrip(pre)
606 baretext = text.lstrip(pre)
585 return [ pre+m for m in magics if m.startswith(baretext)]
607 return [ pre+m for m in magics if m.startswith(baretext)]
586
608
587 def alias_matches(self, text):
609 def alias_matches(self, text):
588 """Match internal system aliases"""
610 """Match internal system aliases"""
589 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
611 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
590
612
591 # if we are not in the first 'item', alias matching
613 # if we are not in the first 'item', alias matching
592 # doesn't make sense - unless we are starting with 'sudo' command.
614 # doesn't make sense - unless we are starting with 'sudo' command.
593 main_text = self.text_until_cursor.lstrip()
615 main_text = self.text_until_cursor.lstrip()
594 if ' ' in main_text and not main_text.startswith('sudo'):
616 if ' ' in main_text and not main_text.startswith('sudo'):
595 return []
617 return []
596 text = os.path.expanduser(text)
618 text = os.path.expanduser(text)
597 aliases = self.alias_table.keys()
619 aliases = self.alias_table.keys()
598 if text == '':
620 if text == '':
599 return aliases
621 return aliases
600 else:
622 else:
601 return [a for a in aliases if a.startswith(text)]
623 return [a for a in aliases if a.startswith(text)]
602
624
603 def python_matches(self,text):
625 def python_matches(self,text):
604 """Match attributes or global python names"""
626 """Match attributes or global python names"""
605
627
606 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
628 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
607 if "." in text:
629 if "." in text:
608 try:
630 try:
609 matches = self.attr_matches(text)
631 matches = self.attr_matches(text)
610 if text.endswith('.') and self.omit__names:
632 if text.endswith('.') and self.omit__names:
611 if self.omit__names == 1:
633 if self.omit__names == 1:
612 # true if txt is _not_ a __ name, false otherwise:
634 # true if txt is _not_ a __ name, false otherwise:
613 no__name = (lambda txt:
635 no__name = (lambda txt:
614 re.match(r'.*\.__.*?__',txt) is None)
636 re.match(r'.*\.__.*?__',txt) is None)
615 else:
637 else:
616 # true if txt is _not_ a _ name, false otherwise:
638 # true if txt is _not_ a _ name, false otherwise:
617 no__name = (lambda txt:
639 no__name = (lambda txt:
618 re.match(r'.*\._.*?',txt) is None)
640 re.match(r'.*\._.*?',txt) is None)
619 matches = filter(no__name, matches)
641 matches = filter(no__name, matches)
620 except NameError:
642 except NameError:
621 # catches <undefined attributes>.<tab>
643 # catches <undefined attributes>.<tab>
622 matches = []
644 matches = []
623 else:
645 else:
624 matches = self.global_matches(text)
646 matches = self.global_matches(text)
625
647
626 return matches
648 return matches
627
649
628 def _default_arguments(self, obj):
650 def _default_arguments(self, obj):
629 """Return the list of default arguments of obj if it is callable,
651 """Return the list of default arguments of obj if it is callable,
630 or empty list otherwise."""
652 or empty list otherwise."""
631
653
632 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
654 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
633 # for classes, check for __init__,__new__
655 # for classes, check for __init__,__new__
634 if inspect.isclass(obj):
656 if inspect.isclass(obj):
635 obj = (getattr(obj,'__init__',None) or
657 obj = (getattr(obj,'__init__',None) or
636 getattr(obj,'__new__',None))
658 getattr(obj,'__new__',None))
637 # for all others, check if they are __call__able
659 # for all others, check if they are __call__able
638 elif hasattr(obj, '__call__'):
660 elif hasattr(obj, '__call__'):
639 obj = obj.__call__
661 obj = obj.__call__
640 # XXX: is there a way to handle the builtins ?
662 # XXX: is there a way to handle the builtins ?
641 try:
663 try:
642 args,_,_1,defaults = inspect.getargspec(obj)
664 args,_,_1,defaults = inspect.getargspec(obj)
643 if defaults:
665 if defaults:
644 return args[-len(defaults):]
666 return args[-len(defaults):]
645 except TypeError: pass
667 except TypeError: pass
646 return []
668 return []
647
669
648 def python_func_kw_matches(self,text):
670 def python_func_kw_matches(self,text):
649 """Match named parameters (kwargs) of the last open function"""
671 """Match named parameters (kwargs) of the last open function"""
650
672
651 if "." in text: # a parameter cannot be dotted
673 if "." in text: # a parameter cannot be dotted
652 return []
674 return []
653 try: regexp = self.__funcParamsRegex
675 try: regexp = self.__funcParamsRegex
654 except AttributeError:
676 except AttributeError:
655 regexp = self.__funcParamsRegex = re.compile(r'''
677 regexp = self.__funcParamsRegex = re.compile(r'''
656 '.*?' | # single quoted strings or
678 '.*?' | # single quoted strings or
657 ".*?" | # double quoted strings or
679 ".*?" | # double quoted strings or
658 \w+ | # identifier
680 \w+ | # identifier
659 \S # other characters
681 \S # other characters
660 ''', re.VERBOSE | re.DOTALL)
682 ''', re.VERBOSE | re.DOTALL)
661 # 1. find the nearest identifier that comes before an unclosed
683 # 1. find the nearest identifier that comes before an unclosed
662 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
684 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
663 tokens = regexp.findall(self.line_buffer)
685 tokens = regexp.findall(self.line_buffer)
664 tokens.reverse()
686 tokens.reverse()
665 iterTokens = iter(tokens); openPar = 0
687 iterTokens = iter(tokens); openPar = 0
666 for token in iterTokens:
688 for token in iterTokens:
667 if token == ')':
689 if token == ')':
668 openPar -= 1
690 openPar -= 1
669 elif token == '(':
691 elif token == '(':
670 openPar += 1
692 openPar += 1
671 if openPar > 0:
693 if openPar > 0:
672 # found the last unclosed parenthesis
694 # found the last unclosed parenthesis
673 break
695 break
674 else:
696 else:
675 return []
697 return []
676 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
698 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
677 ids = []
699 ids = []
678 isId = re.compile(r'\w+$').match
700 isId = re.compile(r'\w+$').match
679 while True:
701 while True:
680 try:
702 try:
681 ids.append(iterTokens.next())
703 ids.append(iterTokens.next())
682 if not isId(ids[-1]):
704 if not isId(ids[-1]):
683 ids.pop(); break
705 ids.pop(); break
684 if not iterTokens.next() == '.':
706 if not iterTokens.next() == '.':
685 break
707 break
686 except StopIteration:
708 except StopIteration:
687 break
709 break
688 # lookup the candidate callable matches either using global_matches
710 # lookup the candidate callable matches either using global_matches
689 # or attr_matches for dotted names
711 # or attr_matches for dotted names
690 if len(ids) == 1:
712 if len(ids) == 1:
691 callableMatches = self.global_matches(ids[0])
713 callableMatches = self.global_matches(ids[0])
692 else:
714 else:
693 callableMatches = self.attr_matches('.'.join(ids[::-1]))
715 callableMatches = self.attr_matches('.'.join(ids[::-1]))
694 argMatches = []
716 argMatches = []
695 for callableMatch in callableMatches:
717 for callableMatch in callableMatches:
696 try:
718 try:
697 namedArgs = self._default_arguments(eval(callableMatch,
719 namedArgs = self._default_arguments(eval(callableMatch,
698 self.namespace))
720 self.namespace))
699 except:
721 except:
700 continue
722 continue
701 for namedArg in namedArgs:
723 for namedArg in namedArgs:
702 if namedArg.startswith(text):
724 if namedArg.startswith(text):
703 argMatches.append("%s=" %namedArg)
725 argMatches.append("%s=" %namedArg)
704 return argMatches
726 return argMatches
705
727
706 def dispatch_custom_completer(self, text):
728 def dispatch_custom_completer(self, text):
707 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
729 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
708 line = self.line_buffer
730 line = self.line_buffer
709 if not line.strip():
731 if not line.strip():
710 return None
732 return None
711
733
712 # Create a little structure to pass all the relevant information about
734 # Create a little structure to pass all the relevant information about
713 # the current completion to any custom completer.
735 # the current completion to any custom completer.
714 event = Bunch()
736 event = Bunch()
715 event.line = line
737 event.line = line
716 event.symbol = text
738 event.symbol = text
717 cmd = line.split(None,1)[0]
739 cmd = line.split(None,1)[0]
718 event.command = cmd
740 event.command = cmd
719 event.text_until_cursor = self.text_until_cursor
741 event.text_until_cursor = self.text_until_cursor
720
742
721 #print "\ncustom:{%s]\n" % event # dbg
743 #print "\ncustom:{%s]\n" % event # dbg
722
744
723 # for foo etc, try also to find completer for %foo
745 # for foo etc, try also to find completer for %foo
724 if not cmd.startswith(self.magic_escape):
746 if not cmd.startswith(self.magic_escape):
725 try_magic = self.custom_completers.s_matches(
747 try_magic = self.custom_completers.s_matches(
726 self.magic_escape + cmd)
748 self.magic_escape + cmd)
727 else:
749 else:
728 try_magic = []
750 try_magic = []
729
751
730 for c in itertools.chain(self.custom_completers.s_matches(cmd),
752 for c in itertools.chain(self.custom_completers.s_matches(cmd),
731 try_magic,
753 try_magic,
732 self.custom_completers.flat_matches(self.text_until_cursor)):
754 self.custom_completers.flat_matches(self.text_until_cursor)):
733 #print "try",c # dbg
755 #print "try",c # dbg
734 try:
756 try:
735 res = c(event)
757 res = c(event)
736 if res:
758 if res:
737 # first, try case sensitive match
759 # first, try case sensitive match
738 withcase = [r for r in res if r.startswith(text)]
760 withcase = [r for r in res if r.startswith(text)]
739 if withcase:
761 if withcase:
740 return withcase
762 return withcase
741 # if none, then case insensitive ones are ok too
763 # if none, then case insensitive ones are ok too
742 text_low = text.lower()
764 text_low = text.lower()
743 return [r for r in res if r.lower().startswith(text_low)]
765 return [r for r in res if r.lower().startswith(text_low)]
744 except TryNext:
766 except TryNext:
745 pass
767 pass
746
768
747 return None
769 return None
748
770
749 def complete(self, text=None, line_buffer=None, cursor_pos=None):
771 def complete(self, text=None, line_buffer=None, cursor_pos=None):
750 """Find completions for the given text and line context.
772 """Find completions for the given text and line context.
751
773
752 This is called successively with state == 0, 1, 2, ... until it
774 This is called successively with state == 0, 1, 2, ... until it
753 returns None. The completion should begin with 'text'.
775 returns None. The completion should begin with 'text'.
754
776
755 Note that both the text and the line_buffer are optional, but at least
777 Note that both the text and the line_buffer are optional, but at least
756 one of them must be given.
778 one of them must be given.
757
779
758 Parameters
780 Parameters
759 ----------
781 ----------
760 text : string, optional
782 text : string, optional
761 Text to perform the completion on. If not given, the line buffer
783 Text to perform the completion on. If not given, the line buffer
762 is split using the instance's CompletionSplitter object.
784 is split using the instance's CompletionSplitter object.
763
785
764 line_buffer : string, optional
786 line_buffer : string, optional
765 If not given, the completer attempts to obtain the current line
787 If not given, the completer attempts to obtain the current line
766 buffer via readline. This keyword allows clients which are
788 buffer via readline. This keyword allows clients which are
767 requesting for text completions in non-readline contexts to inform
789 requesting for text completions in non-readline contexts to inform
768 the completer of the entire text.
790 the completer of the entire text.
769
791
770 cursor_pos : int, optional
792 cursor_pos : int, optional
771 Index of the cursor in the full line buffer. Should be provided by
793 Index of the cursor in the full line buffer. Should be provided by
772 remote frontends where kernel has no access to frontend state.
794 remote frontends where kernel has no access to frontend state.
773
795
774 Returns
796 Returns
775 -------
797 -------
776 text : str
798 text : str
777 Text that was actually used in the completion.
799 Text that was actually used in the completion.
778
800
779 matches : list
801 matches : list
780 A list of completion matches.
802 A list of completion matches.
781 """
803 """
782 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
804 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
783
805
784 # if the cursor position isn't given, the only sane assumption we can
806 # if the cursor position isn't given, the only sane assumption we can
785 # make is that it's at the end of the line (the common case)
807 # make is that it's at the end of the line (the common case)
786 if cursor_pos is None:
808 if cursor_pos is None:
787 cursor_pos = len(line_buffer) if text is None else len(text)
809 cursor_pos = len(line_buffer) if text is None else len(text)
788
810
789 # if text is either None or an empty string, rely on the line buffer
811 # if text is either None or an empty string, rely on the line buffer
790 if not text:
812 if not text:
791 text = self.splitter.split_line(line_buffer, cursor_pos)
813 text = self.splitter.split_line(line_buffer, cursor_pos)
792
814
793 # If no line buffer is given, assume the input text is all there was
815 # If no line buffer is given, assume the input text is all there was
794 if line_buffer is None:
816 if line_buffer is None:
795 line_buffer = text
817 line_buffer = text
796
818
797 self.line_buffer = line_buffer
819 self.line_buffer = line_buffer
798 self.text_until_cursor = self.line_buffer[:cursor_pos]
820 self.text_until_cursor = self.line_buffer[:cursor_pos]
799 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
821 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
800
822
801 # Start with a clean slate of completions
823 # Start with a clean slate of completions
802 self.matches[:] = []
824 self.matches[:] = []
803 custom_res = self.dispatch_custom_completer(text)
825 custom_res = self.dispatch_custom_completer(text)
804 if custom_res is not None:
826 if custom_res is not None:
805 # did custom completers produce something?
827 # did custom completers produce something?
806 self.matches = custom_res
828 self.matches = custom_res
807 else:
829 else:
808 # Extend the list of completions with the results of each
830 # Extend the list of completions with the results of each
809 # matcher, so we return results to the user from all
831 # matcher, so we return results to the user from all
810 # namespaces.
832 # namespaces.
811 if self.merge_completions:
833 if self.merge_completions:
812 self.matches = []
834 self.matches = []
813 for matcher in self.matchers:
835 for matcher in self.matchers:
814 try:
836 try:
815 self.matches.extend(matcher(text))
837 self.matches.extend(matcher(text))
816 except:
838 except:
817 # Show the ugly traceback if the matcher causes an
839 # Show the ugly traceback if the matcher causes an
818 # exception, but do NOT crash the kernel!
840 # exception, but do NOT crash the kernel!
819 sys.excepthook(*sys.exc_info())
841 sys.excepthook(*sys.exc_info())
820 else:
842 else:
821 for matcher in self.matchers:
843 for matcher in self.matchers:
822 self.matches = matcher(text)
844 self.matches = matcher(text)
823 if self.matches:
845 if self.matches:
824 break
846 break
825 # FIXME: we should extend our api to return a dict with completions for
847 # FIXME: we should extend our api to return a dict with completions for
826 # different types of objects. The rlcomplete() method could then
848 # different types of objects. The rlcomplete() method could then
827 # simply collapse the dict into a list for readline, but we'd have
849 # simply collapse the dict into a list for readline, but we'd have
828 # richer completion semantics in other evironments.
850 # richer completion semantics in other evironments.
829 self.matches = sorted(set(self.matches))
851 self.matches = sorted(set(self.matches))
830 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
852 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
831 return text, self.matches
853 return text, self.matches
832
854
833 def rlcomplete(self, text, state):
855 def rlcomplete(self, text, state):
834 """Return the state-th possible completion for 'text'.
856 """Return the state-th possible completion for 'text'.
835
857
836 This is called successively with state == 0, 1, 2, ... until it
858 This is called successively with state == 0, 1, 2, ... until it
837 returns None. The completion should begin with 'text'.
859 returns None. The completion should begin with 'text'.
838
860
839 Parameters
861 Parameters
840 ----------
862 ----------
841 text : string
863 text : string
842 Text to perform the completion on.
864 Text to perform the completion on.
843
865
844 state : int
866 state : int
845 Counter used by readline.
867 Counter used by readline.
846 """
868 """
847 if state==0:
869 if state==0:
848
870
849 self.line_buffer = line_buffer = self.readline.get_line_buffer()
871 self.line_buffer = line_buffer = self.readline.get_line_buffer()
850 cursor_pos = self.readline.get_endidx()
872 cursor_pos = self.readline.get_endidx()
851
873
852 #io.rprint("\nRLCOMPLETE: %r %r %r" %
874 #io.rprint("\nRLCOMPLETE: %r %r %r" %
853 # (text, line_buffer, cursor_pos) ) # dbg
875 # (text, line_buffer, cursor_pos) ) # dbg
854
876
855 # if there is only a tab on a line with only whitespace, instead of
877 # if there is only a tab on a line with only whitespace, instead of
856 # the mostly useless 'do you want to see all million completions'
878 # the mostly useless 'do you want to see all million completions'
857 # message, just do the right thing and give the user his tab!
879 # message, just do the right thing and give the user his tab!
858 # Incidentally, this enables pasting of tabbed text from an editor
880 # Incidentally, this enables pasting of tabbed text from an editor
859 # (as long as autoindent is off).
881 # (as long as autoindent is off).
860
882
861 # It should be noted that at least pyreadline still shows file
883 # It should be noted that at least pyreadline still shows file
862 # completions - is there a way around it?
884 # completions - is there a way around it?
863
885
864 # don't apply this on 'dumb' terminals, such as emacs buffers, so
886 # don't apply this on 'dumb' terminals, such as emacs buffers, so
865 # we don't interfere with their own tab-completion mechanism.
887 # we don't interfere with their own tab-completion mechanism.
866 if not (self.dumb_terminal or line_buffer.strip()):
888 if not (self.dumb_terminal or line_buffer.strip()):
867 self.readline.insert_text('\t')
889 self.readline.insert_text('\t')
868 sys.stdout.flush()
890 sys.stdout.flush()
869 return None
891 return None
870
892
871 # Note: debugging exceptions that may occur in completion is very
893 # Note: debugging exceptions that may occur in completion is very
872 # tricky, because readline unconditionally silences them. So if
894 # tricky, because readline unconditionally silences them. So if
873 # during development you suspect a bug in the completion code, turn
895 # during development you suspect a bug in the completion code, turn
874 # this flag on temporarily by uncommenting the second form (don't
896 # this flag on temporarily by uncommenting the second form (don't
875 # flip the value in the first line, as the '# dbg' marker can be
897 # flip the value in the first line, as the '# dbg' marker can be
876 # automatically detected and is used elsewhere).
898 # automatically detected and is used elsewhere).
877 DEBUG = False
899 DEBUG = False
878 #DEBUG = True # dbg
900 #DEBUG = True # dbg
879 if DEBUG:
901 if DEBUG:
880 try:
902 try:
881 self.complete(text, line_buffer, cursor_pos)
903 self.complete(text, line_buffer, cursor_pos)
882 except:
904 except:
883 import traceback; traceback.print_exc()
905 import traceback; traceback.print_exc()
884 else:
906 else:
885 # The normal production version is here
907 # The normal production version is here
886
908
887 # This method computes the self.matches array
909 # This method computes the self.matches array
888 self.complete(text, line_buffer, cursor_pos)
910 self.complete(text, line_buffer, cursor_pos)
889
911
890 try:
912 try:
891 return self.matches[state]
913 return self.matches[state]
892 except IndexError:
914 except IndexError:
893 return None
915 return None
@@ -1,232 +1,244 b''
1 """Tests for the IPython tab-completion machinery.
1 """Tests for the IPython tab-completion machinery.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # stdlib
7 # stdlib
8 import os
8 import os
9 import sys
9 import sys
10 import unittest
10 import unittest
11
11
12 # third party
12 # third party
13 import nose.tools as nt
13 import nose.tools as nt
14
14
15 # our own packages
15 # our own packages
16 from IPython.config.loader import Config
16 from IPython.config.loader import Config
17 from IPython.core import completer
17 from IPython.core import completer
18 from IPython.external.decorators import knownfailureif
18 from IPython.external.decorators import knownfailureif
19 from IPython.utils.tempdir import TemporaryDirectory
19 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.utils.generics import complete_object
20 from IPython.utils.generics import complete_object
21 from IPython.testing.globalipapp import get_ipython
21
22
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 # Test functions
24 # Test functions
24 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
25 def test_protect_filename():
26 def test_protect_filename():
26 pairs = [ ('abc','abc'),
27 pairs = [ ('abc','abc'),
27 (' abc',r'\ abc'),
28 (' abc',r'\ abc'),
28 ('a bc',r'a\ bc'),
29 ('a bc',r'a\ bc'),
29 ('a bc',r'a\ \ bc'),
30 ('a bc',r'a\ \ bc'),
30 (' bc',r'\ \ bc'),
31 (' bc',r'\ \ bc'),
31 ]
32 ]
32 # On posix, we also protect parens and other special characters
33 # On posix, we also protect parens and other special characters
33 if sys.platform != 'win32':
34 if sys.platform != 'win32':
34 pairs.extend( [('a(bc',r'a\(bc'),
35 pairs.extend( [('a(bc',r'a\(bc'),
35 ('a)bc',r'a\)bc'),
36 ('a)bc',r'a\)bc'),
36 ('a( )bc',r'a\(\ \)bc'),
37 ('a( )bc',r'a\(\ \)bc'),
37 ('a[1]bc', r'a\[1\]bc'),
38 ('a[1]bc', r'a\[1\]bc'),
38 ('a{1}bc', r'a\{1\}bc'),
39 ('a{1}bc', r'a\{1\}bc'),
39 ('a#bc', r'a\#bc'),
40 ('a#bc', r'a\#bc'),
40 ('a?bc', r'a\?bc'),
41 ('a?bc', r'a\?bc'),
41 ('a=bc', r'a\=bc'),
42 ('a=bc', r'a\=bc'),
42 ('a\\bc', r'a\\bc'),
43 ('a\\bc', r'a\\bc'),
43 ('a|bc', r'a\|bc'),
44 ('a|bc', r'a\|bc'),
44 ('a;bc', r'a\;bc'),
45 ('a;bc', r'a\;bc'),
45 ('a:bc', r'a\:bc'),
46 ('a:bc', r'a\:bc'),
46 ("a'bc", r"a\'bc"),
47 ("a'bc", r"a\'bc"),
47 ('a*bc', r'a\*bc'),
48 ('a*bc', r'a\*bc'),
48 ('a"bc', r'a\"bc'),
49 ('a"bc', r'a\"bc'),
49 ('a^bc', r'a\^bc'),
50 ('a^bc', r'a\^bc'),
50 ('a&bc', r'a\&bc'),
51 ('a&bc', r'a\&bc'),
51 ] )
52 ] )
52 # run the actual tests
53 # run the actual tests
53 for s1, s2 in pairs:
54 for s1, s2 in pairs:
54 s1p = completer.protect_filename(s1)
55 s1p = completer.protect_filename(s1)
55 nt.assert_equals(s1p, s2)
56 nt.assert_equals(s1p, s2)
56
57
57
58
58 def check_line_split(splitter, test_specs):
59 def check_line_split(splitter, test_specs):
59 for part1, part2, split in test_specs:
60 for part1, part2, split in test_specs:
60 cursor_pos = len(part1)
61 cursor_pos = len(part1)
61 line = part1+part2
62 line = part1+part2
62 out = splitter.split_line(line, cursor_pos)
63 out = splitter.split_line(line, cursor_pos)
63 nt.assert_equal(out, split)
64 nt.assert_equal(out, split)
64
65
65
66
66 def test_line_split():
67 def test_line_split():
67 """Basice line splitter test with default specs."""
68 """Basice line splitter test with default specs."""
68 sp = completer.CompletionSplitter()
69 sp = completer.CompletionSplitter()
69 # The format of the test specs is: part1, part2, expected answer. Parts 1
70 # The format of the test specs is: part1, part2, expected answer. Parts 1
70 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
71 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
71 # was at the end of part1. So an empty part2 represents someone hitting
72 # was at the end of part1. So an empty part2 represents someone hitting
72 # tab at the end of the line, the most common case.
73 # tab at the end of the line, the most common case.
73 t = [('run some/scrip', '', 'some/scrip'),
74 t = [('run some/scrip', '', 'some/scrip'),
74 ('run scripts/er', 'ror.py foo', 'scripts/er'),
75 ('run scripts/er', 'ror.py foo', 'scripts/er'),
75 ('echo $HOM', '', 'HOM'),
76 ('echo $HOM', '', 'HOM'),
76 ('print sys.pa', '', 'sys.pa'),
77 ('print sys.pa', '', 'sys.pa'),
77 ('print(sys.pa', '', 'sys.pa'),
78 ('print(sys.pa', '', 'sys.pa'),
78 ("execfile('scripts/er", '', 'scripts/er'),
79 ("execfile('scripts/er", '', 'scripts/er'),
79 ('a[x.', '', 'x.'),
80 ('a[x.', '', 'x.'),
80 ('a[x.', 'y', 'x.'),
81 ('a[x.', 'y', 'x.'),
81 ('cd "some_file/', '', 'some_file/'),
82 ('cd "some_file/', '', 'some_file/'),
82 ]
83 ]
83 check_line_split(sp, t)
84 check_line_split(sp, t)
84 # Ensure splitting works OK with unicode by re-running the tests with
85 # Ensure splitting works OK with unicode by re-running the tests with
85 # all inputs turned into unicode
86 # all inputs turned into unicode
86 check_line_split(sp, [ map(unicode, p) for p in t] )
87 check_line_split(sp, [ map(unicode, p) for p in t] )
87
88
88 def test_custom_completion_error():
89 def test_custom_completion_error():
89 """Test that errors from custom attribute completers are silenced."""
90 """Test that errors from custom attribute completers are silenced."""
90 ip = get_ipython()
91 ip = get_ipython()
91 class A(object): pass
92 class A(object): pass
92 ip.user_ns['a'] = A()
93 ip.user_ns['a'] = A()
93
94
94 @complete_object.when_type(A)
95 @complete_object.when_type(A)
95 def complete_A(a, existing_completions):
96 def complete_A(a, existing_completions):
96 raise TypeError("this should be silenced")
97 raise TypeError("this should be silenced")
97
98
98 ip.complete("a.")
99 ip.complete("a.")
99
100
100
101
101 def test_unicode_completions():
102 def test_unicode_completions():
102 ip = get_ipython()
103 ip = get_ipython()
103 # Some strings that trigger different types of completion. Check them both
104 # Some strings that trigger different types of completion. Check them both
104 # in str and unicode forms
105 # in str and unicode forms
105 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
106 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
106 for t in s + map(unicode, s):
107 for t in s + map(unicode, s):
107 # We don't need to check exact completion values (they may change
108 # We don't need to check exact completion values (they may change
108 # depending on the state of the namespace, but at least no exceptions
109 # depending on the state of the namespace, but at least no exceptions
109 # should be thrown and the return value should be a pair of text, list
110 # should be thrown and the return value should be a pair of text, list
110 # values.
111 # values.
111 text, matches = ip.complete(t)
112 text, matches = ip.complete(t)
112 nt.assert_true(isinstance(text, basestring))
113 nt.assert_true(isinstance(text, basestring))
113 nt.assert_true(isinstance(matches, list))
114 nt.assert_true(isinstance(matches, list))
114
115
115
116
116 class CompletionSplitterTestCase(unittest.TestCase):
117 class CompletionSplitterTestCase(unittest.TestCase):
117 def setUp(self):
118 def setUp(self):
118 self.sp = completer.CompletionSplitter()
119 self.sp = completer.CompletionSplitter()
119
120
120 def test_delim_setting(self):
121 def test_delim_setting(self):
121 self.sp.set_delims(' ')
122 self.sp.set_delims(' ')
122 nt.assert_equal(self.sp.get_delims(), ' ')
123 nt.assert_equal(self.sp.get_delims(), ' ')
123 nt.assert_equal(self.sp._delim_expr, '[\ ]')
124 nt.assert_equal(self.sp._delim_expr, '[\ ]')
124
125
125 def test_spaces(self):
126 def test_spaces(self):
126 """Test with only spaces as split chars."""
127 """Test with only spaces as split chars."""
127 self.sp.delims = ' '
128 self.sp.delims = ' '
128 t = [('foo', '', 'foo'),
129 t = [('foo', '', 'foo'),
129 ('run foo', '', 'foo'),
130 ('run foo', '', 'foo'),
130 ('run foo', 'bar', 'foo'),
131 ('run foo', 'bar', 'foo'),
131 ]
132 ]
132 check_line_split(self.sp, t)
133 check_line_split(self.sp, t)
133
134
134
135
135 def test_has_open_quotes1():
136 def test_has_open_quotes1():
136 for s in ["'", "'''", "'hi' '"]:
137 for s in ["'", "'''", "'hi' '"]:
137 nt.assert_equal(completer.has_open_quotes(s), "'")
138 nt.assert_equal(completer.has_open_quotes(s), "'")
138
139
139
140
140 def test_has_open_quotes2():
141 def test_has_open_quotes2():
141 for s in ['"', '"""', '"hi" "']:
142 for s in ['"', '"""', '"hi" "']:
142 nt.assert_equal(completer.has_open_quotes(s), '"')
143 nt.assert_equal(completer.has_open_quotes(s), '"')
143
144
144
145
145 def test_has_open_quotes3():
146 def test_has_open_quotes3():
146 for s in ["''", "''' '''", "'hi' 'ipython'"]:
147 for s in ["''", "''' '''", "'hi' 'ipython'"]:
147 nt.assert_false(completer.has_open_quotes(s))
148 nt.assert_false(completer.has_open_quotes(s))
148
149
149
150
150 def test_has_open_quotes4():
151 def test_has_open_quotes4():
151 for s in ['""', '""" """', '"hi" "ipython"']:
152 for s in ['""', '""" """', '"hi" "ipython"']:
152 nt.assert_false(completer.has_open_quotes(s))
153 nt.assert_false(completer.has_open_quotes(s))
153
154
154 @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows")
155 @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows")
155 def test_abspath_file_completions():
156 def test_abspath_file_completions():
156 ip = get_ipython()
157 ip = get_ipython()
157 with TemporaryDirectory() as tmpdir:
158 with TemporaryDirectory() as tmpdir:
158 prefix = os.path.join(tmpdir, 'foo')
159 prefix = os.path.join(tmpdir, 'foo')
159 suffixes = map(str, [1,2])
160 suffixes = map(str, [1,2])
160 names = [prefix+s for s in suffixes]
161 names = [prefix+s for s in suffixes]
161 for n in names:
162 for n in names:
162 open(n, 'w').close()
163 open(n, 'w').close()
163
164
164 # Check simple completion
165 # Check simple completion
165 c = ip.complete(prefix)[1]
166 c = ip.complete(prefix)[1]
166 nt.assert_equal(c, names)
167 nt.assert_equal(c, names)
167
168
168 # Now check with a function call
169 # Now check with a function call
169 cmd = 'a = f("%s' % prefix
170 cmd = 'a = f("%s' % prefix
170 c = ip.complete(prefix, cmd)[1]
171 c = ip.complete(prefix, cmd)[1]
171 comp = [prefix+s for s in suffixes]
172 comp = [prefix+s for s in suffixes]
172 nt.assert_equal(c, comp)
173 nt.assert_equal(c, comp)
173
174
174 def test_local_file_completions():
175 def test_local_file_completions():
175 ip = get_ipython()
176 ip = get_ipython()
176 cwd = os.getcwdu()
177 cwd = os.getcwdu()
177 try:
178 try:
178 with TemporaryDirectory() as tmpdir:
179 with TemporaryDirectory() as tmpdir:
179 os.chdir(tmpdir)
180 os.chdir(tmpdir)
180 prefix = './foo'
181 prefix = './foo'
181 suffixes = map(str, [1,2])
182 suffixes = map(str, [1,2])
182 names = [prefix+s for s in suffixes]
183 names = [prefix+s for s in suffixes]
183 for n in names:
184 for n in names:
184 open(n, 'w').close()
185 open(n, 'w').close()
185
186
186 # Check simple completion
187 # Check simple completion
187 c = ip.complete(prefix)[1]
188 c = ip.complete(prefix)[1]
188 nt.assert_equal(c, names)
189 nt.assert_equal(c, names)
189
190
190 # Now check with a function call
191 # Now check with a function call
191 cmd = 'a = f("%s' % prefix
192 cmd = 'a = f("%s' % prefix
192 c = ip.complete(prefix, cmd)[1]
193 c = ip.complete(prefix, cmd)[1]
193 comp = [prefix+s for s in suffixes]
194 comp = [prefix+s for s in suffixes]
194 nt.assert_equal(c, comp)
195 nt.assert_equal(c, comp)
195 finally:
196 finally:
196 # prevent failures from making chdir stick
197 # prevent failures from making chdir stick
197 os.chdir(cwd)
198 os.chdir(cwd)
198
199
199 def test_greedy_completions():
200 def test_greedy_completions():
200 ip = get_ipython()
201 ip = get_ipython()
201 ip.Completer.greedy = False
202 ip.Completer.greedy = False
202 ip.ex('a=range(5)')
203 ip.ex('a=range(5)')
203 _,c = ip.complete('.',line='a[0].')
204 _,c = ip.complete('.',line='a[0].')
204 nt.assert_false('a[0].real' in c, "Shouldn't have completed on a[0]: %s"%c)
205 nt.assert_false('a[0].real' in c, "Shouldn't have completed on a[0]: %s"%c)
205 ip.Completer.greedy = True
206 ip.Completer.greedy = True
206 _,c = ip.complete('.',line='a[0].')
207 _,c = ip.complete('.',line='a[0].')
207 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
208 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
208
209
209 def test_omit__names():
210 def test_omit__names():
210 # also happens to test IPCompleter as a configurable
211 # also happens to test IPCompleter as a configurable
211 ip = get_ipython()
212 ip = get_ipython()
212 ip._hidden_attr = 1
213 ip._hidden_attr = 1
213 c = ip.Completer
214 c = ip.Completer
214 ip.ex('ip=get_ipython()')
215 ip.ex('ip=get_ipython()')
215 cfg = Config()
216 cfg = Config()
216 cfg.IPCompleter.omit__names = 0
217 cfg.IPCompleter.omit__names = 0
217 c.update_config(cfg)
218 c.update_config(cfg)
218 s,matches = c.complete('ip.')
219 s,matches = c.complete('ip.')
219 nt.assert_true('ip.__str__' in matches)
220 nt.assert_true('ip.__str__' in matches)
220 nt.assert_true('ip._hidden_attr' in matches)
221 nt.assert_true('ip._hidden_attr' in matches)
221 cfg.IPCompleter.omit__names = 1
222 cfg.IPCompleter.omit__names = 1
222 c.update_config(cfg)
223 c.update_config(cfg)
223 s,matches = c.complete('ip.')
224 s,matches = c.complete('ip.')
224 nt.assert_false('ip.__str__' in matches)
225 nt.assert_false('ip.__str__' in matches)
225 nt.assert_true('ip._hidden_attr' in matches)
226 nt.assert_true('ip._hidden_attr' in matches)
226 cfg.IPCompleter.omit__names = 2
227 cfg.IPCompleter.omit__names = 2
227 c.update_config(cfg)
228 c.update_config(cfg)
228 s,matches = c.complete('ip.')
229 s,matches = c.complete('ip.')
229 nt.assert_false('ip.__str__' in matches)
230 nt.assert_false('ip.__str__' in matches)
230 nt.assert_false('ip._hidden_attr' in matches)
231 nt.assert_false('ip._hidden_attr' in matches)
231 del ip._hidden_attr
232 del ip._hidden_attr
232 No newline at end of file
233
234 def test_get__all__entries_ok():
235 class A(object):
236 __all__ = ['x', 1]
237 words = completer.get__all__entries(A())
238 nt.assert_equal(words, ['x'])
239
240 def test_get__all__entries_no__all__ok():
241 class A(object):
242 pass
243 words = completer.get__all__entries(A())
244 nt.assert_equal(words, []) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now