##// END OF EJS Templates
fix some issues in dict key completion...
MinRK -
Show More
@@ -1,1124 +1,1157 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 IPython Development Team
56 # Copyright (C) 2008 IPython Development Team
57 # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001 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
64
65 #-----------------------------------------------------------------------------
65 #-----------------------------------------------------------------------------
66 # Imports
66 # Imports
67 #-----------------------------------------------------------------------------
67 #-----------------------------------------------------------------------------
68
68
69 import __main__
69 import __main__
70 import glob
70 import glob
71 import inspect
71 import inspect
72 import itertools
72 import itertools
73 import keyword
73 import keyword
74 import os
74 import os
75 import re
75 import re
76 import sys
76 import sys
77
77
78 from IPython.config.configurable import Configurable
78 from IPython.config.configurable import Configurable
79 from IPython.core.error import TryNext
79 from IPython.core.error import TryNext
80 from IPython.core.inputsplitter import ESC_MAGIC
80 from IPython.core.inputsplitter import ESC_MAGIC
81 from IPython.utils import generics
81 from IPython.utils import generics
82 from IPython.utils import io
82 from IPython.utils.dir2 import dir2
83 from IPython.utils.dir2 import dir2
83 from IPython.utils.process import arg_split
84 from IPython.utils.process import arg_split
84 from IPython.utils.py3compat import builtin_mod, string_types
85 from IPython.utils.py3compat import builtin_mod, string_types
85 from IPython.utils.traitlets import CBool, Enum
86 from IPython.utils.traitlets import CBool, Enum
86
87
87 #-----------------------------------------------------------------------------
88 #-----------------------------------------------------------------------------
88 # Globals
89 # Globals
89 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
90
91
91 # Public API
92 # Public API
92 __all__ = ['Completer','IPCompleter']
93 __all__ = ['Completer','IPCompleter']
93
94
94 if sys.platform == 'win32':
95 if sys.platform == 'win32':
95 PROTECTABLES = ' '
96 PROTECTABLES = ' '
96 else:
97 else:
97 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
98
99
99
100
100 #-----------------------------------------------------------------------------
101 #-----------------------------------------------------------------------------
101 # Main functions and classes
102 # Main functions and classes
102 #-----------------------------------------------------------------------------
103 #-----------------------------------------------------------------------------
103
104
104 def has_open_quotes(s):
105 def has_open_quotes(s):
105 """Return whether a string has open quotes.
106 """Return whether a string has open quotes.
106
107
107 This simply counts whether the number of quote characters of either type in
108 This simply counts whether the number of quote characters of either type in
108 the string is odd.
109 the string is odd.
109
110
110 Returns
111 Returns
111 -------
112 -------
112 If there is an open quote, the quote character is returned. Else, return
113 If there is an open quote, the quote character is returned. Else, return
113 False.
114 False.
114 """
115 """
115 # We check " first, then ', so complex cases with nested quotes will get
116 # We check " first, then ', so complex cases with nested quotes will get
116 # the " to take precedence.
117 # the " to take precedence.
117 if s.count('"') % 2:
118 if s.count('"') % 2:
118 return '"'
119 return '"'
119 elif s.count("'") % 2:
120 elif s.count("'") % 2:
120 return "'"
121 return "'"
121 else:
122 else:
122 return False
123 return False
123
124
124
125
125 def protect_filename(s):
126 def protect_filename(s):
126 """Escape a string to protect certain characters."""
127 """Escape a string to protect certain characters."""
127
128
128 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
129 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
129 for ch in s])
130 for ch in s])
130
131
131 def expand_user(path):
132 def expand_user(path):
132 """Expand '~'-style usernames in strings.
133 """Expand '~'-style usernames in strings.
133
134
134 This is similar to :func:`os.path.expanduser`, but it computes and returns
135 This is similar to :func:`os.path.expanduser`, but it computes and returns
135 extra information that will be useful if the input was being used in
136 extra information that will be useful if the input was being used in
136 computing completions, and you wish to return the completions with the
137 computing completions, and you wish to return the completions with the
137 original '~' instead of its expanded value.
138 original '~' instead of its expanded value.
138
139
139 Parameters
140 Parameters
140 ----------
141 ----------
141 path : str
142 path : str
142 String to be expanded. If no ~ is present, the output is the same as the
143 String to be expanded. If no ~ is present, the output is the same as the
143 input.
144 input.
144
145
145 Returns
146 Returns
146 -------
147 -------
147 newpath : str
148 newpath : str
148 Result of ~ expansion in the input path.
149 Result of ~ expansion in the input path.
149 tilde_expand : bool
150 tilde_expand : bool
150 Whether any expansion was performed or not.
151 Whether any expansion was performed or not.
151 tilde_val : str
152 tilde_val : str
152 The value that ~ was replaced with.
153 The value that ~ was replaced with.
153 """
154 """
154 # Default values
155 # Default values
155 tilde_expand = False
156 tilde_expand = False
156 tilde_val = ''
157 tilde_val = ''
157 newpath = path
158 newpath = path
158
159
159 if path.startswith('~'):
160 if path.startswith('~'):
160 tilde_expand = True
161 tilde_expand = True
161 rest = len(path)-1
162 rest = len(path)-1
162 newpath = os.path.expanduser(path)
163 newpath = os.path.expanduser(path)
163 if rest:
164 if rest:
164 tilde_val = newpath[:-rest]
165 tilde_val = newpath[:-rest]
165 else:
166 else:
166 tilde_val = newpath
167 tilde_val = newpath
167
168
168 return newpath, tilde_expand, tilde_val
169 return newpath, tilde_expand, tilde_val
169
170
170
171
171 def compress_user(path, tilde_expand, tilde_val):
172 def compress_user(path, tilde_expand, tilde_val):
172 """Does the opposite of expand_user, with its outputs.
173 """Does the opposite of expand_user, with its outputs.
173 """
174 """
174 if tilde_expand:
175 if tilde_expand:
175 return path.replace(tilde_val, '~')
176 return path.replace(tilde_val, '~')
176 else:
177 else:
177 return path
178 return path
178
179
179
180
180
181
181 def penalize_magics_key(word):
182 def penalize_magics_key(word):
182 """key for sorting that penalizes magic commands in the ordering
183 """key for sorting that penalizes magic commands in the ordering
183
184
184 Normal words are left alone.
185 Normal words are left alone.
185
186
186 Magic commands have the initial % moved to the end, e.g.
187 Magic commands have the initial % moved to the end, e.g.
187 %matplotlib is transformed as follows:
188 %matplotlib is transformed as follows:
188
189
189 %matplotlib -> matplotlib%
190 %matplotlib -> matplotlib%
190
191
191 [The choice of the final % is arbitrary.]
192 [The choice of the final % is arbitrary.]
192
193
193 Since "matplotlib" < "matplotlib%" as strings,
194 Since "matplotlib" < "matplotlib%" as strings,
194 "timeit" will appear before the magic "%timeit" in the ordering
195 "timeit" will appear before the magic "%timeit" in the ordering
195
196
196 For consistency, move "%%" to the end, so cell magics appear *after*
197 For consistency, move "%%" to the end, so cell magics appear *after*
197 line magics with the same name.
198 line magics with the same name.
198
199
199 A check is performed that there are no other "%" in the string;
200 A check is performed that there are no other "%" in the string;
200 if there are, then the string is not a magic command and is left unchanged.
201 if there are, then the string is not a magic command and is left unchanged.
201
202
202 """
203 """
203
204
204 # Move any % signs from start to end of the key
205 # Move any % signs from start to end of the key
205 # provided there are no others elsewhere in the string
206 # provided there are no others elsewhere in the string
206
207
207 if word[:2] == "%%":
208 if word[:2] == "%%":
208 if not "%" in word[2:]:
209 if not "%" in word[2:]:
209 return word[2:] + "%%"
210 return word[2:] + "%%"
210
211
211 if word[:1] == "%":
212 if word[:1] == "%":
212 if not "%" in word[1:]:
213 if not "%" in word[1:]:
213 return word[1:] + "%"
214 return word[1:] + "%"
214
215
215 return word
216 return word
216
217
217
218
218
219
219 class Bunch(object): pass
220 class Bunch(object): pass
220
221
221
222
222 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
223 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
223 GREEDY_DELIMS = ' =\r\n'
224 GREEDY_DELIMS = ' =\r\n'
224
225
225
226
226 class CompletionSplitter(object):
227 class CompletionSplitter(object):
227 """An object to split an input line in a manner similar to readline.
228 """An object to split an input line in a manner similar to readline.
228
229
229 By having our own implementation, we can expose readline-like completion in
230 By having our own implementation, we can expose readline-like completion in
230 a uniform manner to all frontends. This object only needs to be given the
231 a uniform manner to all frontends. This object only needs to be given the
231 line of text to be split and the cursor position on said line, and it
232 line of text to be split and the cursor position on said line, and it
232 returns the 'word' to be completed on at the cursor after splitting the
233 returns the 'word' to be completed on at the cursor after splitting the
233 entire line.
234 entire line.
234
235
235 What characters are used as splitting delimiters can be controlled by
236 What characters are used as splitting delimiters can be controlled by
236 setting the `delims` attribute (this is a property that internally
237 setting the `delims` attribute (this is a property that internally
237 automatically builds the necessary regular expression)"""
238 automatically builds the necessary regular expression)"""
238
239
239 # Private interface
240 # Private interface
240
241
241 # A string of delimiter characters. The default value makes sense for
242 # A string of delimiter characters. The default value makes sense for
242 # IPython's most typical usage patterns.
243 # IPython's most typical usage patterns.
243 _delims = DELIMS
244 _delims = DELIMS
244
245
245 # The expression (a normal string) to be compiled into a regular expression
246 # The expression (a normal string) to be compiled into a regular expression
246 # for actual splitting. We store it as an attribute mostly for ease of
247 # for actual splitting. We store it as an attribute mostly for ease of
247 # debugging, since this type of code can be so tricky to debug.
248 # debugging, since this type of code can be so tricky to debug.
248 _delim_expr = None
249 _delim_expr = None
249
250
250 # The regular expression that does the actual splitting
251 # The regular expression that does the actual splitting
251 _delim_re = None
252 _delim_re = None
252
253
253 def __init__(self, delims=None):
254 def __init__(self, delims=None):
254 delims = CompletionSplitter._delims if delims is None else delims
255 delims = CompletionSplitter._delims if delims is None else delims
255 self.delims = delims
256 self.delims = delims
256
257
257 @property
258 @property
258 def delims(self):
259 def delims(self):
259 """Return the string of delimiter characters."""
260 """Return the string of delimiter characters."""
260 return self._delims
261 return self._delims
261
262
262 @delims.setter
263 @delims.setter
263 def delims(self, delims):
264 def delims(self, delims):
264 """Set the delimiters for line splitting."""
265 """Set the delimiters for line splitting."""
265 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
266 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
266 self._delim_re = re.compile(expr)
267 self._delim_re = re.compile(expr)
267 self._delims = delims
268 self._delims = delims
268 self._delim_expr = expr
269 self._delim_expr = expr
269
270
270 def split_line(self, line, cursor_pos=None):
271 def split_line(self, line, cursor_pos=None):
271 """Split a line of text with a cursor at the given position.
272 """Split a line of text with a cursor at the given position.
272 """
273 """
273 l = line if cursor_pos is None else line[:cursor_pos]
274 l = line if cursor_pos is None else line[:cursor_pos]
274 return self._delim_re.split(l)[-1]
275 return self._delim_re.split(l)[-1]
275
276
276
277
277 class Completer(Configurable):
278 class Completer(Configurable):
278
279
279 greedy = CBool(False, config=True,
280 greedy = CBool(False, config=True,
280 help="""Activate greedy completion
281 help="""Activate greedy completion
281
282
282 This will enable completion on elements of lists, results of function calls, etc.,
283 This will enable completion on elements of lists, results of function calls, etc.,
283 but can be unsafe because the code is actually evaluated on TAB.
284 but can be unsafe because the code is actually evaluated on TAB.
284 """
285 """
285 )
286 )
286
287
287
288
288 def __init__(self, namespace=None, global_namespace=None, **kwargs):
289 def __init__(self, namespace=None, global_namespace=None, **kwargs):
289 """Create a new completer for the command line.
290 """Create a new completer for the command line.
290
291
291 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
292 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
292
293
293 If unspecified, the default namespace where completions are performed
294 If unspecified, the default namespace where completions are performed
294 is __main__ (technically, __main__.__dict__). Namespaces should be
295 is __main__ (technically, __main__.__dict__). Namespaces should be
295 given as dictionaries.
296 given as dictionaries.
296
297
297 An optional second namespace can be given. This allows the completer
298 An optional second namespace can be given. This allows the completer
298 to handle cases where both the local and global scopes need to be
299 to handle cases where both the local and global scopes need to be
299 distinguished.
300 distinguished.
300
301
301 Completer instances should be used as the completion mechanism of
302 Completer instances should be used as the completion mechanism of
302 readline via the set_completer() call:
303 readline via the set_completer() call:
303
304
304 readline.set_completer(Completer(my_namespace).complete)
305 readline.set_completer(Completer(my_namespace).complete)
305 """
306 """
306
307
307 # Don't bind to namespace quite yet, but flag whether the user wants a
308 # Don't bind to namespace quite yet, but flag whether the user wants a
308 # specific namespace or to use __main__.__dict__. This will allow us
309 # specific namespace or to use __main__.__dict__. This will allow us
309 # to bind to __main__.__dict__ at completion time, not now.
310 # to bind to __main__.__dict__ at completion time, not now.
310 if namespace is None:
311 if namespace is None:
311 self.use_main_ns = 1
312 self.use_main_ns = 1
312 else:
313 else:
313 self.use_main_ns = 0
314 self.use_main_ns = 0
314 self.namespace = namespace
315 self.namespace = namespace
315
316
316 # The global namespace, if given, can be bound directly
317 # The global namespace, if given, can be bound directly
317 if global_namespace is None:
318 if global_namespace is None:
318 self.global_namespace = {}
319 self.global_namespace = {}
319 else:
320 else:
320 self.global_namespace = global_namespace
321 self.global_namespace = global_namespace
321
322
322 super(Completer, self).__init__(**kwargs)
323 super(Completer, self).__init__(**kwargs)
323
324
324 def complete(self, text, state):
325 def complete(self, text, state):
325 """Return the next possible completion for 'text'.
326 """Return the next possible completion for 'text'.
326
327
327 This is called successively with state == 0, 1, 2, ... until it
328 This is called successively with state == 0, 1, 2, ... until it
328 returns None. The completion should begin with 'text'.
329 returns None. The completion should begin with 'text'.
329
330
330 """
331 """
331 if self.use_main_ns:
332 if self.use_main_ns:
332 self.namespace = __main__.__dict__
333 self.namespace = __main__.__dict__
333
334
334 if state == 0:
335 if state == 0:
335 if "." in text:
336 if "." in text:
336 self.matches = self.attr_matches(text)
337 self.matches = self.attr_matches(text)
337 else:
338 else:
338 self.matches = self.global_matches(text)
339 self.matches = self.global_matches(text)
339 try:
340 try:
340 return self.matches[state]
341 return self.matches[state]
341 except IndexError:
342 except IndexError:
342 return None
343 return None
343
344
344 def global_matches(self, text):
345 def global_matches(self, text):
345 """Compute matches when text is a simple name.
346 """Compute matches when text is a simple name.
346
347
347 Return a list of all keywords, built-in functions and names currently
348 Return a list of all keywords, built-in functions and names currently
348 defined in self.namespace or self.global_namespace that match.
349 defined in self.namespace or self.global_namespace that match.
349
350
350 """
351 """
351 #print 'Completer->global_matches, txt=%r' % text # dbg
352 #print 'Completer->global_matches, txt=%r' % text # dbg
352 matches = []
353 matches = []
353 match_append = matches.append
354 match_append = matches.append
354 n = len(text)
355 n = len(text)
355 for lst in [keyword.kwlist,
356 for lst in [keyword.kwlist,
356 builtin_mod.__dict__.keys(),
357 builtin_mod.__dict__.keys(),
357 self.namespace.keys(),
358 self.namespace.keys(),
358 self.global_namespace.keys()]:
359 self.global_namespace.keys()]:
359 for word in lst:
360 for word in lst:
360 if word[:n] == text and word != "__builtins__":
361 if word[:n] == text and word != "__builtins__":
361 match_append(word)
362 match_append(word)
362 return matches
363 return matches
363
364
364 def attr_matches(self, text):
365 def attr_matches(self, text):
365 """Compute matches when text contains a dot.
366 """Compute matches when text contains a dot.
366
367
367 Assuming the text is of the form NAME.NAME....[NAME], and is
368 Assuming the text is of the form NAME.NAME....[NAME], and is
368 evaluatable in self.namespace or self.global_namespace, it will be
369 evaluatable in self.namespace or self.global_namespace, it will be
369 evaluated and its attributes (as revealed by dir()) are used as
370 evaluated and its attributes (as revealed by dir()) are used as
370 possible completions. (For class instances, class members are are
371 possible completions. (For class instances, class members are are
371 also considered.)
372 also considered.)
372
373
373 WARNING: this can still invoke arbitrary C code, if an object
374 WARNING: this can still invoke arbitrary C code, if an object
374 with a __getattr__ hook is evaluated.
375 with a __getattr__ hook is evaluated.
375
376
376 """
377 """
377
378
378 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
379 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
379 # Another option, seems to work great. Catches things like ''.<tab>
380 # Another option, seems to work great. Catches things like ''.<tab>
380 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
381 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
381
382
382 if m:
383 if m:
383 expr, attr = m.group(1, 3)
384 expr, attr = m.group(1, 3)
384 elif self.greedy:
385 elif self.greedy:
385 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
386 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
386 if not m2:
387 if not m2:
387 return []
388 return []
388 expr, attr = m2.group(1,2)
389 expr, attr = m2.group(1,2)
389 else:
390 else:
390 return []
391 return []
391
392
392 try:
393 try:
393 obj = eval(expr, self.namespace)
394 obj = eval(expr, self.namespace)
394 except:
395 except:
395 try:
396 try:
396 obj = eval(expr, self.global_namespace)
397 obj = eval(expr, self.global_namespace)
397 except:
398 except:
398 return []
399 return []
399
400
400 if self.limit_to__all__ and hasattr(obj, '__all__'):
401 if self.limit_to__all__ and hasattr(obj, '__all__'):
401 words = get__all__entries(obj)
402 words = get__all__entries(obj)
402 else:
403 else:
403 words = dir2(obj)
404 words = dir2(obj)
404
405
405 try:
406 try:
406 words = generics.complete_object(obj, words)
407 words = generics.complete_object(obj, words)
407 except TryNext:
408 except TryNext:
408 pass
409 pass
409 except Exception:
410 except Exception:
410 # Silence errors from completion function
411 # Silence errors from completion function
411 #raise # dbg
412 #raise # dbg
412 pass
413 pass
413 # Build match list to return
414 # Build match list to return
414 n = len(attr)
415 n = len(attr)
415 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
416 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
416 return res
417 return res
417
418
418
419
419 def get__all__entries(obj):
420 def get__all__entries(obj):
420 """returns the strings in the __all__ attribute"""
421 """returns the strings in the __all__ attribute"""
421 try:
422 try:
422 words = getattr(obj, '__all__')
423 words = getattr(obj, '__all__')
423 except:
424 except:
424 return []
425 return []
425
426
426 return [w for w in words if isinstance(w, string_types)]
427 return [w for w in words if isinstance(w, string_types)]
427
428
428
429
429 def match_dict_keys(keys, prefix):
430 def match_dict_keys(keys, prefix):
430 """Used by dict_key_matches, matching the prefix to a list of keys"""
431 """Used by dict_key_matches, matching the prefix to a list of keys"""
431 if not prefix:
432 if not prefix:
432 return None, [repr(k) for k in keys
433 return None, 0, [repr(k) for k in keys
433 if isinstance(k, (string_types, bytes))]
434 if isinstance(k, (string_types, bytes))]
434 quote_match = re.search('["\']', prefix)
435 quote_match = re.search('["\']', prefix)
435 quote = quote_match.group()
436 quote = quote_match.group()
436 try:
437 try:
437 prefix_str = eval(prefix + quote, {})
438 prefix_str = eval(prefix + quote, {})
438 except Exception:
439 except Exception:
439 return None, []
440 return None, 0, []
440
441
441 token_prefix = re.search('\w*$', prefix).group()
442 token_match = re.search(r'\w*$', prefix, re.UNICODE)
443 token_start = token_match.start()
444 token_prefix = token_match.group()
442
445
443 # TODO: support bytes in Py3k
446 # TODO: support bytes in Py3k
444 matched = []
447 matched = []
445 for key in keys:
448 for key in keys:
446 try:
449 try:
447 if not key.startswith(prefix_str):
450 if not key.startswith(prefix_str):
448 continue
451 continue
449 except (AttributeError, TypeError, UnicodeError):
452 except (AttributeError, TypeError, UnicodeError):
450 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
453 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
451 continue
454 continue
452
455
453 # reformat remainder of key to begin with prefix
456 # reformat remainder of key to begin with prefix
454 rem = key[len(prefix_str):]
457 rem = key[len(prefix_str):]
455 # force repr wrapped in '
458 # force repr wrapped in '
456 rem_repr = repr(rem + '"')
459 rem_repr = repr(rem + '"')
457 if rem_repr.startswith('u') and prefix[0] not in 'uU':
460 if rem_repr.startswith('u') and prefix[0] not in 'uU':
458 # Found key is unicode, but prefix is Py2 string.
461 # Found key is unicode, but prefix is Py2 string.
459 # Therefore attempt to interpret key as string.
462 # Therefore attempt to interpret key as string.
460 try:
463 try:
461 rem_repr = repr(rem.encode('ascii') + '"')
464 rem_repr = repr(rem.encode('ascii') + '"')
462 except UnicodeEncodeError:
465 except UnicodeEncodeError:
463 continue
466 continue
464
467
465 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
468 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
466 if quote == '"':
469 if quote == '"':
467 # The entered prefix is quoted with ",
470 # The entered prefix is quoted with ",
468 # but the match is quoted with '.
471 # but the match is quoted with '.
469 # A contained " hence needs escaping for comparison:
472 # A contained " hence needs escaping for comparison:
470 rem_repr = rem_repr.replace('"', '\\"')
473 rem_repr = rem_repr.replace('"', '\\"')
471
474
472 # then reinsert prefix from start of token
475 # then reinsert prefix from start of token
473 matched.append('%s%s' % (token_prefix, rem_repr))
476 matched.append('%s%s' % (token_prefix, rem_repr))
474 return quote, matched
477 return quote, token_start, matched
475
478
476
479
477 def _safe_isinstance(obj, module, class_name):
480 def _safe_isinstance(obj, module, class_name):
478 """Checks if obj is an instance of module.class_name if loaded
481 """Checks if obj is an instance of module.class_name if loaded
479 """
482 """
480 return (module in sys.modules and
483 return (module in sys.modules and
481 isinstance(obj, getattr(__import__(module), class_name)))
484 isinstance(obj, getattr(__import__(module), class_name)))
482
485
483
486
484
487
485 class IPCompleter(Completer):
488 class IPCompleter(Completer):
486 """Extension of the completer class with IPython-specific features"""
489 """Extension of the completer class with IPython-specific features"""
487
490
488 def _greedy_changed(self, name, old, new):
491 def _greedy_changed(self, name, old, new):
489 """update the splitter and readline delims when greedy is changed"""
492 """update the splitter and readline delims when greedy is changed"""
490 if new:
493 if new:
491 self.splitter.delims = GREEDY_DELIMS
494 self.splitter.delims = GREEDY_DELIMS
492 else:
495 else:
493 self.splitter.delims = DELIMS
496 self.splitter.delims = DELIMS
494
497
495 if self.readline:
498 if self.readline:
496 self.readline.set_completer_delims(self.splitter.delims)
499 self.readline.set_completer_delims(self.splitter.delims)
497
500
498 merge_completions = CBool(True, config=True,
501 merge_completions = CBool(True, config=True,
499 help="""Whether to merge completion results into a single list
502 help="""Whether to merge completion results into a single list
500
503
501 If False, only the completion results from the first non-empty
504 If False, only the completion results from the first non-empty
502 completer will be returned.
505 completer will be returned.
503 """
506 """
504 )
507 )
505 omit__names = Enum((0,1,2), default_value=2, config=True,
508 omit__names = Enum((0,1,2), default_value=2, config=True,
506 help="""Instruct the completer to omit private method names
509 help="""Instruct the completer to omit private method names
507
510
508 Specifically, when completing on ``object.<tab>``.
511 Specifically, when completing on ``object.<tab>``.
509
512
510 When 2 [default]: all names that start with '_' will be excluded.
513 When 2 [default]: all names that start with '_' will be excluded.
511
514
512 When 1: all 'magic' names (``__foo__``) will be excluded.
515 When 1: all 'magic' names (``__foo__``) will be excluded.
513
516
514 When 0: nothing will be excluded.
517 When 0: nothing will be excluded.
515 """
518 """
516 )
519 )
517 limit_to__all__ = CBool(default_value=False, config=True,
520 limit_to__all__ = CBool(default_value=False, config=True,
518 help="""Instruct the completer to use __all__ for the completion
521 help="""Instruct the completer to use __all__ for the completion
519
522
520 Specifically, when completing on ``object.<tab>``.
523 Specifically, when completing on ``object.<tab>``.
521
524
522 When True: only those names in obj.__all__ will be included.
525 When True: only those names in obj.__all__ will be included.
523
526
524 When False [default]: the __all__ attribute is ignored
527 When False [default]: the __all__ attribute is ignored
525 """
528 """
526 )
529 )
527
530
528 def __init__(self, shell=None, namespace=None, global_namespace=None,
531 def __init__(self, shell=None, namespace=None, global_namespace=None,
529 use_readline=True, config=None, **kwargs):
532 use_readline=True, config=None, **kwargs):
530 """IPCompleter() -> completer
533 """IPCompleter() -> completer
531
534
532 Return a completer object suitable for use by the readline library
535 Return a completer object suitable for use by the readline library
533 via readline.set_completer().
536 via readline.set_completer().
534
537
535 Inputs:
538 Inputs:
536
539
537 - shell: a pointer to the ipython shell itself. This is needed
540 - shell: a pointer to the ipython shell itself. This is needed
538 because this completer knows about magic functions, and those can
541 because this completer knows about magic functions, and those can
539 only be accessed via the ipython instance.
542 only be accessed via the ipython instance.
540
543
541 - namespace: an optional dict where completions are performed.
544 - namespace: an optional dict where completions are performed.
542
545
543 - global_namespace: secondary optional dict for completions, to
546 - global_namespace: secondary optional dict for completions, to
544 handle cases (such as IPython embedded inside functions) where
547 handle cases (such as IPython embedded inside functions) where
545 both Python scopes are visible.
548 both Python scopes are visible.
546
549
547 use_readline : bool, optional
550 use_readline : bool, optional
548 If true, use the readline library. This completer can still function
551 If true, use the readline library. This completer can still function
549 without readline, though in that case callers must provide some extra
552 without readline, though in that case callers must provide some extra
550 information on each call about the current line."""
553 information on each call about the current line."""
551
554
552 self.magic_escape = ESC_MAGIC
555 self.magic_escape = ESC_MAGIC
553 self.splitter = CompletionSplitter()
556 self.splitter = CompletionSplitter()
554
557
555 # Readline configuration, only used by the rlcompleter method.
558 # Readline configuration, only used by the rlcompleter method.
556 if use_readline:
559 if use_readline:
557 # We store the right version of readline so that later code
560 # We store the right version of readline so that later code
558 import IPython.utils.rlineimpl as readline
561 import IPython.utils.rlineimpl as readline
559 self.readline = readline
562 self.readline = readline
560 else:
563 else:
561 self.readline = None
564 self.readline = None
562
565
563 # _greedy_changed() depends on splitter and readline being defined:
566 # _greedy_changed() depends on splitter and readline being defined:
564 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
567 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
565 config=config, **kwargs)
568 config=config, **kwargs)
566
569
567 # List where completion matches will be stored
570 # List where completion matches will be stored
568 self.matches = []
571 self.matches = []
569 self.shell = shell
572 self.shell = shell
570 # Regexp to split filenames with spaces in them
573 # Regexp to split filenames with spaces in them
571 self.space_name_re = re.compile(r'([^\\] )')
574 self.space_name_re = re.compile(r'([^\\] )')
572 # Hold a local ref. to glob.glob for speed
575 # Hold a local ref. to glob.glob for speed
573 self.glob = glob.glob
576 self.glob = glob.glob
574
577
575 # Determine if we are running on 'dumb' terminals, like (X)Emacs
578 # Determine if we are running on 'dumb' terminals, like (X)Emacs
576 # buffers, to avoid completion problems.
579 # buffers, to avoid completion problems.
577 term = os.environ.get('TERM','xterm')
580 term = os.environ.get('TERM','xterm')
578 self.dumb_terminal = term in ['dumb','emacs']
581 self.dumb_terminal = term in ['dumb','emacs']
579
582
580 # Special handling of backslashes needed in win32 platforms
583 # Special handling of backslashes needed in win32 platforms
581 if sys.platform == "win32":
584 if sys.platform == "win32":
582 self.clean_glob = self._clean_glob_win32
585 self.clean_glob = self._clean_glob_win32
583 else:
586 else:
584 self.clean_glob = self._clean_glob
587 self.clean_glob = self._clean_glob
585
588
586 #regexp to parse docstring for function signature
589 #regexp to parse docstring for function signature
587 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
590 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
588 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
591 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
589 #use this if positional argument name is also needed
592 #use this if positional argument name is also needed
590 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
593 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
591
594
592 # All active matcher routines for completion
595 # All active matcher routines for completion
593 self.matchers = [self.python_matches,
596 self.matchers = [self.python_matches,
594 self.file_matches,
597 self.file_matches,
595 self.magic_matches,
598 self.magic_matches,
596 self.python_func_kw_matches,
599 self.python_func_kw_matches,
597 self.dict_key_matches,
600 self.dict_key_matches,
598 ]
601 ]
599
602
600 def all_completions(self, text):
603 def all_completions(self, text):
601 """
604 """
602 Wrapper around the complete method for the benefit of emacs
605 Wrapper around the complete method for the benefit of emacs
603 and pydb.
606 and pydb.
604 """
607 """
605 return self.complete(text)[1]
608 return self.complete(text)[1]
606
609
607 def _clean_glob(self,text):
610 def _clean_glob(self,text):
608 return self.glob("%s*" % text)
611 return self.glob("%s*" % text)
609
612
610 def _clean_glob_win32(self,text):
613 def _clean_glob_win32(self,text):
611 return [f.replace("\\","/")
614 return [f.replace("\\","/")
612 for f in self.glob("%s*" % text)]
615 for f in self.glob("%s*" % text)]
613
616
614 def file_matches(self, text):
617 def file_matches(self, text):
615 """Match filenames, expanding ~USER type strings.
618 """Match filenames, expanding ~USER type strings.
616
619
617 Most of the seemingly convoluted logic in this completer is an
620 Most of the seemingly convoluted logic in this completer is an
618 attempt to handle filenames with spaces in them. And yet it's not
621 attempt to handle filenames with spaces in them. And yet it's not
619 quite perfect, because Python's readline doesn't expose all of the
622 quite perfect, because Python's readline doesn't expose all of the
620 GNU readline details needed for this to be done correctly.
623 GNU readline details needed for this to be done correctly.
621
624
622 For a filename with a space in it, the printed completions will be
625 For a filename with a space in it, the printed completions will be
623 only the parts after what's already been typed (instead of the
626 only the parts after what's already been typed (instead of the
624 full completions, as is normally done). I don't think with the
627 full completions, as is normally done). I don't think with the
625 current (as of Python 2.3) Python readline it's possible to do
628 current (as of Python 2.3) Python readline it's possible to do
626 better."""
629 better."""
627
630
628 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
631 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
629
632
630 # chars that require escaping with backslash - i.e. chars
633 # chars that require escaping with backslash - i.e. chars
631 # that readline treats incorrectly as delimiters, but we
634 # that readline treats incorrectly as delimiters, but we
632 # don't want to treat as delimiters in filename matching
635 # don't want to treat as delimiters in filename matching
633 # when escaped with backslash
636 # when escaped with backslash
634 if text.startswith('!'):
637 if text.startswith('!'):
635 text = text[1:]
638 text = text[1:]
636 text_prefix = '!'
639 text_prefix = '!'
637 else:
640 else:
638 text_prefix = ''
641 text_prefix = ''
639
642
640 text_until_cursor = self.text_until_cursor
643 text_until_cursor = self.text_until_cursor
641 # track strings with open quotes
644 # track strings with open quotes
642 open_quotes = has_open_quotes(text_until_cursor)
645 open_quotes = has_open_quotes(text_until_cursor)
643
646
644 if '(' in text_until_cursor or '[' in text_until_cursor:
647 if '(' in text_until_cursor or '[' in text_until_cursor:
645 lsplit = text
648 lsplit = text
646 else:
649 else:
647 try:
650 try:
648 # arg_split ~ shlex.split, but with unicode bugs fixed by us
651 # arg_split ~ shlex.split, but with unicode bugs fixed by us
649 lsplit = arg_split(text_until_cursor)[-1]
652 lsplit = arg_split(text_until_cursor)[-1]
650 except ValueError:
653 except ValueError:
651 # typically an unmatched ", or backslash without escaped char.
654 # typically an unmatched ", or backslash without escaped char.
652 if open_quotes:
655 if open_quotes:
653 lsplit = text_until_cursor.split(open_quotes)[-1]
656 lsplit = text_until_cursor.split(open_quotes)[-1]
654 else:
657 else:
655 return []
658 return []
656 except IndexError:
659 except IndexError:
657 # tab pressed on empty line
660 # tab pressed on empty line
658 lsplit = ""
661 lsplit = ""
659
662
660 if not open_quotes and lsplit != protect_filename(lsplit):
663 if not open_quotes and lsplit != protect_filename(lsplit):
661 # if protectables are found, do matching on the whole escaped name
664 # if protectables are found, do matching on the whole escaped name
662 has_protectables = True
665 has_protectables = True
663 text0,text = text,lsplit
666 text0,text = text,lsplit
664 else:
667 else:
665 has_protectables = False
668 has_protectables = False
666 text = os.path.expanduser(text)
669 text = os.path.expanduser(text)
667
670
668 if text == "":
671 if text == "":
669 return [text_prefix + protect_filename(f) for f in self.glob("*")]
672 return [text_prefix + protect_filename(f) for f in self.glob("*")]
670
673
671 # Compute the matches from the filesystem
674 # Compute the matches from the filesystem
672 m0 = self.clean_glob(text.replace('\\',''))
675 m0 = self.clean_glob(text.replace('\\',''))
673
676
674 if has_protectables:
677 if has_protectables:
675 # If we had protectables, we need to revert our changes to the
678 # If we had protectables, we need to revert our changes to the
676 # beginning of filename so that we don't double-write the part
679 # beginning of filename so that we don't double-write the part
677 # of the filename we have so far
680 # of the filename we have so far
678 len_lsplit = len(lsplit)
681 len_lsplit = len(lsplit)
679 matches = [text_prefix + text0 +
682 matches = [text_prefix + text0 +
680 protect_filename(f[len_lsplit:]) for f in m0]
683 protect_filename(f[len_lsplit:]) for f in m0]
681 else:
684 else:
682 if open_quotes:
685 if open_quotes:
683 # if we have a string with an open quote, we don't need to
686 # if we have a string with an open quote, we don't need to
684 # protect the names at all (and we _shouldn't_, as it
687 # protect the names at all (and we _shouldn't_, as it
685 # would cause bugs when the filesystem call is made).
688 # would cause bugs when the filesystem call is made).
686 matches = m0
689 matches = m0
687 else:
690 else:
688 matches = [text_prefix +
691 matches = [text_prefix +
689 protect_filename(f) for f in m0]
692 protect_filename(f) for f in m0]
690
693
691 #io.rprint('mm', matches) # dbg
694 #io.rprint('mm', matches) # dbg
692
695
693 # Mark directories in input list by appending '/' to their names.
696 # Mark directories in input list by appending '/' to their names.
694 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
697 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
695 return matches
698 return matches
696
699
697 def magic_matches(self, text):
700 def magic_matches(self, text):
698 """Match magics"""
701 """Match magics"""
699 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
702 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
700 # Get all shell magics now rather than statically, so magics loaded at
703 # Get all shell magics now rather than statically, so magics loaded at
701 # runtime show up too.
704 # runtime show up too.
702 lsm = self.shell.magics_manager.lsmagic()
705 lsm = self.shell.magics_manager.lsmagic()
703 line_magics = lsm['line']
706 line_magics = lsm['line']
704 cell_magics = lsm['cell']
707 cell_magics = lsm['cell']
705 pre = self.magic_escape
708 pre = self.magic_escape
706 pre2 = pre+pre
709 pre2 = pre+pre
707
710
708 # Completion logic:
711 # Completion logic:
709 # - user gives %%: only do cell magics
712 # - user gives %%: only do cell magics
710 # - user gives %: do both line and cell magics
713 # - user gives %: do both line and cell magics
711 # - no prefix: do both
714 # - no prefix: do both
712 # In other words, line magics are skipped if the user gives %% explicitly
715 # In other words, line magics are skipped if the user gives %% explicitly
713 bare_text = text.lstrip(pre)
716 bare_text = text.lstrip(pre)
714 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
717 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
715 if not text.startswith(pre2):
718 if not text.startswith(pre2):
716 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
719 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
717 return comp
720 return comp
718
721
719 def python_matches(self,text):
722 def python_matches(self,text):
720 """Match attributes or global python names"""
723 """Match attributes or global python names"""
721
724
722 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
725 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
723 if "." in text:
726 if "." in text:
724 try:
727 try:
725 matches = self.attr_matches(text)
728 matches = self.attr_matches(text)
726 if text.endswith('.') and self.omit__names:
729 if text.endswith('.') and self.omit__names:
727 if self.omit__names == 1:
730 if self.omit__names == 1:
728 # true if txt is _not_ a __ name, false otherwise:
731 # true if txt is _not_ a __ name, false otherwise:
729 no__name = (lambda txt:
732 no__name = (lambda txt:
730 re.match(r'.*\.__.*?__',txt) is None)
733 re.match(r'.*\.__.*?__',txt) is None)
731 else:
734 else:
732 # true if txt is _not_ a _ name, false otherwise:
735 # true if txt is _not_ a _ name, false otherwise:
733 no__name = (lambda txt:
736 no__name = (lambda txt:
734 re.match(r'.*\._.*?',txt) is None)
737 re.match(r'.*\._.*?',txt) is None)
735 matches = filter(no__name, matches)
738 matches = filter(no__name, matches)
736 except NameError:
739 except NameError:
737 # catches <undefined attributes>.<tab>
740 # catches <undefined attributes>.<tab>
738 matches = []
741 matches = []
739 else:
742 else:
740 matches = self.global_matches(text)
743 matches = self.global_matches(text)
741
744
742 return matches
745 return matches
743
746
744 def _default_arguments_from_docstring(self, doc):
747 def _default_arguments_from_docstring(self, doc):
745 """Parse the first line of docstring for call signature.
748 """Parse the first line of docstring for call signature.
746
749
747 Docstring should be of the form 'min(iterable[, key=func])\n'.
750 Docstring should be of the form 'min(iterable[, key=func])\n'.
748 It can also parse cython docstring of the form
751 It can also parse cython docstring of the form
749 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
752 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
750 """
753 """
751 if doc is None:
754 if doc is None:
752 return []
755 return []
753
756
754 #care only the firstline
757 #care only the firstline
755 line = doc.lstrip().splitlines()[0]
758 line = doc.lstrip().splitlines()[0]
756
759
757 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
760 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
758 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
761 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
759 sig = self.docstring_sig_re.search(line)
762 sig = self.docstring_sig_re.search(line)
760 if sig is None:
763 if sig is None:
761 return []
764 return []
762 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
765 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
763 sig = sig.groups()[0].split(',')
766 sig = sig.groups()[0].split(',')
764 ret = []
767 ret = []
765 for s in sig:
768 for s in sig:
766 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
769 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
767 ret += self.docstring_kwd_re.findall(s)
770 ret += self.docstring_kwd_re.findall(s)
768 return ret
771 return ret
769
772
770 def _default_arguments(self, obj):
773 def _default_arguments(self, obj):
771 """Return the list of default arguments of obj if it is callable,
774 """Return the list of default arguments of obj if it is callable,
772 or empty list otherwise."""
775 or empty list otherwise."""
773 call_obj = obj
776 call_obj = obj
774 ret = []
777 ret = []
775 if inspect.isbuiltin(obj):
778 if inspect.isbuiltin(obj):
776 pass
779 pass
777 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
780 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
778 if inspect.isclass(obj):
781 if inspect.isclass(obj):
779 #for cython embededsignature=True the constructor docstring
782 #for cython embededsignature=True the constructor docstring
780 #belongs to the object itself not __init__
783 #belongs to the object itself not __init__
781 ret += self._default_arguments_from_docstring(
784 ret += self._default_arguments_from_docstring(
782 getattr(obj, '__doc__', ''))
785 getattr(obj, '__doc__', ''))
783 # for classes, check for __init__,__new__
786 # for classes, check for __init__,__new__
784 call_obj = (getattr(obj, '__init__', None) or
787 call_obj = (getattr(obj, '__init__', None) or
785 getattr(obj, '__new__', None))
788 getattr(obj, '__new__', None))
786 # for all others, check if they are __call__able
789 # for all others, check if they are __call__able
787 elif hasattr(obj, '__call__'):
790 elif hasattr(obj, '__call__'):
788 call_obj = obj.__call__
791 call_obj = obj.__call__
789
792
790 ret += self._default_arguments_from_docstring(
793 ret += self._default_arguments_from_docstring(
791 getattr(call_obj, '__doc__', ''))
794 getattr(call_obj, '__doc__', ''))
792
795
793 try:
796 try:
794 args,_,_1,defaults = inspect.getargspec(call_obj)
797 args,_,_1,defaults = inspect.getargspec(call_obj)
795 if defaults:
798 if defaults:
796 ret+=args[-len(defaults):]
799 ret+=args[-len(defaults):]
797 except TypeError:
800 except TypeError:
798 pass
801 pass
799
802
800 return list(set(ret))
803 return list(set(ret))
801
804
802 def python_func_kw_matches(self,text):
805 def python_func_kw_matches(self,text):
803 """Match named parameters (kwargs) of the last open function"""
806 """Match named parameters (kwargs) of the last open function"""
804
807
805 if "." in text: # a parameter cannot be dotted
808 if "." in text: # a parameter cannot be dotted
806 return []
809 return []
807 try: regexp = self.__funcParamsRegex
810 try: regexp = self.__funcParamsRegex
808 except AttributeError:
811 except AttributeError:
809 regexp = self.__funcParamsRegex = re.compile(r'''
812 regexp = self.__funcParamsRegex = re.compile(r'''
810 '.*?(?<!\\)' | # single quoted strings or
813 '.*?(?<!\\)' | # single quoted strings or
811 ".*?(?<!\\)" | # double quoted strings or
814 ".*?(?<!\\)" | # double quoted strings or
812 \w+ | # identifier
815 \w+ | # identifier
813 \S # other characters
816 \S # other characters
814 ''', re.VERBOSE | re.DOTALL)
817 ''', re.VERBOSE | re.DOTALL)
815 # 1. find the nearest identifier that comes before an unclosed
818 # 1. find the nearest identifier that comes before an unclosed
816 # parenthesis before the cursor
819 # parenthesis before the cursor
817 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
820 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
818 tokens = regexp.findall(self.text_until_cursor)
821 tokens = regexp.findall(self.text_until_cursor)
819 tokens.reverse()
822 tokens.reverse()
820 iterTokens = iter(tokens); openPar = 0
823 iterTokens = iter(tokens); openPar = 0
821
824
822 for token in iterTokens:
825 for token in iterTokens:
823 if token == ')':
826 if token == ')':
824 openPar -= 1
827 openPar -= 1
825 elif token == '(':
828 elif token == '(':
826 openPar += 1
829 openPar += 1
827 if openPar > 0:
830 if openPar > 0:
828 # found the last unclosed parenthesis
831 # found the last unclosed parenthesis
829 break
832 break
830 else:
833 else:
831 return []
834 return []
832 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
835 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
833 ids = []
836 ids = []
834 isId = re.compile(r'\w+$').match
837 isId = re.compile(r'\w+$').match
835
838
836 while True:
839 while True:
837 try:
840 try:
838 ids.append(next(iterTokens))
841 ids.append(next(iterTokens))
839 if not isId(ids[-1]):
842 if not isId(ids[-1]):
840 ids.pop(); break
843 ids.pop(); break
841 if not next(iterTokens) == '.':
844 if not next(iterTokens) == '.':
842 break
845 break
843 except StopIteration:
846 except StopIteration:
844 break
847 break
845 # lookup the candidate callable matches either using global_matches
848 # lookup the candidate callable matches either using global_matches
846 # or attr_matches for dotted names
849 # or attr_matches for dotted names
847 if len(ids) == 1:
850 if len(ids) == 1:
848 callableMatches = self.global_matches(ids[0])
851 callableMatches = self.global_matches(ids[0])
849 else:
852 else:
850 callableMatches = self.attr_matches('.'.join(ids[::-1]))
853 callableMatches = self.attr_matches('.'.join(ids[::-1]))
851 argMatches = []
854 argMatches = []
852 for callableMatch in callableMatches:
855 for callableMatch in callableMatches:
853 try:
856 try:
854 namedArgs = self._default_arguments(eval(callableMatch,
857 namedArgs = self._default_arguments(eval(callableMatch,
855 self.namespace))
858 self.namespace))
856 except:
859 except:
857 continue
860 continue
858
861
859 for namedArg in namedArgs:
862 for namedArg in namedArgs:
860 if namedArg.startswith(text):
863 if namedArg.startswith(text):
861 argMatches.append("%s=" %namedArg)
864 argMatches.append("%s=" %namedArg)
862 return argMatches
865 return argMatches
863
866
864 def dict_key_matches(self, text):
867 def dict_key_matches(self, text):
865 def get_keys(obj):
868 def get_keys(obj):
866 # Only allow completion for known in-memory dict-like types
869 # Only allow completion for known in-memory dict-like types
867 if isinstance(obj, dict) or\
870 if isinstance(obj, dict) or\
868 _safe_isinstance(obj, 'pandas', 'DataFrame'):
871 _safe_isinstance(obj, 'pandas', 'DataFrame'):
869 try:
872 try:
870 return list(obj.keys())
873 return list(obj.keys())
871 except Exception:
874 except Exception:
872 return []
875 return []
873 elif _safe_isinstance(obj, 'numpy', 'ndarray'):
876 elif _safe_isinstance(obj, 'numpy', 'ndarray'):
874 return obj.dtype.names or []
877 return obj.dtype.names or []
875 return []
878 return []
876
879
877 try:
880 try:
878 regexps = self.__dict_key_regexps
881 regexps = self.__dict_key_regexps
879 except AttributeError:
882 except AttributeError:
880 dict_key_re_fmt = r'''(?x)
883 dict_key_re_fmt = r'''(?x)
881 ( # match dict-referring expression wrt greedy setting
884 ( # match dict-referring expression wrt greedy setting
882 %s
885 %s
883 )
886 )
884 \[ # open bracket
887 \[ # open bracket
885 \s* # and optional whitespace
888 \s* # and optional whitespace
886 ([uUbB]? # string prefix (r not handled)
889 ([uUbB]? # string prefix (r not handled)
887 (?: # unclosed string
890 (?: # unclosed string
888 '(?:[^']|(?<!\\)\\')*
891 '(?:[^']|(?<!\\)\\')*
889 |
892 |
890 "(?:[^"]|(?<!\\)\\")*
893 "(?:[^"]|(?<!\\)\\")*
891 )
894 )
892 )?
895 )?
893 $
896 $
894 '''
897 '''
895 regexps = self.__dict_key_regexps = {
898 regexps = self.__dict_key_regexps = {
896 False: re.compile(dict_key_re_fmt % '''
899 False: re.compile(dict_key_re_fmt % '''
897 # identifiers separated by .
900 # identifiers separated by .
898 (?!\d)\w+
901 (?!\d)\w+
899 (?:\.(?!\d)\w+)*
902 (?:\.(?!\d)\w+)*
900 '''),
903 '''),
901 True: re.compile(dict_key_re_fmt % '''
904 True: re.compile(dict_key_re_fmt % '''
902 .+
905 .+
903 ''')
906 ''')
904 }
907 }
905
908
906 match = regexps[self.greedy].search(self.text_until_cursor)
909 match = regexps[self.greedy].search(self.text_until_cursor)
907 if match is None:
910 if match is None:
908 return []
911 return []
909
912
910 expr, prefix = match.groups()
913 expr, prefix = match.groups()
911 try:
914 try:
912 obj = eval(expr, self.namespace)
915 obj = eval(expr, self.namespace)
913 except Exception:
916 except Exception:
914 try:
917 try:
915 obj = eval(expr, self.global_namespace)
918 obj = eval(expr, self.global_namespace)
916 except Exception:
919 except Exception:
917 return []
920 return []
918
921
919 keys = get_keys(obj)
922 keys = get_keys(obj)
920 if not keys:
923 if not keys:
921 return keys
924 return keys
922 closing_quote, matches = match_dict_keys(keys, prefix)
925 closing_quote, token_offset, matches = match_dict_keys(keys, prefix)
926 if not matches:
927 return matches
928
929 # get the cursor position of
930 # - the text being completed
931 # - the start of the key text
932 # - the start of the completion
933 text_start = len(self.text_until_cursor) - len(text)
934 if prefix:
935 key_start = match.start(2)
936 completion_start = key_start + token_offset
937 else:
938 key_start = completion_start = match.end()
939
940 # grab the leading prefix, to make sure all completions start with `text`
941 if text_start > key_start:
942 leading = ''
943 else:
944 leading = text[text_start:completion_start]
945
946 # the index of the `[` character
947 bracket_idx = match.end(1)
923
948
924 # append closing quote and bracket as appropriate
949 # append closing quote and bracket as appropriate
950 # this is *not* appropriate if the opening quote or bracket is outside
951 # the text given to this method
952 suf = ''
925 continuation = self.line_buffer[len(self.text_until_cursor):]
953 continuation = self.line_buffer[len(self.text_until_cursor):]
926 if closing_quote and continuation.startswith(closing_quote):
954 if key_start > text_start and closing_quote:
927 suf = ''
955 # quotes were opened inside text, maybe close them
928 elif continuation.startswith(']'):
956 if continuation.startswith(closing_quote):
929 suf = closing_quote or ''
957 continuation = continuation[len(closing_quote):]
930 else:
958 else:
931 suf = (closing_quote or '') + ']'
959 suf += closing_quote
932 return [k + suf for k in matches]
960 if bracket_idx > text_start:
961 # brackets were opened inside text, maybe close them
962 if not continuation.startswith(']'):
963 suf += ']'
964
965 return [leading + k + suf for k in matches]
933
966
934 def dispatch_custom_completer(self, text):
967 def dispatch_custom_completer(self, text):
935 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
968 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
936 line = self.line_buffer
969 line = self.line_buffer
937 if not line.strip():
970 if not line.strip():
938 return None
971 return None
939
972
940 # Create a little structure to pass all the relevant information about
973 # Create a little structure to pass all the relevant information about
941 # the current completion to any custom completer.
974 # the current completion to any custom completer.
942 event = Bunch()
975 event = Bunch()
943 event.line = line
976 event.line = line
944 event.symbol = text
977 event.symbol = text
945 cmd = line.split(None,1)[0]
978 cmd = line.split(None,1)[0]
946 event.command = cmd
979 event.command = cmd
947 event.text_until_cursor = self.text_until_cursor
980 event.text_until_cursor = self.text_until_cursor
948
981
949 #print "\ncustom:{%s]\n" % event # dbg
982 #print "\ncustom:{%s]\n" % event # dbg
950
983
951 # for foo etc, try also to find completer for %foo
984 # for foo etc, try also to find completer for %foo
952 if not cmd.startswith(self.magic_escape):
985 if not cmd.startswith(self.magic_escape):
953 try_magic = self.custom_completers.s_matches(
986 try_magic = self.custom_completers.s_matches(
954 self.magic_escape + cmd)
987 self.magic_escape + cmd)
955 else:
988 else:
956 try_magic = []
989 try_magic = []
957
990
958 for c in itertools.chain(self.custom_completers.s_matches(cmd),
991 for c in itertools.chain(self.custom_completers.s_matches(cmd),
959 try_magic,
992 try_magic,
960 self.custom_completers.flat_matches(self.text_until_cursor)):
993 self.custom_completers.flat_matches(self.text_until_cursor)):
961 #print "try",c # dbg
994 #print "try",c # dbg
962 try:
995 try:
963 res = c(event)
996 res = c(event)
964 if res:
997 if res:
965 # first, try case sensitive match
998 # first, try case sensitive match
966 withcase = [r for r in res if r.startswith(text)]
999 withcase = [r for r in res if r.startswith(text)]
967 if withcase:
1000 if withcase:
968 return withcase
1001 return withcase
969 # if none, then case insensitive ones are ok too
1002 # if none, then case insensitive ones are ok too
970 text_low = text.lower()
1003 text_low = text.lower()
971 return [r for r in res if r.lower().startswith(text_low)]
1004 return [r for r in res if r.lower().startswith(text_low)]
972 except TryNext:
1005 except TryNext:
973 pass
1006 pass
974
1007
975 return None
1008 return None
976
1009
977 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1010 def complete(self, text=None, line_buffer=None, cursor_pos=None):
978 """Find completions for the given text and line context.
1011 """Find completions for the given text and line context.
979
1012
980 This is called successively with state == 0, 1, 2, ... until it
1013 This is called successively with state == 0, 1, 2, ... until it
981 returns None. The completion should begin with 'text'.
1014 returns None. The completion should begin with 'text'.
982
1015
983 Note that both the text and the line_buffer are optional, but at least
1016 Note that both the text and the line_buffer are optional, but at least
984 one of them must be given.
1017 one of them must be given.
985
1018
986 Parameters
1019 Parameters
987 ----------
1020 ----------
988 text : string, optional
1021 text : string, optional
989 Text to perform the completion on. If not given, the line buffer
1022 Text to perform the completion on. If not given, the line buffer
990 is split using the instance's CompletionSplitter object.
1023 is split using the instance's CompletionSplitter object.
991
1024
992 line_buffer : string, optional
1025 line_buffer : string, optional
993 If not given, the completer attempts to obtain the current line
1026 If not given, the completer attempts to obtain the current line
994 buffer via readline. This keyword allows clients which are
1027 buffer via readline. This keyword allows clients which are
995 requesting for text completions in non-readline contexts to inform
1028 requesting for text completions in non-readline contexts to inform
996 the completer of the entire text.
1029 the completer of the entire text.
997
1030
998 cursor_pos : int, optional
1031 cursor_pos : int, optional
999 Index of the cursor in the full line buffer. Should be provided by
1032 Index of the cursor in the full line buffer. Should be provided by
1000 remote frontends where kernel has no access to frontend state.
1033 remote frontends where kernel has no access to frontend state.
1001
1034
1002 Returns
1035 Returns
1003 -------
1036 -------
1004 text : str
1037 text : str
1005 Text that was actually used in the completion.
1038 Text that was actually used in the completion.
1006
1039
1007 matches : list
1040 matches : list
1008 A list of completion matches.
1041 A list of completion matches.
1009 """
1042 """
1010 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1043 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1011
1044
1012 # if the cursor position isn't given, the only sane assumption we can
1045 # if the cursor position isn't given, the only sane assumption we can
1013 # make is that it's at the end of the line (the common case)
1046 # make is that it's at the end of the line (the common case)
1014 if cursor_pos is None:
1047 if cursor_pos is None:
1015 cursor_pos = len(line_buffer) if text is None else len(text)
1048 cursor_pos = len(line_buffer) if text is None else len(text)
1016
1049
1017 # if text is either None or an empty string, rely on the line buffer
1050 # if text is either None or an empty string, rely on the line buffer
1018 if not text:
1051 if not text:
1019 text = self.splitter.split_line(line_buffer, cursor_pos)
1052 text = self.splitter.split_line(line_buffer, cursor_pos)
1020
1053
1021 # If no line buffer is given, assume the input text is all there was
1054 # If no line buffer is given, assume the input text is all there was
1022 if line_buffer is None:
1055 if line_buffer is None:
1023 line_buffer = text
1056 line_buffer = text
1024
1057
1025 self.line_buffer = line_buffer
1058 self.line_buffer = line_buffer
1026 self.text_until_cursor = self.line_buffer[:cursor_pos]
1059 self.text_until_cursor = self.line_buffer[:cursor_pos]
1027 #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1060 #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1028
1061
1029 # Start with a clean slate of completions
1062 # Start with a clean slate of completions
1030 self.matches[:] = []
1063 self.matches[:] = []
1031 custom_res = self.dispatch_custom_completer(text)
1064 custom_res = self.dispatch_custom_completer(text)
1032 if custom_res is not None:
1065 if custom_res is not None:
1033 # did custom completers produce something?
1066 # did custom completers produce something?
1034 self.matches = custom_res
1067 self.matches = custom_res
1035 else:
1068 else:
1036 # Extend the list of completions with the results of each
1069 # Extend the list of completions with the results of each
1037 # matcher, so we return results to the user from all
1070 # matcher, so we return results to the user from all
1038 # namespaces.
1071 # namespaces.
1039 if self.merge_completions:
1072 if self.merge_completions:
1040 self.matches = []
1073 self.matches = []
1041 for matcher in self.matchers:
1074 for matcher in self.matchers:
1042 try:
1075 try:
1043 self.matches.extend(matcher(text))
1076 self.matches.extend(matcher(text))
1044 except:
1077 except:
1045 # Show the ugly traceback if the matcher causes an
1078 # Show the ugly traceback if the matcher causes an
1046 # exception, but do NOT crash the kernel!
1079 # exception, but do NOT crash the kernel!
1047 sys.excepthook(*sys.exc_info())
1080 sys.excepthook(*sys.exc_info())
1048 else:
1081 else:
1049 for matcher in self.matchers:
1082 for matcher in self.matchers:
1050 self.matches = matcher(text)
1083 self.matches = matcher(text)
1051 if self.matches:
1084 if self.matches:
1052 break
1085 break
1053 # FIXME: we should extend our api to return a dict with completions for
1086 # FIXME: we should extend our api to return a dict with completions for
1054 # different types of objects. The rlcomplete() method could then
1087 # different types of objects. The rlcomplete() method could then
1055 # simply collapse the dict into a list for readline, but we'd have
1088 # simply collapse the dict into a list for readline, but we'd have
1056 # richer completion semantics in other evironments.
1089 # richer completion semantics in other evironments.
1057
1090
1058 # use penalize_magics_key to put magics after variables with same name
1091 # use penalize_magics_key to put magics after variables with same name
1059 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1092 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1060
1093
1061 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1094 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1062 return text, self.matches
1095 return text, self.matches
1063
1096
1064 def rlcomplete(self, text, state):
1097 def rlcomplete(self, text, state):
1065 """Return the state-th possible completion for 'text'.
1098 """Return the state-th possible completion for 'text'.
1066
1099
1067 This is called successively with state == 0, 1, 2, ... until it
1100 This is called successively with state == 0, 1, 2, ... until it
1068 returns None. The completion should begin with 'text'.
1101 returns None. The completion should begin with 'text'.
1069
1102
1070 Parameters
1103 Parameters
1071 ----------
1104 ----------
1072 text : string
1105 text : string
1073 Text to perform the completion on.
1106 Text to perform the completion on.
1074
1107
1075 state : int
1108 state : int
1076 Counter used by readline.
1109 Counter used by readline.
1077 """
1110 """
1078 if state==0:
1111 if state==0:
1079
1112
1080 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1113 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1081 cursor_pos = self.readline.get_endidx()
1114 cursor_pos = self.readline.get_endidx()
1082
1115
1083 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1116 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1084 # (text, line_buffer, cursor_pos) ) # dbg
1117 # (text, line_buffer, cursor_pos) ) # dbg
1085
1118
1086 # if there is only a tab on a line with only whitespace, instead of
1119 # if there is only a tab on a line with only whitespace, instead of
1087 # the mostly useless 'do you want to see all million completions'
1120 # the mostly useless 'do you want to see all million completions'
1088 # message, just do the right thing and give the user his tab!
1121 # message, just do the right thing and give the user his tab!
1089 # Incidentally, this enables pasting of tabbed text from an editor
1122 # Incidentally, this enables pasting of tabbed text from an editor
1090 # (as long as autoindent is off).
1123 # (as long as autoindent is off).
1091
1124
1092 # It should be noted that at least pyreadline still shows file
1125 # It should be noted that at least pyreadline still shows file
1093 # completions - is there a way around it?
1126 # completions - is there a way around it?
1094
1127
1095 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1128 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1096 # we don't interfere with their own tab-completion mechanism.
1129 # we don't interfere with their own tab-completion mechanism.
1097 if not (self.dumb_terminal or line_buffer.strip()):
1130 if not (self.dumb_terminal or line_buffer.strip()):
1098 self.readline.insert_text('\t')
1131 self.readline.insert_text('\t')
1099 sys.stdout.flush()
1132 sys.stdout.flush()
1100 return None
1133 return None
1101
1134
1102 # Note: debugging exceptions that may occur in completion is very
1135 # Note: debugging exceptions that may occur in completion is very
1103 # tricky, because readline unconditionally silences them. So if
1136 # tricky, because readline unconditionally silences them. So if
1104 # during development you suspect a bug in the completion code, turn
1137 # during development you suspect a bug in the completion code, turn
1105 # this flag on temporarily by uncommenting the second form (don't
1138 # this flag on temporarily by uncommenting the second form (don't
1106 # flip the value in the first line, as the '# dbg' marker can be
1139 # flip the value in the first line, as the '# dbg' marker can be
1107 # automatically detected and is used elsewhere).
1140 # automatically detected and is used elsewhere).
1108 DEBUG = False
1141 DEBUG = False
1109 #DEBUG = True # dbg
1142 #DEBUG = True # dbg
1110 if DEBUG:
1143 if DEBUG:
1111 try:
1144 try:
1112 self.complete(text, line_buffer, cursor_pos)
1145 self.complete(text, line_buffer, cursor_pos)
1113 except:
1146 except:
1114 import traceback; traceback.print_exc()
1147 import traceback; traceback.print_exc()
1115 else:
1148 else:
1116 # The normal production version is here
1149 # The normal production version is here
1117
1150
1118 # This method computes the self.matches array
1151 # This method computes the self.matches array
1119 self.complete(text, line_buffer, cursor_pos)
1152 self.complete(text, line_buffer, cursor_pos)
1120
1153
1121 try:
1154 try:
1122 return self.matches[state]
1155 return self.matches[state]
1123 except IndexError:
1156 except IndexError:
1124 return None
1157 return None
@@ -1,627 +1,683 b''
1 """Tests for the IPython tab-completion machinery.
1 # encoding: utf-8
2 """
2 """Tests for the IPython tab-completion machinery."""
3 #-----------------------------------------------------------------------------
3
4 # Module imports
4 # Copyright (c) IPython Development Team.
5 #-----------------------------------------------------------------------------
5 # Distributed under the terms of the Modified BSD License.
6
6
7 # stdlib
8 import os
7 import os
9 import sys
8 import sys
10 import unittest
9 import unittest
11
10
12 # third party
11 from contextlib import contextmanager
12
13 import nose.tools as nt
13 import nose.tools as nt
14
14
15 # our own packages
16 from IPython.config.loader import Config
15 from IPython.config.loader import Config
17 from IPython.core import completer
16 from IPython.core import completer
18 from IPython.external.decorators import knownfailureif
17 from IPython.external.decorators import knownfailureif
19 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.utils.generics import complete_object
19 from IPython.utils.generics import complete_object
21 from IPython.utils import py3compat
20 from IPython.utils import py3compat
22 from IPython.utils.py3compat import string_types, unicode_type
21 from IPython.utils.py3compat import string_types, unicode_type
23 from IPython.testing import decorators as dec
22 from IPython.testing import decorators as dec
24
23
25 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
26 # Test functions
25 # Test functions
27 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
28 @contextmanager
29 def greedy_completion():
30 ip = get_ipython()
31 greedy_original = ip.Completer.greedy
32 try:
33 ip.Completer.greedy = True
34 yield
35 finally:
36 ip.Completer.greedy = greedy_original
37
28 def test_protect_filename():
38 def test_protect_filename():
29 pairs = [ ('abc','abc'),
39 pairs = [ ('abc','abc'),
30 (' abc',r'\ abc'),
40 (' abc',r'\ abc'),
31 ('a bc',r'a\ bc'),
41 ('a bc',r'a\ bc'),
32 ('a bc',r'a\ \ bc'),
42 ('a bc',r'a\ \ bc'),
33 (' bc',r'\ \ bc'),
43 (' bc',r'\ \ bc'),
34 ]
44 ]
35 # On posix, we also protect parens and other special characters
45 # On posix, we also protect parens and other special characters
36 if sys.platform != 'win32':
46 if sys.platform != 'win32':
37 pairs.extend( [('a(bc',r'a\(bc'),
47 pairs.extend( [('a(bc',r'a\(bc'),
38 ('a)bc',r'a\)bc'),
48 ('a)bc',r'a\)bc'),
39 ('a( )bc',r'a\(\ \)bc'),
49 ('a( )bc',r'a\(\ \)bc'),
40 ('a[1]bc', r'a\[1\]bc'),
50 ('a[1]bc', r'a\[1\]bc'),
41 ('a{1}bc', r'a\{1\}bc'),
51 ('a{1}bc', r'a\{1\}bc'),
42 ('a#bc', r'a\#bc'),
52 ('a#bc', r'a\#bc'),
43 ('a?bc', r'a\?bc'),
53 ('a?bc', r'a\?bc'),
44 ('a=bc', r'a\=bc'),
54 ('a=bc', r'a\=bc'),
45 ('a\\bc', r'a\\bc'),
55 ('a\\bc', r'a\\bc'),
46 ('a|bc', r'a\|bc'),
56 ('a|bc', r'a\|bc'),
47 ('a;bc', r'a\;bc'),
57 ('a;bc', r'a\;bc'),
48 ('a:bc', r'a\:bc'),
58 ('a:bc', r'a\:bc'),
49 ("a'bc", r"a\'bc"),
59 ("a'bc", r"a\'bc"),
50 ('a*bc', r'a\*bc'),
60 ('a*bc', r'a\*bc'),
51 ('a"bc', r'a\"bc'),
61 ('a"bc', r'a\"bc'),
52 ('a^bc', r'a\^bc'),
62 ('a^bc', r'a\^bc'),
53 ('a&bc', r'a\&bc'),
63 ('a&bc', r'a\&bc'),
54 ] )
64 ] )
55 # run the actual tests
65 # run the actual tests
56 for s1, s2 in pairs:
66 for s1, s2 in pairs:
57 s1p = completer.protect_filename(s1)
67 s1p = completer.protect_filename(s1)
58 nt.assert_equal(s1p, s2)
68 nt.assert_equal(s1p, s2)
59
69
60
70
61 def check_line_split(splitter, test_specs):
71 def check_line_split(splitter, test_specs):
62 for part1, part2, split in test_specs:
72 for part1, part2, split in test_specs:
63 cursor_pos = len(part1)
73 cursor_pos = len(part1)
64 line = part1+part2
74 line = part1+part2
65 out = splitter.split_line(line, cursor_pos)
75 out = splitter.split_line(line, cursor_pos)
66 nt.assert_equal(out, split)
76 nt.assert_equal(out, split)
67
77
68
78
69 def test_line_split():
79 def test_line_split():
70 """Basic line splitter test with default specs."""
80 """Basic line splitter test with default specs."""
71 sp = completer.CompletionSplitter()
81 sp = completer.CompletionSplitter()
72 # The format of the test specs is: part1, part2, expected answer. Parts 1
82 # The format of the test specs is: part1, part2, expected answer. Parts 1
73 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
83 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
74 # was at the end of part1. So an empty part2 represents someone hitting
84 # was at the end of part1. So an empty part2 represents someone hitting
75 # tab at the end of the line, the most common case.
85 # tab at the end of the line, the most common case.
76 t = [('run some/scrip', '', 'some/scrip'),
86 t = [('run some/scrip', '', 'some/scrip'),
77 ('run scripts/er', 'ror.py foo', 'scripts/er'),
87 ('run scripts/er', 'ror.py foo', 'scripts/er'),
78 ('echo $HOM', '', 'HOM'),
88 ('echo $HOM', '', 'HOM'),
79 ('print sys.pa', '', 'sys.pa'),
89 ('print sys.pa', '', 'sys.pa'),
80 ('print(sys.pa', '', 'sys.pa'),
90 ('print(sys.pa', '', 'sys.pa'),
81 ("execfile('scripts/er", '', 'scripts/er'),
91 ("execfile('scripts/er", '', 'scripts/er'),
82 ('a[x.', '', 'x.'),
92 ('a[x.', '', 'x.'),
83 ('a[x.', 'y', 'x.'),
93 ('a[x.', 'y', 'x.'),
84 ('cd "some_file/', '', 'some_file/'),
94 ('cd "some_file/', '', 'some_file/'),
85 ]
95 ]
86 check_line_split(sp, t)
96 check_line_split(sp, t)
87 # Ensure splitting works OK with unicode by re-running the tests with
97 # Ensure splitting works OK with unicode by re-running the tests with
88 # all inputs turned into unicode
98 # all inputs turned into unicode
89 check_line_split(sp, [ map(unicode_type, p) for p in t] )
99 check_line_split(sp, [ map(unicode_type, p) for p in t] )
90
100
91
101
92 def test_custom_completion_error():
102 def test_custom_completion_error():
93 """Test that errors from custom attribute completers are silenced."""
103 """Test that errors from custom attribute completers are silenced."""
94 ip = get_ipython()
104 ip = get_ipython()
95 class A(object): pass
105 class A(object): pass
96 ip.user_ns['a'] = A()
106 ip.user_ns['a'] = A()
97
107
98 @complete_object.when_type(A)
108 @complete_object.when_type(A)
99 def complete_A(a, existing_completions):
109 def complete_A(a, existing_completions):
100 raise TypeError("this should be silenced")
110 raise TypeError("this should be silenced")
101
111
102 ip.complete("a.")
112 ip.complete("a.")
103
113
104
114
105 def test_unicode_completions():
115 def test_unicode_completions():
106 ip = get_ipython()
116 ip = get_ipython()
107 # Some strings that trigger different types of completion. Check them both
117 # Some strings that trigger different types of completion. Check them both
108 # in str and unicode forms
118 # in str and unicode forms
109 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
119 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
110 for t in s + list(map(unicode_type, s)):
120 for t in s + list(map(unicode_type, s)):
111 # We don't need to check exact completion values (they may change
121 # We don't need to check exact completion values (they may change
112 # depending on the state of the namespace, but at least no exceptions
122 # depending on the state of the namespace, but at least no exceptions
113 # should be thrown and the return value should be a pair of text, list
123 # should be thrown and the return value should be a pair of text, list
114 # values.
124 # values.
115 text, matches = ip.complete(t)
125 text, matches = ip.complete(t)
116 nt.assert_true(isinstance(text, string_types))
126 nt.assert_true(isinstance(text, string_types))
117 nt.assert_true(isinstance(matches, list))
127 nt.assert_true(isinstance(matches, list))
118
128
119
129
120 class CompletionSplitterTestCase(unittest.TestCase):
130 class CompletionSplitterTestCase(unittest.TestCase):
121 def setUp(self):
131 def setUp(self):
122 self.sp = completer.CompletionSplitter()
132 self.sp = completer.CompletionSplitter()
123
133
124 def test_delim_setting(self):
134 def test_delim_setting(self):
125 self.sp.delims = ' '
135 self.sp.delims = ' '
126 nt.assert_equal(self.sp.delims, ' ')
136 nt.assert_equal(self.sp.delims, ' ')
127 nt.assert_equal(self.sp._delim_expr, '[\ ]')
137 nt.assert_equal(self.sp._delim_expr, '[\ ]')
128
138
129 def test_spaces(self):
139 def test_spaces(self):
130 """Test with only spaces as split chars."""
140 """Test with only spaces as split chars."""
131 self.sp.delims = ' '
141 self.sp.delims = ' '
132 t = [('foo', '', 'foo'),
142 t = [('foo', '', 'foo'),
133 ('run foo', '', 'foo'),
143 ('run foo', '', 'foo'),
134 ('run foo', 'bar', 'foo'),
144 ('run foo', 'bar', 'foo'),
135 ]
145 ]
136 check_line_split(self.sp, t)
146 check_line_split(self.sp, t)
137
147
138
148
139 def test_has_open_quotes1():
149 def test_has_open_quotes1():
140 for s in ["'", "'''", "'hi' '"]:
150 for s in ["'", "'''", "'hi' '"]:
141 nt.assert_equal(completer.has_open_quotes(s), "'")
151 nt.assert_equal(completer.has_open_quotes(s), "'")
142
152
143
153
144 def test_has_open_quotes2():
154 def test_has_open_quotes2():
145 for s in ['"', '"""', '"hi" "']:
155 for s in ['"', '"""', '"hi" "']:
146 nt.assert_equal(completer.has_open_quotes(s), '"')
156 nt.assert_equal(completer.has_open_quotes(s), '"')
147
157
148
158
149 def test_has_open_quotes3():
159 def test_has_open_quotes3():
150 for s in ["''", "''' '''", "'hi' 'ipython'"]:
160 for s in ["''", "''' '''", "'hi' 'ipython'"]:
151 nt.assert_false(completer.has_open_quotes(s))
161 nt.assert_false(completer.has_open_quotes(s))
152
162
153
163
154 def test_has_open_quotes4():
164 def test_has_open_quotes4():
155 for s in ['""', '""" """', '"hi" "ipython"']:
165 for s in ['""', '""" """', '"hi" "ipython"']:
156 nt.assert_false(completer.has_open_quotes(s))
166 nt.assert_false(completer.has_open_quotes(s))
157
167
158
168
159 @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows")
169 @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows")
160 def test_abspath_file_completions():
170 def test_abspath_file_completions():
161 ip = get_ipython()
171 ip = get_ipython()
162 with TemporaryDirectory() as tmpdir:
172 with TemporaryDirectory() as tmpdir:
163 prefix = os.path.join(tmpdir, 'foo')
173 prefix = os.path.join(tmpdir, 'foo')
164 suffixes = ['1', '2']
174 suffixes = ['1', '2']
165 names = [prefix+s for s in suffixes]
175 names = [prefix+s for s in suffixes]
166 for n in names:
176 for n in names:
167 open(n, 'w').close()
177 open(n, 'w').close()
168
178
169 # Check simple completion
179 # Check simple completion
170 c = ip.complete(prefix)[1]
180 c = ip.complete(prefix)[1]
171 nt.assert_equal(c, names)
181 nt.assert_equal(c, names)
172
182
173 # Now check with a function call
183 # Now check with a function call
174 cmd = 'a = f("%s' % prefix
184 cmd = 'a = f("%s' % prefix
175 c = ip.complete(prefix, cmd)[1]
185 c = ip.complete(prefix, cmd)[1]
176 comp = [prefix+s for s in suffixes]
186 comp = [prefix+s for s in suffixes]
177 nt.assert_equal(c, comp)
187 nt.assert_equal(c, comp)
178
188
179
189
180 def test_local_file_completions():
190 def test_local_file_completions():
181 ip = get_ipython()
191 ip = get_ipython()
182 cwd = py3compat.getcwd()
192 cwd = py3compat.getcwd()
183 try:
193 try:
184 with TemporaryDirectory() as tmpdir:
194 with TemporaryDirectory() as tmpdir:
185 os.chdir(tmpdir)
195 os.chdir(tmpdir)
186 prefix = './foo'
196 prefix = './foo'
187 suffixes = ['1', '2']
197 suffixes = ['1', '2']
188 names = [prefix+s for s in suffixes]
198 names = [prefix+s for s in suffixes]
189 for n in names:
199 for n in names:
190 open(n, 'w').close()
200 open(n, 'w').close()
191
201
192 # Check simple completion
202 # Check simple completion
193 c = ip.complete(prefix)[1]
203 c = ip.complete(prefix)[1]
194 nt.assert_equal(c, names)
204 nt.assert_equal(c, names)
195
205
196 # Now check with a function call
206 # Now check with a function call
197 cmd = 'a = f("%s' % prefix
207 cmd = 'a = f("%s' % prefix
198 c = ip.complete(prefix, cmd)[1]
208 c = ip.complete(prefix, cmd)[1]
199 comp = [prefix+s for s in suffixes]
209 comp = [prefix+s for s in suffixes]
200 nt.assert_equal(c, comp)
210 nt.assert_equal(c, comp)
201 finally:
211 finally:
202 # prevent failures from making chdir stick
212 # prevent failures from making chdir stick
203 os.chdir(cwd)
213 os.chdir(cwd)
204
214
205
215
206 def test_greedy_completions():
216 def test_greedy_completions():
207 ip = get_ipython()
217 ip = get_ipython()
208 greedy_original = ip.Completer.greedy
218 ip.ex('a=list(range(5))')
209 try:
219 _,c = ip.complete('.',line='a[0].')
210 ip.Completer.greedy = False
220 nt.assert_false('a[0].real' in c,
211 ip.ex('a=list(range(5))')
221 "Shouldn't have completed on a[0]: %s"%c)
212 _,c = ip.complete('.',line='a[0].')
222 with greedy_completion():
213 nt.assert_false('a[0].real' in c,
214 "Shouldn't have completed on a[0]: %s"%c)
215 ip.Completer.greedy = True
216 _,c = ip.complete('.',line='a[0].')
223 _,c = ip.complete('.',line='a[0].')
217 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
224 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
218 finally:
219 ip.Completer.greedy = greedy_original
220
225
221
226
222 def test_omit__names():
227 def test_omit__names():
223 # also happens to test IPCompleter as a configurable
228 # also happens to test IPCompleter as a configurable
224 ip = get_ipython()
229 ip = get_ipython()
225 ip._hidden_attr = 1
230 ip._hidden_attr = 1
226 c = ip.Completer
231 c = ip.Completer
227 ip.ex('ip=get_ipython()')
232 ip.ex('ip=get_ipython()')
228 cfg = Config()
233 cfg = Config()
229 cfg.IPCompleter.omit__names = 0
234 cfg.IPCompleter.omit__names = 0
230 c.update_config(cfg)
235 c.update_config(cfg)
231 s,matches = c.complete('ip.')
236 s,matches = c.complete('ip.')
232 nt.assert_in('ip.__str__', matches)
237 nt.assert_in('ip.__str__', matches)
233 nt.assert_in('ip._hidden_attr', matches)
238 nt.assert_in('ip._hidden_attr', matches)
234 cfg.IPCompleter.omit__names = 1
239 cfg.IPCompleter.omit__names = 1
235 c.update_config(cfg)
240 c.update_config(cfg)
236 s,matches = c.complete('ip.')
241 s,matches = c.complete('ip.')
237 nt.assert_not_in('ip.__str__', matches)
242 nt.assert_not_in('ip.__str__', matches)
238 nt.assert_in('ip._hidden_attr', matches)
243 nt.assert_in('ip._hidden_attr', matches)
239 cfg.IPCompleter.omit__names = 2
244 cfg.IPCompleter.omit__names = 2
240 c.update_config(cfg)
245 c.update_config(cfg)
241 s,matches = c.complete('ip.')
246 s,matches = c.complete('ip.')
242 nt.assert_not_in('ip.__str__', matches)
247 nt.assert_not_in('ip.__str__', matches)
243 nt.assert_not_in('ip._hidden_attr', matches)
248 nt.assert_not_in('ip._hidden_attr', matches)
244 del ip._hidden_attr
249 del ip._hidden_attr
245
250
246
251
247 def test_limit_to__all__False_ok():
252 def test_limit_to__all__False_ok():
248 ip = get_ipython()
253 ip = get_ipython()
249 c = ip.Completer
254 c = ip.Completer
250 ip.ex('class D: x=24')
255 ip.ex('class D: x=24')
251 ip.ex('d=D()')
256 ip.ex('d=D()')
252 cfg = Config()
257 cfg = Config()
253 cfg.IPCompleter.limit_to__all__ = False
258 cfg.IPCompleter.limit_to__all__ = False
254 c.update_config(cfg)
259 c.update_config(cfg)
255 s, matches = c.complete('d.')
260 s, matches = c.complete('d.')
256 nt.assert_in('d.x', matches)
261 nt.assert_in('d.x', matches)
257
262
258
263
259 def test_limit_to__all__True_ok():
264 def test_limit_to__all__True_ok():
260 ip = get_ipython()
265 ip = get_ipython()
261 c = ip.Completer
266 c = ip.Completer
262 ip.ex('class D: x=24')
267 ip.ex('class D: x=24')
263 ip.ex('d=D()')
268 ip.ex('d=D()')
264 ip.ex("d.__all__=['z']")
269 ip.ex("d.__all__=['z']")
265 cfg = Config()
270 cfg = Config()
266 cfg.IPCompleter.limit_to__all__ = True
271 cfg.IPCompleter.limit_to__all__ = True
267 c.update_config(cfg)
272 c.update_config(cfg)
268 s, matches = c.complete('d.')
273 s, matches = c.complete('d.')
269 nt.assert_in('d.z', matches)
274 nt.assert_in('d.z', matches)
270 nt.assert_not_in('d.x', matches)
275 nt.assert_not_in('d.x', matches)
271
276
272
277
273 def test_get__all__entries_ok():
278 def test_get__all__entries_ok():
274 class A(object):
279 class A(object):
275 __all__ = ['x', 1]
280 __all__ = ['x', 1]
276 words = completer.get__all__entries(A())
281 words = completer.get__all__entries(A())
277 nt.assert_equal(words, ['x'])
282 nt.assert_equal(words, ['x'])
278
283
279
284
280 def test_get__all__entries_no__all__ok():
285 def test_get__all__entries_no__all__ok():
281 class A(object):
286 class A(object):
282 pass
287 pass
283 words = completer.get__all__entries(A())
288 words = completer.get__all__entries(A())
284 nt.assert_equal(words, [])
289 nt.assert_equal(words, [])
285
290
286
291
287 def test_func_kw_completions():
292 def test_func_kw_completions():
288 ip = get_ipython()
293 ip = get_ipython()
289 c = ip.Completer
294 c = ip.Completer
290 ip.ex('def myfunc(a=1,b=2): return a+b')
295 ip.ex('def myfunc(a=1,b=2): return a+b')
291 s, matches = c.complete(None, 'myfunc(1,b')
296 s, matches = c.complete(None, 'myfunc(1,b')
292 nt.assert_in('b=', matches)
297 nt.assert_in('b=', matches)
293 # Simulate completing with cursor right after b (pos==10):
298 # Simulate completing with cursor right after b (pos==10):
294 s, matches = c.complete(None, 'myfunc(1,b)', 10)
299 s, matches = c.complete(None, 'myfunc(1,b)', 10)
295 nt.assert_in('b=', matches)
300 nt.assert_in('b=', matches)
296 s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b')
301 s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b')
297 nt.assert_in('b=', matches)
302 nt.assert_in('b=', matches)
298 #builtin function
303 #builtin function
299 s, matches = c.complete(None, 'min(k, k')
304 s, matches = c.complete(None, 'min(k, k')
300 nt.assert_in('key=', matches)
305 nt.assert_in('key=', matches)
301
306
302
307
303 def test_default_arguments_from_docstring():
308 def test_default_arguments_from_docstring():
304 doc = min.__doc__
309 doc = min.__doc__
305 ip = get_ipython()
310 ip = get_ipython()
306 c = ip.Completer
311 c = ip.Completer
307 kwd = c._default_arguments_from_docstring(
312 kwd = c._default_arguments_from_docstring(
308 'min(iterable[, key=func]) -> value')
313 'min(iterable[, key=func]) -> value')
309 nt.assert_equal(kwd, ['key'])
314 nt.assert_equal(kwd, ['key'])
310 #with cython type etc
315 #with cython type etc
311 kwd = c._default_arguments_from_docstring(
316 kwd = c._default_arguments_from_docstring(
312 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
317 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
313 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
318 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
314 #white spaces
319 #white spaces
315 kwd = c._default_arguments_from_docstring(
320 kwd = c._default_arguments_from_docstring(
316 '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
321 '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
317 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
322 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
318
323
319 def test_line_magics():
324 def test_line_magics():
320 ip = get_ipython()
325 ip = get_ipython()
321 c = ip.Completer
326 c = ip.Completer
322 s, matches = c.complete(None, 'lsmag')
327 s, matches = c.complete(None, 'lsmag')
323 nt.assert_in('%lsmagic', matches)
328 nt.assert_in('%lsmagic', matches)
324 s, matches = c.complete(None, '%lsmag')
329 s, matches = c.complete(None, '%lsmag')
325 nt.assert_in('%lsmagic', matches)
330 nt.assert_in('%lsmagic', matches)
326
331
327
332
328 def test_cell_magics():
333 def test_cell_magics():
329 from IPython.core.magic import register_cell_magic
334 from IPython.core.magic import register_cell_magic
330
335
331 @register_cell_magic
336 @register_cell_magic
332 def _foo_cellm(line, cell):
337 def _foo_cellm(line, cell):
333 pass
338 pass
334
339
335 ip = get_ipython()
340 ip = get_ipython()
336 c = ip.Completer
341 c = ip.Completer
337
342
338 s, matches = c.complete(None, '_foo_ce')
343 s, matches = c.complete(None, '_foo_ce')
339 nt.assert_in('%%_foo_cellm', matches)
344 nt.assert_in('%%_foo_cellm', matches)
340 s, matches = c.complete(None, '%%_foo_ce')
345 s, matches = c.complete(None, '%%_foo_ce')
341 nt.assert_in('%%_foo_cellm', matches)
346 nt.assert_in('%%_foo_cellm', matches)
342
347
343
348
344 def test_line_cell_magics():
349 def test_line_cell_magics():
345 from IPython.core.magic import register_line_cell_magic
350 from IPython.core.magic import register_line_cell_magic
346
351
347 @register_line_cell_magic
352 @register_line_cell_magic
348 def _bar_cellm(line, cell):
353 def _bar_cellm(line, cell):
349 pass
354 pass
350
355
351 ip = get_ipython()
356 ip = get_ipython()
352 c = ip.Completer
357 c = ip.Completer
353
358
354 # The policy here is trickier, see comments in completion code. The
359 # The policy here is trickier, see comments in completion code. The
355 # returned values depend on whether the user passes %% or not explicitly,
360 # returned values depend on whether the user passes %% or not explicitly,
356 # and this will show a difference if the same name is both a line and cell
361 # and this will show a difference if the same name is both a line and cell
357 # magic.
362 # magic.
358 s, matches = c.complete(None, '_bar_ce')
363 s, matches = c.complete(None, '_bar_ce')
359 nt.assert_in('%_bar_cellm', matches)
364 nt.assert_in('%_bar_cellm', matches)
360 nt.assert_in('%%_bar_cellm', matches)
365 nt.assert_in('%%_bar_cellm', matches)
361 s, matches = c.complete(None, '%_bar_ce')
366 s, matches = c.complete(None, '%_bar_ce')
362 nt.assert_in('%_bar_cellm', matches)
367 nt.assert_in('%_bar_cellm', matches)
363 nt.assert_in('%%_bar_cellm', matches)
368 nt.assert_in('%%_bar_cellm', matches)
364 s, matches = c.complete(None, '%%_bar_ce')
369 s, matches = c.complete(None, '%%_bar_ce')
365 nt.assert_not_in('%_bar_cellm', matches)
370 nt.assert_not_in('%_bar_cellm', matches)
366 nt.assert_in('%%_bar_cellm', matches)
371 nt.assert_in('%%_bar_cellm', matches)
367
372
368
373
369 def test_magic_completion_order():
374 def test_magic_completion_order():
370
375
371 ip = get_ipython()
376 ip = get_ipython()
372 c = ip.Completer
377 c = ip.Completer
373
378
374 # Test ordering of magics and non-magics with the same name
379 # Test ordering of magics and non-magics with the same name
375 # We want the non-magic first
380 # We want the non-magic first
376
381
377 # Before importing matplotlib, there should only be one option:
382 # Before importing matplotlib, there should only be one option:
378
383
379 text, matches = c.complete('mat')
384 text, matches = c.complete('mat')
380 nt.assert_equal(matches, ["%matplotlib"])
385 nt.assert_equal(matches, ["%matplotlib"])
381
386
382
387
383 ip.run_cell("matplotlib = 1") # introduce name into namespace
388 ip.run_cell("matplotlib = 1") # introduce name into namespace
384
389
385 # After the import, there should be two options, ordered like this:
390 # After the import, there should be two options, ordered like this:
386 text, matches = c.complete('mat')
391 text, matches = c.complete('mat')
387 nt.assert_equal(matches, ["matplotlib", "%matplotlib"])
392 nt.assert_equal(matches, ["matplotlib", "%matplotlib"])
388
393
389
394
390 ip.run_cell("timeit = 1") # define a user variable called 'timeit'
395 ip.run_cell("timeit = 1") # define a user variable called 'timeit'
391
396
392 # Order of user variable and line and cell magics with same name:
397 # Order of user variable and line and cell magics with same name:
393 text, matches = c.complete('timeit')
398 text, matches = c.complete('timeit')
394 nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"])
399 nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"])
395
400
396
401
397
398 def test_dict_key_completion_string():
402 def test_dict_key_completion_string():
399 """Test dictionary key completion for string keys"""
403 """Test dictionary key completion for string keys"""
400 ip = get_ipython()
404 ip = get_ipython()
401 complete = ip.Completer.complete
405 complete = ip.Completer.complete
402
406
403 ip.user_ns['d'] = {'abc': None}
407 ip.user_ns['d'] = {'abc': None}
404
408
405 # check completion at different stages
409 # check completion at different stages
406 _, matches = complete(line_buffer="d[")
410 _, matches = complete(line_buffer="d[")
407 nt.assert_in("'abc']", matches)
411 nt.assert_in("'abc'", matches)
412 nt.assert_not_in("'abc']", matches)
408
413
409 _, matches = complete(line_buffer="d['")
414 _, matches = complete(line_buffer="d['")
410 nt.assert_in("abc']", matches)
415 nt.assert_in("abc", matches)
416 nt.assert_not_in("abc']", matches)
411
417
412 _, matches = complete(line_buffer="d['a")
418 _, matches = complete(line_buffer="d['a")
413 nt.assert_in("abc']", matches)
419 nt.assert_in("abc", matches)
420 nt.assert_not_in("abc']", matches)
414
421
415 # check use of different quoting
422 # check use of different quoting
416 _, matches = complete(line_buffer="d[\"")
423 _, matches = complete(line_buffer="d[\"")
417 nt.assert_in("abc\"]", matches)
424 nt.assert_in("abc", matches)
425 nt.assert_not_in('abc\"]', matches)
418
426
419 _, matches = complete(line_buffer="d[\"a")
427 _, matches = complete(line_buffer="d[\"a")
420 nt.assert_in("abc\"]", matches)
428 nt.assert_in("abc", matches)
429 nt.assert_not_in('abc\"]', matches)
421
430
422 # check sensitivity to following context
431 # check sensitivity to following context
423 _, matches = complete(line_buffer="d[]", cursor_pos=2)
432 _, matches = complete(line_buffer="d[]", cursor_pos=2)
424 nt.assert_in("'abc'", matches)
433 nt.assert_in("'abc'", matches)
425
434
426 _, matches = complete(line_buffer="d['']", cursor_pos=3)
435 _, matches = complete(line_buffer="d['']", cursor_pos=3)
427 nt.assert_in("abc", matches)
436 nt.assert_in("abc", matches)
437 nt.assert_not_in("abc'", matches)
438 nt.assert_not_in("abc']", matches)
428
439
429 # check multiple solutions are correctly returned and that noise is not
440 # check multiple solutions are correctly returned and that noise is not
430 ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None,
441 ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None,
431 5: None}
442 5: None}
432
443
433 _, matches = complete(line_buffer="d['a")
444 _, matches = complete(line_buffer="d['a")
434 nt.assert_in("abc']", matches)
445 nt.assert_in("abc", matches)
435 nt.assert_in("abd']", matches)
446 nt.assert_in("abd", matches)
436 nt.assert_not_in("bad']", matches)
447 nt.assert_not_in("bad", matches)
448 assert not any(m.endswith((']', '"', "'")) for m in matches), matches
437
449
438 # check escaping and whitespace
450 # check escaping and whitespace
439 ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None}
451 ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None}
440 _, matches = complete(line_buffer="d['a")
452 _, matches = complete(line_buffer="d['a")
441 nt.assert_in("a\\nb']", matches)
453 nt.assert_in("a\\nb", matches)
442 nt.assert_in("a\\'b']", matches)
454 nt.assert_in("a\\'b", matches)
443 nt.assert_in("a\"b']", matches)
455 nt.assert_in("a\"b", matches)
444 nt.assert_in("a word']", matches)
456 nt.assert_in("a word", matches)
457 assert not any(m.endswith((']', '"', "'")) for m in matches), matches
445
458
446 # - can complete on non-initial word of the string
459 # - can complete on non-initial word of the string
447 _, matches = complete(line_buffer="d['a w")
460 _, matches = complete(line_buffer="d['a w")
448 nt.assert_in("word']", matches)
461 nt.assert_in("word", matches)
449
462
450 # - understands quote escaping
463 # - understands quote escaping
451 _, matches = complete(line_buffer="d['a\\'")
464 _, matches = complete(line_buffer="d['a\\'")
452 nt.assert_in("b']", matches)
465 nt.assert_in("b", matches)
453
466
454 # - default quoting should work like repr
467 # - default quoting should work like repr
455 _, matches = complete(line_buffer="d[")
468 _, matches = complete(line_buffer="d[")
456 nt.assert_in("\"a'b\"]", matches)
469 nt.assert_in("\"a'b\"", matches)
457
470
458 # - when opening quote with ", possible to match with unescaped apostrophe
471 # - when opening quote with ", possible to match with unescaped apostrophe
459 _, matches = complete(line_buffer="d[\"a'")
472 _, matches = complete(line_buffer="d[\"a'")
460 nt.assert_in("b\"]", matches)
473 nt.assert_in("b", matches)
461
474
462
475
463 def test_dict_key_completion_contexts():
476 def test_dict_key_completion_contexts():
464 """Test expression contexts in which dict key completion occurs"""
477 """Test expression contexts in which dict key completion occurs"""
465 ip = get_ipython()
478 ip = get_ipython()
466 complete = ip.Completer.complete
479 complete = ip.Completer.complete
467 d = {'abc': None}
480 d = {'abc': None}
468 ip.user_ns['d'] = d
481 ip.user_ns['d'] = d
469
482
470 class C:
483 class C:
471 data = d
484 data = d
472 ip.user_ns['C'] = C
485 ip.user_ns['C'] = C
473 ip.user_ns['get'] = lambda: d
486 ip.user_ns['get'] = lambda: d
474
487
475 def assert_no_completion(**kwargs):
488 def assert_no_completion(**kwargs):
476 _, matches = complete(**kwargs)
489 _, matches = complete(**kwargs)
477 nt.assert_not_in('abc', matches)
490 nt.assert_not_in('abc', matches)
478 nt.assert_not_in('abc\'', matches)
491 nt.assert_not_in('abc\'', matches)
479 nt.assert_not_in('abc\']', matches)
492 nt.assert_not_in('abc\']', matches)
480 nt.assert_not_in('\'abc\'', matches)
493 nt.assert_not_in('\'abc\'', matches)
481 nt.assert_not_in('\'abc\']', matches)
494 nt.assert_not_in('\'abc\']', matches)
482
495
483 def assert_completion(**kwargs):
496 def assert_completion(**kwargs):
484 _, matches = complete(**kwargs)
497 _, matches = complete(**kwargs)
485 nt.assert_in("'abc']", matches)
498 nt.assert_in("'abc'", matches)
499 nt.assert_not_in("'abc']", matches)
486
500
487 # no completion after string closed, even if reopened
501 # no completion after string closed, even if reopened
488 ip.Completer.greedy = False
489 assert_no_completion(line_buffer="d['a'")
502 assert_no_completion(line_buffer="d['a'")
490 assert_no_completion(line_buffer="d[\"a\"")
503 assert_no_completion(line_buffer="d[\"a\"")
491 assert_no_completion(line_buffer="d['a' + ")
504 assert_no_completion(line_buffer="d['a' + ")
492 assert_no_completion(line_buffer="d['a' + '")
505 assert_no_completion(line_buffer="d['a' + '")
493
506
494 # completion in non-trivial expressions
507 # completion in non-trivial expressions
495 assert_completion(line_buffer="+ d[")
508 assert_completion(line_buffer="+ d[")
496 assert_completion(line_buffer="(d[")
509 assert_completion(line_buffer="(d[")
497 assert_completion(line_buffer="C.data[")
510 assert_completion(line_buffer="C.data[")
498
511
499 # greedy flag
512 # greedy flag
513 def assert_completion(**kwargs):
514 _, matches = complete(**kwargs)
515 nt.assert_in("get()['abc']", matches)
516
500 assert_no_completion(line_buffer="get()[")
517 assert_no_completion(line_buffer="get()[")
501 ip.Completer.greedy = True
518 with greedy_completion():
502 assert_completion(line_buffer="get()[")
519 assert_completion(line_buffer="get()[")
520 assert_completion(line_buffer="get()['")
521 assert_completion(line_buffer="get()['a")
522 assert_completion(line_buffer="get()['ab")
523 assert_completion(line_buffer="get()['abc")
503
524
504
525
505
526
506 @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3')
527 @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3')
507 def test_dict_key_completion_bytes():
528 def test_dict_key_completion_bytes():
508 """Test handling of bytes in dict key completion"""
529 """Test handling of bytes in dict key completion"""
509 ip = get_ipython()
530 ip = get_ipython()
510 complete = ip.Completer.complete
531 complete = ip.Completer.complete
511
532
512 ip.user_ns['d'] = {'abc': None, b'abd': None}
533 ip.user_ns['d'] = {'abc': None, b'abd': None}
513
534
514 _, matches = complete(line_buffer="d[")
535 _, matches = complete(line_buffer="d[")
515 nt.assert_in("'abc']", matches)
536 nt.assert_in("'abc'", matches)
516 nt.assert_in("b'abd']", matches)
537 nt.assert_in("b'abd'", matches)
517
538
518 if False: # not currently implemented
539 if False: # not currently implemented
519 _, matches = complete(line_buffer="d[b")
540 _, matches = complete(line_buffer="d[b")
520 nt.assert_in("b'abd']", matches)
541 nt.assert_in("b'abd'", matches)
521 nt.assert_not_in("b'abc']", matches)
542 nt.assert_not_in("b'abc'", matches)
522
543
523 _, matches = complete(line_buffer="d[b'")
544 _, matches = complete(line_buffer="d[b'")
524 nt.assert_in("abd']", matches)
545 nt.assert_in("abd", matches)
525 nt.assert_not_in("abc']", matches)
546 nt.assert_not_in("abc", matches)
526
547
527 _, matches = complete(line_buffer="d[B'")
548 _, matches = complete(line_buffer="d[B'")
528 nt.assert_in("abd']", matches)
549 nt.assert_in("abd", matches)
529 nt.assert_not_in("abc']", matches)
550 nt.assert_not_in("abc", matches)
530
551
531 _, matches = complete(line_buffer="d['")
552 _, matches = complete(line_buffer="d['")
532 nt.assert_in("abc']", matches)
553 nt.assert_in("abc", matches)
533 nt.assert_not_in("abd']", matches)
554 nt.assert_not_in("abd", matches)
534
555
535
556
536 @dec.onlyif(sys.version_info[0] < 3, 'This test only applies in Py<3')
557 @dec.onlyif(sys.version_info[0] < 3, 'This test only applies in Py<3')
537 def test_dict_key_completion_unicode_py2():
558 def test_dict_key_completion_unicode_py2():
538 """Test handling of unicode in dict key completion"""
559 """Test handling of unicode in dict key completion"""
539 ip = get_ipython()
560 ip = get_ipython()
540 complete = ip.Completer.complete
561 complete = ip.Completer.complete
541
562
542 ip.user_ns['d'] = {u'abc': None,
563 ip.user_ns['d'] = {u'abc': None,
543 unicode_type('a\xd7\x90', 'utf8'): None}
564 u'a\u05d0b': None}
544
565
545 _, matches = complete(line_buffer="d[")
566 _, matches = complete(line_buffer="d[")
546 nt.assert_in("u'abc']", matches)
567 nt.assert_in("u'abc'", matches)
547 nt.assert_in("u'a\\u05d0']", matches)
568 nt.assert_in("u'a\\u05d0b'", matches)
548
569
549 _, matches = complete(line_buffer="d['a")
570 _, matches = complete(line_buffer="d['a")
550 nt.assert_in("abc']", matches)
571 nt.assert_in("abc", matches)
551 nt.assert_not_in("a\\u05d0']", matches)
572 nt.assert_not_in("a\\u05d0b", matches)
552
573
553 _, matches = complete(line_buffer="d[u'a")
574 _, matches = complete(line_buffer="d[u'a")
554 nt.assert_in("abc']", matches)
575 nt.assert_in("abc", matches)
555 nt.assert_in("a\\u05d0']", matches)
576 nt.assert_in("a\\u05d0b", matches)
556
577
557 _, matches = complete(line_buffer="d[U'a")
578 _, matches = complete(line_buffer="d[U'a")
558 nt.assert_in("abc']", matches)
579 nt.assert_in("abc", matches)
559 nt.assert_in("a\\u05d0']", matches)
580 nt.assert_in("a\\u05d0b", matches)
560
581
561 # query using escape
582 # query using escape
562 _, matches = complete(line_buffer="d[u'a\\u05d0")
583 _, matches = complete(line_buffer=u"d[u'a\\u05d0")
563 nt.assert_in("u05d0']", matches) # tokenized after \\
584 nt.assert_in("u05d0b", matches) # tokenized after \\
564
585
565 # query using character
586 # query using character
566 _, matches = complete(line_buffer=unicode_type("d[u'a\xd7\x90", 'utf8'))
587 _, matches = complete(line_buffer=u"d[u'a\u05d0")
567 nt.assert_in("']", matches)
588 nt.assert_in(u"a\u05d0b", matches)
589
590 with greedy_completion():
591 _, matches = complete(line_buffer="d[")
592 nt.assert_in("d[u'abc']", matches)
593 nt.assert_in("d[u'a\\u05d0b']", matches)
594
595 _, matches = complete(line_buffer="d['a")
596 nt.assert_in("d['abc']", matches)
597 nt.assert_not_in("d[u'a\\u05d0b']", matches)
598
599 _, matches = complete(line_buffer="d[u'a")
600 nt.assert_in("d[u'abc']", matches)
601 nt.assert_in("d[u'a\\u05d0b']", matches)
602
603 _, matches = complete(line_buffer="d[U'a")
604 nt.assert_in("d[U'abc']", matches)
605 nt.assert_in("d[U'a\\u05d0b']", matches)
606
607 # query using escape
608 _, matches = complete(line_buffer=u"d[u'a\\u05d0")
609 nt.assert_in("d[u'a\\u05d0b']", matches) # tokenized after \\
610
611 # query using character
612 _, matches = complete(line_buffer=u"d[u'a\u05d0")
613 nt.assert_in(u"d[u'a\u05d0b']", matches)
568
614
569
615
570 @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3')
616 @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3')
571 def test_dict_key_completion_unicode_py3():
617 def test_dict_key_completion_unicode_py3():
572 """Test handling of unicode in dict key completion"""
618 """Test handling of unicode in dict key completion"""
573 ip = get_ipython()
619 ip = get_ipython()
574 complete = ip.Completer.complete
620 complete = ip.Completer.complete
575
621
576 ip.user_ns['d'] = {unicode_type(b'a\xd7\x90', 'utf8'): None}
622 ip.user_ns['d'] = {u'a\u05d0': None}
577
623
578 # query using escape
624 # query using escape
579 _, matches = complete(line_buffer="d['a\\u05d0")
625 _, matches = complete(line_buffer="d['a\\u05d0")
580 nt.assert_in("u05d0']", matches) # tokenized after \\
626 nt.assert_in("u05d0", matches) # tokenized after \\
581
627
582 # query using character
628 # query using character
583 _, matches = complete(line_buffer=unicode_type(b"d['a\xd7\x90", 'utf8'))
629 _, matches = complete(line_buffer="d['a\u05d0")
584 nt.assert_in(unicode_type(b"a\xd7\x90']", 'utf8'), matches)
630 nt.assert_in(u"a\u05d0", matches)
631
632 with greedy_completion():
633 # query using escape
634 _, matches = complete(line_buffer="d['a\\u05d0")
635 nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\
636
637 # query using character
638 _, matches = complete(line_buffer="d['a\u05d0")
639 nt.assert_in(u"d['a\u05d0']", matches)
640
585
641
586
642
587 @dec.skip_without('numpy')
643 @dec.skip_without('numpy')
588 def test_struct_array_key_completion():
644 def test_struct_array_key_completion():
589 """Test dict key completion applies to numpy struct arrays"""
645 """Test dict key completion applies to numpy struct arrays"""
590 import numpy
646 import numpy
591 ip = get_ipython()
647 ip = get_ipython()
592 complete = ip.Completer.complete
648 complete = ip.Completer.complete
593 ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')])
649 ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')])
594 _, matches = complete(line_buffer="d['")
650 _, matches = complete(line_buffer="d['")
595 nt.assert_in("hello']", matches)
651 nt.assert_in("hello", matches)
596 nt.assert_in("world']", matches)
652 nt.assert_in("world", matches)
597
653
598
654
599 @dec.skip_without('pandas')
655 @dec.skip_without('pandas')
600 def test_dataframe_key_completion():
656 def test_dataframe_key_completion():
601 """Test dict key completion applies to pandas DataFrames"""
657 """Test dict key completion applies to pandas DataFrames"""
602 import pandas
658 import pandas
603 ip = get_ipython()
659 ip = get_ipython()
604 complete = ip.Completer.complete
660 complete = ip.Completer.complete
605 ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]})
661 ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]})
606 _, matches = complete(line_buffer="d['")
662 _, matches = complete(line_buffer="d['")
607 nt.assert_in("hello']", matches)
663 nt.assert_in("hello", matches)
608 nt.assert_in("world']", matches)
664 nt.assert_in("world", matches)
609
665
610
666
611 def test_dict_key_completion_invalids():
667 def test_dict_key_completion_invalids():
612 """Smoke test cases dict key completion can't handle"""
668 """Smoke test cases dict key completion can't handle"""
613 ip = get_ipython()
669 ip = get_ipython()
614 complete = ip.Completer.complete
670 complete = ip.Completer.complete
615
671
616 ip.user_ns['no_getitem'] = None
672 ip.user_ns['no_getitem'] = None
617 ip.user_ns['no_keys'] = []
673 ip.user_ns['no_keys'] = []
618 ip.user_ns['cant_call_keys'] = dict
674 ip.user_ns['cant_call_keys'] = dict
619 ip.user_ns['empty'] = {}
675 ip.user_ns['empty'] = {}
620 ip.user_ns['d'] = {'abc': 5}
676 ip.user_ns['d'] = {'abc': 5}
621
677
622 _, matches = complete(line_buffer="no_getitem['")
678 _, matches = complete(line_buffer="no_getitem['")
623 _, matches = complete(line_buffer="no_keys['")
679 _, matches = complete(line_buffer="no_keys['")
624 _, matches = complete(line_buffer="cant_call_keys['")
680 _, matches = complete(line_buffer="cant_call_keys['")
625 _, matches = complete(line_buffer="empty['")
681 _, matches = complete(line_buffer="empty['")
626 _, matches = complete(line_buffer="name_error['")
682 _, matches = complete(line_buffer="name_error['")
627 _, matches = complete(line_buffer="d['\\") # incomplete escape
683 _, matches = complete(line_buffer="d['\\") # incomplete escape
General Comments 0
You need to be logged in to leave comments. Login now