##// END OF EJS Templates
Misc. typo fixes...
luzpaz -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,2045 +1,2045 b''
1 """Completion for IPython.
1 """Completion for IPython.
2
2
3 This module started as fork of the rlcompleter module in the Python standard
3 This module started as 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,
5 upstream and were accepted as of Python 2.3,
6
6
7 This module now support a wide variety of completion mechanism both available
7 This module now support a wide variety of completion mechanism both available
8 for normal classic Python code, as well as completer for IPython specific
8 for normal classic Python code, as well as completer for IPython specific
9 Syntax like magics.
9 Syntax like magics.
10
10
11 Latex and Unicode completion
11 Latex and Unicode completion
12 ============================
12 ============================
13
13
14 IPython and compatible frontends not only can complete your code, but can help
14 IPython and compatible frontends not only can complete your code, but can help
15 you to input a wide range of characters. In particular we allow you to insert
15 you to input a wide range of characters. In particular we allow you to insert
16 a unicode character using the tab completion mechanism.
16 a unicode character using the tab completion mechanism.
17
17
18 Forward latex/unicode completion
18 Forward latex/unicode completion
19 --------------------------------
19 --------------------------------
20
20
21 Forward completion allows you to easily type a unicode character using its latex
21 Forward completion allows you to easily type a unicode character using its latex
22 name, or unicode long description. To do so type a backslash follow by the
22 name, or unicode long description. To do so type a backslash follow by the
23 relevant name and press tab:
23 relevant name and press tab:
24
24
25
25
26 Using latex completion:
26 Using latex completion:
27
27
28 .. code::
28 .. code::
29
29
30 \\alpha<tab>
30 \\alpha<tab>
31 α
31 α
32
32
33 or using unicode completion:
33 or using unicode completion:
34
34
35
35
36 .. code::
36 .. code::
37
37
38 \\greek small letter alpha<tab>
38 \\greek small letter alpha<tab>
39 α
39 α
40
40
41
41
42 Only valid Python identifiers will complete. Combining characters (like arrow or
42 Only valid Python identifiers will complete. Combining characters (like arrow or
43 dots) are also available, unlike latex they need to be put after the their
43 dots) are also available, unlike latex they need to be put after the their
44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
45
45
46 Some browsers are known to display combining characters incorrectly.
46 Some browsers are known to display combining characters incorrectly.
47
47
48 Backward latex completion
48 Backward latex completion
49 -------------------------
49 -------------------------
50
50
51 It is sometime challenging to know how to type a character, if you are using
51 It is sometime challenging to know how to type a character, if you are using
52 IPython, or any compatible frontend you can prepend backslash to the character
52 IPython, or any compatible frontend you can prepend backslash to the character
53 and press `<tab>` to expand it to its latex form.
53 and press `<tab>` to expand it to its latex form.
54
54
55 .. code::
55 .. code::
56
56
57 \\α<tab>
57 \\α<tab>
58 \\alpha
58 \\alpha
59
59
60
60
61 Both forward and backward completions can be deactivated by setting the
61 Both forward and backward completions can be deactivated by setting the
62 ``Completer.backslash_combining_completions`` option to ``False``.
62 ``Completer.backslash_combining_completions`` option to ``False``.
63
63
64
64
65 Experimental
65 Experimental
66 ============
66 ============
67
67
68 Starting with IPython 6.0, this module can make use of the Jedi library to
68 Starting with IPython 6.0, this module can make use of the Jedi library to
69 generate completions both using static analysis of the code, and dynamically
69 generate completions both using static analysis of the code, and dynamically
70 inspecting multiple namespaces. The APIs attached to this new mechanism is
70 inspecting multiple namespaces. The APIs attached to this new mechanism is
71 unstable and will raise unless use in an :any:`provisionalcompleter` context
71 unstable and will raise unless use in an :any:`provisionalcompleter` context
72 manager.
72 manager.
73
73
74 You will find that the following are experimental:
74 You will find that the following are experimental:
75
75
76 - :any:`provisionalcompleter`
76 - :any:`provisionalcompleter`
77 - :any:`IPCompleter.completions`
77 - :any:`IPCompleter.completions`
78 - :any:`Completion`
78 - :any:`Completion`
79 - :any:`rectify_completions`
79 - :any:`rectify_completions`
80
80
81 .. note::
81 .. note::
82
82
83 better name for :any:`rectify_completions` ?
83 better name for :any:`rectify_completions` ?
84
84
85 We welcome any feedback on these new API, and we also encourage you to try this
85 We welcome any feedback on these new API, and we also encourage you to try this
86 module in debug mode (start IPython with ``--Completer.debug=True``) in order
86 module in debug mode (start IPython with ``--Completer.debug=True``) in order
87 to have extra logging information is :any:`jedi` is crashing, or if current
87 to have extra logging information is :any:`jedi` is crashing, or if current
88 IPython completer pending deprecations are returning results not yet handled
88 IPython completer pending deprecations are returning results not yet handled
89 by :any:`jedi`
89 by :any:`jedi`
90
90
91 Using Jedi for tab completion allow snippets like the following to work without
91 Using Jedi for tab completion allow snippets like the following to work without
92 having to execute any code:
92 having to execute any code:
93
93
94 >>> myvar = ['hello', 42]
94 >>> myvar = ['hello', 42]
95 ... myvar[1].bi<tab>
95 ... myvar[1].bi<tab>
96
96
97 Tab completion will be able to infer that ``myvar[1]`` is a real number without
97 Tab completion will be able to infer that ``myvar[1]`` is a real number without
98 executing any code unlike the previously available ``IPCompleter.greedy``
98 executing any code unlike the previously available ``IPCompleter.greedy``
99 option.
99 option.
100
100
101 Be sure to update :any:`jedi` to the latest stable version or to try the
101 Be sure to update :any:`jedi` to the latest stable version or to try the
102 current development version to get better completions.
102 current development version to get better completions.
103 """
103 """
104
104
105
105
106 # Copyright (c) IPython Development Team.
106 # Copyright (c) IPython Development Team.
107 # Distributed under the terms of the Modified BSD License.
107 # Distributed under the terms of the Modified BSD License.
108 #
108 #
109 # Some of this code originated from rlcompleter in the Python standard library
109 # Some of this code originated from rlcompleter in the Python standard library
110 # Copyright (C) 2001 Python Software Foundation, www.python.org
110 # Copyright (C) 2001 Python Software Foundation, www.python.org
111
111
112
112
113 import __main__
113 import __main__
114 import builtins as builtin_mod
114 import builtins as builtin_mod
115 import glob
115 import glob
116 import time
116 import time
117 import inspect
117 import inspect
118 import itertools
118 import itertools
119 import keyword
119 import keyword
120 import os
120 import os
121 import re
121 import re
122 import sys
122 import sys
123 import unicodedata
123 import unicodedata
124 import string
124 import string
125 import warnings
125 import warnings
126
126
127 from contextlib import contextmanager
127 from contextlib import contextmanager
128 from importlib import import_module
128 from importlib import import_module
129 from typing import Iterator, List, Tuple, Iterable, Union
129 from typing import Iterator, List, Tuple, Iterable, Union
130 from types import SimpleNamespace
130 from types import SimpleNamespace
131
131
132 from traitlets.config.configurable import Configurable
132 from traitlets.config.configurable import Configurable
133 from IPython.core.error import TryNext
133 from IPython.core.error import TryNext
134 from IPython.core.inputsplitter import ESC_MAGIC
134 from IPython.core.inputsplitter import ESC_MAGIC
135 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
135 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
136 from IPython.core.oinspect import InspectColors
136 from IPython.core.oinspect import InspectColors
137 from IPython.utils import generics
137 from IPython.utils import generics
138 from IPython.utils.dir2 import dir2, get_real_method
138 from IPython.utils.dir2 import dir2, get_real_method
139 from IPython.utils.process import arg_split
139 from IPython.utils.process import arg_split
140 from traitlets import Bool, Enum, observe, Int
140 from traitlets import Bool, Enum, observe, Int
141
141
142 # skip module docstests
142 # skip module docstests
143 skip_doctest = True
143 skip_doctest = True
144
144
145 try:
145 try:
146 import jedi
146 import jedi
147 import jedi.api.helpers
147 import jedi.api.helpers
148 import jedi.api.classes
148 import jedi.api.classes
149 JEDI_INSTALLED = True
149 JEDI_INSTALLED = True
150 except ImportError:
150 except ImportError:
151 JEDI_INSTALLED = False
151 JEDI_INSTALLED = False
152 #-----------------------------------------------------------------------------
152 #-----------------------------------------------------------------------------
153 # Globals
153 # Globals
154 #-----------------------------------------------------------------------------
154 #-----------------------------------------------------------------------------
155
155
156 # Public API
156 # Public API
157 __all__ = ['Completer','IPCompleter']
157 __all__ = ['Completer','IPCompleter']
158
158
159 if sys.platform == 'win32':
159 if sys.platform == 'win32':
160 PROTECTABLES = ' '
160 PROTECTABLES = ' '
161 else:
161 else:
162 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
162 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
163
163
164 # Protect against returning an enormous number of completions which the frontend
164 # Protect against returning an enormous number of completions which the frontend
165 # may have trouble processing.
165 # may have trouble processing.
166 MATCHES_LIMIT = 500
166 MATCHES_LIMIT = 500
167
167
168 _deprecation_readline_sentinel = object()
168 _deprecation_readline_sentinel = object()
169
169
170
170
171 class ProvisionalCompleterWarning(FutureWarning):
171 class ProvisionalCompleterWarning(FutureWarning):
172 """
172 """
173 Exception raise by an experimental feature in this module.
173 Exception raise by an experimental feature in this module.
174
174
175 Wrap code in :any:`provisionalcompleter` context manager if you
175 Wrap code in :any:`provisionalcompleter` context manager if you
176 are certain you want to use an unstable feature.
176 are certain you want to use an unstable feature.
177 """
177 """
178 pass
178 pass
179
179
180 warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
180 warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
181
181
182 @contextmanager
182 @contextmanager
183 def provisionalcompleter(action='ignore'):
183 def provisionalcompleter(action='ignore'):
184 """
184 """
185
185
186
186
187 This contest manager has to be used in any place where unstable completer
187 This contest manager has to be used in any place where unstable completer
188 behavior and API may be called.
188 behavior and API may be called.
189
189
190 >>> with provisionalcompleter():
190 >>> with provisionalcompleter():
191 ... completer.do_experimetal_things() # works
191 ... completer.do_experimetal_things() # works
192
192
193 >>> completer.do_experimental_things() # raises.
193 >>> completer.do_experimental_things() # raises.
194
194
195 .. note:: Unstable
195 .. note:: Unstable
196
196
197 By using this context manager you agree that the API in use may change
197 By using this context manager you agree that the API in use may change
198 without warning, and that you won't complain if they do so.
198 without warning, and that you won't complain if they do so.
199
199
200 You also understand that if the API is not to you liking you should report
200 You also understand that if the API is not to you liking you should report
201 a bug to explain your use case upstream and improve the API and will loose
201 a bug to explain your use case upstream and improve the API and will loose
202 credibility if you complain after the API is make stable.
202 credibility if you complain after the API is make stable.
203
203
204 We'll be happy to get your feedback , feature request and improvement on
204 We'll be happy to get your feedback , feature request and improvement on
205 any of the unstable APIs !
205 any of the unstable APIs !
206 """
206 """
207 with warnings.catch_warnings():
207 with warnings.catch_warnings():
208 warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
208 warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
209 yield
209 yield
210
210
211
211
212 def has_open_quotes(s):
212 def has_open_quotes(s):
213 """Return whether a string has open quotes.
213 """Return whether a string has open quotes.
214
214
215 This simply counts whether the number of quote characters of either type in
215 This simply counts whether the number of quote characters of either type in
216 the string is odd.
216 the string is odd.
217
217
218 Returns
218 Returns
219 -------
219 -------
220 If there is an open quote, the quote character is returned. Else, return
220 If there is an open quote, the quote character is returned. Else, return
221 False.
221 False.
222 """
222 """
223 # We check " first, then ', so complex cases with nested quotes will get
223 # We check " first, then ', so complex cases with nested quotes will get
224 # the " to take precedence.
224 # the " to take precedence.
225 if s.count('"') % 2:
225 if s.count('"') % 2:
226 return '"'
226 return '"'
227 elif s.count("'") % 2:
227 elif s.count("'") % 2:
228 return "'"
228 return "'"
229 else:
229 else:
230 return False
230 return False
231
231
232
232
233 def protect_filename(s, protectables=PROTECTABLES):
233 def protect_filename(s, protectables=PROTECTABLES):
234 """Escape a string to protect certain characters."""
234 """Escape a string to protect certain characters."""
235 if set(s) & set(protectables):
235 if set(s) & set(protectables):
236 if sys.platform == "win32":
236 if sys.platform == "win32":
237 return '"' + s + '"'
237 return '"' + s + '"'
238 else:
238 else:
239 return "".join(("\\" + c if c in protectables else c) for c in s)
239 return "".join(("\\" + c if c in protectables else c) for c in s)
240 else:
240 else:
241 return s
241 return s
242
242
243
243
244 def expand_user(path:str) -> Tuple[str, bool, str]:
244 def expand_user(path:str) -> Tuple[str, bool, str]:
245 """Expand ``~``-style usernames in strings.
245 """Expand ``~``-style usernames in strings.
246
246
247 This is similar to :func:`os.path.expanduser`, but it computes and returns
247 This is similar to :func:`os.path.expanduser`, but it computes and returns
248 extra information that will be useful if the input was being used in
248 extra information that will be useful if the input was being used in
249 computing completions, and you wish to return the completions with the
249 computing completions, and you wish to return the completions with the
250 original '~' instead of its expanded value.
250 original '~' instead of its expanded value.
251
251
252 Parameters
252 Parameters
253 ----------
253 ----------
254 path : str
254 path : str
255 String to be expanded. If no ~ is present, the output is the same as the
255 String to be expanded. If no ~ is present, the output is the same as the
256 input.
256 input.
257
257
258 Returns
258 Returns
259 -------
259 -------
260 newpath : str
260 newpath : str
261 Result of ~ expansion in the input path.
261 Result of ~ expansion in the input path.
262 tilde_expand : bool
262 tilde_expand : bool
263 Whether any expansion was performed or not.
263 Whether any expansion was performed or not.
264 tilde_val : str
264 tilde_val : str
265 The value that ~ was replaced with.
265 The value that ~ was replaced with.
266 """
266 """
267 # Default values
267 # Default values
268 tilde_expand = False
268 tilde_expand = False
269 tilde_val = ''
269 tilde_val = ''
270 newpath = path
270 newpath = path
271
271
272 if path.startswith('~'):
272 if path.startswith('~'):
273 tilde_expand = True
273 tilde_expand = True
274 rest = len(path)-1
274 rest = len(path)-1
275 newpath = os.path.expanduser(path)
275 newpath = os.path.expanduser(path)
276 if rest:
276 if rest:
277 tilde_val = newpath[:-rest]
277 tilde_val = newpath[:-rest]
278 else:
278 else:
279 tilde_val = newpath
279 tilde_val = newpath
280
280
281 return newpath, tilde_expand, tilde_val
281 return newpath, tilde_expand, tilde_val
282
282
283
283
284 def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
284 def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
285 """Does the opposite of expand_user, with its outputs.
285 """Does the opposite of expand_user, with its outputs.
286 """
286 """
287 if tilde_expand:
287 if tilde_expand:
288 return path.replace(tilde_val, '~')
288 return path.replace(tilde_val, '~')
289 else:
289 else:
290 return path
290 return path
291
291
292
292
293 def completions_sorting_key(word):
293 def completions_sorting_key(word):
294 """key for sorting completions
294 """key for sorting completions
295
295
296 This does several things:
296 This does several things:
297
297
298 - Demote any completions starting with underscores to the end
298 - Demote any completions starting with underscores to the end
299 - Insert any %magic and %%cellmagic completions in the alphabetical order
299 - Insert any %magic and %%cellmagic completions in the alphabetical order
300 by their name
300 by their name
301 """
301 """
302 prio1, prio2 = 0, 0
302 prio1, prio2 = 0, 0
303
303
304 if word.startswith('__'):
304 if word.startswith('__'):
305 prio1 = 2
305 prio1 = 2
306 elif word.startswith('_'):
306 elif word.startswith('_'):
307 prio1 = 1
307 prio1 = 1
308
308
309 if word.endswith('='):
309 if word.endswith('='):
310 prio1 = -1
310 prio1 = -1
311
311
312 if word.startswith('%%'):
312 if word.startswith('%%'):
313 # If there's another % in there, this is something else, so leave it alone
313 # If there's another % in there, this is something else, so leave it alone
314 if not "%" in word[2:]:
314 if not "%" in word[2:]:
315 word = word[2:]
315 word = word[2:]
316 prio2 = 2
316 prio2 = 2
317 elif word.startswith('%'):
317 elif word.startswith('%'):
318 if not "%" in word[1:]:
318 if not "%" in word[1:]:
319 word = word[1:]
319 word = word[1:]
320 prio2 = 1
320 prio2 = 1
321
321
322 return prio1, word, prio2
322 return prio1, word, prio2
323
323
324
324
325 class _FakeJediCompletion:
325 class _FakeJediCompletion:
326 """
326 """
327 This is a workaround to communicate to the UI that Jedi has crashed and to
327 This is a workaround to communicate to the UI that Jedi has crashed and to
328 report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
328 report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
329
329
330 Added in IPython 6.0 so should likely be removed for 7.0
330 Added in IPython 6.0 so should likely be removed for 7.0
331
331
332 """
332 """
333
333
334 def __init__(self, name):
334 def __init__(self, name):
335
335
336 self.name = name
336 self.name = name
337 self.complete = name
337 self.complete = name
338 self.type = 'crashed'
338 self.type = 'crashed'
339 self.name_with_symbols = name
339 self.name_with_symbols = name
340 self.signature = ''
340 self.signature = ''
341 self._origin = 'fake'
341 self._origin = 'fake'
342
342
343 def __repr__(self):
343 def __repr__(self):
344 return '<Fake completion object jedi has crashed>'
344 return '<Fake completion object jedi has crashed>'
345
345
346
346
347 class Completion:
347 class Completion:
348 """
348 """
349 Completion object used and return by IPython completers.
349 Completion object used and return by IPython completers.
350
350
351 .. warning:: Unstable
351 .. warning:: Unstable
352
352
353 This function is unstable, API may change without warning.
353 This function is unstable, API may change without warning.
354 It will also raise unless use in proper context manager.
354 It will also raise unless use in proper context manager.
355
355
356 This act as a middle ground :any:`Completion` object between the
356 This act as a middle ground :any:`Completion` object between the
357 :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
357 :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
358 object. While Jedi need a lot of information about evaluator and how the
358 object. While Jedi need a lot of information about evaluator and how the
359 code should be ran/inspected, PromptToolkit (and other frontend) mostly
359 code should be ran/inspected, PromptToolkit (and other frontend) mostly
360 need user facing information.
360 need user facing information.
361
361
362 - Which range should be replaced replaced by what.
362 - Which range should be replaced replaced by what.
363 - Some metadata (like completion type), or meta informations to displayed to
363 - Some metadata (like completion type), or meta information to displayed to
364 the use user.
364 the use user.
365
365
366 For debugging purpose we can also store the origin of the completion (``jedi``,
366 For debugging purpose we can also store the origin of the completion (``jedi``,
367 ``IPython.python_matches``, ``IPython.magics_matches``...).
367 ``IPython.python_matches``, ``IPython.magics_matches``...).
368 """
368 """
369
369
370 __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
370 __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
371
371
372 def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
372 def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
373 warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
373 warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
374 "It may change without warnings. "
374 "It may change without warnings. "
375 "Use in corresponding context manager.",
375 "Use in corresponding context manager.",
376 category=ProvisionalCompleterWarning, stacklevel=2)
376 category=ProvisionalCompleterWarning, stacklevel=2)
377
377
378 self.start = start
378 self.start = start
379 self.end = end
379 self.end = end
380 self.text = text
380 self.text = text
381 self.type = type
381 self.type = type
382 self.signature = signature
382 self.signature = signature
383 self._origin = _origin
383 self._origin = _origin
384
384
385 def __repr__(self):
385 def __repr__(self):
386 return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
386 return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
387 (self.start, self.end, self.text, self.type or '?', self.signature or '?')
387 (self.start, self.end, self.text, self.type or '?', self.signature or '?')
388
388
389 def __eq__(self, other)->Bool:
389 def __eq__(self, other)->Bool:
390 """
390 """
391 Equality and hash do not hash the type (as some completer may not be
391 Equality and hash do not hash the type (as some completer may not be
392 able to infer the type), but are use to (partially) de-duplicate
392 able to infer the type), but are use to (partially) de-duplicate
393 completion.
393 completion.
394
394
395 Completely de-duplicating completion is a bit tricker that just
395 Completely de-duplicating completion is a bit tricker that just
396 comparing as it depends on surrounding text, which Completions are not
396 comparing as it depends on surrounding text, which Completions are not
397 aware of.
397 aware of.
398 """
398 """
399 return self.start == other.start and \
399 return self.start == other.start and \
400 self.end == other.end and \
400 self.end == other.end and \
401 self.text == other.text
401 self.text == other.text
402
402
403 def __hash__(self):
403 def __hash__(self):
404 return hash((self.start, self.end, self.text))
404 return hash((self.start, self.end, self.text))
405
405
406
406
407 _IC = Iterable[Completion]
407 _IC = Iterable[Completion]
408
408
409
409
410 def _deduplicate_completions(text: str, completions: _IC)-> _IC:
410 def _deduplicate_completions(text: str, completions: _IC)-> _IC:
411 """
411 """
412 Deduplicate a set of completions.
412 Deduplicate a set of completions.
413
413
414 .. warning:: Unstable
414 .. warning:: Unstable
415
415
416 This function is unstable, API may change without warning.
416 This function is unstable, API may change without warning.
417
417
418 Parameters
418 Parameters
419 ----------
419 ----------
420 text: str
420 text: str
421 text that should be completed.
421 text that should be completed.
422 completions: Iterator[Completion]
422 completions: Iterator[Completion]
423 iterator over the completions to deduplicate
423 iterator over the completions to deduplicate
424
424
425 Yields
425 Yields
426 ------
426 ------
427 `Completions` objects
427 `Completions` objects
428
428
429
429
430 Completions coming from multiple sources, may be different but end up having
430 Completions coming from multiple sources, may be different but end up having
431 the same effect when applied to ``text``. If this is the case, this will
431 the same effect when applied to ``text``. If this is the case, this will
432 consider completions as equal and only emit the first encountered.
432 consider completions as equal and only emit the first encountered.
433
433
434 Not folded in `completions()` yet for debugging purpose, and to detect when
434 Not folded in `completions()` yet for debugging purpose, and to detect when
435 the IPython completer does return things that Jedi does not, but should be
435 the IPython completer does return things that Jedi does not, but should be
436 at some point.
436 at some point.
437 """
437 """
438 completions = list(completions)
438 completions = list(completions)
439 if not completions:
439 if not completions:
440 return
440 return
441
441
442 new_start = min(c.start for c in completions)
442 new_start = min(c.start for c in completions)
443 new_end = max(c.end for c in completions)
443 new_end = max(c.end for c in completions)
444
444
445 seen = set()
445 seen = set()
446 for c in completions:
446 for c in completions:
447 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
447 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
448 if new_text not in seen:
448 if new_text not in seen:
449 yield c
449 yield c
450 seen.add(new_text)
450 seen.add(new_text)
451
451
452
452
453 def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
453 def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
454 """
454 """
455 Rectify a set of completions to all have the same ``start`` and ``end``
455 Rectify a set of completions to all have the same ``start`` and ``end``
456
456
457 .. warning:: Unstable
457 .. warning:: Unstable
458
458
459 This function is unstable, API may change without warning.
459 This function is unstable, API may change without warning.
460 It will also raise unless use in proper context manager.
460 It will also raise unless use in proper context manager.
461
461
462 Parameters
462 Parameters
463 ----------
463 ----------
464 text: str
464 text: str
465 text that should be completed.
465 text that should be completed.
466 completions: Iterator[Completion]
466 completions: Iterator[Completion]
467 iterator over the completions to rectify
467 iterator over the completions to rectify
468
468
469
469
470 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
470 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
471 the Jupyter Protocol requires them to behave like so. This will readjust
471 the Jupyter Protocol requires them to behave like so. This will readjust
472 the completion to have the same ``start`` and ``end`` by padding both
472 the completion to have the same ``start`` and ``end`` by padding both
473 extremities with surrounding text.
473 extremities with surrounding text.
474
474
475 During stabilisation should support a ``_debug`` option to log which
475 During stabilisation should support a ``_debug`` option to log which
476 completion are return by the IPython completer and not found in Jedi in
476 completion are return by the IPython completer and not found in Jedi in
477 order to make upstream bug report.
477 order to make upstream bug report.
478 """
478 """
479 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
479 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
480 "It may change without warnings. "
480 "It may change without warnings. "
481 "Use in corresponding context manager.",
481 "Use in corresponding context manager.",
482 category=ProvisionalCompleterWarning, stacklevel=2)
482 category=ProvisionalCompleterWarning, stacklevel=2)
483
483
484 completions = list(completions)
484 completions = list(completions)
485 if not completions:
485 if not completions:
486 return
486 return
487 starts = (c.start for c in completions)
487 starts = (c.start for c in completions)
488 ends = (c.end for c in completions)
488 ends = (c.end for c in completions)
489
489
490 new_start = min(starts)
490 new_start = min(starts)
491 new_end = max(ends)
491 new_end = max(ends)
492
492
493 seen_jedi = set()
493 seen_jedi = set()
494 seen_python_matches = set()
494 seen_python_matches = set()
495 for c in completions:
495 for c in completions:
496 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
496 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
497 if c._origin == 'jedi':
497 if c._origin == 'jedi':
498 seen_jedi.add(new_text)
498 seen_jedi.add(new_text)
499 elif c._origin == 'IPCompleter.python_matches':
499 elif c._origin == 'IPCompleter.python_matches':
500 seen_python_matches.add(new_text)
500 seen_python_matches.add(new_text)
501 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
501 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
502 diff = seen_python_matches.difference(seen_jedi)
502 diff = seen_python_matches.difference(seen_jedi)
503 if diff and _debug:
503 if diff and _debug:
504 print('IPython.python matches have extras:', diff)
504 print('IPython.python matches have extras:', diff)
505
505
506
506
507 if sys.platform == 'win32':
507 if sys.platform == 'win32':
508 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
508 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
509 else:
509 else:
510 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
510 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
511
511
512 GREEDY_DELIMS = ' =\r\n'
512 GREEDY_DELIMS = ' =\r\n'
513
513
514
514
515 class CompletionSplitter(object):
515 class CompletionSplitter(object):
516 """An object to split an input line in a manner similar to readline.
516 """An object to split an input line in a manner similar to readline.
517
517
518 By having our own implementation, we can expose readline-like completion in
518 By having our own implementation, we can expose readline-like completion in
519 a uniform manner to all frontends. This object only needs to be given the
519 a uniform manner to all frontends. This object only needs to be given the
520 line of text to be split and the cursor position on said line, and it
520 line of text to be split and the cursor position on said line, and it
521 returns the 'word' to be completed on at the cursor after splitting the
521 returns the 'word' to be completed on at the cursor after splitting the
522 entire line.
522 entire line.
523
523
524 What characters are used as splitting delimiters can be controlled by
524 What characters are used as splitting delimiters can be controlled by
525 setting the ``delims`` attribute (this is a property that internally
525 setting the ``delims`` attribute (this is a property that internally
526 automatically builds the necessary regular expression)"""
526 automatically builds the necessary regular expression)"""
527
527
528 # Private interface
528 # Private interface
529
529
530 # A string of delimiter characters. The default value makes sense for
530 # A string of delimiter characters. The default value makes sense for
531 # IPython's most typical usage patterns.
531 # IPython's most typical usage patterns.
532 _delims = DELIMS
532 _delims = DELIMS
533
533
534 # The expression (a normal string) to be compiled into a regular expression
534 # The expression (a normal string) to be compiled into a regular expression
535 # for actual splitting. We store it as an attribute mostly for ease of
535 # for actual splitting. We store it as an attribute mostly for ease of
536 # debugging, since this type of code can be so tricky to debug.
536 # debugging, since this type of code can be so tricky to debug.
537 _delim_expr = None
537 _delim_expr = None
538
538
539 # The regular expression that does the actual splitting
539 # The regular expression that does the actual splitting
540 _delim_re = None
540 _delim_re = None
541
541
542 def __init__(self, delims=None):
542 def __init__(self, delims=None):
543 delims = CompletionSplitter._delims if delims is None else delims
543 delims = CompletionSplitter._delims if delims is None else delims
544 self.delims = delims
544 self.delims = delims
545
545
546 @property
546 @property
547 def delims(self):
547 def delims(self):
548 """Return the string of delimiter characters."""
548 """Return the string of delimiter characters."""
549 return self._delims
549 return self._delims
550
550
551 @delims.setter
551 @delims.setter
552 def delims(self, delims):
552 def delims(self, delims):
553 """Set the delimiters for line splitting."""
553 """Set the delimiters for line splitting."""
554 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
554 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
555 self._delim_re = re.compile(expr)
555 self._delim_re = re.compile(expr)
556 self._delims = delims
556 self._delims = delims
557 self._delim_expr = expr
557 self._delim_expr = expr
558
558
559 def split_line(self, line, cursor_pos=None):
559 def split_line(self, line, cursor_pos=None):
560 """Split a line of text with a cursor at the given position.
560 """Split a line of text with a cursor at the given position.
561 """
561 """
562 l = line if cursor_pos is None else line[:cursor_pos]
562 l = line if cursor_pos is None else line[:cursor_pos]
563 return self._delim_re.split(l)[-1]
563 return self._delim_re.split(l)[-1]
564
564
565
565
566
566
567 class Completer(Configurable):
567 class Completer(Configurable):
568
568
569 greedy = Bool(False,
569 greedy = Bool(False,
570 help="""Activate greedy completion
570 help="""Activate greedy completion
571 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
571 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
572
572
573 This will enable completion on elements of lists, results of function calls, etc.,
573 This will enable completion on elements of lists, results of function calls, etc.,
574 but can be unsafe because the code is actually evaluated on TAB.
574 but can be unsafe because the code is actually evaluated on TAB.
575 """
575 """
576 ).tag(config=True)
576 ).tag(config=True)
577
577
578 use_jedi = Bool(default_value=JEDI_INSTALLED,
578 use_jedi = Bool(default_value=JEDI_INSTALLED,
579 help="Experimental: Use Jedi to generate autocompletions. "
579 help="Experimental: Use Jedi to generate autocompletions. "
580 "Default to True if jedi is installed").tag(config=True)
580 "Default to True if jedi is installed").tag(config=True)
581
581
582 jedi_compute_type_timeout = Int(default_value=400,
582 jedi_compute_type_timeout = Int(default_value=400,
583 help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
583 help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
584 Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
584 Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
585 performance by preventing jedi to build its cache.
585 performance by preventing jedi to build its cache.
586 """).tag(config=True)
586 """).tag(config=True)
587
587
588 debug = Bool(default_value=False,
588 debug = Bool(default_value=False,
589 help='Enable debug for the Completer. Mostly print extra '
589 help='Enable debug for the Completer. Mostly print extra '
590 'information for experimental jedi integration.')\
590 'information for experimental jedi integration.')\
591 .tag(config=True)
591 .tag(config=True)
592
592
593 backslash_combining_completions = Bool(True,
593 backslash_combining_completions = Bool(True,
594 help="Enable unicode completions, e.g. \\alpha<tab> . "
594 help="Enable unicode completions, e.g. \\alpha<tab> . "
595 "Includes completion of latex commands, unicode names, and expanding "
595 "Includes completion of latex commands, unicode names, and expanding "
596 "unicode characters back to latex commands.").tag(config=True)
596 "unicode characters back to latex commands.").tag(config=True)
597
597
598
598
599
599
600 def __init__(self, namespace=None, global_namespace=None, **kwargs):
600 def __init__(self, namespace=None, global_namespace=None, **kwargs):
601 """Create a new completer for the command line.
601 """Create a new completer for the command line.
602
602
603 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
603 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
604
604
605 If unspecified, the default namespace where completions are performed
605 If unspecified, the default namespace where completions are performed
606 is __main__ (technically, __main__.__dict__). Namespaces should be
606 is __main__ (technically, __main__.__dict__). Namespaces should be
607 given as dictionaries.
607 given as dictionaries.
608
608
609 An optional second namespace can be given. This allows the completer
609 An optional second namespace can be given. This allows the completer
610 to handle cases where both the local and global scopes need to be
610 to handle cases where both the local and global scopes need to be
611 distinguished.
611 distinguished.
612 """
612 """
613
613
614 # Don't bind to namespace quite yet, but flag whether the user wants a
614 # Don't bind to namespace quite yet, but flag whether the user wants a
615 # specific namespace or to use __main__.__dict__. This will allow us
615 # specific namespace or to use __main__.__dict__. This will allow us
616 # to bind to __main__.__dict__ at completion time, not now.
616 # to bind to __main__.__dict__ at completion time, not now.
617 if namespace is None:
617 if namespace is None:
618 self.use_main_ns = True
618 self.use_main_ns = True
619 else:
619 else:
620 self.use_main_ns = False
620 self.use_main_ns = False
621 self.namespace = namespace
621 self.namespace = namespace
622
622
623 # The global namespace, if given, can be bound directly
623 # The global namespace, if given, can be bound directly
624 if global_namespace is None:
624 if global_namespace is None:
625 self.global_namespace = {}
625 self.global_namespace = {}
626 else:
626 else:
627 self.global_namespace = global_namespace
627 self.global_namespace = global_namespace
628
628
629 super(Completer, self).__init__(**kwargs)
629 super(Completer, self).__init__(**kwargs)
630
630
631 def complete(self, text, state):
631 def complete(self, text, state):
632 """Return the next possible completion for 'text'.
632 """Return the next possible completion for 'text'.
633
633
634 This is called successively with state == 0, 1, 2, ... until it
634 This is called successively with state == 0, 1, 2, ... until it
635 returns None. The completion should begin with 'text'.
635 returns None. The completion should begin with 'text'.
636
636
637 """
637 """
638 if self.use_main_ns:
638 if self.use_main_ns:
639 self.namespace = __main__.__dict__
639 self.namespace = __main__.__dict__
640
640
641 if state == 0:
641 if state == 0:
642 if "." in text:
642 if "." in text:
643 self.matches = self.attr_matches(text)
643 self.matches = self.attr_matches(text)
644 else:
644 else:
645 self.matches = self.global_matches(text)
645 self.matches = self.global_matches(text)
646 try:
646 try:
647 return self.matches[state]
647 return self.matches[state]
648 except IndexError:
648 except IndexError:
649 return None
649 return None
650
650
651 def global_matches(self, text):
651 def global_matches(self, text):
652 """Compute matches when text is a simple name.
652 """Compute matches when text is a simple name.
653
653
654 Return a list of all keywords, built-in functions and names currently
654 Return a list of all keywords, built-in functions and names currently
655 defined in self.namespace or self.global_namespace that match.
655 defined in self.namespace or self.global_namespace that match.
656
656
657 """
657 """
658 matches = []
658 matches = []
659 match_append = matches.append
659 match_append = matches.append
660 n = len(text)
660 n = len(text)
661 for lst in [keyword.kwlist,
661 for lst in [keyword.kwlist,
662 builtin_mod.__dict__.keys(),
662 builtin_mod.__dict__.keys(),
663 self.namespace.keys(),
663 self.namespace.keys(),
664 self.global_namespace.keys()]:
664 self.global_namespace.keys()]:
665 for word in lst:
665 for word in lst:
666 if word[:n] == text and word != "__builtins__":
666 if word[:n] == text and word != "__builtins__":
667 match_append(word)
667 match_append(word)
668
668
669 snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
669 snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
670 for lst in [self.namespace.keys(),
670 for lst in [self.namespace.keys(),
671 self.global_namespace.keys()]:
671 self.global_namespace.keys()]:
672 shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
672 shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
673 for word in lst if snake_case_re.match(word)}
673 for word in lst if snake_case_re.match(word)}
674 for word in shortened.keys():
674 for word in shortened.keys():
675 if word[:n] == text and word != "__builtins__":
675 if word[:n] == text and word != "__builtins__":
676 match_append(shortened[word])
676 match_append(shortened[word])
677 return matches
677 return matches
678
678
679 def attr_matches(self, text):
679 def attr_matches(self, text):
680 """Compute matches when text contains a dot.
680 """Compute matches when text contains a dot.
681
681
682 Assuming the text is of the form NAME.NAME....[NAME], and is
682 Assuming the text is of the form NAME.NAME....[NAME], and is
683 evaluatable in self.namespace or self.global_namespace, it will be
683 evaluatable in self.namespace or self.global_namespace, it will be
684 evaluated and its attributes (as revealed by dir()) are used as
684 evaluated and its attributes (as revealed by dir()) are used as
685 possible completions. (For class instances, class members are are
685 possible completions. (For class instances, class members are are
686 also considered.)
686 also considered.)
687
687
688 WARNING: this can still invoke arbitrary C code, if an object
688 WARNING: this can still invoke arbitrary C code, if an object
689 with a __getattr__ hook is evaluated.
689 with a __getattr__ hook is evaluated.
690
690
691 """
691 """
692
692
693 # Another option, seems to work great. Catches things like ''.<tab>
693 # Another option, seems to work great. Catches things like ''.<tab>
694 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
694 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
695
695
696 if m:
696 if m:
697 expr, attr = m.group(1, 3)
697 expr, attr = m.group(1, 3)
698 elif self.greedy:
698 elif self.greedy:
699 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
699 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
700 if not m2:
700 if not m2:
701 return []
701 return []
702 expr, attr = m2.group(1,2)
702 expr, attr = m2.group(1,2)
703 else:
703 else:
704 return []
704 return []
705
705
706 try:
706 try:
707 obj = eval(expr, self.namespace)
707 obj = eval(expr, self.namespace)
708 except:
708 except:
709 try:
709 try:
710 obj = eval(expr, self.global_namespace)
710 obj = eval(expr, self.global_namespace)
711 except:
711 except:
712 return []
712 return []
713
713
714 if self.limit_to__all__ and hasattr(obj, '__all__'):
714 if self.limit_to__all__ and hasattr(obj, '__all__'):
715 words = get__all__entries(obj)
715 words = get__all__entries(obj)
716 else:
716 else:
717 words = dir2(obj)
717 words = dir2(obj)
718
718
719 try:
719 try:
720 words = generics.complete_object(obj, words)
720 words = generics.complete_object(obj, words)
721 except TryNext:
721 except TryNext:
722 pass
722 pass
723 except AssertionError:
723 except AssertionError:
724 raise
724 raise
725 except Exception:
725 except Exception:
726 # Silence errors from completion function
726 # Silence errors from completion function
727 #raise # dbg
727 #raise # dbg
728 pass
728 pass
729 # Build match list to return
729 # Build match list to return
730 n = len(attr)
730 n = len(attr)
731 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
731 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
732
732
733
733
734 def get__all__entries(obj):
734 def get__all__entries(obj):
735 """returns the strings in the __all__ attribute"""
735 """returns the strings in the __all__ attribute"""
736 try:
736 try:
737 words = getattr(obj, '__all__')
737 words = getattr(obj, '__all__')
738 except:
738 except:
739 return []
739 return []
740
740
741 return [w for w in words if isinstance(w, str)]
741 return [w for w in words if isinstance(w, str)]
742
742
743
743
744 def match_dict_keys(keys: List[str], prefix: str, delims: str):
744 def match_dict_keys(keys: List[str], prefix: str, delims: str):
745 """Used by dict_key_matches, matching the prefix to a list of keys
745 """Used by dict_key_matches, matching the prefix to a list of keys
746
746
747 Parameters
747 Parameters
748 ==========
748 ==========
749 keys:
749 keys:
750 list of keys in dictionary currently being completed.
750 list of keys in dictionary currently being completed.
751 prefix:
751 prefix:
752 Part of the text already typed by the user. e.g. `mydict[b'fo`
752 Part of the text already typed by the user. e.g. `mydict[b'fo`
753 delims:
753 delims:
754 String of delimiters to consider when finding the current key.
754 String of delimiters to consider when finding the current key.
755
755
756 Returns
756 Returns
757 =======
757 =======
758
758
759 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
759 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
760 ``quote`` being the quote that need to be used to close current string.
760 ``quote`` being the quote that need to be used to close current string.
761 ``token_start`` the position where the replacement should start occurring,
761 ``token_start`` the position where the replacement should start occurring,
762 ``matches`` a list of replacement/completion
762 ``matches`` a list of replacement/completion
763
763
764 """
764 """
765 if not prefix:
765 if not prefix:
766 return None, 0, [repr(k) for k in keys
766 return None, 0, [repr(k) for k in keys
767 if isinstance(k, (str, bytes))]
767 if isinstance(k, (str, bytes))]
768 quote_match = re.search('["\']', prefix)
768 quote_match = re.search('["\']', prefix)
769 quote = quote_match.group()
769 quote = quote_match.group()
770 try:
770 try:
771 prefix_str = eval(prefix + quote, {})
771 prefix_str = eval(prefix + quote, {})
772 except Exception:
772 except Exception:
773 return None, 0, []
773 return None, 0, []
774
774
775 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
775 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
776 token_match = re.search(pattern, prefix, re.UNICODE)
776 token_match = re.search(pattern, prefix, re.UNICODE)
777 token_start = token_match.start()
777 token_start = token_match.start()
778 token_prefix = token_match.group()
778 token_prefix = token_match.group()
779
779
780 matched = []
780 matched = []
781 for key in keys:
781 for key in keys:
782 try:
782 try:
783 if not key.startswith(prefix_str):
783 if not key.startswith(prefix_str):
784 continue
784 continue
785 except (AttributeError, TypeError, UnicodeError):
785 except (AttributeError, TypeError, UnicodeError):
786 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
786 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
787 continue
787 continue
788
788
789 # reformat remainder of key to begin with prefix
789 # reformat remainder of key to begin with prefix
790 rem = key[len(prefix_str):]
790 rem = key[len(prefix_str):]
791 # force repr wrapped in '
791 # force repr wrapped in '
792 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
792 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
793 if rem_repr.startswith('u') and prefix[0] not in 'uU':
793 if rem_repr.startswith('u') and prefix[0] not in 'uU':
794 # Found key is unicode, but prefix is Py2 string.
794 # Found key is unicode, but prefix is Py2 string.
795 # Therefore attempt to interpret key as string.
795 # Therefore attempt to interpret key as string.
796 try:
796 try:
797 rem_repr = repr(rem.encode('ascii') + '"')
797 rem_repr = repr(rem.encode('ascii') + '"')
798 except UnicodeEncodeError:
798 except UnicodeEncodeError:
799 continue
799 continue
800
800
801 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
801 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
802 if quote == '"':
802 if quote == '"':
803 # The entered prefix is quoted with ",
803 # The entered prefix is quoted with ",
804 # but the match is quoted with '.
804 # but the match is quoted with '.
805 # A contained " hence needs escaping for comparison:
805 # A contained " hence needs escaping for comparison:
806 rem_repr = rem_repr.replace('"', '\\"')
806 rem_repr = rem_repr.replace('"', '\\"')
807
807
808 # then reinsert prefix from start of token
808 # then reinsert prefix from start of token
809 matched.append('%s%s' % (token_prefix, rem_repr))
809 matched.append('%s%s' % (token_prefix, rem_repr))
810 return quote, token_start, matched
810 return quote, token_start, matched
811
811
812
812
813 def cursor_to_position(text:str, line:int, column:int)->int:
813 def cursor_to_position(text:str, line:int, column:int)->int:
814 """
814 """
815
815
816 Convert the (line,column) position of the cursor in text to an offset in a
816 Convert the (line,column) position of the cursor in text to an offset in a
817 string.
817 string.
818
818
819 Parameters
819 Parameters
820 ----------
820 ----------
821
821
822 text : str
822 text : str
823 The text in which to calculate the cursor offset
823 The text in which to calculate the cursor offset
824 line : int
824 line : int
825 Line of the cursor; 0-indexed
825 Line of the cursor; 0-indexed
826 column : int
826 column : int
827 Column of the cursor 0-indexed
827 Column of the cursor 0-indexed
828
828
829 Return
829 Return
830 ------
830 ------
831 Position of the cursor in ``text``, 0-indexed.
831 Position of the cursor in ``text``, 0-indexed.
832
832
833 See Also
833 See Also
834 --------
834 --------
835 position_to_cursor: reciprocal of this function
835 position_to_cursor: reciprocal of this function
836
836
837 """
837 """
838 lines = text.split('\n')
838 lines = text.split('\n')
839 assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
839 assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
840
840
841 return sum(len(l) + 1 for l in lines[:line]) + column
841 return sum(len(l) + 1 for l in lines[:line]) + column
842
842
843 def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
843 def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
844 """
844 """
845 Convert the position of the cursor in text (0 indexed) to a line
845 Convert the position of the cursor in text (0 indexed) to a line
846 number(0-indexed) and a column number (0-indexed) pair
846 number(0-indexed) and a column number (0-indexed) pair
847
847
848 Position should be a valid position in ``text``.
848 Position should be a valid position in ``text``.
849
849
850 Parameters
850 Parameters
851 ----------
851 ----------
852
852
853 text : str
853 text : str
854 The text in which to calculate the cursor offset
854 The text in which to calculate the cursor offset
855 offset : int
855 offset : int
856 Position of the cursor in ``text``, 0-indexed.
856 Position of the cursor in ``text``, 0-indexed.
857
857
858 Return
858 Return
859 ------
859 ------
860 (line, column) : (int, int)
860 (line, column) : (int, int)
861 Line of the cursor; 0-indexed, column of the cursor 0-indexed
861 Line of the cursor; 0-indexed, column of the cursor 0-indexed
862
862
863
863
864 See Also
864 See Also
865 --------
865 --------
866 cursor_to_position : reciprocal of this function
866 cursor_to_position : reciprocal of this function
867
867
868
868
869 """
869 """
870
870
871 assert 0 < offset <= len(text) , "0 < %s <= %s" % (offset , len(text))
871 assert 0 < offset <= len(text) , "0 < %s <= %s" % (offset , len(text))
872
872
873 before = text[:offset]
873 before = text[:offset]
874 blines = before.split('\n') # ! splitnes trim trailing \n
874 blines = before.split('\n') # ! splitnes trim trailing \n
875 line = before.count('\n')
875 line = before.count('\n')
876 col = len(blines[-1])
876 col = len(blines[-1])
877 return line, col
877 return line, col
878
878
879
879
880 def _safe_isinstance(obj, module, class_name):
880 def _safe_isinstance(obj, module, class_name):
881 """Checks if obj is an instance of module.class_name if loaded
881 """Checks if obj is an instance of module.class_name if loaded
882 """
882 """
883 return (module in sys.modules and
883 return (module in sys.modules and
884 isinstance(obj, getattr(import_module(module), class_name)))
884 isinstance(obj, getattr(import_module(module), class_name)))
885
885
886
886
887 def back_unicode_name_matches(text):
887 def back_unicode_name_matches(text):
888 u"""Match unicode characters back to unicode name
888 u"""Match unicode characters back to unicode name
889
889
890 This does ``☃`` -> ``\\snowman``
890 This does ``☃`` -> ``\\snowman``
891
891
892 Note that snowman is not a valid python3 combining character but will be expanded.
892 Note that snowman is not a valid python3 combining character but will be expanded.
893 Though it will not recombine back to the snowman character by the completion machinery.
893 Though it will not recombine back to the snowman character by the completion machinery.
894
894
895 This will not either back-complete standard sequences like \\n, \\b ...
895 This will not either back-complete standard sequences like \\n, \\b ...
896
896
897 Used on Python 3 only.
897 Used on Python 3 only.
898 """
898 """
899 if len(text)<2:
899 if len(text)<2:
900 return u'', ()
900 return u'', ()
901 maybe_slash = text[-2]
901 maybe_slash = text[-2]
902 if maybe_slash != '\\':
902 if maybe_slash != '\\':
903 return u'', ()
903 return u'', ()
904
904
905 char = text[-1]
905 char = text[-1]
906 # no expand on quote for completion in strings.
906 # no expand on quote for completion in strings.
907 # nor backcomplete standard ascii keys
907 # nor backcomplete standard ascii keys
908 if char in string.ascii_letters or char in ['"',"'"]:
908 if char in string.ascii_letters or char in ['"',"'"]:
909 return u'', ()
909 return u'', ()
910 try :
910 try :
911 unic = unicodedata.name(char)
911 unic = unicodedata.name(char)
912 return '\\'+char,['\\'+unic]
912 return '\\'+char,['\\'+unic]
913 except KeyError:
913 except KeyError:
914 pass
914 pass
915 return u'', ()
915 return u'', ()
916
916
917 def back_latex_name_matches(text:str):
917 def back_latex_name_matches(text:str):
918 """Match latex characters back to unicode name
918 """Match latex characters back to unicode name
919
919
920 This does ``\\ℵ`` -> ``\\aleph``
920 This does ``\\ℵ`` -> ``\\aleph``
921
921
922 Used on Python 3 only.
922 Used on Python 3 only.
923 """
923 """
924 if len(text)<2:
924 if len(text)<2:
925 return u'', ()
925 return u'', ()
926 maybe_slash = text[-2]
926 maybe_slash = text[-2]
927 if maybe_slash != '\\':
927 if maybe_slash != '\\':
928 return u'', ()
928 return u'', ()
929
929
930
930
931 char = text[-1]
931 char = text[-1]
932 # no expand on quote for completion in strings.
932 # no expand on quote for completion in strings.
933 # nor backcomplete standard ascii keys
933 # nor backcomplete standard ascii keys
934 if char in string.ascii_letters or char in ['"',"'"]:
934 if char in string.ascii_letters or char in ['"',"'"]:
935 return u'', ()
935 return u'', ()
936 try :
936 try :
937 latex = reverse_latex_symbol[char]
937 latex = reverse_latex_symbol[char]
938 # '\\' replace the \ as well
938 # '\\' replace the \ as well
939 return '\\'+char,[latex]
939 return '\\'+char,[latex]
940 except KeyError:
940 except KeyError:
941 pass
941 pass
942 return u'', ()
942 return u'', ()
943
943
944
944
945 def _formatparamchildren(parameter) -> str:
945 def _formatparamchildren(parameter) -> str:
946 """
946 """
947 Get parameter name and value from Jedi Private API
947 Get parameter name and value from Jedi Private API
948
948
949 Jedi does not expose a simple way to get `param=value` from its API.
949 Jedi does not expose a simple way to get `param=value` from its API.
950
950
951 Prameter
951 Prameter
952 ========
952 ========
953
953
954 parameter:
954 parameter:
955 Jedi's function `Param`
955 Jedi's function `Param`
956
956
957 Returns
957 Returns
958 =======
958 =======
959
959
960 A string like 'a', 'b=1', '*args', '**kwargs'
960 A string like 'a', 'b=1', '*args', '**kwargs'
961
961
962
962
963 """
963 """
964 description = parameter.description
964 description = parameter.description
965 if not description.startswith('param '):
965 if not description.startswith('param '):
966 raise ValueError('Jedi function parameter description have change format.'
966 raise ValueError('Jedi function parameter description have change format.'
967 'Expected "param ...", found %r".' % description)
967 'Expected "param ...", found %r".' % description)
968 return description[6:]
968 return description[6:]
969
969
970 def _make_signature(completion)-> str:
970 def _make_signature(completion)-> str:
971 """
971 """
972 Make the signature from a jedi completion
972 Make the signature from a jedi completion
973
973
974 Parameter
974 Parameter
975 =========
975 =========
976
976
977 completion: jedi.Completion
977 completion: jedi.Completion
978 object does not complete a function type
978 object does not complete a function type
979
979
980 Returns
980 Returns
981 =======
981 =======
982
982
983 a string consisting of the function signature, with the parenthesis but
983 a string consisting of the function signature, with the parenthesis but
984 without the function name. example:
984 without the function name. example:
985 `(a, *args, b=1, **kwargs)`
985 `(a, *args, b=1, **kwargs)`
986
986
987 """
987 """
988
988
989 return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f])
989 return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f])
990
990
991 class IPCompleter(Completer):
991 class IPCompleter(Completer):
992 """Extension of the completer class with IPython-specific features"""
992 """Extension of the completer class with IPython-specific features"""
993
993
994 @observe('greedy')
994 @observe('greedy')
995 def _greedy_changed(self, change):
995 def _greedy_changed(self, change):
996 """update the splitter and readline delims when greedy is changed"""
996 """update the splitter and readline delims when greedy is changed"""
997 if change['new']:
997 if change['new']:
998 self.splitter.delims = GREEDY_DELIMS
998 self.splitter.delims = GREEDY_DELIMS
999 else:
999 else:
1000 self.splitter.delims = DELIMS
1000 self.splitter.delims = DELIMS
1001
1001
1002 merge_completions = Bool(True,
1002 merge_completions = Bool(True,
1003 help="""Whether to merge completion results into a single list
1003 help="""Whether to merge completion results into a single list
1004
1004
1005 If False, only the completion results from the first non-empty
1005 If False, only the completion results from the first non-empty
1006 completer will be returned.
1006 completer will be returned.
1007 """
1007 """
1008 ).tag(config=True)
1008 ).tag(config=True)
1009 omit__names = Enum((0,1,2), default_value=2,
1009 omit__names = Enum((0,1,2), default_value=2,
1010 help="""Instruct the completer to omit private method names
1010 help="""Instruct the completer to omit private method names
1011
1011
1012 Specifically, when completing on ``object.<tab>``.
1012 Specifically, when completing on ``object.<tab>``.
1013
1013
1014 When 2 [default]: all names that start with '_' will be excluded.
1014 When 2 [default]: all names that start with '_' will be excluded.
1015
1015
1016 When 1: all 'magic' names (``__foo__``) will be excluded.
1016 When 1: all 'magic' names (``__foo__``) will be excluded.
1017
1017
1018 When 0: nothing will be excluded.
1018 When 0: nothing will be excluded.
1019 """
1019 """
1020 ).tag(config=True)
1020 ).tag(config=True)
1021 limit_to__all__ = Bool(False,
1021 limit_to__all__ = Bool(False,
1022 help="""
1022 help="""
1023 DEPRECATED as of version 5.0.
1023 DEPRECATED as of version 5.0.
1024
1024
1025 Instruct the completer to use __all__ for the completion
1025 Instruct the completer to use __all__ for the completion
1026
1026
1027 Specifically, when completing on ``object.<tab>``.
1027 Specifically, when completing on ``object.<tab>``.
1028
1028
1029 When True: only those names in obj.__all__ will be included.
1029 When True: only those names in obj.__all__ will be included.
1030
1030
1031 When False [default]: the __all__ attribute is ignored
1031 When False [default]: the __all__ attribute is ignored
1032 """,
1032 """,
1033 ).tag(config=True)
1033 ).tag(config=True)
1034
1034
1035 @observe('limit_to__all__')
1035 @observe('limit_to__all__')
1036 def _limit_to_all_changed(self, change):
1036 def _limit_to_all_changed(self, change):
1037 warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration '
1037 warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration '
1038 'value has been deprecated since IPython 5.0, will be made to have '
1038 'value has been deprecated since IPython 5.0, will be made to have '
1039 'no effects and then removed in future version of IPython.',
1039 'no effects and then removed in future version of IPython.',
1040 UserWarning)
1040 UserWarning)
1041
1041
1042 def __init__(self, shell=None, namespace=None, global_namespace=None,
1042 def __init__(self, shell=None, namespace=None, global_namespace=None,
1043 use_readline=_deprecation_readline_sentinel, config=None, **kwargs):
1043 use_readline=_deprecation_readline_sentinel, config=None, **kwargs):
1044 """IPCompleter() -> completer
1044 """IPCompleter() -> completer
1045
1045
1046 Return a completer object.
1046 Return a completer object.
1047
1047
1048 Parameters
1048 Parameters
1049 ----------
1049 ----------
1050
1050
1051 shell
1051 shell
1052 a pointer to the ipython shell itself. This is needed
1052 a pointer to the ipython shell itself. This is needed
1053 because this completer knows about magic functions, and those can
1053 because this completer knows about magic functions, and those can
1054 only be accessed via the ipython instance.
1054 only be accessed via the ipython instance.
1055
1055
1056 namespace : dict, optional
1056 namespace : dict, optional
1057 an optional dict where completions are performed.
1057 an optional dict where completions are performed.
1058
1058
1059 global_namespace : dict, optional
1059 global_namespace : dict, optional
1060 secondary optional dict for completions, to
1060 secondary optional dict for completions, to
1061 handle cases (such as IPython embedded inside functions) where
1061 handle cases (such as IPython embedded inside functions) where
1062 both Python scopes are visible.
1062 both Python scopes are visible.
1063
1063
1064 use_readline : bool, optional
1064 use_readline : bool, optional
1065 DEPRECATED, ignored since IPython 6.0, will have no effects
1065 DEPRECATED, ignored since IPython 6.0, will have no effects
1066 """
1066 """
1067
1067
1068 self.magic_escape = ESC_MAGIC
1068 self.magic_escape = ESC_MAGIC
1069 self.splitter = CompletionSplitter()
1069 self.splitter = CompletionSplitter()
1070
1070
1071 if use_readline is not _deprecation_readline_sentinel:
1071 if use_readline is not _deprecation_readline_sentinel:
1072 warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.',
1072 warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.',
1073 DeprecationWarning, stacklevel=2)
1073 DeprecationWarning, stacklevel=2)
1074
1074
1075 # _greedy_changed() depends on splitter and readline being defined:
1075 # _greedy_changed() depends on splitter and readline being defined:
1076 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
1076 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
1077 config=config, **kwargs)
1077 config=config, **kwargs)
1078
1078
1079 # List where completion matches will be stored
1079 # List where completion matches will be stored
1080 self.matches = []
1080 self.matches = []
1081 self.shell = shell
1081 self.shell = shell
1082 # Regexp to split filenames with spaces in them
1082 # Regexp to split filenames with spaces in them
1083 self.space_name_re = re.compile(r'([^\\] )')
1083 self.space_name_re = re.compile(r'([^\\] )')
1084 # Hold a local ref. to glob.glob for speed
1084 # Hold a local ref. to glob.glob for speed
1085 self.glob = glob.glob
1085 self.glob = glob.glob
1086
1086
1087 # Determine if we are running on 'dumb' terminals, like (X)Emacs
1087 # Determine if we are running on 'dumb' terminals, like (X)Emacs
1088 # buffers, to avoid completion problems.
1088 # buffers, to avoid completion problems.
1089 term = os.environ.get('TERM','xterm')
1089 term = os.environ.get('TERM','xterm')
1090 self.dumb_terminal = term in ['dumb','emacs']
1090 self.dumb_terminal = term in ['dumb','emacs']
1091
1091
1092 # Special handling of backslashes needed in win32 platforms
1092 # Special handling of backslashes needed in win32 platforms
1093 if sys.platform == "win32":
1093 if sys.platform == "win32":
1094 self.clean_glob = self._clean_glob_win32
1094 self.clean_glob = self._clean_glob_win32
1095 else:
1095 else:
1096 self.clean_glob = self._clean_glob
1096 self.clean_glob = self._clean_glob
1097
1097
1098 #regexp to parse docstring for function signature
1098 #regexp to parse docstring for function signature
1099 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1099 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1100 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1100 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1101 #use this if positional argument name is also needed
1101 #use this if positional argument name is also needed
1102 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
1102 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
1103
1103
1104 # All active matcher routines for completion
1104 # All active matcher routines for completion
1105 self.matchers = [
1105 self.matchers = [
1106 self.python_matches,
1106 self.python_matches,
1107 self.file_matches,
1107 self.file_matches,
1108 self.magic_matches,
1108 self.magic_matches,
1109 self.python_func_kw_matches,
1109 self.python_func_kw_matches,
1110 self.dict_key_matches,
1110 self.dict_key_matches,
1111 ]
1111 ]
1112 self.magic_arg_matchers = [
1112 self.magic_arg_matchers = [
1113 self.magic_config_matches,
1113 self.magic_config_matches,
1114 self.magic_color_matches,
1114 self.magic_color_matches,
1115 ]
1115 ]
1116
1116
1117 # This is set externally by InteractiveShell
1117 # This is set externally by InteractiveShell
1118 self.custom_completers = None
1118 self.custom_completers = None
1119
1119
1120 def all_completions(self, text):
1120 def all_completions(self, text):
1121 """
1121 """
1122 Wrapper around the complete method for the benefit of emacs.
1122 Wrapper around the complete method for the benefit of emacs.
1123 """
1123 """
1124 return self.complete(text)[1]
1124 return self.complete(text)[1]
1125
1125
1126 def _clean_glob(self, text):
1126 def _clean_glob(self, text):
1127 return self.glob("%s*" % text)
1127 return self.glob("%s*" % text)
1128
1128
1129 def _clean_glob_win32(self,text):
1129 def _clean_glob_win32(self,text):
1130 return [f.replace("\\","/")
1130 return [f.replace("\\","/")
1131 for f in self.glob("%s*" % text)]
1131 for f in self.glob("%s*" % text)]
1132
1132
1133 def file_matches(self, text):
1133 def file_matches(self, text):
1134 """Match filenames, expanding ~USER type strings.
1134 """Match filenames, expanding ~USER type strings.
1135
1135
1136 Most of the seemingly convoluted logic in this completer is an
1136 Most of the seemingly convoluted logic in this completer is an
1137 attempt to handle filenames with spaces in them. And yet it's not
1137 attempt to handle filenames with spaces in them. And yet it's not
1138 quite perfect, because Python's readline doesn't expose all of the
1138 quite perfect, because Python's readline doesn't expose all of the
1139 GNU readline details needed for this to be done correctly.
1139 GNU readline details needed for this to be done correctly.
1140
1140
1141 For a filename with a space in it, the printed completions will be
1141 For a filename with a space in it, the printed completions will be
1142 only the parts after what's already been typed (instead of the
1142 only the parts after what's already been typed (instead of the
1143 full completions, as is normally done). I don't think with the
1143 full completions, as is normally done). I don't think with the
1144 current (as of Python 2.3) Python readline it's possible to do
1144 current (as of Python 2.3) Python readline it's possible to do
1145 better."""
1145 better."""
1146
1146
1147 # chars that require escaping with backslash - i.e. chars
1147 # chars that require escaping with backslash - i.e. chars
1148 # that readline treats incorrectly as delimiters, but we
1148 # that readline treats incorrectly as delimiters, but we
1149 # don't want to treat as delimiters in filename matching
1149 # don't want to treat as delimiters in filename matching
1150 # when escaped with backslash
1150 # when escaped with backslash
1151 if text.startswith('!'):
1151 if text.startswith('!'):
1152 text = text[1:]
1152 text = text[1:]
1153 text_prefix = u'!'
1153 text_prefix = u'!'
1154 else:
1154 else:
1155 text_prefix = u''
1155 text_prefix = u''
1156
1156
1157 text_until_cursor = self.text_until_cursor
1157 text_until_cursor = self.text_until_cursor
1158 # track strings with open quotes
1158 # track strings with open quotes
1159 open_quotes = has_open_quotes(text_until_cursor)
1159 open_quotes = has_open_quotes(text_until_cursor)
1160
1160
1161 if '(' in text_until_cursor or '[' in text_until_cursor:
1161 if '(' in text_until_cursor or '[' in text_until_cursor:
1162 lsplit = text
1162 lsplit = text
1163 else:
1163 else:
1164 try:
1164 try:
1165 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1165 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1166 lsplit = arg_split(text_until_cursor)[-1]
1166 lsplit = arg_split(text_until_cursor)[-1]
1167 except ValueError:
1167 except ValueError:
1168 # typically an unmatched ", or backslash without escaped char.
1168 # typically an unmatched ", or backslash without escaped char.
1169 if open_quotes:
1169 if open_quotes:
1170 lsplit = text_until_cursor.split(open_quotes)[-1]
1170 lsplit = text_until_cursor.split(open_quotes)[-1]
1171 else:
1171 else:
1172 return []
1172 return []
1173 except IndexError:
1173 except IndexError:
1174 # tab pressed on empty line
1174 # tab pressed on empty line
1175 lsplit = ""
1175 lsplit = ""
1176
1176
1177 if not open_quotes and lsplit != protect_filename(lsplit):
1177 if not open_quotes and lsplit != protect_filename(lsplit):
1178 # if protectables are found, do matching on the whole escaped name
1178 # if protectables are found, do matching on the whole escaped name
1179 has_protectables = True
1179 has_protectables = True
1180 text0,text = text,lsplit
1180 text0,text = text,lsplit
1181 else:
1181 else:
1182 has_protectables = False
1182 has_protectables = False
1183 text = os.path.expanduser(text)
1183 text = os.path.expanduser(text)
1184
1184
1185 if text == "":
1185 if text == "":
1186 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1186 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1187
1187
1188 # Compute the matches from the filesystem
1188 # Compute the matches from the filesystem
1189 if sys.platform == 'win32':
1189 if sys.platform == 'win32':
1190 m0 = self.clean_glob(text)
1190 m0 = self.clean_glob(text)
1191 else:
1191 else:
1192 m0 = self.clean_glob(text.replace('\\', ''))
1192 m0 = self.clean_glob(text.replace('\\', ''))
1193
1193
1194 if has_protectables:
1194 if has_protectables:
1195 # If we had protectables, we need to revert our changes to the
1195 # If we had protectables, we need to revert our changes to the
1196 # beginning of filename so that we don't double-write the part
1196 # beginning of filename so that we don't double-write the part
1197 # of the filename we have so far
1197 # of the filename we have so far
1198 len_lsplit = len(lsplit)
1198 len_lsplit = len(lsplit)
1199 matches = [text_prefix + text0 +
1199 matches = [text_prefix + text0 +
1200 protect_filename(f[len_lsplit:]) for f in m0]
1200 protect_filename(f[len_lsplit:]) for f in m0]
1201 else:
1201 else:
1202 if open_quotes:
1202 if open_quotes:
1203 # if we have a string with an open quote, we don't need to
1203 # if we have a string with an open quote, we don't need to
1204 # protect the names beyond the quote (and we _shouldn't_, as
1204 # protect the names beyond the quote (and we _shouldn't_, as
1205 # it would cause bugs when the filesystem call is made).
1205 # it would cause bugs when the filesystem call is made).
1206 matches = m0 if sys.platform == "win32" else\
1206 matches = m0 if sys.platform == "win32" else\
1207 [protect_filename(f, open_quotes) for f in m0]
1207 [protect_filename(f, open_quotes) for f in m0]
1208 else:
1208 else:
1209 matches = [text_prefix +
1209 matches = [text_prefix +
1210 protect_filename(f) for f in m0]
1210 protect_filename(f) for f in m0]
1211
1211
1212 # Mark directories in input list by appending '/' to their names.
1212 # Mark directories in input list by appending '/' to their names.
1213 return [x+'/' if os.path.isdir(x) else x for x in matches]
1213 return [x+'/' if os.path.isdir(x) else x for x in matches]
1214
1214
1215 def magic_matches(self, text):
1215 def magic_matches(self, text):
1216 """Match magics"""
1216 """Match magics"""
1217 # Get all shell magics now rather than statically, so magics loaded at
1217 # Get all shell magics now rather than statically, so magics loaded at
1218 # runtime show up too.
1218 # runtime show up too.
1219 lsm = self.shell.magics_manager.lsmagic()
1219 lsm = self.shell.magics_manager.lsmagic()
1220 line_magics = lsm['line']
1220 line_magics = lsm['line']
1221 cell_magics = lsm['cell']
1221 cell_magics = lsm['cell']
1222 pre = self.magic_escape
1222 pre = self.magic_escape
1223 pre2 = pre+pre
1223 pre2 = pre+pre
1224
1224
1225 explicit_magic = text.startswith(pre)
1225 explicit_magic = text.startswith(pre)
1226
1226
1227 # Completion logic:
1227 # Completion logic:
1228 # - user gives %%: only do cell magics
1228 # - user gives %%: only do cell magics
1229 # - user gives %: do both line and cell magics
1229 # - user gives %: do both line and cell magics
1230 # - no prefix: do both
1230 # - no prefix: do both
1231 # In other words, line magics are skipped if the user gives %% explicitly
1231 # In other words, line magics are skipped if the user gives %% explicitly
1232 #
1232 #
1233 # We also exclude magics that match any currently visible names:
1233 # We also exclude magics that match any currently visible names:
1234 # https://github.com/ipython/ipython/issues/4877, unless the user has
1234 # https://github.com/ipython/ipython/issues/4877, unless the user has
1235 # typed a %:
1235 # typed a %:
1236 # https://github.com/ipython/ipython/issues/10754
1236 # https://github.com/ipython/ipython/issues/10754
1237 bare_text = text.lstrip(pre)
1237 bare_text = text.lstrip(pre)
1238 global_matches = self.global_matches(bare_text)
1238 global_matches = self.global_matches(bare_text)
1239 if not explicit_magic:
1239 if not explicit_magic:
1240 def matches(magic):
1240 def matches(magic):
1241 """
1241 """
1242 Filter magics, in particular remove magics that match
1242 Filter magics, in particular remove magics that match
1243 a name present in global namespace.
1243 a name present in global namespace.
1244 """
1244 """
1245 return ( magic.startswith(bare_text) and
1245 return ( magic.startswith(bare_text) and
1246 magic not in global_matches )
1246 magic not in global_matches )
1247 else:
1247 else:
1248 def matches(magic):
1248 def matches(magic):
1249 return magic.startswith(bare_text)
1249 return magic.startswith(bare_text)
1250
1250
1251 comp = [ pre2+m for m in cell_magics if matches(m)]
1251 comp = [ pre2+m for m in cell_magics if matches(m)]
1252 if not text.startswith(pre2):
1252 if not text.startswith(pre2):
1253 comp += [ pre+m for m in line_magics if matches(m)]
1253 comp += [ pre+m for m in line_magics if matches(m)]
1254
1254
1255 return comp
1255 return comp
1256
1256
1257 def magic_config_matches(self, text:str) -> List[str]:
1257 def magic_config_matches(self, text:str) -> List[str]:
1258 """ Match class names and attributes for %config magic """
1258 """ Match class names and attributes for %config magic """
1259 texts = text.strip().split()
1259 texts = text.strip().split()
1260
1260
1261 if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'):
1261 if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'):
1262 # get all configuration classes
1262 # get all configuration classes
1263 classes = sorted(set([ c for c in self.shell.configurables
1263 classes = sorted(set([ c for c in self.shell.configurables
1264 if c.__class__.class_traits(config=True)
1264 if c.__class__.class_traits(config=True)
1265 ]), key=lambda x: x.__class__.__name__)
1265 ]), key=lambda x: x.__class__.__name__)
1266 classnames = [ c.__class__.__name__ for c in classes ]
1266 classnames = [ c.__class__.__name__ for c in classes ]
1267
1267
1268 # return all classnames if config or %config is given
1268 # return all classnames if config or %config is given
1269 if len(texts) == 1:
1269 if len(texts) == 1:
1270 return classnames
1270 return classnames
1271
1271
1272 # match classname
1272 # match classname
1273 classname_texts = texts[1].split('.')
1273 classname_texts = texts[1].split('.')
1274 classname = classname_texts[0]
1274 classname = classname_texts[0]
1275 classname_matches = [ c for c in classnames
1275 classname_matches = [ c for c in classnames
1276 if c.startswith(classname) ]
1276 if c.startswith(classname) ]
1277
1277
1278 # return matched classes or the matched class with attributes
1278 # return matched classes or the matched class with attributes
1279 if texts[1].find('.') < 0:
1279 if texts[1].find('.') < 0:
1280 return classname_matches
1280 return classname_matches
1281 elif len(classname_matches) == 1 and \
1281 elif len(classname_matches) == 1 and \
1282 classname_matches[0] == classname:
1282 classname_matches[0] == classname:
1283 cls = classes[classnames.index(classname)].__class__
1283 cls = classes[classnames.index(classname)].__class__
1284 help = cls.class_get_help()
1284 help = cls.class_get_help()
1285 # strip leading '--' from cl-args:
1285 # strip leading '--' from cl-args:
1286 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
1286 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
1287 return [ attr.split('=')[0]
1287 return [ attr.split('=')[0]
1288 for attr in help.strip().splitlines()
1288 for attr in help.strip().splitlines()
1289 if attr.startswith(texts[1]) ]
1289 if attr.startswith(texts[1]) ]
1290 return []
1290 return []
1291
1291
1292 def magic_color_matches(self, text:str) -> List[str] :
1292 def magic_color_matches(self, text:str) -> List[str] :
1293 """ Match color schemes for %colors magic"""
1293 """ Match color schemes for %colors magic"""
1294 texts = text.split()
1294 texts = text.split()
1295 if text.endswith(' '):
1295 if text.endswith(' '):
1296 # .split() strips off the trailing whitespace. Add '' back
1296 # .split() strips off the trailing whitespace. Add '' back
1297 # so that: '%colors ' -> ['%colors', '']
1297 # so that: '%colors ' -> ['%colors', '']
1298 texts.append('')
1298 texts.append('')
1299
1299
1300 if len(texts) == 2 and (texts[0] == 'colors' or texts[0] == '%colors'):
1300 if len(texts) == 2 and (texts[0] == 'colors' or texts[0] == '%colors'):
1301 prefix = texts[1]
1301 prefix = texts[1]
1302 return [ color for color in InspectColors.keys()
1302 return [ color for color in InspectColors.keys()
1303 if color.startswith(prefix) ]
1303 if color.startswith(prefix) ]
1304 return []
1304 return []
1305
1305
1306 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1306 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1307 """
1307 """
1308
1308
1309 Return a list of :any:`jedi.api.Completions` object from a ``text`` and
1309 Return a list of :any:`jedi.api.Completions` object from a ``text`` and
1310 cursor position.
1310 cursor position.
1311
1311
1312 Parameters
1312 Parameters
1313 ----------
1313 ----------
1314 cursor_column : int
1314 cursor_column : int
1315 column position of the cursor in ``text``, 0-indexed.
1315 column position of the cursor in ``text``, 0-indexed.
1316 cursor_line : int
1316 cursor_line : int
1317 line position of the cursor in ``text``, 0-indexed
1317 line position of the cursor in ``text``, 0-indexed
1318 text : str
1318 text : str
1319 text to complete
1319 text to complete
1320
1320
1321 Debugging
1321 Debugging
1322 ---------
1322 ---------
1323
1323
1324 If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
1324 If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
1325 object containing a string with the Jedi debug information attached.
1325 object containing a string with the Jedi debug information attached.
1326 """
1326 """
1327 namespaces = [self.namespace]
1327 namespaces = [self.namespace]
1328 if self.global_namespace is not None:
1328 if self.global_namespace is not None:
1329 namespaces.append(self.global_namespace)
1329 namespaces.append(self.global_namespace)
1330
1330
1331 completion_filter = lambda x:x
1331 completion_filter = lambda x:x
1332 # cursor_pos is an it, jedi wants line and column
1332 # cursor_pos is an it, jedi wants line and column
1333 offset = cursor_to_position(text, cursor_line, cursor_column)
1333 offset = cursor_to_position(text, cursor_line, cursor_column)
1334 # filter output if we are completing for object members
1334 # filter output if we are completing for object members
1335 if offset:
1335 if offset:
1336 pre = text[offset-1]
1336 pre = text[offset-1]
1337 if pre == '.':
1337 if pre == '.':
1338 if self.omit__names == 2:
1338 if self.omit__names == 2:
1339 completion_filter = lambda c:not c.name.startswith('_')
1339 completion_filter = lambda c:not c.name.startswith('_')
1340 elif self.omit__names == 1:
1340 elif self.omit__names == 1:
1341 completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__'))
1341 completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__'))
1342 elif self.omit__names == 0:
1342 elif self.omit__names == 0:
1343 completion_filter = lambda x:x
1343 completion_filter = lambda x:x
1344 else:
1344 else:
1345 raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names))
1345 raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names))
1346
1346
1347 interpreter = jedi.Interpreter(
1347 interpreter = jedi.Interpreter(
1348 text, namespaces, column=cursor_column, line=cursor_line + 1)
1348 text, namespaces, column=cursor_column, line=cursor_line + 1)
1349 try_jedi = True
1349 try_jedi = True
1350
1350
1351 try:
1351 try:
1352 # should we check the type of the node is Error ?
1352 # should we check the type of the node is Error ?
1353 try:
1353 try:
1354 # jedi >= 0.11
1354 # jedi >= 0.11
1355 from parso.tree import ErrorLeaf
1355 from parso.tree import ErrorLeaf
1356 except ImportError:
1356 except ImportError:
1357 # jedi < 0.11
1357 # jedi < 0.11
1358 from jedi.parser.tree import ErrorLeaf
1358 from jedi.parser.tree import ErrorLeaf
1359
1359
1360 next_to_last_tree = interpreter._get_module().tree_node.children[-2]
1360 next_to_last_tree = interpreter._get_module().tree_node.children[-2]
1361 completing_string = False
1361 completing_string = False
1362 if isinstance(next_to_last_tree, ErrorLeaf):
1362 if isinstance(next_to_last_tree, ErrorLeaf):
1363 completing_string = next_to_last_tree.value[0] in {'"', "'"}
1363 completing_string = next_to_last_tree.value[0] in {'"', "'"}
1364 # if we are in a string jedi is likely not the right candidate for
1364 # if we are in a string jedi is likely not the right candidate for
1365 # now. Skip it.
1365 # now. Skip it.
1366 try_jedi = not completing_string
1366 try_jedi = not completing_string
1367 except Exception as e:
1367 except Exception as e:
1368 # many of things can go wrong, we are using private API just don't crash.
1368 # many of things can go wrong, we are using private API just don't crash.
1369 if self.debug:
1369 if self.debug:
1370 print("Error detecting if completing a non-finished string :", e, '|')
1370 print("Error detecting if completing a non-finished string :", e, '|')
1371
1371
1372 if not try_jedi:
1372 if not try_jedi:
1373 return []
1373 return []
1374 try:
1374 try:
1375 return filter(completion_filter, interpreter.completions())
1375 return filter(completion_filter, interpreter.completions())
1376 except Exception as e:
1376 except Exception as e:
1377 if self.debug:
1377 if self.debug:
1378 return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))]
1378 return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))]
1379 else:
1379 else:
1380 return []
1380 return []
1381
1381
1382 def python_matches(self, text):
1382 def python_matches(self, text):
1383 """Match attributes or global python names"""
1383 """Match attributes or global python names"""
1384 if "." in text:
1384 if "." in text:
1385 try:
1385 try:
1386 matches = self.attr_matches(text)
1386 matches = self.attr_matches(text)
1387 if text.endswith('.') and self.omit__names:
1387 if text.endswith('.') and self.omit__names:
1388 if self.omit__names == 1:
1388 if self.omit__names == 1:
1389 # true if txt is _not_ a __ name, false otherwise:
1389 # true if txt is _not_ a __ name, false otherwise:
1390 no__name = (lambda txt:
1390 no__name = (lambda txt:
1391 re.match(r'.*\.__.*?__',txt) is None)
1391 re.match(r'.*\.__.*?__',txt) is None)
1392 else:
1392 else:
1393 # true if txt is _not_ a _ name, false otherwise:
1393 # true if txt is _not_ a _ name, false otherwise:
1394 no__name = (lambda txt:
1394 no__name = (lambda txt:
1395 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
1395 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
1396 matches = filter(no__name, matches)
1396 matches = filter(no__name, matches)
1397 except NameError:
1397 except NameError:
1398 # catches <undefined attributes>.<tab>
1398 # catches <undefined attributes>.<tab>
1399 matches = []
1399 matches = []
1400 else:
1400 else:
1401 matches = self.global_matches(text)
1401 matches = self.global_matches(text)
1402 return matches
1402 return matches
1403
1403
1404 def _default_arguments_from_docstring(self, doc):
1404 def _default_arguments_from_docstring(self, doc):
1405 """Parse the first line of docstring for call signature.
1405 """Parse the first line of docstring for call signature.
1406
1406
1407 Docstring should be of the form 'min(iterable[, key=func])\n'.
1407 Docstring should be of the form 'min(iterable[, key=func])\n'.
1408 It can also parse cython docstring of the form
1408 It can also parse cython docstring of the form
1409 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
1409 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
1410 """
1410 """
1411 if doc is None:
1411 if doc is None:
1412 return []
1412 return []
1413
1413
1414 #care only the firstline
1414 #care only the firstline
1415 line = doc.lstrip().splitlines()[0]
1415 line = doc.lstrip().splitlines()[0]
1416
1416
1417 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1417 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1418 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
1418 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
1419 sig = self.docstring_sig_re.search(line)
1419 sig = self.docstring_sig_re.search(line)
1420 if sig is None:
1420 if sig is None:
1421 return []
1421 return []
1422 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
1422 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
1423 sig = sig.groups()[0].split(',')
1423 sig = sig.groups()[0].split(',')
1424 ret = []
1424 ret = []
1425 for s in sig:
1425 for s in sig:
1426 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1426 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1427 ret += self.docstring_kwd_re.findall(s)
1427 ret += self.docstring_kwd_re.findall(s)
1428 return ret
1428 return ret
1429
1429
1430 def _default_arguments(self, obj):
1430 def _default_arguments(self, obj):
1431 """Return the list of default arguments of obj if it is callable,
1431 """Return the list of default arguments of obj if it is callable,
1432 or empty list otherwise."""
1432 or empty list otherwise."""
1433 call_obj = obj
1433 call_obj = obj
1434 ret = []
1434 ret = []
1435 if inspect.isbuiltin(obj):
1435 if inspect.isbuiltin(obj):
1436 pass
1436 pass
1437 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
1437 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
1438 if inspect.isclass(obj):
1438 if inspect.isclass(obj):
1439 #for cython embedsignature=True the constructor docstring
1439 #for cython embedsignature=True the constructor docstring
1440 #belongs to the object itself not __init__
1440 #belongs to the object itself not __init__
1441 ret += self._default_arguments_from_docstring(
1441 ret += self._default_arguments_from_docstring(
1442 getattr(obj, '__doc__', ''))
1442 getattr(obj, '__doc__', ''))
1443 # for classes, check for __init__,__new__
1443 # for classes, check for __init__,__new__
1444 call_obj = (getattr(obj, '__init__', None) or
1444 call_obj = (getattr(obj, '__init__', None) or
1445 getattr(obj, '__new__', None))
1445 getattr(obj, '__new__', None))
1446 # for all others, check if they are __call__able
1446 # for all others, check if they are __call__able
1447 elif hasattr(obj, '__call__'):
1447 elif hasattr(obj, '__call__'):
1448 call_obj = obj.__call__
1448 call_obj = obj.__call__
1449 ret += self._default_arguments_from_docstring(
1449 ret += self._default_arguments_from_docstring(
1450 getattr(call_obj, '__doc__', ''))
1450 getattr(call_obj, '__doc__', ''))
1451
1451
1452 _keeps = (inspect.Parameter.KEYWORD_ONLY,
1452 _keeps = (inspect.Parameter.KEYWORD_ONLY,
1453 inspect.Parameter.POSITIONAL_OR_KEYWORD)
1453 inspect.Parameter.POSITIONAL_OR_KEYWORD)
1454
1454
1455 try:
1455 try:
1456 sig = inspect.signature(call_obj)
1456 sig = inspect.signature(call_obj)
1457 ret.extend(k for k, v in sig.parameters.items() if
1457 ret.extend(k for k, v in sig.parameters.items() if
1458 v.kind in _keeps)
1458 v.kind in _keeps)
1459 except ValueError:
1459 except ValueError:
1460 pass
1460 pass
1461
1461
1462 return list(set(ret))
1462 return list(set(ret))
1463
1463
1464 def python_func_kw_matches(self,text):
1464 def python_func_kw_matches(self,text):
1465 """Match named parameters (kwargs) of the last open function"""
1465 """Match named parameters (kwargs) of the last open function"""
1466
1466
1467 if "." in text: # a parameter cannot be dotted
1467 if "." in text: # a parameter cannot be dotted
1468 return []
1468 return []
1469 try: regexp = self.__funcParamsRegex
1469 try: regexp = self.__funcParamsRegex
1470 except AttributeError:
1470 except AttributeError:
1471 regexp = self.__funcParamsRegex = re.compile(r'''
1471 regexp = self.__funcParamsRegex = re.compile(r'''
1472 '.*?(?<!\\)' | # single quoted strings or
1472 '.*?(?<!\\)' | # single quoted strings or
1473 ".*?(?<!\\)" | # double quoted strings or
1473 ".*?(?<!\\)" | # double quoted strings or
1474 \w+ | # identifier
1474 \w+ | # identifier
1475 \S # other characters
1475 \S # other characters
1476 ''', re.VERBOSE | re.DOTALL)
1476 ''', re.VERBOSE | re.DOTALL)
1477 # 1. find the nearest identifier that comes before an unclosed
1477 # 1. find the nearest identifier that comes before an unclosed
1478 # parenthesis before the cursor
1478 # parenthesis before the cursor
1479 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1479 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1480 tokens = regexp.findall(self.text_until_cursor)
1480 tokens = regexp.findall(self.text_until_cursor)
1481 iterTokens = reversed(tokens); openPar = 0
1481 iterTokens = reversed(tokens); openPar = 0
1482
1482
1483 for token in iterTokens:
1483 for token in iterTokens:
1484 if token == ')':
1484 if token == ')':
1485 openPar -= 1
1485 openPar -= 1
1486 elif token == '(':
1486 elif token == '(':
1487 openPar += 1
1487 openPar += 1
1488 if openPar > 0:
1488 if openPar > 0:
1489 # found the last unclosed parenthesis
1489 # found the last unclosed parenthesis
1490 break
1490 break
1491 else:
1491 else:
1492 return []
1492 return []
1493 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1493 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1494 ids = []
1494 ids = []
1495 isId = re.compile(r'\w+$').match
1495 isId = re.compile(r'\w+$').match
1496
1496
1497 while True:
1497 while True:
1498 try:
1498 try:
1499 ids.append(next(iterTokens))
1499 ids.append(next(iterTokens))
1500 if not isId(ids[-1]):
1500 if not isId(ids[-1]):
1501 ids.pop(); break
1501 ids.pop(); break
1502 if not next(iterTokens) == '.':
1502 if not next(iterTokens) == '.':
1503 break
1503 break
1504 except StopIteration:
1504 except StopIteration:
1505 break
1505 break
1506
1506
1507 # Find all named arguments already assigned to, as to avoid suggesting
1507 # Find all named arguments already assigned to, as to avoid suggesting
1508 # them again
1508 # them again
1509 usedNamedArgs = set()
1509 usedNamedArgs = set()
1510 par_level = -1
1510 par_level = -1
1511 for token, next_token in zip(tokens, tokens[1:]):
1511 for token, next_token in zip(tokens, tokens[1:]):
1512 if token == '(':
1512 if token == '(':
1513 par_level += 1
1513 par_level += 1
1514 elif token == ')':
1514 elif token == ')':
1515 par_level -= 1
1515 par_level -= 1
1516
1516
1517 if par_level != 0:
1517 if par_level != 0:
1518 continue
1518 continue
1519
1519
1520 if next_token != '=':
1520 if next_token != '=':
1521 continue
1521 continue
1522
1522
1523 usedNamedArgs.add(token)
1523 usedNamedArgs.add(token)
1524
1524
1525 # lookup the candidate callable matches either using global_matches
1525 # lookup the candidate callable matches either using global_matches
1526 # or attr_matches for dotted names
1526 # or attr_matches for dotted names
1527 if len(ids) == 1:
1527 if len(ids) == 1:
1528 callableMatches = self.global_matches(ids[0])
1528 callableMatches = self.global_matches(ids[0])
1529 else:
1529 else:
1530 callableMatches = self.attr_matches('.'.join(ids[::-1]))
1530 callableMatches = self.attr_matches('.'.join(ids[::-1]))
1531 argMatches = []
1531 argMatches = []
1532 for callableMatch in callableMatches:
1532 for callableMatch in callableMatches:
1533 try:
1533 try:
1534 namedArgs = self._default_arguments(eval(callableMatch,
1534 namedArgs = self._default_arguments(eval(callableMatch,
1535 self.namespace))
1535 self.namespace))
1536 except:
1536 except:
1537 continue
1537 continue
1538
1538
1539 # Remove used named arguments from the list, no need to show twice
1539 # Remove used named arguments from the list, no need to show twice
1540 for namedArg in set(namedArgs) - usedNamedArgs:
1540 for namedArg in set(namedArgs) - usedNamedArgs:
1541 if namedArg.startswith(text):
1541 if namedArg.startswith(text):
1542 argMatches.append(u"%s=" %namedArg)
1542 argMatches.append(u"%s=" %namedArg)
1543 return argMatches
1543 return argMatches
1544
1544
1545 def dict_key_matches(self, text):
1545 def dict_key_matches(self, text):
1546 "Match string keys in a dictionary, after e.g. 'foo[' "
1546 "Match string keys in a dictionary, after e.g. 'foo[' "
1547 def get_keys(obj):
1547 def get_keys(obj):
1548 # Objects can define their own completions by defining an
1548 # Objects can define their own completions by defining an
1549 # _ipy_key_completions_() method.
1549 # _ipy_key_completions_() method.
1550 method = get_real_method(obj, '_ipython_key_completions_')
1550 method = get_real_method(obj, '_ipython_key_completions_')
1551 if method is not None:
1551 if method is not None:
1552 return method()
1552 return method()
1553
1553
1554 # Special case some common in-memory dict-like types
1554 # Special case some common in-memory dict-like types
1555 if isinstance(obj, dict) or\
1555 if isinstance(obj, dict) or\
1556 _safe_isinstance(obj, 'pandas', 'DataFrame'):
1556 _safe_isinstance(obj, 'pandas', 'DataFrame'):
1557 try:
1557 try:
1558 return list(obj.keys())
1558 return list(obj.keys())
1559 except Exception:
1559 except Exception:
1560 return []
1560 return []
1561 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
1561 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
1562 _safe_isinstance(obj, 'numpy', 'void'):
1562 _safe_isinstance(obj, 'numpy', 'void'):
1563 return obj.dtype.names or []
1563 return obj.dtype.names or []
1564 return []
1564 return []
1565
1565
1566 try:
1566 try:
1567 regexps = self.__dict_key_regexps
1567 regexps = self.__dict_key_regexps
1568 except AttributeError:
1568 except AttributeError:
1569 dict_key_re_fmt = r'''(?x)
1569 dict_key_re_fmt = r'''(?x)
1570 ( # match dict-referring expression wrt greedy setting
1570 ( # match dict-referring expression wrt greedy setting
1571 %s
1571 %s
1572 )
1572 )
1573 \[ # open bracket
1573 \[ # open bracket
1574 \s* # and optional whitespace
1574 \s* # and optional whitespace
1575 ([uUbB]? # string prefix (r not handled)
1575 ([uUbB]? # string prefix (r not handled)
1576 (?: # unclosed string
1576 (?: # unclosed string
1577 '(?:[^']|(?<!\\)\\')*
1577 '(?:[^']|(?<!\\)\\')*
1578 |
1578 |
1579 "(?:[^"]|(?<!\\)\\")*
1579 "(?:[^"]|(?<!\\)\\")*
1580 )
1580 )
1581 )?
1581 )?
1582 $
1582 $
1583 '''
1583 '''
1584 regexps = self.__dict_key_regexps = {
1584 regexps = self.__dict_key_regexps = {
1585 False: re.compile(dict_key_re_fmt % '''
1585 False: re.compile(dict_key_re_fmt % '''
1586 # identifiers separated by .
1586 # identifiers separated by .
1587 (?!\d)\w+
1587 (?!\d)\w+
1588 (?:\.(?!\d)\w+)*
1588 (?:\.(?!\d)\w+)*
1589 '''),
1589 '''),
1590 True: re.compile(dict_key_re_fmt % '''
1590 True: re.compile(dict_key_re_fmt % '''
1591 .+
1591 .+
1592 ''')
1592 ''')
1593 }
1593 }
1594
1594
1595 match = regexps[self.greedy].search(self.text_until_cursor)
1595 match = regexps[self.greedy].search(self.text_until_cursor)
1596 if match is None:
1596 if match is None:
1597 return []
1597 return []
1598
1598
1599 expr, prefix = match.groups()
1599 expr, prefix = match.groups()
1600 try:
1600 try:
1601 obj = eval(expr, self.namespace)
1601 obj = eval(expr, self.namespace)
1602 except Exception:
1602 except Exception:
1603 try:
1603 try:
1604 obj = eval(expr, self.global_namespace)
1604 obj = eval(expr, self.global_namespace)
1605 except Exception:
1605 except Exception:
1606 return []
1606 return []
1607
1607
1608 keys = get_keys(obj)
1608 keys = get_keys(obj)
1609 if not keys:
1609 if not keys:
1610 return keys
1610 return keys
1611 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1611 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1612 if not matches:
1612 if not matches:
1613 return matches
1613 return matches
1614
1614
1615 # get the cursor position of
1615 # get the cursor position of
1616 # - the text being completed
1616 # - the text being completed
1617 # - the start of the key text
1617 # - the start of the key text
1618 # - the start of the completion
1618 # - the start of the completion
1619 text_start = len(self.text_until_cursor) - len(text)
1619 text_start = len(self.text_until_cursor) - len(text)
1620 if prefix:
1620 if prefix:
1621 key_start = match.start(2)
1621 key_start = match.start(2)
1622 completion_start = key_start + token_offset
1622 completion_start = key_start + token_offset
1623 else:
1623 else:
1624 key_start = completion_start = match.end()
1624 key_start = completion_start = match.end()
1625
1625
1626 # grab the leading prefix, to make sure all completions start with `text`
1626 # grab the leading prefix, to make sure all completions start with `text`
1627 if text_start > key_start:
1627 if text_start > key_start:
1628 leading = ''
1628 leading = ''
1629 else:
1629 else:
1630 leading = text[text_start:completion_start]
1630 leading = text[text_start:completion_start]
1631
1631
1632 # the index of the `[` character
1632 # the index of the `[` character
1633 bracket_idx = match.end(1)
1633 bracket_idx = match.end(1)
1634
1634
1635 # append closing quote and bracket as appropriate
1635 # append closing quote and bracket as appropriate
1636 # this is *not* appropriate if the opening quote or bracket is outside
1636 # this is *not* appropriate if the opening quote or bracket is outside
1637 # the text given to this method
1637 # the text given to this method
1638 suf = ''
1638 suf = ''
1639 continuation = self.line_buffer[len(self.text_until_cursor):]
1639 continuation = self.line_buffer[len(self.text_until_cursor):]
1640 if key_start > text_start and closing_quote:
1640 if key_start > text_start and closing_quote:
1641 # quotes were opened inside text, maybe close them
1641 # quotes were opened inside text, maybe close them
1642 if continuation.startswith(closing_quote):
1642 if continuation.startswith(closing_quote):
1643 continuation = continuation[len(closing_quote):]
1643 continuation = continuation[len(closing_quote):]
1644 else:
1644 else:
1645 suf += closing_quote
1645 suf += closing_quote
1646 if bracket_idx > text_start:
1646 if bracket_idx > text_start:
1647 # brackets were opened inside text, maybe close them
1647 # brackets were opened inside text, maybe close them
1648 if not continuation.startswith(']'):
1648 if not continuation.startswith(']'):
1649 suf += ']'
1649 suf += ']'
1650
1650
1651 return [leading + k + suf for k in matches]
1651 return [leading + k + suf for k in matches]
1652
1652
1653 def unicode_name_matches(self, text):
1653 def unicode_name_matches(self, text):
1654 u"""Match Latex-like syntax for unicode characters base
1654 u"""Match Latex-like syntax for unicode characters base
1655 on the name of the character.
1655 on the name of the character.
1656
1656
1657 This does ``\\GREEK SMALL LETTER ETA`` -> ``η``
1657 This does ``\\GREEK SMALL LETTER ETA`` -> ``η``
1658
1658
1659 Works only on valid python 3 identifier, or on combining characters that
1659 Works only on valid python 3 identifier, or on combining characters that
1660 will combine to form a valid identifier.
1660 will combine to form a valid identifier.
1661
1661
1662 Used on Python 3 only.
1662 Used on Python 3 only.
1663 """
1663 """
1664 slashpos = text.rfind('\\')
1664 slashpos = text.rfind('\\')
1665 if slashpos > -1:
1665 if slashpos > -1:
1666 s = text[slashpos+1:]
1666 s = text[slashpos+1:]
1667 try :
1667 try :
1668 unic = unicodedata.lookup(s)
1668 unic = unicodedata.lookup(s)
1669 # allow combining chars
1669 # allow combining chars
1670 if ('a'+unic).isidentifier():
1670 if ('a'+unic).isidentifier():
1671 return '\\'+s,[unic]
1671 return '\\'+s,[unic]
1672 except KeyError:
1672 except KeyError:
1673 pass
1673 pass
1674 return u'', []
1674 return u'', []
1675
1675
1676
1676
1677 def latex_matches(self, text):
1677 def latex_matches(self, text):
1678 u"""Match Latex syntax for unicode characters.
1678 u"""Match Latex syntax for unicode characters.
1679
1679
1680 This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``α``
1680 This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``α``
1681
1681
1682 Used on Python 3 only.
1682 Used on Python 3 only.
1683 """
1683 """
1684 slashpos = text.rfind('\\')
1684 slashpos = text.rfind('\\')
1685 if slashpos > -1:
1685 if slashpos > -1:
1686 s = text[slashpos:]
1686 s = text[slashpos:]
1687 if s in latex_symbols:
1687 if s in latex_symbols:
1688 # Try to complete a full latex symbol to unicode
1688 # Try to complete a full latex symbol to unicode
1689 # \\alpha -> α
1689 # \\alpha -> α
1690 return s, [latex_symbols[s]]
1690 return s, [latex_symbols[s]]
1691 else:
1691 else:
1692 # If a user has partially typed a latex symbol, give them
1692 # If a user has partially typed a latex symbol, give them
1693 # a full list of options \al -> [\aleph, \alpha]
1693 # a full list of options \al -> [\aleph, \alpha]
1694 matches = [k for k in latex_symbols if k.startswith(s)]
1694 matches = [k for k in latex_symbols if k.startswith(s)]
1695 return s, matches
1695 return s, matches
1696 return u'', []
1696 return u'', []
1697
1697
1698 def dispatch_custom_completer(self, text):
1698 def dispatch_custom_completer(self, text):
1699 if not self.custom_completers:
1699 if not self.custom_completers:
1700 return
1700 return
1701
1701
1702 line = self.line_buffer
1702 line = self.line_buffer
1703 if not line.strip():
1703 if not line.strip():
1704 return None
1704 return None
1705
1705
1706 # Create a little structure to pass all the relevant information about
1706 # Create a little structure to pass all the relevant information about
1707 # the current completion to any custom completer.
1707 # the current completion to any custom completer.
1708 event = SimpleNamespace()
1708 event = SimpleNamespace()
1709 event.line = line
1709 event.line = line
1710 event.symbol = text
1710 event.symbol = text
1711 cmd = line.split(None,1)[0]
1711 cmd = line.split(None,1)[0]
1712 event.command = cmd
1712 event.command = cmd
1713 event.text_until_cursor = self.text_until_cursor
1713 event.text_until_cursor = self.text_until_cursor
1714
1714
1715 # for foo etc, try also to find completer for %foo
1715 # for foo etc, try also to find completer for %foo
1716 if not cmd.startswith(self.magic_escape):
1716 if not cmd.startswith(self.magic_escape):
1717 try_magic = self.custom_completers.s_matches(
1717 try_magic = self.custom_completers.s_matches(
1718 self.magic_escape + cmd)
1718 self.magic_escape + cmd)
1719 else:
1719 else:
1720 try_magic = []
1720 try_magic = []
1721
1721
1722 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1722 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1723 try_magic,
1723 try_magic,
1724 self.custom_completers.flat_matches(self.text_until_cursor)):
1724 self.custom_completers.flat_matches(self.text_until_cursor)):
1725 try:
1725 try:
1726 res = c(event)
1726 res = c(event)
1727 if res:
1727 if res:
1728 # first, try case sensitive match
1728 # first, try case sensitive match
1729 withcase = [r for r in res if r.startswith(text)]
1729 withcase = [r for r in res if r.startswith(text)]
1730 if withcase:
1730 if withcase:
1731 return withcase
1731 return withcase
1732 # if none, then case insensitive ones are ok too
1732 # if none, then case insensitive ones are ok too
1733 text_low = text.lower()
1733 text_low = text.lower()
1734 return [r for r in res if r.lower().startswith(text_low)]
1734 return [r for r in res if r.lower().startswith(text_low)]
1735 except TryNext:
1735 except TryNext:
1736 pass
1736 pass
1737 except KeyboardInterrupt:
1737 except KeyboardInterrupt:
1738 """
1738 """
1739 If custom completer take too long,
1739 If custom completer take too long,
1740 let keyboard interrupt abort and return nothing.
1740 let keyboard interrupt abort and return nothing.
1741 """
1741 """
1742 break
1742 break
1743
1743
1744 return None
1744 return None
1745
1745
1746 def completions(self, text: str, offset: int)->Iterator[Completion]:
1746 def completions(self, text: str, offset: int)->Iterator[Completion]:
1747 """
1747 """
1748 Returns an iterator over the possible completions
1748 Returns an iterator over the possible completions
1749
1749
1750 .. warning:: Unstable
1750 .. warning:: Unstable
1751
1751
1752 This function is unstable, API may change without warning.
1752 This function is unstable, API may change without warning.
1753 It will also raise unless use in proper context manager.
1753 It will also raise unless use in proper context manager.
1754
1754
1755 Parameters
1755 Parameters
1756 ----------
1756 ----------
1757
1757
1758 text:str
1758 text:str
1759 Full text of the current input, multi line string.
1759 Full text of the current input, multi line string.
1760 offset:int
1760 offset:int
1761 Integer representing the position of the cursor in ``text``. Offset
1761 Integer representing the position of the cursor in ``text``. Offset
1762 is 0-based indexed.
1762 is 0-based indexed.
1763
1763
1764 Yields
1764 Yields
1765 ------
1765 ------
1766 :any:`Completion` object
1766 :any:`Completion` object
1767
1767
1768
1768
1769 The cursor on a text can either be seen as being "in between"
1769 The cursor on a text can either be seen as being "in between"
1770 characters or "On" a character depending on the interface visible to
1770 characters or "On" a character depending on the interface visible to
1771 the user. For consistency the cursor being on "in between" characters X
1771 the user. For consistency the cursor being on "in between" characters X
1772 and Y is equivalent to the cursor being "on" character Y, that is to say
1772 and Y is equivalent to the cursor being "on" character Y, that is to say
1773 the character the cursor is on is considered as being after the cursor.
1773 the character the cursor is on is considered as being after the cursor.
1774
1774
1775 Combining characters may span more that one position in the
1775 Combining characters may span more that one position in the
1776 text.
1776 text.
1777
1777
1778
1778
1779 .. note::
1779 .. note::
1780
1780
1781 If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--``
1781 If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--``
1782 fake Completion token to distinguish completion returned by Jedi
1782 fake Completion token to distinguish completion returned by Jedi
1783 and usual IPython completion.
1783 and usual IPython completion.
1784
1784
1785 .. note::
1785 .. note::
1786
1786
1787 Completions are not completely deduplicated yet. If identical
1787 Completions are not completely deduplicated yet. If identical
1788 completions are coming from different sources this function does not
1788 completions are coming from different sources this function does not
1789 ensure that each completion object will only be present once.
1789 ensure that each completion object will only be present once.
1790 """
1790 """
1791 warnings.warn("_complete is a provisional API (as of IPython 6.0). "
1791 warnings.warn("_complete is a provisional API (as of IPython 6.0). "
1792 "It may change without warnings. "
1792 "It may change without warnings. "
1793 "Use in corresponding context manager.",
1793 "Use in corresponding context manager.",
1794 category=ProvisionalCompleterWarning, stacklevel=2)
1794 category=ProvisionalCompleterWarning, stacklevel=2)
1795
1795
1796 seen = set()
1796 seen = set()
1797 for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
1797 for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
1798 if c and (c in seen):
1798 if c and (c in seen):
1799 continue
1799 continue
1800 yield c
1800 yield c
1801 seen.add(c)
1801 seen.add(c)
1802
1802
1803 def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]:
1803 def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]:
1804 """
1804 """
1805 Core completion module.Same signature as :any:`completions`, with the
1805 Core completion module.Same signature as :any:`completions`, with the
1806 extra `timeout` parameter (in seconds).
1806 extra `timeout` parameter (in seconds).
1807
1807
1808
1808
1809 Computing jedi's completion ``.type`` can be quite expensive (it is a
1809 Computing jedi's completion ``.type`` can be quite expensive (it is a
1810 lazy property) and can require some warm-up, more warm up than just
1810 lazy property) and can require some warm-up, more warm up than just
1811 computing the ``name`` of a completion. The warm-up can be :
1811 computing the ``name`` of a completion. The warm-up can be :
1812
1812
1813 - Long warm-up the first time a module is encountered after
1813 - Long warm-up the first time a module is encountered after
1814 install/update: actually build parse/inference tree.
1814 install/update: actually build parse/inference tree.
1815
1815
1816 - first time the module is encountered in a session: load tree from
1816 - first time the module is encountered in a session: load tree from
1817 disk.
1817 disk.
1818
1818
1819 We don't want to block completions for tens of seconds so we give the
1819 We don't want to block completions for tens of seconds so we give the
1820 completer a "budget" of ``_timeout`` seconds per invocation to compute
1820 completer a "budget" of ``_timeout`` seconds per invocation to compute
1821 completions types, the completions that have not yet been computed will
1821 completions types, the completions that have not yet been computed will
1822 be marked as "unknown" an will have a chance to be computed next round
1822 be marked as "unknown" an will have a chance to be computed next round
1823 are things get cached.
1823 are things get cached.
1824
1824
1825 Keep in mind that Jedi is not the only thing treating the completion so
1825 Keep in mind that Jedi is not the only thing treating the completion so
1826 keep the timeout short-ish as if we take more than 0.3 second we still
1826 keep the timeout short-ish as if we take more than 0.3 second we still
1827 have lots of processing to do.
1827 have lots of processing to do.
1828
1828
1829 """
1829 """
1830 deadline = time.monotonic() + _timeout
1830 deadline = time.monotonic() + _timeout
1831
1831
1832
1832
1833 before = full_text[:offset]
1833 before = full_text[:offset]
1834 cursor_line, cursor_column = position_to_cursor(full_text, offset)
1834 cursor_line, cursor_column = position_to_cursor(full_text, offset)
1835
1835
1836 matched_text, matches, matches_origin, jedi_matches = self._complete(
1836 matched_text, matches, matches_origin, jedi_matches = self._complete(
1837 full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
1837 full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
1838
1838
1839 iter_jm = iter(jedi_matches)
1839 iter_jm = iter(jedi_matches)
1840 if _timeout:
1840 if _timeout:
1841 for jm in iter_jm:
1841 for jm in iter_jm:
1842 try:
1842 try:
1843 type_ = jm.type
1843 type_ = jm.type
1844 except Exception:
1844 except Exception:
1845 if self.debug:
1845 if self.debug:
1846 print("Error in Jedi getting type of ", jm)
1846 print("Error in Jedi getting type of ", jm)
1847 type_ = None
1847 type_ = None
1848 delta = len(jm.name_with_symbols) - len(jm.complete)
1848 delta = len(jm.name_with_symbols) - len(jm.complete)
1849 if type_ == 'function':
1849 if type_ == 'function':
1850 signature = _make_signature(jm)
1850 signature = _make_signature(jm)
1851 else:
1851 else:
1852 signature = ''
1852 signature = ''
1853 yield Completion(start=offset - delta,
1853 yield Completion(start=offset - delta,
1854 end=offset,
1854 end=offset,
1855 text=jm.name_with_symbols,
1855 text=jm.name_with_symbols,
1856 type=type_,
1856 type=type_,
1857 signature=signature,
1857 signature=signature,
1858 _origin='jedi')
1858 _origin='jedi')
1859
1859
1860 if time.monotonic() > deadline:
1860 if time.monotonic() > deadline:
1861 break
1861 break
1862
1862
1863 for jm in iter_jm:
1863 for jm in iter_jm:
1864 delta = len(jm.name_with_symbols) - len(jm.complete)
1864 delta = len(jm.name_with_symbols) - len(jm.complete)
1865 yield Completion(start=offset - delta,
1865 yield Completion(start=offset - delta,
1866 end=offset,
1866 end=offset,
1867 text=jm.name_with_symbols,
1867 text=jm.name_with_symbols,
1868 type='<unknown>', # don't compute type for speed
1868 type='<unknown>', # don't compute type for speed
1869 _origin='jedi',
1869 _origin='jedi',
1870 signature='')
1870 signature='')
1871
1871
1872
1872
1873 start_offset = before.rfind(matched_text)
1873 start_offset = before.rfind(matched_text)
1874
1874
1875 # TODO:
1875 # TODO:
1876 # Supress this, right now just for debug.
1876 # Suppress this, right now just for debug.
1877 if jedi_matches and matches and self.debug:
1877 if jedi_matches and matches and self.debug:
1878 yield Completion(start=start_offset, end=offset, text='--jedi/ipython--',
1878 yield Completion(start=start_offset, end=offset, text='--jedi/ipython--',
1879 _origin='debug', type='none', signature='')
1879 _origin='debug', type='none', signature='')
1880
1880
1881 # I'm unsure if this is always true, so let's assert and see if it
1881 # I'm unsure if this is always true, so let's assert and see if it
1882 # crash
1882 # crash
1883 assert before.endswith(matched_text)
1883 assert before.endswith(matched_text)
1884 for m, t in zip(matches, matches_origin):
1884 for m, t in zip(matches, matches_origin):
1885 yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>')
1885 yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>')
1886
1886
1887
1887
1888 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1888 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1889 """Find completions for the given text and line context.
1889 """Find completions for the given text and line context.
1890
1890
1891 Note that both the text and the line_buffer are optional, but at least
1891 Note that both the text and the line_buffer are optional, but at least
1892 one of them must be given.
1892 one of them must be given.
1893
1893
1894 Parameters
1894 Parameters
1895 ----------
1895 ----------
1896 text : string, optional
1896 text : string, optional
1897 Text to perform the completion on. If not given, the line buffer
1897 Text to perform the completion on. If not given, the line buffer
1898 is split using the instance's CompletionSplitter object.
1898 is split using the instance's CompletionSplitter object.
1899
1899
1900 line_buffer : string, optional
1900 line_buffer : string, optional
1901 If not given, the completer attempts to obtain the current line
1901 If not given, the completer attempts to obtain the current line
1902 buffer via readline. This keyword allows clients which are
1902 buffer via readline. This keyword allows clients which are
1903 requesting for text completions in non-readline contexts to inform
1903 requesting for text completions in non-readline contexts to inform
1904 the completer of the entire text.
1904 the completer of the entire text.
1905
1905
1906 cursor_pos : int, optional
1906 cursor_pos : int, optional
1907 Index of the cursor in the full line buffer. Should be provided by
1907 Index of the cursor in the full line buffer. Should be provided by
1908 remote frontends where kernel has no access to frontend state.
1908 remote frontends where kernel has no access to frontend state.
1909
1909
1910 Returns
1910 Returns
1911 -------
1911 -------
1912 text : str
1912 text : str
1913 Text that was actually used in the completion.
1913 Text that was actually used in the completion.
1914
1914
1915 matches : list
1915 matches : list
1916 A list of completion matches.
1916 A list of completion matches.
1917
1917
1918
1918
1919 .. note::
1919 .. note::
1920
1920
1921 This API is likely to be deprecated and replaced by
1921 This API is likely to be deprecated and replaced by
1922 :any:`IPCompleter.completions` in the future.
1922 :any:`IPCompleter.completions` in the future.
1923
1923
1924
1924
1925 """
1925 """
1926 warnings.warn('`Completer.complete` is pending deprecation since '
1926 warnings.warn('`Completer.complete` is pending deprecation since '
1927 'IPython 6.0 and will be replaced by `Completer.completions`.',
1927 'IPython 6.0 and will be replaced by `Completer.completions`.',
1928 PendingDeprecationWarning)
1928 PendingDeprecationWarning)
1929 # potential todo, FOLD the 3rd throw away argument of _complete
1929 # potential todo, FOLD the 3rd throw away argument of _complete
1930 # into the first 2 one.
1930 # into the first 2 one.
1931 return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2]
1931 return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2]
1932
1932
1933 def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
1933 def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
1934 full_text=None, return_jedi_results=True) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]:
1934 full_text=None, return_jedi_results=True) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]:
1935 """
1935 """
1936
1936
1937 Like complete but can also returns raw jedi completions as well as the
1937 Like complete but can also returns raw jedi completions as well as the
1938 origin of the completion text. This could (and should) be made much
1938 origin of the completion text. This could (and should) be made much
1939 cleaner but that will be simpler once we drop the old (and stateful)
1939 cleaner but that will be simpler once we drop the old (and stateful)
1940 :any:`complete` API.
1940 :any:`complete` API.
1941
1941
1942
1942
1943 With current provisional API, cursor_pos act both (depending on the
1943 With current provisional API, cursor_pos act both (depending on the
1944 caller) as the offset in the ``text`` or ``line_buffer``, or as the
1944 caller) as the offset in the ``text`` or ``line_buffer``, or as the
1945 ``column`` when passing multiline strings this could/should be renamed
1945 ``column`` when passing multiline strings this could/should be renamed
1946 but would add extra noise.
1946 but would add extra noise.
1947 """
1947 """
1948
1948
1949 # if the cursor position isn't given, the only sane assumption we can
1949 # if the cursor position isn't given, the only sane assumption we can
1950 # make is that it's at the end of the line (the common case)
1950 # make is that it's at the end of the line (the common case)
1951 if cursor_pos is None:
1951 if cursor_pos is None:
1952 cursor_pos = len(line_buffer) if text is None else len(text)
1952 cursor_pos = len(line_buffer) if text is None else len(text)
1953
1953
1954 if self.use_main_ns:
1954 if self.use_main_ns:
1955 self.namespace = __main__.__dict__
1955 self.namespace = __main__.__dict__
1956
1956
1957 # if text is either None or an empty string, rely on the line buffer
1957 # if text is either None or an empty string, rely on the line buffer
1958 if (not line_buffer) and full_text:
1958 if (not line_buffer) and full_text:
1959 line_buffer = full_text.split('\n')[cursor_line]
1959 line_buffer = full_text.split('\n')[cursor_line]
1960 if not text:
1960 if not text:
1961 text = self.splitter.split_line(line_buffer, cursor_pos)
1961 text = self.splitter.split_line(line_buffer, cursor_pos)
1962
1962
1963 if self.backslash_combining_completions:
1963 if self.backslash_combining_completions:
1964 # allow deactivation of these on windows.
1964 # allow deactivation of these on windows.
1965 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1965 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1966 latex_text, latex_matches = self.latex_matches(base_text)
1966 latex_text, latex_matches = self.latex_matches(base_text)
1967 if latex_matches:
1967 if latex_matches:
1968 return latex_text, latex_matches, ['latex_matches']*len(latex_matches), ()
1968 return latex_text, latex_matches, ['latex_matches']*len(latex_matches), ()
1969 name_text = ''
1969 name_text = ''
1970 name_matches = []
1970 name_matches = []
1971 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1971 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1972 name_text, name_matches = meth(base_text)
1972 name_text, name_matches = meth(base_text)
1973 if name_text:
1973 if name_text:
1974 return name_text, name_matches[:MATCHES_LIMIT], \
1974 return name_text, name_matches[:MATCHES_LIMIT], \
1975 [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
1975 [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
1976
1976
1977
1977
1978 # If no line buffer is given, assume the input text is all there was
1978 # If no line buffer is given, assume the input text is all there was
1979 if line_buffer is None:
1979 if line_buffer is None:
1980 line_buffer = text
1980 line_buffer = text
1981
1981
1982 self.line_buffer = line_buffer
1982 self.line_buffer = line_buffer
1983 self.text_until_cursor = self.line_buffer[:cursor_pos]
1983 self.text_until_cursor = self.line_buffer[:cursor_pos]
1984
1984
1985 # Do magic arg matches
1985 # Do magic arg matches
1986 for matcher in self.magic_arg_matchers:
1986 for matcher in self.magic_arg_matchers:
1987 matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
1987 matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
1988 if matches:
1988 if matches:
1989 origins = [matcher.__qualname__] * len(matches)
1989 origins = [matcher.__qualname__] * len(matches)
1990 return text, matches, origins, ()
1990 return text, matches, origins, ()
1991
1991
1992 # Start with a clean slate of completions
1992 # Start with a clean slate of completions
1993 matches = []
1993 matches = []
1994 custom_res = self.dispatch_custom_completer(text)
1994 custom_res = self.dispatch_custom_completer(text)
1995 # FIXME: we should extend our api to return a dict with completions for
1995 # FIXME: we should extend our api to return a dict with completions for
1996 # different types of objects. The rlcomplete() method could then
1996 # different types of objects. The rlcomplete() method could then
1997 # simply collapse the dict into a list for readline, but we'd have
1997 # simply collapse the dict into a list for readline, but we'd have
1998 # richer completion semantics in other evironments.
1998 # richer completion semantics in other evironments.
1999 completions = ()
1999 completions = ()
2000 if self.use_jedi and return_jedi_results:
2000 if self.use_jedi and return_jedi_results:
2001 if not full_text:
2001 if not full_text:
2002 full_text = line_buffer
2002 full_text = line_buffer
2003 completions = self._jedi_matches(
2003 completions = self._jedi_matches(
2004 cursor_pos, cursor_line, full_text)
2004 cursor_pos, cursor_line, full_text)
2005 if custom_res is not None:
2005 if custom_res is not None:
2006 # did custom completers produce something?
2006 # did custom completers produce something?
2007 matches = [(m, 'custom') for m in custom_res]
2007 matches = [(m, 'custom') for m in custom_res]
2008 else:
2008 else:
2009 # Extend the list of completions with the results of each
2009 # Extend the list of completions with the results of each
2010 # matcher, so we return results to the user from all
2010 # matcher, so we return results to the user from all
2011 # namespaces.
2011 # namespaces.
2012 if self.merge_completions:
2012 if self.merge_completions:
2013 matches = []
2013 matches = []
2014 for matcher in self.matchers:
2014 for matcher in self.matchers:
2015 try:
2015 try:
2016 matches.extend([(m, matcher.__qualname__)
2016 matches.extend([(m, matcher.__qualname__)
2017 for m in matcher(text)])
2017 for m in matcher(text)])
2018 except:
2018 except:
2019 # Show the ugly traceback if the matcher causes an
2019 # Show the ugly traceback if the matcher causes an
2020 # exception, but do NOT crash the kernel!
2020 # exception, but do NOT crash the kernel!
2021 sys.excepthook(*sys.exc_info())
2021 sys.excepthook(*sys.exc_info())
2022 else:
2022 else:
2023 for matcher in self.matchers:
2023 for matcher in self.matchers:
2024 matches = [(m, matcher.__qualname__)
2024 matches = [(m, matcher.__qualname__)
2025 for m in matcher(text)]
2025 for m in matcher(text)]
2026 if matches:
2026 if matches:
2027 break
2027 break
2028 seen = set()
2028 seen = set()
2029 filtered_matches = set()
2029 filtered_matches = set()
2030 for m in matches:
2030 for m in matches:
2031 t, c = m
2031 t, c = m
2032 if t not in seen:
2032 if t not in seen:
2033 filtered_matches.add(m)
2033 filtered_matches.add(m)
2034 seen.add(t)
2034 seen.add(t)
2035
2035
2036 _filtered_matches = sorted(
2036 _filtered_matches = sorted(
2037 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2037 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2038 [:MATCHES_LIMIT]
2038 [:MATCHES_LIMIT]
2039
2039
2040 _matches = [m[0] for m in _filtered_matches]
2040 _matches = [m[0] for m in _filtered_matches]
2041 origins = [m[1] for m in _filtered_matches]
2041 origins = [m[1] for m in _filtered_matches]
2042
2042
2043 self.matches = _matches
2043 self.matches = _matches
2044
2044
2045 return text, _matches, origins, completions
2045 return text, _matches, origins, completions
@@ -1,229 +1,229 b''
1 """Hooks for IPython.
1 """Hooks for IPython.
2
2
3 In Python, it is possible to overwrite any method of any object if you really
3 In Python, it is possible to overwrite any method of any object if you really
4 want to. But IPython exposes a few 'hooks', methods which are *designed* to
4 want to. But IPython exposes a few 'hooks', methods which are *designed* to
5 be overwritten by users for customization purposes. This module defines the
5 be overwritten by users for customization purposes. This module defines the
6 default versions of all such hooks, which get used by IPython if not
6 default versions of all such hooks, which get used by IPython if not
7 overridden by the user.
7 overridden by the user.
8
8
9 Hooks are simple functions, but they should be declared with ``self`` as their
9 Hooks are simple functions, but they should be declared with ``self`` as their
10 first argument, because when activated they are registered into IPython as
10 first argument, because when activated they are registered into IPython as
11 instance methods. The self argument will be the IPython running instance
11 instance methods. The self argument will be the IPython running instance
12 itself, so hooks have full access to the entire IPython object.
12 itself, so hooks have full access to the entire IPython object.
13
13
14 If you wish to define a new hook and activate it, you can make an :doc:`extension
14 If you wish to define a new hook and activate it, you can make an :doc:`extension
15 </config/extensions/index>` or a :ref:`startup script <startup_files>`. For
15 </config/extensions/index>` or a :ref:`startup script <startup_files>`. For
16 example, you could use a startup file like this::
16 example, you could use a startup file like this::
17
17
18 import os
18 import os
19
19
20 def calljed(self,filename, linenum):
20 def calljed(self,filename, linenum):
21 "My editor hook calls the jed editor directly."
21 "My editor hook calls the jed editor directly."
22 print "Calling my own editor, jed ..."
22 print "Calling my own editor, jed ..."
23 if os.system('jed +%d %s' % (linenum,filename)) != 0:
23 if os.system('jed +%d %s' % (linenum,filename)) != 0:
24 raise TryNext()
24 raise TryNext()
25
25
26 def load_ipython_extension(ip):
26 def load_ipython_extension(ip):
27 ip.set_hook('editor', calljed)
27 ip.set_hook('editor', calljed)
28
28
29 """
29 """
30
30
31 #*****************************************************************************
31 #*****************************************************************************
32 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
32 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
33 #
33 #
34 # Distributed under the terms of the BSD License. The full license is in
34 # Distributed under the terms of the BSD License. The full license is in
35 # the file COPYING, distributed as part of this software.
35 # the file COPYING, distributed as part of this software.
36 #*****************************************************************************
36 #*****************************************************************************
37
37
38 import os
38 import os
39 import subprocess
39 import subprocess
40 import warnings
40 import warnings
41 import sys
41 import sys
42
42
43 from IPython.core.error import TryNext
43 from IPython.core.error import TryNext
44
44
45 # List here all the default hooks. For now it's just the editor functions
45 # List here all the default hooks. For now it's just the editor functions
46 # but over time we'll move here all the public API for user-accessible things.
46 # but over time we'll move here all the public API for user-accessible things.
47
47
48 __all__ = ['editor', 'synchronize_with_editor',
48 __all__ = ['editor', 'synchronize_with_editor',
49 'shutdown_hook', 'late_startup_hook',
49 'shutdown_hook', 'late_startup_hook',
50 'show_in_pager','pre_prompt_hook',
50 'show_in_pager','pre_prompt_hook',
51 'pre_run_code_hook', 'clipboard_get']
51 'pre_run_code_hook', 'clipboard_get']
52
52
53 deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event",
53 deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event",
54 'late_startup_hook': "a callback for the 'shell_initialized' event",
54 'late_startup_hook': "a callback for the 'shell_initialized' event",
55 'shutdown_hook': "the atexit module",
55 'shutdown_hook': "the atexit module",
56 }
56 }
57
57
58 def editor(self, filename, linenum=None, wait=True):
58 def editor(self, filename, linenum=None, wait=True):
59 """Open the default editor at the given filename and linenumber.
59 """Open the default editor at the given filename and linenumber.
60
60
61 This is IPython's default editor hook, you can use it as an example to
61 This is IPython's default editor hook, you can use it as an example to
62 write your own modified one. To set your own editor function as the
62 write your own modified one. To set your own editor function as the
63 new editor hook, call ip.set_hook('editor',yourfunc)."""
63 new editor hook, call ip.set_hook('editor',yourfunc)."""
64
64
65 # IPython configures a default editor at startup by reading $EDITOR from
65 # IPython configures a default editor at startup by reading $EDITOR from
66 # the environment, and falling back on vi (unix) or notepad (win32).
66 # the environment, and falling back on vi (unix) or notepad (win32).
67 editor = self.editor
67 editor = self.editor
68
68
69 # marker for at which line to open the file (for existing objects)
69 # marker for at which line to open the file (for existing objects)
70 if linenum is None or editor=='notepad':
70 if linenum is None or editor=='notepad':
71 linemark = ''
71 linemark = ''
72 else:
72 else:
73 linemark = '+%d' % int(linenum)
73 linemark = '+%d' % int(linenum)
74
74
75 # Enclose in quotes if necessary and legal
75 # Enclose in quotes if necessary and legal
76 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
76 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
77 editor = '"%s"' % editor
77 editor = '"%s"' % editor
78
78
79 # Call the actual editor
79 # Call the actual editor
80 proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename),
80 proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename),
81 shell=True)
81 shell=True)
82 if wait and proc.wait() != 0:
82 if wait and proc.wait() != 0:
83 raise TryNext()
83 raise TryNext()
84
84
85 import tempfile
85 import tempfile
86 from IPython.utils.decorators import undoc
86 from IPython.utils.decorators import undoc
87
87
88 @undoc
88 @undoc
89 def fix_error_editor(self,filename,linenum,column,msg):
89 def fix_error_editor(self,filename,linenum,column,msg):
90 """DEPRECATED
90 """DEPRECATED
91
91
92 Open the editor at the given filename, linenumber, column and
92 Open the editor at the given filename, linenumber, column and
93 show an error message. This is used for correcting syntax errors.
93 show an error message. This is used for correcting syntax errors.
94 The current implementation only has special support for the VIM editor,
94 The current implementation only has special support for the VIM editor,
95 and falls back on the 'editor' hook if VIM is not used.
95 and falls back on the 'editor' hook if VIM is not used.
96
96
97 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
97 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
98 """
98 """
99
99
100 warnings.warn("""
100 warnings.warn("""
101 `fix_error_editor` is deprecated as of IPython 6.0 and will be removed
101 `fix_error_editor` is deprecated as of IPython 6.0 and will be removed
102 in future versions. It appears to be used only for automatically fixing syntax
102 in future versions. It appears to be used only for automatically fixing syntax
103 error that has been broken for a few years and has thus been removed. If you
103 error that has been broken for a few years and has thus been removed. If you
104 happend to use this function and still need it please make your voice heard on
104 happened to use this function and still need it please make your voice heard on
105 the mailing list ipython-dev@python.org , or on the GitHub Issue tracker:
105 the mailing list ipython-dev@python.org , or on the GitHub Issue tracker:
106 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
106 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
107
107
108 def vim_quickfix_file():
108 def vim_quickfix_file():
109 t = tempfile.NamedTemporaryFile()
109 t = tempfile.NamedTemporaryFile()
110 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
110 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
111 t.flush()
111 t.flush()
112 return t
112 return t
113 if os.path.basename(self.editor) != 'vim':
113 if os.path.basename(self.editor) != 'vim':
114 self.hooks.editor(filename,linenum)
114 self.hooks.editor(filename,linenum)
115 return
115 return
116 t = vim_quickfix_file()
116 t = vim_quickfix_file()
117 try:
117 try:
118 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
118 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
119 raise TryNext()
119 raise TryNext()
120 finally:
120 finally:
121 t.close()
121 t.close()
122
122
123
123
124 def synchronize_with_editor(self, filename, linenum, column):
124 def synchronize_with_editor(self, filename, linenum, column):
125 pass
125 pass
126
126
127
127
128 class CommandChainDispatcher:
128 class CommandChainDispatcher:
129 """ Dispatch calls to a chain of commands until some func can handle it
129 """ Dispatch calls to a chain of commands until some func can handle it
130
130
131 Usage: instantiate, execute "add" to add commands (with optional
131 Usage: instantiate, execute "add" to add commands (with optional
132 priority), execute normally via f() calling mechanism.
132 priority), execute normally via f() calling mechanism.
133
133
134 """
134 """
135 def __init__(self,commands=None):
135 def __init__(self,commands=None):
136 if commands is None:
136 if commands is None:
137 self.chain = []
137 self.chain = []
138 else:
138 else:
139 self.chain = commands
139 self.chain = commands
140
140
141
141
142 def __call__(self,*args, **kw):
142 def __call__(self,*args, **kw):
143 """ Command chain is called just like normal func.
143 """ Command chain is called just like normal func.
144
144
145 This will call all funcs in chain with the same args as were given to
145 This will call all funcs in chain with the same args as were given to
146 this function, and return the result of first func that didn't raise
146 this function, and return the result of first func that didn't raise
147 TryNext"""
147 TryNext"""
148 last_exc = TryNext()
148 last_exc = TryNext()
149 for prio,cmd in self.chain:
149 for prio,cmd in self.chain:
150 #print "prio",prio,"cmd",cmd #dbg
150 #print "prio",prio,"cmd",cmd #dbg
151 try:
151 try:
152 return cmd(*args, **kw)
152 return cmd(*args, **kw)
153 except TryNext as exc:
153 except TryNext as exc:
154 last_exc = exc
154 last_exc = exc
155 # if no function will accept it, raise TryNext up to the caller
155 # if no function will accept it, raise TryNext up to the caller
156 raise last_exc
156 raise last_exc
157
157
158 def __str__(self):
158 def __str__(self):
159 return str(self.chain)
159 return str(self.chain)
160
160
161 def add(self, func, priority=0):
161 def add(self, func, priority=0):
162 """ Add a func to the cmd chain with given priority """
162 """ Add a func to the cmd chain with given priority """
163 self.chain.append((priority, func))
163 self.chain.append((priority, func))
164 self.chain.sort(key=lambda x: x[0])
164 self.chain.sort(key=lambda x: x[0])
165
165
166 def __iter__(self):
166 def __iter__(self):
167 """ Return all objects in chain.
167 """ Return all objects in chain.
168
168
169 Handy if the objects are not callable.
169 Handy if the objects are not callable.
170 """
170 """
171 return iter(self.chain)
171 return iter(self.chain)
172
172
173
173
174 def shutdown_hook(self):
174 def shutdown_hook(self):
175 """ default shutdown hook
175 """ default shutdown hook
176
176
177 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
177 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
178 """
178 """
179
179
180 #print "default shutdown hook ok" # dbg
180 #print "default shutdown hook ok" # dbg
181 return
181 return
182
182
183
183
184 def late_startup_hook(self):
184 def late_startup_hook(self):
185 """ Executed after ipython has been constructed and configured
185 """ Executed after ipython has been constructed and configured
186
186
187 """
187 """
188 #print "default startup hook ok" # dbg
188 #print "default startup hook ok" # dbg
189
189
190
190
191 def show_in_pager(self, data, start, screen_lines):
191 def show_in_pager(self, data, start, screen_lines):
192 """ Run a string through pager """
192 """ Run a string through pager """
193 # raising TryNext here will use the default paging functionality
193 # raising TryNext here will use the default paging functionality
194 raise TryNext
194 raise TryNext
195
195
196
196
197 def pre_prompt_hook(self):
197 def pre_prompt_hook(self):
198 """ Run before displaying the next prompt
198 """ Run before displaying the next prompt
199
199
200 Use this e.g. to display output from asynchronous operations (in order
200 Use this e.g. to display output from asynchronous operations (in order
201 to not mess up text entry)
201 to not mess up text entry)
202 """
202 """
203
203
204 return None
204 return None
205
205
206
206
207 def pre_run_code_hook(self):
207 def pre_run_code_hook(self):
208 """ Executed before running the (prefiltered) code in IPython """
208 """ Executed before running the (prefiltered) code in IPython """
209 return None
209 return None
210
210
211
211
212 def clipboard_get(self):
212 def clipboard_get(self):
213 """ Get text from the clipboard.
213 """ Get text from the clipboard.
214 """
214 """
215 from IPython.lib.clipboard import (
215 from IPython.lib.clipboard import (
216 osx_clipboard_get, tkinter_clipboard_get,
216 osx_clipboard_get, tkinter_clipboard_get,
217 win32_clipboard_get
217 win32_clipboard_get
218 )
218 )
219 if sys.platform == 'win32':
219 if sys.platform == 'win32':
220 chain = [win32_clipboard_get, tkinter_clipboard_get]
220 chain = [win32_clipboard_get, tkinter_clipboard_get]
221 elif sys.platform == 'darwin':
221 elif sys.platform == 'darwin':
222 chain = [osx_clipboard_get, tkinter_clipboard_get]
222 chain = [osx_clipboard_get, tkinter_clipboard_get]
223 else:
223 else:
224 chain = [tkinter_clipboard_get]
224 chain = [tkinter_clipboard_get]
225 dispatcher = CommandChainDispatcher()
225 dispatcher = CommandChainDispatcher()
226 for func in chain:
226 for func in chain:
227 dispatcher.add(func)
227 dispatcher.add(func)
228 text = dispatcher()
228 text = dispatcher()
229 return text
229 return text
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,614 +1,614 b''
1 """Implementation of basic magic functions."""
1 """Implementation of basic magic functions."""
2
2
3
3
4 import argparse
4 import argparse
5 import textwrap
5 import textwrap
6 import io
6 import io
7 import sys
7 import sys
8 from pprint import pformat
8 from pprint import pformat
9
9
10 from IPython.core import magic_arguments, page
10 from IPython.core import magic_arguments, page
11 from IPython.core.error import UsageError
11 from IPython.core.error import UsageError
12 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
12 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
13 from IPython.utils.text import format_screen, dedent, indent
13 from IPython.utils.text import format_screen, dedent, indent
14 from IPython.testing.skipdoctest import skip_doctest
14 from IPython.testing.skipdoctest import skip_doctest
15 from IPython.utils.ipstruct import Struct
15 from IPython.utils.ipstruct import Struct
16 from warnings import warn
16 from warnings import warn
17 from logging import error
17 from logging import error
18
18
19
19
20 class MagicsDisplay(object):
20 class MagicsDisplay(object):
21 def __init__(self, magics_manager, ignore=None):
21 def __init__(self, magics_manager, ignore=None):
22 self.ignore = ignore if ignore else []
22 self.ignore = ignore if ignore else []
23 self.magics_manager = magics_manager
23 self.magics_manager = magics_manager
24
24
25 def _lsmagic(self):
25 def _lsmagic(self):
26 """The main implementation of the %lsmagic"""
26 """The main implementation of the %lsmagic"""
27 mesc = magic_escapes['line']
27 mesc = magic_escapes['line']
28 cesc = magic_escapes['cell']
28 cesc = magic_escapes['cell']
29 mman = self.magics_manager
29 mman = self.magics_manager
30 magics = mman.lsmagic()
30 magics = mman.lsmagic()
31 out = ['Available line magics:',
31 out = ['Available line magics:',
32 mesc + (' '+mesc).join(sorted([m for m,v in magics['line'].items() if (v not in self.ignore)])),
32 mesc + (' '+mesc).join(sorted([m for m,v in magics['line'].items() if (v not in self.ignore)])),
33 '',
33 '',
34 'Available cell magics:',
34 'Available cell magics:',
35 cesc + (' '+cesc).join(sorted([m for m,v in magics['cell'].items() if (v not in self.ignore)])),
35 cesc + (' '+cesc).join(sorted([m for m,v in magics['cell'].items() if (v not in self.ignore)])),
36 '',
36 '',
37 mman.auto_status()]
37 mman.auto_status()]
38 return '\n'.join(out)
38 return '\n'.join(out)
39
39
40 def _repr_pretty_(self, p, cycle):
40 def _repr_pretty_(self, p, cycle):
41 p.text(self._lsmagic())
41 p.text(self._lsmagic())
42
42
43 def __str__(self):
43 def __str__(self):
44 return self._lsmagic()
44 return self._lsmagic()
45
45
46 def _jsonable(self):
46 def _jsonable(self):
47 """turn magics dict into jsonable dict of the same structure
47 """turn magics dict into jsonable dict of the same structure
48
48
49 replaces object instances with their class names as strings
49 replaces object instances with their class names as strings
50 """
50 """
51 magic_dict = {}
51 magic_dict = {}
52 mman = self.magics_manager
52 mman = self.magics_manager
53 magics = mman.lsmagic()
53 magics = mman.lsmagic()
54 for key, subdict in magics.items():
54 for key, subdict in magics.items():
55 d = {}
55 d = {}
56 magic_dict[key] = d
56 magic_dict[key] = d
57 for name, obj in subdict.items():
57 for name, obj in subdict.items():
58 try:
58 try:
59 classname = obj.__self__.__class__.__name__
59 classname = obj.__self__.__class__.__name__
60 except AttributeError:
60 except AttributeError:
61 classname = 'Other'
61 classname = 'Other'
62
62
63 d[name] = classname
63 d[name] = classname
64 return magic_dict
64 return magic_dict
65
65
66 def _repr_json_(self):
66 def _repr_json_(self):
67 return self._jsonable()
67 return self._jsonable()
68
68
69
69
70 @magics_class
70 @magics_class
71 class BasicMagics(Magics):
71 class BasicMagics(Magics):
72 """Magics that provide central IPython functionality.
72 """Magics that provide central IPython functionality.
73
73
74 These are various magics that don't fit into specific categories but that
74 These are various magics that don't fit into specific categories but that
75 are all part of the base 'IPython experience'."""
75 are all part of the base 'IPython experience'."""
76
76
77 @magic_arguments.magic_arguments()
77 @magic_arguments.magic_arguments()
78 @magic_arguments.argument(
78 @magic_arguments.argument(
79 '-l', '--line', action='store_true',
79 '-l', '--line', action='store_true',
80 help="""Create a line magic alias."""
80 help="""Create a line magic alias."""
81 )
81 )
82 @magic_arguments.argument(
82 @magic_arguments.argument(
83 '-c', '--cell', action='store_true',
83 '-c', '--cell', action='store_true',
84 help="""Create a cell magic alias."""
84 help="""Create a cell magic alias."""
85 )
85 )
86 @magic_arguments.argument(
86 @magic_arguments.argument(
87 'name',
87 'name',
88 help="""Name of the magic to be created."""
88 help="""Name of the magic to be created."""
89 )
89 )
90 @magic_arguments.argument(
90 @magic_arguments.argument(
91 'target',
91 'target',
92 help="""Name of the existing line or cell magic."""
92 help="""Name of the existing line or cell magic."""
93 )
93 )
94 @magic_arguments.argument(
94 @magic_arguments.argument(
95 '-p', '--params', default=None,
95 '-p', '--params', default=None,
96 help="""Parameters passed to the magic function."""
96 help="""Parameters passed to the magic function."""
97 )
97 )
98 @line_magic
98 @line_magic
99 def alias_magic(self, line=''):
99 def alias_magic(self, line=''):
100 """Create an alias for an existing line or cell magic.
100 """Create an alias for an existing line or cell magic.
101
101
102 Examples
102 Examples
103 --------
103 --------
104 ::
104 ::
105
105
106 In [1]: %alias_magic t timeit
106 In [1]: %alias_magic t timeit
107 Created `%t` as an alias for `%timeit`.
107 Created `%t` as an alias for `%timeit`.
108 Created `%%t` as an alias for `%%timeit`.
108 Created `%%t` as an alias for `%%timeit`.
109
109
110 In [2]: %t -n1 pass
110 In [2]: %t -n1 pass
111 1 loops, best of 3: 954 ns per loop
111 1 loops, best of 3: 954 ns per loop
112
112
113 In [3]: %%t -n1
113 In [3]: %%t -n1
114 ...: pass
114 ...: pass
115 ...:
115 ...:
116 1 loops, best of 3: 954 ns per loop
116 1 loops, best of 3: 954 ns per loop
117
117
118 In [4]: %alias_magic --cell whereami pwd
118 In [4]: %alias_magic --cell whereami pwd
119 UsageError: Cell magic function `%%pwd` not found.
119 UsageError: Cell magic function `%%pwd` not found.
120 In [5]: %alias_magic --line whereami pwd
120 In [5]: %alias_magic --line whereami pwd
121 Created `%whereami` as an alias for `%pwd`.
121 Created `%whereami` as an alias for `%pwd`.
122
122
123 In [6]: %whereami
123 In [6]: %whereami
124 Out[6]: u'/home/testuser'
124 Out[6]: u'/home/testuser'
125
125
126 In [7]: %alias_magic h history -p "-l 30" --line
126 In [7]: %alias_magic h history -p "-l 30" --line
127 Created `%h` as an alias for `%history -l 30`.
127 Created `%h` as an alias for `%history -l 30`.
128 """
128 """
129
129
130 args = magic_arguments.parse_argstring(self.alias_magic, line)
130 args = magic_arguments.parse_argstring(self.alias_magic, line)
131 shell = self.shell
131 shell = self.shell
132 mman = self.shell.magics_manager
132 mman = self.shell.magics_manager
133 escs = ''.join(magic_escapes.values())
133 escs = ''.join(magic_escapes.values())
134
134
135 target = args.target.lstrip(escs)
135 target = args.target.lstrip(escs)
136 name = args.name.lstrip(escs)
136 name = args.name.lstrip(escs)
137
137
138 params = args.params
138 params = args.params
139 if (params and
139 if (params and
140 ((params.startswith('"') and params.endswith('"'))
140 ((params.startswith('"') and params.endswith('"'))
141 or (params.startswith("'") and params.endswith("'")))):
141 or (params.startswith("'") and params.endswith("'")))):
142 params = params[1:-1]
142 params = params[1:-1]
143
143
144 # Find the requested magics.
144 # Find the requested magics.
145 m_line = shell.find_magic(target, 'line')
145 m_line = shell.find_magic(target, 'line')
146 m_cell = shell.find_magic(target, 'cell')
146 m_cell = shell.find_magic(target, 'cell')
147 if args.line and m_line is None:
147 if args.line and m_line is None:
148 raise UsageError('Line magic function `%s%s` not found.' %
148 raise UsageError('Line magic function `%s%s` not found.' %
149 (magic_escapes['line'], target))
149 (magic_escapes['line'], target))
150 if args.cell and m_cell is None:
150 if args.cell and m_cell is None:
151 raise UsageError('Cell magic function `%s%s` not found.' %
151 raise UsageError('Cell magic function `%s%s` not found.' %
152 (magic_escapes['cell'], target))
152 (magic_escapes['cell'], target))
153
153
154 # If --line and --cell are not specified, default to the ones
154 # If --line and --cell are not specified, default to the ones
155 # that are available.
155 # that are available.
156 if not args.line and not args.cell:
156 if not args.line and not args.cell:
157 if not m_line and not m_cell:
157 if not m_line and not m_cell:
158 raise UsageError(
158 raise UsageError(
159 'No line or cell magic with name `%s` found.' % target
159 'No line or cell magic with name `%s` found.' % target
160 )
160 )
161 args.line = bool(m_line)
161 args.line = bool(m_line)
162 args.cell = bool(m_cell)
162 args.cell = bool(m_cell)
163
163
164 params_str = "" if params is None else " " + params
164 params_str = "" if params is None else " " + params
165
165
166 if args.line:
166 if args.line:
167 mman.register_alias(name, target, 'line', params)
167 mman.register_alias(name, target, 'line', params)
168 print('Created `%s%s` as an alias for `%s%s%s`.' % (
168 print('Created `%s%s` as an alias for `%s%s%s`.' % (
169 magic_escapes['line'], name,
169 magic_escapes['line'], name,
170 magic_escapes['line'], target, params_str))
170 magic_escapes['line'], target, params_str))
171
171
172 if args.cell:
172 if args.cell:
173 mman.register_alias(name, target, 'cell', params)
173 mman.register_alias(name, target, 'cell', params)
174 print('Created `%s%s` as an alias for `%s%s%s`.' % (
174 print('Created `%s%s` as an alias for `%s%s%s`.' % (
175 magic_escapes['cell'], name,
175 magic_escapes['cell'], name,
176 magic_escapes['cell'], target, params_str))
176 magic_escapes['cell'], target, params_str))
177
177
178 @line_magic
178 @line_magic
179 def lsmagic(self, parameter_s=''):
179 def lsmagic(self, parameter_s=''):
180 """List currently available magic functions."""
180 """List currently available magic functions."""
181 return MagicsDisplay(self.shell.magics_manager, ignore=[self.pip])
181 return MagicsDisplay(self.shell.magics_manager, ignore=[self.pip])
182
182
183 def _magic_docs(self, brief=False, rest=False):
183 def _magic_docs(self, brief=False, rest=False):
184 """Return docstrings from magic functions."""
184 """Return docstrings from magic functions."""
185 mman = self.shell.magics_manager
185 mman = self.shell.magics_manager
186 docs = mman.lsmagic_docs(brief, missing='No documentation')
186 docs = mman.lsmagic_docs(brief, missing='No documentation')
187
187
188 if rest:
188 if rest:
189 format_string = '**%s%s**::\n\n%s\n\n'
189 format_string = '**%s%s**::\n\n%s\n\n'
190 else:
190 else:
191 format_string = '%s%s:\n%s\n'
191 format_string = '%s%s:\n%s\n'
192
192
193 return ''.join(
193 return ''.join(
194 [format_string % (magic_escapes['line'], fname,
194 [format_string % (magic_escapes['line'], fname,
195 indent(dedent(fndoc)))
195 indent(dedent(fndoc)))
196 for fname, fndoc in sorted(docs['line'].items())]
196 for fname, fndoc in sorted(docs['line'].items())]
197 +
197 +
198 [format_string % (magic_escapes['cell'], fname,
198 [format_string % (magic_escapes['cell'], fname,
199 indent(dedent(fndoc)))
199 indent(dedent(fndoc)))
200 for fname, fndoc in sorted(docs['cell'].items())]
200 for fname, fndoc in sorted(docs['cell'].items())]
201 )
201 )
202
202
203 @line_magic
203 @line_magic
204 def magic(self, parameter_s=''):
204 def magic(self, parameter_s=''):
205 """Print information about the magic function system.
205 """Print information about the magic function system.
206
206
207 Supported formats: -latex, -brief, -rest
207 Supported formats: -latex, -brief, -rest
208 """
208 """
209
209
210 mode = ''
210 mode = ''
211 try:
211 try:
212 mode = parameter_s.split()[0][1:]
212 mode = parameter_s.split()[0][1:]
213 except IndexError:
213 except IndexError:
214 pass
214 pass
215
215
216 brief = (mode == 'brief')
216 brief = (mode == 'brief')
217 rest = (mode == 'rest')
217 rest = (mode == 'rest')
218 magic_docs = self._magic_docs(brief, rest)
218 magic_docs = self._magic_docs(brief, rest)
219
219
220 if mode == 'latex':
220 if mode == 'latex':
221 print(self.format_latex(magic_docs))
221 print(self.format_latex(magic_docs))
222 return
222 return
223 else:
223 else:
224 magic_docs = format_screen(magic_docs)
224 magic_docs = format_screen(magic_docs)
225
225
226 out = ["""
226 out = ["""
227 IPython's 'magic' functions
227 IPython's 'magic' functions
228 ===========================
228 ===========================
229
229
230 The magic function system provides a series of functions which allow you to
230 The magic function system provides a series of functions which allow you to
231 control the behavior of IPython itself, plus a lot of system-type
231 control the behavior of IPython itself, plus a lot of system-type
232 features. There are two kinds of magics, line-oriented and cell-oriented.
232 features. There are two kinds of magics, line-oriented and cell-oriented.
233
233
234 Line magics are prefixed with the % character and work much like OS
234 Line magics are prefixed with the % character and work much like OS
235 command-line calls: they get as an argument the rest of the line, where
235 command-line calls: they get as an argument the rest of the line, where
236 arguments are passed without parentheses or quotes. For example, this will
236 arguments are passed without parentheses or quotes. For example, this will
237 time the given statement::
237 time the given statement::
238
238
239 %timeit range(1000)
239 %timeit range(1000)
240
240
241 Cell magics are prefixed with a double %%, and they are functions that get as
241 Cell magics are prefixed with a double %%, and they are functions that get as
242 an argument not only the rest of the line, but also the lines below it in a
242 an argument not only the rest of the line, but also the lines below it in a
243 separate argument. These magics are called with two arguments: the rest of the
243 separate argument. These magics are called with two arguments: the rest of the
244 call line and the body of the cell, consisting of the lines below the first.
244 call line and the body of the cell, consisting of the lines below the first.
245 For example::
245 For example::
246
246
247 %%timeit x = numpy.random.randn((100, 100))
247 %%timeit x = numpy.random.randn((100, 100))
248 numpy.linalg.svd(x)
248 numpy.linalg.svd(x)
249
249
250 will time the execution of the numpy svd routine, running the assignment of x
250 will time the execution of the numpy svd routine, running the assignment of x
251 as part of the setup phase, which is not timed.
251 as part of the setup phase, which is not timed.
252
252
253 In a line-oriented client (the terminal or Qt console IPython), starting a new
253 In a line-oriented client (the terminal or Qt console IPython), starting a new
254 input with %% will automatically enter cell mode, and IPython will continue
254 input with %% will automatically enter cell mode, and IPython will continue
255 reading input until a blank line is given. In the notebook, simply type the
255 reading input until a blank line is given. In the notebook, simply type the
256 whole cell as one entity, but keep in mind that the %% escape can only be at
256 whole cell as one entity, but keep in mind that the %% escape can only be at
257 the very start of the cell.
257 the very start of the cell.
258
258
259 NOTE: If you have 'automagic' enabled (via the command line option or with the
259 NOTE: If you have 'automagic' enabled (via the command line option or with the
260 %automagic function), you don't need to type in the % explicitly for line
260 %automagic function), you don't need to type in the % explicitly for line
261 magics; cell magics always require an explicit '%%' escape. By default,
261 magics; cell magics always require an explicit '%%' escape. By default,
262 IPython ships with automagic on, so you should only rarely need the % escape.
262 IPython ships with automagic on, so you should only rarely need the % escape.
263
263
264 Example: typing '%cd mydir' (without the quotes) changes your working directory
264 Example: typing '%cd mydir' (without the quotes) changes your working directory
265 to 'mydir', if it exists.
265 to 'mydir', if it exists.
266
266
267 For a list of the available magic functions, use %lsmagic. For a description
267 For a list of the available magic functions, use %lsmagic. For a description
268 of any of them, type %magic_name?, e.g. '%cd?'.
268 of any of them, type %magic_name?, e.g. '%cd?'.
269
269
270 Currently the magic system has the following functions:""",
270 Currently the magic system has the following functions:""",
271 magic_docs,
271 magic_docs,
272 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
272 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
273 str(self.lsmagic()),
273 str(self.lsmagic()),
274 ]
274 ]
275 page.page('\n'.join(out))
275 page.page('\n'.join(out))
276
276
277
277
278 @line_magic
278 @line_magic
279 def page(self, parameter_s=''):
279 def page(self, parameter_s=''):
280 """Pretty print the object and display it through a pager.
280 """Pretty print the object and display it through a pager.
281
281
282 %page [options] OBJECT
282 %page [options] OBJECT
283
283
284 If no object is given, use _ (last output).
284 If no object is given, use _ (last output).
285
285
286 Options:
286 Options:
287
287
288 -r: page str(object), don't pretty-print it."""
288 -r: page str(object), don't pretty-print it."""
289
289
290 # After a function contributed by Olivier Aubert, slightly modified.
290 # After a function contributed by Olivier Aubert, slightly modified.
291
291
292 # Process options/args
292 # Process options/args
293 opts, args = self.parse_options(parameter_s, 'r')
293 opts, args = self.parse_options(parameter_s, 'r')
294 raw = 'r' in opts
294 raw = 'r' in opts
295
295
296 oname = args and args or '_'
296 oname = args and args or '_'
297 info = self.shell._ofind(oname)
297 info = self.shell._ofind(oname)
298 if info['found']:
298 if info['found']:
299 txt = (raw and str or pformat)( info['obj'] )
299 txt = (raw and str or pformat)( info['obj'] )
300 page.page(txt)
300 page.page(txt)
301 else:
301 else:
302 print('Object `%s` not found' % oname)
302 print('Object `%s` not found' % oname)
303
303
304 @line_magic
304 @line_magic
305 def profile(self, parameter_s=''):
305 def profile(self, parameter_s=''):
306 """DEPRECATED since IPython 2.0.
306 """DEPRECATED since IPython 2.0.
307
307
308 Raise `UsageError`. To profile code use the :magic:`prun` magic.
308 Raise `UsageError`. To profile code use the :magic:`prun` magic.
309
309
310
310
311 See Also
311 See Also
312 --------
312 --------
313 prun : run code using the Python profiler (:magic:`prun`)
313 prun : run code using the Python profiler (:magic:`prun`)
314 """
314 """
315 raise UsageError("The `%profile` magic has been deprecated since IPython 2.0. "
315 raise UsageError("The `%profile` magic has been deprecated since IPython 2.0. "
316 "and removed in IPython 6.0. Please use the value of `get_ipython().profile` instead "
316 "and removed in IPython 6.0. Please use the value of `get_ipython().profile` instead "
317 "to see current profile in use. Perhaps you meant to use `%prun` to profile code?")
317 "to see current profile in use. Perhaps you meant to use `%prun` to profile code?")
318
318
319 @line_magic
319 @line_magic
320 def pprint(self, parameter_s=''):
320 def pprint(self, parameter_s=''):
321 """Toggle pretty printing on/off."""
321 """Toggle pretty printing on/off."""
322 ptformatter = self.shell.display_formatter.formatters['text/plain']
322 ptformatter = self.shell.display_formatter.formatters['text/plain']
323 ptformatter.pprint = bool(1 - ptformatter.pprint)
323 ptformatter.pprint = bool(1 - ptformatter.pprint)
324 print('Pretty printing has been turned',
324 print('Pretty printing has been turned',
325 ['OFF','ON'][ptformatter.pprint])
325 ['OFF','ON'][ptformatter.pprint])
326
326
327 @line_magic
327 @line_magic
328 def colors(self, parameter_s=''):
328 def colors(self, parameter_s=''):
329 """Switch color scheme for prompts, info system and exception handlers.
329 """Switch color scheme for prompts, info system and exception handlers.
330
330
331 Currently implemented schemes: NoColor, Linux, LightBG.
331 Currently implemented schemes: NoColor, Linux, LightBG.
332
332
333 Color scheme names are not case-sensitive.
333 Color scheme names are not case-sensitive.
334
334
335 Examples
335 Examples
336 --------
336 --------
337 To get a plain black and white terminal::
337 To get a plain black and white terminal::
338
338
339 %colors nocolor
339 %colors nocolor
340 """
340 """
341 def color_switch_err(name):
341 def color_switch_err(name):
342 warn('Error changing %s color schemes.\n%s' %
342 warn('Error changing %s color schemes.\n%s' %
343 (name, sys.exc_info()[1]), stacklevel=2)
343 (name, sys.exc_info()[1]), stacklevel=2)
344
344
345
345
346 new_scheme = parameter_s.strip()
346 new_scheme = parameter_s.strip()
347 if not new_scheme:
347 if not new_scheme:
348 raise UsageError(
348 raise UsageError(
349 "%colors: you must specify a color scheme. See '%colors?'")
349 "%colors: you must specify a color scheme. See '%colors?'")
350 # local shortcut
350 # local shortcut
351 shell = self.shell
351 shell = self.shell
352
352
353 # Set shell colour scheme
353 # Set shell colour scheme
354 try:
354 try:
355 shell.colors = new_scheme
355 shell.colors = new_scheme
356 shell.refresh_style()
356 shell.refresh_style()
357 except:
357 except:
358 color_switch_err('shell')
358 color_switch_err('shell')
359
359
360 # Set exception colors
360 # Set exception colors
361 try:
361 try:
362 shell.InteractiveTB.set_colors(scheme = new_scheme)
362 shell.InteractiveTB.set_colors(scheme = new_scheme)
363 shell.SyntaxTB.set_colors(scheme = new_scheme)
363 shell.SyntaxTB.set_colors(scheme = new_scheme)
364 except:
364 except:
365 color_switch_err('exception')
365 color_switch_err('exception')
366
366
367 # Set info (for 'object?') colors
367 # Set info (for 'object?') colors
368 if shell.color_info:
368 if shell.color_info:
369 try:
369 try:
370 shell.inspector.set_active_scheme(new_scheme)
370 shell.inspector.set_active_scheme(new_scheme)
371 except:
371 except:
372 color_switch_err('object inspector')
372 color_switch_err('object inspector')
373 else:
373 else:
374 shell.inspector.set_active_scheme('NoColor')
374 shell.inspector.set_active_scheme('NoColor')
375
375
376 @line_magic
376 @line_magic
377 def xmode(self, parameter_s=''):
377 def xmode(self, parameter_s=''):
378 """Switch modes for the exception handlers.
378 """Switch modes for the exception handlers.
379
379
380 Valid modes: Plain, Context and Verbose.
380 Valid modes: Plain, Context and Verbose.
381
381
382 If called without arguments, acts as a toggle."""
382 If called without arguments, acts as a toggle."""
383
383
384 def xmode_switch_err(name):
384 def xmode_switch_err(name):
385 warn('Error changing %s exception modes.\n%s' %
385 warn('Error changing %s exception modes.\n%s' %
386 (name,sys.exc_info()[1]))
386 (name,sys.exc_info()[1]))
387
387
388 shell = self.shell
388 shell = self.shell
389 new_mode = parameter_s.strip().capitalize()
389 new_mode = parameter_s.strip().capitalize()
390 try:
390 try:
391 shell.InteractiveTB.set_mode(mode=new_mode)
391 shell.InteractiveTB.set_mode(mode=new_mode)
392 print('Exception reporting mode:',shell.InteractiveTB.mode)
392 print('Exception reporting mode:',shell.InteractiveTB.mode)
393 except:
393 except:
394 xmode_switch_err('user')
394 xmode_switch_err('user')
395
395
396 @line_magic
396 @line_magic
397 def pip(self, args=''):
397 def pip(self, args=''):
398 """
398 """
399 Intercept usage of ``pip`` in IPython and direct user to run command outside of IPython.
399 Intercept usage of ``pip`` in IPython and direct user to run command outside of IPython.
400 """
400 """
401 print(textwrap.dedent('''
401 print(textwrap.dedent('''
402 The following command must be run outside of the IPython shell:
402 The following command must be run outside of the IPython shell:
403
403
404 $ pip {args}
404 $ pip {args}
405
405
406 The Python package manager (pip) can only be used from outside of IPython.
406 The Python package manager (pip) can only be used from outside of IPython.
407 Please reissue the `pip` command in a separate terminal or command prompt.
407 Please reissue the `pip` command in a separate terminal or command prompt.
408
408
409 See the Python documentation for more informations on how to install packages:
409 See the Python documentation for more information on how to install packages:
410
410
411 https://docs.python.org/3/installing/'''.format(args=args)))
411 https://docs.python.org/3/installing/'''.format(args=args)))
412
412
413 @line_magic
413 @line_magic
414 def quickref(self, arg):
414 def quickref(self, arg):
415 """ Show a quick reference sheet """
415 """ Show a quick reference sheet """
416 from IPython.core.usage import quick_reference
416 from IPython.core.usage import quick_reference
417 qr = quick_reference + self._magic_docs(brief=True)
417 qr = quick_reference + self._magic_docs(brief=True)
418 page.page(qr)
418 page.page(qr)
419
419
420 @line_magic
420 @line_magic
421 def doctest_mode(self, parameter_s=''):
421 def doctest_mode(self, parameter_s=''):
422 """Toggle doctest mode on and off.
422 """Toggle doctest mode on and off.
423
423
424 This mode is intended to make IPython behave as much as possible like a
424 This mode is intended to make IPython behave as much as possible like a
425 plain Python shell, from the perspective of how its prompts, exceptions
425 plain Python shell, from the perspective of how its prompts, exceptions
426 and output look. This makes it easy to copy and paste parts of a
426 and output look. This makes it easy to copy and paste parts of a
427 session into doctests. It does so by:
427 session into doctests. It does so by:
428
428
429 - Changing the prompts to the classic ``>>>`` ones.
429 - Changing the prompts to the classic ``>>>`` ones.
430 - Changing the exception reporting mode to 'Plain'.
430 - Changing the exception reporting mode to 'Plain'.
431 - Disabling pretty-printing of output.
431 - Disabling pretty-printing of output.
432
432
433 Note that IPython also supports the pasting of code snippets that have
433 Note that IPython also supports the pasting of code snippets that have
434 leading '>>>' and '...' prompts in them. This means that you can paste
434 leading '>>>' and '...' prompts in them. This means that you can paste
435 doctests from files or docstrings (even if they have leading
435 doctests from files or docstrings (even if they have leading
436 whitespace), and the code will execute correctly. You can then use
436 whitespace), and the code will execute correctly. You can then use
437 '%history -t' to see the translated history; this will give you the
437 '%history -t' to see the translated history; this will give you the
438 input after removal of all the leading prompts and whitespace, which
438 input after removal of all the leading prompts and whitespace, which
439 can be pasted back into an editor.
439 can be pasted back into an editor.
440
440
441 With these features, you can switch into this mode easily whenever you
441 With these features, you can switch into this mode easily whenever you
442 need to do testing and changes to doctests, without having to leave
442 need to do testing and changes to doctests, without having to leave
443 your existing IPython session.
443 your existing IPython session.
444 """
444 """
445
445
446 # Shorthands
446 # Shorthands
447 shell = self.shell
447 shell = self.shell
448 meta = shell.meta
448 meta = shell.meta
449 disp_formatter = self.shell.display_formatter
449 disp_formatter = self.shell.display_formatter
450 ptformatter = disp_formatter.formatters['text/plain']
450 ptformatter = disp_formatter.formatters['text/plain']
451 # dstore is a data store kept in the instance metadata bag to track any
451 # dstore is a data store kept in the instance metadata bag to track any
452 # changes we make, so we can undo them later.
452 # changes we make, so we can undo them later.
453 dstore = meta.setdefault('doctest_mode',Struct())
453 dstore = meta.setdefault('doctest_mode',Struct())
454 save_dstore = dstore.setdefault
454 save_dstore = dstore.setdefault
455
455
456 # save a few values we'll need to recover later
456 # save a few values we'll need to recover later
457 mode = save_dstore('mode',False)
457 mode = save_dstore('mode',False)
458 save_dstore('rc_pprint',ptformatter.pprint)
458 save_dstore('rc_pprint',ptformatter.pprint)
459 save_dstore('xmode',shell.InteractiveTB.mode)
459 save_dstore('xmode',shell.InteractiveTB.mode)
460 save_dstore('rc_separate_out',shell.separate_out)
460 save_dstore('rc_separate_out',shell.separate_out)
461 save_dstore('rc_separate_out2',shell.separate_out2)
461 save_dstore('rc_separate_out2',shell.separate_out2)
462 save_dstore('rc_separate_in',shell.separate_in)
462 save_dstore('rc_separate_in',shell.separate_in)
463 save_dstore('rc_active_types',disp_formatter.active_types)
463 save_dstore('rc_active_types',disp_formatter.active_types)
464
464
465 if not mode:
465 if not mode:
466 # turn on
466 # turn on
467
467
468 # Prompt separators like plain python
468 # Prompt separators like plain python
469 shell.separate_in = ''
469 shell.separate_in = ''
470 shell.separate_out = ''
470 shell.separate_out = ''
471 shell.separate_out2 = ''
471 shell.separate_out2 = ''
472
472
473
473
474 ptformatter.pprint = False
474 ptformatter.pprint = False
475 disp_formatter.active_types = ['text/plain']
475 disp_formatter.active_types = ['text/plain']
476
476
477 shell.magic('xmode Plain')
477 shell.magic('xmode Plain')
478 else:
478 else:
479 # turn off
479 # turn off
480 shell.separate_in = dstore.rc_separate_in
480 shell.separate_in = dstore.rc_separate_in
481
481
482 shell.separate_out = dstore.rc_separate_out
482 shell.separate_out = dstore.rc_separate_out
483 shell.separate_out2 = dstore.rc_separate_out2
483 shell.separate_out2 = dstore.rc_separate_out2
484
484
485 ptformatter.pprint = dstore.rc_pprint
485 ptformatter.pprint = dstore.rc_pprint
486 disp_formatter.active_types = dstore.rc_active_types
486 disp_formatter.active_types = dstore.rc_active_types
487
487
488 shell.magic('xmode ' + dstore.xmode)
488 shell.magic('xmode ' + dstore.xmode)
489
489
490 # mode here is the state before we switch; switch_doctest_mode takes
490 # mode here is the state before we switch; switch_doctest_mode takes
491 # the mode we're switching to.
491 # the mode we're switching to.
492 shell.switch_doctest_mode(not mode)
492 shell.switch_doctest_mode(not mode)
493
493
494 # Store new mode and inform
494 # Store new mode and inform
495 dstore.mode = bool(not mode)
495 dstore.mode = bool(not mode)
496 mode_label = ['OFF','ON'][dstore.mode]
496 mode_label = ['OFF','ON'][dstore.mode]
497 print('Doctest mode is:', mode_label)
497 print('Doctest mode is:', mode_label)
498
498
499 @line_magic
499 @line_magic
500 def gui(self, parameter_s=''):
500 def gui(self, parameter_s=''):
501 """Enable or disable IPython GUI event loop integration.
501 """Enable or disable IPython GUI event loop integration.
502
502
503 %gui [GUINAME]
503 %gui [GUINAME]
504
504
505 This magic replaces IPython's threaded shells that were activated
505 This magic replaces IPython's threaded shells that were activated
506 using the (pylab/wthread/etc.) command line flags. GUI toolkits
506 using the (pylab/wthread/etc.) command line flags. GUI toolkits
507 can now be enabled at runtime and keyboard
507 can now be enabled at runtime and keyboard
508 interrupts should work without any problems. The following toolkits
508 interrupts should work without any problems. The following toolkits
509 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
509 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
510
510
511 %gui wx # enable wxPython event loop integration
511 %gui wx # enable wxPython event loop integration
512 %gui qt4|qt # enable PyQt4 event loop integration
512 %gui qt4|qt # enable PyQt4 event loop integration
513 %gui qt5 # enable PyQt5 event loop integration
513 %gui qt5 # enable PyQt5 event loop integration
514 %gui gtk # enable PyGTK event loop integration
514 %gui gtk # enable PyGTK event loop integration
515 %gui gtk3 # enable Gtk3 event loop integration
515 %gui gtk3 # enable Gtk3 event loop integration
516 %gui tk # enable Tk event loop integration
516 %gui tk # enable Tk event loop integration
517 %gui osx # enable Cocoa event loop integration
517 %gui osx # enable Cocoa event loop integration
518 # (requires %matplotlib 1.1)
518 # (requires %matplotlib 1.1)
519 %gui # disable all event loop integration
519 %gui # disable all event loop integration
520
520
521 WARNING: after any of these has been called you can simply create
521 WARNING: after any of these has been called you can simply create
522 an application object, but DO NOT start the event loop yourself, as
522 an application object, but DO NOT start the event loop yourself, as
523 we have already handled that.
523 we have already handled that.
524 """
524 """
525 opts, arg = self.parse_options(parameter_s, '')
525 opts, arg = self.parse_options(parameter_s, '')
526 if arg=='': arg = None
526 if arg=='': arg = None
527 try:
527 try:
528 return self.shell.enable_gui(arg)
528 return self.shell.enable_gui(arg)
529 except Exception as e:
529 except Exception as e:
530 # print simple error message, rather than traceback if we can't
530 # print simple error message, rather than traceback if we can't
531 # hook up the GUI
531 # hook up the GUI
532 error(str(e))
532 error(str(e))
533
533
534 @skip_doctest
534 @skip_doctest
535 @line_magic
535 @line_magic
536 def precision(self, s=''):
536 def precision(self, s=''):
537 """Set floating point precision for pretty printing.
537 """Set floating point precision for pretty printing.
538
538
539 Can set either integer precision or a format string.
539 Can set either integer precision or a format string.
540
540
541 If numpy has been imported and precision is an int,
541 If numpy has been imported and precision is an int,
542 numpy display precision will also be set, via ``numpy.set_printoptions``.
542 numpy display precision will also be set, via ``numpy.set_printoptions``.
543
543
544 If no argument is given, defaults will be restored.
544 If no argument is given, defaults will be restored.
545
545
546 Examples
546 Examples
547 --------
547 --------
548 ::
548 ::
549
549
550 In [1]: from math import pi
550 In [1]: from math import pi
551
551
552 In [2]: %precision 3
552 In [2]: %precision 3
553 Out[2]: u'%.3f'
553 Out[2]: u'%.3f'
554
554
555 In [3]: pi
555 In [3]: pi
556 Out[3]: 3.142
556 Out[3]: 3.142
557
557
558 In [4]: %precision %i
558 In [4]: %precision %i
559 Out[4]: u'%i'
559 Out[4]: u'%i'
560
560
561 In [5]: pi
561 In [5]: pi
562 Out[5]: 3
562 Out[5]: 3
563
563
564 In [6]: %precision %e
564 In [6]: %precision %e
565 Out[6]: u'%e'
565 Out[6]: u'%e'
566
566
567 In [7]: pi**10
567 In [7]: pi**10
568 Out[7]: 9.364805e+04
568 Out[7]: 9.364805e+04
569
569
570 In [8]: %precision
570 In [8]: %precision
571 Out[8]: u'%r'
571 Out[8]: u'%r'
572
572
573 In [9]: pi**10
573 In [9]: pi**10
574 Out[9]: 93648.047476082982
574 Out[9]: 93648.047476082982
575 """
575 """
576 ptformatter = self.shell.display_formatter.formatters['text/plain']
576 ptformatter = self.shell.display_formatter.formatters['text/plain']
577 ptformatter.float_precision = s
577 ptformatter.float_precision = s
578 return ptformatter.float_format
578 return ptformatter.float_format
579
579
580 @magic_arguments.magic_arguments()
580 @magic_arguments.magic_arguments()
581 @magic_arguments.argument(
581 @magic_arguments.argument(
582 '-e', '--export', action='store_true', default=False,
582 '-e', '--export', action='store_true', default=False,
583 help=argparse.SUPPRESS
583 help=argparse.SUPPRESS
584 )
584 )
585 @magic_arguments.argument(
585 @magic_arguments.argument(
586 'filename', type=str,
586 'filename', type=str,
587 help='Notebook name or filename'
587 help='Notebook name or filename'
588 )
588 )
589 @line_magic
589 @line_magic
590 def notebook(self, s):
590 def notebook(self, s):
591 """Export and convert IPython notebooks.
591 """Export and convert IPython notebooks.
592
592
593 This function can export the current IPython history to a notebook file.
593 This function can export the current IPython history to a notebook file.
594 For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
594 For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
595
595
596 The -e or --export flag is deprecated in IPython 5.2, and will be
596 The -e or --export flag is deprecated in IPython 5.2, and will be
597 removed in the future.
597 removed in the future.
598 """
598 """
599 args = magic_arguments.parse_argstring(self.notebook, s)
599 args = magic_arguments.parse_argstring(self.notebook, s)
600
600
601 from nbformat import write, v4
601 from nbformat import write, v4
602
602
603 cells = []
603 cells = []
604 hist = list(self.shell.history_manager.get_range())
604 hist = list(self.shell.history_manager.get_range())
605 if(len(hist)<=1):
605 if(len(hist)<=1):
606 raise ValueError('History is empty, cannot export')
606 raise ValueError('History is empty, cannot export')
607 for session, execution_count, source in hist[:-1]:
607 for session, execution_count, source in hist[:-1]:
608 cells.append(v4.new_code_cell(
608 cells.append(v4.new_code_cell(
609 execution_count=execution_count,
609 execution_count=execution_count,
610 source=source
610 source=source
611 ))
611 ))
612 nb = v4.new_notebook(cells=cells)
612 nb = v4.new_notebook(cells=cells)
613 with io.open(args.filename, 'w', encoding='utf-8') as f:
613 with io.open(args.filename, 'w', encoding='utf-8') as f:
614 write(nb, f, version=4)
614 write(nb, f, version=4)
@@ -1,740 +1,740 b''
1 """Implementation of code management magic functions.
1 """Implementation of code management magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import inspect
16 import inspect
17 import io
17 import io
18 import os
18 import os
19 import re
19 import re
20 import sys
20 import sys
21 import ast
21 import ast
22 from itertools import chain
22 from itertools import chain
23
23
24 # Our own packages
24 # Our own packages
25 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
25 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
26 from IPython.core.macro import Macro
26 from IPython.core.macro import Macro
27 from IPython.core.magic import Magics, magics_class, line_magic
27 from IPython.core.magic import Magics, magics_class, line_magic
28 from IPython.core.oinspect import find_file, find_source_lines
28 from IPython.core.oinspect import find_file, find_source_lines
29 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.testing.skipdoctest import skip_doctest
30 from IPython.utils import py3compat
30 from IPython.utils import py3compat
31 from IPython.utils.contexts import preserve_keys
31 from IPython.utils.contexts import preserve_keys
32 from IPython.utils.path import get_py_filename
32 from IPython.utils.path import get_py_filename
33 from warnings import warn
33 from warnings import warn
34 from logging import error
34 from logging import error
35 from IPython.utils.text import get_text_list
35 from IPython.utils.text import get_text_list
36
36
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 # Magic implementation classes
38 # Magic implementation classes
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40
40
41 # Used for exception handling in magic_edit
41 # Used for exception handling in magic_edit
42 class MacroToEdit(ValueError): pass
42 class MacroToEdit(ValueError): pass
43
43
44 ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$")
44 ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$")
45
45
46 # To match, e.g. 8-10 1:5 :10 3-
46 # To match, e.g. 8-10 1:5 :10 3-
47 range_re = re.compile(r"""
47 range_re = re.compile(r"""
48 (?P<start>\d+)?
48 (?P<start>\d+)?
49 ((?P<sep>[\-:])
49 ((?P<sep>[\-:])
50 (?P<end>\d+)?)?
50 (?P<end>\d+)?)?
51 $""", re.VERBOSE)
51 $""", re.VERBOSE)
52
52
53
53
54 def extract_code_ranges(ranges_str):
54 def extract_code_ranges(ranges_str):
55 """Turn a string of range for %%load into 2-tuples of (start, stop)
55 """Turn a string of range for %%load into 2-tuples of (start, stop)
56 ready to use as a slice of the content splitted by lines.
56 ready to use as a slice of the content splitted by lines.
57
57
58 Examples
58 Examples
59 --------
59 --------
60 list(extract_input_ranges("5-10 2"))
60 list(extract_input_ranges("5-10 2"))
61 [(4, 10), (1, 2)]
61 [(4, 10), (1, 2)]
62 """
62 """
63 for range_str in ranges_str.split():
63 for range_str in ranges_str.split():
64 rmatch = range_re.match(range_str)
64 rmatch = range_re.match(range_str)
65 if not rmatch:
65 if not rmatch:
66 continue
66 continue
67 sep = rmatch.group("sep")
67 sep = rmatch.group("sep")
68 start = rmatch.group("start")
68 start = rmatch.group("start")
69 end = rmatch.group("end")
69 end = rmatch.group("end")
70
70
71 if sep == '-':
71 if sep == '-':
72 start = int(start) - 1 if start else None
72 start = int(start) - 1 if start else None
73 end = int(end) if end else None
73 end = int(end) if end else None
74 elif sep == ':':
74 elif sep == ':':
75 start = int(start) - 1 if start else None
75 start = int(start) - 1 if start else None
76 end = int(end) - 1 if end else None
76 end = int(end) - 1 if end else None
77 else:
77 else:
78 end = int(start)
78 end = int(start)
79 start = int(start) - 1
79 start = int(start) - 1
80 yield (start, end)
80 yield (start, end)
81
81
82
82
83 def extract_symbols(code, symbols):
83 def extract_symbols(code, symbols):
84 """
84 """
85 Return a tuple (blocks, not_found)
85 Return a tuple (blocks, not_found)
86 where ``blocks`` is a list of code fragments
86 where ``blocks`` is a list of code fragments
87 for each symbol parsed from code, and ``not_found`` are
87 for each symbol parsed from code, and ``not_found`` are
88 symbols not found in the code.
88 symbols not found in the code.
89
89
90 For example::
90 For example::
91
91
92 In [1]: code = '''a = 10
92 In [1]: code = '''a = 10
93 ...: def b(): return 42
93 ...: def b(): return 42
94 ...: class A: pass'''
94 ...: class A: pass'''
95
95
96 In [2]: extract_symbols(code, 'A,b,z')
96 In [2]: extract_symbols(code, 'A,b,z')
97 Out[2]: (['class A: pass\\n', 'def b(): return 42\\n'], ['z'])
97 Out[2]: (['class A: pass\\n', 'def b(): return 42\\n'], ['z'])
98 """
98 """
99 symbols = symbols.split(',')
99 symbols = symbols.split(',')
100
100
101 # this will raise SyntaxError if code isn't valid Python
101 # this will raise SyntaxError if code isn't valid Python
102 py_code = ast.parse(code)
102 py_code = ast.parse(code)
103
103
104 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
104 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
105 code = code.split('\n')
105 code = code.split('\n')
106
106
107 symbols_lines = {}
107 symbols_lines = {}
108
108
109 # we already know the start_lineno of each symbol (marks).
109 # we already know the start_lineno of each symbol (marks).
110 # To find each end_lineno, we traverse in reverse order until each
110 # To find each end_lineno, we traverse in reverse order until each
111 # non-blank line
111 # non-blank line
112 end = len(code)
112 end = len(code)
113 for name, start in reversed(marks):
113 for name, start in reversed(marks):
114 while not code[end - 1].strip():
114 while not code[end - 1].strip():
115 end -= 1
115 end -= 1
116 if name:
116 if name:
117 symbols_lines[name] = (start - 1, end)
117 symbols_lines[name] = (start - 1, end)
118 end = start - 1
118 end = start - 1
119
119
120 # Now symbols_lines is a map
120 # Now symbols_lines is a map
121 # {'symbol_name': (start_lineno, end_lineno), ...}
121 # {'symbol_name': (start_lineno, end_lineno), ...}
122
122
123 # fill a list with chunks of codes for each requested symbol
123 # fill a list with chunks of codes for each requested symbol
124 blocks = []
124 blocks = []
125 not_found = []
125 not_found = []
126 for symbol in symbols:
126 for symbol in symbols:
127 if symbol in symbols_lines:
127 if symbol in symbols_lines:
128 start, end = symbols_lines[symbol]
128 start, end = symbols_lines[symbol]
129 blocks.append('\n'.join(code[start:end]) + '\n')
129 blocks.append('\n'.join(code[start:end]) + '\n')
130 else:
130 else:
131 not_found.append(symbol)
131 not_found.append(symbol)
132
132
133 return blocks, not_found
133 return blocks, not_found
134
134
135 def strip_initial_indent(lines):
135 def strip_initial_indent(lines):
136 """For %load, strip indent from lines until finding an unindented line.
136 """For %load, strip indent from lines until finding an unindented line.
137
137
138 https://github.com/ipython/ipython/issues/9775
138 https://github.com/ipython/ipython/issues/9775
139 """
139 """
140 indent_re = re.compile(r'\s+')
140 indent_re = re.compile(r'\s+')
141
141
142 it = iter(lines)
142 it = iter(lines)
143 first_line = next(it)
143 first_line = next(it)
144 indent_match = indent_re.match(first_line)
144 indent_match = indent_re.match(first_line)
145
145
146 if indent_match:
146 if indent_match:
147 # First line was indented
147 # First line was indented
148 indent = indent_match.group()
148 indent = indent_match.group()
149 yield first_line[len(indent):]
149 yield first_line[len(indent):]
150
150
151 for line in it:
151 for line in it:
152 if line.startswith(indent):
152 if line.startswith(indent):
153 yield line[len(indent):]
153 yield line[len(indent):]
154 else:
154 else:
155 # Less indented than the first line - stop dedenting
155 # Less indented than the first line - stop dedenting
156 yield line
156 yield line
157 break
157 break
158 else:
158 else:
159 yield first_line
159 yield first_line
160
160
161 # Pass the remaining lines through without dedenting
161 # Pass the remaining lines through without dedenting
162 for line in it:
162 for line in it:
163 yield line
163 yield line
164
164
165
165
166 class InteractivelyDefined(Exception):
166 class InteractivelyDefined(Exception):
167 """Exception for interactively defined variable in magic_edit"""
167 """Exception for interactively defined variable in magic_edit"""
168 def __init__(self, index):
168 def __init__(self, index):
169 self.index = index
169 self.index = index
170
170
171
171
172 @magics_class
172 @magics_class
173 class CodeMagics(Magics):
173 class CodeMagics(Magics):
174 """Magics related to code management (loading, saving, editing, ...)."""
174 """Magics related to code management (loading, saving, editing, ...)."""
175
175
176 def __init__(self, *args, **kwargs):
176 def __init__(self, *args, **kwargs):
177 self._knowntemps = set()
177 self._knowntemps = set()
178 super(CodeMagics, self).__init__(*args, **kwargs)
178 super(CodeMagics, self).__init__(*args, **kwargs)
179
179
180 @line_magic
180 @line_magic
181 def save(self, parameter_s=''):
181 def save(self, parameter_s=''):
182 """Save a set of lines or a macro to a given filename.
182 """Save a set of lines or a macro to a given filename.
183
183
184 Usage:\\
184 Usage:\\
185 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
185 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
186
186
187 Options:
187 Options:
188
188
189 -r: use 'raw' input. By default, the 'processed' history is used,
189 -r: use 'raw' input. By default, the 'processed' history is used,
190 so that magics are loaded in their transformed version to valid
190 so that magics are loaded in their transformed version to valid
191 Python. If this option is given, the raw input as typed as the
191 Python. If this option is given, the raw input as typed as the
192 command line is used instead.
192 command line is used instead.
193
193
194 -f: force overwrite. If file exists, %save will prompt for overwrite
194 -f: force overwrite. If file exists, %save will prompt for overwrite
195 unless -f is given.
195 unless -f is given.
196
196
197 -a: append to the file instead of overwriting it.
197 -a: append to the file instead of overwriting it.
198
198
199 This function uses the same syntax as %history for input ranges,
199 This function uses the same syntax as %history for input ranges,
200 then saves the lines to the filename you specify.
200 then saves the lines to the filename you specify.
201
201
202 It adds a '.py' extension to the file if you don't do so yourself, and
202 It adds a '.py' extension to the file if you don't do so yourself, and
203 it asks for confirmation before overwriting existing files.
203 it asks for confirmation before overwriting existing files.
204
204
205 If `-r` option is used, the default extension is `.ipy`.
205 If `-r` option is used, the default extension is `.ipy`.
206 """
206 """
207
207
208 opts,args = self.parse_options(parameter_s,'fra',mode='list')
208 opts,args = self.parse_options(parameter_s,'fra',mode='list')
209 if not args:
209 if not args:
210 raise UsageError('Missing filename.')
210 raise UsageError('Missing filename.')
211 raw = 'r' in opts
211 raw = 'r' in opts
212 force = 'f' in opts
212 force = 'f' in opts
213 append = 'a' in opts
213 append = 'a' in opts
214 mode = 'a' if append else 'w'
214 mode = 'a' if append else 'w'
215 ext = u'.ipy' if raw else u'.py'
215 ext = u'.ipy' if raw else u'.py'
216 fname, codefrom = args[0], " ".join(args[1:])
216 fname, codefrom = args[0], " ".join(args[1:])
217 if not fname.endswith((u'.py',u'.ipy')):
217 if not fname.endswith((u'.py',u'.ipy')):
218 fname += ext
218 fname += ext
219 file_exists = os.path.isfile(fname)
219 file_exists = os.path.isfile(fname)
220 if file_exists and not force and not append:
220 if file_exists and not force and not append:
221 try:
221 try:
222 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
222 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
223 except StdinNotImplementedError:
223 except StdinNotImplementedError:
224 print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
224 print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
225 return
225 return
226 if not overwrite :
226 if not overwrite :
227 print('Operation cancelled.')
227 print('Operation cancelled.')
228 return
228 return
229 try:
229 try:
230 cmds = self.shell.find_user_code(codefrom,raw)
230 cmds = self.shell.find_user_code(codefrom,raw)
231 except (TypeError, ValueError) as e:
231 except (TypeError, ValueError) as e:
232 print(e.args[0])
232 print(e.args[0])
233 return
233 return
234 out = py3compat.cast_unicode(cmds)
234 out = py3compat.cast_unicode(cmds)
235 with io.open(fname, mode, encoding="utf-8") as f:
235 with io.open(fname, mode, encoding="utf-8") as f:
236 if not file_exists or not append:
236 if not file_exists or not append:
237 f.write(u"# coding: utf-8\n")
237 f.write(u"# coding: utf-8\n")
238 f.write(out)
238 f.write(out)
239 # make sure we end on a newline
239 # make sure we end on a newline
240 if not out.endswith(u'\n'):
240 if not out.endswith(u'\n'):
241 f.write(u'\n')
241 f.write(u'\n')
242 print('The following commands were written to file `%s`:' % fname)
242 print('The following commands were written to file `%s`:' % fname)
243 print(cmds)
243 print(cmds)
244
244
245 @line_magic
245 @line_magic
246 def pastebin(self, parameter_s=''):
246 def pastebin(self, parameter_s=''):
247 """Upload code to Github's Gist paste bin, returning the URL.
247 """Upload code to Github's Gist paste bin, returning the URL.
248
248
249 Usage:\\
249 Usage:\\
250 %pastebin [-d "Custom description"] 1-7
250 %pastebin [-d "Custom description"] 1-7
251
251
252 The argument can be an input history range, a filename, or the name of a
252 The argument can be an input history range, a filename, or the name of a
253 string or macro.
253 string or macro.
254
254
255 Options:
255 Options:
256
256
257 -d: Pass a custom description for the gist. The default will say
257 -d: Pass a custom description for the gist. The default will say
258 "Pasted from IPython".
258 "Pasted from IPython".
259 """
259 """
260 opts, args = self.parse_options(parameter_s, 'd:')
260 opts, args = self.parse_options(parameter_s, 'd:')
261
261
262 try:
262 try:
263 code = self.shell.find_user_code(args)
263 code = self.shell.find_user_code(args)
264 except (ValueError, TypeError) as e:
264 except (ValueError, TypeError) as e:
265 print(e.args[0])
265 print(e.args[0])
266 return
266 return
267
267
268 # Deferred import
268 # Deferred import
269 try:
269 try:
270 from urllib.request import urlopen # Py 3
270 from urllib.request import urlopen # Py 3
271 except ImportError:
271 except ImportError:
272 from urllib2 import urlopen
272 from urllib2 import urlopen
273 import json
273 import json
274 post_data = json.dumps({
274 post_data = json.dumps({
275 "description": opts.get('d', "Pasted from IPython"),
275 "description": opts.get('d', "Pasted from IPython"),
276 "public": True,
276 "public": True,
277 "files": {
277 "files": {
278 "file1.py": {
278 "file1.py": {
279 "content": code
279 "content": code
280 }
280 }
281 }
281 }
282 }).encode('utf-8')
282 }).encode('utf-8')
283
283
284 response = urlopen("https://api.github.com/gists", post_data)
284 response = urlopen("https://api.github.com/gists", post_data)
285 response_data = json.loads(response.read().decode('utf-8'))
285 response_data = json.loads(response.read().decode('utf-8'))
286 return response_data['html_url']
286 return response_data['html_url']
287
287
288 @line_magic
288 @line_magic
289 def loadpy(self, arg_s):
289 def loadpy(self, arg_s):
290 """Alias of `%load`
290 """Alias of `%load`
291
291
292 `%loadpy` has gained some flexibility and dropped the requirement of a `.py`
292 `%loadpy` has gained some flexibility and dropped the requirement of a `.py`
293 extension. So it has been renamed simply into %load. You can look at
293 extension. So it has been renamed simply into %load. You can look at
294 `%load`'s docstring for more info.
294 `%load`'s docstring for more info.
295 """
295 """
296 self.load(arg_s)
296 self.load(arg_s)
297
297
298 @line_magic
298 @line_magic
299 def load(self, arg_s):
299 def load(self, arg_s):
300 """Load code into the current frontend.
300 """Load code into the current frontend.
301
301
302 Usage:\\
302 Usage:\\
303 %load [options] source
303 %load [options] source
304
304
305 where source can be a filename, URL, input history range, macro, or
305 where source can be a filename, URL, input history range, macro, or
306 element in the user namespace
306 element in the user namespace
307
307
308 Options:
308 Options:
309
309
310 -r <lines>: Specify lines or ranges of lines to load from the source.
310 -r <lines>: Specify lines or ranges of lines to load from the source.
311 Ranges could be specified as x-y (x..y) or in python-style x:y
311 Ranges could be specified as x-y (x..y) or in python-style x:y
312 (x..(y-1)). Both limits x and y can be left blank (meaning the
312 (x..(y-1)). Both limits x and y can be left blank (meaning the
313 beginning and end of the file, respectively).
313 beginning and end of the file, respectively).
314
314
315 -s <symbols>: Specify function or classes to load from python source.
315 -s <symbols>: Specify function or classes to load from python source.
316
316
317 -y : Don't ask confirmation for loading source above 200 000 characters.
317 -y : Don't ask confirmation for loading source above 200 000 characters.
318
318
319 -n : Include the user's namespace when searching for source code.
319 -n : Include the user's namespace when searching for source code.
320
320
321 This magic command can either take a local filename, a URL, an history
321 This magic command can either take a local filename, a URL, an history
322 range (see %history) or a macro as argument, it will prompt for
322 range (see %history) or a macro as argument, it will prompt for
323 confirmation before loading source with more than 200 000 characters, unless
323 confirmation before loading source with more than 200 000 characters, unless
324 -y flag is passed or if the frontend does not support raw_input::
324 -y flag is passed or if the frontend does not support raw_input::
325
325
326 %load myscript.py
326 %load myscript.py
327 %load 7-27
327 %load 7-27
328 %load myMacro
328 %load myMacro
329 %load http://www.example.com/myscript.py
329 %load http://www.example.com/myscript.py
330 %load -r 5-10 myscript.py
330 %load -r 5-10 myscript.py
331 %load -r 10-20,30,40: foo.py
331 %load -r 10-20,30,40: foo.py
332 %load -s MyClass,wonder_function myscript.py
332 %load -s MyClass,wonder_function myscript.py
333 %load -n MyClass
333 %load -n MyClass
334 %load -n my_module.wonder_function
334 %load -n my_module.wonder_function
335 """
335 """
336 opts,args = self.parse_options(arg_s,'yns:r:')
336 opts,args = self.parse_options(arg_s,'yns:r:')
337
337
338 if not args:
338 if not args:
339 raise UsageError('Missing filename, URL, input history range, '
339 raise UsageError('Missing filename, URL, input history range, '
340 'macro, or element in the user namespace.')
340 'macro, or element in the user namespace.')
341
341
342 search_ns = 'n' in opts
342 search_ns = 'n' in opts
343
343
344 contents = self.shell.find_user_code(args, search_ns=search_ns)
344 contents = self.shell.find_user_code(args, search_ns=search_ns)
345
345
346 if 's' in opts:
346 if 's' in opts:
347 try:
347 try:
348 blocks, not_found = extract_symbols(contents, opts['s'])
348 blocks, not_found = extract_symbols(contents, opts['s'])
349 except SyntaxError:
349 except SyntaxError:
350 # non python code
350 # non python code
351 error("Unable to parse the input as valid Python code")
351 error("Unable to parse the input as valid Python code")
352 return
352 return
353
353
354 if len(not_found) == 1:
354 if len(not_found) == 1:
355 warn('The symbol `%s` was not found' % not_found[0])
355 warn('The symbol `%s` was not found' % not_found[0])
356 elif len(not_found) > 1:
356 elif len(not_found) > 1:
357 warn('The symbols %s were not found' % get_text_list(not_found,
357 warn('The symbols %s were not found' % get_text_list(not_found,
358 wrap_item_with='`')
358 wrap_item_with='`')
359 )
359 )
360
360
361 contents = '\n'.join(blocks)
361 contents = '\n'.join(blocks)
362
362
363 if 'r' in opts:
363 if 'r' in opts:
364 ranges = opts['r'].replace(',', ' ')
364 ranges = opts['r'].replace(',', ' ')
365 lines = contents.split('\n')
365 lines = contents.split('\n')
366 slices = extract_code_ranges(ranges)
366 slices = extract_code_ranges(ranges)
367 contents = [lines[slice(*slc)] for slc in slices]
367 contents = [lines[slice(*slc)] for slc in slices]
368 contents = '\n'.join(strip_initial_indent(chain.from_iterable(contents)))
368 contents = '\n'.join(strip_initial_indent(chain.from_iterable(contents)))
369
369
370 l = len(contents)
370 l = len(contents)
371
371
372 # 200 000 is ~ 2500 full 80 caracter lines
372 # 200 000 is ~ 2500 full 80 character lines
373 # so in average, more than 5000 lines
373 # so in average, more than 5000 lines
374 if l > 200000 and 'y' not in opts:
374 if l > 200000 and 'y' not in opts:
375 try:
375 try:
376 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
376 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
377 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
377 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
378 except StdinNotImplementedError:
378 except StdinNotImplementedError:
379 #asume yes if raw input not implemented
379 #assume yes if raw input not implemented
380 ans = True
380 ans = True
381
381
382 if ans is False :
382 if ans is False :
383 print('Operation cancelled.')
383 print('Operation cancelled.')
384 return
384 return
385
385
386 contents = "# %load {}\n".format(arg_s) + contents
386 contents = "# %load {}\n".format(arg_s) + contents
387
387
388 self.shell.set_next_input(contents, replace=True)
388 self.shell.set_next_input(contents, replace=True)
389
389
390 @staticmethod
390 @staticmethod
391 def _find_edit_target(shell, args, opts, last_call):
391 def _find_edit_target(shell, args, opts, last_call):
392 """Utility method used by magic_edit to find what to edit."""
392 """Utility method used by magic_edit to find what to edit."""
393
393
394 def make_filename(arg):
394 def make_filename(arg):
395 "Make a filename from the given args"
395 "Make a filename from the given args"
396 try:
396 try:
397 filename = get_py_filename(arg)
397 filename = get_py_filename(arg)
398 except IOError:
398 except IOError:
399 # If it ends with .py but doesn't already exist, assume we want
399 # If it ends with .py but doesn't already exist, assume we want
400 # a new file.
400 # a new file.
401 if arg.endswith('.py'):
401 if arg.endswith('.py'):
402 filename = arg
402 filename = arg
403 else:
403 else:
404 filename = None
404 filename = None
405 return filename
405 return filename
406
406
407 # Set a few locals from the options for convenience:
407 # Set a few locals from the options for convenience:
408 opts_prev = 'p' in opts
408 opts_prev = 'p' in opts
409 opts_raw = 'r' in opts
409 opts_raw = 'r' in opts
410
410
411 # custom exceptions
411 # custom exceptions
412 class DataIsObject(Exception): pass
412 class DataIsObject(Exception): pass
413
413
414 # Default line number value
414 # Default line number value
415 lineno = opts.get('n',None)
415 lineno = opts.get('n',None)
416
416
417 if opts_prev:
417 if opts_prev:
418 args = '_%s' % last_call[0]
418 args = '_%s' % last_call[0]
419 if args not in shell.user_ns:
419 if args not in shell.user_ns:
420 args = last_call[1]
420 args = last_call[1]
421
421
422 # by default this is done with temp files, except when the given
422 # by default this is done with temp files, except when the given
423 # arg is a filename
423 # arg is a filename
424 use_temp = True
424 use_temp = True
425
425
426 data = ''
426 data = ''
427
427
428 # First, see if the arguments should be a filename.
428 # First, see if the arguments should be a filename.
429 filename = make_filename(args)
429 filename = make_filename(args)
430 if filename:
430 if filename:
431 use_temp = False
431 use_temp = False
432 elif args:
432 elif args:
433 # Mode where user specifies ranges of lines, like in %macro.
433 # Mode where user specifies ranges of lines, like in %macro.
434 data = shell.extract_input_lines(args, opts_raw)
434 data = shell.extract_input_lines(args, opts_raw)
435 if not data:
435 if not data:
436 try:
436 try:
437 # Load the parameter given as a variable. If not a string,
437 # Load the parameter given as a variable. If not a string,
438 # process it as an object instead (below)
438 # process it as an object instead (below)
439
439
440 #print '*** args',args,'type',type(args) # dbg
440 #print '*** args',args,'type',type(args) # dbg
441 data = eval(args, shell.user_ns)
441 data = eval(args, shell.user_ns)
442 if not isinstance(data, str):
442 if not isinstance(data, str):
443 raise DataIsObject
443 raise DataIsObject
444
444
445 except (NameError,SyntaxError):
445 except (NameError,SyntaxError):
446 # given argument is not a variable, try as a filename
446 # given argument is not a variable, try as a filename
447 filename = make_filename(args)
447 filename = make_filename(args)
448 if filename is None:
448 if filename is None:
449 warn("Argument given (%s) can't be found as a variable "
449 warn("Argument given (%s) can't be found as a variable "
450 "or as a filename." % args)
450 "or as a filename." % args)
451 return (None, None, None)
451 return (None, None, None)
452 use_temp = False
452 use_temp = False
453
453
454 except DataIsObject:
454 except DataIsObject:
455 # macros have a special edit function
455 # macros have a special edit function
456 if isinstance(data, Macro):
456 if isinstance(data, Macro):
457 raise MacroToEdit(data)
457 raise MacroToEdit(data)
458
458
459 # For objects, try to edit the file where they are defined
459 # For objects, try to edit the file where they are defined
460 filename = find_file(data)
460 filename = find_file(data)
461 if filename:
461 if filename:
462 if 'fakemodule' in filename.lower() and \
462 if 'fakemodule' in filename.lower() and \
463 inspect.isclass(data):
463 inspect.isclass(data):
464 # class created by %edit? Try to find source
464 # class created by %edit? Try to find source
465 # by looking for method definitions instead, the
465 # by looking for method definitions instead, the
466 # __module__ in those classes is FakeModule.
466 # __module__ in those classes is FakeModule.
467 attrs = [getattr(data, aname) for aname in dir(data)]
467 attrs = [getattr(data, aname) for aname in dir(data)]
468 for attr in attrs:
468 for attr in attrs:
469 if not inspect.ismethod(attr):
469 if not inspect.ismethod(attr):
470 continue
470 continue
471 filename = find_file(attr)
471 filename = find_file(attr)
472 if filename and \
472 if filename and \
473 'fakemodule' not in filename.lower():
473 'fakemodule' not in filename.lower():
474 # change the attribute to be the edit
474 # change the attribute to be the edit
475 # target instead
475 # target instead
476 data = attr
476 data = attr
477 break
477 break
478
478
479 m = ipython_input_pat.match(os.path.basename(filename))
479 m = ipython_input_pat.match(os.path.basename(filename))
480 if m:
480 if m:
481 raise InteractivelyDefined(int(m.groups()[0]))
481 raise InteractivelyDefined(int(m.groups()[0]))
482
482
483 datafile = 1
483 datafile = 1
484 if filename is None:
484 if filename is None:
485 filename = make_filename(args)
485 filename = make_filename(args)
486 datafile = 1
486 datafile = 1
487 if filename is not None:
487 if filename is not None:
488 # only warn about this if we get a real name
488 # only warn about this if we get a real name
489 warn('Could not find file where `%s` is defined.\n'
489 warn('Could not find file where `%s` is defined.\n'
490 'Opening a file named `%s`' % (args, filename))
490 'Opening a file named `%s`' % (args, filename))
491 # Now, make sure we can actually read the source (if it was
491 # Now, make sure we can actually read the source (if it was
492 # in a temp file it's gone by now).
492 # in a temp file it's gone by now).
493 if datafile:
493 if datafile:
494 if lineno is None:
494 if lineno is None:
495 lineno = find_source_lines(data)
495 lineno = find_source_lines(data)
496 if lineno is None:
496 if lineno is None:
497 filename = make_filename(args)
497 filename = make_filename(args)
498 if filename is None:
498 if filename is None:
499 warn('The file where `%s` was defined '
499 warn('The file where `%s` was defined '
500 'cannot be read or found.' % data)
500 'cannot be read or found.' % data)
501 return (None, None, None)
501 return (None, None, None)
502 use_temp = False
502 use_temp = False
503
503
504 if use_temp:
504 if use_temp:
505 filename = shell.mktempfile(data)
505 filename = shell.mktempfile(data)
506 print('IPython will make a temporary file named:',filename)
506 print('IPython will make a temporary file named:',filename)
507
507
508 # use last_call to remember the state of the previous call, but don't
508 # use last_call to remember the state of the previous call, but don't
509 # let it be clobbered by successive '-p' calls.
509 # let it be clobbered by successive '-p' calls.
510 try:
510 try:
511 last_call[0] = shell.displayhook.prompt_count
511 last_call[0] = shell.displayhook.prompt_count
512 if not opts_prev:
512 if not opts_prev:
513 last_call[1] = args
513 last_call[1] = args
514 except:
514 except:
515 pass
515 pass
516
516
517
517
518 return filename, lineno, use_temp
518 return filename, lineno, use_temp
519
519
520 def _edit_macro(self,mname,macro):
520 def _edit_macro(self,mname,macro):
521 """open an editor with the macro data in a file"""
521 """open an editor with the macro data in a file"""
522 filename = self.shell.mktempfile(macro.value)
522 filename = self.shell.mktempfile(macro.value)
523 self.shell.hooks.editor(filename)
523 self.shell.hooks.editor(filename)
524
524
525 # and make a new macro object, to replace the old one
525 # and make a new macro object, to replace the old one
526 with open(filename) as mfile:
526 with open(filename) as mfile:
527 mvalue = mfile.read()
527 mvalue = mfile.read()
528 self.shell.user_ns[mname] = Macro(mvalue)
528 self.shell.user_ns[mname] = Macro(mvalue)
529
529
530 @skip_doctest
530 @skip_doctest
531 @line_magic
531 @line_magic
532 def edit(self, parameter_s='',last_call=['','']):
532 def edit(self, parameter_s='',last_call=['','']):
533 """Bring up an editor and execute the resulting code.
533 """Bring up an editor and execute the resulting code.
534
534
535 Usage:
535 Usage:
536 %edit [options] [args]
536 %edit [options] [args]
537
537
538 %edit runs IPython's editor hook. The default version of this hook is
538 %edit runs IPython's editor hook. The default version of this hook is
539 set to call the editor specified by your $EDITOR environment variable.
539 set to call the editor specified by your $EDITOR environment variable.
540 If this isn't found, it will default to vi under Linux/Unix and to
540 If this isn't found, it will default to vi under Linux/Unix and to
541 notepad under Windows. See the end of this docstring for how to change
541 notepad under Windows. See the end of this docstring for how to change
542 the editor hook.
542 the editor hook.
543
543
544 You can also set the value of this editor via the
544 You can also set the value of this editor via the
545 ``TerminalInteractiveShell.editor`` option in your configuration file.
545 ``TerminalInteractiveShell.editor`` option in your configuration file.
546 This is useful if you wish to use a different editor from your typical
546 This is useful if you wish to use a different editor from your typical
547 default with IPython (and for Windows users who typically don't set
547 default with IPython (and for Windows users who typically don't set
548 environment variables).
548 environment variables).
549
549
550 This command allows you to conveniently edit multi-line code right in
550 This command allows you to conveniently edit multi-line code right in
551 your IPython session.
551 your IPython session.
552
552
553 If called without arguments, %edit opens up an empty editor with a
553 If called without arguments, %edit opens up an empty editor with a
554 temporary file and will execute the contents of this file when you
554 temporary file and will execute the contents of this file when you
555 close it (don't forget to save it!).
555 close it (don't forget to save it!).
556
556
557
557
558 Options:
558 Options:
559
559
560 -n <number>: open the editor at a specified line number. By default,
560 -n <number>: open the editor at a specified line number. By default,
561 the IPython editor hook uses the unix syntax 'editor +N filename', but
561 the IPython editor hook uses the unix syntax 'editor +N filename', but
562 you can configure this by providing your own modified hook if your
562 you can configure this by providing your own modified hook if your
563 favorite editor supports line-number specifications with a different
563 favorite editor supports line-number specifications with a different
564 syntax.
564 syntax.
565
565
566 -p: this will call the editor with the same data as the previous time
566 -p: this will call the editor with the same data as the previous time
567 it was used, regardless of how long ago (in your current session) it
567 it was used, regardless of how long ago (in your current session) it
568 was.
568 was.
569
569
570 -r: use 'raw' input. This option only applies to input taken from the
570 -r: use 'raw' input. This option only applies to input taken from the
571 user's history. By default, the 'processed' history is used, so that
571 user's history. By default, the 'processed' history is used, so that
572 magics are loaded in their transformed version to valid Python. If
572 magics are loaded in their transformed version to valid Python. If
573 this option is given, the raw input as typed as the command line is
573 this option is given, the raw input as typed as the command line is
574 used instead. When you exit the editor, it will be executed by
574 used instead. When you exit the editor, it will be executed by
575 IPython's own processor.
575 IPython's own processor.
576
576
577 -x: do not execute the edited code immediately upon exit. This is
577 -x: do not execute the edited code immediately upon exit. This is
578 mainly useful if you are editing programs which need to be called with
578 mainly useful if you are editing programs which need to be called with
579 command line arguments, which you can then do using %run.
579 command line arguments, which you can then do using %run.
580
580
581
581
582 Arguments:
582 Arguments:
583
583
584 If arguments are given, the following possibilities exist:
584 If arguments are given, the following possibilities exist:
585
585
586 - If the argument is a filename, IPython will load that into the
586 - If the argument is a filename, IPython will load that into the
587 editor. It will execute its contents with execfile() when you exit,
587 editor. It will execute its contents with execfile() when you exit,
588 loading any code in the file into your interactive namespace.
588 loading any code in the file into your interactive namespace.
589
589
590 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
590 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
591 The syntax is the same as in the %history magic.
591 The syntax is the same as in the %history magic.
592
592
593 - If the argument is a string variable, its contents are loaded
593 - If the argument is a string variable, its contents are loaded
594 into the editor. You can thus edit any string which contains
594 into the editor. You can thus edit any string which contains
595 python code (including the result of previous edits).
595 python code (including the result of previous edits).
596
596
597 - If the argument is the name of an object (other than a string),
597 - If the argument is the name of an object (other than a string),
598 IPython will try to locate the file where it was defined and open the
598 IPython will try to locate the file where it was defined and open the
599 editor at the point where it is defined. You can use `%edit function`
599 editor at the point where it is defined. You can use `%edit function`
600 to load an editor exactly at the point where 'function' is defined,
600 to load an editor exactly at the point where 'function' is defined,
601 edit it and have the file be executed automatically.
601 edit it and have the file be executed automatically.
602
602
603 - If the object is a macro (see %macro for details), this opens up your
603 - If the object is a macro (see %macro for details), this opens up your
604 specified editor with a temporary file containing the macro's data.
604 specified editor with a temporary file containing the macro's data.
605 Upon exit, the macro is reloaded with the contents of the file.
605 Upon exit, the macro is reloaded with the contents of the file.
606
606
607 Note: opening at an exact line is only supported under Unix, and some
607 Note: opening at an exact line is only supported under Unix, and some
608 editors (like kedit and gedit up to Gnome 2.8) do not understand the
608 editors (like kedit and gedit up to Gnome 2.8) do not understand the
609 '+NUMBER' parameter necessary for this feature. Good editors like
609 '+NUMBER' parameter necessary for this feature. Good editors like
610 (X)Emacs, vi, jed, pico and joe all do.
610 (X)Emacs, vi, jed, pico and joe all do.
611
611
612 After executing your code, %edit will return as output the code you
612 After executing your code, %edit will return as output the code you
613 typed in the editor (except when it was an existing file). This way
613 typed in the editor (except when it was an existing file). This way
614 you can reload the code in further invocations of %edit as a variable,
614 you can reload the code in further invocations of %edit as a variable,
615 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
615 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
616 the output.
616 the output.
617
617
618 Note that %edit is also available through the alias %ed.
618 Note that %edit is also available through the alias %ed.
619
619
620 This is an example of creating a simple function inside the editor and
620 This is an example of creating a simple function inside the editor and
621 then modifying it. First, start up the editor::
621 then modifying it. First, start up the editor::
622
622
623 In [1]: edit
623 In [1]: edit
624 Editing... done. Executing edited code...
624 Editing... done. Executing edited code...
625 Out[1]: 'def foo():\\n print "foo() was defined in an editing
625 Out[1]: 'def foo():\\n print "foo() was defined in an editing
626 session"\\n'
626 session"\\n'
627
627
628 We can then call the function foo()::
628 We can then call the function foo()::
629
629
630 In [2]: foo()
630 In [2]: foo()
631 foo() was defined in an editing session
631 foo() was defined in an editing session
632
632
633 Now we edit foo. IPython automatically loads the editor with the
633 Now we edit foo. IPython automatically loads the editor with the
634 (temporary) file where foo() was previously defined::
634 (temporary) file where foo() was previously defined::
635
635
636 In [3]: edit foo
636 In [3]: edit foo
637 Editing... done. Executing edited code...
637 Editing... done. Executing edited code...
638
638
639 And if we call foo() again we get the modified version::
639 And if we call foo() again we get the modified version::
640
640
641 In [4]: foo()
641 In [4]: foo()
642 foo() has now been changed!
642 foo() has now been changed!
643
643
644 Here is an example of how to edit a code snippet successive
644 Here is an example of how to edit a code snippet successive
645 times. First we call the editor::
645 times. First we call the editor::
646
646
647 In [5]: edit
647 In [5]: edit
648 Editing... done. Executing edited code...
648 Editing... done. Executing edited code...
649 hello
649 hello
650 Out[5]: "print 'hello'\\n"
650 Out[5]: "print 'hello'\\n"
651
651
652 Now we call it again with the previous output (stored in _)::
652 Now we call it again with the previous output (stored in _)::
653
653
654 In [6]: edit _
654 In [6]: edit _
655 Editing... done. Executing edited code...
655 Editing... done. Executing edited code...
656 hello world
656 hello world
657 Out[6]: "print 'hello world'\\n"
657 Out[6]: "print 'hello world'\\n"
658
658
659 Now we call it with the output #8 (stored in _8, also as Out[8])::
659 Now we call it with the output #8 (stored in _8, also as Out[8])::
660
660
661 In [7]: edit _8
661 In [7]: edit _8
662 Editing... done. Executing edited code...
662 Editing... done. Executing edited code...
663 hello again
663 hello again
664 Out[7]: "print 'hello again'\\n"
664 Out[7]: "print 'hello again'\\n"
665
665
666
666
667 Changing the default editor hook:
667 Changing the default editor hook:
668
668
669 If you wish to write your own editor hook, you can put it in a
669 If you wish to write your own editor hook, you can put it in a
670 configuration file which you load at startup time. The default hook
670 configuration file which you load at startup time. The default hook
671 is defined in the IPython.core.hooks module, and you can use that as a
671 is defined in the IPython.core.hooks module, and you can use that as a
672 starting example for further modifications. That file also has
672 starting example for further modifications. That file also has
673 general instructions on how to set a new hook for use once you've
673 general instructions on how to set a new hook for use once you've
674 defined it."""
674 defined it."""
675 opts,args = self.parse_options(parameter_s,'prxn:')
675 opts,args = self.parse_options(parameter_s,'prxn:')
676
676
677 try:
677 try:
678 filename, lineno, is_temp = self._find_edit_target(self.shell,
678 filename, lineno, is_temp = self._find_edit_target(self.shell,
679 args, opts, last_call)
679 args, opts, last_call)
680 except MacroToEdit as e:
680 except MacroToEdit as e:
681 self._edit_macro(args, e.args[0])
681 self._edit_macro(args, e.args[0])
682 return
682 return
683 except InteractivelyDefined as e:
683 except InteractivelyDefined as e:
684 print("Editing In[%i]" % e.index)
684 print("Editing In[%i]" % e.index)
685 args = str(e.index)
685 args = str(e.index)
686 filename, lineno, is_temp = self._find_edit_target(self.shell,
686 filename, lineno, is_temp = self._find_edit_target(self.shell,
687 args, opts, last_call)
687 args, opts, last_call)
688 if filename is None:
688 if filename is None:
689 # nothing was found, warnings have already been issued,
689 # nothing was found, warnings have already been issued,
690 # just give up.
690 # just give up.
691 return
691 return
692
692
693 if is_temp:
693 if is_temp:
694 self._knowntemps.add(filename)
694 self._knowntemps.add(filename)
695 elif (filename in self._knowntemps):
695 elif (filename in self._knowntemps):
696 is_temp = True
696 is_temp = True
697
697
698
698
699 # do actual editing here
699 # do actual editing here
700 print('Editing...', end=' ')
700 print('Editing...', end=' ')
701 sys.stdout.flush()
701 sys.stdout.flush()
702 try:
702 try:
703 # Quote filenames that may have spaces in them
703 # Quote filenames that may have spaces in them
704 if ' ' in filename:
704 if ' ' in filename:
705 filename = "'%s'" % filename
705 filename = "'%s'" % filename
706 self.shell.hooks.editor(filename,lineno)
706 self.shell.hooks.editor(filename,lineno)
707 except TryNext:
707 except TryNext:
708 warn('Could not open editor')
708 warn('Could not open editor')
709 return
709 return
710
710
711 # XXX TODO: should this be generalized for all string vars?
711 # XXX TODO: should this be generalized for all string vars?
712 # For now, this is special-cased to blocks created by cpaste
712 # For now, this is special-cased to blocks created by cpaste
713 if args.strip() == 'pasted_block':
713 if args.strip() == 'pasted_block':
714 with open(filename, 'r') as f:
714 with open(filename, 'r') as f:
715 self.shell.user_ns['pasted_block'] = f.read()
715 self.shell.user_ns['pasted_block'] = f.read()
716
716
717 if 'x' in opts: # -x prevents actual execution
717 if 'x' in opts: # -x prevents actual execution
718 print()
718 print()
719 else:
719 else:
720 print('done. Executing edited code...')
720 print('done. Executing edited code...')
721 with preserve_keys(self.shell.user_ns, '__file__'):
721 with preserve_keys(self.shell.user_ns, '__file__'):
722 if not is_temp:
722 if not is_temp:
723 self.shell.user_ns['__file__'] = filename
723 self.shell.user_ns['__file__'] = filename
724 if 'r' in opts: # Untranslated IPython code
724 if 'r' in opts: # Untranslated IPython code
725 with open(filename, 'r') as f:
725 with open(filename, 'r') as f:
726 source = f.read()
726 source = f.read()
727 self.shell.run_cell(source, store_history=False)
727 self.shell.run_cell(source, store_history=False)
728 else:
728 else:
729 self.shell.safe_execfile(filename, self.shell.user_ns,
729 self.shell.safe_execfile(filename, self.shell.user_ns,
730 self.shell.user_ns)
730 self.shell.user_ns)
731
731
732 if is_temp:
732 if is_temp:
733 try:
733 try:
734 return open(filename).read()
734 return open(filename).read()
735 except IOError as msg:
735 except IOError as msg:
736 if msg.filename == filename:
736 if msg.filename == filename:
737 warn('File not found. Did you forget to save?')
737 warn('File not found. Did you forget to save?')
738 return
738 return
739 else:
739 else:
740 self.shell.showtraceback()
740 self.shell.showtraceback()
@@ -1,996 +1,996 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tools for inspecting Python objects.
2 """Tools for inspecting Python objects.
3
3
4 Uses syntax highlighting for presenting the various information elements.
4 Uses syntax highlighting for presenting the various information elements.
5
5
6 Similar in spirit to the inspect module, but all calls take a name argument to
6 Similar in spirit to the inspect module, but all calls take a name argument to
7 reference the name under which an object is being read.
7 reference the name under which an object is being read.
8 """
8 """
9
9
10 # Copyright (c) IPython Development Team.
10 # Copyright (c) IPython Development Team.
11 # Distributed under the terms of the Modified BSD License.
11 # Distributed under the terms of the Modified BSD License.
12
12
13 __all__ = ['Inspector','InspectColors']
13 __all__ = ['Inspector','InspectColors']
14
14
15 # stdlib modules
15 # stdlib modules
16 import inspect
16 import inspect
17 from inspect import signature
17 from inspect import signature
18 import linecache
18 import linecache
19 import warnings
19 import warnings
20 import os
20 import os
21 from textwrap import dedent
21 from textwrap import dedent
22 import types
22 import types
23 import io as stdlib_io
23 import io as stdlib_io
24 from itertools import zip_longest
24 from itertools import zip_longest
25
25
26 # IPython's own
26 # IPython's own
27 from IPython.core import page
27 from IPython.core import page
28 from IPython.lib.pretty import pretty
28 from IPython.lib.pretty import pretty
29 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.testing.skipdoctest import skip_doctest
30 from IPython.utils import PyColorize
30 from IPython.utils import PyColorize
31 from IPython.utils import openpy
31 from IPython.utils import openpy
32 from IPython.utils import py3compat
32 from IPython.utils import py3compat
33 from IPython.utils.dir2 import safe_hasattr
33 from IPython.utils.dir2 import safe_hasattr
34 from IPython.utils.path import compress_user
34 from IPython.utils.path import compress_user
35 from IPython.utils.text import indent
35 from IPython.utils.text import indent
36 from IPython.utils.wildcard import list_namespace
36 from IPython.utils.wildcard import list_namespace
37 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
37 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
38 from IPython.utils.py3compat import cast_unicode
38 from IPython.utils.py3compat import cast_unicode
39 from IPython.utils.colorable import Colorable
39 from IPython.utils.colorable import Colorable
40 from IPython.utils.decorators import undoc
40 from IPython.utils.decorators import undoc
41
41
42 from pygments import highlight
42 from pygments import highlight
43 from pygments.lexers import PythonLexer
43 from pygments.lexers import PythonLexer
44 from pygments.formatters import HtmlFormatter
44 from pygments.formatters import HtmlFormatter
45
45
46 def pylight(code):
46 def pylight(code):
47 return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))
47 return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))
48
48
49 # builtin docstrings to ignore
49 # builtin docstrings to ignore
50 _func_call_docstring = types.FunctionType.__call__.__doc__
50 _func_call_docstring = types.FunctionType.__call__.__doc__
51 _object_init_docstring = object.__init__.__doc__
51 _object_init_docstring = object.__init__.__doc__
52 _builtin_type_docstrings = {
52 _builtin_type_docstrings = {
53 inspect.getdoc(t) for t in (types.ModuleType, types.MethodType,
53 inspect.getdoc(t) for t in (types.ModuleType, types.MethodType,
54 types.FunctionType, property)
54 types.FunctionType, property)
55 }
55 }
56
56
57 _builtin_func_type = type(all)
57 _builtin_func_type = type(all)
58 _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions
58 _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions
59 #****************************************************************************
59 #****************************************************************************
60 # Builtin color schemes
60 # Builtin color schemes
61
61
62 Colors = TermColors # just a shorthand
62 Colors = TermColors # just a shorthand
63
63
64 InspectColors = PyColorize.ANSICodeColors
64 InspectColors = PyColorize.ANSICodeColors
65
65
66 #****************************************************************************
66 #****************************************************************************
67 # Auxiliary functions and objects
67 # Auxiliary functions and objects
68
68
69 # See the messaging spec for the definition of all these fields. This list
69 # See the messaging spec for the definition of all these fields. This list
70 # effectively defines the order of display
70 # effectively defines the order of display
71 info_fields = ['type_name', 'base_class', 'string_form', 'namespace',
71 info_fields = ['type_name', 'base_class', 'string_form', 'namespace',
72 'length', 'file', 'definition', 'docstring', 'source',
72 'length', 'file', 'definition', 'docstring', 'source',
73 'init_definition', 'class_docstring', 'init_docstring',
73 'init_definition', 'class_docstring', 'init_docstring',
74 'call_def', 'call_docstring',
74 'call_def', 'call_docstring',
75 # These won't be printed but will be used to determine how to
75 # These won't be printed but will be used to determine how to
76 # format the object
76 # format the object
77 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name'
77 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name'
78 ]
78 ]
79
79
80
80
81 def object_info(**kw):
81 def object_info(**kw):
82 """Make an object info dict with all fields present."""
82 """Make an object info dict with all fields present."""
83 infodict = dict(zip_longest(info_fields, [None]))
83 infodict = dict(zip_longest(info_fields, [None]))
84 infodict.update(kw)
84 infodict.update(kw)
85 return infodict
85 return infodict
86
86
87
87
88 def get_encoding(obj):
88 def get_encoding(obj):
89 """Get encoding for python source file defining obj
89 """Get encoding for python source file defining obj
90
90
91 Returns None if obj is not defined in a sourcefile.
91 Returns None if obj is not defined in a sourcefile.
92 """
92 """
93 ofile = find_file(obj)
93 ofile = find_file(obj)
94 # run contents of file through pager starting at line where the object
94 # run contents of file through pager starting at line where the object
95 # is defined, as long as the file isn't binary and is actually on the
95 # is defined, as long as the file isn't binary and is actually on the
96 # filesystem.
96 # filesystem.
97 if ofile is None:
97 if ofile is None:
98 return None
98 return None
99 elif ofile.endswith(('.so', '.dll', '.pyd')):
99 elif ofile.endswith(('.so', '.dll', '.pyd')):
100 return None
100 return None
101 elif not os.path.isfile(ofile):
101 elif not os.path.isfile(ofile):
102 return None
102 return None
103 else:
103 else:
104 # Print only text files, not extension binaries. Note that
104 # Print only text files, not extension binaries. Note that
105 # getsourcelines returns lineno with 1-offset and page() uses
105 # getsourcelines returns lineno with 1-offset and page() uses
106 # 0-offset, so we must adjust.
106 # 0-offset, so we must adjust.
107 with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2
107 with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2
108 encoding, lines = openpy.detect_encoding(buffer.readline)
108 encoding, lines = openpy.detect_encoding(buffer.readline)
109 return encoding
109 return encoding
110
110
111 def getdoc(obj):
111 def getdoc(obj):
112 """Stable wrapper around inspect.getdoc.
112 """Stable wrapper around inspect.getdoc.
113
113
114 This can't crash because of attribute problems.
114 This can't crash because of attribute problems.
115
115
116 It also attempts to call a getdoc() method on the given object. This
116 It also attempts to call a getdoc() method on the given object. This
117 allows objects which provide their docstrings via non-standard mechanisms
117 allows objects which provide their docstrings via non-standard mechanisms
118 (like Pyro proxies) to still be inspected by ipython's ? system.
118 (like Pyro proxies) to still be inspected by ipython's ? system.
119 """
119 """
120 # Allow objects to offer customized documentation via a getdoc method:
120 # Allow objects to offer customized documentation via a getdoc method:
121 try:
121 try:
122 ds = obj.getdoc()
122 ds = obj.getdoc()
123 except Exception:
123 except Exception:
124 pass
124 pass
125 else:
125 else:
126 if isinstance(ds, str):
126 if isinstance(ds, str):
127 return inspect.cleandoc(ds)
127 return inspect.cleandoc(ds)
128 docstr = inspect.getdoc(obj)
128 docstr = inspect.getdoc(obj)
129 encoding = get_encoding(obj)
129 encoding = get_encoding(obj)
130 return py3compat.cast_unicode(docstr, encoding=encoding)
130 return py3compat.cast_unicode(docstr, encoding=encoding)
131
131
132
132
133 def getsource(obj, oname=''):
133 def getsource(obj, oname=''):
134 """Wrapper around inspect.getsource.
134 """Wrapper around inspect.getsource.
135
135
136 This can be modified by other projects to provide customized source
136 This can be modified by other projects to provide customized source
137 extraction.
137 extraction.
138
138
139 Parameters
139 Parameters
140 ----------
140 ----------
141 obj : object
141 obj : object
142 an object whose source code we will attempt to extract
142 an object whose source code we will attempt to extract
143 oname : str
143 oname : str
144 (optional) a name under which the object is known
144 (optional) a name under which the object is known
145
145
146 Returns
146 Returns
147 -------
147 -------
148 src : unicode or None
148 src : unicode or None
149
149
150 """
150 """
151
151
152 if isinstance(obj, property):
152 if isinstance(obj, property):
153 sources = []
153 sources = []
154 for attrname in ['fget', 'fset', 'fdel']:
154 for attrname in ['fget', 'fset', 'fdel']:
155 fn = getattr(obj, attrname)
155 fn = getattr(obj, attrname)
156 if fn is not None:
156 if fn is not None:
157 encoding = get_encoding(fn)
157 encoding = get_encoding(fn)
158 oname_prefix = ('%s.' % oname) if oname else ''
158 oname_prefix = ('%s.' % oname) if oname else ''
159 sources.append(cast_unicode(
159 sources.append(cast_unicode(
160 ''.join(('# ', oname_prefix, attrname)),
160 ''.join(('# ', oname_prefix, attrname)),
161 encoding=encoding))
161 encoding=encoding))
162 if inspect.isfunction(fn):
162 if inspect.isfunction(fn):
163 sources.append(dedent(getsource(fn)))
163 sources.append(dedent(getsource(fn)))
164 else:
164 else:
165 # Default str/repr only prints function name,
165 # Default str/repr only prints function name,
166 # pretty.pretty prints module name too.
166 # pretty.pretty prints module name too.
167 sources.append(cast_unicode(
167 sources.append(cast_unicode(
168 '%s%s = %s\n' % (
168 '%s%s = %s\n' % (
169 oname_prefix, attrname, pretty(fn)),
169 oname_prefix, attrname, pretty(fn)),
170 encoding=encoding))
170 encoding=encoding))
171 if sources:
171 if sources:
172 return '\n'.join(sources)
172 return '\n'.join(sources)
173 else:
173 else:
174 return None
174 return None
175
175
176 else:
176 else:
177 # Get source for non-property objects.
177 # Get source for non-property objects.
178
178
179 obj = _get_wrapped(obj)
179 obj = _get_wrapped(obj)
180
180
181 try:
181 try:
182 src = inspect.getsource(obj)
182 src = inspect.getsource(obj)
183 except TypeError:
183 except TypeError:
184 # The object itself provided no meaningful source, try looking for
184 # The object itself provided no meaningful source, try looking for
185 # its class definition instead.
185 # its class definition instead.
186 if hasattr(obj, '__class__'):
186 if hasattr(obj, '__class__'):
187 try:
187 try:
188 src = inspect.getsource(obj.__class__)
188 src = inspect.getsource(obj.__class__)
189 except TypeError:
189 except TypeError:
190 return None
190 return None
191
191
192 encoding = get_encoding(obj)
192 encoding = get_encoding(obj)
193 return cast_unicode(src, encoding=encoding)
193 return cast_unicode(src, encoding=encoding)
194
194
195
195
196 def is_simple_callable(obj):
196 def is_simple_callable(obj):
197 """True if obj is a function ()"""
197 """True if obj is a function ()"""
198 return (inspect.isfunction(obj) or inspect.ismethod(obj) or \
198 return (inspect.isfunction(obj) or inspect.ismethod(obj) or \
199 isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type))
199 isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type))
200
200
201
201
202 def getargspec(obj):
202 def getargspec(obj):
203 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
203 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
204 :func:inspect.getargspec` on Python 2.
204 :func:inspect.getargspec` on Python 2.
205
205
206 In addition to functions and methods, this can also handle objects with a
206 In addition to functions and methods, this can also handle objects with a
207 ``__call__`` attribute.
207 ``__call__`` attribute.
208 """
208 """
209 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
209 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
210 obj = obj.__call__
210 obj = obj.__call__
211
211
212 return inspect.getfullargspec(obj)
212 return inspect.getfullargspec(obj)
213
213
214
214
215 def format_argspec(argspec):
215 def format_argspec(argspec):
216 """Format argspect, convenience wrapper around inspect's.
216 """Format argspect, convenience wrapper around inspect's.
217
217
218 This takes a dict instead of ordered arguments and calls
218 This takes a dict instead of ordered arguments and calls
219 inspect.format_argspec with the arguments in the necessary order.
219 inspect.format_argspec with the arguments in the necessary order.
220 """
220 """
221 return inspect.formatargspec(argspec['args'], argspec['varargs'],
221 return inspect.formatargspec(argspec['args'], argspec['varargs'],
222 argspec['varkw'], argspec['defaults'])
222 argspec['varkw'], argspec['defaults'])
223
223
224 @undoc
224 @undoc
225 def call_tip(oinfo, format_call=True):
225 def call_tip(oinfo, format_call=True):
226 """DEPRECATED. Extract call tip data from an oinfo dict.
226 """DEPRECATED. Extract call tip data from an oinfo dict.
227 """
227 """
228 warnings.warn('`call_tip` function is deprecated as of IPython 6.0'
228 warnings.warn('`call_tip` function is deprecated as of IPython 6.0'
229 'and will be removed in future versions.', DeprecationWarning, stacklevel=2)
229 'and will be removed in future versions.', DeprecationWarning, stacklevel=2)
230 # Get call definition
230 # Get call definition
231 argspec = oinfo.get('argspec')
231 argspec = oinfo.get('argspec')
232 if argspec is None:
232 if argspec is None:
233 call_line = None
233 call_line = None
234 else:
234 else:
235 # Callable objects will have 'self' as their first argument, prune
235 # Callable objects will have 'self' as their first argument, prune
236 # it out if it's there for clarity (since users do *not* pass an
236 # it out if it's there for clarity (since users do *not* pass an
237 # extra first argument explicitly).
237 # extra first argument explicitly).
238 try:
238 try:
239 has_self = argspec['args'][0] == 'self'
239 has_self = argspec['args'][0] == 'self'
240 except (KeyError, IndexError):
240 except (KeyError, IndexError):
241 pass
241 pass
242 else:
242 else:
243 if has_self:
243 if has_self:
244 argspec['args'] = argspec['args'][1:]
244 argspec['args'] = argspec['args'][1:]
245
245
246 call_line = oinfo['name']+format_argspec(argspec)
246 call_line = oinfo['name']+format_argspec(argspec)
247
247
248 # Now get docstring.
248 # Now get docstring.
249 # The priority is: call docstring, constructor docstring, main one.
249 # The priority is: call docstring, constructor docstring, main one.
250 doc = oinfo.get('call_docstring')
250 doc = oinfo.get('call_docstring')
251 if doc is None:
251 if doc is None:
252 doc = oinfo.get('init_docstring')
252 doc = oinfo.get('init_docstring')
253 if doc is None:
253 if doc is None:
254 doc = oinfo.get('docstring','')
254 doc = oinfo.get('docstring','')
255
255
256 return call_line, doc
256 return call_line, doc
257
257
258
258
259 def _get_wrapped(obj):
259 def _get_wrapped(obj):
260 """Get the original object if wrapped in one or more @decorators
260 """Get the original object if wrapped in one or more @decorators
261
261
262 Some objects automatically construct similar objects on any unrecognised
262 Some objects automatically construct similar objects on any unrecognised
263 attribute access (e.g. unittest.mock.call). To protect against infinite loops,
263 attribute access (e.g. unittest.mock.call). To protect against infinite loops,
264 this will arbitrarily cut off after 100 levels of obj.__wrapped__
264 this will arbitrarily cut off after 100 levels of obj.__wrapped__
265 attribute access. --TK, Jan 2016
265 attribute access. --TK, Jan 2016
266 """
266 """
267 orig_obj = obj
267 orig_obj = obj
268 i = 0
268 i = 0
269 while safe_hasattr(obj, '__wrapped__'):
269 while safe_hasattr(obj, '__wrapped__'):
270 obj = obj.__wrapped__
270 obj = obj.__wrapped__
271 i += 1
271 i += 1
272 if i > 100:
272 if i > 100:
273 # __wrapped__ is probably a lie, so return the thing we started with
273 # __wrapped__ is probably a lie, so return the thing we started with
274 return orig_obj
274 return orig_obj
275 return obj
275 return obj
276
276
277 def find_file(obj):
277 def find_file(obj):
278 """Find the absolute path to the file where an object was defined.
278 """Find the absolute path to the file where an object was defined.
279
279
280 This is essentially a robust wrapper around `inspect.getabsfile`.
280 This is essentially a robust wrapper around `inspect.getabsfile`.
281
281
282 Returns None if no file can be found.
282 Returns None if no file can be found.
283
283
284 Parameters
284 Parameters
285 ----------
285 ----------
286 obj : any Python object
286 obj : any Python object
287
287
288 Returns
288 Returns
289 -------
289 -------
290 fname : str
290 fname : str
291 The absolute path to the file where the object was defined.
291 The absolute path to the file where the object was defined.
292 """
292 """
293 obj = _get_wrapped(obj)
293 obj = _get_wrapped(obj)
294
294
295 fname = None
295 fname = None
296 try:
296 try:
297 fname = inspect.getabsfile(obj)
297 fname = inspect.getabsfile(obj)
298 except TypeError:
298 except TypeError:
299 # For an instance, the file that matters is where its class was
299 # For an instance, the file that matters is where its class was
300 # declared.
300 # declared.
301 if hasattr(obj, '__class__'):
301 if hasattr(obj, '__class__'):
302 try:
302 try:
303 fname = inspect.getabsfile(obj.__class__)
303 fname = inspect.getabsfile(obj.__class__)
304 except TypeError:
304 except TypeError:
305 # Can happen for builtins
305 # Can happen for builtins
306 pass
306 pass
307 except:
307 except:
308 pass
308 pass
309 return cast_unicode(fname)
309 return cast_unicode(fname)
310
310
311
311
312 def find_source_lines(obj):
312 def find_source_lines(obj):
313 """Find the line number in a file where an object was defined.
313 """Find the line number in a file where an object was defined.
314
314
315 This is essentially a robust wrapper around `inspect.getsourcelines`.
315 This is essentially a robust wrapper around `inspect.getsourcelines`.
316
316
317 Returns None if no file can be found.
317 Returns None if no file can be found.
318
318
319 Parameters
319 Parameters
320 ----------
320 ----------
321 obj : any Python object
321 obj : any Python object
322
322
323 Returns
323 Returns
324 -------
324 -------
325 lineno : int
325 lineno : int
326 The line number where the object definition starts.
326 The line number where the object definition starts.
327 """
327 """
328 obj = _get_wrapped(obj)
328 obj = _get_wrapped(obj)
329
329
330 try:
330 try:
331 try:
331 try:
332 lineno = inspect.getsourcelines(obj)[1]
332 lineno = inspect.getsourcelines(obj)[1]
333 except TypeError:
333 except TypeError:
334 # For instances, try the class object like getsource() does
334 # For instances, try the class object like getsource() does
335 if hasattr(obj, '__class__'):
335 if hasattr(obj, '__class__'):
336 lineno = inspect.getsourcelines(obj.__class__)[1]
336 lineno = inspect.getsourcelines(obj.__class__)[1]
337 else:
337 else:
338 lineno = None
338 lineno = None
339 except:
339 except:
340 return None
340 return None
341
341
342 return lineno
342 return lineno
343
343
344 class Inspector(Colorable):
344 class Inspector(Colorable):
345
345
346 def __init__(self, color_table=InspectColors,
346 def __init__(self, color_table=InspectColors,
347 code_color_table=PyColorize.ANSICodeColors,
347 code_color_table=PyColorize.ANSICodeColors,
348 scheme=None,
348 scheme=None,
349 str_detail_level=0,
349 str_detail_level=0,
350 parent=None, config=None):
350 parent=None, config=None):
351 super(Inspector, self).__init__(parent=parent, config=config)
351 super(Inspector, self).__init__(parent=parent, config=config)
352 self.color_table = color_table
352 self.color_table = color_table
353 self.parser = PyColorize.Parser(out='str', parent=self, style=scheme)
353 self.parser = PyColorize.Parser(out='str', parent=self, style=scheme)
354 self.format = self.parser.format
354 self.format = self.parser.format
355 self.str_detail_level = str_detail_level
355 self.str_detail_level = str_detail_level
356 self.set_active_scheme(scheme)
356 self.set_active_scheme(scheme)
357
357
358 def _getdef(self,obj,oname=''):
358 def _getdef(self,obj,oname=''):
359 """Return the call signature for any callable object.
359 """Return the call signature for any callable object.
360
360
361 If any exception is generated, None is returned instead and the
361 If any exception is generated, None is returned instead and the
362 exception is suppressed."""
362 exception is suppressed."""
363 try:
363 try:
364 hdef = oname + str(signature(obj))
364 hdef = oname + str(signature(obj))
365 return cast_unicode(hdef)
365 return cast_unicode(hdef)
366 except:
366 except:
367 return None
367 return None
368
368
369 def __head(self,h):
369 def __head(self,h):
370 """Return a header string with proper colors."""
370 """Return a header string with proper colors."""
371 return '%s%s%s' % (self.color_table.active_colors.header,h,
371 return '%s%s%s' % (self.color_table.active_colors.header,h,
372 self.color_table.active_colors.normal)
372 self.color_table.active_colors.normal)
373
373
374 def set_active_scheme(self, scheme):
374 def set_active_scheme(self, scheme):
375 if scheme is not None:
375 if scheme is not None:
376 self.color_table.set_active_scheme(scheme)
376 self.color_table.set_active_scheme(scheme)
377 self.parser.color_table.set_active_scheme(scheme)
377 self.parser.color_table.set_active_scheme(scheme)
378
378
379 def noinfo(self, msg, oname):
379 def noinfo(self, msg, oname):
380 """Generic message when no information is found."""
380 """Generic message when no information is found."""
381 print('No %s found' % msg, end=' ')
381 print('No %s found' % msg, end=' ')
382 if oname:
382 if oname:
383 print('for %s' % oname)
383 print('for %s' % oname)
384 else:
384 else:
385 print()
385 print()
386
386
387 def pdef(self, obj, oname=''):
387 def pdef(self, obj, oname=''):
388 """Print the call signature for any callable object.
388 """Print the call signature for any callable object.
389
389
390 If the object is a class, print the constructor information."""
390 If the object is a class, print the constructor information."""
391
391
392 if not callable(obj):
392 if not callable(obj):
393 print('Object is not callable.')
393 print('Object is not callable.')
394 return
394 return
395
395
396 header = ''
396 header = ''
397
397
398 if inspect.isclass(obj):
398 if inspect.isclass(obj):
399 header = self.__head('Class constructor information:\n')
399 header = self.__head('Class constructor information:\n')
400
400
401
401
402 output = self._getdef(obj,oname)
402 output = self._getdef(obj,oname)
403 if output is None:
403 if output is None:
404 self.noinfo('definition header',oname)
404 self.noinfo('definition header',oname)
405 else:
405 else:
406 print(header,self.format(output), end=' ')
406 print(header,self.format(output), end=' ')
407
407
408 # In Python 3, all classes are new-style, so they all have __init__.
408 # In Python 3, all classes are new-style, so they all have __init__.
409 @skip_doctest
409 @skip_doctest
410 def pdoc(self, obj, oname='', formatter=None):
410 def pdoc(self, obj, oname='', formatter=None):
411 """Print the docstring for any object.
411 """Print the docstring for any object.
412
412
413 Optional:
413 Optional:
414 -formatter: a function to run the docstring through for specially
414 -formatter: a function to run the docstring through for specially
415 formatted docstrings.
415 formatted docstrings.
416
416
417 Examples
417 Examples
418 --------
418 --------
419
419
420 In [1]: class NoInit:
420 In [1]: class NoInit:
421 ...: pass
421 ...: pass
422
422
423 In [2]: class NoDoc:
423 In [2]: class NoDoc:
424 ...: def __init__(self):
424 ...: def __init__(self):
425 ...: pass
425 ...: pass
426
426
427 In [3]: %pdoc NoDoc
427 In [3]: %pdoc NoDoc
428 No documentation found for NoDoc
428 No documentation found for NoDoc
429
429
430 In [4]: %pdoc NoInit
430 In [4]: %pdoc NoInit
431 No documentation found for NoInit
431 No documentation found for NoInit
432
432
433 In [5]: obj = NoInit()
433 In [5]: obj = NoInit()
434
434
435 In [6]: %pdoc obj
435 In [6]: %pdoc obj
436 No documentation found for obj
436 No documentation found for obj
437
437
438 In [5]: obj2 = NoDoc()
438 In [5]: obj2 = NoDoc()
439
439
440 In [6]: %pdoc obj2
440 In [6]: %pdoc obj2
441 No documentation found for obj2
441 No documentation found for obj2
442 """
442 """
443
443
444 head = self.__head # For convenience
444 head = self.__head # For convenience
445 lines = []
445 lines = []
446 ds = getdoc(obj)
446 ds = getdoc(obj)
447 if formatter:
447 if formatter:
448 ds = formatter(ds).get('plain/text', ds)
448 ds = formatter(ds).get('plain/text', ds)
449 if ds:
449 if ds:
450 lines.append(head("Class docstring:"))
450 lines.append(head("Class docstring:"))
451 lines.append(indent(ds))
451 lines.append(indent(ds))
452 if inspect.isclass(obj) and hasattr(obj, '__init__'):
452 if inspect.isclass(obj) and hasattr(obj, '__init__'):
453 init_ds = getdoc(obj.__init__)
453 init_ds = getdoc(obj.__init__)
454 if init_ds is not None:
454 if init_ds is not None:
455 lines.append(head("Init docstring:"))
455 lines.append(head("Init docstring:"))
456 lines.append(indent(init_ds))
456 lines.append(indent(init_ds))
457 elif hasattr(obj,'__call__'):
457 elif hasattr(obj,'__call__'):
458 call_ds = getdoc(obj.__call__)
458 call_ds = getdoc(obj.__call__)
459 if call_ds:
459 if call_ds:
460 lines.append(head("Call docstring:"))
460 lines.append(head("Call docstring:"))
461 lines.append(indent(call_ds))
461 lines.append(indent(call_ds))
462
462
463 if not lines:
463 if not lines:
464 self.noinfo('documentation',oname)
464 self.noinfo('documentation',oname)
465 else:
465 else:
466 page.page('\n'.join(lines))
466 page.page('\n'.join(lines))
467
467
468 def psource(self, obj, oname=''):
468 def psource(self, obj, oname=''):
469 """Print the source code for an object."""
469 """Print the source code for an object."""
470
470
471 # Flush the source cache because inspect can return out-of-date source
471 # Flush the source cache because inspect can return out-of-date source
472 linecache.checkcache()
472 linecache.checkcache()
473 try:
473 try:
474 src = getsource(obj, oname=oname)
474 src = getsource(obj, oname=oname)
475 except Exception:
475 except Exception:
476 src = None
476 src = None
477
477
478 if src is None:
478 if src is None:
479 self.noinfo('source', oname)
479 self.noinfo('source', oname)
480 else:
480 else:
481 page.page(self.format(src))
481 page.page(self.format(src))
482
482
483 def pfile(self, obj, oname=''):
483 def pfile(self, obj, oname=''):
484 """Show the whole file where an object was defined."""
484 """Show the whole file where an object was defined."""
485
485
486 lineno = find_source_lines(obj)
486 lineno = find_source_lines(obj)
487 if lineno is None:
487 if lineno is None:
488 self.noinfo('file', oname)
488 self.noinfo('file', oname)
489 return
489 return
490
490
491 ofile = find_file(obj)
491 ofile = find_file(obj)
492 # run contents of file through pager starting at line where the object
492 # run contents of file through pager starting at line where the object
493 # is defined, as long as the file isn't binary and is actually on the
493 # is defined, as long as the file isn't binary and is actually on the
494 # filesystem.
494 # filesystem.
495 if ofile.endswith(('.so', '.dll', '.pyd')):
495 if ofile.endswith(('.so', '.dll', '.pyd')):
496 print('File %r is binary, not printing.' % ofile)
496 print('File %r is binary, not printing.' % ofile)
497 elif not os.path.isfile(ofile):
497 elif not os.path.isfile(ofile):
498 print('File %r does not exist, not printing.' % ofile)
498 print('File %r does not exist, not printing.' % ofile)
499 else:
499 else:
500 # Print only text files, not extension binaries. Note that
500 # Print only text files, not extension binaries. Note that
501 # getsourcelines returns lineno with 1-offset and page() uses
501 # getsourcelines returns lineno with 1-offset and page() uses
502 # 0-offset, so we must adjust.
502 # 0-offset, so we must adjust.
503 page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
503 page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
504
504
505 def _format_fields(self, fields, title_width=0):
505 def _format_fields(self, fields, title_width=0):
506 """Formats a list of fields for display.
506 """Formats a list of fields for display.
507
507
508 Parameters
508 Parameters
509 ----------
509 ----------
510 fields : list
510 fields : list
511 A list of 2-tuples: (field_title, field_content)
511 A list of 2-tuples: (field_title, field_content)
512 title_width : int
512 title_width : int
513 How many characters to pad titles to. Default to longest title.
513 How many characters to pad titles to. Default to longest title.
514 """
514 """
515 out = []
515 out = []
516 header = self.__head
516 header = self.__head
517 if title_width == 0:
517 if title_width == 0:
518 title_width = max(len(title) + 2 for title, _ in fields)
518 title_width = max(len(title) + 2 for title, _ in fields)
519 for title, content in fields:
519 for title, content in fields:
520 if len(content.splitlines()) > 1:
520 if len(content.splitlines()) > 1:
521 title = header(title + ':') + '\n'
521 title = header(title + ':') + '\n'
522 else:
522 else:
523 title = header((title + ':').ljust(title_width))
523 title = header((title + ':').ljust(title_width))
524 out.append(cast_unicode(title) + cast_unicode(content))
524 out.append(cast_unicode(title) + cast_unicode(content))
525 return "\n".join(out)
525 return "\n".join(out)
526
526
527 def _mime_format(self, text, formatter=None):
527 def _mime_format(self, text, formatter=None):
528 """Return a mime bundle representation of the input text.
528 """Return a mime bundle representation of the input text.
529
529
530 - if `formatter` is None, the returned mime bundle has
530 - if `formatter` is None, the returned mime bundle has
531 a `text/plain` field, with the input text.
531 a `text/plain` field, with the input text.
532 a `text/html` field with a `<pre>` tag containing the input text.
532 a `text/html` field with a `<pre>` tag containing the input text.
533
533
534 - if `formatter` is not None, it must be a callable transforming the
534 - if `formatter` is not None, it must be a callable transforming the
535 input text into a mime bundle. Default values for `text/plain` and
535 input text into a mime bundle. Default values for `text/plain` and
536 `text/html` representations are the ones described above.
536 `text/html` representations are the ones described above.
537
537
538 Note:
538 Note:
539
539
540 Formatters returning strings are supported but this behavior is deprecated.
540 Formatters returning strings are supported but this behavior is deprecated.
541
541
542 """
542 """
543 text = cast_unicode(text)
543 text = cast_unicode(text)
544 defaults = {
544 defaults = {
545 'text/plain': text,
545 'text/plain': text,
546 'text/html': '<pre>' + text + '</pre>'
546 'text/html': '<pre>' + text + '</pre>'
547 }
547 }
548
548
549 if formatter is None:
549 if formatter is None:
550 return defaults
550 return defaults
551 else:
551 else:
552 formatted = formatter(text)
552 formatted = formatter(text)
553
553
554 if not isinstance(formatted, dict):
554 if not isinstance(formatted, dict):
555 # Handle the deprecated behavior of a formatter returning
555 # Handle the deprecated behavior of a formatter returning
556 # a string instead of a mime bundle.
556 # a string instead of a mime bundle.
557 return {
557 return {
558 'text/plain': formatted,
558 'text/plain': formatted,
559 'text/html': '<pre>' + formatted + '</pre>'
559 'text/html': '<pre>' + formatted + '</pre>'
560 }
560 }
561
561
562 else:
562 else:
563 return dict(defaults, **formatted)
563 return dict(defaults, **formatted)
564
564
565
565
566 def format_mime(self, bundle):
566 def format_mime(self, bundle):
567
567
568 text_plain = bundle['text/plain']
568 text_plain = bundle['text/plain']
569
569
570 text = ''
570 text = ''
571 heads, bodies = list(zip(*text_plain))
571 heads, bodies = list(zip(*text_plain))
572 _len = max(len(h) for h in heads)
572 _len = max(len(h) for h in heads)
573
573
574 for head, body in zip(heads, bodies):
574 for head, body in zip(heads, bodies):
575 body = body.strip('\n')
575 body = body.strip('\n')
576 delim = '\n' if '\n' in body else ' '
576 delim = '\n' if '\n' in body else ' '
577 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
577 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
578
578
579 bundle['text/plain'] = text
579 bundle['text/plain'] = text
580 return bundle
580 return bundle
581
581
582 def _get_info(self, obj, oname='', formatter=None, info=None, detail_level=0):
582 def _get_info(self, obj, oname='', formatter=None, info=None, detail_level=0):
583 """Retrieve an info dict and format it.
583 """Retrieve an info dict and format it.
584
584
585 Parameters
585 Parameters
586 ==========
586 ==========
587
587
588 obj: any
588 obj: any
589 Object to inspect and return info from
589 Object to inspect and return info from
590 oname: str (default: ''):
590 oname: str (default: ''):
591 Name of the variable pointing to `obj`.
591 Name of the variable pointing to `obj`.
592 formatter: callable
592 formatter: callable
593 info:
593 info:
594 already computed informations
594 already computed information
595 detail_level: integer
595 detail_level: integer
596 Granularity of detail level, if set to 1, give more informations.
596 Granularity of detail level, if set to 1, give more information.
597 """
597 """
598
598
599 info = self._info(obj, oname=oname, info=info, detail_level=detail_level)
599 info = self._info(obj, oname=oname, info=info, detail_level=detail_level)
600
600
601 _mime = {
601 _mime = {
602 'text/plain': [],
602 'text/plain': [],
603 'text/html': '',
603 'text/html': '',
604 }
604 }
605
605
606 def append_field(bundle, title, key, formatter=None):
606 def append_field(bundle, title, key, formatter=None):
607 field = info[key]
607 field = info[key]
608 if field is not None:
608 if field is not None:
609 formatted_field = self._mime_format(field, formatter)
609 formatted_field = self._mime_format(field, formatter)
610 bundle['text/plain'].append((title, formatted_field['text/plain']))
610 bundle['text/plain'].append((title, formatted_field['text/plain']))
611 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n'
611 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n'
612
612
613 def code_formatter(text):
613 def code_formatter(text):
614 return {
614 return {
615 'text/plain': self.format(text),
615 'text/plain': self.format(text),
616 'text/html': pylight(text)
616 'text/html': pylight(text)
617 }
617 }
618
618
619 if info['isalias']:
619 if info['isalias']:
620 append_field(_mime, 'Repr', 'string_form')
620 append_field(_mime, 'Repr', 'string_form')
621
621
622 elif info['ismagic']:
622 elif info['ismagic']:
623 if detail_level > 0:
623 if detail_level > 0:
624 append_field(_mime, 'Source', 'source', code_formatter)
624 append_field(_mime, 'Source', 'source', code_formatter)
625 else:
625 else:
626 append_field(_mime, 'Docstring', 'docstring', formatter)
626 append_field(_mime, 'Docstring', 'docstring', formatter)
627 append_field(_mime, 'File', 'file')
627 append_field(_mime, 'File', 'file')
628
628
629 elif info['isclass'] or is_simple_callable(obj):
629 elif info['isclass'] or is_simple_callable(obj):
630 # Functions, methods, classes
630 # Functions, methods, classes
631 append_field(_mime, 'Signature', 'definition', code_formatter)
631 append_field(_mime, 'Signature', 'definition', code_formatter)
632 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
632 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
633 if detail_level > 0 and info['source']:
633 if detail_level > 0 and info['source']:
634 append_field(_mime, 'Source', 'source', code_formatter)
634 append_field(_mime, 'Source', 'source', code_formatter)
635 else:
635 else:
636 append_field(_mime, 'Docstring', 'docstring', formatter)
636 append_field(_mime, 'Docstring', 'docstring', formatter)
637 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
637 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
638
638
639 append_field(_mime, 'File', 'file')
639 append_field(_mime, 'File', 'file')
640 append_field(_mime, 'Type', 'type_name')
640 append_field(_mime, 'Type', 'type_name')
641
641
642 else:
642 else:
643 # General Python objects
643 # General Python objects
644 append_field(_mime, 'Signature', 'definition', code_formatter)
644 append_field(_mime, 'Signature', 'definition', code_formatter)
645 append_field(_mime, 'Call signature', 'call_def', code_formatter)
645 append_field(_mime, 'Call signature', 'call_def', code_formatter)
646 append_field(_mime, 'Type', 'type_name')
646 append_field(_mime, 'Type', 'type_name')
647 append_field(_mime, 'String form', 'string_form')
647 append_field(_mime, 'String form', 'string_form')
648
648
649 # Namespace
649 # Namespace
650 if info['namespace'] != 'Interactive':
650 if info['namespace'] != 'Interactive':
651 append_field(_mime, 'Namespace', 'namespace')
651 append_field(_mime, 'Namespace', 'namespace')
652
652
653 append_field(_mime, 'Length', 'length')
653 append_field(_mime, 'Length', 'length')
654 append_field(_mime, 'File', 'file')
654 append_field(_mime, 'File', 'file')
655
655
656 # Source or docstring, depending on detail level and whether
656 # Source or docstring, depending on detail level and whether
657 # source found.
657 # source found.
658 if detail_level > 0 and info['source']:
658 if detail_level > 0 and info['source']:
659 append_field(_mime, 'Source', 'source', code_formatter)
659 append_field(_mime, 'Source', 'source', code_formatter)
660 else:
660 else:
661 append_field(_mime, 'Docstring', 'docstring', formatter)
661 append_field(_mime, 'Docstring', 'docstring', formatter)
662
662
663 append_field(_mime, 'Class docstring', 'class_docstring', formatter)
663 append_field(_mime, 'Class docstring', 'class_docstring', formatter)
664 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
664 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
665 append_field(_mime, 'Call docstring', 'call_docstring', formatter)
665 append_field(_mime, 'Call docstring', 'call_docstring', formatter)
666
666
667
667
668 return self.format_mime(_mime)
668 return self.format_mime(_mime)
669
669
670 def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True):
670 def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True):
671 """Show detailed information about an object.
671 """Show detailed information about an object.
672
672
673 Optional arguments:
673 Optional arguments:
674
674
675 - oname: name of the variable pointing to the object.
675 - oname: name of the variable pointing to the object.
676
676
677 - formatter: callable (optional)
677 - formatter: callable (optional)
678 A special formatter for docstrings.
678 A special formatter for docstrings.
679
679
680 The formatter is a callable that takes a string as an input
680 The formatter is a callable that takes a string as an input
681 and returns either a formatted string or a mime type bundle
681 and returns either a formatted string or a mime type bundle
682 in the form of a dictionnary.
682 in the form of a dictionary.
683
683
684 Although the support of custom formatter returning a string
684 Although the support of custom formatter returning a string
685 instead of a mime type bundle is deprecated.
685 instead of a mime type bundle is deprecated.
686
686
687 - info: a structure with some information fields which may have been
687 - info: a structure with some information fields which may have been
688 precomputed already.
688 precomputed already.
689
689
690 - detail_level: if set to 1, more information is given.
690 - detail_level: if set to 1, more information is given.
691 """
691 """
692 info = self._get_info(obj, oname, formatter, info, detail_level)
692 info = self._get_info(obj, oname, formatter, info, detail_level)
693 if not enable_html_pager:
693 if not enable_html_pager:
694 del info['text/html']
694 del info['text/html']
695 page.page(info)
695 page.page(info)
696
696
697 def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
697 def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
698 """DEPRECATED. Compute a dict with detailed information about an object.
698 """DEPRECATED. Compute a dict with detailed information about an object.
699 """
699 """
700 if formatter is not None:
700 if formatter is not None:
701 warnings.warn('The `formatter` keyword argument to `Inspector.info`'
701 warnings.warn('The `formatter` keyword argument to `Inspector.info`'
702 'is deprecated as of IPython 5.0 and will have no effects.',
702 'is deprecated as of IPython 5.0 and will have no effects.',
703 DeprecationWarning, stacklevel=2)
703 DeprecationWarning, stacklevel=2)
704 return self._info(obj, oname=oname, info=info, detail_level=detail_level)
704 return self._info(obj, oname=oname, info=info, detail_level=detail_level)
705
705
706 def _info(self, obj, oname='', info=None, detail_level=0) -> dict:
706 def _info(self, obj, oname='', info=None, detail_level=0) -> dict:
707 """Compute a dict with detailed information about an object.
707 """Compute a dict with detailed information about an object.
708
708
709 Parameters
709 Parameters
710 ==========
710 ==========
711
711
712 obj: any
712 obj: any
713 An object to find information about
713 An object to find information about
714 oname: str (default: ''):
714 oname: str (default: ''):
715 Name of the variable pointing to `obj`.
715 Name of the variable pointing to `obj`.
716 info: (default: None)
716 info: (default: None)
717 A struct (dict like with attr access) with some information fields
717 A struct (dict like with attr access) with some information fields
718 which may have been precomputed already.
718 which may have been precomputed already.
719 detail_level: int (default:0)
719 detail_level: int (default:0)
720 If set to 1, more information is given.
720 If set to 1, more information is given.
721
721
722 Returns
722 Returns
723 =======
723 =======
724
724
725 An object info dict with known fields from `info_fields`.
725 An object info dict with known fields from `info_fields`.
726 """
726 """
727
727
728 if info is None:
728 if info is None:
729 ismagic = False
729 ismagic = False
730 isalias = False
730 isalias = False
731 ospace = ''
731 ospace = ''
732 else:
732 else:
733 ismagic = info.ismagic
733 ismagic = info.ismagic
734 isalias = info.isalias
734 isalias = info.isalias
735 ospace = info.namespace
735 ospace = info.namespace
736
736
737 # Get docstring, special-casing aliases:
737 # Get docstring, special-casing aliases:
738 if isalias:
738 if isalias:
739 if not callable(obj):
739 if not callable(obj):
740 try:
740 try:
741 ds = "Alias to the system command:\n %s" % obj[1]
741 ds = "Alias to the system command:\n %s" % obj[1]
742 except:
742 except:
743 ds = "Alias: " + str(obj)
743 ds = "Alias: " + str(obj)
744 else:
744 else:
745 ds = "Alias to " + str(obj)
745 ds = "Alias to " + str(obj)
746 if obj.__doc__:
746 if obj.__doc__:
747 ds += "\nDocstring:\n" + obj.__doc__
747 ds += "\nDocstring:\n" + obj.__doc__
748 else:
748 else:
749 ds = getdoc(obj)
749 ds = getdoc(obj)
750 if ds is None:
750 if ds is None:
751 ds = '<no docstring>'
751 ds = '<no docstring>'
752
752
753 # store output in a dict, we initialize it here and fill it as we go
753 # store output in a dict, we initialize it here and fill it as we go
754 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic)
754 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic)
755
755
756 string_max = 200 # max size of strings to show (snipped if longer)
756 string_max = 200 # max size of strings to show (snipped if longer)
757 shalf = int((string_max - 5) / 2)
757 shalf = int((string_max - 5) / 2)
758
758
759 if ismagic:
759 if ismagic:
760 out['type_name'] = 'Magic function'
760 out['type_name'] = 'Magic function'
761 elif isalias:
761 elif isalias:
762 out['type_name'] = 'System alias'
762 out['type_name'] = 'System alias'
763 else:
763 else:
764 out['type_name'] = type(obj).__name__
764 out['type_name'] = type(obj).__name__
765
765
766 try:
766 try:
767 bclass = obj.__class__
767 bclass = obj.__class__
768 out['base_class'] = str(bclass)
768 out['base_class'] = str(bclass)
769 except:
769 except:
770 pass
770 pass
771
771
772 # String form, but snip if too long in ? form (full in ??)
772 # String form, but snip if too long in ? form (full in ??)
773 if detail_level >= self.str_detail_level:
773 if detail_level >= self.str_detail_level:
774 try:
774 try:
775 ostr = str(obj)
775 ostr = str(obj)
776 str_head = 'string_form'
776 str_head = 'string_form'
777 if not detail_level and len(ostr)>string_max:
777 if not detail_level and len(ostr)>string_max:
778 ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
778 ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
779 ostr = ("\n" + " " * len(str_head.expandtabs())).\
779 ostr = ("\n" + " " * len(str_head.expandtabs())).\
780 join(q.strip() for q in ostr.split("\n"))
780 join(q.strip() for q in ostr.split("\n"))
781 out[str_head] = ostr
781 out[str_head] = ostr
782 except:
782 except:
783 pass
783 pass
784
784
785 if ospace:
785 if ospace:
786 out['namespace'] = ospace
786 out['namespace'] = ospace
787
787
788 # Length (for strings and lists)
788 # Length (for strings and lists)
789 try:
789 try:
790 out['length'] = str(len(obj))
790 out['length'] = str(len(obj))
791 except Exception:
791 except Exception:
792 pass
792 pass
793
793
794 # Filename where object was defined
794 # Filename where object was defined
795 binary_file = False
795 binary_file = False
796 fname = find_file(obj)
796 fname = find_file(obj)
797 if fname is None:
797 if fname is None:
798 # if anything goes wrong, we don't want to show source, so it's as
798 # if anything goes wrong, we don't want to show source, so it's as
799 # if the file was binary
799 # if the file was binary
800 binary_file = True
800 binary_file = True
801 else:
801 else:
802 if fname.endswith(('.so', '.dll', '.pyd')):
802 if fname.endswith(('.so', '.dll', '.pyd')):
803 binary_file = True
803 binary_file = True
804 elif fname.endswith('<string>'):
804 elif fname.endswith('<string>'):
805 fname = 'Dynamically generated function. No source code available.'
805 fname = 'Dynamically generated function. No source code available.'
806 out['file'] = compress_user(fname)
806 out['file'] = compress_user(fname)
807
807
808 # Original source code for a callable, class or property.
808 # Original source code for a callable, class or property.
809 if detail_level:
809 if detail_level:
810 # Flush the source cache because inspect can return out-of-date
810 # Flush the source cache because inspect can return out-of-date
811 # source
811 # source
812 linecache.checkcache()
812 linecache.checkcache()
813 try:
813 try:
814 if isinstance(obj, property) or not binary_file:
814 if isinstance(obj, property) or not binary_file:
815 src = getsource(obj, oname)
815 src = getsource(obj, oname)
816 if src is not None:
816 if src is not None:
817 src = src.rstrip()
817 src = src.rstrip()
818 out['source'] = src
818 out['source'] = src
819
819
820 except Exception:
820 except Exception:
821 pass
821 pass
822
822
823 # Add docstring only if no source is to be shown (avoid repetitions).
823 # Add docstring only if no source is to be shown (avoid repetitions).
824 if ds and out.get('source', None) is None:
824 if ds and out.get('source', None) is None:
825 out['docstring'] = ds
825 out['docstring'] = ds
826
826
827 # Constructor docstring for classes
827 # Constructor docstring for classes
828 if inspect.isclass(obj):
828 if inspect.isclass(obj):
829 out['isclass'] = True
829 out['isclass'] = True
830
830
831 # get the init signature:
831 # get the init signature:
832 try:
832 try:
833 init_def = self._getdef(obj, oname)
833 init_def = self._getdef(obj, oname)
834 except AttributeError:
834 except AttributeError:
835 init_def = None
835 init_def = None
836
836
837 # get the __init__ docstring
837 # get the __init__ docstring
838 try:
838 try:
839 obj_init = obj.__init__
839 obj_init = obj.__init__
840 except AttributeError:
840 except AttributeError:
841 init_ds = None
841 init_ds = None
842 else:
842 else:
843 if init_def is None:
843 if init_def is None:
844 # Get signature from init if top-level sig failed.
844 # Get signature from init if top-level sig failed.
845 # Can happen for built-in types (list, etc.).
845 # Can happen for built-in types (list, etc.).
846 try:
846 try:
847 init_def = self._getdef(obj_init, oname)
847 init_def = self._getdef(obj_init, oname)
848 except AttributeError:
848 except AttributeError:
849 pass
849 pass
850 init_ds = getdoc(obj_init)
850 init_ds = getdoc(obj_init)
851 # Skip Python's auto-generated docstrings
851 # Skip Python's auto-generated docstrings
852 if init_ds == _object_init_docstring:
852 if init_ds == _object_init_docstring:
853 init_ds = None
853 init_ds = None
854
854
855 if init_def:
855 if init_def:
856 out['init_definition'] = init_def
856 out['init_definition'] = init_def
857
857
858 if init_ds:
858 if init_ds:
859 out['init_docstring'] = init_ds
859 out['init_docstring'] = init_ds
860
860
861 # and class docstring for instances:
861 # and class docstring for instances:
862 else:
862 else:
863 # reconstruct the function definition and print it:
863 # reconstruct the function definition and print it:
864 defln = self._getdef(obj, oname)
864 defln = self._getdef(obj, oname)
865 if defln:
865 if defln:
866 out['definition'] = defln
866 out['definition'] = defln
867
867
868 # First, check whether the instance docstring is identical to the
868 # First, check whether the instance docstring is identical to the
869 # class one, and print it separately if they don't coincide. In
869 # class one, and print it separately if they don't coincide. In
870 # most cases they will, but it's nice to print all the info for
870 # most cases they will, but it's nice to print all the info for
871 # objects which use instance-customized docstrings.
871 # objects which use instance-customized docstrings.
872 if ds:
872 if ds:
873 try:
873 try:
874 cls = getattr(obj,'__class__')
874 cls = getattr(obj,'__class__')
875 except:
875 except:
876 class_ds = None
876 class_ds = None
877 else:
877 else:
878 class_ds = getdoc(cls)
878 class_ds = getdoc(cls)
879 # Skip Python's auto-generated docstrings
879 # Skip Python's auto-generated docstrings
880 if class_ds in _builtin_type_docstrings:
880 if class_ds in _builtin_type_docstrings:
881 class_ds = None
881 class_ds = None
882 if class_ds and ds != class_ds:
882 if class_ds and ds != class_ds:
883 out['class_docstring'] = class_ds
883 out['class_docstring'] = class_ds
884
884
885 # Next, try to show constructor docstrings
885 # Next, try to show constructor docstrings
886 try:
886 try:
887 init_ds = getdoc(obj.__init__)
887 init_ds = getdoc(obj.__init__)
888 # Skip Python's auto-generated docstrings
888 # Skip Python's auto-generated docstrings
889 if init_ds == _object_init_docstring:
889 if init_ds == _object_init_docstring:
890 init_ds = None
890 init_ds = None
891 except AttributeError:
891 except AttributeError:
892 init_ds = None
892 init_ds = None
893 if init_ds:
893 if init_ds:
894 out['init_docstring'] = init_ds
894 out['init_docstring'] = init_ds
895
895
896 # Call form docstring for callable instances
896 # Call form docstring for callable instances
897 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
897 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
898 call_def = self._getdef(obj.__call__, oname)
898 call_def = self._getdef(obj.__call__, oname)
899 if call_def and (call_def != out.get('definition')):
899 if call_def and (call_def != out.get('definition')):
900 # it may never be the case that call def and definition differ,
900 # it may never be the case that call def and definition differ,
901 # but don't include the same signature twice
901 # but don't include the same signature twice
902 out['call_def'] = call_def
902 out['call_def'] = call_def
903 call_ds = getdoc(obj.__call__)
903 call_ds = getdoc(obj.__call__)
904 # Skip Python's auto-generated docstrings
904 # Skip Python's auto-generated docstrings
905 if call_ds == _func_call_docstring:
905 if call_ds == _func_call_docstring:
906 call_ds = None
906 call_ds = None
907 if call_ds:
907 if call_ds:
908 out['call_docstring'] = call_ds
908 out['call_docstring'] = call_ds
909
909
910 # Compute the object's argspec as a callable. The key is to decide
910 # Compute the object's argspec as a callable. The key is to decide
911 # whether to pull it from the object itself, from its __init__ or
911 # whether to pull it from the object itself, from its __init__ or
912 # from its __call__ method.
912 # from its __call__ method.
913
913
914 if inspect.isclass(obj):
914 if inspect.isclass(obj):
915 # Old-style classes need not have an __init__
915 # Old-style classes need not have an __init__
916 callable_obj = getattr(obj, "__init__", None)
916 callable_obj = getattr(obj, "__init__", None)
917 elif callable(obj):
917 elif callable(obj):
918 callable_obj = obj
918 callable_obj = obj
919 else:
919 else:
920 callable_obj = None
920 callable_obj = None
921
921
922 if callable_obj is not None:
922 if callable_obj is not None:
923 try:
923 try:
924 argspec = getargspec(callable_obj)
924 argspec = getargspec(callable_obj)
925 except Exception:
925 except Exception:
926 # For extensions/builtins we can't retrieve the argspec
926 # For extensions/builtins we can't retrieve the argspec
927 pass
927 pass
928 else:
928 else:
929 # named tuples' _asdict() method returns an OrderedDict, but we
929 # named tuples' _asdict() method returns an OrderedDict, but we
930 # we want a normal
930 # we want a normal
931 out['argspec'] = argspec_dict = dict(argspec._asdict())
931 out['argspec'] = argspec_dict = dict(argspec._asdict())
932 # We called this varkw before argspec became a named tuple.
932 # We called this varkw before argspec became a named tuple.
933 # With getfullargspec it's also called varkw.
933 # With getfullargspec it's also called varkw.
934 if 'varkw' not in argspec_dict:
934 if 'varkw' not in argspec_dict:
935 argspec_dict['varkw'] = argspec_dict.pop('keywords')
935 argspec_dict['varkw'] = argspec_dict.pop('keywords')
936
936
937 return object_info(**out)
937 return object_info(**out)
938
938
939 def psearch(self,pattern,ns_table,ns_search=[],
939 def psearch(self,pattern,ns_table,ns_search=[],
940 ignore_case=False,show_all=False):
940 ignore_case=False,show_all=False):
941 """Search namespaces with wildcards for objects.
941 """Search namespaces with wildcards for objects.
942
942
943 Arguments:
943 Arguments:
944
944
945 - pattern: string containing shell-like wildcards to use in namespace
945 - pattern: string containing shell-like wildcards to use in namespace
946 searches and optionally a type specification to narrow the search to
946 searches and optionally a type specification to narrow the search to
947 objects of that type.
947 objects of that type.
948
948
949 - ns_table: dict of name->namespaces for search.
949 - ns_table: dict of name->namespaces for search.
950
950
951 Optional arguments:
951 Optional arguments:
952
952
953 - ns_search: list of namespace names to include in search.
953 - ns_search: list of namespace names to include in search.
954
954
955 - ignore_case(False): make the search case-insensitive.
955 - ignore_case(False): make the search case-insensitive.
956
956
957 - show_all(False): show all names, including those starting with
957 - show_all(False): show all names, including those starting with
958 underscores.
958 underscores.
959 """
959 """
960 #print 'ps pattern:<%r>' % pattern # dbg
960 #print 'ps pattern:<%r>' % pattern # dbg
961
961
962 # defaults
962 # defaults
963 type_pattern = 'all'
963 type_pattern = 'all'
964 filter = ''
964 filter = ''
965
965
966 cmds = pattern.split()
966 cmds = pattern.split()
967 len_cmds = len(cmds)
967 len_cmds = len(cmds)
968 if len_cmds == 1:
968 if len_cmds == 1:
969 # Only filter pattern given
969 # Only filter pattern given
970 filter = cmds[0]
970 filter = cmds[0]
971 elif len_cmds == 2:
971 elif len_cmds == 2:
972 # Both filter and type specified
972 # Both filter and type specified
973 filter,type_pattern = cmds
973 filter,type_pattern = cmds
974 else:
974 else:
975 raise ValueError('invalid argument string for psearch: <%s>' %
975 raise ValueError('invalid argument string for psearch: <%s>' %
976 pattern)
976 pattern)
977
977
978 # filter search namespaces
978 # filter search namespaces
979 for name in ns_search:
979 for name in ns_search:
980 if name not in ns_table:
980 if name not in ns_table:
981 raise ValueError('invalid namespace <%s>. Valid names: %s' %
981 raise ValueError('invalid namespace <%s>. Valid names: %s' %
982 (name,ns_table.keys()))
982 (name,ns_table.keys()))
983
983
984 #print 'type_pattern:',type_pattern # dbg
984 #print 'type_pattern:',type_pattern # dbg
985 search_result, namespaces_seen = set(), set()
985 search_result, namespaces_seen = set(), set()
986 for ns_name in ns_search:
986 for ns_name in ns_search:
987 ns = ns_table[ns_name]
987 ns = ns_table[ns_name]
988 # Normally, locals and globals are the same, so we just check one.
988 # Normally, locals and globals are the same, so we just check one.
989 if id(ns) in namespaces_seen:
989 if id(ns) in namespaces_seen:
990 continue
990 continue
991 namespaces_seen.add(id(ns))
991 namespaces_seen.add(id(ns))
992 tmp_res = list_namespace(ns, type_pattern, filter,
992 tmp_res = list_namespace(ns, type_pattern, filter,
993 ignore_case=ignore_case, show_all=show_all)
993 ignore_case=ignore_case, show_all=show_all)
994 search_result.update(tmp_res)
994 search_result.update(tmp_res)
995
995
996 page.page('\n'.join(sorted(search_result)))
996 page.page('\n'.join(sorted(search_result)))
@@ -1,709 +1,709 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Prefiltering components.
3 Prefiltering components.
4
4
5 Prefilters transform user input before it is exec'd by Python. These
5 Prefilters transform user input before it is exec'd by Python. These
6 transforms are used to implement additional syntax such as !ls and %magic.
6 transforms are used to implement additional syntax such as !ls and %magic.
7 """
7 """
8
8
9 # Copyright (c) IPython Development Team.
9 # Copyright (c) IPython Development Team.
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11
11
12 from keyword import iskeyword
12 from keyword import iskeyword
13 import re
13 import re
14
14
15 from IPython.core.autocall import IPyAutocall
15 from IPython.core.autocall import IPyAutocall
16 from traitlets.config.configurable import Configurable
16 from traitlets.config.configurable import Configurable
17 from IPython.core.inputsplitter import (
17 from IPython.core.inputsplitter import (
18 ESC_MAGIC,
18 ESC_MAGIC,
19 ESC_QUOTE,
19 ESC_QUOTE,
20 ESC_QUOTE2,
20 ESC_QUOTE2,
21 ESC_PAREN,
21 ESC_PAREN,
22 )
22 )
23 from IPython.core.macro import Macro
23 from IPython.core.macro import Macro
24 from IPython.core.splitinput import LineInfo
24 from IPython.core.splitinput import LineInfo
25
25
26 from traitlets import (
26 from traitlets import (
27 List, Integer, Unicode, Bool, Instance, CRegExp
27 List, Integer, Unicode, Bool, Instance, CRegExp
28 )
28 )
29
29
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31 # Global utilities, errors and constants
31 # Global utilities, errors and constants
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33
33
34
34
35 class PrefilterError(Exception):
35 class PrefilterError(Exception):
36 pass
36 pass
37
37
38
38
39 # RegExp to identify potential function names
39 # RegExp to identify potential function names
40 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
40 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
41
41
42 # RegExp to exclude strings with this start from autocalling. In
42 # RegExp to exclude strings with this start from autocalling. In
43 # particular, all binary operators should be excluded, so that if foo is
43 # particular, all binary operators should be excluded, so that if foo is
44 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
44 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
45 # characters '!=()' don't need to be checked for, as the checkPythonChars
45 # characters '!=()' don't need to be checked for, as the checkPythonChars
46 # routine explicitely does so, to catch direct calls and rebindings of
46 # routine explicitly does so, to catch direct calls and rebindings of
47 # existing names.
47 # existing names.
48
48
49 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
49 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
50 # it affects the rest of the group in square brackets.
50 # it affects the rest of the group in square brackets.
51 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
51 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
52 r'|^is |^not |^in |^and |^or ')
52 r'|^is |^not |^in |^and |^or ')
53
53
54 # try to catch also methods for stuff in lists/tuples/dicts: off
54 # try to catch also methods for stuff in lists/tuples/dicts: off
55 # (experimental). For this to work, the line_split regexp would need
55 # (experimental). For this to work, the line_split regexp would need
56 # to be modified so it wouldn't break things at '['. That line is
56 # to be modified so it wouldn't break things at '['. That line is
57 # nasty enough that I shouldn't change it until I can test it _well_.
57 # nasty enough that I shouldn't change it until I can test it _well_.
58 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
58 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
59
59
60
60
61 # Handler Check Utilities
61 # Handler Check Utilities
62 def is_shadowed(identifier, ip):
62 def is_shadowed(identifier, ip):
63 """Is the given identifier defined in one of the namespaces which shadow
63 """Is the given identifier defined in one of the namespaces which shadow
64 the alias and magic namespaces? Note that an identifier is different
64 the alias and magic namespaces? Note that an identifier is different
65 than ifun, because it can not contain a '.' character."""
65 than ifun, because it can not contain a '.' character."""
66 # This is much safer than calling ofind, which can change state
66 # This is much safer than calling ofind, which can change state
67 return (identifier in ip.user_ns \
67 return (identifier in ip.user_ns \
68 or identifier in ip.user_global_ns \
68 or identifier in ip.user_global_ns \
69 or identifier in ip.ns_table['builtin']\
69 or identifier in ip.ns_table['builtin']\
70 or iskeyword(identifier))
70 or iskeyword(identifier))
71
71
72
72
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74 # Main Prefilter manager
74 # Main Prefilter manager
75 #-----------------------------------------------------------------------------
75 #-----------------------------------------------------------------------------
76
76
77
77
78 class PrefilterManager(Configurable):
78 class PrefilterManager(Configurable):
79 """Main prefilter component.
79 """Main prefilter component.
80
80
81 The IPython prefilter is run on all user input before it is run. The
81 The IPython prefilter is run on all user input before it is run. The
82 prefilter consumes lines of input and produces transformed lines of
82 prefilter consumes lines of input and produces transformed lines of
83 input.
83 input.
84
84
85 The iplementation consists of two phases:
85 The iplementation consists of two phases:
86
86
87 1. Transformers
87 1. Transformers
88 2. Checkers and handlers
88 2. Checkers and handlers
89
89
90 Over time, we plan on deprecating the checkers and handlers and doing
90 Over time, we plan on deprecating the checkers and handlers and doing
91 everything in the transformers.
91 everything in the transformers.
92
92
93 The transformers are instances of :class:`PrefilterTransformer` and have
93 The transformers are instances of :class:`PrefilterTransformer` and have
94 a single method :meth:`transform` that takes a line and returns a
94 a single method :meth:`transform` that takes a line and returns a
95 transformed line. The transformation can be accomplished using any
95 transformed line. The transformation can be accomplished using any
96 tool, but our current ones use regular expressions for speed.
96 tool, but our current ones use regular expressions for speed.
97
97
98 After all the transformers have been run, the line is fed to the checkers,
98 After all the transformers have been run, the line is fed to the checkers,
99 which are instances of :class:`PrefilterChecker`. The line is passed to
99 which are instances of :class:`PrefilterChecker`. The line is passed to
100 the :meth:`check` method, which either returns `None` or a
100 the :meth:`check` method, which either returns `None` or a
101 :class:`PrefilterHandler` instance. If `None` is returned, the other
101 :class:`PrefilterHandler` instance. If `None` is returned, the other
102 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
102 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
103 the line is passed to the :meth:`handle` method of the returned
103 the line is passed to the :meth:`handle` method of the returned
104 handler and no further checkers are tried.
104 handler and no further checkers are tried.
105
105
106 Both transformers and checkers have a `priority` attribute, that determines
106 Both transformers and checkers have a `priority` attribute, that determines
107 the order in which they are called. Smaller priorities are tried first.
107 the order in which they are called. Smaller priorities are tried first.
108
108
109 Both transformers and checkers also have `enabled` attribute, which is
109 Both transformers and checkers also have `enabled` attribute, which is
110 a boolean that determines if the instance is used.
110 a boolean that determines if the instance is used.
111
111
112 Users or developers can change the priority or enabled attribute of
112 Users or developers can change the priority or enabled attribute of
113 transformers or checkers, but they must call the :meth:`sort_checkers`
113 transformers or checkers, but they must call the :meth:`sort_checkers`
114 or :meth:`sort_transformers` method after changing the priority.
114 or :meth:`sort_transformers` method after changing the priority.
115 """
115 """
116
116
117 multi_line_specials = Bool(True).tag(config=True)
117 multi_line_specials = Bool(True).tag(config=True)
118 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
118 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
119
119
120 def __init__(self, shell=None, **kwargs):
120 def __init__(self, shell=None, **kwargs):
121 super(PrefilterManager, self).__init__(shell=shell, **kwargs)
121 super(PrefilterManager, self).__init__(shell=shell, **kwargs)
122 self.shell = shell
122 self.shell = shell
123 self.init_transformers()
123 self.init_transformers()
124 self.init_handlers()
124 self.init_handlers()
125 self.init_checkers()
125 self.init_checkers()
126
126
127 #-------------------------------------------------------------------------
127 #-------------------------------------------------------------------------
128 # API for managing transformers
128 # API for managing transformers
129 #-------------------------------------------------------------------------
129 #-------------------------------------------------------------------------
130
130
131 def init_transformers(self):
131 def init_transformers(self):
132 """Create the default transformers."""
132 """Create the default transformers."""
133 self._transformers = []
133 self._transformers = []
134 for transformer_cls in _default_transformers:
134 for transformer_cls in _default_transformers:
135 transformer_cls(
135 transformer_cls(
136 shell=self.shell, prefilter_manager=self, parent=self
136 shell=self.shell, prefilter_manager=self, parent=self
137 )
137 )
138
138
139 def sort_transformers(self):
139 def sort_transformers(self):
140 """Sort the transformers by priority.
140 """Sort the transformers by priority.
141
141
142 This must be called after the priority of a transformer is changed.
142 This must be called after the priority of a transformer is changed.
143 The :meth:`register_transformer` method calls this automatically.
143 The :meth:`register_transformer` method calls this automatically.
144 """
144 """
145 self._transformers.sort(key=lambda x: x.priority)
145 self._transformers.sort(key=lambda x: x.priority)
146
146
147 @property
147 @property
148 def transformers(self):
148 def transformers(self):
149 """Return a list of checkers, sorted by priority."""
149 """Return a list of checkers, sorted by priority."""
150 return self._transformers
150 return self._transformers
151
151
152 def register_transformer(self, transformer):
152 def register_transformer(self, transformer):
153 """Register a transformer instance."""
153 """Register a transformer instance."""
154 if transformer not in self._transformers:
154 if transformer not in self._transformers:
155 self._transformers.append(transformer)
155 self._transformers.append(transformer)
156 self.sort_transformers()
156 self.sort_transformers()
157
157
158 def unregister_transformer(self, transformer):
158 def unregister_transformer(self, transformer):
159 """Unregister a transformer instance."""
159 """Unregister a transformer instance."""
160 if transformer in self._transformers:
160 if transformer in self._transformers:
161 self._transformers.remove(transformer)
161 self._transformers.remove(transformer)
162
162
163 #-------------------------------------------------------------------------
163 #-------------------------------------------------------------------------
164 # API for managing checkers
164 # API for managing checkers
165 #-------------------------------------------------------------------------
165 #-------------------------------------------------------------------------
166
166
167 def init_checkers(self):
167 def init_checkers(self):
168 """Create the default checkers."""
168 """Create the default checkers."""
169 self._checkers = []
169 self._checkers = []
170 for checker in _default_checkers:
170 for checker in _default_checkers:
171 checker(
171 checker(
172 shell=self.shell, prefilter_manager=self, parent=self
172 shell=self.shell, prefilter_manager=self, parent=self
173 )
173 )
174
174
175 def sort_checkers(self):
175 def sort_checkers(self):
176 """Sort the checkers by priority.
176 """Sort the checkers by priority.
177
177
178 This must be called after the priority of a checker is changed.
178 This must be called after the priority of a checker is changed.
179 The :meth:`register_checker` method calls this automatically.
179 The :meth:`register_checker` method calls this automatically.
180 """
180 """
181 self._checkers.sort(key=lambda x: x.priority)
181 self._checkers.sort(key=lambda x: x.priority)
182
182
183 @property
183 @property
184 def checkers(self):
184 def checkers(self):
185 """Return a list of checkers, sorted by priority."""
185 """Return a list of checkers, sorted by priority."""
186 return self._checkers
186 return self._checkers
187
187
188 def register_checker(self, checker):
188 def register_checker(self, checker):
189 """Register a checker instance."""
189 """Register a checker instance."""
190 if checker not in self._checkers:
190 if checker not in self._checkers:
191 self._checkers.append(checker)
191 self._checkers.append(checker)
192 self.sort_checkers()
192 self.sort_checkers()
193
193
194 def unregister_checker(self, checker):
194 def unregister_checker(self, checker):
195 """Unregister a checker instance."""
195 """Unregister a checker instance."""
196 if checker in self._checkers:
196 if checker in self._checkers:
197 self._checkers.remove(checker)
197 self._checkers.remove(checker)
198
198
199 #-------------------------------------------------------------------------
199 #-------------------------------------------------------------------------
200 # API for managing handlers
200 # API for managing handlers
201 #-------------------------------------------------------------------------
201 #-------------------------------------------------------------------------
202
202
203 def init_handlers(self):
203 def init_handlers(self):
204 """Create the default handlers."""
204 """Create the default handlers."""
205 self._handlers = {}
205 self._handlers = {}
206 self._esc_handlers = {}
206 self._esc_handlers = {}
207 for handler in _default_handlers:
207 for handler in _default_handlers:
208 handler(
208 handler(
209 shell=self.shell, prefilter_manager=self, parent=self
209 shell=self.shell, prefilter_manager=self, parent=self
210 )
210 )
211
211
212 @property
212 @property
213 def handlers(self):
213 def handlers(self):
214 """Return a dict of all the handlers."""
214 """Return a dict of all the handlers."""
215 return self._handlers
215 return self._handlers
216
216
217 def register_handler(self, name, handler, esc_strings):
217 def register_handler(self, name, handler, esc_strings):
218 """Register a handler instance by name with esc_strings."""
218 """Register a handler instance by name with esc_strings."""
219 self._handlers[name] = handler
219 self._handlers[name] = handler
220 for esc_str in esc_strings:
220 for esc_str in esc_strings:
221 self._esc_handlers[esc_str] = handler
221 self._esc_handlers[esc_str] = handler
222
222
223 def unregister_handler(self, name, handler, esc_strings):
223 def unregister_handler(self, name, handler, esc_strings):
224 """Unregister a handler instance by name with esc_strings."""
224 """Unregister a handler instance by name with esc_strings."""
225 try:
225 try:
226 del self._handlers[name]
226 del self._handlers[name]
227 except KeyError:
227 except KeyError:
228 pass
228 pass
229 for esc_str in esc_strings:
229 for esc_str in esc_strings:
230 h = self._esc_handlers.get(esc_str)
230 h = self._esc_handlers.get(esc_str)
231 if h is handler:
231 if h is handler:
232 del self._esc_handlers[esc_str]
232 del self._esc_handlers[esc_str]
233
233
234 def get_handler_by_name(self, name):
234 def get_handler_by_name(self, name):
235 """Get a handler by its name."""
235 """Get a handler by its name."""
236 return self._handlers.get(name)
236 return self._handlers.get(name)
237
237
238 def get_handler_by_esc(self, esc_str):
238 def get_handler_by_esc(self, esc_str):
239 """Get a handler by its escape string."""
239 """Get a handler by its escape string."""
240 return self._esc_handlers.get(esc_str)
240 return self._esc_handlers.get(esc_str)
241
241
242 #-------------------------------------------------------------------------
242 #-------------------------------------------------------------------------
243 # Main prefiltering API
243 # Main prefiltering API
244 #-------------------------------------------------------------------------
244 #-------------------------------------------------------------------------
245
245
246 def prefilter_line_info(self, line_info):
246 def prefilter_line_info(self, line_info):
247 """Prefilter a line that has been converted to a LineInfo object.
247 """Prefilter a line that has been converted to a LineInfo object.
248
248
249 This implements the checker/handler part of the prefilter pipe.
249 This implements the checker/handler part of the prefilter pipe.
250 """
250 """
251 # print "prefilter_line_info: ", line_info
251 # print "prefilter_line_info: ", line_info
252 handler = self.find_handler(line_info)
252 handler = self.find_handler(line_info)
253 return handler.handle(line_info)
253 return handler.handle(line_info)
254
254
255 def find_handler(self, line_info):
255 def find_handler(self, line_info):
256 """Find a handler for the line_info by trying checkers."""
256 """Find a handler for the line_info by trying checkers."""
257 for checker in self.checkers:
257 for checker in self.checkers:
258 if checker.enabled:
258 if checker.enabled:
259 handler = checker.check(line_info)
259 handler = checker.check(line_info)
260 if handler:
260 if handler:
261 return handler
261 return handler
262 return self.get_handler_by_name('normal')
262 return self.get_handler_by_name('normal')
263
263
264 def transform_line(self, line, continue_prompt):
264 def transform_line(self, line, continue_prompt):
265 """Calls the enabled transformers in order of increasing priority."""
265 """Calls the enabled transformers in order of increasing priority."""
266 for transformer in self.transformers:
266 for transformer in self.transformers:
267 if transformer.enabled:
267 if transformer.enabled:
268 line = transformer.transform(line, continue_prompt)
268 line = transformer.transform(line, continue_prompt)
269 return line
269 return line
270
270
271 def prefilter_line(self, line, continue_prompt=False):
271 def prefilter_line(self, line, continue_prompt=False):
272 """Prefilter a single input line as text.
272 """Prefilter a single input line as text.
273
273
274 This method prefilters a single line of text by calling the
274 This method prefilters a single line of text by calling the
275 transformers and then the checkers/handlers.
275 transformers and then the checkers/handlers.
276 """
276 """
277
277
278 # print "prefilter_line: ", line, continue_prompt
278 # print "prefilter_line: ", line, continue_prompt
279 # All handlers *must* return a value, even if it's blank ('').
279 # All handlers *must* return a value, even if it's blank ('').
280
280
281 # save the line away in case we crash, so the post-mortem handler can
281 # save the line away in case we crash, so the post-mortem handler can
282 # record it
282 # record it
283 self.shell._last_input_line = line
283 self.shell._last_input_line = line
284
284
285 if not line:
285 if not line:
286 # Return immediately on purely empty lines, so that if the user
286 # Return immediately on purely empty lines, so that if the user
287 # previously typed some whitespace that started a continuation
287 # previously typed some whitespace that started a continuation
288 # prompt, he can break out of that loop with just an empty line.
288 # prompt, he can break out of that loop with just an empty line.
289 # This is how the default python prompt works.
289 # This is how the default python prompt works.
290 return ''
290 return ''
291
291
292 # At this point, we invoke our transformers.
292 # At this point, we invoke our transformers.
293 if not continue_prompt or (continue_prompt and self.multi_line_specials):
293 if not continue_prompt or (continue_prompt and self.multi_line_specials):
294 line = self.transform_line(line, continue_prompt)
294 line = self.transform_line(line, continue_prompt)
295
295
296 # Now we compute line_info for the checkers and handlers
296 # Now we compute line_info for the checkers and handlers
297 line_info = LineInfo(line, continue_prompt)
297 line_info = LineInfo(line, continue_prompt)
298
298
299 # the input history needs to track even empty lines
299 # the input history needs to track even empty lines
300 stripped = line.strip()
300 stripped = line.strip()
301
301
302 normal_handler = self.get_handler_by_name('normal')
302 normal_handler = self.get_handler_by_name('normal')
303 if not stripped:
303 if not stripped:
304 return normal_handler.handle(line_info)
304 return normal_handler.handle(line_info)
305
305
306 # special handlers are only allowed for single line statements
306 # special handlers are only allowed for single line statements
307 if continue_prompt and not self.multi_line_specials:
307 if continue_prompt and not self.multi_line_specials:
308 return normal_handler.handle(line_info)
308 return normal_handler.handle(line_info)
309
309
310 prefiltered = self.prefilter_line_info(line_info)
310 prefiltered = self.prefilter_line_info(line_info)
311 # print "prefiltered line: %r" % prefiltered
311 # print "prefiltered line: %r" % prefiltered
312 return prefiltered
312 return prefiltered
313
313
314 def prefilter_lines(self, lines, continue_prompt=False):
314 def prefilter_lines(self, lines, continue_prompt=False):
315 """Prefilter multiple input lines of text.
315 """Prefilter multiple input lines of text.
316
316
317 This is the main entry point for prefiltering multiple lines of
317 This is the main entry point for prefiltering multiple lines of
318 input. This simply calls :meth:`prefilter_line` for each line of
318 input. This simply calls :meth:`prefilter_line` for each line of
319 input.
319 input.
320
320
321 This covers cases where there are multiple lines in the user entry,
321 This covers cases where there are multiple lines in the user entry,
322 which is the case when the user goes back to a multiline history
322 which is the case when the user goes back to a multiline history
323 entry and presses enter.
323 entry and presses enter.
324 """
324 """
325 llines = lines.rstrip('\n').split('\n')
325 llines = lines.rstrip('\n').split('\n')
326 # We can get multiple lines in one shot, where multiline input 'blends'
326 # We can get multiple lines in one shot, where multiline input 'blends'
327 # into one line, in cases like recalling from the readline history
327 # into one line, in cases like recalling from the readline history
328 # buffer. We need to make sure that in such cases, we correctly
328 # buffer. We need to make sure that in such cases, we correctly
329 # communicate downstream which line is first and which are continuation
329 # communicate downstream which line is first and which are continuation
330 # ones.
330 # ones.
331 if len(llines) > 1:
331 if len(llines) > 1:
332 out = '\n'.join([self.prefilter_line(line, lnum>0)
332 out = '\n'.join([self.prefilter_line(line, lnum>0)
333 for lnum, line in enumerate(llines) ])
333 for lnum, line in enumerate(llines) ])
334 else:
334 else:
335 out = self.prefilter_line(llines[0], continue_prompt)
335 out = self.prefilter_line(llines[0], continue_prompt)
336
336
337 return out
337 return out
338
338
339 #-----------------------------------------------------------------------------
339 #-----------------------------------------------------------------------------
340 # Prefilter transformers
340 # Prefilter transformers
341 #-----------------------------------------------------------------------------
341 #-----------------------------------------------------------------------------
342
342
343
343
344 class PrefilterTransformer(Configurable):
344 class PrefilterTransformer(Configurable):
345 """Transform a line of user input."""
345 """Transform a line of user input."""
346
346
347 priority = Integer(100).tag(config=True)
347 priority = Integer(100).tag(config=True)
348 # Transformers don't currently use shell or prefilter_manager, but as we
348 # Transformers don't currently use shell or prefilter_manager, but as we
349 # move away from checkers and handlers, they will need them.
349 # move away from checkers and handlers, they will need them.
350 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
350 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
351 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
351 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
352 enabled = Bool(True).tag(config=True)
352 enabled = Bool(True).tag(config=True)
353
353
354 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
354 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
355 super(PrefilterTransformer, self).__init__(
355 super(PrefilterTransformer, self).__init__(
356 shell=shell, prefilter_manager=prefilter_manager, **kwargs
356 shell=shell, prefilter_manager=prefilter_manager, **kwargs
357 )
357 )
358 self.prefilter_manager.register_transformer(self)
358 self.prefilter_manager.register_transformer(self)
359
359
360 def transform(self, line, continue_prompt):
360 def transform(self, line, continue_prompt):
361 """Transform a line, returning the new one."""
361 """Transform a line, returning the new one."""
362 return None
362 return None
363
363
364 def __repr__(self):
364 def __repr__(self):
365 return "<%s(priority=%r, enabled=%r)>" % (
365 return "<%s(priority=%r, enabled=%r)>" % (
366 self.__class__.__name__, self.priority, self.enabled)
366 self.__class__.__name__, self.priority, self.enabled)
367
367
368
368
369 #-----------------------------------------------------------------------------
369 #-----------------------------------------------------------------------------
370 # Prefilter checkers
370 # Prefilter checkers
371 #-----------------------------------------------------------------------------
371 #-----------------------------------------------------------------------------
372
372
373
373
374 class PrefilterChecker(Configurable):
374 class PrefilterChecker(Configurable):
375 """Inspect an input line and return a handler for that line."""
375 """Inspect an input line and return a handler for that line."""
376
376
377 priority = Integer(100).tag(config=True)
377 priority = Integer(100).tag(config=True)
378 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
378 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
379 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
379 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
380 enabled = Bool(True).tag(config=True)
380 enabled = Bool(True).tag(config=True)
381
381
382 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
382 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
383 super(PrefilterChecker, self).__init__(
383 super(PrefilterChecker, self).__init__(
384 shell=shell, prefilter_manager=prefilter_manager, **kwargs
384 shell=shell, prefilter_manager=prefilter_manager, **kwargs
385 )
385 )
386 self.prefilter_manager.register_checker(self)
386 self.prefilter_manager.register_checker(self)
387
387
388 def check(self, line_info):
388 def check(self, line_info):
389 """Inspect line_info and return a handler instance or None."""
389 """Inspect line_info and return a handler instance or None."""
390 return None
390 return None
391
391
392 def __repr__(self):
392 def __repr__(self):
393 return "<%s(priority=%r, enabled=%r)>" % (
393 return "<%s(priority=%r, enabled=%r)>" % (
394 self.__class__.__name__, self.priority, self.enabled)
394 self.__class__.__name__, self.priority, self.enabled)
395
395
396
396
397 class EmacsChecker(PrefilterChecker):
397 class EmacsChecker(PrefilterChecker):
398
398
399 priority = Integer(100).tag(config=True)
399 priority = Integer(100).tag(config=True)
400 enabled = Bool(False).tag(config=True)
400 enabled = Bool(False).tag(config=True)
401
401
402 def check(self, line_info):
402 def check(self, line_info):
403 "Emacs ipython-mode tags certain input lines."
403 "Emacs ipython-mode tags certain input lines."
404 if line_info.line.endswith('# PYTHON-MODE'):
404 if line_info.line.endswith('# PYTHON-MODE'):
405 return self.prefilter_manager.get_handler_by_name('emacs')
405 return self.prefilter_manager.get_handler_by_name('emacs')
406 else:
406 else:
407 return None
407 return None
408
408
409
409
410 class MacroChecker(PrefilterChecker):
410 class MacroChecker(PrefilterChecker):
411
411
412 priority = Integer(250).tag(config=True)
412 priority = Integer(250).tag(config=True)
413
413
414 def check(self, line_info):
414 def check(self, line_info):
415 obj = self.shell.user_ns.get(line_info.ifun)
415 obj = self.shell.user_ns.get(line_info.ifun)
416 if isinstance(obj, Macro):
416 if isinstance(obj, Macro):
417 return self.prefilter_manager.get_handler_by_name('macro')
417 return self.prefilter_manager.get_handler_by_name('macro')
418 else:
418 else:
419 return None
419 return None
420
420
421
421
422 class IPyAutocallChecker(PrefilterChecker):
422 class IPyAutocallChecker(PrefilterChecker):
423
423
424 priority = Integer(300).tag(config=True)
424 priority = Integer(300).tag(config=True)
425
425
426 def check(self, line_info):
426 def check(self, line_info):
427 "Instances of IPyAutocall in user_ns get autocalled immediately"
427 "Instances of IPyAutocall in user_ns get autocalled immediately"
428 obj = self.shell.user_ns.get(line_info.ifun, None)
428 obj = self.shell.user_ns.get(line_info.ifun, None)
429 if isinstance(obj, IPyAutocall):
429 if isinstance(obj, IPyAutocall):
430 obj.set_ip(self.shell)
430 obj.set_ip(self.shell)
431 return self.prefilter_manager.get_handler_by_name('auto')
431 return self.prefilter_manager.get_handler_by_name('auto')
432 else:
432 else:
433 return None
433 return None
434
434
435
435
436 class AssignmentChecker(PrefilterChecker):
436 class AssignmentChecker(PrefilterChecker):
437
437
438 priority = Integer(600).tag(config=True)
438 priority = Integer(600).tag(config=True)
439
439
440 def check(self, line_info):
440 def check(self, line_info):
441 """Check to see if user is assigning to a var for the first time, in
441 """Check to see if user is assigning to a var for the first time, in
442 which case we want to avoid any sort of automagic / autocall games.
442 which case we want to avoid any sort of automagic / autocall games.
443
443
444 This allows users to assign to either alias or magic names true python
444 This allows users to assign to either alias or magic names true python
445 variables (the magic/alias systems always take second seat to true
445 variables (the magic/alias systems always take second seat to true
446 python code). E.g. ls='hi', or ls,that=1,2"""
446 python code). E.g. ls='hi', or ls,that=1,2"""
447 if line_info.the_rest:
447 if line_info.the_rest:
448 if line_info.the_rest[0] in '=,':
448 if line_info.the_rest[0] in '=,':
449 return self.prefilter_manager.get_handler_by_name('normal')
449 return self.prefilter_manager.get_handler_by_name('normal')
450 else:
450 else:
451 return None
451 return None
452
452
453
453
454 class AutoMagicChecker(PrefilterChecker):
454 class AutoMagicChecker(PrefilterChecker):
455
455
456 priority = Integer(700).tag(config=True)
456 priority = Integer(700).tag(config=True)
457
457
458 def check(self, line_info):
458 def check(self, line_info):
459 """If the ifun is magic, and automagic is on, run it. Note: normal,
459 """If the ifun is magic, and automagic is on, run it. Note: normal,
460 non-auto magic would already have been triggered via '%' in
460 non-auto magic would already have been triggered via '%' in
461 check_esc_chars. This just checks for automagic. Also, before
461 check_esc_chars. This just checks for automagic. Also, before
462 triggering the magic handler, make sure that there is nothing in the
462 triggering the magic handler, make sure that there is nothing in the
463 user namespace which could shadow it."""
463 user namespace which could shadow it."""
464 if not self.shell.automagic or not self.shell.find_magic(line_info.ifun):
464 if not self.shell.automagic or not self.shell.find_magic(line_info.ifun):
465 return None
465 return None
466
466
467 # We have a likely magic method. Make sure we should actually call it.
467 # We have a likely magic method. Make sure we should actually call it.
468 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
468 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
469 return None
469 return None
470
470
471 head = line_info.ifun.split('.',1)[0]
471 head = line_info.ifun.split('.',1)[0]
472 if is_shadowed(head, self.shell):
472 if is_shadowed(head, self.shell):
473 return None
473 return None
474
474
475 return self.prefilter_manager.get_handler_by_name('magic')
475 return self.prefilter_manager.get_handler_by_name('magic')
476
476
477
477
478 class PythonOpsChecker(PrefilterChecker):
478 class PythonOpsChecker(PrefilterChecker):
479
479
480 priority = Integer(900).tag(config=True)
480 priority = Integer(900).tag(config=True)
481
481
482 def check(self, line_info):
482 def check(self, line_info):
483 """If the 'rest' of the line begins with a function call or pretty much
483 """If the 'rest' of the line begins with a function call or pretty much
484 any python operator, we should simply execute the line (regardless of
484 any python operator, we should simply execute the line (regardless of
485 whether or not there's a possible autocall expansion). This avoids
485 whether or not there's a possible autocall expansion). This avoids
486 spurious (and very confusing) geattr() accesses."""
486 spurious (and very confusing) geattr() accesses."""
487 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
487 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
488 return self.prefilter_manager.get_handler_by_name('normal')
488 return self.prefilter_manager.get_handler_by_name('normal')
489 else:
489 else:
490 return None
490 return None
491
491
492
492
493 class AutocallChecker(PrefilterChecker):
493 class AutocallChecker(PrefilterChecker):
494
494
495 priority = Integer(1000).tag(config=True)
495 priority = Integer(1000).tag(config=True)
496
496
497 function_name_regexp = CRegExp(re_fun_name,
497 function_name_regexp = CRegExp(re_fun_name,
498 help="RegExp to identify potential function names."
498 help="RegExp to identify potential function names."
499 ).tag(config=True)
499 ).tag(config=True)
500 exclude_regexp = CRegExp(re_exclude_auto,
500 exclude_regexp = CRegExp(re_exclude_auto,
501 help="RegExp to exclude strings with this start from autocalling."
501 help="RegExp to exclude strings with this start from autocalling."
502 ).tag(config=True)
502 ).tag(config=True)
503
503
504 def check(self, line_info):
504 def check(self, line_info):
505 "Check if the initial word/function is callable and autocall is on."
505 "Check if the initial word/function is callable and autocall is on."
506 if not self.shell.autocall:
506 if not self.shell.autocall:
507 return None
507 return None
508
508
509 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
509 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
510 if not oinfo['found']:
510 if not oinfo['found']:
511 return None
511 return None
512
512
513 ignored_funs = ['b', 'f', 'r', 'u', 'br', 'rb', 'fr', 'rf']
513 ignored_funs = ['b', 'f', 'r', 'u', 'br', 'rb', 'fr', 'rf']
514 ifun = line_info.ifun
514 ifun = line_info.ifun
515 line = line_info.line
515 line = line_info.line
516 if ifun.lower() in ignored_funs and (line.startswith(ifun + "'") or line.startswith(ifun + '"')):
516 if ifun.lower() in ignored_funs and (line.startswith(ifun + "'") or line.startswith(ifun + '"')):
517 return None
517 return None
518
518
519 if callable(oinfo['obj']) \
519 if callable(oinfo['obj']) \
520 and (not self.exclude_regexp.match(line_info.the_rest)) \
520 and (not self.exclude_regexp.match(line_info.the_rest)) \
521 and self.function_name_regexp.match(line_info.ifun):
521 and self.function_name_regexp.match(line_info.ifun):
522 return self.prefilter_manager.get_handler_by_name('auto')
522 return self.prefilter_manager.get_handler_by_name('auto')
523 else:
523 else:
524 return None
524 return None
525
525
526
526
527 #-----------------------------------------------------------------------------
527 #-----------------------------------------------------------------------------
528 # Prefilter handlers
528 # Prefilter handlers
529 #-----------------------------------------------------------------------------
529 #-----------------------------------------------------------------------------
530
530
531
531
532 class PrefilterHandler(Configurable):
532 class PrefilterHandler(Configurable):
533
533
534 handler_name = Unicode('normal')
534 handler_name = Unicode('normal')
535 esc_strings = List([])
535 esc_strings = List([])
536 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
536 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
537 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
537 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
538
538
539 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
539 def __init__(self, shell=None, prefilter_manager=None, **kwargs):
540 super(PrefilterHandler, self).__init__(
540 super(PrefilterHandler, self).__init__(
541 shell=shell, prefilter_manager=prefilter_manager, **kwargs
541 shell=shell, prefilter_manager=prefilter_manager, **kwargs
542 )
542 )
543 self.prefilter_manager.register_handler(
543 self.prefilter_manager.register_handler(
544 self.handler_name,
544 self.handler_name,
545 self,
545 self,
546 self.esc_strings
546 self.esc_strings
547 )
547 )
548
548
549 def handle(self, line_info):
549 def handle(self, line_info):
550 # print "normal: ", line_info
550 # print "normal: ", line_info
551 """Handle normal input lines. Use as a template for handlers."""
551 """Handle normal input lines. Use as a template for handlers."""
552
552
553 # With autoindent on, we need some way to exit the input loop, and I
553 # With autoindent on, we need some way to exit the input loop, and I
554 # don't want to force the user to have to backspace all the way to
554 # don't want to force the user to have to backspace all the way to
555 # clear the line. The rule will be in this case, that either two
555 # clear the line. The rule will be in this case, that either two
556 # lines of pure whitespace in a row, or a line of pure whitespace but
556 # lines of pure whitespace in a row, or a line of pure whitespace but
557 # of a size different to the indent level, will exit the input loop.
557 # of a size different to the indent level, will exit the input loop.
558 line = line_info.line
558 line = line_info.line
559 continue_prompt = line_info.continue_prompt
559 continue_prompt = line_info.continue_prompt
560
560
561 if (continue_prompt and
561 if (continue_prompt and
562 self.shell.autoindent and
562 self.shell.autoindent and
563 line.isspace() and
563 line.isspace() and
564 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2):
564 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2):
565 line = ''
565 line = ''
566
566
567 return line
567 return line
568
568
569 def __str__(self):
569 def __str__(self):
570 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
570 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
571
571
572
572
573 class MacroHandler(PrefilterHandler):
573 class MacroHandler(PrefilterHandler):
574 handler_name = Unicode("macro")
574 handler_name = Unicode("macro")
575
575
576 def handle(self, line_info):
576 def handle(self, line_info):
577 obj = self.shell.user_ns.get(line_info.ifun)
577 obj = self.shell.user_ns.get(line_info.ifun)
578 pre_space = line_info.pre_whitespace
578 pre_space = line_info.pre_whitespace
579 line_sep = "\n" + pre_space
579 line_sep = "\n" + pre_space
580 return pre_space + line_sep.join(obj.value.splitlines())
580 return pre_space + line_sep.join(obj.value.splitlines())
581
581
582
582
583 class MagicHandler(PrefilterHandler):
583 class MagicHandler(PrefilterHandler):
584
584
585 handler_name = Unicode('magic')
585 handler_name = Unicode('magic')
586 esc_strings = List([ESC_MAGIC])
586 esc_strings = List([ESC_MAGIC])
587
587
588 def handle(self, line_info):
588 def handle(self, line_info):
589 """Execute magic functions."""
589 """Execute magic functions."""
590 ifun = line_info.ifun
590 ifun = line_info.ifun
591 the_rest = line_info.the_rest
591 the_rest = line_info.the_rest
592 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
592 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
593 t_arg_s = ifun + " " + the_rest
593 t_arg_s = ifun + " " + the_rest
594 t_magic_name, _, t_magic_arg_s = t_arg_s.partition(' ')
594 t_magic_name, _, t_magic_arg_s = t_arg_s.partition(' ')
595 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
595 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
596 cmd = '%sget_ipython().run_line_magic(%r, %r)' % (line_info.pre_whitespace, t_magic_name, t_magic_arg_s)
596 cmd = '%sget_ipython().run_line_magic(%r, %r)' % (line_info.pre_whitespace, t_magic_name, t_magic_arg_s)
597 return cmd
597 return cmd
598
598
599
599
600 class AutoHandler(PrefilterHandler):
600 class AutoHandler(PrefilterHandler):
601
601
602 handler_name = Unicode('auto')
602 handler_name = Unicode('auto')
603 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
603 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
604
604
605 def handle(self, line_info):
605 def handle(self, line_info):
606 """Handle lines which can be auto-executed, quoting if requested."""
606 """Handle lines which can be auto-executed, quoting if requested."""
607 line = line_info.line
607 line = line_info.line
608 ifun = line_info.ifun
608 ifun = line_info.ifun
609 the_rest = line_info.the_rest
609 the_rest = line_info.the_rest
610 esc = line_info.esc
610 esc = line_info.esc
611 continue_prompt = line_info.continue_prompt
611 continue_prompt = line_info.continue_prompt
612 obj = line_info.ofind(self.shell)['obj']
612 obj = line_info.ofind(self.shell)['obj']
613
613
614 # This should only be active for single-line input!
614 # This should only be active for single-line input!
615 if continue_prompt:
615 if continue_prompt:
616 return line
616 return line
617
617
618 force_auto = isinstance(obj, IPyAutocall)
618 force_auto = isinstance(obj, IPyAutocall)
619
619
620 # User objects sometimes raise exceptions on attribute access other
620 # User objects sometimes raise exceptions on attribute access other
621 # than AttributeError (we've seen it in the past), so it's safest to be
621 # than AttributeError (we've seen it in the past), so it's safest to be
622 # ultra-conservative here and catch all.
622 # ultra-conservative here and catch all.
623 try:
623 try:
624 auto_rewrite = obj.rewrite
624 auto_rewrite = obj.rewrite
625 except Exception:
625 except Exception:
626 auto_rewrite = True
626 auto_rewrite = True
627
627
628 if esc == ESC_QUOTE:
628 if esc == ESC_QUOTE:
629 # Auto-quote splitting on whitespace
629 # Auto-quote splitting on whitespace
630 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
630 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
631 elif esc == ESC_QUOTE2:
631 elif esc == ESC_QUOTE2:
632 # Auto-quote whole string
632 # Auto-quote whole string
633 newcmd = '%s("%s")' % (ifun,the_rest)
633 newcmd = '%s("%s")' % (ifun,the_rest)
634 elif esc == ESC_PAREN:
634 elif esc == ESC_PAREN:
635 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
635 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
636 else:
636 else:
637 # Auto-paren.
637 # Auto-paren.
638 if force_auto:
638 if force_auto:
639 # Don't rewrite if it is already a call.
639 # Don't rewrite if it is already a call.
640 do_rewrite = not the_rest.startswith('(')
640 do_rewrite = not the_rest.startswith('(')
641 else:
641 else:
642 if not the_rest:
642 if not the_rest:
643 # We only apply it to argument-less calls if the autocall
643 # We only apply it to argument-less calls if the autocall
644 # parameter is set to 2.
644 # parameter is set to 2.
645 do_rewrite = (self.shell.autocall >= 2)
645 do_rewrite = (self.shell.autocall >= 2)
646 elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
646 elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
647 # Don't autocall in this case: item access for an object
647 # Don't autocall in this case: item access for an object
648 # which is BOTH callable and implements __getitem__.
648 # which is BOTH callable and implements __getitem__.
649 do_rewrite = False
649 do_rewrite = False
650 else:
650 else:
651 do_rewrite = True
651 do_rewrite = True
652
652
653 # Figure out the rewritten command
653 # Figure out the rewritten command
654 if do_rewrite:
654 if do_rewrite:
655 if the_rest.endswith(';'):
655 if the_rest.endswith(';'):
656 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
656 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
657 else:
657 else:
658 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
658 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
659 else:
659 else:
660 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
660 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
661 return normal_handler.handle(line_info)
661 return normal_handler.handle(line_info)
662
662
663 # Display the rewritten call
663 # Display the rewritten call
664 if auto_rewrite:
664 if auto_rewrite:
665 self.shell.auto_rewrite_input(newcmd)
665 self.shell.auto_rewrite_input(newcmd)
666
666
667 return newcmd
667 return newcmd
668
668
669
669
670 class EmacsHandler(PrefilterHandler):
670 class EmacsHandler(PrefilterHandler):
671
671
672 handler_name = Unicode('emacs')
672 handler_name = Unicode('emacs')
673 esc_strings = List([])
673 esc_strings = List([])
674
674
675 def handle(self, line_info):
675 def handle(self, line_info):
676 """Handle input lines marked by python-mode."""
676 """Handle input lines marked by python-mode."""
677
677
678 # Currently, nothing is done. Later more functionality can be added
678 # Currently, nothing is done. Later more functionality can be added
679 # here if needed.
679 # here if needed.
680
680
681 # The input cache shouldn't be updated
681 # The input cache shouldn't be updated
682 return line_info.line
682 return line_info.line
683
683
684
684
685 #-----------------------------------------------------------------------------
685 #-----------------------------------------------------------------------------
686 # Defaults
686 # Defaults
687 #-----------------------------------------------------------------------------
687 #-----------------------------------------------------------------------------
688
688
689
689
690 _default_transformers = [
690 _default_transformers = [
691 ]
691 ]
692
692
693 _default_checkers = [
693 _default_checkers = [
694 EmacsChecker,
694 EmacsChecker,
695 MacroChecker,
695 MacroChecker,
696 IPyAutocallChecker,
696 IPyAutocallChecker,
697 AssignmentChecker,
697 AssignmentChecker,
698 AutoMagicChecker,
698 AutoMagicChecker,
699 PythonOpsChecker,
699 PythonOpsChecker,
700 AutocallChecker
700 AutocallChecker
701 ]
701 ]
702
702
703 _default_handlers = [
703 _default_handlers = [
704 PrefilterHandler,
704 PrefilterHandler,
705 MacroHandler,
705 MacroHandler,
706 MagicHandler,
706 MagicHandler,
707 AutoHandler,
707 AutoHandler,
708 EmacsHandler
708 EmacsHandler
709 ]
709 ]
@@ -1,103 +1,103 b''
1 from IPython.testing.tools import AssertPrints, AssertNotPrints
1 from IPython.testing.tools import AssertPrints, AssertNotPrints
2
2
3 ip = get_ipython()
3 ip = get_ipython()
4
4
5 def test_output_displayed():
5 def test_output_displayed():
6 """Checking to make sure that output is displayed"""
6 """Checking to make sure that output is displayed"""
7
7
8 with AssertPrints('2'):
8 with AssertPrints('2'):
9 ip.run_cell('1+1', store_history=True)
9 ip.run_cell('1+1', store_history=True)
10
10
11 with AssertPrints('2'):
11 with AssertPrints('2'):
12 ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
12 ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
13
13
14 with AssertPrints('2'):
14 with AssertPrints('2'):
15 ip.run_cell('1+1\n#commented_out_function();', store_history=True)
15 ip.run_cell('1+1\n#commented_out_function();', store_history=True)
16
16
17
17
18 def test_output_quiet():
18 def test_output_quiet():
19 """Checking to make sure that output is quiet"""
19 """Checking to make sure that output is quiet"""
20
20
21 with AssertNotPrints('2'):
21 with AssertNotPrints('2'):
22 ip.run_cell('1+1;', store_history=True)
22 ip.run_cell('1+1;', store_history=True)
23
23
24 with AssertNotPrints('2'):
24 with AssertNotPrints('2'):
25 ip.run_cell('1+1; # comment with a semicolon', store_history=True)
25 ip.run_cell('1+1; # comment with a semicolon', store_history=True)
26
26
27 with AssertNotPrints('2'):
27 with AssertNotPrints('2'):
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
29
29
30 def test_underscore_no_overrite_user():
30 def test_underscore_no_overrite_user():
31 ip.run_cell('_ = 42', store_history=True)
31 ip.run_cell('_ = 42', store_history=True)
32 ip.run_cell('1+1', store_history=True)
32 ip.run_cell('1+1', store_history=True)
33
33
34 with AssertPrints('42'):
34 with AssertPrints('42'):
35 ip.run_cell('print(_)', store_history=True)
35 ip.run_cell('print(_)', store_history=True)
36
36
37 ip.run_cell('del _', store_history=True)
37 ip.run_cell('del _', store_history=True)
38 ip.run_cell('6+6', store_history=True)
38 ip.run_cell('6+6', store_history=True)
39 with AssertPrints('12'):
39 with AssertPrints('12'):
40 ip.run_cell('_', store_history=True)
40 ip.run_cell('_', store_history=True)
41
41
42
42
43 def test_underscore_no_overrite_builtins():
43 def test_underscore_no_overrite_builtins():
44 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
44 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
45 ip.run_cell('3+3', store_history=True)
45 ip.run_cell('3+3', store_history=True)
46
46
47 with AssertPrints('gettext'):
47 with AssertPrints('gettext'):
48 ip.run_cell('print(_)', store_history=True)
48 ip.run_cell('print(_)', store_history=True)
49
49
50 ip.run_cell('_ = "userset"', store_history=True)
50 ip.run_cell('_ = "userset"', store_history=True)
51
51
52 with AssertPrints('userset'):
52 with AssertPrints('userset'):
53 ip.run_cell('print(_)', store_history=True)
53 ip.run_cell('print(_)', store_history=True)
54 ip.run_cell('import builtins; del builtins._')
54 ip.run_cell('import builtins; del builtins._')
55
55
56
56
57 def test_interactivehooks_ast_modes():
57 def test_interactivehooks_ast_modes():
58 """
58 """
59 Test that ast nodes can be triggerd with different modes
59 Test that ast nodes can be triggered with different modes
60 """
60 """
61 saved_mode = ip.ast_node_interactivity
61 saved_mode = ip.ast_node_interactivity
62 ip.ast_node_interactivity = 'last_expr_or_assign'
62 ip.ast_node_interactivity = 'last_expr_or_assign'
63
63
64 try:
64 try:
65 with AssertPrints('2'):
65 with AssertPrints('2'):
66 ip.run_cell('a = 1+1', store_history=True)
66 ip.run_cell('a = 1+1', store_history=True)
67
67
68 with AssertPrints('9'):
68 with AssertPrints('9'):
69 ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)
69 ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)
70
70
71 with AssertPrints('7'):
71 with AssertPrints('7'):
72 ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)
72 ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)
73
73
74 ip.run_cell('d = 11', store_history=True)
74 ip.run_cell('d = 11', store_history=True)
75 with AssertPrints('12'):
75 with AssertPrints('12'):
76 ip.run_cell('d += 1', store_history=True)
76 ip.run_cell('d += 1', store_history=True)
77
77
78 with AssertNotPrints('42'):
78 with AssertNotPrints('42'):
79 ip.run_cell('(u,v) = (41+1, 43-1)')
79 ip.run_cell('(u,v) = (41+1, 43-1)')
80
80
81 finally:
81 finally:
82 ip.ast_node_interactivity = saved_mode
82 ip.ast_node_interactivity = saved_mode
83
83
84 def test_interactivehooks_ast_modes_semi_supress():
84 def test_interactivehooks_ast_modes_semi_supress():
85 """
85 """
86 Test that ast nodes can be triggerd with different modes and supressed
86 Test that ast nodes can be triggered with different modes and suppressed
87 by semicolon
87 by semicolon
88 """
88 """
89 saved_mode = ip.ast_node_interactivity
89 saved_mode = ip.ast_node_interactivity
90 ip.ast_node_interactivity = 'last_expr_or_assign'
90 ip.ast_node_interactivity = 'last_expr_or_assign'
91
91
92 try:
92 try:
93 with AssertNotPrints('2'):
93 with AssertNotPrints('2'):
94 ip.run_cell('x = 1+1;', store_history=True)
94 ip.run_cell('x = 1+1;', store_history=True)
95
95
96 with AssertNotPrints('7'):
96 with AssertNotPrints('7'):
97 ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True)
97 ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True)
98
98
99 with AssertNotPrints('9'):
99 with AssertNotPrints('9'):
100 ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True)
100 ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True)
101
101
102 finally:
102 finally:
103 ip.ast_node_interactivity = saved_mode
103 ip.ast_node_interactivity = saved_mode
@@ -1,1073 +1,1073 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6
6
7 import io
7 import io
8 import os
8 import os
9 import re
9 import re
10 import sys
10 import sys
11 import warnings
11 import warnings
12 from unittest import TestCase
12 from unittest import TestCase
13 from importlib import invalidate_caches
13 from importlib import invalidate_caches
14 from io import StringIO
14 from io import StringIO
15
15
16 import nose.tools as nt
16 import nose.tools as nt
17
17
18 import shlex
18 import shlex
19
19
20 from IPython import get_ipython
20 from IPython import get_ipython
21 from IPython.core import magic
21 from IPython.core import magic
22 from IPython.core.error import UsageError
22 from IPython.core.error import UsageError
23 from IPython.core.magic import (Magics, magics_class, line_magic,
23 from IPython.core.magic import (Magics, magics_class, line_magic,
24 cell_magic,
24 cell_magic,
25 register_line_magic, register_cell_magic)
25 register_line_magic, register_cell_magic)
26 from IPython.core.magics import execution, script, code, logging
26 from IPython.core.magics import execution, script, code, logging
27 from IPython.testing import decorators as dec
27 from IPython.testing import decorators as dec
28 from IPython.testing import tools as tt
28 from IPython.testing import tools as tt
29 from IPython.utils import py3compat
29 from IPython.utils import py3compat
30 from IPython.utils.io import capture_output
30 from IPython.utils.io import capture_output
31 from IPython.utils.tempdir import TemporaryDirectory
31 from IPython.utils.tempdir import TemporaryDirectory
32 from IPython.utils.process import find_cmd
32 from IPython.utils.process import find_cmd
33
33
34
34
35
35
36 _ip = get_ipython()
36 _ip = get_ipython()
37
37
38 @magic.magics_class
38 @magic.magics_class
39 class DummyMagics(magic.Magics): pass
39 class DummyMagics(magic.Magics): pass
40
40
41 def test_extract_code_ranges():
41 def test_extract_code_ranges():
42 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
42 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
43 expected = [(0, 1),
43 expected = [(0, 1),
44 (2, 3),
44 (2, 3),
45 (4, 6),
45 (4, 6),
46 (6, 9),
46 (6, 9),
47 (9, 14),
47 (9, 14),
48 (16, None),
48 (16, None),
49 (None, 9),
49 (None, 9),
50 (9, None),
50 (9, None),
51 (None, 13),
51 (None, 13),
52 (None, None)]
52 (None, None)]
53 actual = list(code.extract_code_ranges(instr))
53 actual = list(code.extract_code_ranges(instr))
54 nt.assert_equal(actual, expected)
54 nt.assert_equal(actual, expected)
55
55
56 def test_extract_symbols():
56 def test_extract_symbols():
57 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
57 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
58 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
58 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
59 expected = [([], ['a']),
59 expected = [([], ['a']),
60 (["def b():\n return 42\n"], []),
60 (["def b():\n return 42\n"], []),
61 (["class A: pass\n"], []),
61 (["class A: pass\n"], []),
62 (["class A: pass\n", "def b():\n return 42\n"], []),
62 (["class A: pass\n", "def b():\n return 42\n"], []),
63 (["class A: pass\n"], ['a']),
63 (["class A: pass\n"], ['a']),
64 ([], ['z'])]
64 ([], ['z'])]
65 for symbols, exp in zip(symbols_args, expected):
65 for symbols, exp in zip(symbols_args, expected):
66 nt.assert_equal(code.extract_symbols(source, symbols), exp)
66 nt.assert_equal(code.extract_symbols(source, symbols), exp)
67
67
68
68
69 def test_extract_symbols_raises_exception_with_non_python_code():
69 def test_extract_symbols_raises_exception_with_non_python_code():
70 source = ("=begin A Ruby program :)=end\n"
70 source = ("=begin A Ruby program :)=end\n"
71 "def hello\n"
71 "def hello\n"
72 "puts 'Hello world'\n"
72 "puts 'Hello world'\n"
73 "end")
73 "end")
74 with nt.assert_raises(SyntaxError):
74 with nt.assert_raises(SyntaxError):
75 code.extract_symbols(source, "hello")
75 code.extract_symbols(source, "hello")
76
76
77
77
78 def test_magic_not_found():
78 def test_magic_not_found():
79 # magic not found raises UsageError
79 # magic not found raises UsageError
80 with nt.assert_raises(UsageError):
80 with nt.assert_raises(UsageError):
81 _ip.magic('doesntexist')
81 _ip.magic('doesntexist')
82
82
83 # ensure result isn't success when a magic isn't found
83 # ensure result isn't success when a magic isn't found
84 result = _ip.run_cell('%doesntexist')
84 result = _ip.run_cell('%doesntexist')
85 assert isinstance(result.error_in_exec, UsageError)
85 assert isinstance(result.error_in_exec, UsageError)
86
86
87
87
88 def test_cell_magic_not_found():
88 def test_cell_magic_not_found():
89 # magic not found raises UsageError
89 # magic not found raises UsageError
90 with nt.assert_raises(UsageError):
90 with nt.assert_raises(UsageError):
91 _ip.run_cell_magic('doesntexist', 'line', 'cell')
91 _ip.run_cell_magic('doesntexist', 'line', 'cell')
92
92
93 # ensure result isn't success when a magic isn't found
93 # ensure result isn't success when a magic isn't found
94 result = _ip.run_cell('%%doesntexist')
94 result = _ip.run_cell('%%doesntexist')
95 assert isinstance(result.error_in_exec, UsageError)
95 assert isinstance(result.error_in_exec, UsageError)
96
96
97
97
98 def test_magic_error_status():
98 def test_magic_error_status():
99 def fail(shell):
99 def fail(shell):
100 1/0
100 1/0
101 _ip.register_magic_function(fail)
101 _ip.register_magic_function(fail)
102 result = _ip.run_cell('%fail')
102 result = _ip.run_cell('%fail')
103 assert isinstance(result.error_in_exec, ZeroDivisionError)
103 assert isinstance(result.error_in_exec, ZeroDivisionError)
104
104
105
105
106 def test_config():
106 def test_config():
107 """ test that config magic does not raise
107 """ test that config magic does not raise
108 can happen if Configurable init is moved too early into
108 can happen if Configurable init is moved too early into
109 Magics.__init__ as then a Config object will be registerd as a
109 Magics.__init__ as then a Config object will be registered as a
110 magic.
110 magic.
111 """
111 """
112 ## should not raise.
112 ## should not raise.
113 _ip.magic('config')
113 _ip.magic('config')
114
114
115 def test_config_available_configs():
115 def test_config_available_configs():
116 """ test that config magic prints available configs in unique and
116 """ test that config magic prints available configs in unique and
117 sorted order. """
117 sorted order. """
118 with capture_output() as captured:
118 with capture_output() as captured:
119 _ip.magic('config')
119 _ip.magic('config')
120
120
121 stdout = captured.stdout
121 stdout = captured.stdout
122 config_classes = stdout.strip().split('\n')[1:]
122 config_classes = stdout.strip().split('\n')[1:]
123 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
123 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
124
124
125 def test_config_print_class():
125 def test_config_print_class():
126 """ test that config with a classname prints the class's options. """
126 """ test that config with a classname prints the class's options. """
127 with capture_output() as captured:
127 with capture_output() as captured:
128 _ip.magic('config TerminalInteractiveShell')
128 _ip.magic('config TerminalInteractiveShell')
129
129
130 stdout = captured.stdout
130 stdout = captured.stdout
131 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
131 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
132 print(stdout)
132 print(stdout)
133 raise AssertionError("1st line of stdout not like "
133 raise AssertionError("1st line of stdout not like "
134 "'TerminalInteractiveShell.* options'")
134 "'TerminalInteractiveShell.* options'")
135
135
136 def test_rehashx():
136 def test_rehashx():
137 # clear up everything
137 # clear up everything
138 _ip.alias_manager.clear_aliases()
138 _ip.alias_manager.clear_aliases()
139 del _ip.db['syscmdlist']
139 del _ip.db['syscmdlist']
140
140
141 _ip.magic('rehashx')
141 _ip.magic('rehashx')
142 # Practically ALL ipython development systems will have more than 10 aliases
142 # Practically ALL ipython development systems will have more than 10 aliases
143
143
144 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
144 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
145 for name, cmd in _ip.alias_manager.aliases:
145 for name, cmd in _ip.alias_manager.aliases:
146 # we must strip dots from alias names
146 # we must strip dots from alias names
147 nt.assert_not_in('.', name)
147 nt.assert_not_in('.', name)
148
148
149 # rehashx must fill up syscmdlist
149 # rehashx must fill up syscmdlist
150 scoms = _ip.db['syscmdlist']
150 scoms = _ip.db['syscmdlist']
151 nt.assert_true(len(scoms) > 10)
151 nt.assert_true(len(scoms) > 10)
152
152
153
153
154 def test_magic_parse_options():
154 def test_magic_parse_options():
155 """Test that we don't mangle paths when parsing magic options."""
155 """Test that we don't mangle paths when parsing magic options."""
156 ip = get_ipython()
156 ip = get_ipython()
157 path = 'c:\\x'
157 path = 'c:\\x'
158 m = DummyMagics(ip)
158 m = DummyMagics(ip)
159 opts = m.parse_options('-f %s' % path,'f:')[0]
159 opts = m.parse_options('-f %s' % path,'f:')[0]
160 # argv splitting is os-dependent
160 # argv splitting is os-dependent
161 if os.name == 'posix':
161 if os.name == 'posix':
162 expected = 'c:x'
162 expected = 'c:x'
163 else:
163 else:
164 expected = path
164 expected = path
165 nt.assert_equal(opts['f'], expected)
165 nt.assert_equal(opts['f'], expected)
166
166
167 def test_magic_parse_long_options():
167 def test_magic_parse_long_options():
168 """Magic.parse_options can handle --foo=bar long options"""
168 """Magic.parse_options can handle --foo=bar long options"""
169 ip = get_ipython()
169 ip = get_ipython()
170 m = DummyMagics(ip)
170 m = DummyMagics(ip)
171 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
171 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
172 nt.assert_in('foo', opts)
172 nt.assert_in('foo', opts)
173 nt.assert_in('bar', opts)
173 nt.assert_in('bar', opts)
174 nt.assert_equal(opts['bar'], "bubble")
174 nt.assert_equal(opts['bar'], "bubble")
175
175
176
176
177 @dec.skip_without('sqlite3')
177 @dec.skip_without('sqlite3')
178 def doctest_hist_f():
178 def doctest_hist_f():
179 """Test %hist -f with temporary filename.
179 """Test %hist -f with temporary filename.
180
180
181 In [9]: import tempfile
181 In [9]: import tempfile
182
182
183 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
183 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
184
184
185 In [11]: %hist -nl -f $tfile 3
185 In [11]: %hist -nl -f $tfile 3
186
186
187 In [13]: import os; os.unlink(tfile)
187 In [13]: import os; os.unlink(tfile)
188 """
188 """
189
189
190
190
191 @dec.skip_without('sqlite3')
191 @dec.skip_without('sqlite3')
192 def doctest_hist_r():
192 def doctest_hist_r():
193 """Test %hist -r
193 """Test %hist -r
194
194
195 XXX - This test is not recording the output correctly. For some reason, in
195 XXX - This test is not recording the output correctly. For some reason, in
196 testing mode the raw history isn't getting populated. No idea why.
196 testing mode the raw history isn't getting populated. No idea why.
197 Disabling the output checking for now, though at least we do run it.
197 Disabling the output checking for now, though at least we do run it.
198
198
199 In [1]: 'hist' in _ip.lsmagic()
199 In [1]: 'hist' in _ip.lsmagic()
200 Out[1]: True
200 Out[1]: True
201
201
202 In [2]: x=1
202 In [2]: x=1
203
203
204 In [3]: %hist -rl 2
204 In [3]: %hist -rl 2
205 x=1 # random
205 x=1 # random
206 %hist -r 2
206 %hist -r 2
207 """
207 """
208
208
209
209
210 @dec.skip_without('sqlite3')
210 @dec.skip_without('sqlite3')
211 def doctest_hist_op():
211 def doctest_hist_op():
212 """Test %hist -op
212 """Test %hist -op
213
213
214 In [1]: class b(float):
214 In [1]: class b(float):
215 ...: pass
215 ...: pass
216 ...:
216 ...:
217
217
218 In [2]: class s(object):
218 In [2]: class s(object):
219 ...: def __str__(self):
219 ...: def __str__(self):
220 ...: return 's'
220 ...: return 's'
221 ...:
221 ...:
222
222
223 In [3]:
223 In [3]:
224
224
225 In [4]: class r(b):
225 In [4]: class r(b):
226 ...: def __repr__(self):
226 ...: def __repr__(self):
227 ...: return 'r'
227 ...: return 'r'
228 ...:
228 ...:
229
229
230 In [5]: class sr(s,r): pass
230 In [5]: class sr(s,r): pass
231 ...:
231 ...:
232
232
233 In [6]:
233 In [6]:
234
234
235 In [7]: bb=b()
235 In [7]: bb=b()
236
236
237 In [8]: ss=s()
237 In [8]: ss=s()
238
238
239 In [9]: rr=r()
239 In [9]: rr=r()
240
240
241 In [10]: ssrr=sr()
241 In [10]: ssrr=sr()
242
242
243 In [11]: 4.5
243 In [11]: 4.5
244 Out[11]: 4.5
244 Out[11]: 4.5
245
245
246 In [12]: str(ss)
246 In [12]: str(ss)
247 Out[12]: 's'
247 Out[12]: 's'
248
248
249 In [13]:
249 In [13]:
250
250
251 In [14]: %hist -op
251 In [14]: %hist -op
252 >>> class b:
252 >>> class b:
253 ... pass
253 ... pass
254 ...
254 ...
255 >>> class s(b):
255 >>> class s(b):
256 ... def __str__(self):
256 ... def __str__(self):
257 ... return 's'
257 ... return 's'
258 ...
258 ...
259 >>>
259 >>>
260 >>> class r(b):
260 >>> class r(b):
261 ... def __repr__(self):
261 ... def __repr__(self):
262 ... return 'r'
262 ... return 'r'
263 ...
263 ...
264 >>> class sr(s,r): pass
264 >>> class sr(s,r): pass
265 >>>
265 >>>
266 >>> bb=b()
266 >>> bb=b()
267 >>> ss=s()
267 >>> ss=s()
268 >>> rr=r()
268 >>> rr=r()
269 >>> ssrr=sr()
269 >>> ssrr=sr()
270 >>> 4.5
270 >>> 4.5
271 4.5
271 4.5
272 >>> str(ss)
272 >>> str(ss)
273 's'
273 's'
274 >>>
274 >>>
275 """
275 """
276
276
277 def test_hist_pof():
277 def test_hist_pof():
278 ip = get_ipython()
278 ip = get_ipython()
279 ip.run_cell(u"1+2", store_history=True)
279 ip.run_cell(u"1+2", store_history=True)
280 #raise Exception(ip.history_manager.session_number)
280 #raise Exception(ip.history_manager.session_number)
281 #raise Exception(list(ip.history_manager._get_range_session()))
281 #raise Exception(list(ip.history_manager._get_range_session()))
282 with TemporaryDirectory() as td:
282 with TemporaryDirectory() as td:
283 tf = os.path.join(td, 'hist.py')
283 tf = os.path.join(td, 'hist.py')
284 ip.run_line_magic('history', '-pof %s' % tf)
284 ip.run_line_magic('history', '-pof %s' % tf)
285 assert os.path.isfile(tf)
285 assert os.path.isfile(tf)
286
286
287
287
288 @dec.skip_without('sqlite3')
288 @dec.skip_without('sqlite3')
289 def test_macro():
289 def test_macro():
290 ip = get_ipython()
290 ip = get_ipython()
291 ip.history_manager.reset() # Clear any existing history.
291 ip.history_manager.reset() # Clear any existing history.
292 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
292 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
293 for i, cmd in enumerate(cmds, start=1):
293 for i, cmd in enumerate(cmds, start=1):
294 ip.history_manager.store_inputs(i, cmd)
294 ip.history_manager.store_inputs(i, cmd)
295 ip.magic("macro test 1-3")
295 ip.magic("macro test 1-3")
296 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
296 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
297
297
298 # List macros
298 # List macros
299 nt.assert_in("test", ip.magic("macro"))
299 nt.assert_in("test", ip.magic("macro"))
300
300
301
301
302 @dec.skip_without('sqlite3')
302 @dec.skip_without('sqlite3')
303 def test_macro_run():
303 def test_macro_run():
304 """Test that we can run a multi-line macro successfully."""
304 """Test that we can run a multi-line macro successfully."""
305 ip = get_ipython()
305 ip = get_ipython()
306 ip.history_manager.reset()
306 ip.history_manager.reset()
307 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
307 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
308 "%macro test 2-3"]
308 "%macro test 2-3"]
309 for cmd in cmds:
309 for cmd in cmds:
310 ip.run_cell(cmd, store_history=True)
310 ip.run_cell(cmd, store_history=True)
311 nt.assert_equal(ip.user_ns["test"].value,
311 nt.assert_equal(ip.user_ns["test"].value,
312 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
312 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
313 with tt.AssertPrints("12"):
313 with tt.AssertPrints("12"):
314 ip.run_cell("test")
314 ip.run_cell("test")
315 with tt.AssertPrints("13"):
315 with tt.AssertPrints("13"):
316 ip.run_cell("test")
316 ip.run_cell("test")
317
317
318
318
319 def test_magic_magic():
319 def test_magic_magic():
320 """Test %magic"""
320 """Test %magic"""
321 ip = get_ipython()
321 ip = get_ipython()
322 with capture_output() as captured:
322 with capture_output() as captured:
323 ip.magic("magic")
323 ip.magic("magic")
324
324
325 stdout = captured.stdout
325 stdout = captured.stdout
326 nt.assert_in('%magic', stdout)
326 nt.assert_in('%magic', stdout)
327 nt.assert_in('IPython', stdout)
327 nt.assert_in('IPython', stdout)
328 nt.assert_in('Available', stdout)
328 nt.assert_in('Available', stdout)
329
329
330
330
331 @dec.skipif_not_numpy
331 @dec.skipif_not_numpy
332 def test_numpy_reset_array_undec():
332 def test_numpy_reset_array_undec():
333 "Test '%reset array' functionality"
333 "Test '%reset array' functionality"
334 _ip.ex('import numpy as np')
334 _ip.ex('import numpy as np')
335 _ip.ex('a = np.empty(2)')
335 _ip.ex('a = np.empty(2)')
336 nt.assert_in('a', _ip.user_ns)
336 nt.assert_in('a', _ip.user_ns)
337 _ip.magic('reset -f array')
337 _ip.magic('reset -f array')
338 nt.assert_not_in('a', _ip.user_ns)
338 nt.assert_not_in('a', _ip.user_ns)
339
339
340 def test_reset_out():
340 def test_reset_out():
341 "Test '%reset out' magic"
341 "Test '%reset out' magic"
342 _ip.run_cell("parrot = 'dead'", store_history=True)
342 _ip.run_cell("parrot = 'dead'", store_history=True)
343 # test '%reset -f out', make an Out prompt
343 # test '%reset -f out', make an Out prompt
344 _ip.run_cell("parrot", store_history=True)
344 _ip.run_cell("parrot", store_history=True)
345 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
345 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
346 _ip.magic('reset -f out')
346 _ip.magic('reset -f out')
347 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
347 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
348 nt.assert_equal(len(_ip.user_ns['Out']), 0)
348 nt.assert_equal(len(_ip.user_ns['Out']), 0)
349
349
350 def test_reset_in():
350 def test_reset_in():
351 "Test '%reset in' magic"
351 "Test '%reset in' magic"
352 # test '%reset -f in'
352 # test '%reset -f in'
353 _ip.run_cell("parrot", store_history=True)
353 _ip.run_cell("parrot", store_history=True)
354 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
354 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
355 _ip.magic('%reset -f in')
355 _ip.magic('%reset -f in')
356 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
356 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
357 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
357 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
358
358
359 def test_reset_dhist():
359 def test_reset_dhist():
360 "Test '%reset dhist' magic"
360 "Test '%reset dhist' magic"
361 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
361 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
362 _ip.magic('cd ' + os.path.dirname(nt.__file__))
362 _ip.magic('cd ' + os.path.dirname(nt.__file__))
363 _ip.magic('cd -')
363 _ip.magic('cd -')
364 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
364 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
365 _ip.magic('reset -f dhist')
365 _ip.magic('reset -f dhist')
366 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
366 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
367 _ip.run_cell("_dh = [d for d in tmp]") #restore
367 _ip.run_cell("_dh = [d for d in tmp]") #restore
368
368
369 def test_reset_in_length():
369 def test_reset_in_length():
370 "Test that '%reset in' preserves In[] length"
370 "Test that '%reset in' preserves In[] length"
371 _ip.run_cell("print 'foo'")
371 _ip.run_cell("print 'foo'")
372 _ip.run_cell("reset -f in")
372 _ip.run_cell("reset -f in")
373 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
373 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
374
374
375 def test_tb_syntaxerror():
375 def test_tb_syntaxerror():
376 """test %tb after a SyntaxError"""
376 """test %tb after a SyntaxError"""
377 ip = get_ipython()
377 ip = get_ipython()
378 ip.run_cell("for")
378 ip.run_cell("for")
379
379
380 # trap and validate stdout
380 # trap and validate stdout
381 save_stdout = sys.stdout
381 save_stdout = sys.stdout
382 try:
382 try:
383 sys.stdout = StringIO()
383 sys.stdout = StringIO()
384 ip.run_cell("%tb")
384 ip.run_cell("%tb")
385 out = sys.stdout.getvalue()
385 out = sys.stdout.getvalue()
386 finally:
386 finally:
387 sys.stdout = save_stdout
387 sys.stdout = save_stdout
388 # trim output, and only check the last line
388 # trim output, and only check the last line
389 last_line = out.rstrip().splitlines()[-1].strip()
389 last_line = out.rstrip().splitlines()[-1].strip()
390 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
390 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
391
391
392
392
393 def test_time():
393 def test_time():
394 ip = get_ipython()
394 ip = get_ipython()
395
395
396 with tt.AssertPrints("Wall time: "):
396 with tt.AssertPrints("Wall time: "):
397 ip.run_cell("%time None")
397 ip.run_cell("%time None")
398
398
399 ip.run_cell("def f(kmjy):\n"
399 ip.run_cell("def f(kmjy):\n"
400 " %time print (2*kmjy)")
400 " %time print (2*kmjy)")
401
401
402 with tt.AssertPrints("Wall time: "):
402 with tt.AssertPrints("Wall time: "):
403 with tt.AssertPrints("hihi", suppress=False):
403 with tt.AssertPrints("hihi", suppress=False):
404 ip.run_cell("f('hi')")
404 ip.run_cell("f('hi')")
405
405
406
406
407 @dec.skip_win32
407 @dec.skip_win32
408 def test_time2():
408 def test_time2():
409 ip = get_ipython()
409 ip = get_ipython()
410
410
411 with tt.AssertPrints("CPU times: user "):
411 with tt.AssertPrints("CPU times: user "):
412 ip.run_cell("%time None")
412 ip.run_cell("%time None")
413
413
414 def test_time3():
414 def test_time3():
415 """Erroneous magic function calls, issue gh-3334"""
415 """Erroneous magic function calls, issue gh-3334"""
416 ip = get_ipython()
416 ip = get_ipython()
417 ip.user_ns.pop('run', None)
417 ip.user_ns.pop('run', None)
418
418
419 with tt.AssertNotPrints("not found", channel='stderr'):
419 with tt.AssertNotPrints("not found", channel='stderr'):
420 ip.run_cell("%%time\n"
420 ip.run_cell("%%time\n"
421 "run = 0\n"
421 "run = 0\n"
422 "run += 1")
422 "run += 1")
423
423
424 def test_doctest_mode():
424 def test_doctest_mode():
425 "Toggle doctest_mode twice, it should be a no-op and run without error"
425 "Toggle doctest_mode twice, it should be a no-op and run without error"
426 _ip.magic('doctest_mode')
426 _ip.magic('doctest_mode')
427 _ip.magic('doctest_mode')
427 _ip.magic('doctest_mode')
428
428
429
429
430 def test_parse_options():
430 def test_parse_options():
431 """Tests for basic options parsing in magics."""
431 """Tests for basic options parsing in magics."""
432 # These are only the most minimal of tests, more should be added later. At
432 # These are only the most minimal of tests, more should be added later. At
433 # the very least we check that basic text/unicode calls work OK.
433 # the very least we check that basic text/unicode calls work OK.
434 m = DummyMagics(_ip)
434 m = DummyMagics(_ip)
435 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
435 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
436 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
436 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
437
437
438
438
439 def test_dirops():
439 def test_dirops():
440 """Test various directory handling operations."""
440 """Test various directory handling operations."""
441 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
441 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
442 curpath = os.getcwd
442 curpath = os.getcwd
443 startdir = os.getcwd()
443 startdir = os.getcwd()
444 ipdir = os.path.realpath(_ip.ipython_dir)
444 ipdir = os.path.realpath(_ip.ipython_dir)
445 try:
445 try:
446 _ip.magic('cd "%s"' % ipdir)
446 _ip.magic('cd "%s"' % ipdir)
447 nt.assert_equal(curpath(), ipdir)
447 nt.assert_equal(curpath(), ipdir)
448 _ip.magic('cd -')
448 _ip.magic('cd -')
449 nt.assert_equal(curpath(), startdir)
449 nt.assert_equal(curpath(), startdir)
450 _ip.magic('pushd "%s"' % ipdir)
450 _ip.magic('pushd "%s"' % ipdir)
451 nt.assert_equal(curpath(), ipdir)
451 nt.assert_equal(curpath(), ipdir)
452 _ip.magic('popd')
452 _ip.magic('popd')
453 nt.assert_equal(curpath(), startdir)
453 nt.assert_equal(curpath(), startdir)
454 finally:
454 finally:
455 os.chdir(startdir)
455 os.chdir(startdir)
456
456
457
457
458 def test_xmode():
458 def test_xmode():
459 # Calling xmode three times should be a no-op
459 # Calling xmode three times should be a no-op
460 xmode = _ip.InteractiveTB.mode
460 xmode = _ip.InteractiveTB.mode
461 for i in range(3):
461 for i in range(3):
462 _ip.magic("xmode")
462 _ip.magic("xmode")
463 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
463 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
464
464
465 def test_reset_hard():
465 def test_reset_hard():
466 monitor = []
466 monitor = []
467 class A(object):
467 class A(object):
468 def __del__(self):
468 def __del__(self):
469 monitor.append(1)
469 monitor.append(1)
470 def __repr__(self):
470 def __repr__(self):
471 return "<A instance>"
471 return "<A instance>"
472
472
473 _ip.user_ns["a"] = A()
473 _ip.user_ns["a"] = A()
474 _ip.run_cell("a")
474 _ip.run_cell("a")
475
475
476 nt.assert_equal(monitor, [])
476 nt.assert_equal(monitor, [])
477 _ip.magic("reset -f")
477 _ip.magic("reset -f")
478 nt.assert_equal(monitor, [1])
478 nt.assert_equal(monitor, [1])
479
479
480 class TestXdel(tt.TempFileMixin):
480 class TestXdel(tt.TempFileMixin):
481 def test_xdel(self):
481 def test_xdel(self):
482 """Test that references from %run are cleared by xdel."""
482 """Test that references from %run are cleared by xdel."""
483 src = ("class A(object):\n"
483 src = ("class A(object):\n"
484 " monitor = []\n"
484 " monitor = []\n"
485 " def __del__(self):\n"
485 " def __del__(self):\n"
486 " self.monitor.append(1)\n"
486 " self.monitor.append(1)\n"
487 "a = A()\n")
487 "a = A()\n")
488 self.mktmp(src)
488 self.mktmp(src)
489 # %run creates some hidden references...
489 # %run creates some hidden references...
490 _ip.magic("run %s" % self.fname)
490 _ip.magic("run %s" % self.fname)
491 # ... as does the displayhook.
491 # ... as does the displayhook.
492 _ip.run_cell("a")
492 _ip.run_cell("a")
493
493
494 monitor = _ip.user_ns["A"].monitor
494 monitor = _ip.user_ns["A"].monitor
495 nt.assert_equal(monitor, [])
495 nt.assert_equal(monitor, [])
496
496
497 _ip.magic("xdel a")
497 _ip.magic("xdel a")
498
498
499 # Check that a's __del__ method has been called.
499 # Check that a's __del__ method has been called.
500 nt.assert_equal(monitor, [1])
500 nt.assert_equal(monitor, [1])
501
501
502 def doctest_who():
502 def doctest_who():
503 """doctest for %who
503 """doctest for %who
504
504
505 In [1]: %reset -f
505 In [1]: %reset -f
506
506
507 In [2]: alpha = 123
507 In [2]: alpha = 123
508
508
509 In [3]: beta = 'beta'
509 In [3]: beta = 'beta'
510
510
511 In [4]: %who int
511 In [4]: %who int
512 alpha
512 alpha
513
513
514 In [5]: %who str
514 In [5]: %who str
515 beta
515 beta
516
516
517 In [6]: %whos
517 In [6]: %whos
518 Variable Type Data/Info
518 Variable Type Data/Info
519 ----------------------------
519 ----------------------------
520 alpha int 123
520 alpha int 123
521 beta str beta
521 beta str beta
522
522
523 In [7]: %who_ls
523 In [7]: %who_ls
524 Out[7]: ['alpha', 'beta']
524 Out[7]: ['alpha', 'beta']
525 """
525 """
526
526
527 def test_whos():
527 def test_whos():
528 """Check that whos is protected against objects where repr() fails."""
528 """Check that whos is protected against objects where repr() fails."""
529 class A(object):
529 class A(object):
530 def __repr__(self):
530 def __repr__(self):
531 raise Exception()
531 raise Exception()
532 _ip.user_ns['a'] = A()
532 _ip.user_ns['a'] = A()
533 _ip.magic("whos")
533 _ip.magic("whos")
534
534
535 @py3compat.u_format
535 @py3compat.u_format
536 def doctest_precision():
536 def doctest_precision():
537 """doctest for %precision
537 """doctest for %precision
538
538
539 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
539 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
540
540
541 In [2]: %precision 5
541 In [2]: %precision 5
542 Out[2]: '%.5f'
542 Out[2]: '%.5f'
543
543
544 In [3]: f.float_format
544 In [3]: f.float_format
545 Out[3]: '%.5f'
545 Out[3]: '%.5f'
546
546
547 In [4]: %precision %e
547 In [4]: %precision %e
548 Out[4]: '%e'
548 Out[4]: '%e'
549
549
550 In [5]: f(3.1415927)
550 In [5]: f(3.1415927)
551 Out[5]: '3.141593e+00'
551 Out[5]: '3.141593e+00'
552 """
552 """
553
553
554 def test_psearch():
554 def test_psearch():
555 with tt.AssertPrints("dict.fromkeys"):
555 with tt.AssertPrints("dict.fromkeys"):
556 _ip.run_cell("dict.fr*?")
556 _ip.run_cell("dict.fr*?")
557
557
558 def test_timeit_shlex():
558 def test_timeit_shlex():
559 """test shlex issues with timeit (#1109)"""
559 """test shlex issues with timeit (#1109)"""
560 _ip.ex("def f(*a,**kw): pass")
560 _ip.ex("def f(*a,**kw): pass")
561 _ip.magic('timeit -n1 "this is a bug".count(" ")')
561 _ip.magic('timeit -n1 "this is a bug".count(" ")')
562 _ip.magic('timeit -r1 -n1 f(" ", 1)')
562 _ip.magic('timeit -r1 -n1 f(" ", 1)')
563 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
563 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
564 _ip.magic('timeit -r1 -n1 ("a " + "b")')
564 _ip.magic('timeit -r1 -n1 ("a " + "b")')
565 _ip.magic('timeit -r1 -n1 f("a " + "b")')
565 _ip.magic('timeit -r1 -n1 f("a " + "b")')
566 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
566 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
567
567
568
568
569 def test_timeit_arguments():
569 def test_timeit_arguments():
570 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
570 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
571 _ip.magic("timeit ('#')")
571 _ip.magic("timeit ('#')")
572
572
573
573
574 def test_timeit_special_syntax():
574 def test_timeit_special_syntax():
575 "Test %%timeit with IPython special syntax"
575 "Test %%timeit with IPython special syntax"
576 @register_line_magic
576 @register_line_magic
577 def lmagic(line):
577 def lmagic(line):
578 ip = get_ipython()
578 ip = get_ipython()
579 ip.user_ns['lmagic_out'] = line
579 ip.user_ns['lmagic_out'] = line
580
580
581 # line mode test
581 # line mode test
582 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
582 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
583 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
583 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
584 # cell mode test
584 # cell mode test
585 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
585 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
586 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
586 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
587
587
588 def test_timeit_return():
588 def test_timeit_return():
589 """
589 """
590 test wether timeit -o return object
590 test whether timeit -o return object
591 """
591 """
592
592
593 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
593 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
594 assert(res is not None)
594 assert(res is not None)
595
595
596 def test_timeit_quiet():
596 def test_timeit_quiet():
597 """
597 """
598 test quiet option of timeit magic
598 test quiet option of timeit magic
599 """
599 """
600 with tt.AssertNotPrints("loops"):
600 with tt.AssertNotPrints("loops"):
601 _ip.run_cell("%timeit -n1 -r1 -q 1")
601 _ip.run_cell("%timeit -n1 -r1 -q 1")
602
602
603 def test_timeit_return_quiet():
603 def test_timeit_return_quiet():
604 with tt.AssertNotPrints("loops"):
604 with tt.AssertNotPrints("loops"):
605 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
605 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
606 assert (res is not None)
606 assert (res is not None)
607
607
608 def test_timeit_invalid_return():
608 def test_timeit_invalid_return():
609 with nt.assert_raises_regex(SyntaxError, "outside function"):
609 with nt.assert_raises_regex(SyntaxError, "outside function"):
610 _ip.run_line_magic('timeit', 'return')
610 _ip.run_line_magic('timeit', 'return')
611
611
612 @dec.skipif(execution.profile is None)
612 @dec.skipif(execution.profile is None)
613 def test_prun_special_syntax():
613 def test_prun_special_syntax():
614 "Test %%prun with IPython special syntax"
614 "Test %%prun with IPython special syntax"
615 @register_line_magic
615 @register_line_magic
616 def lmagic(line):
616 def lmagic(line):
617 ip = get_ipython()
617 ip = get_ipython()
618 ip.user_ns['lmagic_out'] = line
618 ip.user_ns['lmagic_out'] = line
619
619
620 # line mode test
620 # line mode test
621 _ip.run_line_magic('prun', '-q %lmagic my line')
621 _ip.run_line_magic('prun', '-q %lmagic my line')
622 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
622 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
623 # cell mode test
623 # cell mode test
624 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
624 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
625 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
625 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
626
626
627 @dec.skipif(execution.profile is None)
627 @dec.skipif(execution.profile is None)
628 def test_prun_quotes():
628 def test_prun_quotes():
629 "Test that prun does not clobber string escapes (GH #1302)"
629 "Test that prun does not clobber string escapes (GH #1302)"
630 _ip.magic(r"prun -q x = '\t'")
630 _ip.magic(r"prun -q x = '\t'")
631 nt.assert_equal(_ip.user_ns['x'], '\t')
631 nt.assert_equal(_ip.user_ns['x'], '\t')
632
632
633 def test_extension():
633 def test_extension():
634 # Debugging information for failures of this test
634 # Debugging information for failures of this test
635 print('sys.path:')
635 print('sys.path:')
636 for p in sys.path:
636 for p in sys.path:
637 print(' ', p)
637 print(' ', p)
638 print('CWD', os.getcwd())
638 print('CWD', os.getcwd())
639
639
640 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
640 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
641 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
641 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
642 sys.path.insert(0, daft_path)
642 sys.path.insert(0, daft_path)
643 try:
643 try:
644 _ip.user_ns.pop('arq', None)
644 _ip.user_ns.pop('arq', None)
645 invalidate_caches() # Clear import caches
645 invalidate_caches() # Clear import caches
646 _ip.magic("load_ext daft_extension")
646 _ip.magic("load_ext daft_extension")
647 nt.assert_equal(_ip.user_ns['arq'], 185)
647 nt.assert_equal(_ip.user_ns['arq'], 185)
648 _ip.magic("unload_ext daft_extension")
648 _ip.magic("unload_ext daft_extension")
649 assert 'arq' not in _ip.user_ns
649 assert 'arq' not in _ip.user_ns
650 finally:
650 finally:
651 sys.path.remove(daft_path)
651 sys.path.remove(daft_path)
652
652
653
653
654 def test_notebook_export_json():
654 def test_notebook_export_json():
655 _ip = get_ipython()
655 _ip = get_ipython()
656 _ip.history_manager.reset() # Clear any existing history.
656 _ip.history_manager.reset() # Clear any existing history.
657 cmds = [u"a=1", u"def b():\n return a**2", u"print('noël, été', b())"]
657 cmds = [u"a=1", u"def b():\n return a**2", u"print('noël, été', b())"]
658 for i, cmd in enumerate(cmds, start=1):
658 for i, cmd in enumerate(cmds, start=1):
659 _ip.history_manager.store_inputs(i, cmd)
659 _ip.history_manager.store_inputs(i, cmd)
660 with TemporaryDirectory() as td:
660 with TemporaryDirectory() as td:
661 outfile = os.path.join(td, "nb.ipynb")
661 outfile = os.path.join(td, "nb.ipynb")
662 _ip.magic("notebook -e %s" % outfile)
662 _ip.magic("notebook -e %s" % outfile)
663
663
664
664
665 class TestEnv(TestCase):
665 class TestEnv(TestCase):
666
666
667 def test_env(self):
667 def test_env(self):
668 env = _ip.magic("env")
668 env = _ip.magic("env")
669 self.assertTrue(isinstance(env, dict))
669 self.assertTrue(isinstance(env, dict))
670
670
671 def test_env_get_set_simple(self):
671 def test_env_get_set_simple(self):
672 env = _ip.magic("env var val1")
672 env = _ip.magic("env var val1")
673 self.assertEqual(env, None)
673 self.assertEqual(env, None)
674 self.assertEqual(os.environ['var'], 'val1')
674 self.assertEqual(os.environ['var'], 'val1')
675 self.assertEqual(_ip.magic("env var"), 'val1')
675 self.assertEqual(_ip.magic("env var"), 'val1')
676 env = _ip.magic("env var=val2")
676 env = _ip.magic("env var=val2")
677 self.assertEqual(env, None)
677 self.assertEqual(env, None)
678 self.assertEqual(os.environ['var'], 'val2')
678 self.assertEqual(os.environ['var'], 'val2')
679
679
680 def test_env_get_set_complex(self):
680 def test_env_get_set_complex(self):
681 env = _ip.magic("env var 'val1 '' 'val2")
681 env = _ip.magic("env var 'val1 '' 'val2")
682 self.assertEqual(env, None)
682 self.assertEqual(env, None)
683 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
683 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
684 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
684 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
685 env = _ip.magic('env var=val2 val3="val4')
685 env = _ip.magic('env var=val2 val3="val4')
686 self.assertEqual(env, None)
686 self.assertEqual(env, None)
687 self.assertEqual(os.environ['var'], 'val2 val3="val4')
687 self.assertEqual(os.environ['var'], 'val2 val3="val4')
688
688
689 def test_env_set_bad_input(self):
689 def test_env_set_bad_input(self):
690 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
690 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
691
691
692 def test_env_set_whitespace(self):
692 def test_env_set_whitespace(self):
693 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
693 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
694
694
695
695
696 class CellMagicTestCase(TestCase):
696 class CellMagicTestCase(TestCase):
697
697
698 def check_ident(self, magic):
698 def check_ident(self, magic):
699 # Manually called, we get the result
699 # Manually called, we get the result
700 out = _ip.run_cell_magic(magic, 'a', 'b')
700 out = _ip.run_cell_magic(magic, 'a', 'b')
701 nt.assert_equal(out, ('a','b'))
701 nt.assert_equal(out, ('a','b'))
702 # Via run_cell, it goes into the user's namespace via displayhook
702 # Via run_cell, it goes into the user's namespace via displayhook
703 _ip.run_cell('%%' + magic +' c\nd')
703 _ip.run_cell('%%' + magic +' c\nd')
704 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
704 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
705
705
706 def test_cell_magic_func_deco(self):
706 def test_cell_magic_func_deco(self):
707 "Cell magic using simple decorator"
707 "Cell magic using simple decorator"
708 @register_cell_magic
708 @register_cell_magic
709 def cellm(line, cell):
709 def cellm(line, cell):
710 return line, cell
710 return line, cell
711
711
712 self.check_ident('cellm')
712 self.check_ident('cellm')
713
713
714 def test_cell_magic_reg(self):
714 def test_cell_magic_reg(self):
715 "Cell magic manually registered"
715 "Cell magic manually registered"
716 def cellm(line, cell):
716 def cellm(line, cell):
717 return line, cell
717 return line, cell
718
718
719 _ip.register_magic_function(cellm, 'cell', 'cellm2')
719 _ip.register_magic_function(cellm, 'cell', 'cellm2')
720 self.check_ident('cellm2')
720 self.check_ident('cellm2')
721
721
722 def test_cell_magic_class(self):
722 def test_cell_magic_class(self):
723 "Cell magics declared via a class"
723 "Cell magics declared via a class"
724 @magics_class
724 @magics_class
725 class MyMagics(Magics):
725 class MyMagics(Magics):
726
726
727 @cell_magic
727 @cell_magic
728 def cellm3(self, line, cell):
728 def cellm3(self, line, cell):
729 return line, cell
729 return line, cell
730
730
731 _ip.register_magics(MyMagics)
731 _ip.register_magics(MyMagics)
732 self.check_ident('cellm3')
732 self.check_ident('cellm3')
733
733
734 def test_cell_magic_class2(self):
734 def test_cell_magic_class2(self):
735 "Cell magics declared via a class, #2"
735 "Cell magics declared via a class, #2"
736 @magics_class
736 @magics_class
737 class MyMagics2(Magics):
737 class MyMagics2(Magics):
738
738
739 @cell_magic('cellm4')
739 @cell_magic('cellm4')
740 def cellm33(self, line, cell):
740 def cellm33(self, line, cell):
741 return line, cell
741 return line, cell
742
742
743 _ip.register_magics(MyMagics2)
743 _ip.register_magics(MyMagics2)
744 self.check_ident('cellm4')
744 self.check_ident('cellm4')
745 # Check that nothing is registered as 'cellm33'
745 # Check that nothing is registered as 'cellm33'
746 c33 = _ip.find_cell_magic('cellm33')
746 c33 = _ip.find_cell_magic('cellm33')
747 nt.assert_equal(c33, None)
747 nt.assert_equal(c33, None)
748
748
749 def test_file():
749 def test_file():
750 """Basic %%file"""
750 """Basic %%file"""
751 ip = get_ipython()
751 ip = get_ipython()
752 with TemporaryDirectory() as td:
752 with TemporaryDirectory() as td:
753 fname = os.path.join(td, 'file1')
753 fname = os.path.join(td, 'file1')
754 ip.run_cell_magic("file", fname, u'\n'.join([
754 ip.run_cell_magic("file", fname, u'\n'.join([
755 'line1',
755 'line1',
756 'line2',
756 'line2',
757 ]))
757 ]))
758 with open(fname) as f:
758 with open(fname) as f:
759 s = f.read()
759 s = f.read()
760 nt.assert_in('line1\n', s)
760 nt.assert_in('line1\n', s)
761 nt.assert_in('line2', s)
761 nt.assert_in('line2', s)
762
762
763 def test_file_var_expand():
763 def test_file_var_expand():
764 """%%file $filename"""
764 """%%file $filename"""
765 ip = get_ipython()
765 ip = get_ipython()
766 with TemporaryDirectory() as td:
766 with TemporaryDirectory() as td:
767 fname = os.path.join(td, 'file1')
767 fname = os.path.join(td, 'file1')
768 ip.user_ns['filename'] = fname
768 ip.user_ns['filename'] = fname
769 ip.run_cell_magic("file", '$filename', u'\n'.join([
769 ip.run_cell_magic("file", '$filename', u'\n'.join([
770 'line1',
770 'line1',
771 'line2',
771 'line2',
772 ]))
772 ]))
773 with open(fname) as f:
773 with open(fname) as f:
774 s = f.read()
774 s = f.read()
775 nt.assert_in('line1\n', s)
775 nt.assert_in('line1\n', s)
776 nt.assert_in('line2', s)
776 nt.assert_in('line2', s)
777
777
778 def test_file_unicode():
778 def test_file_unicode():
779 """%%file with unicode cell"""
779 """%%file with unicode cell"""
780 ip = get_ipython()
780 ip = get_ipython()
781 with TemporaryDirectory() as td:
781 with TemporaryDirectory() as td:
782 fname = os.path.join(td, 'file1')
782 fname = os.path.join(td, 'file1')
783 ip.run_cell_magic("file", fname, u'\n'.join([
783 ip.run_cell_magic("file", fname, u'\n'.join([
784 u'liné1',
784 u'liné1',
785 u'liné2',
785 u'liné2',
786 ]))
786 ]))
787 with io.open(fname, encoding='utf-8') as f:
787 with io.open(fname, encoding='utf-8') as f:
788 s = f.read()
788 s = f.read()
789 nt.assert_in(u'liné1\n', s)
789 nt.assert_in(u'liné1\n', s)
790 nt.assert_in(u'liné2', s)
790 nt.assert_in(u'liné2', s)
791
791
792 def test_file_amend():
792 def test_file_amend():
793 """%%file -a amends files"""
793 """%%file -a amends files"""
794 ip = get_ipython()
794 ip = get_ipython()
795 with TemporaryDirectory() as td:
795 with TemporaryDirectory() as td:
796 fname = os.path.join(td, 'file2')
796 fname = os.path.join(td, 'file2')
797 ip.run_cell_magic("file", fname, u'\n'.join([
797 ip.run_cell_magic("file", fname, u'\n'.join([
798 'line1',
798 'line1',
799 'line2',
799 'line2',
800 ]))
800 ]))
801 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
801 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
802 'line3',
802 'line3',
803 'line4',
803 'line4',
804 ]))
804 ]))
805 with open(fname) as f:
805 with open(fname) as f:
806 s = f.read()
806 s = f.read()
807 nt.assert_in('line1\n', s)
807 nt.assert_in('line1\n', s)
808 nt.assert_in('line3\n', s)
808 nt.assert_in('line3\n', s)
809
809
810
810
811 def test_script_config():
811 def test_script_config():
812 ip = get_ipython()
812 ip = get_ipython()
813 ip.config.ScriptMagics.script_magics = ['whoda']
813 ip.config.ScriptMagics.script_magics = ['whoda']
814 sm = script.ScriptMagics(shell=ip)
814 sm = script.ScriptMagics(shell=ip)
815 nt.assert_in('whoda', sm.magics['cell'])
815 nt.assert_in('whoda', sm.magics['cell'])
816
816
817 @dec.skip_win32
817 @dec.skip_win32
818 def test_script_out():
818 def test_script_out():
819 ip = get_ipython()
819 ip = get_ipython()
820 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
820 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
821 nt.assert_equal(ip.user_ns['output'], 'hi\n')
821 nt.assert_equal(ip.user_ns['output'], 'hi\n')
822
822
823 @dec.skip_win32
823 @dec.skip_win32
824 def test_script_err():
824 def test_script_err():
825 ip = get_ipython()
825 ip = get_ipython()
826 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
826 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
827 nt.assert_equal(ip.user_ns['error'], 'hello\n')
827 nt.assert_equal(ip.user_ns['error'], 'hello\n')
828
828
829 @dec.skip_win32
829 @dec.skip_win32
830 def test_script_out_err():
830 def test_script_out_err():
831 ip = get_ipython()
831 ip = get_ipython()
832 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
832 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
833 nt.assert_equal(ip.user_ns['output'], 'hi\n')
833 nt.assert_equal(ip.user_ns['output'], 'hi\n')
834 nt.assert_equal(ip.user_ns['error'], 'hello\n')
834 nt.assert_equal(ip.user_ns['error'], 'hello\n')
835
835
836 @dec.skip_win32
836 @dec.skip_win32
837 def test_script_bg_out():
837 def test_script_bg_out():
838 ip = get_ipython()
838 ip = get_ipython()
839 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
839 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
840 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
840 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
841
841
842 @dec.skip_win32
842 @dec.skip_win32
843 def test_script_bg_err():
843 def test_script_bg_err():
844 ip = get_ipython()
844 ip = get_ipython()
845 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
845 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
846 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
846 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
847
847
848 @dec.skip_win32
848 @dec.skip_win32
849 def test_script_bg_out_err():
849 def test_script_bg_out_err():
850 ip = get_ipython()
850 ip = get_ipython()
851 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
851 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
852 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
852 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
853 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
853 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
854
854
855 def test_script_defaults():
855 def test_script_defaults():
856 ip = get_ipython()
856 ip = get_ipython()
857 for cmd in ['sh', 'bash', 'perl', 'ruby']:
857 for cmd in ['sh', 'bash', 'perl', 'ruby']:
858 try:
858 try:
859 find_cmd(cmd)
859 find_cmd(cmd)
860 except Exception:
860 except Exception:
861 pass
861 pass
862 else:
862 else:
863 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
863 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
864
864
865
865
866 @magics_class
866 @magics_class
867 class FooFoo(Magics):
867 class FooFoo(Magics):
868 """class with both %foo and %%foo magics"""
868 """class with both %foo and %%foo magics"""
869 @line_magic('foo')
869 @line_magic('foo')
870 def line_foo(self, line):
870 def line_foo(self, line):
871 "I am line foo"
871 "I am line foo"
872 pass
872 pass
873
873
874 @cell_magic("foo")
874 @cell_magic("foo")
875 def cell_foo(self, line, cell):
875 def cell_foo(self, line, cell):
876 "I am cell foo, not line foo"
876 "I am cell foo, not line foo"
877 pass
877 pass
878
878
879 def test_line_cell_info():
879 def test_line_cell_info():
880 """%%foo and %foo magics are distinguishable to inspect"""
880 """%%foo and %foo magics are distinguishable to inspect"""
881 ip = get_ipython()
881 ip = get_ipython()
882 ip.magics_manager.register(FooFoo)
882 ip.magics_manager.register(FooFoo)
883 oinfo = ip.object_inspect('foo')
883 oinfo = ip.object_inspect('foo')
884 nt.assert_true(oinfo['found'])
884 nt.assert_true(oinfo['found'])
885 nt.assert_true(oinfo['ismagic'])
885 nt.assert_true(oinfo['ismagic'])
886
886
887 oinfo = ip.object_inspect('%%foo')
887 oinfo = ip.object_inspect('%%foo')
888 nt.assert_true(oinfo['found'])
888 nt.assert_true(oinfo['found'])
889 nt.assert_true(oinfo['ismagic'])
889 nt.assert_true(oinfo['ismagic'])
890 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
890 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
891
891
892 oinfo = ip.object_inspect('%foo')
892 oinfo = ip.object_inspect('%foo')
893 nt.assert_true(oinfo['found'])
893 nt.assert_true(oinfo['found'])
894 nt.assert_true(oinfo['ismagic'])
894 nt.assert_true(oinfo['ismagic'])
895 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
895 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
896
896
897 def test_multiple_magics():
897 def test_multiple_magics():
898 ip = get_ipython()
898 ip = get_ipython()
899 foo1 = FooFoo(ip)
899 foo1 = FooFoo(ip)
900 foo2 = FooFoo(ip)
900 foo2 = FooFoo(ip)
901 mm = ip.magics_manager
901 mm = ip.magics_manager
902 mm.register(foo1)
902 mm.register(foo1)
903 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
903 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
904 mm.register(foo2)
904 mm.register(foo2)
905 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
905 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
906
906
907 def test_alias_magic():
907 def test_alias_magic():
908 """Test %alias_magic."""
908 """Test %alias_magic."""
909 ip = get_ipython()
909 ip = get_ipython()
910 mm = ip.magics_manager
910 mm = ip.magics_manager
911
911
912 # Basic operation: both cell and line magics are created, if possible.
912 # Basic operation: both cell and line magics are created, if possible.
913 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
913 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
914 nt.assert_in('timeit_alias', mm.magics['line'])
914 nt.assert_in('timeit_alias', mm.magics['line'])
915 nt.assert_in('timeit_alias', mm.magics['cell'])
915 nt.assert_in('timeit_alias', mm.magics['cell'])
916
916
917 # --cell is specified, line magic not created.
917 # --cell is specified, line magic not created.
918 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
918 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
919 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
919 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
920 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
920 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
921
921
922 # Test that line alias is created successfully.
922 # Test that line alias is created successfully.
923 ip.run_line_magic('alias_magic', '--line env_alias env')
923 ip.run_line_magic('alias_magic', '--line env_alias env')
924 nt.assert_equal(ip.run_line_magic('env', ''),
924 nt.assert_equal(ip.run_line_magic('env', ''),
925 ip.run_line_magic('env_alias', ''))
925 ip.run_line_magic('env_alias', ''))
926
926
927 # Test that line alias with parameters passed in is created successfully.
927 # Test that line alias with parameters passed in is created successfully.
928 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
928 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
929 nt.assert_in('history_alias', mm.magics['line'])
929 nt.assert_in('history_alias', mm.magics['line'])
930
930
931
931
932 def test_save():
932 def test_save():
933 """Test %save."""
933 """Test %save."""
934 ip = get_ipython()
934 ip = get_ipython()
935 ip.history_manager.reset() # Clear any existing history.
935 ip.history_manager.reset() # Clear any existing history.
936 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
936 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
937 for i, cmd in enumerate(cmds, start=1):
937 for i, cmd in enumerate(cmds, start=1):
938 ip.history_manager.store_inputs(i, cmd)
938 ip.history_manager.store_inputs(i, cmd)
939 with TemporaryDirectory() as tmpdir:
939 with TemporaryDirectory() as tmpdir:
940 file = os.path.join(tmpdir, "testsave.py")
940 file = os.path.join(tmpdir, "testsave.py")
941 ip.run_line_magic("save", "%s 1-10" % file)
941 ip.run_line_magic("save", "%s 1-10" % file)
942 with open(file) as f:
942 with open(file) as f:
943 content = f.read()
943 content = f.read()
944 nt.assert_equal(content.count(cmds[0]), 1)
944 nt.assert_equal(content.count(cmds[0]), 1)
945 nt.assert_in('coding: utf-8', content)
945 nt.assert_in('coding: utf-8', content)
946 ip.run_line_magic("save", "-a %s 1-10" % file)
946 ip.run_line_magic("save", "-a %s 1-10" % file)
947 with open(file) as f:
947 with open(file) as f:
948 content = f.read()
948 content = f.read()
949 nt.assert_equal(content.count(cmds[0]), 2)
949 nt.assert_equal(content.count(cmds[0]), 2)
950 nt.assert_in('coding: utf-8', content)
950 nt.assert_in('coding: utf-8', content)
951
951
952
952
953 def test_store():
953 def test_store():
954 """Test %store."""
954 """Test %store."""
955 ip = get_ipython()
955 ip = get_ipython()
956 ip.run_line_magic('load_ext', 'storemagic')
956 ip.run_line_magic('load_ext', 'storemagic')
957
957
958 # make sure the storage is empty
958 # make sure the storage is empty
959 ip.run_line_magic('store', '-z')
959 ip.run_line_magic('store', '-z')
960 ip.user_ns['var'] = 42
960 ip.user_ns['var'] = 42
961 ip.run_line_magic('store', 'var')
961 ip.run_line_magic('store', 'var')
962 ip.user_ns['var'] = 39
962 ip.user_ns['var'] = 39
963 ip.run_line_magic('store', '-r')
963 ip.run_line_magic('store', '-r')
964 nt.assert_equal(ip.user_ns['var'], 42)
964 nt.assert_equal(ip.user_ns['var'], 42)
965
965
966 ip.run_line_magic('store', '-d var')
966 ip.run_line_magic('store', '-d var')
967 ip.user_ns['var'] = 39
967 ip.user_ns['var'] = 39
968 ip.run_line_magic('store' , '-r')
968 ip.run_line_magic('store' , '-r')
969 nt.assert_equal(ip.user_ns['var'], 39)
969 nt.assert_equal(ip.user_ns['var'], 39)
970
970
971
971
972 def _run_edit_test(arg_s, exp_filename=None,
972 def _run_edit_test(arg_s, exp_filename=None,
973 exp_lineno=-1,
973 exp_lineno=-1,
974 exp_contents=None,
974 exp_contents=None,
975 exp_is_temp=None):
975 exp_is_temp=None):
976 ip = get_ipython()
976 ip = get_ipython()
977 M = code.CodeMagics(ip)
977 M = code.CodeMagics(ip)
978 last_call = ['','']
978 last_call = ['','']
979 opts,args = M.parse_options(arg_s,'prxn:')
979 opts,args = M.parse_options(arg_s,'prxn:')
980 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
980 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
981
981
982 if exp_filename is not None:
982 if exp_filename is not None:
983 nt.assert_equal(exp_filename, filename)
983 nt.assert_equal(exp_filename, filename)
984 if exp_contents is not None:
984 if exp_contents is not None:
985 with io.open(filename, 'r', encoding='utf-8') as f:
985 with io.open(filename, 'r', encoding='utf-8') as f:
986 contents = f.read()
986 contents = f.read()
987 nt.assert_equal(exp_contents, contents)
987 nt.assert_equal(exp_contents, contents)
988 if exp_lineno != -1:
988 if exp_lineno != -1:
989 nt.assert_equal(exp_lineno, lineno)
989 nt.assert_equal(exp_lineno, lineno)
990 if exp_is_temp is not None:
990 if exp_is_temp is not None:
991 nt.assert_equal(exp_is_temp, is_temp)
991 nt.assert_equal(exp_is_temp, is_temp)
992
992
993
993
994 def test_edit_interactive():
994 def test_edit_interactive():
995 """%edit on interactively defined objects"""
995 """%edit on interactively defined objects"""
996 ip = get_ipython()
996 ip = get_ipython()
997 n = ip.execution_count
997 n = ip.execution_count
998 ip.run_cell(u"def foo(): return 1", store_history=True)
998 ip.run_cell(u"def foo(): return 1", store_history=True)
999
999
1000 try:
1000 try:
1001 _run_edit_test("foo")
1001 _run_edit_test("foo")
1002 except code.InteractivelyDefined as e:
1002 except code.InteractivelyDefined as e:
1003 nt.assert_equal(e.index, n)
1003 nt.assert_equal(e.index, n)
1004 else:
1004 else:
1005 raise AssertionError("Should have raised InteractivelyDefined")
1005 raise AssertionError("Should have raised InteractivelyDefined")
1006
1006
1007
1007
1008 def test_edit_cell():
1008 def test_edit_cell():
1009 """%edit [cell id]"""
1009 """%edit [cell id]"""
1010 ip = get_ipython()
1010 ip = get_ipython()
1011
1011
1012 ip.run_cell(u"def foo(): return 1", store_history=True)
1012 ip.run_cell(u"def foo(): return 1", store_history=True)
1013
1013
1014 # test
1014 # test
1015 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1015 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1016
1016
1017 def test_bookmark():
1017 def test_bookmark():
1018 ip = get_ipython()
1018 ip = get_ipython()
1019 ip.run_line_magic('bookmark', 'bmname')
1019 ip.run_line_magic('bookmark', 'bmname')
1020 with tt.AssertPrints('bmname'):
1020 with tt.AssertPrints('bmname'):
1021 ip.run_line_magic('bookmark', '-l')
1021 ip.run_line_magic('bookmark', '-l')
1022 ip.run_line_magic('bookmark', '-d bmname')
1022 ip.run_line_magic('bookmark', '-d bmname')
1023
1023
1024 def test_ls_magic():
1024 def test_ls_magic():
1025 ip = get_ipython()
1025 ip = get_ipython()
1026 json_formatter = ip.display_formatter.formatters['application/json']
1026 json_formatter = ip.display_formatter.formatters['application/json']
1027 json_formatter.enabled = True
1027 json_formatter.enabled = True
1028 lsmagic = ip.magic('lsmagic')
1028 lsmagic = ip.magic('lsmagic')
1029 with warnings.catch_warnings(record=True) as w:
1029 with warnings.catch_warnings(record=True) as w:
1030 j = json_formatter(lsmagic)
1030 j = json_formatter(lsmagic)
1031 nt.assert_equal(sorted(j), ['cell', 'line'])
1031 nt.assert_equal(sorted(j), ['cell', 'line'])
1032 nt.assert_equal(w, []) # no warnings
1032 nt.assert_equal(w, []) # no warnings
1033
1033
1034 def test_strip_initial_indent():
1034 def test_strip_initial_indent():
1035 def sii(s):
1035 def sii(s):
1036 lines = s.splitlines()
1036 lines = s.splitlines()
1037 return '\n'.join(code.strip_initial_indent(lines))
1037 return '\n'.join(code.strip_initial_indent(lines))
1038
1038
1039 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1039 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1040 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1040 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1041 nt.assert_equal(sii("a\n b"), "a\n b")
1041 nt.assert_equal(sii("a\n b"), "a\n b")
1042
1042
1043 def test_logging_magic_quiet_from_arg():
1043 def test_logging_magic_quiet_from_arg():
1044 _ip.config.LoggingMagics.quiet = False
1044 _ip.config.LoggingMagics.quiet = False
1045 lm = logging.LoggingMagics(shell=_ip)
1045 lm = logging.LoggingMagics(shell=_ip)
1046 with TemporaryDirectory() as td:
1046 with TemporaryDirectory() as td:
1047 try:
1047 try:
1048 with tt.AssertNotPrints(re.compile("Activating.*")):
1048 with tt.AssertNotPrints(re.compile("Activating.*")):
1049 lm.logstart('-q {}'.format(
1049 lm.logstart('-q {}'.format(
1050 os.path.join(td, "quiet_from_arg.log")))
1050 os.path.join(td, "quiet_from_arg.log")))
1051 finally:
1051 finally:
1052 _ip.logger.logstop()
1052 _ip.logger.logstop()
1053
1053
1054 def test_logging_magic_quiet_from_config():
1054 def test_logging_magic_quiet_from_config():
1055 _ip.config.LoggingMagics.quiet = True
1055 _ip.config.LoggingMagics.quiet = True
1056 lm = logging.LoggingMagics(shell=_ip)
1056 lm = logging.LoggingMagics(shell=_ip)
1057 with TemporaryDirectory() as td:
1057 with TemporaryDirectory() as td:
1058 try:
1058 try:
1059 with tt.AssertNotPrints(re.compile("Activating.*")):
1059 with tt.AssertNotPrints(re.compile("Activating.*")):
1060 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1060 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1061 finally:
1061 finally:
1062 _ip.logger.logstop()
1062 _ip.logger.logstop()
1063
1063
1064 def test_logging_magic_not_quiet():
1064 def test_logging_magic_not_quiet():
1065 _ip.config.LoggingMagics.quiet = False
1065 _ip.config.LoggingMagics.quiet = False
1066 lm = logging.LoggingMagics(shell=_ip)
1066 lm = logging.LoggingMagics(shell=_ip)
1067 with TemporaryDirectory() as td:
1067 with TemporaryDirectory() as td:
1068 try:
1068 try:
1069 with tt.AssertPrints(re.compile("Activating.*")):
1069 with tt.AssertPrints(re.compile("Activating.*")):
1070 lm.logstart(os.path.join(td, "not_quiet.log"))
1070 lm.logstart(os.path.join(td, "not_quiet.log"))
1071 finally:
1071 finally:
1072 _ip.logger.logstop()
1072 _ip.logger.logstop()
1073
1073
@@ -1,348 +1,348 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Usage information for the main IPython applications.
2 """Usage information for the main IPython applications.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2008-2011 The IPython Development Team
5 # Copyright (C) 2008-2011 The IPython Development Team
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 #
7 #
8 # Distributed under the terms of the BSD License. The full license is in
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
9 # the file COPYING, distributed as part of this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 import sys
12 import sys
13 from IPython.core import release
13 from IPython.core import release
14
14
15 cl_usage = """\
15 cl_usage = """\
16 =========
16 =========
17 IPython
17 IPython
18 =========
18 =========
19
19
20 Tools for Interactive Computing in Python
20 Tools for Interactive Computing in Python
21 =========================================
21 =========================================
22
22
23 A Python shell with automatic history (input and output), dynamic object
23 A Python shell with automatic history (input and output), dynamic object
24 introspection, easier configuration, command completion, access to the
24 introspection, easier configuration, command completion, access to the
25 system shell and more. IPython can also be embedded in running programs.
25 system shell and more. IPython can also be embedded in running programs.
26
26
27
27
28 Usage
28 Usage
29
29
30 ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...
30 ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...
31
31
32 If invoked with no options, it executes the file and exits, passing the
32 If invoked with no options, it executes the file and exits, passing the
33 remaining arguments to the script, just as if you had specified the same
33 remaining arguments to the script, just as if you had specified the same
34 command with python. You may need to specify `--` before args to be passed
34 command with python. You may need to specify `--` before args to be passed
35 to the script, to prevent IPython from attempting to parse them. If you
35 to the script, to prevent IPython from attempting to parse them. If you
36 specify the option `-i` before the filename, it will enter an interactive
36 specify the option `-i` before the filename, it will enter an interactive
37 IPython session after running the script, rather than exiting. Files ending
37 IPython session after running the script, rather than exiting. Files ending
38 in .py will be treated as normal Python, but files ending in .ipy can
38 in .py will be treated as normal Python, but files ending in .ipy can
39 contain special IPython syntax (magic commands, shell expansions, etc.).
39 contain special IPython syntax (magic commands, shell expansions, etc.).
40
40
41 Almost all configuration in IPython is available via the command-line. Do
41 Almost all configuration in IPython is available via the command-line. Do
42 `ipython --help-all` to see all available options. For persistent
42 `ipython --help-all` to see all available options. For persistent
43 configuration, look into your `ipython_config.py` configuration file for
43 configuration, look into your `ipython_config.py` configuration file for
44 details.
44 details.
45
45
46 This file is typically installed in the `IPYTHONDIR` directory, and there
46 This file is typically installed in the `IPYTHONDIR` directory, and there
47 is a separate configuration directory for each profile. The default profile
47 is a separate configuration directory for each profile. The default profile
48 directory will be located in $IPYTHONDIR/profile_default. IPYTHONDIR
48 directory will be located in $IPYTHONDIR/profile_default. IPYTHONDIR
49 defaults to to `$HOME/.ipython`. For Windows users, $HOME resolves to
49 defaults to to `$HOME/.ipython`. For Windows users, $HOME resolves to
50 C:\\Users\\YourUserName in most instances.
50 C:\\Users\\YourUserName in most instances.
51
51
52 To initialize a profile with the default configuration file, do::
52 To initialize a profile with the default configuration file, do::
53
53
54 $> ipython profile create
54 $> ipython profile create
55
55
56 and start editing `IPYTHONDIR/profile_default/ipython_config.py`
56 and start editing `IPYTHONDIR/profile_default/ipython_config.py`
57
57
58 In IPython's documentation, we will refer to this directory as
58 In IPython's documentation, we will refer to this directory as
59 `IPYTHONDIR`, you can change its default location by creating an
59 `IPYTHONDIR`, you can change its default location by creating an
60 environment variable with this name and setting it to the desired path.
60 environment variable with this name and setting it to the desired path.
61
61
62 For more information, see the manual available in HTML and PDF in your
62 For more information, see the manual available in HTML and PDF in your
63 installation, or online at http://ipython.org/documentation.html.
63 installation, or online at http://ipython.org/documentation.html.
64 """
64 """
65
65
66 interactive_usage = """
66 interactive_usage = """
67 IPython -- An enhanced Interactive Python
67 IPython -- An enhanced Interactive Python
68 =========================================
68 =========================================
69
69
70 IPython offers a fully compatible replacement for the standard Python
70 IPython offers a fully compatible replacement for the standard Python
71 interpreter, with convenient shell features, special commands, command
71 interpreter, with convenient shell features, special commands, command
72 history mechanism and output results caching.
72 history mechanism and output results caching.
73
73
74 At your system command line, type 'ipython -h' to see the command line
74 At your system command line, type 'ipython -h' to see the command line
75 options available. This document only describes interactive features.
75 options available. This document only describes interactive features.
76
76
77 GETTING HELP
77 GETTING HELP
78 ------------
78 ------------
79
79
80 Within IPython you have various way to access help:
80 Within IPython you have various way to access help:
81
81
82 ? -> Introduction and overview of IPython's features (this screen).
82 ? -> Introduction and overview of IPython's features (this screen).
83 object? -> Details about 'object'.
83 object? -> Details about 'object'.
84 object?? -> More detailed, verbose information about 'object'.
84 object?? -> More detailed, verbose information about 'object'.
85 %quickref -> Quick reference of all IPython specific syntax and magics.
85 %quickref -> Quick reference of all IPython specific syntax and magics.
86 help -> Access Python's own help system.
86 help -> Access Python's own help system.
87
87
88 If you are in terminal IPython you can quit this screen by pressing `q`.
88 If you are in terminal IPython you can quit this screen by pressing `q`.
89
89
90
90
91 MAIN FEATURES
91 MAIN FEATURES
92 -------------
92 -------------
93
93
94 * Access to the standard Python help with object docstrings and the Python
94 * Access to the standard Python help with object docstrings and the Python
95 manuals. Simply type 'help' (no quotes) to invoke it.
95 manuals. Simply type 'help' (no quotes) to invoke it.
96
96
97 * Magic commands: type %magic for information on the magic subsystem.
97 * Magic commands: type %magic for information on the magic subsystem.
98
98
99 * System command aliases, via the %alias command or the configuration file(s).
99 * System command aliases, via the %alias command or the configuration file(s).
100
100
101 * Dynamic object information:
101 * Dynamic object information:
102
102
103 Typing ?word or word? prints detailed information about an object. Certain
103 Typing ?word or word? prints detailed information about an object. Certain
104 long strings (code, etc.) get snipped in the center for brevity.
104 long strings (code, etc.) get snipped in the center for brevity.
105
105
106 Typing ??word or word?? gives access to the full information without
106 Typing ??word or word?? gives access to the full information without
107 snipping long strings. Strings that are longer than the screen are printed
107 snipping long strings. Strings that are longer than the screen are printed
108 through the less pager.
108 through the less pager.
109
109
110 The ?/?? system gives access to the full source code for any object (if
110 The ?/?? system gives access to the full source code for any object (if
111 available), shows function prototypes and other useful information.
111 available), shows function prototypes and other useful information.
112
112
113 If you just want to see an object's docstring, type '%pdoc object' (without
113 If you just want to see an object's docstring, type '%pdoc object' (without
114 quotes, and without % if you have automagic on).
114 quotes, and without % if you have automagic on).
115
115
116 * Tab completion in the local namespace:
116 * Tab completion in the local namespace:
117
117
118 At any time, hitting tab will complete any available python commands or
118 At any time, hitting tab will complete any available python commands or
119 variable names, and show you a list of the possible completions if there's
119 variable names, and show you a list of the possible completions if there's
120 no unambiguous one. It will also complete filenames in the current directory.
120 no unambiguous one. It will also complete filenames in the current directory.
121
121
122 * Search previous command history in multiple ways:
122 * Search previous command history in multiple ways:
123
123
124 - Start typing, and then use arrow keys up/down or (Ctrl-p/Ctrl-n) to search
124 - Start typing, and then use arrow keys up/down or (Ctrl-p/Ctrl-n) to search
125 through the history items that match what you've typed so far.
125 through the history items that match what you've typed so far.
126
126
127 - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches
127 - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches
128 your history for lines that match what you've typed so far, completing as
128 your history for lines that match what you've typed so far, completing as
129 much as it can.
129 much as it can.
130
130
131 - %hist: search history by index.
131 - %hist: search history by index.
132
132
133 * Persistent command history across sessions.
133 * Persistent command history across sessions.
134
134
135 * Logging of input with the ability to save and restore a working session.
135 * Logging of input with the ability to save and restore a working session.
136
136
137 * System shell with !. Typing !ls will run 'ls' in the current directory.
137 * System shell with !. Typing !ls will run 'ls' in the current directory.
138
138
139 * The reload command does a 'deep' reload of a module: changes made to the
139 * The reload command does a 'deep' reload of a module: changes made to the
140 module since you imported will actually be available without having to exit.
140 module since you imported will actually be available without having to exit.
141
141
142 * Verbose and colored exception traceback printouts. See the magic xmode and
142 * Verbose and colored exception traceback printouts. See the magic xmode and
143 xcolor functions for details (just type %magic).
143 xcolor functions for details (just type %magic).
144
144
145 * Input caching system:
145 * Input caching system:
146
146
147 IPython offers numbered prompts (In/Out) with input and output caching. All
147 IPython offers numbered prompts (In/Out) with input and output caching. All
148 input is saved and can be retrieved as variables (besides the usual arrow
148 input is saved and can be retrieved as variables (besides the usual arrow
149 key recall).
149 key recall).
150
150
151 The following GLOBAL variables always exist (so don't overwrite them!):
151 The following GLOBAL variables always exist (so don't overwrite them!):
152 _i: stores previous input.
152 _i: stores previous input.
153 _ii: next previous.
153 _ii: next previous.
154 _iii: next-next previous.
154 _iii: next-next previous.
155 _ih : a list of all input _ih[n] is the input from line n.
155 _ih : a list of all input _ih[n] is the input from line n.
156
156
157 Additionally, global variables named _i<n> are dynamically created (<n>
157 Additionally, global variables named _i<n> are dynamically created (<n>
158 being the prompt counter), such that _i<n> == _ih[<n>]
158 being the prompt counter), such that _i<n> == _ih[<n>]
159
159
160 For example, what you typed at prompt 14 is available as _i14 and _ih[14].
160 For example, what you typed at prompt 14 is available as _i14 and _ih[14].
161
161
162 You can create macros which contain multiple input lines from this history,
162 You can create macros which contain multiple input lines from this history,
163 for later re-execution, with the %macro function.
163 for later re-execution, with the %macro function.
164
164
165 The history function %hist allows you to see any part of your input history
165 The history function %hist allows you to see any part of your input history
166 by printing a range of the _i variables. Note that inputs which contain
166 by printing a range of the _i variables. Note that inputs which contain
167 magic functions (%) appear in the history with a prepended comment. This is
167 magic functions (%) appear in the history with a prepended comment. This is
168 because they aren't really valid Python code, so you can't exec them.
168 because they aren't really valid Python code, so you can't exec them.
169
169
170 * Output caching system:
170 * Output caching system:
171
171
172 For output that is returned from actions, a system similar to the input
172 For output that is returned from actions, a system similar to the input
173 cache exists but using _ instead of _i. Only actions that produce a result
173 cache exists but using _ instead of _i. Only actions that produce a result
174 (NOT assignments, for example) are cached. If you are familiar with
174 (NOT assignments, for example) are cached. If you are familiar with
175 Mathematica, IPython's _ variables behave exactly like Mathematica's %
175 Mathematica, IPython's _ variables behave exactly like Mathematica's %
176 variables.
176 variables.
177
177
178 The following GLOBAL variables always exist (so don't overwrite them!):
178 The following GLOBAL variables always exist (so don't overwrite them!):
179 _ (one underscore): previous output.
179 _ (one underscore): previous output.
180 __ (two underscores): next previous.
180 __ (two underscores): next previous.
181 ___ (three underscores): next-next previous.
181 ___ (three underscores): next-next previous.
182
182
183 Global variables named _<n> are dynamically created (<n> being the prompt
183 Global variables named _<n> are dynamically created (<n> being the prompt
184 counter), such that the result of output <n> is always available as _<n>.
184 counter), such that the result of output <n> is always available as _<n>.
185
185
186 Finally, a global dictionary named _oh exists with entries for all lines
186 Finally, a global dictionary named _oh exists with entries for all lines
187 which generated output.
187 which generated output.
188
188
189 * Directory history:
189 * Directory history:
190
190
191 Your history of visited directories is kept in the global list _dh, and the
191 Your history of visited directories is kept in the global list _dh, and the
192 magic %cd command can be used to go to any entry in that list.
192 magic %cd command can be used to go to any entry in that list.
193
193
194 * Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython)
194 * Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython)
195
195
196 1. Auto-parentheses
196 1. Auto-parentheses
197
197
198 Callable objects (i.e. functions, methods, etc) can be invoked like
198 Callable objects (i.e. functions, methods, etc) can be invoked like
199 this (notice the commas between the arguments)::
199 this (notice the commas between the arguments)::
200
200
201 In [1]: callable_ob arg1, arg2, arg3
201 In [1]: callable_ob arg1, arg2, arg3
202
202
203 and the input will be translated to this::
203 and the input will be translated to this::
204
204
205 callable_ob(arg1, arg2, arg3)
205 callable_ob(arg1, arg2, arg3)
206
206
207 This feature is off by default (in rare cases it can produce
207 This feature is off by default (in rare cases it can produce
208 undesirable side-effects), but you can activate it at the command-line
208 undesirable side-effects), but you can activate it at the command-line
209 by starting IPython with `--autocall 1`, set it permanently in your
209 by starting IPython with `--autocall 1`, set it permanently in your
210 configuration file, or turn on at runtime with `%autocall 1`.
210 configuration file, or turn on at runtime with `%autocall 1`.
211
211
212 You can force auto-parentheses by using '/' as the first character
212 You can force auto-parentheses by using '/' as the first character
213 of a line. For example::
213 of a line. For example::
214
214
215 In [1]: /globals # becomes 'globals()'
215 In [1]: /globals # becomes 'globals()'
216
216
217 Note that the '/' MUST be the first character on the line! This
217 Note that the '/' MUST be the first character on the line! This
218 won't work::
218 won't work::
219
219
220 In [2]: print /globals # syntax error
220 In [2]: print /globals # syntax error
221
221
222 In most cases the automatic algorithm should work, so you should
222 In most cases the automatic algorithm should work, so you should
223 rarely need to explicitly invoke /. One notable exception is if you
223 rarely need to explicitly invoke /. One notable exception is if you
224 are trying to call a function with a list of tuples as arguments (the
224 are trying to call a function with a list of tuples as arguments (the
225 parenthesis will confuse IPython)::
225 parenthesis will confuse IPython)::
226
226
227 In [1]: zip (1,2,3),(4,5,6) # won't work
227 In [1]: zip (1,2,3),(4,5,6) # won't work
228
228
229 but this will work::
229 but this will work::
230
230
231 In [2]: /zip (1,2,3),(4,5,6)
231 In [2]: /zip (1,2,3),(4,5,6)
232 ------> zip ((1,2,3),(4,5,6))
232 ------> zip ((1,2,3),(4,5,6))
233 Out[2]= [(1, 4), (2, 5), (3, 6)]
233 Out[2]= [(1, 4), (2, 5), (3, 6)]
234
234
235 IPython tells you that it has altered your command line by
235 IPython tells you that it has altered your command line by
236 displaying the new command line preceded by -->. e.g.::
236 displaying the new command line preceded by -->. e.g.::
237
237
238 In [18]: callable list
238 In [18]: callable list
239 -------> callable (list)
239 -------> callable (list)
240
240
241 2. Auto-Quoting
241 2. Auto-Quoting
242
242
243 You can force auto-quoting of a function's arguments by using ',' as
243 You can force auto-quoting of a function's arguments by using ',' as
244 the first character of a line. For example::
244 the first character of a line. For example::
245
245
246 In [1]: ,my_function /home/me # becomes my_function("/home/me")
246 In [1]: ,my_function /home/me # becomes my_function("/home/me")
247
247
248 If you use ';' instead, the whole argument is quoted as a single
248 If you use ';' instead, the whole argument is quoted as a single
249 string (while ',' splits on whitespace)::
249 string (while ',' splits on whitespace)::
250
250
251 In [2]: ,my_function a b c # becomes my_function("a","b","c")
251 In [2]: ,my_function a b c # becomes my_function("a","b","c")
252 In [3]: ;my_function a b c # becomes my_function("a b c")
252 In [3]: ;my_function a b c # becomes my_function("a b c")
253
253
254 Note that the ',' MUST be the first character on the line! This
254 Note that the ',' MUST be the first character on the line! This
255 won't work::
255 won't work::
256
256
257 In [4]: x = ,my_function /home/me # syntax error
257 In [4]: x = ,my_function /home/me # syntax error
258 """
258 """
259
259
260 interactive_usage_min = """\
260 interactive_usage_min = """\
261 An enhanced console for Python.
261 An enhanced console for Python.
262 Some of its features are:
262 Some of its features are:
263 - Tab completion in the local namespace.
263 - Tab completion in the local namespace.
264 - Logging of input, see command-line options.
264 - Logging of input, see command-line options.
265 - System shell escape via ! , eg !ls.
265 - System shell escape via ! , eg !ls.
266 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
266 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
267 - Keeps track of locally defined variables via %who, %whos.
267 - Keeps track of locally defined variables via %who, %whos.
268 - Show object information with a ? eg ?x or x? (use ?? for more info).
268 - Show object information with a ? eg ?x or x? (use ?? for more info).
269 """
269 """
270
270
271 quick_reference = r"""
271 quick_reference = r"""
272 IPython -- An enhanced Interactive Python - Quick Reference Card
272 IPython -- An enhanced Interactive Python - Quick Reference Card
273 ================================================================
273 ================================================================
274
274
275 obj?, obj?? : Get help, or more help for object (also works as
275 obj?, obj?? : Get help, or more help for object (also works as
276 ?obj, ??obj).
276 ?obj, ??obj).
277 ?foo.*abc* : List names in 'foo' containing 'abc' in them.
277 ?foo.*abc* : List names in 'foo' containing 'abc' in them.
278 %magic : Information about IPython's 'magic' % functions.
278 %magic : Information about IPython's 'magic' % functions.
279
279
280 Magic functions are prefixed by % or %%, and typically take their arguments
280 Magic functions are prefixed by % or %%, and typically take their arguments
281 without parentheses, quotes or even commas for convenience. Line magics take a
281 without parentheses, quotes or even commas for convenience. Line magics take a
282 single % and cell magics are prefixed with two %%.
282 single % and cell magics are prefixed with two %%.
283
283
284 Example magic function calls:
284 Example magic function calls:
285
285
286 %alias d ls -F : 'd' is now an alias for 'ls -F'
286 %alias d ls -F : 'd' is now an alias for 'ls -F'
287 alias d ls -F : Works if 'alias' not a python name
287 alias d ls -F : Works if 'alias' not a python name
288 alist = %alias : Get list of aliases to 'alist'
288 alist = %alias : Get list of aliases to 'alist'
289 cd /usr/share : Obvious. cd -<tab> to choose from visited dirs.
289 cd /usr/share : Obvious. cd -<tab> to choose from visited dirs.
290 %cd?? : See help AND source for magic %cd
290 %cd?? : See help AND source for magic %cd
291 %timeit x=10 : time the 'x=10' statement with high precision.
291 %timeit x=10 : time the 'x=10' statement with high precision.
292 %%timeit x=2**100
292 %%timeit x=2**100
293 x**100 : time 'x**100' with a setup of 'x=2**100'; setup code is not
293 x**100 : time 'x**100' with a setup of 'x=2**100'; setup code is not
294 counted. This is an example of a cell magic.
294 counted. This is an example of a cell magic.
295
295
296 System commands:
296 System commands:
297
297
298 !cp a.txt b/ : System command escape, calls os.system()
298 !cp a.txt b/ : System command escape, calls os.system()
299 cp a.txt b/ : after %rehashx, most system commands work without !
299 cp a.txt b/ : after %rehashx, most system commands work without !
300 cp ${f}.txt $bar : Variable expansion in magics and system commands
300 cp ${f}.txt $bar : Variable expansion in magics and system commands
301 files = !ls /usr : Capture sytem command output
301 files = !ls /usr : Capture system command output
302 files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'
302 files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'
303
303
304 History:
304 History:
305
305
306 _i, _ii, _iii : Previous, next previous, next next previous input
306 _i, _ii, _iii : Previous, next previous, next next previous input
307 _i4, _ih[2:5] : Input history line 4, lines 2-4
307 _i4, _ih[2:5] : Input history line 4, lines 2-4
308 exec _i81 : Execute input history line #81 again
308 exec _i81 : Execute input history line #81 again
309 %rep 81 : Edit input history line #81
309 %rep 81 : Edit input history line #81
310 _, __, ___ : previous, next previous, next next previous output
310 _, __, ___ : previous, next previous, next next previous output
311 _dh : Directory history
311 _dh : Directory history
312 _oh : Output history
312 _oh : Output history
313 %hist : Command history of current session.
313 %hist : Command history of current session.
314 %hist -g foo : Search command history of (almost) all sessions for 'foo'.
314 %hist -g foo : Search command history of (almost) all sessions for 'foo'.
315 %hist -g : Command history of (almost) all sessions.
315 %hist -g : Command history of (almost) all sessions.
316 %hist 1/2-8 : Command history containing lines 2-8 of session 1.
316 %hist 1/2-8 : Command history containing lines 2-8 of session 1.
317 %hist 1/ ~2/ : Command history of session 1 and 2 sessions before current.
317 %hist 1/ ~2/ : Command history of session 1 and 2 sessions before current.
318 %hist ~8/1-~6/5 : Command history from line 1 of 8 sessions ago to
318 %hist ~8/1-~6/5 : Command history from line 1 of 8 sessions ago to
319 line 5 of 6 sessions ago.
319 line 5 of 6 sessions ago.
320 %edit 0/ : Open editor to execute code with history of current session.
320 %edit 0/ : Open editor to execute code with history of current session.
321
321
322 Autocall:
322 Autocall:
323
323
324 f 1,2 : f(1,2) # Off by default, enable with %autocall magic.
324 f 1,2 : f(1,2) # Off by default, enable with %autocall magic.
325 /f 1,2 : f(1,2) (forced autoparen)
325 /f 1,2 : f(1,2) (forced autoparen)
326 ,f 1 2 : f("1","2")
326 ,f 1 2 : f("1","2")
327 ;f 1 2 : f("1 2")
327 ;f 1 2 : f("1 2")
328
328
329 Remember: TAB completion works in many contexts, not just file names
329 Remember: TAB completion works in many contexts, not just file names
330 or python names.
330 or python names.
331
331
332 The following magic functions are currently available:
332 The following magic functions are currently available:
333
333
334 """
334 """
335
335
336 default_banner_parts = ["Python %s\n"%sys.version.split("\n")[0],
336 default_banner_parts = ["Python %s\n"%sys.version.split("\n")[0],
337 "Type 'copyright', 'credits' or 'license' for more information\n" ,
337 "Type 'copyright', 'credits' or 'license' for more information\n" ,
338 "IPython {version} -- An enhanced Interactive Python. Type '?' for help.\n".format(version=release.version),
338 "IPython {version} -- An enhanced Interactive Python. Type '?' for help.\n".format(version=release.version),
339 ]
339 ]
340
340
341 default_banner = ''.join(default_banner_parts)
341 default_banner = ''.join(default_banner_parts)
342
342
343 # deprecated GUI banner
343 # deprecated GUI banner
344
344
345 default_gui_banner = '\n'.join([
345 default_gui_banner = '\n'.join([
346 'DEPRECATED: IPython.core.usage.default_gui_banner is deprecated and will be removed',
346 'DEPRECATED: IPython.core.usage.default_gui_banner is deprecated and will be removed',
347 default_banner,
347 default_banner,
348 ])
348 ])
@@ -1,347 +1,347 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Provides a reload() function that acts recursively.
3 Provides a reload() function that acts recursively.
4
4
5 Python's normal :func:`python:reload` function only reloads the module that it's
5 Python's normal :func:`python:reload` function only reloads the module that it's
6 passed. The :func:`reload` function in this module also reloads everything
6 passed. The :func:`reload` function in this module also reloads everything
7 imported from that module, which is useful when you're changing files deep
7 imported from that module, which is useful when you're changing files deep
8 inside a package.
8 inside a package.
9
9
10 To use this as your default reload function, type this for Python 2::
10 To use this as your default reload function, type this for Python 2::
11
11
12 import __builtin__
12 import __builtin__
13 from IPython.lib import deepreload
13 from IPython.lib import deepreload
14 __builtin__.reload = deepreload.reload
14 __builtin__.reload = deepreload.reload
15
15
16 Or this for Python 3::
16 Or this for Python 3::
17
17
18 import builtins
18 import builtins
19 from IPython.lib import deepreload
19 from IPython.lib import deepreload
20 builtins.reload = deepreload.reload
20 builtins.reload = deepreload.reload
21
21
22 A reference to the original :func:`python:reload` is stored in this module as
22 A reference to the original :func:`python:reload` is stored in this module as
23 :data:`original_reload`, so you can restore it later.
23 :data:`original_reload`, so you can restore it later.
24
24
25 This code is almost entirely based on knee.py, which is a Python
25 This code is almost entirely based on knee.py, which is a Python
26 re-implementation of hierarchical module import.
26 re-implementation of hierarchical module import.
27 """
27 """
28 #*****************************************************************************
28 #*****************************************************************************
29 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
29 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
30 #
30 #
31 # Distributed under the terms of the BSD License. The full license is in
31 # Distributed under the terms of the BSD License. The full license is in
32 # the file COPYING, distributed as part of this software.
32 # the file COPYING, distributed as part of this software.
33 #*****************************************************************************
33 #*****************************************************************************
34
34
35 import builtins as builtin_mod
35 import builtins as builtin_mod
36 from contextlib import contextmanager
36 from contextlib import contextmanager
37 import imp
37 import imp
38 import sys
38 import sys
39
39
40 from types import ModuleType
40 from types import ModuleType
41 from warnings import warn
41 from warnings import warn
42 import types
42 import types
43
43
44 original_import = builtin_mod.__import__
44 original_import = builtin_mod.__import__
45
45
46 @contextmanager
46 @contextmanager
47 def replace_import_hook(new_import):
47 def replace_import_hook(new_import):
48 saved_import = builtin_mod.__import__
48 saved_import = builtin_mod.__import__
49 builtin_mod.__import__ = new_import
49 builtin_mod.__import__ = new_import
50 try:
50 try:
51 yield
51 yield
52 finally:
52 finally:
53 builtin_mod.__import__ = saved_import
53 builtin_mod.__import__ = saved_import
54
54
55 def get_parent(globals, level):
55 def get_parent(globals, level):
56 """
56 """
57 parent, name = get_parent(globals, level)
57 parent, name = get_parent(globals, level)
58
58
59 Return the package that an import is being performed in. If globals comes
59 Return the package that an import is being performed in. If globals comes
60 from the module foo.bar.bat (not itself a package), this returns the
60 from the module foo.bar.bat (not itself a package), this returns the
61 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
61 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
62 the package's entry in sys.modules is returned.
62 the package's entry in sys.modules is returned.
63
63
64 If globals doesn't come from a package or a module in a package, or a
64 If globals doesn't come from a package or a module in a package, or a
65 corresponding entry is not found in sys.modules, None is returned.
65 corresponding entry is not found in sys.modules, None is returned.
66 """
66 """
67 orig_level = level
67 orig_level = level
68
68
69 if not level or not isinstance(globals, dict):
69 if not level or not isinstance(globals, dict):
70 return None, ''
70 return None, ''
71
71
72 pkgname = globals.get('__package__', None)
72 pkgname = globals.get('__package__', None)
73
73
74 if pkgname is not None:
74 if pkgname is not None:
75 # __package__ is set, so use it
75 # __package__ is set, so use it
76 if not hasattr(pkgname, 'rindex'):
76 if not hasattr(pkgname, 'rindex'):
77 raise ValueError('__package__ set to non-string')
77 raise ValueError('__package__ set to non-string')
78 if len(pkgname) == 0:
78 if len(pkgname) == 0:
79 if level > 0:
79 if level > 0:
80 raise ValueError('Attempted relative import in non-package')
80 raise ValueError('Attempted relative import in non-package')
81 return None, ''
81 return None, ''
82 name = pkgname
82 name = pkgname
83 else:
83 else:
84 # __package__ not set, so figure it out and set it
84 # __package__ not set, so figure it out and set it
85 if '__name__' not in globals:
85 if '__name__' not in globals:
86 return None, ''
86 return None, ''
87 modname = globals['__name__']
87 modname = globals['__name__']
88
88
89 if '__path__' in globals:
89 if '__path__' in globals:
90 # __path__ is set, so modname is already the package name
90 # __path__ is set, so modname is already the package name
91 globals['__package__'] = name = modname
91 globals['__package__'] = name = modname
92 else:
92 else:
93 # Normal module, so work out the package name if any
93 # Normal module, so work out the package name if any
94 lastdot = modname.rfind('.')
94 lastdot = modname.rfind('.')
95 if lastdot < 0 < level:
95 if lastdot < 0 < level:
96 raise ValueError("Attempted relative import in non-package")
96 raise ValueError("Attempted relative import in non-package")
97 if lastdot < 0:
97 if lastdot < 0:
98 globals['__package__'] = None
98 globals['__package__'] = None
99 return None, ''
99 return None, ''
100 globals['__package__'] = name = modname[:lastdot]
100 globals['__package__'] = name = modname[:lastdot]
101
101
102 dot = len(name)
102 dot = len(name)
103 for x in range(level, 1, -1):
103 for x in range(level, 1, -1):
104 try:
104 try:
105 dot = name.rindex('.', 0, dot)
105 dot = name.rindex('.', 0, dot)
106 except ValueError:
106 except ValueError:
107 raise ValueError("attempted relative import beyond top-level "
107 raise ValueError("attempted relative import beyond top-level "
108 "package")
108 "package")
109 name = name[:dot]
109 name = name[:dot]
110
110
111 try:
111 try:
112 parent = sys.modules[name]
112 parent = sys.modules[name]
113 except:
113 except:
114 if orig_level < 1:
114 if orig_level < 1:
115 warn("Parent module '%.200s' not found while handling absolute "
115 warn("Parent module '%.200s' not found while handling absolute "
116 "import" % name)
116 "import" % name)
117 parent = None
117 parent = None
118 else:
118 else:
119 raise SystemError("Parent module '%.200s' not loaded, cannot "
119 raise SystemError("Parent module '%.200s' not loaded, cannot "
120 "perform relative import" % name)
120 "perform relative import" % name)
121
121
122 # We expect, but can't guarantee, if parent != None, that:
122 # We expect, but can't guarantee, if parent != None, that:
123 # - parent.__name__ == name
123 # - parent.__name__ == name
124 # - parent.__dict__ is globals
124 # - parent.__dict__ is globals
125 # If this is violated... Who cares?
125 # If this is violated... Who cares?
126 return parent, name
126 return parent, name
127
127
128 def load_next(mod, altmod, name, buf):
128 def load_next(mod, altmod, name, buf):
129 """
129 """
130 mod, name, buf = load_next(mod, altmod, name, buf)
130 mod, name, buf = load_next(mod, altmod, name, buf)
131
131
132 altmod is either None or same as mod
132 altmod is either None or same as mod
133 """
133 """
134
134
135 if len(name) == 0:
135 if len(name) == 0:
136 # completely empty module name should only happen in
136 # completely empty module name should only happen in
137 # 'from . import' (or '__import__("")')
137 # 'from . import' (or '__import__("")')
138 return mod, None, buf
138 return mod, None, buf
139
139
140 dot = name.find('.')
140 dot = name.find('.')
141 if dot == 0:
141 if dot == 0:
142 raise ValueError('Empty module name')
142 raise ValueError('Empty module name')
143
143
144 if dot < 0:
144 if dot < 0:
145 subname = name
145 subname = name
146 next = None
146 next = None
147 else:
147 else:
148 subname = name[:dot]
148 subname = name[:dot]
149 next = name[dot+1:]
149 next = name[dot+1:]
150
150
151 if buf != '':
151 if buf != '':
152 buf += '.'
152 buf += '.'
153 buf += subname
153 buf += subname
154
154
155 result = import_submodule(mod, subname, buf)
155 result = import_submodule(mod, subname, buf)
156 if result is None and mod != altmod:
156 if result is None and mod != altmod:
157 result = import_submodule(altmod, subname, subname)
157 result = import_submodule(altmod, subname, subname)
158 if result is not None:
158 if result is not None:
159 buf = subname
159 buf = subname
160
160
161 if result is None:
161 if result is None:
162 raise ImportError("No module named %.200s" % name)
162 raise ImportError("No module named %.200s" % name)
163
163
164 return result, next, buf
164 return result, next, buf
165
165
166
166
167 # Need to keep track of what we've already reloaded to prevent cyclic evil
167 # Need to keep track of what we've already reloaded to prevent cyclic evil
168 found_now = {}
168 found_now = {}
169
169
170 def import_submodule(mod, subname, fullname):
170 def import_submodule(mod, subname, fullname):
171 """m = import_submodule(mod, subname, fullname)"""
171 """m = import_submodule(mod, subname, fullname)"""
172 # Require:
172 # Require:
173 # if mod == None: subname == fullname
173 # if mod == None: subname == fullname
174 # else: mod.__name__ + "." + subname == fullname
174 # else: mod.__name__ + "." + subname == fullname
175
175
176 global found_now
176 global found_now
177 if fullname in found_now and fullname in sys.modules:
177 if fullname in found_now and fullname in sys.modules:
178 m = sys.modules[fullname]
178 m = sys.modules[fullname]
179 else:
179 else:
180 print('Reloading', fullname)
180 print('Reloading', fullname)
181 found_now[fullname] = 1
181 found_now[fullname] = 1
182 oldm = sys.modules.get(fullname, None)
182 oldm = sys.modules.get(fullname, None)
183
183
184 if mod is None:
184 if mod is None:
185 path = None
185 path = None
186 elif hasattr(mod, '__path__'):
186 elif hasattr(mod, '__path__'):
187 path = mod.__path__
187 path = mod.__path__
188 else:
188 else:
189 return None
189 return None
190
190
191 try:
191 try:
192 # This appears to be necessary on Python 3, because imp.find_module()
192 # This appears to be necessary on Python 3, because imp.find_module()
193 # tries to import standard libraries (like io) itself, and we don't
193 # tries to import standard libraries (like io) itself, and we don't
194 # want them to be processed by our deep_import_hook.
194 # want them to be processed by our deep_import_hook.
195 with replace_import_hook(original_import):
195 with replace_import_hook(original_import):
196 fp, filename, stuff = imp.find_module(subname, path)
196 fp, filename, stuff = imp.find_module(subname, path)
197 except ImportError:
197 except ImportError:
198 return None
198 return None
199
199
200 try:
200 try:
201 m = imp.load_module(fullname, fp, filename, stuff)
201 m = imp.load_module(fullname, fp, filename, stuff)
202 except:
202 except:
203 # load_module probably removed name from modules because of
203 # load_module probably removed name from modules because of
204 # the error. Put back the original module object.
204 # the error. Put back the original module object.
205 if oldm:
205 if oldm:
206 sys.modules[fullname] = oldm
206 sys.modules[fullname] = oldm
207 raise
207 raise
208 finally:
208 finally:
209 if fp: fp.close()
209 if fp: fp.close()
210
210
211 add_submodule(mod, m, fullname, subname)
211 add_submodule(mod, m, fullname, subname)
212
212
213 return m
213 return m
214
214
215 def add_submodule(mod, submod, fullname, subname):
215 def add_submodule(mod, submod, fullname, subname):
216 """mod.{subname} = submod"""
216 """mod.{subname} = submod"""
217 if mod is None:
217 if mod is None:
218 return #Nothing to do here.
218 return #Nothing to do here.
219
219
220 if submod is None:
220 if submod is None:
221 submod = sys.modules[fullname]
221 submod = sys.modules[fullname]
222
222
223 setattr(mod, subname, submod)
223 setattr(mod, subname, submod)
224
224
225 return
225 return
226
226
227 def ensure_fromlist(mod, fromlist, buf, recursive):
227 def ensure_fromlist(mod, fromlist, buf, recursive):
228 """Handle 'from module import a, b, c' imports."""
228 """Handle 'from module import a, b, c' imports."""
229 if not hasattr(mod, '__path__'):
229 if not hasattr(mod, '__path__'):
230 return
230 return
231 for item in fromlist:
231 for item in fromlist:
232 if not hasattr(item, 'rindex'):
232 if not hasattr(item, 'rindex'):
233 raise TypeError("Item in ``from list'' not a string")
233 raise TypeError("Item in ``from list'' not a string")
234 if item == '*':
234 if item == '*':
235 if recursive:
235 if recursive:
236 continue # avoid endless recursion
236 continue # avoid endless recursion
237 try:
237 try:
238 all = mod.__all__
238 all = mod.__all__
239 except AttributeError:
239 except AttributeError:
240 pass
240 pass
241 else:
241 else:
242 ret = ensure_fromlist(mod, all, buf, 1)
242 ret = ensure_fromlist(mod, all, buf, 1)
243 if not ret:
243 if not ret:
244 return 0
244 return 0
245 elif not hasattr(mod, item):
245 elif not hasattr(mod, item):
246 import_submodule(mod, item, buf + '.' + item)
246 import_submodule(mod, item, buf + '.' + item)
247
247
248 def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1):
248 def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1):
249 """Replacement for __import__()"""
249 """Replacement for __import__()"""
250 parent, buf = get_parent(globals, level)
250 parent, buf = get_parent(globals, level)
251
251
252 head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
252 head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
253
253
254 tail = head
254 tail = head
255 while name:
255 while name:
256 tail, name, buf = load_next(tail, tail, name, buf)
256 tail, name, buf = load_next(tail, tail, name, buf)
257
257
258 # If tail is None, both get_parent and load_next found
258 # If tail is None, both get_parent and load_next found
259 # an empty module name: someone called __import__("") or
259 # an empty module name: someone called __import__("") or
260 # doctored faulty bytecode
260 # doctored faulty bytecode
261 if tail is None:
261 if tail is None:
262 raise ValueError('Empty module name')
262 raise ValueError('Empty module name')
263
263
264 if not fromlist:
264 if not fromlist:
265 return head
265 return head
266
266
267 ensure_fromlist(tail, fromlist, buf, 0)
267 ensure_fromlist(tail, fromlist, buf, 0)
268 return tail
268 return tail
269
269
270 modules_reloading = {}
270 modules_reloading = {}
271
271
272 def deep_reload_hook(m):
272 def deep_reload_hook(m):
273 """Replacement for reload()."""
273 """Replacement for reload()."""
274 # Hardcode this one as it would raise a NotImplemeentedError from the
274 # Hardcode this one as it would raise a NotImplemeentedError from the
275 # bowels of Python and screw up the import machinery after.
275 # bowels of Python and screw up the import machinery after.
276 # unlike other imports the `exclude` list aleady in place is not enough.
276 # unlike other imports the `exclude` list already in place is not enough.
277
277
278 if m is types:
278 if m is types:
279 return m
279 return m
280 if not isinstance(m, ModuleType):
280 if not isinstance(m, ModuleType):
281 raise TypeError("reload() argument must be module")
281 raise TypeError("reload() argument must be module")
282
282
283 name = m.__name__
283 name = m.__name__
284
284
285 if name not in sys.modules:
285 if name not in sys.modules:
286 raise ImportError("reload(): module %.200s not in sys.modules" % name)
286 raise ImportError("reload(): module %.200s not in sys.modules" % name)
287
287
288 global modules_reloading
288 global modules_reloading
289 try:
289 try:
290 return modules_reloading[name]
290 return modules_reloading[name]
291 except:
291 except:
292 modules_reloading[name] = m
292 modules_reloading[name] = m
293
293
294 dot = name.rfind('.')
294 dot = name.rfind('.')
295 if dot < 0:
295 if dot < 0:
296 subname = name
296 subname = name
297 path = None
297 path = None
298 else:
298 else:
299 try:
299 try:
300 parent = sys.modules[name[:dot]]
300 parent = sys.modules[name[:dot]]
301 except KeyError:
301 except KeyError:
302 modules_reloading.clear()
302 modules_reloading.clear()
303 raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot])
303 raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot])
304 subname = name[dot+1:]
304 subname = name[dot+1:]
305 path = getattr(parent, "__path__", None)
305 path = getattr(parent, "__path__", None)
306
306
307 try:
307 try:
308 # This appears to be necessary on Python 3, because imp.find_module()
308 # This appears to be necessary on Python 3, because imp.find_module()
309 # tries to import standard libraries (like io) itself, and we don't
309 # tries to import standard libraries (like io) itself, and we don't
310 # want them to be processed by our deep_import_hook.
310 # want them to be processed by our deep_import_hook.
311 with replace_import_hook(original_import):
311 with replace_import_hook(original_import):
312 fp, filename, stuff = imp.find_module(subname, path)
312 fp, filename, stuff = imp.find_module(subname, path)
313 finally:
313 finally:
314 modules_reloading.clear()
314 modules_reloading.clear()
315
315
316 try:
316 try:
317 newm = imp.load_module(name, fp, filename, stuff)
317 newm = imp.load_module(name, fp, filename, stuff)
318 except:
318 except:
319 # load_module probably removed name from modules because of
319 # load_module probably removed name from modules because of
320 # the error. Put back the original module object.
320 # the error. Put back the original module object.
321 sys.modules[name] = m
321 sys.modules[name] = m
322 raise
322 raise
323 finally:
323 finally:
324 if fp: fp.close()
324 if fp: fp.close()
325
325
326 modules_reloading.clear()
326 modules_reloading.clear()
327 return newm
327 return newm
328
328
329 # Save the original hooks
329 # Save the original hooks
330 original_reload = imp.reload
330 original_reload = imp.reload
331
331
332 # Replacement for reload()
332 # Replacement for reload()
333 def reload(module, exclude=('sys', 'os.path', 'builtins', '__main__',
333 def reload(module, exclude=('sys', 'os.path', 'builtins', '__main__',
334 'numpy', 'numpy._globals')):
334 'numpy', 'numpy._globals')):
335 """Recursively reload all modules used in the given module. Optionally
335 """Recursively reload all modules used in the given module. Optionally
336 takes a list of modules to exclude from reloading. The default exclude
336 takes a list of modules to exclude from reloading. The default exclude
337 list contains sys, __main__, and __builtin__, to prevent, e.g., resetting
337 list contains sys, __main__, and __builtin__, to prevent, e.g., resetting
338 display, exception, and io hooks.
338 display, exception, and io hooks.
339 """
339 """
340 global found_now
340 global found_now
341 for i in exclude:
341 for i in exclude:
342 found_now[i] = 1
342 found_now[i] = 1
343 try:
343 try:
344 with replace_import_hook(deep_import_hook):
344 with replace_import_hook(deep_import_hook):
345 return deep_reload_hook(module)
345 return deep_reload_hook(module)
346 finally:
346 finally:
347 found_now = {}
347 found_now = {}
@@ -1,667 +1,667 b''
1 """Module for interactive demos using IPython.
1 """Module for interactive demos using IPython.
2
2
3 This module implements a few classes for running Python scripts interactively
3 This module implements a few classes for running Python scripts interactively
4 in IPython for demonstrations. With very simple markup (a few tags in
4 in IPython for demonstrations. With very simple markup (a few tags in
5 comments), you can control points where the script stops executing and returns
5 comments), you can control points where the script stops executing and returns
6 control to IPython.
6 control to IPython.
7
7
8
8
9 Provided classes
9 Provided classes
10 ----------------
10 ----------------
11
11
12 The classes are (see their docstrings for further details):
12 The classes are (see their docstrings for further details):
13
13
14 - Demo: pure python demos
14 - Demo: pure python demos
15
15
16 - IPythonDemo: demos with input to be processed by IPython as if it had been
16 - IPythonDemo: demos with input to be processed by IPython as if it had been
17 typed interactively (so magics work, as well as any other special syntax you
17 typed interactively (so magics work, as well as any other special syntax you
18 may have added via input prefilters).
18 may have added via input prefilters).
19
19
20 - LineDemo: single-line version of the Demo class. These demos are executed
20 - LineDemo: single-line version of the Demo class. These demos are executed
21 one line at a time, and require no markup.
21 one line at a time, and require no markup.
22
22
23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
24 executed a line at a time, but processed via IPython).
24 executed a line at a time, but processed via IPython).
25
25
26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
27 declares an empty marquee and a pre_cmd that clears the screen before each
27 declares an empty marquee and a pre_cmd that clears the screen before each
28 block (see Subclassing below).
28 block (see Subclassing below).
29
29
30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
31 classes.
31 classes.
32
32
33 Inheritance diagram:
33 Inheritance diagram:
34
34
35 .. inheritance-diagram:: IPython.lib.demo
35 .. inheritance-diagram:: IPython.lib.demo
36 :parts: 3
36 :parts: 3
37
37
38 Subclassing
38 Subclassing
39 -----------
39 -----------
40
40
41 The classes here all include a few methods meant to make customization by
41 The classes here all include a few methods meant to make customization by
42 subclassing more convenient. Their docstrings below have some more details:
42 subclassing more convenient. Their docstrings below have some more details:
43
43
44 - highlight(): format every block and optionally highlight comments and
44 - highlight(): format every block and optionally highlight comments and
45 docstring content.
45 docstring content.
46
46
47 - marquee(): generates a marquee to provide visible on-screen markers at each
47 - marquee(): generates a marquee to provide visible on-screen markers at each
48 block start and end.
48 block start and end.
49
49
50 - pre_cmd(): run right before the execution of each block.
50 - pre_cmd(): run right before the execution of each block.
51
51
52 - post_cmd(): run right after the execution of each block. If the block
52 - post_cmd(): run right after the execution of each block. If the block
53 raises an exception, this is NOT called.
53 raises an exception, this is NOT called.
54
54
55
55
56 Operation
56 Operation
57 ---------
57 ---------
58
58
59 The file is run in its own empty namespace (though you can pass it a string of
59 The file is run in its own empty namespace (though you can pass it a string of
60 arguments as if in a command line environment, and it will see those as
60 arguments as if in a command line environment, and it will see those as
61 sys.argv). But at each stop, the global IPython namespace is updated with the
61 sys.argv). But at each stop, the global IPython namespace is updated with the
62 current internal demo namespace, so you can work interactively with the data
62 current internal demo namespace, so you can work interactively with the data
63 accumulated so far.
63 accumulated so far.
64
64
65 By default, each block of code is printed (with syntax highlighting) before
65 By default, each block of code is printed (with syntax highlighting) before
66 executing it and you have to confirm execution. This is intended to show the
66 executing it and you have to confirm execution. This is intended to show the
67 code to an audience first so you can discuss it, and only proceed with
67 code to an audience first so you can discuss it, and only proceed with
68 execution once you agree. There are a few tags which allow you to modify this
68 execution once you agree. There are a few tags which allow you to modify this
69 behavior.
69 behavior.
70
70
71 The supported tags are:
71 The supported tags are:
72
72
73 # <demo> stop
73 # <demo> stop
74
74
75 Defines block boundaries, the points where IPython stops execution of the
75 Defines block boundaries, the points where IPython stops execution of the
76 file and returns to the interactive prompt.
76 file and returns to the interactive prompt.
77
77
78 You can optionally mark the stop tag with extra dashes before and after the
78 You can optionally mark the stop tag with extra dashes before and after the
79 word 'stop', to help visually distinguish the blocks in a text editor:
79 word 'stop', to help visually distinguish the blocks in a text editor:
80
80
81 # <demo> --- stop ---
81 # <demo> --- stop ---
82
82
83
83
84 # <demo> silent
84 # <demo> silent
85
85
86 Make a block execute silently (and hence automatically). Typically used in
86 Make a block execute silently (and hence automatically). Typically used in
87 cases where you have some boilerplate or initialization code which you need
87 cases where you have some boilerplate or initialization code which you need
88 executed but do not want to be seen in the demo.
88 executed but do not want to be seen in the demo.
89
89
90 # <demo> auto
90 # <demo> auto
91
91
92 Make a block execute automatically, but still being printed. Useful for
92 Make a block execute automatically, but still being printed. Useful for
93 simple code which does not warrant discussion, since it avoids the extra
93 simple code which does not warrant discussion, since it avoids the extra
94 manual confirmation.
94 manual confirmation.
95
95
96 # <demo> auto_all
96 # <demo> auto_all
97
97
98 This tag can _only_ be in the first block, and if given it overrides the
98 This tag can _only_ be in the first block, and if given it overrides the
99 individual auto tags to make the whole demo fully automatic (no block asks
99 individual auto tags to make the whole demo fully automatic (no block asks
100 for confirmation). It can also be given at creation time (or the attribute
100 for confirmation). It can also be given at creation time (or the attribute
101 set later) to override what's in the file.
101 set later) to override what's in the file.
102
102
103 While _any_ python file can be run as a Demo instance, if there are no stop
103 While _any_ python file can be run as a Demo instance, if there are no stop
104 tags the whole file will run in a single block (no different that calling
104 tags the whole file will run in a single block (no different that calling
105 first %pycat and then %run). The minimal markup to make this useful is to
105 first %pycat and then %run). The minimal markup to make this useful is to
106 place a set of stop tags; the other tags are only there to let you fine-tune
106 place a set of stop tags; the other tags are only there to let you fine-tune
107 the execution.
107 the execution.
108
108
109 This is probably best explained with the simple example file below. You can
109 This is probably best explained with the simple example file below. You can
110 copy this into a file named ex_demo.py, and try running it via::
110 copy this into a file named ex_demo.py, and try running it via::
111
111
112 from IPython.lib.demo import Demo
112 from IPython.lib.demo import Demo
113 d = Demo('ex_demo.py')
113 d = Demo('ex_demo.py')
114 d()
114 d()
115
115
116 Each time you call the demo object, it runs the next block. The demo object
116 Each time you call the demo object, it runs the next block. The demo object
117 has a few useful methods for navigation, like again(), edit(), jump(), seek()
117 has a few useful methods for navigation, like again(), edit(), jump(), seek()
118 and back(). It can be reset for a new run via reset() or reloaded from disk
118 and back(). It can be reset for a new run via reset() or reloaded from disk
119 (in case you've edited the source) via reload(). See their docstrings below.
119 (in case you've edited the source) via reload(). See their docstrings below.
120
120
121 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
121 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
122 been added to the "docs/examples/core" directory. Just cd to this directory in
122 been added to the "docs/examples/core" directory. Just cd to this directory in
123 an IPython session, and type::
123 an IPython session, and type::
124
124
125 %run demo-exercizer.py
125 %run demo-exercizer.py
126
126
127 and then follow the directions.
127 and then follow the directions.
128
128
129 Example
129 Example
130 -------
130 -------
131
131
132 The following is a very simple example of a valid demo file.
132 The following is a very simple example of a valid demo file.
133
133
134 ::
134 ::
135
135
136 #################### EXAMPLE DEMO <ex_demo.py> ###############################
136 #################### EXAMPLE DEMO <ex_demo.py> ###############################
137 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
137 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
138
138
139 print 'Hello, welcome to an interactive IPython demo.'
139 print 'Hello, welcome to an interactive IPython demo.'
140
140
141 # The mark below defines a block boundary, which is a point where IPython will
141 # The mark below defines a block boundary, which is a point where IPython will
142 # stop execution and return to the interactive prompt. The dashes are actually
142 # stop execution and return to the interactive prompt. The dashes are actually
143 # optional and used only as a visual aid to clearly separate blocks while
143 # optional and used only as a visual aid to clearly separate blocks while
144 # editing the demo code.
144 # editing the demo code.
145 # <demo> stop
145 # <demo> stop
146
146
147 x = 1
147 x = 1
148 y = 2
148 y = 2
149
149
150 # <demo> stop
150 # <demo> stop
151
151
152 # the mark below makes this block as silent
152 # the mark below makes this block as silent
153 # <demo> silent
153 # <demo> silent
154
154
155 print 'This is a silent block, which gets executed but not printed.'
155 print 'This is a silent block, which gets executed but not printed.'
156
156
157 # <demo> stop
157 # <demo> stop
158 # <demo> auto
158 # <demo> auto
159 print 'This is an automatic block.'
159 print 'This is an automatic block.'
160 print 'It is executed without asking for confirmation, but printed.'
160 print 'It is executed without asking for confirmation, but printed.'
161 z = x+y
161 z = x+y
162
162
163 print 'z=',x
163 print 'z=',x
164
164
165 # <demo> stop
165 # <demo> stop
166 # This is just another normal block.
166 # This is just another normal block.
167 print 'z is now:', z
167 print 'z is now:', z
168
168
169 print 'bye!'
169 print 'bye!'
170 ################### END EXAMPLE DEMO <ex_demo.py> ############################
170 ################### END EXAMPLE DEMO <ex_demo.py> ############################
171 """
171 """
172
172
173
173
174 #*****************************************************************************
174 #*****************************************************************************
175 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
175 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
176 #
176 #
177 # Distributed under the terms of the BSD License. The full license is in
177 # Distributed under the terms of the BSD License. The full license is in
178 # the file COPYING, distributed as part of this software.
178 # the file COPYING, distributed as part of this software.
179 #
179 #
180 #*****************************************************************************
180 #*****************************************************************************
181
181
182 import os
182 import os
183 import re
183 import re
184 import shlex
184 import shlex
185 import sys
185 import sys
186 import pygments
186 import pygments
187
187
188 from IPython.utils.text import marquee
188 from IPython.utils.text import marquee
189 from IPython.utils import openpy
189 from IPython.utils import openpy
190 from IPython.utils import py3compat
190 from IPython.utils import py3compat
191 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
191 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
192
192
193 class DemoError(Exception): pass
193 class DemoError(Exception): pass
194
194
195 def re_mark(mark):
195 def re_mark(mark):
196 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
196 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
197
197
198 class Demo(object):
198 class Demo(object):
199
199
200 re_stop = re_mark('-*\s?stop\s?-*')
200 re_stop = re_mark('-*\s?stop\s?-*')
201 re_silent = re_mark('silent')
201 re_silent = re_mark('silent')
202 re_auto = re_mark('auto')
202 re_auto = re_mark('auto')
203 re_auto_all = re_mark('auto_all')
203 re_auto_all = re_mark('auto_all')
204
204
205 def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False,
205 def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False,
206 formatter='terminal', style='default'):
206 formatter='terminal', style='default'):
207 """Make a new demo object. To run the demo, simply call the object.
207 """Make a new demo object. To run the demo, simply call the object.
208
208
209 See the module docstring for full details and an example (you can use
209 See the module docstring for full details and an example (you can use
210 IPython.Demo? in IPython to see it).
210 IPython.Demo? in IPython to see it).
211
211
212 Inputs:
212 Inputs:
213
213
214 - src is either a file, or file-like object, or a
214 - src is either a file, or file-like object, or a
215 string that can be resolved to a filename.
215 string that can be resolved to a filename.
216
216
217 Optional inputs:
217 Optional inputs:
218
218
219 - title: a string to use as the demo name. Of most use when the demo
219 - title: a string to use as the demo name. Of most use when the demo
220 you are making comes from an object that has no filename, or if you
220 you are making comes from an object that has no filename, or if you
221 want an alternate denotation distinct from the filename.
221 want an alternate denotation distinct from the filename.
222
222
223 - arg_str(''): a string of arguments, internally converted to a list
223 - arg_str(''): a string of arguments, internally converted to a list
224 just like sys.argv, so the demo script can see a similar
224 just like sys.argv, so the demo script can see a similar
225 environment.
225 environment.
226
226
227 - auto_all(None): global flag to run all blocks automatically without
227 - auto_all(None): global flag to run all blocks automatically without
228 confirmation. This attribute overrides the block-level tags and
228 confirmation. This attribute overrides the block-level tags and
229 applies to the whole demo. It is an attribute of the object, and
229 applies to the whole demo. It is an attribute of the object, and
230 can be changed at runtime simply by reassigning it to a boolean
230 can be changed at runtime simply by reassigning it to a boolean
231 value.
231 value.
232
232
233 - format_rst(False): a bool to enable comments and doc strings
233 - format_rst(False): a bool to enable comments and doc strings
234 formating with pygments rst lexer
234 formatting with pygments rst lexer
235
235
236 - formatter('terminal'): a string of pygments formatter name to be
236 - formatter('terminal'): a string of pygments formatter name to be
237 used. Useful values for terminals: terminal, terminal256,
237 used. Useful values for terminals: terminal, terminal256,
238 terminal16m
238 terminal16m
239
239
240 - style('default'): a string of pygments style name to be used.
240 - style('default'): a string of pygments style name to be used.
241 """
241 """
242 if hasattr(src, "read"):
242 if hasattr(src, "read"):
243 # It seems to be a file or a file-like object
243 # It seems to be a file or a file-like object
244 self.fname = "from a file-like object"
244 self.fname = "from a file-like object"
245 if title == '':
245 if title == '':
246 self.title = "from a file-like object"
246 self.title = "from a file-like object"
247 else:
247 else:
248 self.title = title
248 self.title = title
249 else:
249 else:
250 # Assume it's a string or something that can be converted to one
250 # Assume it's a string or something that can be converted to one
251 self.fname = src
251 self.fname = src
252 if title == '':
252 if title == '':
253 (filepath, filename) = os.path.split(src)
253 (filepath, filename) = os.path.split(src)
254 self.title = filename
254 self.title = filename
255 else:
255 else:
256 self.title = title
256 self.title = title
257 self.sys_argv = [src] + shlex.split(arg_str)
257 self.sys_argv = [src] + shlex.split(arg_str)
258 self.auto_all = auto_all
258 self.auto_all = auto_all
259 self.src = src
259 self.src = src
260
260
261 self.inside_ipython = "get_ipython" in globals()
261 self.inside_ipython = "get_ipython" in globals()
262 if self.inside_ipython:
262 if self.inside_ipython:
263 # get a few things from ipython. While it's a bit ugly design-wise,
263 # get a few things from ipython. While it's a bit ugly design-wise,
264 # it ensures that things like color scheme and the like are always in
264 # it ensures that things like color scheme and the like are always in
265 # sync with the ipython mode being used. This class is only meant to
265 # sync with the ipython mode being used. This class is only meant to
266 # be used inside ipython anyways, so it's OK.
266 # be used inside ipython anyways, so it's OK.
267 ip = get_ipython() # this is in builtins whenever IPython is running
267 ip = get_ipython() # this is in builtins whenever IPython is running
268 self.ip_ns = ip.user_ns
268 self.ip_ns = ip.user_ns
269 self.ip_colorize = ip.pycolorize
269 self.ip_colorize = ip.pycolorize
270 self.ip_showtb = ip.showtraceback
270 self.ip_showtb = ip.showtraceback
271 self.ip_run_cell = ip.run_cell
271 self.ip_run_cell = ip.run_cell
272 self.shell = ip
272 self.shell = ip
273
273
274 self.formatter = pygments.formatters.get_formatter_by_name(formatter,
274 self.formatter = pygments.formatters.get_formatter_by_name(formatter,
275 style=style)
275 style=style)
276 self.python_lexer = pygments.lexers.get_lexer_by_name("py3")
276 self.python_lexer = pygments.lexers.get_lexer_by_name("py3")
277 self.format_rst = format_rst
277 self.format_rst = format_rst
278 if format_rst:
278 if format_rst:
279 self.rst_lexer = pygments.lexers.get_lexer_by_name("rst")
279 self.rst_lexer = pygments.lexers.get_lexer_by_name("rst")
280
280
281 # load user data and initialize data structures
281 # load user data and initialize data structures
282 self.reload()
282 self.reload()
283
283
284 def fload(self):
284 def fload(self):
285 """Load file object."""
285 """Load file object."""
286 # read data and parse into blocks
286 # read data and parse into blocks
287 if hasattr(self, 'fobj') and self.fobj is not None:
287 if hasattr(self, 'fobj') and self.fobj is not None:
288 self.fobj.close()
288 self.fobj.close()
289 if hasattr(self.src, "read"):
289 if hasattr(self.src, "read"):
290 # It seems to be a file or a file-like object
290 # It seems to be a file or a file-like object
291 self.fobj = self.src
291 self.fobj = self.src
292 else:
292 else:
293 # Assume it's a string or something that can be converted to one
293 # Assume it's a string or something that can be converted to one
294 self.fobj = openpy.open(self.fname)
294 self.fobj = openpy.open(self.fname)
295
295
296 def reload(self):
296 def reload(self):
297 """Reload source from disk and initialize state."""
297 """Reload source from disk and initialize state."""
298 self.fload()
298 self.fload()
299
299
300 self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
300 self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
301 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
301 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
302 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
302 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
303 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
303 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
304
304
305 # if auto_all is not given (def. None), we read it from the file
305 # if auto_all is not given (def. None), we read it from the file
306 if self.auto_all is None:
306 if self.auto_all is None:
307 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
307 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
308 else:
308 else:
309 self.auto_all = bool(self.auto_all)
309 self.auto_all = bool(self.auto_all)
310
310
311 # Clean the sources from all markup so it doesn't get displayed when
311 # Clean the sources from all markup so it doesn't get displayed when
312 # running the demo
312 # running the demo
313 src_blocks = []
313 src_blocks = []
314 auto_strip = lambda s: self.re_auto.sub('',s)
314 auto_strip = lambda s: self.re_auto.sub('',s)
315 for i,b in enumerate(src_b):
315 for i,b in enumerate(src_b):
316 if self._auto[i]:
316 if self._auto[i]:
317 src_blocks.append(auto_strip(b))
317 src_blocks.append(auto_strip(b))
318 else:
318 else:
319 src_blocks.append(b)
319 src_blocks.append(b)
320 # remove the auto_all marker
320 # remove the auto_all marker
321 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
321 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
322
322
323 self.nblocks = len(src_blocks)
323 self.nblocks = len(src_blocks)
324 self.src_blocks = src_blocks
324 self.src_blocks = src_blocks
325
325
326 # also build syntax-highlighted source
326 # also build syntax-highlighted source
327 self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
327 self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
328
328
329 # ensure clean namespace and seek offset
329 # ensure clean namespace and seek offset
330 self.reset()
330 self.reset()
331
331
332 def reset(self):
332 def reset(self):
333 """Reset the namespace and seek pointer to restart the demo"""
333 """Reset the namespace and seek pointer to restart the demo"""
334 self.user_ns = {}
334 self.user_ns = {}
335 self.finished = False
335 self.finished = False
336 self.block_index = 0
336 self.block_index = 0
337
337
338 def _validate_index(self,index):
338 def _validate_index(self,index):
339 if index<0 or index>=self.nblocks:
339 if index<0 or index>=self.nblocks:
340 raise ValueError('invalid block index %s' % index)
340 raise ValueError('invalid block index %s' % index)
341
341
342 def _get_index(self,index):
342 def _get_index(self,index):
343 """Get the current block index, validating and checking status.
343 """Get the current block index, validating and checking status.
344
344
345 Returns None if the demo is finished"""
345 Returns None if the demo is finished"""
346
346
347 if index is None:
347 if index is None:
348 if self.finished:
348 if self.finished:
349 print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
349 print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
350 return None
350 return None
351 index = self.block_index
351 index = self.block_index
352 else:
352 else:
353 self._validate_index(index)
353 self._validate_index(index)
354 return index
354 return index
355
355
356 def seek(self,index):
356 def seek(self,index):
357 """Move the current seek pointer to the given block.
357 """Move the current seek pointer to the given block.
358
358
359 You can use negative indices to seek from the end, with identical
359 You can use negative indices to seek from the end, with identical
360 semantics to those of Python lists."""
360 semantics to those of Python lists."""
361 if index<0:
361 if index<0:
362 index = self.nblocks + index
362 index = self.nblocks + index
363 self._validate_index(index)
363 self._validate_index(index)
364 self.block_index = index
364 self.block_index = index
365 self.finished = False
365 self.finished = False
366
366
367 def back(self,num=1):
367 def back(self,num=1):
368 """Move the seek pointer back num blocks (default is 1)."""
368 """Move the seek pointer back num blocks (default is 1)."""
369 self.seek(self.block_index-num)
369 self.seek(self.block_index-num)
370
370
371 def jump(self,num=1):
371 def jump(self,num=1):
372 """Jump a given number of blocks relative to the current one.
372 """Jump a given number of blocks relative to the current one.
373
373
374 The offset can be positive or negative, defaults to 1."""
374 The offset can be positive or negative, defaults to 1."""
375 self.seek(self.block_index+num)
375 self.seek(self.block_index+num)
376
376
377 def again(self):
377 def again(self):
378 """Move the seek pointer back one block and re-execute."""
378 """Move the seek pointer back one block and re-execute."""
379 self.back(1)
379 self.back(1)
380 self()
380 self()
381
381
382 def edit(self,index=None):
382 def edit(self,index=None):
383 """Edit a block.
383 """Edit a block.
384
384
385 If no number is given, use the last block executed.
385 If no number is given, use the last block executed.
386
386
387 This edits the in-memory copy of the demo, it does NOT modify the
387 This edits the in-memory copy of the demo, it does NOT modify the
388 original source file. If you want to do that, simply open the file in
388 original source file. If you want to do that, simply open the file in
389 an editor and use reload() when you make changes to the file. This
389 an editor and use reload() when you make changes to the file. This
390 method is meant to let you change a block during a demonstration for
390 method is meant to let you change a block during a demonstration for
391 explanatory purposes, without damaging your original script."""
391 explanatory purposes, without damaging your original script."""
392
392
393 index = self._get_index(index)
393 index = self._get_index(index)
394 if index is None:
394 if index is None:
395 return
395 return
396 # decrease the index by one (unless we're at the very beginning), so
396 # decrease the index by one (unless we're at the very beginning), so
397 # that the default demo.edit() call opens up the sblock we've last run
397 # that the default demo.edit() call opens up the sblock we've last run
398 if index>0:
398 if index>0:
399 index -= 1
399 index -= 1
400
400
401 filename = self.shell.mktempfile(self.src_blocks[index])
401 filename = self.shell.mktempfile(self.src_blocks[index])
402 self.shell.hooks.editor(filename,1)
402 self.shell.hooks.editor(filename,1)
403 with open(filename, 'r') as f:
403 with open(filename, 'r') as f:
404 new_block = f.read()
404 new_block = f.read()
405 # update the source and colored block
405 # update the source and colored block
406 self.src_blocks[index] = new_block
406 self.src_blocks[index] = new_block
407 self.src_blocks_colored[index] = self.highlight(new_block)
407 self.src_blocks_colored[index] = self.highlight(new_block)
408 self.block_index = index
408 self.block_index = index
409 # call to run with the newly edited index
409 # call to run with the newly edited index
410 self()
410 self()
411
411
412 def show(self,index=None):
412 def show(self,index=None):
413 """Show a single block on screen"""
413 """Show a single block on screen"""
414
414
415 index = self._get_index(index)
415 index = self._get_index(index)
416 if index is None:
416 if index is None:
417 return
417 return
418
418
419 print(self.marquee('<%s> block # %s (%s remaining)' %
419 print(self.marquee('<%s> block # %s (%s remaining)' %
420 (self.title,index,self.nblocks-index-1)))
420 (self.title,index,self.nblocks-index-1)))
421 print(self.src_blocks_colored[index])
421 print(self.src_blocks_colored[index])
422 sys.stdout.flush()
422 sys.stdout.flush()
423
423
424 def show_all(self):
424 def show_all(self):
425 """Show entire demo on screen, block by block"""
425 """Show entire demo on screen, block by block"""
426
426
427 fname = self.title
427 fname = self.title
428 title = self.title
428 title = self.title
429 nblocks = self.nblocks
429 nblocks = self.nblocks
430 silent = self._silent
430 silent = self._silent
431 marquee = self.marquee
431 marquee = self.marquee
432 for index,block in enumerate(self.src_blocks_colored):
432 for index,block in enumerate(self.src_blocks_colored):
433 if silent[index]:
433 if silent[index]:
434 print(marquee('<%s> SILENT block # %s (%s remaining)' %
434 print(marquee('<%s> SILENT block # %s (%s remaining)' %
435 (title,index,nblocks-index-1)))
435 (title,index,nblocks-index-1)))
436 else:
436 else:
437 print(marquee('<%s> block # %s (%s remaining)' %
437 print(marquee('<%s> block # %s (%s remaining)' %
438 (title,index,nblocks-index-1)))
438 (title,index,nblocks-index-1)))
439 print(block, end=' ')
439 print(block, end=' ')
440 sys.stdout.flush()
440 sys.stdout.flush()
441
441
442 def run_cell(self,source):
442 def run_cell(self,source):
443 """Execute a string with one or more lines of code"""
443 """Execute a string with one or more lines of code"""
444
444
445 exec(source, self.user_ns)
445 exec(source, self.user_ns)
446
446
447 def __call__(self,index=None):
447 def __call__(self,index=None):
448 """run a block of the demo.
448 """run a block of the demo.
449
449
450 If index is given, it should be an integer >=1 and <= nblocks. This
450 If index is given, it should be an integer >=1 and <= nblocks. This
451 means that the calling convention is one off from typical Python
451 means that the calling convention is one off from typical Python
452 lists. The reason for the inconsistency is that the demo always
452 lists. The reason for the inconsistency is that the demo always
453 prints 'Block n/N, and N is the total, so it would be very odd to use
453 prints 'Block n/N, and N is the total, so it would be very odd to use
454 zero-indexing here."""
454 zero-indexing here."""
455
455
456 index = self._get_index(index)
456 index = self._get_index(index)
457 if index is None:
457 if index is None:
458 return
458 return
459 try:
459 try:
460 marquee = self.marquee
460 marquee = self.marquee
461 next_block = self.src_blocks[index]
461 next_block = self.src_blocks[index]
462 self.block_index += 1
462 self.block_index += 1
463 if self._silent[index]:
463 if self._silent[index]:
464 print(marquee('Executing silent block # %s (%s remaining)' %
464 print(marquee('Executing silent block # %s (%s remaining)' %
465 (index,self.nblocks-index-1)))
465 (index,self.nblocks-index-1)))
466 else:
466 else:
467 self.pre_cmd()
467 self.pre_cmd()
468 self.show(index)
468 self.show(index)
469 if self.auto_all or self._auto[index]:
469 if self.auto_all or self._auto[index]:
470 print(marquee('output:'))
470 print(marquee('output:'))
471 else:
471 else:
472 print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
472 print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
473 ans = py3compat.input().strip()
473 ans = py3compat.input().strip()
474 if ans:
474 if ans:
475 print(marquee('Block NOT executed'))
475 print(marquee('Block NOT executed'))
476 return
476 return
477 try:
477 try:
478 save_argv = sys.argv
478 save_argv = sys.argv
479 sys.argv = self.sys_argv
479 sys.argv = self.sys_argv
480 self.run_cell(next_block)
480 self.run_cell(next_block)
481 self.post_cmd()
481 self.post_cmd()
482 finally:
482 finally:
483 sys.argv = save_argv
483 sys.argv = save_argv
484
484
485 except:
485 except:
486 if self.inside_ipython:
486 if self.inside_ipython:
487 self.ip_showtb(filename=self.fname)
487 self.ip_showtb(filename=self.fname)
488 else:
488 else:
489 if self.inside_ipython:
489 if self.inside_ipython:
490 self.ip_ns.update(self.user_ns)
490 self.ip_ns.update(self.user_ns)
491
491
492 if self.block_index == self.nblocks:
492 if self.block_index == self.nblocks:
493 mq1 = self.marquee('END OF DEMO')
493 mq1 = self.marquee('END OF DEMO')
494 if mq1:
494 if mq1:
495 # avoid spurious print if empty marquees are used
495 # avoid spurious print if empty marquees are used
496 print()
496 print()
497 print(mq1)
497 print(mq1)
498 print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
498 print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
499 self.finished = True
499 self.finished = True
500
500
501 # These methods are meant to be overridden by subclasses who may wish to
501 # These methods are meant to be overridden by subclasses who may wish to
502 # customize the behavior of of their demos.
502 # customize the behavior of of their demos.
503 def marquee(self,txt='',width=78,mark='*'):
503 def marquee(self,txt='',width=78,mark='*'):
504 """Return the input string centered in a 'marquee'."""
504 """Return the input string centered in a 'marquee'."""
505 return marquee(txt,width,mark)
505 return marquee(txt,width,mark)
506
506
507 def pre_cmd(self):
507 def pre_cmd(self):
508 """Method called before executing each block."""
508 """Method called before executing each block."""
509 pass
509 pass
510
510
511 def post_cmd(self):
511 def post_cmd(self):
512 """Method called after executing each block."""
512 """Method called after executing each block."""
513 pass
513 pass
514
514
515 def highlight(self, block):
515 def highlight(self, block):
516 """Method called on each block to highlight it content"""
516 """Method called on each block to highlight it content"""
517 tokens = pygments.lex(block, self.python_lexer)
517 tokens = pygments.lex(block, self.python_lexer)
518 if self.format_rst:
518 if self.format_rst:
519 from pygments.token import Token
519 from pygments.token import Token
520 toks = []
520 toks = []
521 for token in tokens:
521 for token in tokens:
522 if token[0] == Token.String.Doc and len(token[1]) > 6:
522 if token[0] == Token.String.Doc and len(token[1]) > 6:
523 toks += pygments.lex(token[1][:3], self.python_lexer)
523 toks += pygments.lex(token[1][:3], self.python_lexer)
524 # parse doc string content by rst lexer
524 # parse doc string content by rst lexer
525 toks += pygments.lex(token[1][3:-3], self.rst_lexer)
525 toks += pygments.lex(token[1][3:-3], self.rst_lexer)
526 toks += pygments.lex(token[1][-3:], self.python_lexer)
526 toks += pygments.lex(token[1][-3:], self.python_lexer)
527 elif token[0] == Token.Comment.Single:
527 elif token[0] == Token.Comment.Single:
528 toks.append((Token.Comment.Single, token[1][0]))
528 toks.append((Token.Comment.Single, token[1][0]))
529 # parse comment content by rst lexer
529 # parse comment content by rst lexer
530 # remove the extrat newline added by rst lexer
530 # remove the extrat newline added by rst lexer
531 toks += list(pygments.lex(token[1][1:], self.rst_lexer))[:-1]
531 toks += list(pygments.lex(token[1][1:], self.rst_lexer))[:-1]
532 else:
532 else:
533 toks.append(token)
533 toks.append(token)
534 tokens = toks
534 tokens = toks
535 return pygments.format(tokens, self.formatter)
535 return pygments.format(tokens, self.formatter)
536
536
537
537
538 class IPythonDemo(Demo):
538 class IPythonDemo(Demo):
539 """Class for interactive demos with IPython's input processing applied.
539 """Class for interactive demos with IPython's input processing applied.
540
540
541 This subclasses Demo, but instead of executing each block by the Python
541 This subclasses Demo, but instead of executing each block by the Python
542 interpreter (via exec), it actually calls IPython on it, so that any input
542 interpreter (via exec), it actually calls IPython on it, so that any input
543 filters which may be in place are applied to the input block.
543 filters which may be in place are applied to the input block.
544
544
545 If you have an interactive environment which exposes special input
545 If you have an interactive environment which exposes special input
546 processing, you can use this class instead to write demo scripts which
546 processing, you can use this class instead to write demo scripts which
547 operate exactly as if you had typed them interactively. The default Demo
547 operate exactly as if you had typed them interactively. The default Demo
548 class requires the input to be valid, pure Python code.
548 class requires the input to be valid, pure Python code.
549 """
549 """
550
550
551 def run_cell(self,source):
551 def run_cell(self,source):
552 """Execute a string with one or more lines of code"""
552 """Execute a string with one or more lines of code"""
553
553
554 self.shell.run_cell(source)
554 self.shell.run_cell(source)
555
555
556 class LineDemo(Demo):
556 class LineDemo(Demo):
557 """Demo where each line is executed as a separate block.
557 """Demo where each line is executed as a separate block.
558
558
559 The input script should be valid Python code.
559 The input script should be valid Python code.
560
560
561 This class doesn't require any markup at all, and it's meant for simple
561 This class doesn't require any markup at all, and it's meant for simple
562 scripts (with no nesting or any kind of indentation) which consist of
562 scripts (with no nesting or any kind of indentation) which consist of
563 multiple lines of input to be executed, one at a time, as if they had been
563 multiple lines of input to be executed, one at a time, as if they had been
564 typed in the interactive prompt.
564 typed in the interactive prompt.
565
565
566 Note: the input can not have *any* indentation, which means that only
566 Note: the input can not have *any* indentation, which means that only
567 single-lines of input are accepted, not even function definitions are
567 single-lines of input are accepted, not even function definitions are
568 valid."""
568 valid."""
569
569
570 def reload(self):
570 def reload(self):
571 """Reload source from disk and initialize state."""
571 """Reload source from disk and initialize state."""
572 # read data and parse into blocks
572 # read data and parse into blocks
573 self.fload()
573 self.fload()
574 lines = self.fobj.readlines()
574 lines = self.fobj.readlines()
575 src_b = [l for l in lines if l.strip()]
575 src_b = [l for l in lines if l.strip()]
576 nblocks = len(src_b)
576 nblocks = len(src_b)
577 self.src = ''.join(lines)
577 self.src = ''.join(lines)
578 self._silent = [False]*nblocks
578 self._silent = [False]*nblocks
579 self._auto = [True]*nblocks
579 self._auto = [True]*nblocks
580 self.auto_all = True
580 self.auto_all = True
581 self.nblocks = nblocks
581 self.nblocks = nblocks
582 self.src_blocks = src_b
582 self.src_blocks = src_b
583
583
584 # also build syntax-highlighted source
584 # also build syntax-highlighted source
585 self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
585 self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
586
586
587 # ensure clean namespace and seek offset
587 # ensure clean namespace and seek offset
588 self.reset()
588 self.reset()
589
589
590
590
591 class IPythonLineDemo(IPythonDemo,LineDemo):
591 class IPythonLineDemo(IPythonDemo,LineDemo):
592 """Variant of the LineDemo class whose input is processed by IPython."""
592 """Variant of the LineDemo class whose input is processed by IPython."""
593 pass
593 pass
594
594
595
595
596 class ClearMixin(object):
596 class ClearMixin(object):
597 """Use this mixin to make Demo classes with less visual clutter.
597 """Use this mixin to make Demo classes with less visual clutter.
598
598
599 Demos using this mixin will clear the screen before every block and use
599 Demos using this mixin will clear the screen before every block and use
600 blank marquees.
600 blank marquees.
601
601
602 Note that in order for the methods defined here to actually override those
602 Note that in order for the methods defined here to actually override those
603 of the classes it's mixed with, it must go /first/ in the inheritance
603 of the classes it's mixed with, it must go /first/ in the inheritance
604 tree. For example:
604 tree. For example:
605
605
606 class ClearIPDemo(ClearMixin,IPythonDemo): pass
606 class ClearIPDemo(ClearMixin,IPythonDemo): pass
607
607
608 will provide an IPythonDemo class with the mixin's features.
608 will provide an IPythonDemo class with the mixin's features.
609 """
609 """
610
610
611 def marquee(self,txt='',width=78,mark='*'):
611 def marquee(self,txt='',width=78,mark='*'):
612 """Blank marquee that returns '' no matter what the input."""
612 """Blank marquee that returns '' no matter what the input."""
613 return ''
613 return ''
614
614
615 def pre_cmd(self):
615 def pre_cmd(self):
616 """Method called before executing each block.
616 """Method called before executing each block.
617
617
618 This one simply clears the screen."""
618 This one simply clears the screen."""
619 from IPython.utils.terminal import _term_clear
619 from IPython.utils.terminal import _term_clear
620 _term_clear()
620 _term_clear()
621
621
622 class ClearDemo(ClearMixin,Demo):
622 class ClearDemo(ClearMixin,Demo):
623 pass
623 pass
624
624
625
625
626 class ClearIPDemo(ClearMixin,IPythonDemo):
626 class ClearIPDemo(ClearMixin,IPythonDemo):
627 pass
627 pass
628
628
629
629
630 def slide(file_path, noclear=False, format_rst=True, formatter="terminal",
630 def slide(file_path, noclear=False, format_rst=True, formatter="terminal",
631 style="native", auto_all=False, delimiter='...'):
631 style="native", auto_all=False, delimiter='...'):
632 if noclear:
632 if noclear:
633 demo_class = Demo
633 demo_class = Demo
634 else:
634 else:
635 demo_class = ClearDemo
635 demo_class = ClearDemo
636 demo = demo_class(file_path, format_rst=format_rst, formatter=formatter,
636 demo = demo_class(file_path, format_rst=format_rst, formatter=formatter,
637 style=style, auto_all=auto_all)
637 style=style, auto_all=auto_all)
638 while not demo.finished:
638 while not demo.finished:
639 demo()
639 demo()
640 try:
640 try:
641 py3compat.input('\n' + delimiter)
641 py3compat.input('\n' + delimiter)
642 except KeyboardInterrupt:
642 except KeyboardInterrupt:
643 exit(1)
643 exit(1)
644
644
645 if __name__ == '__main__':
645 if __name__ == '__main__':
646 import argparse
646 import argparse
647 parser = argparse.ArgumentParser(description='Run python demos')
647 parser = argparse.ArgumentParser(description='Run python demos')
648 parser.add_argument('--noclear', '-C', action='store_true',
648 parser.add_argument('--noclear', '-C', action='store_true',
649 help='Do not clear terminal on each slide')
649 help='Do not clear terminal on each slide')
650 parser.add_argument('--rst', '-r', action='store_true',
650 parser.add_argument('--rst', '-r', action='store_true',
651 help='Highlight comments and dostrings as rst')
651 help='Highlight comments and dostrings as rst')
652 parser.add_argument('--formatter', '-f', default='terminal',
652 parser.add_argument('--formatter', '-f', default='terminal',
653 help='pygments formatter name could be: terminal, '
653 help='pygments formatter name could be: terminal, '
654 'terminal256, terminal16m')
654 'terminal256, terminal16m')
655 parser.add_argument('--style', '-s', default='default',
655 parser.add_argument('--style', '-s', default='default',
656 help='pygments style name')
656 help='pygments style name')
657 parser.add_argument('--auto', '-a', action='store_true',
657 parser.add_argument('--auto', '-a', action='store_true',
658 help='Run all blocks automatically without'
658 help='Run all blocks automatically without'
659 'confirmation')
659 'confirmation')
660 parser.add_argument('--delimiter', '-d', default='...',
660 parser.add_argument('--delimiter', '-d', default='...',
661 help='slides delimiter added after each slide run')
661 help='slides delimiter added after each slide run')
662 parser.add_argument('file', nargs=1,
662 parser.add_argument('file', nargs=1,
663 help='python demo file')
663 help='python demo file')
664 args = parser.parse_args()
664 args = parser.parse_args()
665 slide(args.file[0], noclear=args.noclear, format_rst=args.rst,
665 slide(args.file[0], noclear=args.noclear, format_rst=args.rst,
666 formatter=args.formatter, style=args.style, auto_all=args.auto,
666 formatter=args.formatter, style=args.style, auto_all=args.auto,
667 delimiter=args.delimiter)
667 delimiter=args.delimiter)
@@ -1,557 +1,557 b''
1 """Various display related classes.
1 """Various display related classes.
2
2
3 Authors : MinRK, gregcaporaso, dannystaple
3 Authors : MinRK, gregcaporaso, dannystaple
4 """
4 """
5 from os.path import exists, isfile, splitext, abspath, join, isdir
5 from os.path import exists, isfile, splitext, abspath, join, isdir
6 from os import walk, sep
6 from os import walk, sep
7
7
8 from IPython.core.display import DisplayObject
8 from IPython.core.display import DisplayObject
9
9
10 __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
10 __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
11 'FileLink', 'FileLinks']
11 'FileLink', 'FileLinks']
12
12
13
13
14 class Audio(DisplayObject):
14 class Audio(DisplayObject):
15 """Create an audio object.
15 """Create an audio object.
16
16
17 When this object is returned by an input cell or passed to the
17 When this object is returned by an input cell or passed to the
18 display function, it will result in Audio controls being displayed
18 display function, it will result in Audio controls being displayed
19 in the frontend (only works in the notebook).
19 in the frontend (only works in the notebook).
20
20
21 Parameters
21 Parameters
22 ----------
22 ----------
23 data : numpy array, list, unicode, str or bytes
23 data : numpy array, list, unicode, str or bytes
24 Can be one of
24 Can be one of
25
25
26 * Numpy 1d array containing the desired waveform (mono)
26 * Numpy 1d array containing the desired waveform (mono)
27 * Numpy 2d array containing waveforms for each channel.
27 * Numpy 2d array containing waveforms for each channel.
28 Shape=(NCHAN, NSAMPLES). For the standard channel order, see
28 Shape=(NCHAN, NSAMPLES). For the standard channel order, see
29 http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx
29 http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx
30 * List of float or integer representing the waveform (mono)
30 * List of float or integer representing the waveform (mono)
31 * String containing the filename
31 * String containing the filename
32 * Bytestring containing raw PCM data or
32 * Bytestring containing raw PCM data or
33 * URL pointing to a file on the web.
33 * URL pointing to a file on the web.
34
34
35 If the array option is used the waveform will be normalized.
35 If the array option is used the waveform will be normalized.
36
36
37 If a filename or url is used the format support will be browser
37 If a filename or url is used the format support will be browser
38 dependent.
38 dependent.
39 url : unicode
39 url : unicode
40 A URL to download the data from.
40 A URL to download the data from.
41 filename : unicode
41 filename : unicode
42 Path to a local file to load the data from.
42 Path to a local file to load the data from.
43 embed : boolean
43 embed : boolean
44 Should the audio data be embedded using a data URI (True) or should
44 Should the audio data be embedded using a data URI (True) or should
45 the original source be referenced. Set this to True if you want the
45 the original source be referenced. Set this to True if you want the
46 audio to playable later with no internet connection in the notebook.
46 audio to playable later with no internet connection in the notebook.
47
47
48 Default is `True`, unless the keyword argument `url` is set, then
48 Default is `True`, unless the keyword argument `url` is set, then
49 default value is `False`.
49 default value is `False`.
50 rate : integer
50 rate : integer
51 The sampling rate of the raw data.
51 The sampling rate of the raw data.
52 Only required when data parameter is being used as an array
52 Only required when data parameter is being used as an array
53 autoplay : bool
53 autoplay : bool
54 Set to True if the audio should immediately start playing.
54 Set to True if the audio should immediately start playing.
55 Default is `False`.
55 Default is `False`.
56
56
57 Examples
57 Examples
58 --------
58 --------
59 ::
59 ::
60
60
61 # Generate a sound
61 # Generate a sound
62 import numpy as np
62 import numpy as np
63 framerate = 44100
63 framerate = 44100
64 t = np.linspace(0,5,framerate*5)
64 t = np.linspace(0,5,framerate*5)
65 data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t))
65 data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t))
66 Audio(data,rate=framerate)
66 Audio(data,rate=framerate)
67
67
68 # Can also do stereo or more channels
68 # Can also do stereo or more channels
69 dataleft = np.sin(2*np.pi*220*t)
69 dataleft = np.sin(2*np.pi*220*t)
70 dataright = np.sin(2*np.pi*224*t)
70 dataright = np.sin(2*np.pi*224*t)
71 Audio([dataleft, dataright],rate=framerate)
71 Audio([dataleft, dataright],rate=framerate)
72
72
73 Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # From URL
73 Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # From URL
74 Audio(url="http://www.w3schools.com/html/horse.ogg")
74 Audio(url="http://www.w3schools.com/html/horse.ogg")
75
75
76 Audio('/path/to/sound.wav') # From file
76 Audio('/path/to/sound.wav') # From file
77 Audio(filename='/path/to/sound.ogg')
77 Audio(filename='/path/to/sound.ogg')
78
78
79 Audio(b'RAW_WAV_DATA..) # From bytes
79 Audio(b'RAW_WAV_DATA..) # From bytes
80 Audio(data=b'RAW_WAV_DATA..)
80 Audio(data=b'RAW_WAV_DATA..)
81
81
82 """
82 """
83 _read_flags = 'rb'
83 _read_flags = 'rb'
84
84
85 def __init__(self, data=None, filename=None, url=None, embed=None, rate=None, autoplay=False):
85 def __init__(self, data=None, filename=None, url=None, embed=None, rate=None, autoplay=False):
86 if filename is None and url is None and data is None:
86 if filename is None and url is None and data is None:
87 raise ValueError("No image data found. Expecting filename, url, or data.")
87 raise ValueError("No image data found. Expecting filename, url, or data.")
88 if embed is False and url is None:
88 if embed is False and url is None:
89 raise ValueError("No url found. Expecting url when embed=False")
89 raise ValueError("No url found. Expecting url when embed=False")
90
90
91 if url is not None and embed is not True:
91 if url is not None and embed is not True:
92 self.embed = False
92 self.embed = False
93 else:
93 else:
94 self.embed = True
94 self.embed = True
95 self.autoplay = autoplay
95 self.autoplay = autoplay
96 super(Audio, self).__init__(data=data, url=url, filename=filename)
96 super(Audio, self).__init__(data=data, url=url, filename=filename)
97
97
98 if self.data is not None and not isinstance(self.data, bytes):
98 if self.data is not None and not isinstance(self.data, bytes):
99 self.data = self._make_wav(data,rate)
99 self.data = self._make_wav(data,rate)
100
100
101 def reload(self):
101 def reload(self):
102 """Reload the raw data from file or URL."""
102 """Reload the raw data from file or URL."""
103 import mimetypes
103 import mimetypes
104 if self.embed:
104 if self.embed:
105 super(Audio, self).reload()
105 super(Audio, self).reload()
106
106
107 if self.filename is not None:
107 if self.filename is not None:
108 self.mimetype = mimetypes.guess_type(self.filename)[0]
108 self.mimetype = mimetypes.guess_type(self.filename)[0]
109 elif self.url is not None:
109 elif self.url is not None:
110 self.mimetype = mimetypes.guess_type(self.url)[0]
110 self.mimetype = mimetypes.guess_type(self.url)[0]
111 else:
111 else:
112 self.mimetype = "audio/wav"
112 self.mimetype = "audio/wav"
113
113
114 def _make_wav(self, data, rate):
114 def _make_wav(self, data, rate):
115 """ Transform a numpy array to a PCM bytestring """
115 """ Transform a numpy array to a PCM bytestring """
116 import struct
116 import struct
117 from io import BytesIO
117 from io import BytesIO
118 import wave
118 import wave
119
119
120 try:
120 try:
121 import numpy as np
121 import numpy as np
122
122
123 data = np.array(data, dtype=float)
123 data = np.array(data, dtype=float)
124 if len(data.shape) == 1:
124 if len(data.shape) == 1:
125 nchan = 1
125 nchan = 1
126 elif len(data.shape) == 2:
126 elif len(data.shape) == 2:
127 # In wave files,channels are interleaved. E.g.,
127 # In wave files,channels are interleaved. E.g.,
128 # "L1R1L2R2..." for stereo. See
128 # "L1R1L2R2..." for stereo. See
129 # http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx
129 # http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx
130 # for channel ordering
130 # for channel ordering
131 nchan = data.shape[0]
131 nchan = data.shape[0]
132 data = data.T.ravel()
132 data = data.T.ravel()
133 else:
133 else:
134 raise ValueError('Array audio input must be a 1D or 2D array')
134 raise ValueError('Array audio input must be a 1D or 2D array')
135 scaled = np.int16(data/np.max(np.abs(data))*32767).tolist()
135 scaled = np.int16(data/np.max(np.abs(data))*32767).tolist()
136 except ImportError:
136 except ImportError:
137 # check that it is a "1D" list
137 # check that it is a "1D" list
138 idata = iter(data) # fails if not an iterable
138 idata = iter(data) # fails if not an iterable
139 try:
139 try:
140 iter(idata.next())
140 iter(idata.next())
141 raise TypeError('Only lists of mono audio are '
141 raise TypeError('Only lists of mono audio are '
142 'supported if numpy is not installed')
142 'supported if numpy is not installed')
143 except TypeError:
143 except TypeError:
144 # this means it's not a nested list, which is what we want
144 # this means it's not a nested list, which is what we want
145 pass
145 pass
146 maxabsvalue = float(max([abs(x) for x in data]))
146 maxabsvalue = float(max([abs(x) for x in data]))
147 scaled = [int(x/maxabsvalue*32767) for x in data]
147 scaled = [int(x/maxabsvalue*32767) for x in data]
148 nchan = 1
148 nchan = 1
149
149
150 fp = BytesIO()
150 fp = BytesIO()
151 waveobj = wave.open(fp,mode='wb')
151 waveobj = wave.open(fp,mode='wb')
152 waveobj.setnchannels(nchan)
152 waveobj.setnchannels(nchan)
153 waveobj.setframerate(rate)
153 waveobj.setframerate(rate)
154 waveobj.setsampwidth(2)
154 waveobj.setsampwidth(2)
155 waveobj.setcomptype('NONE','NONE')
155 waveobj.setcomptype('NONE','NONE')
156 waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled]))
156 waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled]))
157 val = fp.getvalue()
157 val = fp.getvalue()
158 waveobj.close()
158 waveobj.close()
159
159
160 return val
160 return val
161
161
162 def _data_and_metadata(self):
162 def _data_and_metadata(self):
163 """shortcut for returning metadata with url information, if defined"""
163 """shortcut for returning metadata with url information, if defined"""
164 md = {}
164 md = {}
165 if self.url:
165 if self.url:
166 md['url'] = self.url
166 md['url'] = self.url
167 if md:
167 if md:
168 return self.data, md
168 return self.data, md
169 else:
169 else:
170 return self.data
170 return self.data
171
171
172 def _repr_html_(self):
172 def _repr_html_(self):
173 src = """
173 src = """
174 <audio controls="controls" {autoplay}>
174 <audio controls="controls" {autoplay}>
175 <source src="{src}" type="{type}" />
175 <source src="{src}" type="{type}" />
176 Your browser does not support the audio element.
176 Your browser does not support the audio element.
177 </audio>
177 </audio>
178 """
178 """
179 return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())
179 return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())
180
180
181 def src_attr(self):
181 def src_attr(self):
182 import base64
182 import base64
183 if self.embed and (self.data is not None):
183 if self.embed and (self.data is not None):
184 data = base64=base64.b64encode(self.data).decode('ascii')
184 data = base64=base64.b64encode(self.data).decode('ascii')
185 return """data:{type};base64,{base64}""".format(type=self.mimetype,
185 return """data:{type};base64,{base64}""".format(type=self.mimetype,
186 base64=data)
186 base64=data)
187 elif self.url is not None:
187 elif self.url is not None:
188 return self.url
188 return self.url
189 else:
189 else:
190 return ""
190 return ""
191
191
192 def autoplay_attr(self):
192 def autoplay_attr(self):
193 if(self.autoplay):
193 if(self.autoplay):
194 return 'autoplay="autoplay"'
194 return 'autoplay="autoplay"'
195 else:
195 else:
196 return ''
196 return ''
197
197
198 class IFrame(object):
198 class IFrame(object):
199 """
199 """
200 Generic class to embed an iframe in an IPython notebook
200 Generic class to embed an iframe in an IPython notebook
201 """
201 """
202
202
203 iframe = """
203 iframe = """
204 <iframe
204 <iframe
205 width="{width}"
205 width="{width}"
206 height="{height}"
206 height="{height}"
207 src="{src}{params}"
207 src="{src}{params}"
208 frameborder="0"
208 frameborder="0"
209 allowfullscreen
209 allowfullscreen
210 ></iframe>
210 ></iframe>
211 """
211 """
212
212
213 def __init__(self, src, width, height, **kwargs):
213 def __init__(self, src, width, height, **kwargs):
214 self.src = src
214 self.src = src
215 self.width = width
215 self.width = width
216 self.height = height
216 self.height = height
217 self.params = kwargs
217 self.params = kwargs
218
218
219 def _repr_html_(self):
219 def _repr_html_(self):
220 """return the embed iframe"""
220 """return the embed iframe"""
221 if self.params:
221 if self.params:
222 try:
222 try:
223 from urllib.parse import urlencode # Py 3
223 from urllib.parse import urlencode # Py 3
224 except ImportError:
224 except ImportError:
225 from urllib import urlencode
225 from urllib import urlencode
226 params = "?" + urlencode(self.params)
226 params = "?" + urlencode(self.params)
227 else:
227 else:
228 params = ""
228 params = ""
229 return self.iframe.format(src=self.src,
229 return self.iframe.format(src=self.src,
230 width=self.width,
230 width=self.width,
231 height=self.height,
231 height=self.height,
232 params=params)
232 params=params)
233
233
234 class YouTubeVideo(IFrame):
234 class YouTubeVideo(IFrame):
235 """Class for embedding a YouTube Video in an IPython session, based on its video id.
235 """Class for embedding a YouTube Video in an IPython session, based on its video id.
236
236
237 e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would
237 e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would
238 do::
238 do::
239
239
240 vid = YouTubeVideo("foo")
240 vid = YouTubeVideo("foo")
241 display(vid)
241 display(vid)
242
242
243 To start from 30 seconds::
243 To start from 30 seconds::
244
244
245 vid = YouTubeVideo("abc", start=30)
245 vid = YouTubeVideo("abc", start=30)
246 display(vid)
246 display(vid)
247
247
248 To calculate seconds from time as hours, minutes, seconds use
248 To calculate seconds from time as hours, minutes, seconds use
249 :class:`datetime.timedelta`::
249 :class:`datetime.timedelta`::
250
250
251 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
251 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
252
252
253 Other parameters can be provided as documented at
253 Other parameters can be provided as documented at
254 https://developers.google.com/youtube/player_parameters#Parameters
254 https://developers.google.com/youtube/player_parameters#Parameters
255
255
256 When converting the notebook using nbconvert, a jpeg representation of the video
256 When converting the notebook using nbconvert, a jpeg representation of the video
257 will be inserted in the document.
257 will be inserted in the document.
258 """
258 """
259
259
260 def __init__(self, id, width=400, height=300, **kwargs):
260 def __init__(self, id, width=400, height=300, **kwargs):
261 self.id=id
261 self.id=id
262 src = "https://www.youtube.com/embed/{0}".format(id)
262 src = "https://www.youtube.com/embed/{0}".format(id)
263 super(YouTubeVideo, self).__init__(src, width, height, **kwargs)
263 super(YouTubeVideo, self).__init__(src, width, height, **kwargs)
264
264
265 def _repr_jpeg_(self):
265 def _repr_jpeg_(self):
266 # Deferred import
266 # Deferred import
267 from urllib.request import urlopen
267 from urllib.request import urlopen
268
268
269 try:
269 try:
270 return urlopen("https://img.youtube.com/vi/{id}/hqdefault.jpg".format(id=self.id)).read()
270 return urlopen("https://img.youtube.com/vi/{id}/hqdefault.jpg".format(id=self.id)).read()
271 except IOError:
271 except IOError:
272 return None
272 return None
273
273
274 class VimeoVideo(IFrame):
274 class VimeoVideo(IFrame):
275 """
275 """
276 Class for embedding a Vimeo video in an IPython session, based on its video id.
276 Class for embedding a Vimeo video in an IPython session, based on its video id.
277 """
277 """
278
278
279 def __init__(self, id, width=400, height=300, **kwargs):
279 def __init__(self, id, width=400, height=300, **kwargs):
280 src="https://player.vimeo.com/video/{0}".format(id)
280 src="https://player.vimeo.com/video/{0}".format(id)
281 super(VimeoVideo, self).__init__(src, width, height, **kwargs)
281 super(VimeoVideo, self).__init__(src, width, height, **kwargs)
282
282
283 class ScribdDocument(IFrame):
283 class ScribdDocument(IFrame):
284 """
284 """
285 Class for embedding a Scribd document in an IPython session
285 Class for embedding a Scribd document in an IPython session
286
286
287 Use the start_page params to specify a starting point in the document
287 Use the start_page params to specify a starting point in the document
288 Use the view_mode params to specify display type one off scroll | slideshow | book
288 Use the view_mode params to specify display type one off scroll | slideshow | book
289
289
290 e.g to Display Wes' foundational paper about PANDAS in book mode from page 3
290 e.g to Display Wes' foundational paper about PANDAS in book mode from page 3
291
291
292 ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book")
292 ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book")
293 """
293 """
294
294
295 def __init__(self, id, width=400, height=300, **kwargs):
295 def __init__(self, id, width=400, height=300, **kwargs):
296 src="https://www.scribd.com/embeds/{0}/content".format(id)
296 src="https://www.scribd.com/embeds/{0}/content".format(id)
297 super(ScribdDocument, self).__init__(src, width, height, **kwargs)
297 super(ScribdDocument, self).__init__(src, width, height, **kwargs)
298
298
299 class FileLink(object):
299 class FileLink(object):
300 """Class for embedding a local file link in an IPython session, based on path
300 """Class for embedding a local file link in an IPython session, based on path
301
301
302 e.g. to embed a link that was generated in the IPython notebook as my/data.txt
302 e.g. to embed a link that was generated in the IPython notebook as my/data.txt
303
303
304 you would do::
304 you would do::
305
305
306 local_file = FileLink("my/data.txt")
306 local_file = FileLink("my/data.txt")
307 display(local_file)
307 display(local_file)
308
308
309 or in the HTML notebook, just::
309 or in the HTML notebook, just::
310
310
311 FileLink("my/data.txt")
311 FileLink("my/data.txt")
312 """
312 """
313
313
314 html_link_str = "<a href='%s' target='_blank'>%s</a>"
314 html_link_str = "<a href='%s' target='_blank'>%s</a>"
315
315
316 def __init__(self,
316 def __init__(self,
317 path,
317 path,
318 url_prefix='',
318 url_prefix='',
319 result_html_prefix='',
319 result_html_prefix='',
320 result_html_suffix='<br>'):
320 result_html_suffix='<br>'):
321 """
321 """
322 Parameters
322 Parameters
323 ----------
323 ----------
324 path : str
324 path : str
325 path to the file or directory that should be formatted
325 path to the file or directory that should be formatted
326 url_prefix : str
326 url_prefix : str
327 prefix to be prepended to all files to form a working link [default:
327 prefix to be prepended to all files to form a working link [default:
328 '']
328 '']
329 result_html_prefix : str
329 result_html_prefix : str
330 text to append to beginning to link [default: '']
330 text to append to beginning to link [default: '']
331 result_html_suffix : str
331 result_html_suffix : str
332 text to append at the end of link [default: '<br>']
332 text to append at the end of link [default: '<br>']
333 """
333 """
334 if isdir(path):
334 if isdir(path):
335 raise ValueError("Cannot display a directory using FileLink. "
335 raise ValueError("Cannot display a directory using FileLink. "
336 "Use FileLinks to display '%s'." % path)
336 "Use FileLinks to display '%s'." % path)
337 self.path = path
337 self.path = path
338 self.url_prefix = url_prefix
338 self.url_prefix = url_prefix
339 self.result_html_prefix = result_html_prefix
339 self.result_html_prefix = result_html_prefix
340 self.result_html_suffix = result_html_suffix
340 self.result_html_suffix = result_html_suffix
341
341
342 def _format_path(self):
342 def _format_path(self):
343 fp = ''.join([self.url_prefix,self.path])
343 fp = ''.join([self.url_prefix,self.path])
344 return ''.join([self.result_html_prefix,
344 return ''.join([self.result_html_prefix,
345 self.html_link_str % (fp, self.path),
345 self.html_link_str % (fp, self.path),
346 self.result_html_suffix])
346 self.result_html_suffix])
347
347
348 def _repr_html_(self):
348 def _repr_html_(self):
349 """return html link to file
349 """return html link to file
350 """
350 """
351 if not exists(self.path):
351 if not exists(self.path):
352 return ("Path (<tt>%s</tt>) doesn't exist. "
352 return ("Path (<tt>%s</tt>) doesn't exist. "
353 "It may still be in the process of "
353 "It may still be in the process of "
354 "being generated, or you may have the "
354 "being generated, or you may have the "
355 "incorrect path." % self.path)
355 "incorrect path." % self.path)
356
356
357 return self._format_path()
357 return self._format_path()
358
358
359 def __repr__(self):
359 def __repr__(self):
360 """return absolute path to file
360 """return absolute path to file
361 """
361 """
362 return abspath(self.path)
362 return abspath(self.path)
363
363
364 class FileLinks(FileLink):
364 class FileLinks(FileLink):
365 """Class for embedding local file links in an IPython session, based on path
365 """Class for embedding local file links in an IPython session, based on path
366
366
367 e.g. to embed links to files that were generated in the IPython notebook
367 e.g. to embed links to files that were generated in the IPython notebook
368 under ``my/data``, you would do::
368 under ``my/data``, you would do::
369
369
370 local_files = FileLinks("my/data")
370 local_files = FileLinks("my/data")
371 display(local_files)
371 display(local_files)
372
372
373 or in the HTML notebook, just::
373 or in the HTML notebook, just::
374
374
375 FileLinks("my/data")
375 FileLinks("my/data")
376 """
376 """
377 def __init__(self,
377 def __init__(self,
378 path,
378 path,
379 url_prefix='',
379 url_prefix='',
380 included_suffixes=None,
380 included_suffixes=None,
381 result_html_prefix='',
381 result_html_prefix='',
382 result_html_suffix='<br>',
382 result_html_suffix='<br>',
383 notebook_display_formatter=None,
383 notebook_display_formatter=None,
384 terminal_display_formatter=None,
384 terminal_display_formatter=None,
385 recursive=True):
385 recursive=True):
386 """
386 """
387 See :class:`FileLink` for the ``path``, ``url_prefix``,
387 See :class:`FileLink` for the ``path``, ``url_prefix``,
388 ``result_html_prefix`` and ``result_html_suffix`` parameters.
388 ``result_html_prefix`` and ``result_html_suffix`` parameters.
389
389
390 included_suffixes : list
390 included_suffixes : list
391 Filename suffixes to include when formatting output [default: include
391 Filename suffixes to include when formatting output [default: include
392 all files]
392 all files]
393
393
394 notebook_display_formatter : function
394 notebook_display_formatter : function
395 Used to format links for display in the notebook. See discussion of
395 Used to format links for display in the notebook. See discussion of
396 formatter functions below.
396 formatter functions below.
397
397
398 terminal_display_formatter : function
398 terminal_display_formatter : function
399 Used to format links for display in the terminal. See discussion of
399 Used to format links for display in the terminal. See discussion of
400 formatter functions below.
400 formatter functions below.
401
401
402 Formatter functions must be of the form::
402 Formatter functions must be of the form::
403
403
404 f(dirname, fnames, included_suffixes)
404 f(dirname, fnames, included_suffixes)
405
405
406 dirname : str
406 dirname : str
407 The name of a directory
407 The name of a directory
408 fnames : list
408 fnames : list
409 The files in that directory
409 The files in that directory
410 included_suffixes : list
410 included_suffixes : list
411 The file suffixes that should be included in the output (passing None
411 The file suffixes that should be included in the output (passing None
412 meansto include all suffixes in the output in the built-in formatters)
412 meansto include all suffixes in the output in the built-in formatters)
413 recursive : boolean
413 recursive : boolean
414 Whether to recurse into subdirectories. Default is True.
414 Whether to recurse into subdirectories. Default is True.
415
415
416 The function should return a list of lines that will be printed in the
416 The function should return a list of lines that will be printed in the
417 notebook (if passing notebook_display_formatter) or the terminal (if
417 notebook (if passing notebook_display_formatter) or the terminal (if
418 passing terminal_display_formatter). This function is iterated over for
418 passing terminal_display_formatter). This function is iterated over for
419 each directory in self.path. Default formatters are in place, can be
419 each directory in self.path. Default formatters are in place, can be
420 passed here to support alternative formatting.
420 passed here to support alternative formatting.
421
421
422 """
422 """
423 if isfile(path):
423 if isfile(path):
424 raise ValueError("Cannot display a file using FileLinks. "
424 raise ValueError("Cannot display a file using FileLinks. "
425 "Use FileLink to display '%s'." % path)
425 "Use FileLink to display '%s'." % path)
426 self.included_suffixes = included_suffixes
426 self.included_suffixes = included_suffixes
427 # remove trailing slashs for more consistent output formatting
427 # remove trailing slashs for more consistent output formatting
428 path = path.rstrip('/')
428 path = path.rstrip('/')
429
429
430 self.path = path
430 self.path = path
431 self.url_prefix = url_prefix
431 self.url_prefix = url_prefix
432 self.result_html_prefix = result_html_prefix
432 self.result_html_prefix = result_html_prefix
433 self.result_html_suffix = result_html_suffix
433 self.result_html_suffix = result_html_suffix
434
434
435 self.notebook_display_formatter = \
435 self.notebook_display_formatter = \
436 notebook_display_formatter or self._get_notebook_display_formatter()
436 notebook_display_formatter or self._get_notebook_display_formatter()
437 self.terminal_display_formatter = \
437 self.terminal_display_formatter = \
438 terminal_display_formatter or self._get_terminal_display_formatter()
438 terminal_display_formatter or self._get_terminal_display_formatter()
439
439
440 self.recursive = recursive
440 self.recursive = recursive
441
441
442 def _get_display_formatter(self,
442 def _get_display_formatter(self,
443 dirname_output_format,
443 dirname_output_format,
444 fname_output_format,
444 fname_output_format,
445 fp_format,
445 fp_format,
446 fp_cleaner=None):
446 fp_cleaner=None):
447 """ generate built-in formatter function
447 """ generate built-in formatter function
448
448
449 this is used to define both the notebook and terminal built-in
449 this is used to define both the notebook and terminal built-in
450 formatters as they only differ by some wrapper text for each entry
450 formatters as they only differ by some wrapper text for each entry
451
451
452 dirname_output_format: string to use for formatting directory
452 dirname_output_format: string to use for formatting directory
453 names, dirname will be substituted for a single "%s" which
453 names, dirname will be substituted for a single "%s" which
454 must appear in this string
454 must appear in this string
455 fname_output_format: string to use for formatting file names,
455 fname_output_format: string to use for formatting file names,
456 if a single "%s" appears in the string, fname will be substituted
456 if a single "%s" appears in the string, fname will be substituted
457 if two "%s" appear in the string, the path to fname will be
457 if two "%s" appear in the string, the path to fname will be
458 substituted for the first and fname will be substituted for the
458 substituted for the first and fname will be substituted for the
459 second
459 second
460 fp_format: string to use for formatting filepaths, must contain
460 fp_format: string to use for formatting filepaths, must contain
461 exactly two "%s" and the dirname will be subsituted for the first
461 exactly two "%s" and the dirname will be subsituted for the first
462 and fname will be substituted for the second
462 and fname will be substituted for the second
463 """
463 """
464 def f(dirname, fnames, included_suffixes=None):
464 def f(dirname, fnames, included_suffixes=None):
465 result = []
465 result = []
466 # begin by figuring out which filenames, if any,
466 # begin by figuring out which filenames, if any,
467 # are going to be displayed
467 # are going to be displayed
468 display_fnames = []
468 display_fnames = []
469 for fname in fnames:
469 for fname in fnames:
470 if (isfile(join(dirname,fname)) and
470 if (isfile(join(dirname,fname)) and
471 (included_suffixes is None or
471 (included_suffixes is None or
472 splitext(fname)[1] in included_suffixes)):
472 splitext(fname)[1] in included_suffixes)):
473 display_fnames.append(fname)
473 display_fnames.append(fname)
474
474
475 if len(display_fnames) == 0:
475 if len(display_fnames) == 0:
476 # if there are no filenames to display, don't print anything
476 # if there are no filenames to display, don't print anything
477 # (not even the directory name)
477 # (not even the directory name)
478 pass
478 pass
479 else:
479 else:
480 # otherwise print the formatted directory name followed by
480 # otherwise print the formatted directory name followed by
481 # the formatted filenames
481 # the formatted filenames
482 dirname_output_line = dirname_output_format % dirname
482 dirname_output_line = dirname_output_format % dirname
483 result.append(dirname_output_line)
483 result.append(dirname_output_line)
484 for fname in display_fnames:
484 for fname in display_fnames:
485 fp = fp_format % (dirname,fname)
485 fp = fp_format % (dirname,fname)
486 if fp_cleaner is not None:
486 if fp_cleaner is not None:
487 fp = fp_cleaner(fp)
487 fp = fp_cleaner(fp)
488 try:
488 try:
489 # output can include both a filepath and a filename...
489 # output can include both a filepath and a filename...
490 fname_output_line = fname_output_format % (fp, fname)
490 fname_output_line = fname_output_format % (fp, fname)
491 except TypeError:
491 except TypeError:
492 # ... or just a single filepath
492 # ... or just a single filepath
493 fname_output_line = fname_output_format % fname
493 fname_output_line = fname_output_format % fname
494 result.append(fname_output_line)
494 result.append(fname_output_line)
495 return result
495 return result
496 return f
496 return f
497
497
498 def _get_notebook_display_formatter(self,
498 def _get_notebook_display_formatter(self,
499 spacer="&nbsp;&nbsp;"):
499 spacer="&nbsp;&nbsp;"):
500 """ generate function to use for notebook formatting
500 """ generate function to use for notebook formatting
501 """
501 """
502 dirname_output_format = \
502 dirname_output_format = \
503 self.result_html_prefix + "%s/" + self.result_html_suffix
503 self.result_html_prefix + "%s/" + self.result_html_suffix
504 fname_output_format = \
504 fname_output_format = \
505 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
505 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
506 fp_format = self.url_prefix + '%s/%s'
506 fp_format = self.url_prefix + '%s/%s'
507 if sep == "\\":
507 if sep == "\\":
508 # Working on a platform where the path separator is "\", so
508 # Working on a platform where the path separator is "\", so
509 # must convert these to "/" for generating a URI
509 # must convert these to "/" for generating a URI
510 def fp_cleaner(fp):
510 def fp_cleaner(fp):
511 # Replace all occurences of backslash ("\") with a forward
511 # Replace all occurrences of backslash ("\") with a forward
512 # slash ("/") - this is necessary on windows when a path is
512 # slash ("/") - this is necessary on windows when a path is
513 # provided as input, but we must link to a URI
513 # provided as input, but we must link to a URI
514 return fp.replace('\\','/')
514 return fp.replace('\\','/')
515 else:
515 else:
516 fp_cleaner = None
516 fp_cleaner = None
517
517
518 return self._get_display_formatter(dirname_output_format,
518 return self._get_display_formatter(dirname_output_format,
519 fname_output_format,
519 fname_output_format,
520 fp_format,
520 fp_format,
521 fp_cleaner)
521 fp_cleaner)
522
522
523 def _get_terminal_display_formatter(self,
523 def _get_terminal_display_formatter(self,
524 spacer=" "):
524 spacer=" "):
525 """ generate function to use for terminal formatting
525 """ generate function to use for terminal formatting
526 """
526 """
527 dirname_output_format = "%s/"
527 dirname_output_format = "%s/"
528 fname_output_format = spacer + "%s"
528 fname_output_format = spacer + "%s"
529 fp_format = '%s/%s'
529 fp_format = '%s/%s'
530
530
531 return self._get_display_formatter(dirname_output_format,
531 return self._get_display_formatter(dirname_output_format,
532 fname_output_format,
532 fname_output_format,
533 fp_format)
533 fp_format)
534
534
535 def _format_path(self):
535 def _format_path(self):
536 result_lines = []
536 result_lines = []
537 if self.recursive:
537 if self.recursive:
538 walked_dir = list(walk(self.path))
538 walked_dir = list(walk(self.path))
539 else:
539 else:
540 walked_dir = [next(walk(self.path))]
540 walked_dir = [next(walk(self.path))]
541 walked_dir.sort()
541 walked_dir.sort()
542 for dirname, subdirs, fnames in walked_dir:
542 for dirname, subdirs, fnames in walked_dir:
543 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
543 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
544 return '\n'.join(result_lines)
544 return '\n'.join(result_lines)
545
545
546 def __repr__(self):
546 def __repr__(self):
547 """return newline-separated absolute paths
547 """return newline-separated absolute paths
548 """
548 """
549 result_lines = []
549 result_lines = []
550 if self.recursive:
550 if self.recursive:
551 walked_dir = list(walk(self.path))
551 walked_dir = list(walk(self.path))
552 else:
552 else:
553 walked_dir = [next(walk(self.path))]
553 walked_dir = [next(walk(self.path))]
554 walked_dir.sort()
554 walked_dir.sort()
555 for dirname, subdirs, fnames in walked_dir:
555 for dirname, subdirs, fnames in walked_dir:
556 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
556 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
557 return '\n'.join(result_lines)
557 return '\n'.join(result_lines)
@@ -1,128 +1,128 b''
1 """ 'editor' hooks for common editors that work well with ipython
1 """ 'editor' hooks for common editors that work well with ipython
2
2
3 They should honor the line number argument, at least.
3 They should honor the line number argument, at least.
4
4
5 Contributions are *very* welcome.
5 Contributions are *very* welcome.
6 """
6 """
7
7
8 import os
8 import os
9 import pipes
9 import pipes
10 import shlex
10 import shlex
11 import subprocess
11 import subprocess
12 import sys
12 import sys
13
13
14 from IPython import get_ipython
14 from IPython import get_ipython
15 from IPython.core.error import TryNext
15 from IPython.core.error import TryNext
16 from IPython.utils import py3compat
16 from IPython.utils import py3compat
17
17
18
18
19 def install_editor(template, wait=False):
19 def install_editor(template, wait=False):
20 """Installs the editor that is called by IPython for the %edit magic.
20 """Installs the editor that is called by IPython for the %edit magic.
21
21
22 This overrides the default editor, which is generally set by your EDITOR
22 This overrides the default editor, which is generally set by your EDITOR
23 environment variable or is notepad (windows) or vi (linux). By supplying a
23 environment variable or is notepad (windows) or vi (linux). By supplying a
24 template string `run_template`, you can control how the editor is invoked
24 template string `run_template`, you can control how the editor is invoked
25 by IPython -- (e.g. the format in which it accepts command line options)
25 by IPython -- (e.g. the format in which it accepts command line options)
26
26
27 Parameters
27 Parameters
28 ----------
28 ----------
29 template : basestring
29 template : basestring
30 run_template acts as a template for how your editor is invoked by
30 run_template acts as a template for how your editor is invoked by
31 the shell. It should contain '{filename}', which will be replaced on
31 the shell. It should contain '{filename}', which will be replaced on
32 invokation with the file name, and '{line}', $line by line number
32 invokation with the file name, and '{line}', $line by line number
33 (or 0) to invoke the file with.
33 (or 0) to invoke the file with.
34 wait : bool
34 wait : bool
35 If `wait` is true, wait until the user presses enter before returning,
35 If `wait` is true, wait until the user presses enter before returning,
36 to facilitate non-blocking editors that exit immediately after
36 to facilitate non-blocking editors that exit immediately after
37 the call.
37 the call.
38 """
38 """
39
39
40 # not all editors support $line, so we'll leave out this check
40 # not all editors support $line, so we'll leave out this check
41 # for substitution in ['$file', '$line']:
41 # for substitution in ['$file', '$line']:
42 # if not substitution in run_template:
42 # if not substitution in run_template:
43 # raise ValueError(('run_template should contain %s'
43 # raise ValueError(('run_template should contain %s'
44 # ' for string substitution. You supplied "%s"' % (substitution,
44 # ' for string substitution. You supplied "%s"' % (substitution,
45 # run_template)))
45 # run_template)))
46
46
47 def call_editor(self, filename, line=0):
47 def call_editor(self, filename, line=0):
48 if line is None:
48 if line is None:
49 line = 0
49 line = 0
50 cmd = template.format(filename=pipes.quote(filename), line=line)
50 cmd = template.format(filename=pipes.quote(filename), line=line)
51 print(">", cmd)
51 print(">", cmd)
52 # pipes.quote doesn't work right on Windows, but it does after splitting
52 # pipes.quote doesn't work right on Windows, but it does after splitting
53 if sys.platform.startswith('win'):
53 if sys.platform.startswith('win'):
54 cmd = shlex.split(cmd)
54 cmd = shlex.split(cmd)
55 proc = subprocess.Popen(cmd, shell=True)
55 proc = subprocess.Popen(cmd, shell=True)
56 if proc.wait() != 0:
56 if proc.wait() != 0:
57 raise TryNext()
57 raise TryNext()
58 if wait:
58 if wait:
59 py3compat.input("Press Enter when done editing:")
59 py3compat.input("Press Enter when done editing:")
60
60
61 get_ipython().set_hook('editor', call_editor)
61 get_ipython().set_hook('editor', call_editor)
62 get_ipython().editor = template
62 get_ipython().editor = template
63
63
64
64
65 # in these, exe is always the path/name of the executable. Useful
65 # in these, exe is always the path/name of the executable. Useful
66 # if you don't have the editor directory in your path
66 # if you don't have the editor directory in your path
67 def komodo(exe=u'komodo'):
67 def komodo(exe=u'komodo'):
68 """ Activestate Komodo [Edit] """
68 """ Activestate Komodo [Edit] """
69 install_editor(exe + u' -l {line} {filename}', wait=True)
69 install_editor(exe + u' -l {line} {filename}', wait=True)
70
70
71
71
72 def scite(exe=u"scite"):
72 def scite(exe=u"scite"):
73 """ SciTE or Sc1 """
73 """ SciTE or Sc1 """
74 install_editor(exe + u' {filename} -goto:{line}')
74 install_editor(exe + u' {filename} -goto:{line}')
75
75
76
76
77 def notepadplusplus(exe=u'notepad++'):
77 def notepadplusplus(exe=u'notepad++'):
78 """ Notepad++ http://notepad-plus.sourceforge.net """
78 """ Notepad++ http://notepad-plus.sourceforge.net """
79 install_editor(exe + u' -n{line} {filename}')
79 install_editor(exe + u' -n{line} {filename}')
80
80
81
81
82 def jed(exe=u'jed'):
82 def jed(exe=u'jed'):
83 """ JED, the lightweight emacsish editor """
83 """ JED, the lightweight emacsish editor """
84 install_editor(exe + u' +{line} {filename}')
84 install_editor(exe + u' +{line} {filename}')
85
85
86
86
87 def idle(exe=u'idle'):
87 def idle(exe=u'idle'):
88 """ Idle, the editor bundled with python
88 """ Idle, the editor bundled with python
89
89
90 Parameters
90 Parameters
91 ----------
91 ----------
92 exe : str, None
92 exe : str, None
93 If none, should be pretty smart about finding the executable.
93 If none, should be pretty smart about finding the executable.
94 """
94 """
95 if exe is None:
95 if exe is None:
96 import idlelib
96 import idlelib
97 p = os.path.dirname(idlelib.__filename__)
97 p = os.path.dirname(idlelib.__filename__)
98 # i'm not sure if this actually works. Is this idle.py script
98 # i'm not sure if this actually works. Is this idle.py script
99 # guarenteed to be executable?
99 # guaranteed to be executable?
100 exe = os.path.join(p, 'idle.py')
100 exe = os.path.join(p, 'idle.py')
101 install_editor(exe + u' {filename}')
101 install_editor(exe + u' {filename}')
102
102
103
103
104 def mate(exe=u'mate'):
104 def mate(exe=u'mate'):
105 """ TextMate, the missing editor"""
105 """ TextMate, the missing editor"""
106 # wait=True is not required since we're using the -w flag to mate
106 # wait=True is not required since we're using the -w flag to mate
107 install_editor(exe + u' -w -l {line} {filename}')
107 install_editor(exe + u' -w -l {line} {filename}')
108
108
109
109
110 # ##########################################
110 # ##########################################
111 # these are untested, report any problems
111 # these are untested, report any problems
112 # ##########################################
112 # ##########################################
113
113
114
114
115 def emacs(exe=u'emacs'):
115 def emacs(exe=u'emacs'):
116 install_editor(exe + u' +{line} {filename}')
116 install_editor(exe + u' +{line} {filename}')
117
117
118
118
119 def gnuclient(exe=u'gnuclient'):
119 def gnuclient(exe=u'gnuclient'):
120 install_editor(exe + u' -nw +{line} {filename}')
120 install_editor(exe + u' -nw +{line} {filename}')
121
121
122
122
123 def crimson_editor(exe=u'cedt.exe'):
123 def crimson_editor(exe=u'cedt.exe'):
124 install_editor(exe + u' /L:{line} {filename}')
124 install_editor(exe + u' /L:{line} {filename}')
125
125
126
126
127 def kate(exe=u'kate'):
127 def kate(exe=u'kate'):
128 install_editor(exe + u' -u -l {line} {filename}')
128 install_editor(exe + u' -u -l {line} {filename}')
@@ -1,90 +1,90 b''
1 # Code borrowed from ptpython
1 # Code borrowed from ptpython
2 # https://github.com/jonathanslenders/ptpython/blob/86b71a89626114b18898a0af463978bdb32eeb70/ptpython/eventloop.py
2 # https://github.com/jonathanslenders/ptpython/blob/86b71a89626114b18898a0af463978bdb32eeb70/ptpython/eventloop.py
3
3
4 # Copyright (c) 2015, Jonathan Slenders
4 # Copyright (c) 2015, Jonathan Slenders
5 # All rights reserved.
5 # All rights reserved.
6 #
6 #
7 # Redistribution and use in source and binary forms, with or without modification,
7 # Redistribution and use in source and binary forms, with or without modification,
8 # are permitted provided that the following conditions are met:
8 # are permitted provided that the following conditions are met:
9 #
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
11 # list of conditions and the following disclaimer.
12 #
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice, this
13 # * Redistributions in binary form must reproduce the above copyright notice, this
14 # list of conditions and the following disclaimer in the documentation and/or
14 # list of conditions and the following disclaimer in the documentation and/or
15 # other materials provided with the distribution.
15 # other materials provided with the distribution.
16 #
16 #
17 # * Neither the name of the {organization} nor the names of its
17 # * Neither the name of the {organization} nor the names of its
18 # contributors may be used to endorse or promote products derived from
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
19 # this software without specific prior written permission.
20 #
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
31
32 """
32 """
33 Wrapper around the eventloop that gives some time to the Tkinter GUI to process
33 Wrapper around the eventloop that gives some time to the Tkinter GUI to process
34 events when it's loaded and while we are waiting for input at the REPL. This
34 events when it's loaded and while we are waiting for input at the REPL. This
35 way we don't block the UI of for instance ``turtle`` and other Tk libraries.
35 way we don't block the UI of for instance ``turtle`` and other Tk libraries.
36
36
37 (Normally Tkinter registeres it's callbacks in ``PyOS_InputHook`` to integrate
37 (Normally Tkinter registers it's callbacks in ``PyOS_InputHook`` to integrate
38 in readline. ``prompt-toolkit`` doesn't understand that input hook, but this
38 in readline. ``prompt-toolkit`` doesn't understand that input hook, but this
39 will fix it for Tk.)
39 will fix it for Tk.)
40 """
40 """
41 import time
41 import time
42
42
43 import _tkinter
43 import _tkinter
44 import tkinter
44 import tkinter
45
45
46 def inputhook(inputhook_context):
46 def inputhook(inputhook_context):
47 """
47 """
48 Inputhook for Tk.
48 Inputhook for Tk.
49 Run the Tk eventloop until prompt-toolkit needs to process the next input.
49 Run the Tk eventloop until prompt-toolkit needs to process the next input.
50 """
50 """
51 # Get the current TK application.
51 # Get the current TK application.
52 root = tkinter._default_root
52 root = tkinter._default_root
53
53
54 def wait_using_filehandler():
54 def wait_using_filehandler():
55 """
55 """
56 Run the TK eventloop until the file handler that we got from the
56 Run the TK eventloop until the file handler that we got from the
57 inputhook becomes readable.
57 inputhook becomes readable.
58 """
58 """
59 # Add a handler that sets the stop flag when `prompt-toolkit` has input
59 # Add a handler that sets the stop flag when `prompt-toolkit` has input
60 # to process.
60 # to process.
61 stop = [False]
61 stop = [False]
62 def done(*a):
62 def done(*a):
63 stop[0] = True
63 stop[0] = True
64
64
65 root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done)
65 root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done)
66
66
67 # Run the TK event loop as long as we don't receive input.
67 # Run the TK event loop as long as we don't receive input.
68 while root.dooneevent(_tkinter.ALL_EVENTS):
68 while root.dooneevent(_tkinter.ALL_EVENTS):
69 if stop[0]:
69 if stop[0]:
70 break
70 break
71
71
72 root.deletefilehandler(inputhook_context.fileno())
72 root.deletefilehandler(inputhook_context.fileno())
73
73
74 def wait_using_polling():
74 def wait_using_polling():
75 """
75 """
76 Windows TK doesn't support 'createfilehandler'.
76 Windows TK doesn't support 'createfilehandler'.
77 So, run the TK eventloop and poll until input is ready.
77 So, run the TK eventloop and poll until input is ready.
78 """
78 """
79 while not inputhook_context.input_is_ready():
79 while not inputhook_context.input_is_ready():
80 while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT):
80 while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT):
81 pass
81 pass
82 # Sleep to make the CPU idle, but not too long, so that the UI
82 # Sleep to make the CPU idle, but not too long, so that the UI
83 # stays responsive.
83 # stays responsive.
84 time.sleep(.01)
84 time.sleep(.01)
85
85
86 if root is not None:
86 if root is not None:
87 if hasattr(root, 'createfilehandler'):
87 if hasattr(root, 'createfilehandler'):
88 wait_using_filehandler()
88 wait_using_filehandler()
89 else:
89 else:
90 wait_using_polling()
90 wait_using_polling()
@@ -1,176 +1,176 b''
1 """Experimental code for cleaner support of IPython syntax with unittest.
1 """Experimental code for cleaner support of IPython syntax with unittest.
2
2
3 In IPython up until 0.10, we've used very hacked up nose machinery for running
3 In IPython up until 0.10, we've used very hacked up nose machinery for running
4 tests with IPython special syntax, and this has proved to be extremely slow.
4 tests with IPython special syntax, and this has proved to be extremely slow.
5 This module provides decorators to try a different approach, stemming from a
5 This module provides decorators to try a different approach, stemming from a
6 conversation Brian and I (FP) had about this problem Sept/09.
6 conversation Brian and I (FP) had about this problem Sept/09.
7
7
8 The goal is to be able to easily write simple functions that can be seen by
8 The goal is to be able to easily write simple functions that can be seen by
9 unittest as tests, and ultimately for these to support doctests with full
9 unittest as tests, and ultimately for these to support doctests with full
10 IPython syntax. Nose already offers this based on naming conventions and our
10 IPython syntax. Nose already offers this based on naming conventions and our
11 hackish plugins, but we are seeking to move away from nose dependencies if
11 hackish plugins, but we are seeking to move away from nose dependencies if
12 possible.
12 possible.
13
13
14 This module follows a different approach, based on decorators.
14 This module follows a different approach, based on decorators.
15
15
16 - A decorator called @ipdoctest can mark any function as having a docstring
16 - A decorator called @ipdoctest can mark any function as having a docstring
17 that should be viewed as a doctest, but after syntax conversion.
17 that should be viewed as a doctest, but after syntax conversion.
18
18
19 Authors
19 Authors
20 -------
20 -------
21
21
22 - Fernando Perez <Fernando.Perez@berkeley.edu>
22 - Fernando Perez <Fernando.Perez@berkeley.edu>
23 """
23 """
24
24
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Copyright (C) 2009-2011 The IPython Development Team
27 # Copyright (C) 2009-2011 The IPython Development Team
28 #
28 #
29 # Distributed under the terms of the BSD License. The full license is in
29 # Distributed under the terms of the BSD License. The full license is in
30 # the file COPYING, distributed as part of this software.
30 # the file COPYING, distributed as part of this software.
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Imports
34 # Imports
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 # Stdlib
37 # Stdlib
38 import re
38 import re
39 import unittest
39 import unittest
40 from doctest import DocTestFinder, DocTestRunner, TestResults
40 from doctest import DocTestFinder, DocTestRunner, TestResults
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # Classes and functions
43 # Classes and functions
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45
45
46 def count_failures(runner):
46 def count_failures(runner):
47 """Count number of failures in a doctest runner.
47 """Count number of failures in a doctest runner.
48
48
49 Code modeled after the summarize() method in doctest.
49 Code modeled after the summarize() method in doctest.
50 """
50 """
51 return [TestResults(f, t) for f, t in runner._name2ft.values() if f > 0 ]
51 return [TestResults(f, t) for f, t in runner._name2ft.values() if f > 0 ]
52
52
53
53
54 class IPython2PythonConverter(object):
54 class IPython2PythonConverter(object):
55 """Convert IPython 'syntax' to valid Python.
55 """Convert IPython 'syntax' to valid Python.
56
56
57 Eventually this code may grow to be the full IPython syntax conversion
57 Eventually this code may grow to be the full IPython syntax conversion
58 implementation, but for now it only does prompt convertion."""
58 implementation, but for now it only does prompt conversion."""
59
59
60 def __init__(self):
60 def __init__(self):
61 self.rps1 = re.compile(r'In\ \[\d+\]: ')
61 self.rps1 = re.compile(r'In\ \[\d+\]: ')
62 self.rps2 = re.compile(r'\ \ \ \.\.\.+: ')
62 self.rps2 = re.compile(r'\ \ \ \.\.\.+: ')
63 self.rout = re.compile(r'Out\[\d+\]: \s*?\n?')
63 self.rout = re.compile(r'Out\[\d+\]: \s*?\n?')
64 self.pyps1 = '>>> '
64 self.pyps1 = '>>> '
65 self.pyps2 = '... '
65 self.pyps2 = '... '
66 self.rpyps1 = re.compile ('(\s*%s)(.*)$' % self.pyps1)
66 self.rpyps1 = re.compile ('(\s*%s)(.*)$' % self.pyps1)
67 self.rpyps2 = re.compile ('(\s*%s)(.*)$' % self.pyps2)
67 self.rpyps2 = re.compile ('(\s*%s)(.*)$' % self.pyps2)
68
68
69 def __call__(self, ds):
69 def __call__(self, ds):
70 """Convert IPython prompts to python ones in a string."""
70 """Convert IPython prompts to python ones in a string."""
71 from . import globalipapp
71 from . import globalipapp
72
72
73 pyps1 = '>>> '
73 pyps1 = '>>> '
74 pyps2 = '... '
74 pyps2 = '... '
75 pyout = ''
75 pyout = ''
76
76
77 dnew = ds
77 dnew = ds
78 dnew = self.rps1.sub(pyps1, dnew)
78 dnew = self.rps1.sub(pyps1, dnew)
79 dnew = self.rps2.sub(pyps2, dnew)
79 dnew = self.rps2.sub(pyps2, dnew)
80 dnew = self.rout.sub(pyout, dnew)
80 dnew = self.rout.sub(pyout, dnew)
81 ip = globalipapp.get_ipython()
81 ip = globalipapp.get_ipython()
82
82
83 # Convert input IPython source into valid Python.
83 # Convert input IPython source into valid Python.
84 out = []
84 out = []
85 newline = out.append
85 newline = out.append
86 for line in dnew.splitlines():
86 for line in dnew.splitlines():
87
87
88 mps1 = self.rpyps1.match(line)
88 mps1 = self.rpyps1.match(line)
89 if mps1 is not None:
89 if mps1 is not None:
90 prompt, text = mps1.groups()
90 prompt, text = mps1.groups()
91 newline(prompt+ip.prefilter(text, False))
91 newline(prompt+ip.prefilter(text, False))
92 continue
92 continue
93
93
94 mps2 = self.rpyps2.match(line)
94 mps2 = self.rpyps2.match(line)
95 if mps2 is not None:
95 if mps2 is not None:
96 prompt, text = mps2.groups()
96 prompt, text = mps2.groups()
97 newline(prompt+ip.prefilter(text, True))
97 newline(prompt+ip.prefilter(text, True))
98 continue
98 continue
99
99
100 newline(line)
100 newline(line)
101 newline('') # ensure a closing newline, needed by doctest
101 newline('') # ensure a closing newline, needed by doctest
102 #print "PYSRC:", '\n'.join(out) # dbg
102 #print "PYSRC:", '\n'.join(out) # dbg
103 return '\n'.join(out)
103 return '\n'.join(out)
104
104
105 #return dnew
105 #return dnew
106
106
107
107
108 class Doc2UnitTester(object):
108 class Doc2UnitTester(object):
109 """Class whose instances act as a decorator for docstring testing.
109 """Class whose instances act as a decorator for docstring testing.
110
110
111 In practice we're only likely to need one instance ever, made below (though
111 In practice we're only likely to need one instance ever, made below (though
112 no attempt is made at turning it into a singleton, there is no need for
112 no attempt is made at turning it into a singleton, there is no need for
113 that).
113 that).
114 """
114 """
115 def __init__(self, verbose=False):
115 def __init__(self, verbose=False):
116 """New decorator.
116 """New decorator.
117
117
118 Parameters
118 Parameters
119 ----------
119 ----------
120
120
121 verbose : boolean, optional (False)
121 verbose : boolean, optional (False)
122 Passed to the doctest finder and runner to control verbosity.
122 Passed to the doctest finder and runner to control verbosity.
123 """
123 """
124 self.verbose = verbose
124 self.verbose = verbose
125 # We can reuse the same finder for all instances
125 # We can reuse the same finder for all instances
126 self.finder = DocTestFinder(verbose=verbose, recurse=False)
126 self.finder = DocTestFinder(verbose=verbose, recurse=False)
127
127
128 def __call__(self, func):
128 def __call__(self, func):
129 """Use as a decorator: doctest a function's docstring as a unittest.
129 """Use as a decorator: doctest a function's docstring as a unittest.
130
130
131 This version runs normal doctests, but the idea is to make it later run
131 This version runs normal doctests, but the idea is to make it later run
132 ipython syntax instead."""
132 ipython syntax instead."""
133
133
134 # Capture the enclosing instance with a different name, so the new
134 # Capture the enclosing instance with a different name, so the new
135 # class below can see it without confusion regarding its own 'self'
135 # class below can see it without confusion regarding its own 'self'
136 # that will point to the test instance at runtime
136 # that will point to the test instance at runtime
137 d2u = self
137 d2u = self
138
138
139 # Rewrite the function's docstring to have python syntax
139 # Rewrite the function's docstring to have python syntax
140 if func.__doc__ is not None:
140 if func.__doc__ is not None:
141 func.__doc__ = ip2py(func.__doc__)
141 func.__doc__ = ip2py(func.__doc__)
142
142
143 # Now, create a tester object that is a real unittest instance, so
143 # Now, create a tester object that is a real unittest instance, so
144 # normal unittest machinery (or Nose, or Trial) can find it.
144 # normal unittest machinery (or Nose, or Trial) can find it.
145 class Tester(unittest.TestCase):
145 class Tester(unittest.TestCase):
146 def test(self):
146 def test(self):
147 # Make a new runner per function to be tested
147 # Make a new runner per function to be tested
148 runner = DocTestRunner(verbose=d2u.verbose)
148 runner = DocTestRunner(verbose=d2u.verbose)
149 map(runner.run, d2u.finder.find(func, func.__name__))
149 map(runner.run, d2u.finder.find(func, func.__name__))
150 failed = count_failures(runner)
150 failed = count_failures(runner)
151 if failed:
151 if failed:
152 # Since we only looked at a single function's docstring,
152 # Since we only looked at a single function's docstring,
153 # failed should contain at most one item. More than that
153 # failed should contain at most one item. More than that
154 # is a case we can't handle and should error out on
154 # is a case we can't handle and should error out on
155 if len(failed) > 1:
155 if len(failed) > 1:
156 err = "Invalid number of test results:" % failed
156 err = "Invalid number of test results:" % failed
157 raise ValueError(err)
157 raise ValueError(err)
158 # Report a normal failure.
158 # Report a normal failure.
159 self.fail('failed doctests: %s' % str(failed[0]))
159 self.fail('failed doctests: %s' % str(failed[0]))
160
160
161 # Rename it so test reports have the original signature.
161 # Rename it so test reports have the original signature.
162 Tester.__name__ = func.__name__
162 Tester.__name__ = func.__name__
163 return Tester
163 return Tester
164
164
165
165
166 def ipdocstring(func):
166 def ipdocstring(func):
167 """Change the function docstring via ip2py.
167 """Change the function docstring via ip2py.
168 """
168 """
169 if func.__doc__ is not None:
169 if func.__doc__ is not None:
170 func.__doc__ = ip2py(func.__doc__)
170 func.__doc__ = ip2py(func.__doc__)
171 return func
171 return func
172
172
173
173
174 # Make an instance of the classes for public use
174 # Make an instance of the classes for public use
175 ipdoctest = Doc2UnitTester()
175 ipdoctest = Doc2UnitTester()
176 ip2py = IPython2PythonConverter()
176 ip2py = IPython2PythonConverter()
@@ -1,191 +1,191 b''
1 """Windows-specific implementation of process utilities.
1 """Windows-specific implementation of process utilities.
2
2
3 This file is only meant to be imported by process.py, not by end-users.
3 This file is only meant to be imported by process.py, not by end-users.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2010-2011 The IPython Development Team
7 # Copyright (C) 2010-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 # stdlib
17 # stdlib
18 import os
18 import os
19 import sys
19 import sys
20 import ctypes
20 import ctypes
21
21
22 from ctypes import c_int, POINTER
22 from ctypes import c_int, POINTER
23 from ctypes.wintypes import LPCWSTR, HLOCAL
23 from ctypes.wintypes import LPCWSTR, HLOCAL
24 from subprocess import STDOUT
24 from subprocess import STDOUT
25
25
26 # our own imports
26 # our own imports
27 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
27 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
28 from . import py3compat
28 from . import py3compat
29 from .encoding import DEFAULT_ENCODING
29 from .encoding import DEFAULT_ENCODING
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Function definitions
32 # Function definitions
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34
34
35 class AvoidUNCPath(object):
35 class AvoidUNCPath(object):
36 """A context manager to protect command execution from UNC paths.
36 """A context manager to protect command execution from UNC paths.
37
37
38 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
38 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
39 This context manager temporarily changes directory to the 'C:' drive on
39 This context manager temporarily changes directory to the 'C:' drive on
40 entering, and restores the original working directory on exit.
40 entering, and restores the original working directory on exit.
41
41
42 The context manager returns the starting working directory *if* it made a
42 The context manager returns the starting working directory *if* it made a
43 change and None otherwise, so that users can apply the necessary adjustment
43 change and None otherwise, so that users can apply the necessary adjustment
44 to their system calls in the event of a change.
44 to their system calls in the event of a change.
45
45
46 Examples
46 Examples
47 --------
47 --------
48 ::
48 ::
49 cmd = 'dir'
49 cmd = 'dir'
50 with AvoidUNCPath() as path:
50 with AvoidUNCPath() as path:
51 if path is not None:
51 if path is not None:
52 cmd = '"pushd %s &&"%s' % (path, cmd)
52 cmd = '"pushd %s &&"%s' % (path, cmd)
53 os.system(cmd)
53 os.system(cmd)
54 """
54 """
55 def __enter__(self):
55 def __enter__(self):
56 self.path = os.getcwd()
56 self.path = os.getcwd()
57 self.is_unc_path = self.path.startswith(r"\\")
57 self.is_unc_path = self.path.startswith(r"\\")
58 if self.is_unc_path:
58 if self.is_unc_path:
59 # change to c drive (as cmd.exe cannot handle UNC addresses)
59 # change to c drive (as cmd.exe cannot handle UNC addresses)
60 os.chdir("C:")
60 os.chdir("C:")
61 return self.path
61 return self.path
62 else:
62 else:
63 # We return None to signal that there was no change in the working
63 # We return None to signal that there was no change in the working
64 # directory
64 # directory
65 return None
65 return None
66
66
67 def __exit__(self, exc_type, exc_value, traceback):
67 def __exit__(self, exc_type, exc_value, traceback):
68 if self.is_unc_path:
68 if self.is_unc_path:
69 os.chdir(self.path)
69 os.chdir(self.path)
70
70
71
71
72 def _find_cmd(cmd):
72 def _find_cmd(cmd):
73 """Find the full path to a .bat or .exe using the win32api module."""
73 """Find the full path to a .bat or .exe using the win32api module."""
74 try:
74 try:
75 from win32api import SearchPath
75 from win32api import SearchPath
76 except ImportError:
76 except ImportError:
77 raise ImportError('you need to have pywin32 installed for this to work')
77 raise ImportError('you need to have pywin32 installed for this to work')
78 else:
78 else:
79 PATH = os.environ['PATH']
79 PATH = os.environ['PATH']
80 extensions = ['.exe', '.com', '.bat', '.py']
80 extensions = ['.exe', '.com', '.bat', '.py']
81 path = None
81 path = None
82 for ext in extensions:
82 for ext in extensions:
83 try:
83 try:
84 path = SearchPath(PATH, cmd, ext)[0]
84 path = SearchPath(PATH, cmd, ext)[0]
85 except:
85 except:
86 pass
86 pass
87 if path is None:
87 if path is None:
88 raise OSError("command %r not found" % cmd)
88 raise OSError("command %r not found" % cmd)
89 else:
89 else:
90 return path
90 return path
91
91
92
92
93 def _system_body(p):
93 def _system_body(p):
94 """Callback for _system."""
94 """Callback for _system."""
95 enc = DEFAULT_ENCODING
95 enc = DEFAULT_ENCODING
96 for line in read_no_interrupt(p.stdout).splitlines():
96 for line in read_no_interrupt(p.stdout).splitlines():
97 line = line.decode(enc, 'replace')
97 line = line.decode(enc, 'replace')
98 print(line, file=sys.stdout)
98 print(line, file=sys.stdout)
99 for line in read_no_interrupt(p.stderr).splitlines():
99 for line in read_no_interrupt(p.stderr).splitlines():
100 line = line.decode(enc, 'replace')
100 line = line.decode(enc, 'replace')
101 print(line, file=sys.stderr)
101 print(line, file=sys.stderr)
102
102
103 # Wait to finish for returncode
103 # Wait to finish for returncode
104 return p.wait()
104 return p.wait()
105
105
106
106
107 def system(cmd):
107 def system(cmd):
108 """Win32 version of os.system() that works with network shares.
108 """Win32 version of os.system() that works with network shares.
109
109
110 Note that this implementation returns None, as meant for use in IPython.
110 Note that this implementation returns None, as meant for use in IPython.
111
111
112 Parameters
112 Parameters
113 ----------
113 ----------
114 cmd : str or list
114 cmd : str or list
115 A command to be executed in the system shell.
115 A command to be executed in the system shell.
116
116
117 Returns
117 Returns
118 -------
118 -------
119 None : we explicitly do NOT return the subprocess status code, as this
119 None : we explicitly do NOT return the subprocess status code, as this
120 utility is meant to be used extensively in IPython, where any return value
120 utility is meant to be used extensively in IPython, where any return value
121 would trigger :func:`sys.displayhook` calls.
121 would trigger :func:`sys.displayhook` calls.
122 """
122 """
123 # The controller provides interactivity with both
123 # The controller provides interactivity with both
124 # stdin and stdout
124 # stdin and stdout
125 #import _process_win32_controller
125 #import _process_win32_controller
126 #_process_win32_controller.system(cmd)
126 #_process_win32_controller.system(cmd)
127
127
128 with AvoidUNCPath() as path:
128 with AvoidUNCPath() as path:
129 if path is not None:
129 if path is not None:
130 cmd = '"pushd %s &&"%s' % (path, cmd)
130 cmd = '"pushd %s &&"%s' % (path, cmd)
131 return process_handler(cmd, _system_body)
131 return process_handler(cmd, _system_body)
132
132
133 def getoutput(cmd):
133 def getoutput(cmd):
134 """Return standard output of executing cmd in a shell.
134 """Return standard output of executing cmd in a shell.
135
135
136 Accepts the same arguments as os.system().
136 Accepts the same arguments as os.system().
137
137
138 Parameters
138 Parameters
139 ----------
139 ----------
140 cmd : str or list
140 cmd : str or list
141 A command to be executed in the system shell.
141 A command to be executed in the system shell.
142
142
143 Returns
143 Returns
144 -------
144 -------
145 stdout : str
145 stdout : str
146 """
146 """
147
147
148 with AvoidUNCPath() as path:
148 with AvoidUNCPath() as path:
149 if path is not None:
149 if path is not None:
150 cmd = '"pushd %s &&"%s' % (path, cmd)
150 cmd = '"pushd %s &&"%s' % (path, cmd)
151 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
151 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
152
152
153 if out is None:
153 if out is None:
154 out = b''
154 out = b''
155 return py3compat.decode(out)
155 return py3compat.decode(out)
156
156
157 try:
157 try:
158 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
158 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
159 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
159 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
160 CommandLineToArgvW.restype = POINTER(LPCWSTR)
160 CommandLineToArgvW.restype = POINTER(LPCWSTR)
161 LocalFree = ctypes.windll.kernel32.LocalFree
161 LocalFree = ctypes.windll.kernel32.LocalFree
162 LocalFree.res_type = HLOCAL
162 LocalFree.res_type = HLOCAL
163 LocalFree.arg_types = [HLOCAL]
163 LocalFree.arg_types = [HLOCAL]
164
164
165 def arg_split(commandline, posix=False, strict=True):
165 def arg_split(commandline, posix=False, strict=True):
166 """Split a command line's arguments in a shell-like manner.
166 """Split a command line's arguments in a shell-like manner.
167
167
168 This is a special version for windows that use a ctypes call to CommandLineToArgvW
168 This is a special version for windows that use a ctypes call to CommandLineToArgvW
169 to do the argv splitting. The posix paramter is ignored.
169 to do the argv splitting. The posix parameter is ignored.
170
170
171 If strict=False, process_common.arg_split(...strict=False) is used instead.
171 If strict=False, process_common.arg_split(...strict=False) is used instead.
172 """
172 """
173 #CommandLineToArgvW returns path to executable if called with empty string.
173 #CommandLineToArgvW returns path to executable if called with empty string.
174 if commandline.strip() == "":
174 if commandline.strip() == "":
175 return []
175 return []
176 if not strict:
176 if not strict:
177 # not really a cl-arg, fallback on _process_common
177 # not really a cl-arg, fallback on _process_common
178 return py_arg_split(commandline, posix=posix, strict=strict)
178 return py_arg_split(commandline, posix=posix, strict=strict)
179 argvn = c_int()
179 argvn = c_int()
180 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
180 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
181 result_array_type = LPCWSTR * argvn.value
181 result_array_type = LPCWSTR * argvn.value
182 result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))]
182 result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))]
183 retval = LocalFree(result_pointer)
183 retval = LocalFree(result_pointer)
184 return result
184 return result
185 except AttributeError:
185 except AttributeError:
186 arg_split = py_arg_split
186 arg_split = py_arg_split
187
187
188 def check_pid(pid):
188 def check_pid(pid):
189 # OpenProcess returns 0 if no such process (of ours) exists
189 # OpenProcess returns 0 if no such process (of ours) exists
190 # positive int otherwise
190 # positive int otherwise
191 return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid))
191 return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid))
@@ -1,438 +1,438 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for path handling.
3 Utilities for path handling.
4 """
4 """
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 import os
9 import os
10 import sys
10 import sys
11 import errno
11 import errno
12 import shutil
12 import shutil
13 import random
13 import random
14 import glob
14 import glob
15 from warnings import warn
15 from warnings import warn
16
16
17 from IPython.utils.process import system
17 from IPython.utils.process import system
18 from IPython.utils import py3compat
18 from IPython.utils import py3compat
19 from IPython.utils.decorators import undoc
19 from IPython.utils.decorators import undoc
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Code
22 # Code
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 fs_encoding = sys.getfilesystemencoding()
25 fs_encoding = sys.getfilesystemencoding()
26
26
27 def _writable_dir(path):
27 def _writable_dir(path):
28 """Whether `path` is a directory, to which the user has write access."""
28 """Whether `path` is a directory, to which the user has write access."""
29 return os.path.isdir(path) and os.access(path, os.W_OK)
29 return os.path.isdir(path) and os.access(path, os.W_OK)
30
30
31 if sys.platform == 'win32':
31 if sys.platform == 'win32':
32 def _get_long_path_name(path):
32 def _get_long_path_name(path):
33 """Get a long path name (expand ~) on Windows using ctypes.
33 """Get a long path name (expand ~) on Windows using ctypes.
34
34
35 Examples
35 Examples
36 --------
36 --------
37
37
38 >>> get_long_path_name('c:\\docume~1')
38 >>> get_long_path_name('c:\\docume~1')
39 'c:\\\\Documents and Settings'
39 'c:\\\\Documents and Settings'
40
40
41 """
41 """
42 try:
42 try:
43 import ctypes
43 import ctypes
44 except ImportError:
44 except ImportError:
45 raise ImportError('you need to have ctypes installed for this to work')
45 raise ImportError('you need to have ctypes installed for this to work')
46 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
46 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
47 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
47 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
48 ctypes.c_uint ]
48 ctypes.c_uint ]
49
49
50 buf = ctypes.create_unicode_buffer(260)
50 buf = ctypes.create_unicode_buffer(260)
51 rv = _GetLongPathName(path, buf, 260)
51 rv = _GetLongPathName(path, buf, 260)
52 if rv == 0 or rv > 260:
52 if rv == 0 or rv > 260:
53 return path
53 return path
54 else:
54 else:
55 return buf.value
55 return buf.value
56 else:
56 else:
57 def _get_long_path_name(path):
57 def _get_long_path_name(path):
58 """Dummy no-op."""
58 """Dummy no-op."""
59 return path
59 return path
60
60
61
61
62
62
63 def get_long_path_name(path):
63 def get_long_path_name(path):
64 """Expand a path into its long form.
64 """Expand a path into its long form.
65
65
66 On Windows this expands any ~ in the paths. On other platforms, it is
66 On Windows this expands any ~ in the paths. On other platforms, it is
67 a null operation.
67 a null operation.
68 """
68 """
69 return _get_long_path_name(path)
69 return _get_long_path_name(path)
70
70
71
71
72 def unquote_filename(name, win32=(sys.platform=='win32')):
72 def unquote_filename(name, win32=(sys.platform=='win32')):
73 """ On Windows, remove leading and trailing quotes from filenames.
73 """ On Windows, remove leading and trailing quotes from filenames.
74
74
75 This function has been deprecated and should not be used any more:
75 This function has been deprecated and should not be used any more:
76 unquoting is now taken care of by :func:`IPython.utils.process.arg_split`.
76 unquoting is now taken care of by :func:`IPython.utils.process.arg_split`.
77 """
77 """
78 warn("'unquote_filename' is deprecated since IPython 5.0 and should not "
78 warn("'unquote_filename' is deprecated since IPython 5.0 and should not "
79 "be used anymore", DeprecationWarning, stacklevel=2)
79 "be used anymore", DeprecationWarning, stacklevel=2)
80 if win32:
80 if win32:
81 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
81 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
82 name = name[1:-1]
82 name = name[1:-1]
83 return name
83 return name
84
84
85
85
86 def compress_user(path):
86 def compress_user(path):
87 """Reverse of :func:`os.path.expanduser`
87 """Reverse of :func:`os.path.expanduser`
88 """
88 """
89 home = os.path.expanduser('~')
89 home = os.path.expanduser('~')
90 if path.startswith(home):
90 if path.startswith(home):
91 path = "~" + path[len(home):]
91 path = "~" + path[len(home):]
92 return path
92 return path
93
93
94 def get_py_filename(name, force_win32=None):
94 def get_py_filename(name, force_win32=None):
95 """Return a valid python filename in the current directory.
95 """Return a valid python filename in the current directory.
96
96
97 If the given name is not a file, it adds '.py' and searches again.
97 If the given name is not a file, it adds '.py' and searches again.
98 Raises IOError with an informative message if the file isn't found.
98 Raises IOError with an informative message if the file isn't found.
99 """
99 """
100
100
101 name = os.path.expanduser(name)
101 name = os.path.expanduser(name)
102 if force_win32 is not None:
102 if force_win32 is not None:
103 warn("The 'force_win32' argument to 'get_py_filename' is deprecated "
103 warn("The 'force_win32' argument to 'get_py_filename' is deprecated "
104 "since IPython 5.0 and should not be used anymore",
104 "since IPython 5.0 and should not be used anymore",
105 DeprecationWarning, stacklevel=2)
105 DeprecationWarning, stacklevel=2)
106 if not os.path.isfile(name) and not name.endswith('.py'):
106 if not os.path.isfile(name) and not name.endswith('.py'):
107 name += '.py'
107 name += '.py'
108 if os.path.isfile(name):
108 if os.path.isfile(name):
109 return name
109 return name
110 else:
110 else:
111 raise IOError('File `%r` not found.' % name)
111 raise IOError('File `%r` not found.' % name)
112
112
113
113
114 def filefind(filename, path_dirs=None):
114 def filefind(filename, path_dirs=None):
115 """Find a file by looking through a sequence of paths.
115 """Find a file by looking through a sequence of paths.
116
116
117 This iterates through a sequence of paths looking for a file and returns
117 This iterates through a sequence of paths looking for a file and returns
118 the full, absolute path of the first occurence of the file. If no set of
118 the full, absolute path of the first occurrence of the file. If no set of
119 path dirs is given, the filename is tested as is, after running through
119 path dirs is given, the filename is tested as is, after running through
120 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
120 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
121
121
122 filefind('myfile.txt')
122 filefind('myfile.txt')
123
123
124 will find the file in the current working dir, but::
124 will find the file in the current working dir, but::
125
125
126 filefind('~/myfile.txt')
126 filefind('~/myfile.txt')
127
127
128 Will find the file in the users home directory. This function does not
128 Will find the file in the users home directory. This function does not
129 automatically try any paths, such as the cwd or the user's home directory.
129 automatically try any paths, such as the cwd or the user's home directory.
130
130
131 Parameters
131 Parameters
132 ----------
132 ----------
133 filename : str
133 filename : str
134 The filename to look for.
134 The filename to look for.
135 path_dirs : str, None or sequence of str
135 path_dirs : str, None or sequence of str
136 The sequence of paths to look for the file in. If None, the filename
136 The sequence of paths to look for the file in. If None, the filename
137 need to be absolute or be in the cwd. If a string, the string is
137 need to be absolute or be in the cwd. If a string, the string is
138 put into a sequence and the searched. If a sequence, walk through
138 put into a sequence and the searched. If a sequence, walk through
139 each element and join with ``filename``, calling :func:`expandvars`
139 each element and join with ``filename``, calling :func:`expandvars`
140 and :func:`expanduser` before testing for existence.
140 and :func:`expanduser` before testing for existence.
141
141
142 Returns
142 Returns
143 -------
143 -------
144 Raises :exc:`IOError` or returns absolute path to file.
144 Raises :exc:`IOError` or returns absolute path to file.
145 """
145 """
146
146
147 # If paths are quoted, abspath gets confused, strip them...
147 # If paths are quoted, abspath gets confused, strip them...
148 filename = filename.strip('"').strip("'")
148 filename = filename.strip('"').strip("'")
149 # If the input is an absolute path, just check it exists
149 # If the input is an absolute path, just check it exists
150 if os.path.isabs(filename) and os.path.isfile(filename):
150 if os.path.isabs(filename) and os.path.isfile(filename):
151 return filename
151 return filename
152
152
153 if path_dirs is None:
153 if path_dirs is None:
154 path_dirs = ("",)
154 path_dirs = ("",)
155 elif isinstance(path_dirs, str):
155 elif isinstance(path_dirs, str):
156 path_dirs = (path_dirs,)
156 path_dirs = (path_dirs,)
157
157
158 for path in path_dirs:
158 for path in path_dirs:
159 if path == '.': path = os.getcwd()
159 if path == '.': path = os.getcwd()
160 testname = expand_path(os.path.join(path, filename))
160 testname = expand_path(os.path.join(path, filename))
161 if os.path.isfile(testname):
161 if os.path.isfile(testname):
162 return os.path.abspath(testname)
162 return os.path.abspath(testname)
163
163
164 raise IOError("File %r does not exist in any of the search paths: %r" %
164 raise IOError("File %r does not exist in any of the search paths: %r" %
165 (filename, path_dirs) )
165 (filename, path_dirs) )
166
166
167
167
168 class HomeDirError(Exception):
168 class HomeDirError(Exception):
169 pass
169 pass
170
170
171
171
172 def get_home_dir(require_writable=False):
172 def get_home_dir(require_writable=False):
173 """Return the 'home' directory, as a unicode string.
173 """Return the 'home' directory, as a unicode string.
174
174
175 Uses os.path.expanduser('~'), and checks for writability.
175 Uses os.path.expanduser('~'), and checks for writability.
176
176
177 See stdlib docs for how this is determined.
177 See stdlib docs for how this is determined.
178 $HOME is first priority on *ALL* platforms.
178 $HOME is first priority on *ALL* platforms.
179
179
180 Parameters
180 Parameters
181 ----------
181 ----------
182
182
183 require_writable : bool [default: False]
183 require_writable : bool [default: False]
184 if True:
184 if True:
185 guarantees the return value is a writable directory, otherwise
185 guarantees the return value is a writable directory, otherwise
186 raises HomeDirError
186 raises HomeDirError
187 if False:
187 if False:
188 The path is resolved, but it is not guaranteed to exist or be writable.
188 The path is resolved, but it is not guaranteed to exist or be writable.
189 """
189 """
190
190
191 homedir = os.path.expanduser('~')
191 homedir = os.path.expanduser('~')
192 # Next line will make things work even when /home/ is a symlink to
192 # Next line will make things work even when /home/ is a symlink to
193 # /usr/home as it is on FreeBSD, for example
193 # /usr/home as it is on FreeBSD, for example
194 homedir = os.path.realpath(homedir)
194 homedir = os.path.realpath(homedir)
195
195
196 if not _writable_dir(homedir) and os.name == 'nt':
196 if not _writable_dir(homedir) and os.name == 'nt':
197 # expanduser failed, use the registry to get the 'My Documents' folder.
197 # expanduser failed, use the registry to get the 'My Documents' folder.
198 try:
198 try:
199 try:
199 try:
200 import winreg as wreg # Py 3
200 import winreg as wreg # Py 3
201 except ImportError:
201 except ImportError:
202 import _winreg as wreg # Py 2
202 import _winreg as wreg # Py 2
203 key = wreg.OpenKey(
203 key = wreg.OpenKey(
204 wreg.HKEY_CURRENT_USER,
204 wreg.HKEY_CURRENT_USER,
205 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
205 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
206 )
206 )
207 homedir = wreg.QueryValueEx(key,'Personal')[0]
207 homedir = wreg.QueryValueEx(key,'Personal')[0]
208 key.Close()
208 key.Close()
209 except:
209 except:
210 pass
210 pass
211
211
212 if (not require_writable) or _writable_dir(homedir):
212 if (not require_writable) or _writable_dir(homedir):
213 return py3compat.cast_unicode(homedir, fs_encoding)
213 return py3compat.cast_unicode(homedir, fs_encoding)
214 else:
214 else:
215 raise HomeDirError('%s is not a writable dir, '
215 raise HomeDirError('%s is not a writable dir, '
216 'set $HOME environment variable to override' % homedir)
216 'set $HOME environment variable to override' % homedir)
217
217
218 def get_xdg_dir():
218 def get_xdg_dir():
219 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
219 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
220
220
221 This is only for non-OS X posix (Linux,Unix,etc.) systems.
221 This is only for non-OS X posix (Linux,Unix,etc.) systems.
222 """
222 """
223
223
224 env = os.environ
224 env = os.environ
225
225
226 if os.name == 'posix' and sys.platform != 'darwin':
226 if os.name == 'posix' and sys.platform != 'darwin':
227 # Linux, Unix, AIX, etc.
227 # Linux, Unix, AIX, etc.
228 # use ~/.config if empty OR not set
228 # use ~/.config if empty OR not set
229 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
229 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
230 if xdg and _writable_dir(xdg):
230 if xdg and _writable_dir(xdg):
231 return py3compat.cast_unicode(xdg, fs_encoding)
231 return py3compat.cast_unicode(xdg, fs_encoding)
232
232
233 return None
233 return None
234
234
235
235
236 def get_xdg_cache_dir():
236 def get_xdg_cache_dir():
237 """Return the XDG_CACHE_HOME, if it is defined and exists, else None.
237 """Return the XDG_CACHE_HOME, if it is defined and exists, else None.
238
238
239 This is only for non-OS X posix (Linux,Unix,etc.) systems.
239 This is only for non-OS X posix (Linux,Unix,etc.) systems.
240 """
240 """
241
241
242 env = os.environ
242 env = os.environ
243
243
244 if os.name == 'posix' and sys.platform != 'darwin':
244 if os.name == 'posix' and sys.platform != 'darwin':
245 # Linux, Unix, AIX, etc.
245 # Linux, Unix, AIX, etc.
246 # use ~/.cache if empty OR not set
246 # use ~/.cache if empty OR not set
247 xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache')
247 xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache')
248 if xdg and _writable_dir(xdg):
248 if xdg and _writable_dir(xdg):
249 return py3compat.cast_unicode(xdg, fs_encoding)
249 return py3compat.cast_unicode(xdg, fs_encoding)
250
250
251 return None
251 return None
252
252
253
253
254 @undoc
254 @undoc
255 def get_ipython_dir():
255 def get_ipython_dir():
256 warn("get_ipython_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
256 warn("get_ipython_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
257 from IPython.paths import get_ipython_dir
257 from IPython.paths import get_ipython_dir
258 return get_ipython_dir()
258 return get_ipython_dir()
259
259
260 @undoc
260 @undoc
261 def get_ipython_cache_dir():
261 def get_ipython_cache_dir():
262 warn("get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
262 warn("get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
263 from IPython.paths import get_ipython_cache_dir
263 from IPython.paths import get_ipython_cache_dir
264 return get_ipython_cache_dir()
264 return get_ipython_cache_dir()
265
265
266 @undoc
266 @undoc
267 def get_ipython_package_dir():
267 def get_ipython_package_dir():
268 warn("get_ipython_package_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
268 warn("get_ipython_package_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
269 from IPython.paths import get_ipython_package_dir
269 from IPython.paths import get_ipython_package_dir
270 return get_ipython_package_dir()
270 return get_ipython_package_dir()
271
271
272 @undoc
272 @undoc
273 def get_ipython_module_path(module_str):
273 def get_ipython_module_path(module_str):
274 warn("get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
274 warn("get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
275 from IPython.paths import get_ipython_module_path
275 from IPython.paths import get_ipython_module_path
276 return get_ipython_module_path(module_str)
276 return get_ipython_module_path(module_str)
277
277
278 @undoc
278 @undoc
279 def locate_profile(profile='default'):
279 def locate_profile(profile='default'):
280 warn("locate_profile has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
280 warn("locate_profile has moved to the IPython.paths module since IPython 4.0.", stacklevel=2)
281 from IPython.paths import locate_profile
281 from IPython.paths import locate_profile
282 return locate_profile(profile=profile)
282 return locate_profile(profile=profile)
283
283
284 def expand_path(s):
284 def expand_path(s):
285 """Expand $VARS and ~names in a string, like a shell
285 """Expand $VARS and ~names in a string, like a shell
286
286
287 :Examples:
287 :Examples:
288
288
289 In [2]: os.environ['FOO']='test'
289 In [2]: os.environ['FOO']='test'
290
290
291 In [3]: expand_path('variable FOO is $FOO')
291 In [3]: expand_path('variable FOO is $FOO')
292 Out[3]: 'variable FOO is test'
292 Out[3]: 'variable FOO is test'
293 """
293 """
294 # This is a pretty subtle hack. When expand user is given a UNC path
294 # This is a pretty subtle hack. When expand user is given a UNC path
295 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
295 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
296 # the $ to get (\\server\share\%username%). I think it considered $
296 # the $ to get (\\server\share\%username%). I think it considered $
297 # alone an empty var. But, we need the $ to remains there (it indicates
297 # alone an empty var. But, we need the $ to remains there (it indicates
298 # a hidden share).
298 # a hidden share).
299 if os.name=='nt':
299 if os.name=='nt':
300 s = s.replace('$\\', 'IPYTHON_TEMP')
300 s = s.replace('$\\', 'IPYTHON_TEMP')
301 s = os.path.expandvars(os.path.expanduser(s))
301 s = os.path.expandvars(os.path.expanduser(s))
302 if os.name=='nt':
302 if os.name=='nt':
303 s = s.replace('IPYTHON_TEMP', '$\\')
303 s = s.replace('IPYTHON_TEMP', '$\\')
304 return s
304 return s
305
305
306
306
307 def unescape_glob(string):
307 def unescape_glob(string):
308 """Unescape glob pattern in `string`."""
308 """Unescape glob pattern in `string`."""
309 def unescape(s):
309 def unescape(s):
310 for pattern in '*[]!?':
310 for pattern in '*[]!?':
311 s = s.replace(r'\{0}'.format(pattern), pattern)
311 s = s.replace(r'\{0}'.format(pattern), pattern)
312 return s
312 return s
313 return '\\'.join(map(unescape, string.split('\\\\')))
313 return '\\'.join(map(unescape, string.split('\\\\')))
314
314
315
315
316 def shellglob(args):
316 def shellglob(args):
317 """
317 """
318 Do glob expansion for each element in `args` and return a flattened list.
318 Do glob expansion for each element in `args` and return a flattened list.
319
319
320 Unmatched glob pattern will remain as-is in the returned list.
320 Unmatched glob pattern will remain as-is in the returned list.
321
321
322 """
322 """
323 expanded = []
323 expanded = []
324 # Do not unescape backslash in Windows as it is interpreted as
324 # Do not unescape backslash in Windows as it is interpreted as
325 # path separator:
325 # path separator:
326 unescape = unescape_glob if sys.platform != 'win32' else lambda x: x
326 unescape = unescape_glob if sys.platform != 'win32' else lambda x: x
327 for a in args:
327 for a in args:
328 expanded.extend(glob.glob(a) or [unescape(a)])
328 expanded.extend(glob.glob(a) or [unescape(a)])
329 return expanded
329 return expanded
330
330
331
331
332 def target_outdated(target,deps):
332 def target_outdated(target,deps):
333 """Determine whether a target is out of date.
333 """Determine whether a target is out of date.
334
334
335 target_outdated(target,deps) -> 1/0
335 target_outdated(target,deps) -> 1/0
336
336
337 deps: list of filenames which MUST exist.
337 deps: list of filenames which MUST exist.
338 target: single filename which may or may not exist.
338 target: single filename which may or may not exist.
339
339
340 If target doesn't exist or is older than any file listed in deps, return
340 If target doesn't exist or is older than any file listed in deps, return
341 true, otherwise return false.
341 true, otherwise return false.
342 """
342 """
343 try:
343 try:
344 target_time = os.path.getmtime(target)
344 target_time = os.path.getmtime(target)
345 except os.error:
345 except os.error:
346 return 1
346 return 1
347 for dep in deps:
347 for dep in deps:
348 dep_time = os.path.getmtime(dep)
348 dep_time = os.path.getmtime(dep)
349 if dep_time > target_time:
349 if dep_time > target_time:
350 #print "For target",target,"Dep failed:",dep # dbg
350 #print "For target",target,"Dep failed:",dep # dbg
351 #print "times (dep,tar):",dep_time,target_time # dbg
351 #print "times (dep,tar):",dep_time,target_time # dbg
352 return 1
352 return 1
353 return 0
353 return 0
354
354
355
355
356 def target_update(target,deps,cmd):
356 def target_update(target,deps,cmd):
357 """Update a target with a given command given a list of dependencies.
357 """Update a target with a given command given a list of dependencies.
358
358
359 target_update(target,deps,cmd) -> runs cmd if target is outdated.
359 target_update(target,deps,cmd) -> runs cmd if target is outdated.
360
360
361 This is just a wrapper around target_outdated() which calls the given
361 This is just a wrapper around target_outdated() which calls the given
362 command if target is outdated."""
362 command if target is outdated."""
363
363
364 if target_outdated(target,deps):
364 if target_outdated(target,deps):
365 system(cmd)
365 system(cmd)
366
366
367
367
368 ENOLINK = 1998
368 ENOLINK = 1998
369
369
370 def link(src, dst):
370 def link(src, dst):
371 """Hard links ``src`` to ``dst``, returning 0 or errno.
371 """Hard links ``src`` to ``dst``, returning 0 or errno.
372
372
373 Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
373 Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
374 supported by the operating system.
374 supported by the operating system.
375 """
375 """
376
376
377 if not hasattr(os, "link"):
377 if not hasattr(os, "link"):
378 return ENOLINK
378 return ENOLINK
379 link_errno = 0
379 link_errno = 0
380 try:
380 try:
381 os.link(src, dst)
381 os.link(src, dst)
382 except OSError as e:
382 except OSError as e:
383 link_errno = e.errno
383 link_errno = e.errno
384 return link_errno
384 return link_errno
385
385
386
386
387 def link_or_copy(src, dst):
387 def link_or_copy(src, dst):
388 """Attempts to hardlink ``src`` to ``dst``, copying if the link fails.
388 """Attempts to hardlink ``src`` to ``dst``, copying if the link fails.
389
389
390 Attempts to maintain the semantics of ``shutil.copy``.
390 Attempts to maintain the semantics of ``shutil.copy``.
391
391
392 Because ``os.link`` does not overwrite files, a unique temporary file
392 Because ``os.link`` does not overwrite files, a unique temporary file
393 will be used if the target already exists, then that file will be moved
393 will be used if the target already exists, then that file will be moved
394 into place.
394 into place.
395 """
395 """
396
396
397 if os.path.isdir(dst):
397 if os.path.isdir(dst):
398 dst = os.path.join(dst, os.path.basename(src))
398 dst = os.path.join(dst, os.path.basename(src))
399
399
400 link_errno = link(src, dst)
400 link_errno = link(src, dst)
401 if link_errno == errno.EEXIST:
401 if link_errno == errno.EEXIST:
402 if os.stat(src).st_ino == os.stat(dst).st_ino:
402 if os.stat(src).st_ino == os.stat(dst).st_ino:
403 # dst is already a hard link to the correct file, so we don't need
403 # dst is already a hard link to the correct file, so we don't need
404 # to do anything else. If we try to link and rename the file
404 # to do anything else. If we try to link and rename the file
405 # anyway, we get duplicate files - see http://bugs.python.org/issue21876
405 # anyway, we get duplicate files - see http://bugs.python.org/issue21876
406 return
406 return
407
407
408 new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), )
408 new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), )
409 try:
409 try:
410 link_or_copy(src, new_dst)
410 link_or_copy(src, new_dst)
411 except:
411 except:
412 try:
412 try:
413 os.remove(new_dst)
413 os.remove(new_dst)
414 except OSError:
414 except OSError:
415 pass
415 pass
416 raise
416 raise
417 os.rename(new_dst, dst)
417 os.rename(new_dst, dst)
418 elif link_errno != 0:
418 elif link_errno != 0:
419 # Either link isn't supported, or the filesystem doesn't support
419 # Either link isn't supported, or the filesystem doesn't support
420 # linking, or 'src' and 'dst' are on different filesystems.
420 # linking, or 'src' and 'dst' are on different filesystems.
421 shutil.copy(src, dst)
421 shutil.copy(src, dst)
422
422
423 def ensure_dir_exists(path, mode=0o755):
423 def ensure_dir_exists(path, mode=0o755):
424 """ensure that a directory exists
424 """ensure that a directory exists
425
425
426 If it doesn't exist, try to create it and protect against a race condition
426 If it doesn't exist, try to create it and protect against a race condition
427 if another process is doing the same.
427 if another process is doing the same.
428
428
429 The default permissions are 755, which differ from os.makedirs default of 777.
429 The default permissions are 755, which differ from os.makedirs default of 777.
430 """
430 """
431 if not os.path.exists(path):
431 if not os.path.exists(path):
432 try:
432 try:
433 os.makedirs(path, mode=mode)
433 os.makedirs(path, mode=mode)
434 except OSError as e:
434 except OSError as e:
435 if e.errno != errno.EEXIST:
435 if e.errno != errno.EEXIST:
436 raise
436 raise
437 elif not os.path.isdir(path):
437 elif not os.path.isdir(path):
438 raise IOError("%r exists but is not a directory" % path)
438 raise IOError("%r exists but is not a directory" % path)
@@ -1,481 +1,481 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import os
7 import os
8 import shutil
8 import shutil
9 import sys
9 import sys
10 import tempfile
10 import tempfile
11 import unittest
11 import unittest
12 from contextlib import contextmanager
12 from contextlib import contextmanager
13 from unittest.mock import patch
13 from unittest.mock import patch
14 from os.path import join, abspath
14 from os.path import join, abspath
15 from imp import reload
15 from imp import reload
16
16
17 from nose import SkipTest, with_setup
17 from nose import SkipTest, with_setup
18 import nose.tools as nt
18 import nose.tools as nt
19
19
20 import IPython
20 import IPython
21 from IPython import paths
21 from IPython import paths
22 from IPython.testing import decorators as dec
22 from IPython.testing import decorators as dec
23 from IPython.testing.decorators import (skip_if_not_win32, skip_win32,
23 from IPython.testing.decorators import (skip_if_not_win32, skip_win32,
24 onlyif_unicode_paths,)
24 onlyif_unicode_paths,)
25 from IPython.testing.tools import make_tempfile, AssertPrints
25 from IPython.testing.tools import make_tempfile, AssertPrints
26 from IPython.utils import path
26 from IPython.utils import path
27 from IPython.utils.tempdir import TemporaryDirectory
27 from IPython.utils.tempdir import TemporaryDirectory
28
28
29 # Platform-dependent imports
29 # Platform-dependent imports
30 try:
30 try:
31 import winreg as wreg
31 import winreg as wreg
32 except ImportError:
32 except ImportError:
33 #Fake _winreg module on non-windows platforms
33 #Fake _winreg module on non-windows platforms
34 import types
34 import types
35 wr_name = "winreg"
35 wr_name = "winreg"
36 sys.modules[wr_name] = types.ModuleType(wr_name)
36 sys.modules[wr_name] = types.ModuleType(wr_name)
37 try:
37 try:
38 import winreg as wreg
38 import winreg as wreg
39 except ImportError:
39 except ImportError:
40 import _winreg as wreg
40 import _winreg as wreg
41 #Add entries that needs to be stubbed by the testing code
41 #Add entries that needs to be stubbed by the testing code
42 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
42 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # Globals
45 # Globals
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47 env = os.environ
47 env = os.environ
48 TMP_TEST_DIR = tempfile.mkdtemp()
48 TMP_TEST_DIR = tempfile.mkdtemp()
49 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
49 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
50 #
50 #
51 # Setup/teardown functions/decorators
51 # Setup/teardown functions/decorators
52 #
52 #
53
53
54 def setup():
54 def setup():
55 """Setup testenvironment for the module:
55 """Setup testenvironment for the module:
56
56
57 - Adds dummy home dir tree
57 - Adds dummy home dir tree
58 """
58 """
59 # Do not mask exceptions here. In particular, catching WindowsError is a
59 # Do not mask exceptions here. In particular, catching WindowsError is a
60 # problem because that exception is only defined on Windows...
60 # problem because that exception is only defined on Windows...
61 os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython'))
61 os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython'))
62
62
63
63
64 def teardown():
64 def teardown():
65 """Teardown testenvironment for the module:
65 """Teardown testenvironment for the module:
66
66
67 - Remove dummy home dir tree
67 - Remove dummy home dir tree
68 """
68 """
69 # Note: we remove the parent test dir, which is the root of all test
69 # Note: we remove the parent test dir, which is the root of all test
70 # subdirs we may have created. Use shutil instead of os.removedirs, so
70 # subdirs we may have created. Use shutil instead of os.removedirs, so
71 # that non-empty directories are all recursively removed.
71 # that non-empty directories are all recursively removed.
72 shutil.rmtree(TMP_TEST_DIR)
72 shutil.rmtree(TMP_TEST_DIR)
73
73
74
74
75 def setup_environment():
75 def setup_environment():
76 """Setup testenvironment for some functions that are tested
76 """Setup testenvironment for some functions that are tested
77 in this module. In particular this functions stores attributes
77 in this module. In particular this functions stores attributes
78 and other things that we need to stub in some test functions.
78 and other things that we need to stub in some test functions.
79 This needs to be done on a function level and not module level because
79 This needs to be done on a function level and not module level because
80 each testfunction needs a pristine environment.
80 each testfunction needs a pristine environment.
81 """
81 """
82 global oldstuff, platformstuff
82 global oldstuff, platformstuff
83 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
83 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
84
84
85 def teardown_environment():
85 def teardown_environment():
86 """Restore things that were remembered by the setup_environment function
86 """Restore things that were remembered by the setup_environment function
87 """
87 """
88 (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
88 (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
89 os.chdir(old_wd)
89 os.chdir(old_wd)
90 reload(path)
90 reload(path)
91
91
92 for key in list(env):
92 for key in list(env):
93 if key not in oldenv:
93 if key not in oldenv:
94 del env[key]
94 del env[key]
95 env.update(oldenv)
95 env.update(oldenv)
96 if hasattr(sys, 'frozen'):
96 if hasattr(sys, 'frozen'):
97 del sys.frozen
97 del sys.frozen
98
98
99 # Build decorator that uses the setup_environment/setup_environment
99 # Build decorator that uses the setup_environment/setup_environment
100 with_environment = with_setup(setup_environment, teardown_environment)
100 with_environment = with_setup(setup_environment, teardown_environment)
101
101
102 @skip_if_not_win32
102 @skip_if_not_win32
103 @with_environment
103 @with_environment
104 def test_get_home_dir_1():
104 def test_get_home_dir_1():
105 """Testcase for py2exe logic, un-compressed lib
105 """Testcase for py2exe logic, un-compressed lib
106 """
106 """
107 unfrozen = path.get_home_dir()
107 unfrozen = path.get_home_dir()
108 sys.frozen = True
108 sys.frozen = True
109
109
110 #fake filename for IPython.__init__
110 #fake filename for IPython.__init__
111 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
111 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
112
112
113 home_dir = path.get_home_dir()
113 home_dir = path.get_home_dir()
114 nt.assert_equal(home_dir, unfrozen)
114 nt.assert_equal(home_dir, unfrozen)
115
115
116
116
117 @skip_if_not_win32
117 @skip_if_not_win32
118 @with_environment
118 @with_environment
119 def test_get_home_dir_2():
119 def test_get_home_dir_2():
120 """Testcase for py2exe logic, compressed lib
120 """Testcase for py2exe logic, compressed lib
121 """
121 """
122 unfrozen = path.get_home_dir()
122 unfrozen = path.get_home_dir()
123 sys.frozen = True
123 sys.frozen = True
124 #fake filename for IPython.__init__
124 #fake filename for IPython.__init__
125 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
125 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
126
126
127 home_dir = path.get_home_dir(True)
127 home_dir = path.get_home_dir(True)
128 nt.assert_equal(home_dir, unfrozen)
128 nt.assert_equal(home_dir, unfrozen)
129
129
130
130
131 @with_environment
131 @with_environment
132 def test_get_home_dir_3():
132 def test_get_home_dir_3():
133 """get_home_dir() uses $HOME if set"""
133 """get_home_dir() uses $HOME if set"""
134 env["HOME"] = HOME_TEST_DIR
134 env["HOME"] = HOME_TEST_DIR
135 home_dir = path.get_home_dir(True)
135 home_dir = path.get_home_dir(True)
136 # get_home_dir expands symlinks
136 # get_home_dir expands symlinks
137 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
137 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
138
138
139
139
140 @with_environment
140 @with_environment
141 def test_get_home_dir_4():
141 def test_get_home_dir_4():
142 """get_home_dir() still works if $HOME is not set"""
142 """get_home_dir() still works if $HOME is not set"""
143
143
144 if 'HOME' in env: del env['HOME']
144 if 'HOME' in env: del env['HOME']
145 # this should still succeed, but we don't care what the answer is
145 # this should still succeed, but we don't care what the answer is
146 home = path.get_home_dir(False)
146 home = path.get_home_dir(False)
147
147
148 @with_environment
148 @with_environment
149 def test_get_home_dir_5():
149 def test_get_home_dir_5():
150 """raise HomeDirError if $HOME is specified, but not a writable dir"""
150 """raise HomeDirError if $HOME is specified, but not a writable dir"""
151 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
151 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
152 # set os.name = posix, to prevent My Documents fallback on Windows
152 # set os.name = posix, to prevent My Documents fallback on Windows
153 os.name = 'posix'
153 os.name = 'posix'
154 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
154 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
155
155
156 # Should we stub wreg fully so we can run the test on all platforms?
156 # Should we stub wreg fully so we can run the test on all platforms?
157 @skip_if_not_win32
157 @skip_if_not_win32
158 @with_environment
158 @with_environment
159 def test_get_home_dir_8():
159 def test_get_home_dir_8():
160 """Using registry hack for 'My Documents', os=='nt'
160 """Using registry hack for 'My Documents', os=='nt'
161
161
162 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
162 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
163 """
163 """
164 os.name = 'nt'
164 os.name = 'nt'
165 # Remove from stub environment all keys that may be set
165 # Remove from stub environment all keys that may be set
166 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
166 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
167 env.pop(key, None)
167 env.pop(key, None)
168
168
169 class key:
169 class key:
170 def Close(self):
170 def Close(self):
171 pass
171 pass
172
172
173 with patch.object(wreg, 'OpenKey', return_value=key()), \
173 with patch.object(wreg, 'OpenKey', return_value=key()), \
174 patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]):
174 patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]):
175 home_dir = path.get_home_dir()
175 home_dir = path.get_home_dir()
176 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
176 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
177
177
178 @with_environment
178 @with_environment
179 def test_get_xdg_dir_0():
179 def test_get_xdg_dir_0():
180 """test_get_xdg_dir_0, check xdg_dir"""
180 """test_get_xdg_dir_0, check xdg_dir"""
181 reload(path)
181 reload(path)
182 path._writable_dir = lambda path: True
182 path._writable_dir = lambda path: True
183 path.get_home_dir = lambda : 'somewhere'
183 path.get_home_dir = lambda : 'somewhere'
184 os.name = "posix"
184 os.name = "posix"
185 sys.platform = "linux2"
185 sys.platform = "linux2"
186 env.pop('IPYTHON_DIR', None)
186 env.pop('IPYTHON_DIR', None)
187 env.pop('IPYTHONDIR', None)
187 env.pop('IPYTHONDIR', None)
188 env.pop('XDG_CONFIG_HOME', None)
188 env.pop('XDG_CONFIG_HOME', None)
189
189
190 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
190 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
191
191
192
192
193 @with_environment
193 @with_environment
194 def test_get_xdg_dir_1():
194 def test_get_xdg_dir_1():
195 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
195 """test_get_xdg_dir_1, check nonexistent xdg_dir"""
196 reload(path)
196 reload(path)
197 path.get_home_dir = lambda : HOME_TEST_DIR
197 path.get_home_dir = lambda : HOME_TEST_DIR
198 os.name = "posix"
198 os.name = "posix"
199 sys.platform = "linux2"
199 sys.platform = "linux2"
200 env.pop('IPYTHON_DIR', None)
200 env.pop('IPYTHON_DIR', None)
201 env.pop('IPYTHONDIR', None)
201 env.pop('IPYTHONDIR', None)
202 env.pop('XDG_CONFIG_HOME', None)
202 env.pop('XDG_CONFIG_HOME', None)
203 nt.assert_equal(path.get_xdg_dir(), None)
203 nt.assert_equal(path.get_xdg_dir(), None)
204
204
205 @with_environment
205 @with_environment
206 def test_get_xdg_dir_2():
206 def test_get_xdg_dir_2():
207 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
207 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
208 reload(path)
208 reload(path)
209 path.get_home_dir = lambda : HOME_TEST_DIR
209 path.get_home_dir = lambda : HOME_TEST_DIR
210 os.name = "posix"
210 os.name = "posix"
211 sys.platform = "linux2"
211 sys.platform = "linux2"
212 env.pop('IPYTHON_DIR', None)
212 env.pop('IPYTHON_DIR', None)
213 env.pop('IPYTHONDIR', None)
213 env.pop('IPYTHONDIR', None)
214 env.pop('XDG_CONFIG_HOME', None)
214 env.pop('XDG_CONFIG_HOME', None)
215 cfgdir=os.path.join(path.get_home_dir(), '.config')
215 cfgdir=os.path.join(path.get_home_dir(), '.config')
216 if not os.path.exists(cfgdir):
216 if not os.path.exists(cfgdir):
217 os.makedirs(cfgdir)
217 os.makedirs(cfgdir)
218
218
219 nt.assert_equal(path.get_xdg_dir(), cfgdir)
219 nt.assert_equal(path.get_xdg_dir(), cfgdir)
220
220
221 @with_environment
221 @with_environment
222 def test_get_xdg_dir_3():
222 def test_get_xdg_dir_3():
223 """test_get_xdg_dir_3, check xdg_dir not used on OS X"""
223 """test_get_xdg_dir_3, check xdg_dir not used on OS X"""
224 reload(path)
224 reload(path)
225 path.get_home_dir = lambda : HOME_TEST_DIR
225 path.get_home_dir = lambda : HOME_TEST_DIR
226 os.name = "posix"
226 os.name = "posix"
227 sys.platform = "darwin"
227 sys.platform = "darwin"
228 env.pop('IPYTHON_DIR', None)
228 env.pop('IPYTHON_DIR', None)
229 env.pop('IPYTHONDIR', None)
229 env.pop('IPYTHONDIR', None)
230 env.pop('XDG_CONFIG_HOME', None)
230 env.pop('XDG_CONFIG_HOME', None)
231 cfgdir=os.path.join(path.get_home_dir(), '.config')
231 cfgdir=os.path.join(path.get_home_dir(), '.config')
232 if not os.path.exists(cfgdir):
232 if not os.path.exists(cfgdir):
233 os.makedirs(cfgdir)
233 os.makedirs(cfgdir)
234
234
235 nt.assert_equal(path.get_xdg_dir(), None)
235 nt.assert_equal(path.get_xdg_dir(), None)
236
236
237 def test_filefind():
237 def test_filefind():
238 """Various tests for filefind"""
238 """Various tests for filefind"""
239 f = tempfile.NamedTemporaryFile()
239 f = tempfile.NamedTemporaryFile()
240 # print 'fname:',f.name
240 # print 'fname:',f.name
241 alt_dirs = paths.get_ipython_dir()
241 alt_dirs = paths.get_ipython_dir()
242 t = path.filefind(f.name, alt_dirs)
242 t = path.filefind(f.name, alt_dirs)
243 # print 'found:',t
243 # print 'found:',t
244
244
245
245
246 @dec.skip_if_not_win32
246 @dec.skip_if_not_win32
247 def test_get_long_path_name_win32():
247 def test_get_long_path_name_win32():
248 with TemporaryDirectory() as tmpdir:
248 with TemporaryDirectory() as tmpdir:
249
249
250 # Make a long path. Expands the path of tmpdir prematurely as it may already have a long
250 # Make a long path. Expands the path of tmpdir prematurely as it may already have a long
251 # path component, so ensure we include the long form of it
251 # path component, so ensure we include the long form of it
252 long_path = os.path.join(path.get_long_path_name(tmpdir), 'this is my long path name')
252 long_path = os.path.join(path.get_long_path_name(tmpdir), 'this is my long path name')
253 os.makedirs(long_path)
253 os.makedirs(long_path)
254
254
255 # Test to see if the short path evaluates correctly.
255 # Test to see if the short path evaluates correctly.
256 short_path = os.path.join(tmpdir, 'THISIS~1')
256 short_path = os.path.join(tmpdir, 'THISIS~1')
257 evaluated_path = path.get_long_path_name(short_path)
257 evaluated_path = path.get_long_path_name(short_path)
258 nt.assert_equal(evaluated_path.lower(), long_path.lower())
258 nt.assert_equal(evaluated_path.lower(), long_path.lower())
259
259
260
260
261 @dec.skip_win32
261 @dec.skip_win32
262 def test_get_long_path_name():
262 def test_get_long_path_name():
263 p = path.get_long_path_name('/usr/local')
263 p = path.get_long_path_name('/usr/local')
264 nt.assert_equal(p,'/usr/local')
264 nt.assert_equal(p,'/usr/local')
265
265
266 @dec.skip_win32 # can't create not-user-writable dir on win
266 @dec.skip_win32 # can't create not-user-writable dir on win
267 @with_environment
267 @with_environment
268 def test_not_writable_ipdir():
268 def test_not_writable_ipdir():
269 tmpdir = tempfile.mkdtemp()
269 tmpdir = tempfile.mkdtemp()
270 os.name = "posix"
270 os.name = "posix"
271 env.pop('IPYTHON_DIR', None)
271 env.pop('IPYTHON_DIR', None)
272 env.pop('IPYTHONDIR', None)
272 env.pop('IPYTHONDIR', None)
273 env.pop('XDG_CONFIG_HOME', None)
273 env.pop('XDG_CONFIG_HOME', None)
274 env['HOME'] = tmpdir
274 env['HOME'] = tmpdir
275 ipdir = os.path.join(tmpdir, '.ipython')
275 ipdir = os.path.join(tmpdir, '.ipython')
276 os.mkdir(ipdir, 0o555)
276 os.mkdir(ipdir, 0o555)
277 try:
277 try:
278 open(os.path.join(ipdir, "_foo_"), 'w').close()
278 open(os.path.join(ipdir, "_foo_"), 'w').close()
279 except IOError:
279 except IOError:
280 pass
280 pass
281 else:
281 else:
282 # I can still write to an unwritable dir,
282 # I can still write to an unwritable dir,
283 # assume I'm root and skip the test
283 # assume I'm root and skip the test
284 raise SkipTest("I can't create directories that I can't write to")
284 raise SkipTest("I can't create directories that I can't write to")
285 with AssertPrints('is not a writable location', channel='stderr'):
285 with AssertPrints('is not a writable location', channel='stderr'):
286 ipdir = paths.get_ipython_dir()
286 ipdir = paths.get_ipython_dir()
287 env.pop('IPYTHON_DIR', None)
287 env.pop('IPYTHON_DIR', None)
288
288
289 @with_environment
289 @with_environment
290 def test_get_py_filename():
290 def test_get_py_filename():
291 os.chdir(TMP_TEST_DIR)
291 os.chdir(TMP_TEST_DIR)
292 with make_tempfile('foo.py'):
292 with make_tempfile('foo.py'):
293 nt.assert_equal(path.get_py_filename('foo.py'), 'foo.py')
293 nt.assert_equal(path.get_py_filename('foo.py'), 'foo.py')
294 nt.assert_equal(path.get_py_filename('foo'), 'foo.py')
294 nt.assert_equal(path.get_py_filename('foo'), 'foo.py')
295 with make_tempfile('foo'):
295 with make_tempfile('foo'):
296 nt.assert_equal(path.get_py_filename('foo'), 'foo')
296 nt.assert_equal(path.get_py_filename('foo'), 'foo')
297 nt.assert_raises(IOError, path.get_py_filename, 'foo.py')
297 nt.assert_raises(IOError, path.get_py_filename, 'foo.py')
298 nt.assert_raises(IOError, path.get_py_filename, 'foo')
298 nt.assert_raises(IOError, path.get_py_filename, 'foo')
299 nt.assert_raises(IOError, path.get_py_filename, 'foo.py')
299 nt.assert_raises(IOError, path.get_py_filename, 'foo.py')
300 true_fn = 'foo with spaces.py'
300 true_fn = 'foo with spaces.py'
301 with make_tempfile(true_fn):
301 with make_tempfile(true_fn):
302 nt.assert_equal(path.get_py_filename('foo with spaces'), true_fn)
302 nt.assert_equal(path.get_py_filename('foo with spaces'), true_fn)
303 nt.assert_equal(path.get_py_filename('foo with spaces.py'), true_fn)
303 nt.assert_equal(path.get_py_filename('foo with spaces.py'), true_fn)
304 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"')
304 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"')
305 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'")
305 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'")
306
306
307 @onlyif_unicode_paths
307 @onlyif_unicode_paths
308 def test_unicode_in_filename():
308 def test_unicode_in_filename():
309 """When a file doesn't exist, the exception raised should be safe to call
309 """When a file doesn't exist, the exception raised should be safe to call
310 str() on - i.e. in Python 2 it must only have ASCII characters.
310 str() on - i.e. in Python 2 it must only have ASCII characters.
311
311
312 https://github.com/ipython/ipython/issues/875
312 https://github.com/ipython/ipython/issues/875
313 """
313 """
314 try:
314 try:
315 # these calls should not throw unicode encode exceptions
315 # these calls should not throw unicode encode exceptions
316 path.get_py_filename('fooéè.py', force_win32=False)
316 path.get_py_filename('fooéè.py', force_win32=False)
317 except IOError as ex:
317 except IOError as ex:
318 str(ex)
318 str(ex)
319
319
320
320
321 class TestShellGlob(unittest.TestCase):
321 class TestShellGlob(unittest.TestCase):
322
322
323 @classmethod
323 @classmethod
324 def setUpClass(cls):
324 def setUpClass(cls):
325 cls.filenames_start_with_a = ['a0', 'a1', 'a2']
325 cls.filenames_start_with_a = ['a0', 'a1', 'a2']
326 cls.filenames_end_with_b = ['0b', '1b', '2b']
326 cls.filenames_end_with_b = ['0b', '1b', '2b']
327 cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b
327 cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b
328 cls.tempdir = TemporaryDirectory()
328 cls.tempdir = TemporaryDirectory()
329 td = cls.tempdir.name
329 td = cls.tempdir.name
330
330
331 with cls.in_tempdir():
331 with cls.in_tempdir():
332 # Create empty files
332 # Create empty files
333 for fname in cls.filenames:
333 for fname in cls.filenames:
334 open(os.path.join(td, fname), 'w').close()
334 open(os.path.join(td, fname), 'w').close()
335
335
336 @classmethod
336 @classmethod
337 def tearDownClass(cls):
337 def tearDownClass(cls):
338 cls.tempdir.cleanup()
338 cls.tempdir.cleanup()
339
339
340 @classmethod
340 @classmethod
341 @contextmanager
341 @contextmanager
342 def in_tempdir(cls):
342 def in_tempdir(cls):
343 save = os.getcwd()
343 save = os.getcwd()
344 try:
344 try:
345 os.chdir(cls.tempdir.name)
345 os.chdir(cls.tempdir.name)
346 yield
346 yield
347 finally:
347 finally:
348 os.chdir(save)
348 os.chdir(save)
349
349
350 def check_match(self, patterns, matches):
350 def check_match(self, patterns, matches):
351 with self.in_tempdir():
351 with self.in_tempdir():
352 # glob returns unordered list. that's why sorted is required.
352 # glob returns unordered list. that's why sorted is required.
353 nt.assert_equal(sorted(path.shellglob(patterns)),
353 nt.assert_equal(sorted(path.shellglob(patterns)),
354 sorted(matches))
354 sorted(matches))
355
355
356 def common_cases(self):
356 def common_cases(self):
357 return [
357 return [
358 (['*'], self.filenames),
358 (['*'], self.filenames),
359 (['a*'], self.filenames_start_with_a),
359 (['a*'], self.filenames_start_with_a),
360 (['*c'], ['*c']),
360 (['*c'], ['*c']),
361 (['*', 'a*', '*b', '*c'], self.filenames
361 (['*', 'a*', '*b', '*c'], self.filenames
362 + self.filenames_start_with_a
362 + self.filenames_start_with_a
363 + self.filenames_end_with_b
363 + self.filenames_end_with_b
364 + ['*c']),
364 + ['*c']),
365 (['a[012]'], self.filenames_start_with_a),
365 (['a[012]'], self.filenames_start_with_a),
366 ]
366 ]
367
367
368 @skip_win32
368 @skip_win32
369 def test_match_posix(self):
369 def test_match_posix(self):
370 for (patterns, matches) in self.common_cases() + [
370 for (patterns, matches) in self.common_cases() + [
371 ([r'\*'], ['*']),
371 ([r'\*'], ['*']),
372 ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a),
372 ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a),
373 ([r'a\[012]'], ['a[012]']),
373 ([r'a\[012]'], ['a[012]']),
374 ]:
374 ]:
375 yield (self.check_match, patterns, matches)
375 yield (self.check_match, patterns, matches)
376
376
377 @skip_if_not_win32
377 @skip_if_not_win32
378 def test_match_windows(self):
378 def test_match_windows(self):
379 for (patterns, matches) in self.common_cases() + [
379 for (patterns, matches) in self.common_cases() + [
380 # In windows, backslash is interpreted as path
380 # In windows, backslash is interpreted as path
381 # separator. Therefore, you can't escape glob
381 # separator. Therefore, you can't escape glob
382 # using it.
382 # using it.
383 ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a),
383 ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a),
384 ([r'a\[012]'], [r'a\[012]']),
384 ([r'a\[012]'], [r'a\[012]']),
385 ]:
385 ]:
386 yield (self.check_match, patterns, matches)
386 yield (self.check_match, patterns, matches)
387
387
388
388
389 def test_unescape_glob():
389 def test_unescape_glob():
390 nt.assert_equal(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?')
390 nt.assert_equal(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?')
391 nt.assert_equal(path.unescape_glob(r'\\*'), r'\*')
391 nt.assert_equal(path.unescape_glob(r'\\*'), r'\*')
392 nt.assert_equal(path.unescape_glob(r'\\\*'), r'\*')
392 nt.assert_equal(path.unescape_glob(r'\\\*'), r'\*')
393 nt.assert_equal(path.unescape_glob(r'\\a'), r'\a')
393 nt.assert_equal(path.unescape_glob(r'\\a'), r'\a')
394 nt.assert_equal(path.unescape_glob(r'\a'), r'\a')
394 nt.assert_equal(path.unescape_glob(r'\a'), r'\a')
395
395
396
396
397 @onlyif_unicode_paths
397 @onlyif_unicode_paths
398 def test_ensure_dir_exists():
398 def test_ensure_dir_exists():
399 with TemporaryDirectory() as td:
399 with TemporaryDirectory() as td:
400 d = os.path.join(td, '∂ir')
400 d = os.path.join(td, '∂ir')
401 path.ensure_dir_exists(d) # create it
401 path.ensure_dir_exists(d) # create it
402 assert os.path.isdir(d)
402 assert os.path.isdir(d)
403 path.ensure_dir_exists(d) # no-op
403 path.ensure_dir_exists(d) # no-op
404 f = os.path.join(td, 'ƒile')
404 f = os.path.join(td, 'ƒile')
405 open(f, 'w').close() # touch
405 open(f, 'w').close() # touch
406 with nt.assert_raises(IOError):
406 with nt.assert_raises(IOError):
407 path.ensure_dir_exists(f)
407 path.ensure_dir_exists(f)
408
408
409 class TestLinkOrCopy(object):
409 class TestLinkOrCopy(object):
410 def setUp(self):
410 def setUp(self):
411 self.tempdir = TemporaryDirectory()
411 self.tempdir = TemporaryDirectory()
412 self.src = self.dst("src")
412 self.src = self.dst("src")
413 with open(self.src, "w") as f:
413 with open(self.src, "w") as f:
414 f.write("Hello, world!")
414 f.write("Hello, world!")
415
415
416 def tearDown(self):
416 def tearDown(self):
417 self.tempdir.cleanup()
417 self.tempdir.cleanup()
418
418
419 def dst(self, *args):
419 def dst(self, *args):
420 return os.path.join(self.tempdir.name, *args)
420 return os.path.join(self.tempdir.name, *args)
421
421
422 def assert_inode_not_equal(self, a, b):
422 def assert_inode_not_equal(self, a, b):
423 nt.assert_not_equal(os.stat(a).st_ino, os.stat(b).st_ino,
423 nt.assert_not_equal(os.stat(a).st_ino, os.stat(b).st_ino,
424 "%r and %r do reference the same indoes" %(a, b))
424 "%r and %r do reference the same indoes" %(a, b))
425
425
426 def assert_inode_equal(self, a, b):
426 def assert_inode_equal(self, a, b):
427 nt.assert_equal(os.stat(a).st_ino, os.stat(b).st_ino,
427 nt.assert_equal(os.stat(a).st_ino, os.stat(b).st_ino,
428 "%r and %r do not reference the same indoes" %(a, b))
428 "%r and %r do not reference the same indoes" %(a, b))
429
429
430 def assert_content_equal(self, a, b):
430 def assert_content_equal(self, a, b):
431 with open(a) as a_f:
431 with open(a) as a_f:
432 with open(b) as b_f:
432 with open(b) as b_f:
433 nt.assert_equal(a_f.read(), b_f.read())
433 nt.assert_equal(a_f.read(), b_f.read())
434
434
435 @skip_win32
435 @skip_win32
436 def test_link_successful(self):
436 def test_link_successful(self):
437 dst = self.dst("target")
437 dst = self.dst("target")
438 path.link_or_copy(self.src, dst)
438 path.link_or_copy(self.src, dst)
439 self.assert_inode_equal(self.src, dst)
439 self.assert_inode_equal(self.src, dst)
440
440
441 @skip_win32
441 @skip_win32
442 def test_link_into_dir(self):
442 def test_link_into_dir(self):
443 dst = self.dst("some_dir")
443 dst = self.dst("some_dir")
444 os.mkdir(dst)
444 os.mkdir(dst)
445 path.link_or_copy(self.src, dst)
445 path.link_or_copy(self.src, dst)
446 expected_dst = self.dst("some_dir", os.path.basename(self.src))
446 expected_dst = self.dst("some_dir", os.path.basename(self.src))
447 self.assert_inode_equal(self.src, expected_dst)
447 self.assert_inode_equal(self.src, expected_dst)
448
448
449 @skip_win32
449 @skip_win32
450 def test_target_exists(self):
450 def test_target_exists(self):
451 dst = self.dst("target")
451 dst = self.dst("target")
452 open(dst, "w").close()
452 open(dst, "w").close()
453 path.link_or_copy(self.src, dst)
453 path.link_or_copy(self.src, dst)
454 self.assert_inode_equal(self.src, dst)
454 self.assert_inode_equal(self.src, dst)
455
455
456 @skip_win32
456 @skip_win32
457 def test_no_link(self):
457 def test_no_link(self):
458 real_link = os.link
458 real_link = os.link
459 try:
459 try:
460 del os.link
460 del os.link
461 dst = self.dst("target")
461 dst = self.dst("target")
462 path.link_or_copy(self.src, dst)
462 path.link_or_copy(self.src, dst)
463 self.assert_content_equal(self.src, dst)
463 self.assert_content_equal(self.src, dst)
464 self.assert_inode_not_equal(self.src, dst)
464 self.assert_inode_not_equal(self.src, dst)
465 finally:
465 finally:
466 os.link = real_link
466 os.link = real_link
467
467
468 @skip_if_not_win32
468 @skip_if_not_win32
469 def test_windows(self):
469 def test_windows(self):
470 dst = self.dst("target")
470 dst = self.dst("target")
471 path.link_or_copy(self.src, dst)
471 path.link_or_copy(self.src, dst)
472 self.assert_content_equal(self.src, dst)
472 self.assert_content_equal(self.src, dst)
473
473
474 def test_link_twice(self):
474 def test_link_twice(self):
475 # Linking the same file twice shouldn't leave duplicates around.
475 # Linking the same file twice shouldn't leave duplicates around.
476 # See https://github.com/ipython/ipython/issues/6450
476 # See https://github.com/ipython/ipython/issues/6450
477 dst = self.dst('target')
477 dst = self.dst('target')
478 path.link_or_copy(self.src, dst)
478 path.link_or_copy(self.src, dst)
479 path.link_or_copy(self.src, dst)
479 path.link_or_copy(self.src, dst)
480 self.assert_inode_equal(self.src, dst)
480 self.assert_inode_equal(self.src, dst)
481 nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target'])
481 nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target'])
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now