Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,2045 +1,2045 b'' | |||
|
1 | 1 | """Completion for IPython. |
|
2 | 2 | |
|
3 | 3 | This module started as fork of the rlcompleter module in the Python standard |
|
4 | 4 | library. The original enhancements made to rlcompleter have been sent |
|
5 | 5 | upstream and were accepted as of Python 2.3, |
|
6 | 6 | |
|
7 | 7 | This module now support a wide variety of completion mechanism both available |
|
8 | 8 | for normal classic Python code, as well as completer for IPython specific |
|
9 | 9 | Syntax like magics. |
|
10 | 10 | |
|
11 | 11 | Latex and Unicode completion |
|
12 | 12 | ============================ |
|
13 | 13 | |
|
14 | 14 | IPython and compatible frontends not only can complete your code, but can help |
|
15 | 15 | you to input a wide range of characters. In particular we allow you to insert |
|
16 | 16 | a unicode character using the tab completion mechanism. |
|
17 | 17 | |
|
18 | 18 | Forward latex/unicode completion |
|
19 | 19 | -------------------------------- |
|
20 | 20 | |
|
21 | 21 | Forward completion allows you to easily type a unicode character using its latex |
|
22 | 22 | name, or unicode long description. To do so type a backslash follow by the |
|
23 | 23 | relevant name and press tab: |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | Using latex completion: |
|
27 | 27 | |
|
28 | 28 | .. code:: |
|
29 | 29 | |
|
30 | 30 | \\alpha<tab> |
|
31 | 31 | α |
|
32 | 32 | |
|
33 | 33 | or using unicode completion: |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | .. code:: |
|
37 | 37 | |
|
38 | 38 | \\greek small letter alpha<tab> |
|
39 | 39 | α |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | Only valid Python identifiers will complete. Combining characters (like arrow or |
|
43 | 43 | dots) are also available, unlike latex they need to be put after the their |
|
44 | 44 | counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`. |
|
45 | 45 | |
|
46 | 46 | Some browsers are known to display combining characters incorrectly. |
|
47 | 47 | |
|
48 | 48 | Backward latex completion |
|
49 | 49 | ------------------------- |
|
50 | 50 | |
|
51 | 51 | It is sometime challenging to know how to type a character, if you are using |
|
52 | 52 | IPython, or any compatible frontend you can prepend backslash to the character |
|
53 | 53 | and press `<tab>` to expand it to its latex form. |
|
54 | 54 | |
|
55 | 55 | .. code:: |
|
56 | 56 | |
|
57 | 57 | \\α<tab> |
|
58 | 58 | \\alpha |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | Both forward and backward completions can be deactivated by setting the |
|
62 | 62 | ``Completer.backslash_combining_completions`` option to ``False``. |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | Experimental |
|
66 | 66 | ============ |
|
67 | 67 | |
|
68 | 68 | Starting with IPython 6.0, this module can make use of the Jedi library to |
|
69 | 69 | generate completions both using static analysis of the code, and dynamically |
|
70 | 70 | inspecting multiple namespaces. The APIs attached to this new mechanism is |
|
71 | 71 | unstable and will raise unless use in an :any:`provisionalcompleter` context |
|
72 | 72 | manager. |
|
73 | 73 | |
|
74 | 74 | You will find that the following are experimental: |
|
75 | 75 | |
|
76 | 76 | - :any:`provisionalcompleter` |
|
77 | 77 | - :any:`IPCompleter.completions` |
|
78 | 78 | - :any:`Completion` |
|
79 | 79 | - :any:`rectify_completions` |
|
80 | 80 | |
|
81 | 81 | .. note:: |
|
82 | 82 | |
|
83 | 83 | better name for :any:`rectify_completions` ? |
|
84 | 84 | |
|
85 | 85 | We welcome any feedback on these new API, and we also encourage you to try this |
|
86 | 86 | module in debug mode (start IPython with ``--Completer.debug=True``) in order |
|
87 | 87 | to have extra logging information is :any:`jedi` is crashing, or if current |
|
88 | 88 | IPython completer pending deprecations are returning results not yet handled |
|
89 | 89 | by :any:`jedi` |
|
90 | 90 | |
|
91 | 91 | Using Jedi for tab completion allow snippets like the following to work without |
|
92 | 92 | having to execute any code: |
|
93 | 93 | |
|
94 | 94 | >>> myvar = ['hello', 42] |
|
95 | 95 | ... myvar[1].bi<tab> |
|
96 | 96 | |
|
97 | 97 | Tab completion will be able to infer that ``myvar[1]`` is a real number without |
|
98 | 98 | executing any code unlike the previously available ``IPCompleter.greedy`` |
|
99 | 99 | option. |
|
100 | 100 | |
|
101 | 101 | Be sure to update :any:`jedi` to the latest stable version or to try the |
|
102 | 102 | current development version to get better completions. |
|
103 | 103 | """ |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | # Copyright (c) IPython Development Team. |
|
107 | 107 | # Distributed under the terms of the Modified BSD License. |
|
108 | 108 | # |
|
109 | 109 | # Some of this code originated from rlcompleter in the Python standard library |
|
110 | 110 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | import __main__ |
|
114 | 114 | import builtins as builtin_mod |
|
115 | 115 | import glob |
|
116 | 116 | import time |
|
117 | 117 | import inspect |
|
118 | 118 | import itertools |
|
119 | 119 | import keyword |
|
120 | 120 | import os |
|
121 | 121 | import re |
|
122 | 122 | import sys |
|
123 | 123 | import unicodedata |
|
124 | 124 | import string |
|
125 | 125 | import warnings |
|
126 | 126 | |
|
127 | 127 | from contextlib import contextmanager |
|
128 | 128 | from importlib import import_module |
|
129 | 129 | from typing import Iterator, List, Tuple, Iterable, Union |
|
130 | 130 | from types import SimpleNamespace |
|
131 | 131 | |
|
132 | 132 | from traitlets.config.configurable import Configurable |
|
133 | 133 | from IPython.core.error import TryNext |
|
134 | 134 | from IPython.core.inputsplitter import ESC_MAGIC |
|
135 | 135 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol |
|
136 | 136 | from IPython.core.oinspect import InspectColors |
|
137 | 137 | from IPython.utils import generics |
|
138 | 138 | from IPython.utils.dir2 import dir2, get_real_method |
|
139 | 139 | from IPython.utils.process import arg_split |
|
140 | 140 | from traitlets import Bool, Enum, observe, Int |
|
141 | 141 | |
|
142 | 142 | # skip module docstests |
|
143 | 143 | skip_doctest = True |
|
144 | 144 | |
|
145 | 145 | try: |
|
146 | 146 | import jedi |
|
147 | 147 | import jedi.api.helpers |
|
148 | 148 | import jedi.api.classes |
|
149 | 149 | JEDI_INSTALLED = True |
|
150 | 150 | except ImportError: |
|
151 | 151 | JEDI_INSTALLED = False |
|
152 | 152 | #----------------------------------------------------------------------------- |
|
153 | 153 | # Globals |
|
154 | 154 | #----------------------------------------------------------------------------- |
|
155 | 155 | |
|
156 | 156 | # Public API |
|
157 | 157 | __all__ = ['Completer','IPCompleter'] |
|
158 | 158 | |
|
159 | 159 | if sys.platform == 'win32': |
|
160 | 160 | PROTECTABLES = ' ' |
|
161 | 161 | else: |
|
162 | 162 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
163 | 163 | |
|
164 | 164 | # Protect against returning an enormous number of completions which the frontend |
|
165 | 165 | # may have trouble processing. |
|
166 | 166 | MATCHES_LIMIT = 500 |
|
167 | 167 | |
|
168 | 168 | _deprecation_readline_sentinel = object() |
|
169 | 169 | |
|
170 | 170 | |
|
171 | 171 | class ProvisionalCompleterWarning(FutureWarning): |
|
172 | 172 | """ |
|
173 | 173 | Exception raise by an experimental feature in this module. |
|
174 | 174 | |
|
175 | 175 | Wrap code in :any:`provisionalcompleter` context manager if you |
|
176 | 176 | are certain you want to use an unstable feature. |
|
177 | 177 | """ |
|
178 | 178 | pass |
|
179 | 179 | |
|
180 | 180 | warnings.filterwarnings('error', category=ProvisionalCompleterWarning) |
|
181 | 181 | |
|
182 | 182 | @contextmanager |
|
183 | 183 | def provisionalcompleter(action='ignore'): |
|
184 | 184 | """ |
|
185 | 185 | |
|
186 | 186 | |
|
187 | 187 | This contest manager has to be used in any place where unstable completer |
|
188 | 188 | behavior and API may be called. |
|
189 | 189 | |
|
190 | 190 | >>> with provisionalcompleter(): |
|
191 | 191 | ... completer.do_experimetal_things() # works |
|
192 | 192 | |
|
193 | 193 | >>> completer.do_experimental_things() # raises. |
|
194 | 194 | |
|
195 | 195 | .. note:: Unstable |
|
196 | 196 | |
|
197 | 197 | By using this context manager you agree that the API in use may change |
|
198 | 198 | without warning, and that you won't complain if they do so. |
|
199 | 199 | |
|
200 | 200 | You also understand that if the API is not to you liking you should report |
|
201 | 201 | a bug to explain your use case upstream and improve the API and will loose |
|
202 | 202 | credibility if you complain after the API is make stable. |
|
203 | 203 | |
|
204 | 204 | We'll be happy to get your feedback , feature request and improvement on |
|
205 | 205 | any of the unstable APIs ! |
|
206 | 206 | """ |
|
207 | 207 | with warnings.catch_warnings(): |
|
208 | 208 | warnings.filterwarnings(action, category=ProvisionalCompleterWarning) |
|
209 | 209 | yield |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def has_open_quotes(s): |
|
213 | 213 | """Return whether a string has open quotes. |
|
214 | 214 | |
|
215 | 215 | This simply counts whether the number of quote characters of either type in |
|
216 | 216 | the string is odd. |
|
217 | 217 | |
|
218 | 218 | Returns |
|
219 | 219 | ------- |
|
220 | 220 | If there is an open quote, the quote character is returned. Else, return |
|
221 | 221 | False. |
|
222 | 222 | """ |
|
223 | 223 | # We check " first, then ', so complex cases with nested quotes will get |
|
224 | 224 | # the " to take precedence. |
|
225 | 225 | if s.count('"') % 2: |
|
226 | 226 | return '"' |
|
227 | 227 | elif s.count("'") % 2: |
|
228 | 228 | return "'" |
|
229 | 229 | else: |
|
230 | 230 | return False |
|
231 | 231 | |
|
232 | 232 | |
|
233 | 233 | def protect_filename(s, protectables=PROTECTABLES): |
|
234 | 234 | """Escape a string to protect certain characters.""" |
|
235 | 235 | if set(s) & set(protectables): |
|
236 | 236 | if sys.platform == "win32": |
|
237 | 237 | return '"' + s + '"' |
|
238 | 238 | else: |
|
239 | 239 | return "".join(("\\" + c if c in protectables else c) for c in s) |
|
240 | 240 | else: |
|
241 | 241 | return s |
|
242 | 242 | |
|
243 | 243 | |
|
244 | 244 | def expand_user(path:str) -> Tuple[str, bool, str]: |
|
245 | 245 | """Expand ``~``-style usernames in strings. |
|
246 | 246 | |
|
247 | 247 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
248 | 248 | extra information that will be useful if the input was being used in |
|
249 | 249 | computing completions, and you wish to return the completions with the |
|
250 | 250 | original '~' instead of its expanded value. |
|
251 | 251 | |
|
252 | 252 | Parameters |
|
253 | 253 | ---------- |
|
254 | 254 | path : str |
|
255 | 255 | String to be expanded. If no ~ is present, the output is the same as the |
|
256 | 256 | input. |
|
257 | 257 | |
|
258 | 258 | Returns |
|
259 | 259 | ------- |
|
260 | 260 | newpath : str |
|
261 | 261 | Result of ~ expansion in the input path. |
|
262 | 262 | tilde_expand : bool |
|
263 | 263 | Whether any expansion was performed or not. |
|
264 | 264 | tilde_val : str |
|
265 | 265 | The value that ~ was replaced with. |
|
266 | 266 | """ |
|
267 | 267 | # Default values |
|
268 | 268 | tilde_expand = False |
|
269 | 269 | tilde_val = '' |
|
270 | 270 | newpath = path |
|
271 | 271 | |
|
272 | 272 | if path.startswith('~'): |
|
273 | 273 | tilde_expand = True |
|
274 | 274 | rest = len(path)-1 |
|
275 | 275 | newpath = os.path.expanduser(path) |
|
276 | 276 | if rest: |
|
277 | 277 | tilde_val = newpath[:-rest] |
|
278 | 278 | else: |
|
279 | 279 | tilde_val = newpath |
|
280 | 280 | |
|
281 | 281 | return newpath, tilde_expand, tilde_val |
|
282 | 282 | |
|
283 | 283 | |
|
284 | 284 | def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str: |
|
285 | 285 | """Does the opposite of expand_user, with its outputs. |
|
286 | 286 | """ |
|
287 | 287 | if tilde_expand: |
|
288 | 288 | return path.replace(tilde_val, '~') |
|
289 | 289 | else: |
|
290 | 290 | return path |
|
291 | 291 | |
|
292 | 292 | |
|
293 | 293 | def completions_sorting_key(word): |
|
294 | 294 | """key for sorting completions |
|
295 | 295 | |
|
296 | 296 | This does several things: |
|
297 | 297 | |
|
298 | 298 | - Demote any completions starting with underscores to the end |
|
299 | 299 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
300 | 300 | by their name |
|
301 | 301 | """ |
|
302 | 302 | prio1, prio2 = 0, 0 |
|
303 | 303 | |
|
304 | 304 | if word.startswith('__'): |
|
305 | 305 | prio1 = 2 |
|
306 | 306 | elif word.startswith('_'): |
|
307 | 307 | prio1 = 1 |
|
308 | 308 | |
|
309 | 309 | if word.endswith('='): |
|
310 | 310 | prio1 = -1 |
|
311 | 311 | |
|
312 | 312 | if word.startswith('%%'): |
|
313 | 313 | # If there's another % in there, this is something else, so leave it alone |
|
314 | 314 | if not "%" in word[2:]: |
|
315 | 315 | word = word[2:] |
|
316 | 316 | prio2 = 2 |
|
317 | 317 | elif word.startswith('%'): |
|
318 | 318 | if not "%" in word[1:]: |
|
319 | 319 | word = word[1:] |
|
320 | 320 | prio2 = 1 |
|
321 | 321 | |
|
322 | 322 | return prio1, word, prio2 |
|
323 | 323 | |
|
324 | 324 | |
|
325 | 325 | class _FakeJediCompletion: |
|
326 | 326 | """ |
|
327 | 327 | This is a workaround to communicate to the UI that Jedi has crashed and to |
|
328 | 328 | report a bug. Will be used only id :any:`IPCompleter.debug` is set to true. |
|
329 | 329 | |
|
330 | 330 | Added in IPython 6.0 so should likely be removed for 7.0 |
|
331 | 331 | |
|
332 | 332 | """ |
|
333 | 333 | |
|
334 | 334 | def __init__(self, name): |
|
335 | 335 | |
|
336 | 336 | self.name = name |
|
337 | 337 | self.complete = name |
|
338 | 338 | self.type = 'crashed' |
|
339 | 339 | self.name_with_symbols = name |
|
340 | 340 | self.signature = '' |
|
341 | 341 | self._origin = 'fake' |
|
342 | 342 | |
|
343 | 343 | def __repr__(self): |
|
344 | 344 | return '<Fake completion object jedi has crashed>' |
|
345 | 345 | |
|
346 | 346 | |
|
347 | 347 | class Completion: |
|
348 | 348 | """ |
|
349 | 349 | Completion object used and return by IPython completers. |
|
350 | 350 | |
|
351 | 351 | .. warning:: Unstable |
|
352 | 352 | |
|
353 | 353 | This function is unstable, API may change without warning. |
|
354 | 354 | It will also raise unless use in proper context manager. |
|
355 | 355 | |
|
356 | 356 | This act as a middle ground :any:`Completion` object between the |
|
357 | 357 | :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion |
|
358 | 358 | object. While Jedi need a lot of information about evaluator and how the |
|
359 | 359 | code should be ran/inspected, PromptToolkit (and other frontend) mostly |
|
360 | 360 | need user facing information. |
|
361 | 361 | |
|
362 | 362 | - Which range should be replaced replaced by what. |
|
363 |
- Some metadata (like completion type), or meta information |
|
|
363 | - Some metadata (like completion type), or meta information to displayed to | |
|
364 | 364 | the use user. |
|
365 | 365 | |
|
366 | 366 | For debugging purpose we can also store the origin of the completion (``jedi``, |
|
367 | 367 | ``IPython.python_matches``, ``IPython.magics_matches``...). |
|
368 | 368 | """ |
|
369 | 369 | |
|
370 | 370 | __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin'] |
|
371 | 371 | |
|
372 | 372 | def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None: |
|
373 | 373 | warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). " |
|
374 | 374 | "It may change without warnings. " |
|
375 | 375 | "Use in corresponding context manager.", |
|
376 | 376 | category=ProvisionalCompleterWarning, stacklevel=2) |
|
377 | 377 | |
|
378 | 378 | self.start = start |
|
379 | 379 | self.end = end |
|
380 | 380 | self.text = text |
|
381 | 381 | self.type = type |
|
382 | 382 | self.signature = signature |
|
383 | 383 | self._origin = _origin |
|
384 | 384 | |
|
385 | 385 | def __repr__(self): |
|
386 | 386 | return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \ |
|
387 | 387 | (self.start, self.end, self.text, self.type or '?', self.signature or '?') |
|
388 | 388 | |
|
389 | 389 | def __eq__(self, other)->Bool: |
|
390 | 390 | """ |
|
391 | 391 | Equality and hash do not hash the type (as some completer may not be |
|
392 | 392 | able to infer the type), but are use to (partially) de-duplicate |
|
393 | 393 | completion. |
|
394 | 394 | |
|
395 | 395 | Completely de-duplicating completion is a bit tricker that just |
|
396 | 396 | comparing as it depends on surrounding text, which Completions are not |
|
397 | 397 | aware of. |
|
398 | 398 | """ |
|
399 | 399 | return self.start == other.start and \ |
|
400 | 400 | self.end == other.end and \ |
|
401 | 401 | self.text == other.text |
|
402 | 402 | |
|
403 | 403 | def __hash__(self): |
|
404 | 404 | return hash((self.start, self.end, self.text)) |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | _IC = Iterable[Completion] |
|
408 | 408 | |
|
409 | 409 | |
|
410 | 410 | def _deduplicate_completions(text: str, completions: _IC)-> _IC: |
|
411 | 411 | """ |
|
412 | 412 | Deduplicate a set of completions. |
|
413 | 413 | |
|
414 | 414 | .. warning:: Unstable |
|
415 | 415 | |
|
416 | 416 | This function is unstable, API may change without warning. |
|
417 | 417 | |
|
418 | 418 | Parameters |
|
419 | 419 | ---------- |
|
420 | 420 | text: str |
|
421 | 421 | text that should be completed. |
|
422 | 422 | completions: Iterator[Completion] |
|
423 | 423 | iterator over the completions to deduplicate |
|
424 | 424 | |
|
425 | 425 | Yields |
|
426 | 426 | ------ |
|
427 | 427 | `Completions` objects |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | Completions coming from multiple sources, may be different but end up having |
|
431 | 431 | the same effect when applied to ``text``. If this is the case, this will |
|
432 | 432 | consider completions as equal and only emit the first encountered. |
|
433 | 433 | |
|
434 | 434 | Not folded in `completions()` yet for debugging purpose, and to detect when |
|
435 | 435 | the IPython completer does return things that Jedi does not, but should be |
|
436 | 436 | at some point. |
|
437 | 437 | """ |
|
438 | 438 | completions = list(completions) |
|
439 | 439 | if not completions: |
|
440 | 440 | return |
|
441 | 441 | |
|
442 | 442 | new_start = min(c.start for c in completions) |
|
443 | 443 | new_end = max(c.end for c in completions) |
|
444 | 444 | |
|
445 | 445 | seen = set() |
|
446 | 446 | for c in completions: |
|
447 | 447 | new_text = text[new_start:c.start] + c.text + text[c.end:new_end] |
|
448 | 448 | if new_text not in seen: |
|
449 | 449 | yield c |
|
450 | 450 | seen.add(new_text) |
|
451 | 451 | |
|
452 | 452 | |
|
453 | 453 | def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC: |
|
454 | 454 | """ |
|
455 | 455 | Rectify a set of completions to all have the same ``start`` and ``end`` |
|
456 | 456 | |
|
457 | 457 | .. warning:: Unstable |
|
458 | 458 | |
|
459 | 459 | This function is unstable, API may change without warning. |
|
460 | 460 | It will also raise unless use in proper context manager. |
|
461 | 461 | |
|
462 | 462 | Parameters |
|
463 | 463 | ---------- |
|
464 | 464 | text: str |
|
465 | 465 | text that should be completed. |
|
466 | 466 | completions: Iterator[Completion] |
|
467 | 467 | iterator over the completions to rectify |
|
468 | 468 | |
|
469 | 469 | |
|
470 | 470 | :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though |
|
471 | 471 | the Jupyter Protocol requires them to behave like so. This will readjust |
|
472 | 472 | the completion to have the same ``start`` and ``end`` by padding both |
|
473 | 473 | extremities with surrounding text. |
|
474 | 474 | |
|
475 | 475 | During stabilisation should support a ``_debug`` option to log which |
|
476 | 476 | completion are return by the IPython completer and not found in Jedi in |
|
477 | 477 | order to make upstream bug report. |
|
478 | 478 | """ |
|
479 | 479 | warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). " |
|
480 | 480 | "It may change without warnings. " |
|
481 | 481 | "Use in corresponding context manager.", |
|
482 | 482 | category=ProvisionalCompleterWarning, stacklevel=2) |
|
483 | 483 | |
|
484 | 484 | completions = list(completions) |
|
485 | 485 | if not completions: |
|
486 | 486 | return |
|
487 | 487 | starts = (c.start for c in completions) |
|
488 | 488 | ends = (c.end for c in completions) |
|
489 | 489 | |
|
490 | 490 | new_start = min(starts) |
|
491 | 491 | new_end = max(ends) |
|
492 | 492 | |
|
493 | 493 | seen_jedi = set() |
|
494 | 494 | seen_python_matches = set() |
|
495 | 495 | for c in completions: |
|
496 | 496 | new_text = text[new_start:c.start] + c.text + text[c.end:new_end] |
|
497 | 497 | if c._origin == 'jedi': |
|
498 | 498 | seen_jedi.add(new_text) |
|
499 | 499 | elif c._origin == 'IPCompleter.python_matches': |
|
500 | 500 | seen_python_matches.add(new_text) |
|
501 | 501 | yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature) |
|
502 | 502 | diff = seen_python_matches.difference(seen_jedi) |
|
503 | 503 | if diff and _debug: |
|
504 | 504 | print('IPython.python matches have extras:', diff) |
|
505 | 505 | |
|
506 | 506 | |
|
507 | 507 | if sys.platform == 'win32': |
|
508 | 508 | DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?' |
|
509 | 509 | else: |
|
510 | 510 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
511 | 511 | |
|
512 | 512 | GREEDY_DELIMS = ' =\r\n' |
|
513 | 513 | |
|
514 | 514 | |
|
515 | 515 | class CompletionSplitter(object): |
|
516 | 516 | """An object to split an input line in a manner similar to readline. |
|
517 | 517 | |
|
518 | 518 | By having our own implementation, we can expose readline-like completion in |
|
519 | 519 | a uniform manner to all frontends. This object only needs to be given the |
|
520 | 520 | line of text to be split and the cursor position on said line, and it |
|
521 | 521 | returns the 'word' to be completed on at the cursor after splitting the |
|
522 | 522 | entire line. |
|
523 | 523 | |
|
524 | 524 | What characters are used as splitting delimiters can be controlled by |
|
525 | 525 | setting the ``delims`` attribute (this is a property that internally |
|
526 | 526 | automatically builds the necessary regular expression)""" |
|
527 | 527 | |
|
528 | 528 | # Private interface |
|
529 | 529 | |
|
530 | 530 | # A string of delimiter characters. The default value makes sense for |
|
531 | 531 | # IPython's most typical usage patterns. |
|
532 | 532 | _delims = DELIMS |
|
533 | 533 | |
|
534 | 534 | # The expression (a normal string) to be compiled into a regular expression |
|
535 | 535 | # for actual splitting. We store it as an attribute mostly for ease of |
|
536 | 536 | # debugging, since this type of code can be so tricky to debug. |
|
537 | 537 | _delim_expr = None |
|
538 | 538 | |
|
539 | 539 | # The regular expression that does the actual splitting |
|
540 | 540 | _delim_re = None |
|
541 | 541 | |
|
542 | 542 | def __init__(self, delims=None): |
|
543 | 543 | delims = CompletionSplitter._delims if delims is None else delims |
|
544 | 544 | self.delims = delims |
|
545 | 545 | |
|
546 | 546 | @property |
|
547 | 547 | def delims(self): |
|
548 | 548 | """Return the string of delimiter characters.""" |
|
549 | 549 | return self._delims |
|
550 | 550 | |
|
551 | 551 | @delims.setter |
|
552 | 552 | def delims(self, delims): |
|
553 | 553 | """Set the delimiters for line splitting.""" |
|
554 | 554 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
555 | 555 | self._delim_re = re.compile(expr) |
|
556 | 556 | self._delims = delims |
|
557 | 557 | self._delim_expr = expr |
|
558 | 558 | |
|
559 | 559 | def split_line(self, line, cursor_pos=None): |
|
560 | 560 | """Split a line of text with a cursor at the given position. |
|
561 | 561 | """ |
|
562 | 562 | l = line if cursor_pos is None else line[:cursor_pos] |
|
563 | 563 | return self._delim_re.split(l)[-1] |
|
564 | 564 | |
|
565 | 565 | |
|
566 | 566 | |
|
567 | 567 | class Completer(Configurable): |
|
568 | 568 | |
|
569 | 569 | greedy = Bool(False, |
|
570 | 570 | help="""Activate greedy completion |
|
571 | 571 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
572 | 572 | |
|
573 | 573 | This will enable completion on elements of lists, results of function calls, etc., |
|
574 | 574 | but can be unsafe because the code is actually evaluated on TAB. |
|
575 | 575 | """ |
|
576 | 576 | ).tag(config=True) |
|
577 | 577 | |
|
578 | 578 | use_jedi = Bool(default_value=JEDI_INSTALLED, |
|
579 | 579 | help="Experimental: Use Jedi to generate autocompletions. " |
|
580 | 580 | "Default to True if jedi is installed").tag(config=True) |
|
581 | 581 | |
|
582 | 582 | jedi_compute_type_timeout = Int(default_value=400, |
|
583 | 583 | help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types. |
|
584 | 584 | Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt |
|
585 | 585 | performance by preventing jedi to build its cache. |
|
586 | 586 | """).tag(config=True) |
|
587 | 587 | |
|
588 | 588 | debug = Bool(default_value=False, |
|
589 | 589 | help='Enable debug for the Completer. Mostly print extra ' |
|
590 | 590 | 'information for experimental jedi integration.')\ |
|
591 | 591 | .tag(config=True) |
|
592 | 592 | |
|
593 | 593 | backslash_combining_completions = Bool(True, |
|
594 | 594 | help="Enable unicode completions, e.g. \\alpha<tab> . " |
|
595 | 595 | "Includes completion of latex commands, unicode names, and expanding " |
|
596 | 596 | "unicode characters back to latex commands.").tag(config=True) |
|
597 | 597 | |
|
598 | 598 | |
|
599 | 599 | |
|
600 | 600 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
601 | 601 | """Create a new completer for the command line. |
|
602 | 602 | |
|
603 | 603 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
604 | 604 | |
|
605 | 605 | If unspecified, the default namespace where completions are performed |
|
606 | 606 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
607 | 607 | given as dictionaries. |
|
608 | 608 | |
|
609 | 609 | An optional second namespace can be given. This allows the completer |
|
610 | 610 | to handle cases where both the local and global scopes need to be |
|
611 | 611 | distinguished. |
|
612 | 612 | """ |
|
613 | 613 | |
|
614 | 614 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
615 | 615 | # specific namespace or to use __main__.__dict__. This will allow us |
|
616 | 616 | # to bind to __main__.__dict__ at completion time, not now. |
|
617 | 617 | if namespace is None: |
|
618 | 618 | self.use_main_ns = True |
|
619 | 619 | else: |
|
620 | 620 | self.use_main_ns = False |
|
621 | 621 | self.namespace = namespace |
|
622 | 622 | |
|
623 | 623 | # The global namespace, if given, can be bound directly |
|
624 | 624 | if global_namespace is None: |
|
625 | 625 | self.global_namespace = {} |
|
626 | 626 | else: |
|
627 | 627 | self.global_namespace = global_namespace |
|
628 | 628 | |
|
629 | 629 | super(Completer, self).__init__(**kwargs) |
|
630 | 630 | |
|
631 | 631 | def complete(self, text, state): |
|
632 | 632 | """Return the next possible completion for 'text'. |
|
633 | 633 | |
|
634 | 634 | This is called successively with state == 0, 1, 2, ... until it |
|
635 | 635 | returns None. The completion should begin with 'text'. |
|
636 | 636 | |
|
637 | 637 | """ |
|
638 | 638 | if self.use_main_ns: |
|
639 | 639 | self.namespace = __main__.__dict__ |
|
640 | 640 | |
|
641 | 641 | if state == 0: |
|
642 | 642 | if "." in text: |
|
643 | 643 | self.matches = self.attr_matches(text) |
|
644 | 644 | else: |
|
645 | 645 | self.matches = self.global_matches(text) |
|
646 | 646 | try: |
|
647 | 647 | return self.matches[state] |
|
648 | 648 | except IndexError: |
|
649 | 649 | return None |
|
650 | 650 | |
|
651 | 651 | def global_matches(self, text): |
|
652 | 652 | """Compute matches when text is a simple name. |
|
653 | 653 | |
|
654 | 654 | Return a list of all keywords, built-in functions and names currently |
|
655 | 655 | defined in self.namespace or self.global_namespace that match. |
|
656 | 656 | |
|
657 | 657 | """ |
|
658 | 658 | matches = [] |
|
659 | 659 | match_append = matches.append |
|
660 | 660 | n = len(text) |
|
661 | 661 | for lst in [keyword.kwlist, |
|
662 | 662 | builtin_mod.__dict__.keys(), |
|
663 | 663 | self.namespace.keys(), |
|
664 | 664 | self.global_namespace.keys()]: |
|
665 | 665 | for word in lst: |
|
666 | 666 | if word[:n] == text and word != "__builtins__": |
|
667 | 667 | match_append(word) |
|
668 | 668 | |
|
669 | 669 | snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z") |
|
670 | 670 | for lst in [self.namespace.keys(), |
|
671 | 671 | self.global_namespace.keys()]: |
|
672 | 672 | shortened = {"_".join([sub[0] for sub in word.split('_')]) : word |
|
673 | 673 | for word in lst if snake_case_re.match(word)} |
|
674 | 674 | for word in shortened.keys(): |
|
675 | 675 | if word[:n] == text and word != "__builtins__": |
|
676 | 676 | match_append(shortened[word]) |
|
677 | 677 | return matches |
|
678 | 678 | |
|
679 | 679 | def attr_matches(self, text): |
|
680 | 680 | """Compute matches when text contains a dot. |
|
681 | 681 | |
|
682 | 682 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
683 | 683 | evaluatable in self.namespace or self.global_namespace, it will be |
|
684 | 684 | evaluated and its attributes (as revealed by dir()) are used as |
|
685 | 685 | possible completions. (For class instances, class members are are |
|
686 | 686 | also considered.) |
|
687 | 687 | |
|
688 | 688 | WARNING: this can still invoke arbitrary C code, if an object |
|
689 | 689 | with a __getattr__ hook is evaluated. |
|
690 | 690 | |
|
691 | 691 | """ |
|
692 | 692 | |
|
693 | 693 | # Another option, seems to work great. Catches things like ''.<tab> |
|
694 | 694 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
695 | 695 | |
|
696 | 696 | if m: |
|
697 | 697 | expr, attr = m.group(1, 3) |
|
698 | 698 | elif self.greedy: |
|
699 | 699 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
700 | 700 | if not m2: |
|
701 | 701 | return [] |
|
702 | 702 | expr, attr = m2.group(1,2) |
|
703 | 703 | else: |
|
704 | 704 | return [] |
|
705 | 705 | |
|
706 | 706 | try: |
|
707 | 707 | obj = eval(expr, self.namespace) |
|
708 | 708 | except: |
|
709 | 709 | try: |
|
710 | 710 | obj = eval(expr, self.global_namespace) |
|
711 | 711 | except: |
|
712 | 712 | return [] |
|
713 | 713 | |
|
714 | 714 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
715 | 715 | words = get__all__entries(obj) |
|
716 | 716 | else: |
|
717 | 717 | words = dir2(obj) |
|
718 | 718 | |
|
719 | 719 | try: |
|
720 | 720 | words = generics.complete_object(obj, words) |
|
721 | 721 | except TryNext: |
|
722 | 722 | pass |
|
723 | 723 | except AssertionError: |
|
724 | 724 | raise |
|
725 | 725 | except Exception: |
|
726 | 726 | # Silence errors from completion function |
|
727 | 727 | #raise # dbg |
|
728 | 728 | pass |
|
729 | 729 | # Build match list to return |
|
730 | 730 | n = len(attr) |
|
731 | 731 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
732 | 732 | |
|
733 | 733 | |
|
734 | 734 | def get__all__entries(obj): |
|
735 | 735 | """returns the strings in the __all__ attribute""" |
|
736 | 736 | try: |
|
737 | 737 | words = getattr(obj, '__all__') |
|
738 | 738 | except: |
|
739 | 739 | return [] |
|
740 | 740 | |
|
741 | 741 | return [w for w in words if isinstance(w, str)] |
|
742 | 742 | |
|
743 | 743 | |
|
744 | 744 | def match_dict_keys(keys: List[str], prefix: str, delims: str): |
|
745 | 745 | """Used by dict_key_matches, matching the prefix to a list of keys |
|
746 | 746 | |
|
747 | 747 | Parameters |
|
748 | 748 | ========== |
|
749 | 749 | keys: |
|
750 | 750 | list of keys in dictionary currently being completed. |
|
751 | 751 | prefix: |
|
752 | 752 | Part of the text already typed by the user. e.g. `mydict[b'fo` |
|
753 | 753 | delims: |
|
754 | 754 | String of delimiters to consider when finding the current key. |
|
755 | 755 | |
|
756 | 756 | Returns |
|
757 | 757 | ======= |
|
758 | 758 | |
|
759 | 759 | A tuple of three elements: ``quote``, ``token_start``, ``matched``, with |
|
760 | 760 | ``quote`` being the quote that need to be used to close current string. |
|
761 | 761 | ``token_start`` the position where the replacement should start occurring, |
|
762 | 762 | ``matches`` a list of replacement/completion |
|
763 | 763 | |
|
764 | 764 | """ |
|
765 | 765 | if not prefix: |
|
766 | 766 | return None, 0, [repr(k) for k in keys |
|
767 | 767 | if isinstance(k, (str, bytes))] |
|
768 | 768 | quote_match = re.search('["\']', prefix) |
|
769 | 769 | quote = quote_match.group() |
|
770 | 770 | try: |
|
771 | 771 | prefix_str = eval(prefix + quote, {}) |
|
772 | 772 | except Exception: |
|
773 | 773 | return None, 0, [] |
|
774 | 774 | |
|
775 | 775 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
776 | 776 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
777 | 777 | token_start = token_match.start() |
|
778 | 778 | token_prefix = token_match.group() |
|
779 | 779 | |
|
780 | 780 | matched = [] |
|
781 | 781 | for key in keys: |
|
782 | 782 | try: |
|
783 | 783 | if not key.startswith(prefix_str): |
|
784 | 784 | continue |
|
785 | 785 | except (AttributeError, TypeError, UnicodeError): |
|
786 | 786 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
787 | 787 | continue |
|
788 | 788 | |
|
789 | 789 | # reformat remainder of key to begin with prefix |
|
790 | 790 | rem = key[len(prefix_str):] |
|
791 | 791 | # force repr wrapped in ' |
|
792 | 792 | rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"') |
|
793 | 793 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
794 | 794 | # Found key is unicode, but prefix is Py2 string. |
|
795 | 795 | # Therefore attempt to interpret key as string. |
|
796 | 796 | try: |
|
797 | 797 | rem_repr = repr(rem.encode('ascii') + '"') |
|
798 | 798 | except UnicodeEncodeError: |
|
799 | 799 | continue |
|
800 | 800 | |
|
801 | 801 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
802 | 802 | if quote == '"': |
|
803 | 803 | # The entered prefix is quoted with ", |
|
804 | 804 | # but the match is quoted with '. |
|
805 | 805 | # A contained " hence needs escaping for comparison: |
|
806 | 806 | rem_repr = rem_repr.replace('"', '\\"') |
|
807 | 807 | |
|
808 | 808 | # then reinsert prefix from start of token |
|
809 | 809 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
810 | 810 | return quote, token_start, matched |
|
811 | 811 | |
|
812 | 812 | |
|
813 | 813 | def cursor_to_position(text:str, line:int, column:int)->int: |
|
814 | 814 | """ |
|
815 | 815 | |
|
816 | 816 | Convert the (line,column) position of the cursor in text to an offset in a |
|
817 | 817 | string. |
|
818 | 818 | |
|
819 | 819 | Parameters |
|
820 | 820 | ---------- |
|
821 | 821 | |
|
822 | 822 | text : str |
|
823 | 823 | The text in which to calculate the cursor offset |
|
824 | 824 | line : int |
|
825 | 825 | Line of the cursor; 0-indexed |
|
826 | 826 | column : int |
|
827 | 827 | Column of the cursor 0-indexed |
|
828 | 828 | |
|
829 | 829 | Return |
|
830 | 830 | ------ |
|
831 | 831 | Position of the cursor in ``text``, 0-indexed. |
|
832 | 832 | |
|
833 | 833 | See Also |
|
834 | 834 | -------- |
|
835 | 835 | position_to_cursor: reciprocal of this function |
|
836 | 836 | |
|
837 | 837 | """ |
|
838 | 838 | lines = text.split('\n') |
|
839 | 839 | assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines))) |
|
840 | 840 | |
|
841 | 841 | return sum(len(l) + 1 for l in lines[:line]) + column |
|
842 | 842 | |
|
843 | 843 | def position_to_cursor(text:str, offset:int)->Tuple[int, int]: |
|
844 | 844 | """ |
|
845 | 845 | Convert the position of the cursor in text (0 indexed) to a line |
|
846 | 846 | number(0-indexed) and a column number (0-indexed) pair |
|
847 | 847 | |
|
848 | 848 | Position should be a valid position in ``text``. |
|
849 | 849 | |
|
850 | 850 | Parameters |
|
851 | 851 | ---------- |
|
852 | 852 | |
|
853 | 853 | text : str |
|
854 | 854 | The text in which to calculate the cursor offset |
|
855 | 855 | offset : int |
|
856 | 856 | Position of the cursor in ``text``, 0-indexed. |
|
857 | 857 | |
|
858 | 858 | Return |
|
859 | 859 | ------ |
|
860 | 860 | (line, column) : (int, int) |
|
861 | 861 | Line of the cursor; 0-indexed, column of the cursor 0-indexed |
|
862 | 862 | |
|
863 | 863 | |
|
864 | 864 | See Also |
|
865 | 865 | -------- |
|
866 | 866 | cursor_to_position : reciprocal of this function |
|
867 | 867 | |
|
868 | 868 | |
|
869 | 869 | """ |
|
870 | 870 | |
|
871 | 871 | assert 0 < offset <= len(text) , "0 < %s <= %s" % (offset , len(text)) |
|
872 | 872 | |
|
873 | 873 | before = text[:offset] |
|
874 | 874 | blines = before.split('\n') # ! splitnes trim trailing \n |
|
875 | 875 | line = before.count('\n') |
|
876 | 876 | col = len(blines[-1]) |
|
877 | 877 | return line, col |
|
878 | 878 | |
|
879 | 879 | |
|
880 | 880 | def _safe_isinstance(obj, module, class_name): |
|
881 | 881 | """Checks if obj is an instance of module.class_name if loaded |
|
882 | 882 | """ |
|
883 | 883 | return (module in sys.modules and |
|
884 | 884 | isinstance(obj, getattr(import_module(module), class_name))) |
|
885 | 885 | |
|
886 | 886 | |
|
887 | 887 | def back_unicode_name_matches(text): |
|
888 | 888 | u"""Match unicode characters back to unicode name |
|
889 | 889 | |
|
890 | 890 | This does ``☃`` -> ``\\snowman`` |
|
891 | 891 | |
|
892 | 892 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
893 | 893 | Though it will not recombine back to the snowman character by the completion machinery. |
|
894 | 894 | |
|
895 | 895 | This will not either back-complete standard sequences like \\n, \\b ... |
|
896 | 896 | |
|
897 | 897 | Used on Python 3 only. |
|
898 | 898 | """ |
|
899 | 899 | if len(text)<2: |
|
900 | 900 | return u'', () |
|
901 | 901 | maybe_slash = text[-2] |
|
902 | 902 | if maybe_slash != '\\': |
|
903 | 903 | return u'', () |
|
904 | 904 | |
|
905 | 905 | char = text[-1] |
|
906 | 906 | # no expand on quote for completion in strings. |
|
907 | 907 | # nor backcomplete standard ascii keys |
|
908 | 908 | if char in string.ascii_letters or char in ['"',"'"]: |
|
909 | 909 | return u'', () |
|
910 | 910 | try : |
|
911 | 911 | unic = unicodedata.name(char) |
|
912 | 912 | return '\\'+char,['\\'+unic] |
|
913 | 913 | except KeyError: |
|
914 | 914 | pass |
|
915 | 915 | return u'', () |
|
916 | 916 | |
|
917 | 917 | def back_latex_name_matches(text:str): |
|
918 | 918 | """Match latex characters back to unicode name |
|
919 | 919 | |
|
920 | 920 | This does ``\\ℵ`` -> ``\\aleph`` |
|
921 | 921 | |
|
922 | 922 | Used on Python 3 only. |
|
923 | 923 | """ |
|
924 | 924 | if len(text)<2: |
|
925 | 925 | return u'', () |
|
926 | 926 | maybe_slash = text[-2] |
|
927 | 927 | if maybe_slash != '\\': |
|
928 | 928 | return u'', () |
|
929 | 929 | |
|
930 | 930 | |
|
931 | 931 | char = text[-1] |
|
932 | 932 | # no expand on quote for completion in strings. |
|
933 | 933 | # nor backcomplete standard ascii keys |
|
934 | 934 | if char in string.ascii_letters or char in ['"',"'"]: |
|
935 | 935 | return u'', () |
|
936 | 936 | try : |
|
937 | 937 | latex = reverse_latex_symbol[char] |
|
938 | 938 | # '\\' replace the \ as well |
|
939 | 939 | return '\\'+char,[latex] |
|
940 | 940 | except KeyError: |
|
941 | 941 | pass |
|
942 | 942 | return u'', () |
|
943 | 943 | |
|
944 | 944 | |
|
945 | 945 | def _formatparamchildren(parameter) -> str: |
|
946 | 946 | """ |
|
947 | 947 | Get parameter name and value from Jedi Private API |
|
948 | 948 | |
|
949 | 949 | Jedi does not expose a simple way to get `param=value` from its API. |
|
950 | 950 | |
|
951 | 951 | Prameter |
|
952 | 952 | ======== |
|
953 | 953 | |
|
954 | 954 | parameter: |
|
955 | 955 | Jedi's function `Param` |
|
956 | 956 | |
|
957 | 957 | Returns |
|
958 | 958 | ======= |
|
959 | 959 | |
|
960 | 960 | A string like 'a', 'b=1', '*args', '**kwargs' |
|
961 | 961 | |
|
962 | 962 | |
|
963 | 963 | """ |
|
964 | 964 | description = parameter.description |
|
965 | 965 | if not description.startswith('param '): |
|
966 | 966 | raise ValueError('Jedi function parameter description have change format.' |
|
967 | 967 | 'Expected "param ...", found %r".' % description) |
|
968 | 968 | return description[6:] |
|
969 | 969 | |
|
970 | 970 | def _make_signature(completion)-> str: |
|
971 | 971 | """ |
|
972 | 972 | Make the signature from a jedi completion |
|
973 | 973 | |
|
974 | 974 | Parameter |
|
975 | 975 | ========= |
|
976 | 976 | |
|
977 | 977 | completion: jedi.Completion |
|
978 | 978 | object does not complete a function type |
|
979 | 979 | |
|
980 | 980 | Returns |
|
981 | 981 | ======= |
|
982 | 982 | |
|
983 | 983 | a string consisting of the function signature, with the parenthesis but |
|
984 | 984 | without the function name. example: |
|
985 | 985 | `(a, *args, b=1, **kwargs)` |
|
986 | 986 | |
|
987 | 987 | """ |
|
988 | 988 | |
|
989 | 989 | return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f]) |
|
990 | 990 | |
|
991 | 991 | class IPCompleter(Completer): |
|
992 | 992 | """Extension of the completer class with IPython-specific features""" |
|
993 | 993 | |
|
994 | 994 | @observe('greedy') |
|
995 | 995 | def _greedy_changed(self, change): |
|
996 | 996 | """update the splitter and readline delims when greedy is changed""" |
|
997 | 997 | if change['new']: |
|
998 | 998 | self.splitter.delims = GREEDY_DELIMS |
|
999 | 999 | else: |
|
1000 | 1000 | self.splitter.delims = DELIMS |
|
1001 | 1001 | |
|
1002 | 1002 | merge_completions = Bool(True, |
|
1003 | 1003 | help="""Whether to merge completion results into a single list |
|
1004 | 1004 | |
|
1005 | 1005 | If False, only the completion results from the first non-empty |
|
1006 | 1006 | completer will be returned. |
|
1007 | 1007 | """ |
|
1008 | 1008 | ).tag(config=True) |
|
1009 | 1009 | omit__names = Enum((0,1,2), default_value=2, |
|
1010 | 1010 | help="""Instruct the completer to omit private method names |
|
1011 | 1011 | |
|
1012 | 1012 | Specifically, when completing on ``object.<tab>``. |
|
1013 | 1013 | |
|
1014 | 1014 | When 2 [default]: all names that start with '_' will be excluded. |
|
1015 | 1015 | |
|
1016 | 1016 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
1017 | 1017 | |
|
1018 | 1018 | When 0: nothing will be excluded. |
|
1019 | 1019 | """ |
|
1020 | 1020 | ).tag(config=True) |
|
1021 | 1021 | limit_to__all__ = Bool(False, |
|
1022 | 1022 | help=""" |
|
1023 | 1023 | DEPRECATED as of version 5.0. |
|
1024 | 1024 | |
|
1025 | 1025 | Instruct the completer to use __all__ for the completion |
|
1026 | 1026 | |
|
1027 | 1027 | Specifically, when completing on ``object.<tab>``. |
|
1028 | 1028 | |
|
1029 | 1029 | When True: only those names in obj.__all__ will be included. |
|
1030 | 1030 | |
|
1031 | 1031 | When False [default]: the __all__ attribute is ignored |
|
1032 | 1032 | """, |
|
1033 | 1033 | ).tag(config=True) |
|
1034 | 1034 | |
|
1035 | 1035 | @observe('limit_to__all__') |
|
1036 | 1036 | def _limit_to_all_changed(self, change): |
|
1037 | 1037 | warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration ' |
|
1038 | 1038 | 'value has been deprecated since IPython 5.0, will be made to have ' |
|
1039 | 1039 | 'no effects and then removed in future version of IPython.', |
|
1040 | 1040 | UserWarning) |
|
1041 | 1041 | |
|
1042 | 1042 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
1043 | 1043 | use_readline=_deprecation_readline_sentinel, config=None, **kwargs): |
|
1044 | 1044 | """IPCompleter() -> completer |
|
1045 | 1045 | |
|
1046 | 1046 | Return a completer object. |
|
1047 | 1047 | |
|
1048 | 1048 | Parameters |
|
1049 | 1049 | ---------- |
|
1050 | 1050 | |
|
1051 | 1051 | shell |
|
1052 | 1052 | a pointer to the ipython shell itself. This is needed |
|
1053 | 1053 | because this completer knows about magic functions, and those can |
|
1054 | 1054 | only be accessed via the ipython instance. |
|
1055 | 1055 | |
|
1056 | 1056 | namespace : dict, optional |
|
1057 | 1057 | an optional dict where completions are performed. |
|
1058 | 1058 | |
|
1059 | 1059 | global_namespace : dict, optional |
|
1060 | 1060 | secondary optional dict for completions, to |
|
1061 | 1061 | handle cases (such as IPython embedded inside functions) where |
|
1062 | 1062 | both Python scopes are visible. |
|
1063 | 1063 | |
|
1064 | 1064 | use_readline : bool, optional |
|
1065 | 1065 | DEPRECATED, ignored since IPython 6.0, will have no effects |
|
1066 | 1066 | """ |
|
1067 | 1067 | |
|
1068 | 1068 | self.magic_escape = ESC_MAGIC |
|
1069 | 1069 | self.splitter = CompletionSplitter() |
|
1070 | 1070 | |
|
1071 | 1071 | if use_readline is not _deprecation_readline_sentinel: |
|
1072 | 1072 | warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.', |
|
1073 | 1073 | DeprecationWarning, stacklevel=2) |
|
1074 | 1074 | |
|
1075 | 1075 | # _greedy_changed() depends on splitter and readline being defined: |
|
1076 | 1076 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
1077 | 1077 | config=config, **kwargs) |
|
1078 | 1078 | |
|
1079 | 1079 | # List where completion matches will be stored |
|
1080 | 1080 | self.matches = [] |
|
1081 | 1081 | self.shell = shell |
|
1082 | 1082 | # Regexp to split filenames with spaces in them |
|
1083 | 1083 | self.space_name_re = re.compile(r'([^\\] )') |
|
1084 | 1084 | # Hold a local ref. to glob.glob for speed |
|
1085 | 1085 | self.glob = glob.glob |
|
1086 | 1086 | |
|
1087 | 1087 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
1088 | 1088 | # buffers, to avoid completion problems. |
|
1089 | 1089 | term = os.environ.get('TERM','xterm') |
|
1090 | 1090 | self.dumb_terminal = term in ['dumb','emacs'] |
|
1091 | 1091 | |
|
1092 | 1092 | # Special handling of backslashes needed in win32 platforms |
|
1093 | 1093 | if sys.platform == "win32": |
|
1094 | 1094 | self.clean_glob = self._clean_glob_win32 |
|
1095 | 1095 | else: |
|
1096 | 1096 | self.clean_glob = self._clean_glob |
|
1097 | 1097 | |
|
1098 | 1098 | #regexp to parse docstring for function signature |
|
1099 | 1099 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
1100 | 1100 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
1101 | 1101 | #use this if positional argument name is also needed |
|
1102 | 1102 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
1103 | 1103 | |
|
1104 | 1104 | # All active matcher routines for completion |
|
1105 | 1105 | self.matchers = [ |
|
1106 | 1106 | self.python_matches, |
|
1107 | 1107 | self.file_matches, |
|
1108 | 1108 | self.magic_matches, |
|
1109 | 1109 | self.python_func_kw_matches, |
|
1110 | 1110 | self.dict_key_matches, |
|
1111 | 1111 | ] |
|
1112 | 1112 | self.magic_arg_matchers = [ |
|
1113 | 1113 | self.magic_config_matches, |
|
1114 | 1114 | self.magic_color_matches, |
|
1115 | 1115 | ] |
|
1116 | 1116 | |
|
1117 | 1117 | # This is set externally by InteractiveShell |
|
1118 | 1118 | self.custom_completers = None |
|
1119 | 1119 | |
|
1120 | 1120 | def all_completions(self, text): |
|
1121 | 1121 | """ |
|
1122 | 1122 | Wrapper around the complete method for the benefit of emacs. |
|
1123 | 1123 | """ |
|
1124 | 1124 | return self.complete(text)[1] |
|
1125 | 1125 | |
|
1126 | 1126 | def _clean_glob(self, text): |
|
1127 | 1127 | return self.glob("%s*" % text) |
|
1128 | 1128 | |
|
1129 | 1129 | def _clean_glob_win32(self,text): |
|
1130 | 1130 | return [f.replace("\\","/") |
|
1131 | 1131 | for f in self.glob("%s*" % text)] |
|
1132 | 1132 | |
|
1133 | 1133 | def file_matches(self, text): |
|
1134 | 1134 | """Match filenames, expanding ~USER type strings. |
|
1135 | 1135 | |
|
1136 | 1136 | Most of the seemingly convoluted logic in this completer is an |
|
1137 | 1137 | attempt to handle filenames with spaces in them. And yet it's not |
|
1138 | 1138 | quite perfect, because Python's readline doesn't expose all of the |
|
1139 | 1139 | GNU readline details needed for this to be done correctly. |
|
1140 | 1140 | |
|
1141 | 1141 | For a filename with a space in it, the printed completions will be |
|
1142 | 1142 | only the parts after what's already been typed (instead of the |
|
1143 | 1143 | full completions, as is normally done). I don't think with the |
|
1144 | 1144 | current (as of Python 2.3) Python readline it's possible to do |
|
1145 | 1145 | better.""" |
|
1146 | 1146 | |
|
1147 | 1147 | # chars that require escaping with backslash - i.e. chars |
|
1148 | 1148 | # that readline treats incorrectly as delimiters, but we |
|
1149 | 1149 | # don't want to treat as delimiters in filename matching |
|
1150 | 1150 | # when escaped with backslash |
|
1151 | 1151 | if text.startswith('!'): |
|
1152 | 1152 | text = text[1:] |
|
1153 | 1153 | text_prefix = u'!' |
|
1154 | 1154 | else: |
|
1155 | 1155 | text_prefix = u'' |
|
1156 | 1156 | |
|
1157 | 1157 | text_until_cursor = self.text_until_cursor |
|
1158 | 1158 | # track strings with open quotes |
|
1159 | 1159 | open_quotes = has_open_quotes(text_until_cursor) |
|
1160 | 1160 | |
|
1161 | 1161 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
1162 | 1162 | lsplit = text |
|
1163 | 1163 | else: |
|
1164 | 1164 | try: |
|
1165 | 1165 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
1166 | 1166 | lsplit = arg_split(text_until_cursor)[-1] |
|
1167 | 1167 | except ValueError: |
|
1168 | 1168 | # typically an unmatched ", or backslash without escaped char. |
|
1169 | 1169 | if open_quotes: |
|
1170 | 1170 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
1171 | 1171 | else: |
|
1172 | 1172 | return [] |
|
1173 | 1173 | except IndexError: |
|
1174 | 1174 | # tab pressed on empty line |
|
1175 | 1175 | lsplit = "" |
|
1176 | 1176 | |
|
1177 | 1177 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
1178 | 1178 | # if protectables are found, do matching on the whole escaped name |
|
1179 | 1179 | has_protectables = True |
|
1180 | 1180 | text0,text = text,lsplit |
|
1181 | 1181 | else: |
|
1182 | 1182 | has_protectables = False |
|
1183 | 1183 | text = os.path.expanduser(text) |
|
1184 | 1184 | |
|
1185 | 1185 | if text == "": |
|
1186 | 1186 | return [text_prefix + protect_filename(f) for f in self.glob("*")] |
|
1187 | 1187 | |
|
1188 | 1188 | # Compute the matches from the filesystem |
|
1189 | 1189 | if sys.platform == 'win32': |
|
1190 | 1190 | m0 = self.clean_glob(text) |
|
1191 | 1191 | else: |
|
1192 | 1192 | m0 = self.clean_glob(text.replace('\\', '')) |
|
1193 | 1193 | |
|
1194 | 1194 | if has_protectables: |
|
1195 | 1195 | # If we had protectables, we need to revert our changes to the |
|
1196 | 1196 | # beginning of filename so that we don't double-write the part |
|
1197 | 1197 | # of the filename we have so far |
|
1198 | 1198 | len_lsplit = len(lsplit) |
|
1199 | 1199 | matches = [text_prefix + text0 + |
|
1200 | 1200 | protect_filename(f[len_lsplit:]) for f in m0] |
|
1201 | 1201 | else: |
|
1202 | 1202 | if open_quotes: |
|
1203 | 1203 | # if we have a string with an open quote, we don't need to |
|
1204 | 1204 | # protect the names beyond the quote (and we _shouldn't_, as |
|
1205 | 1205 | # it would cause bugs when the filesystem call is made). |
|
1206 | 1206 | matches = m0 if sys.platform == "win32" else\ |
|
1207 | 1207 | [protect_filename(f, open_quotes) for f in m0] |
|
1208 | 1208 | else: |
|
1209 | 1209 | matches = [text_prefix + |
|
1210 | 1210 | protect_filename(f) for f in m0] |
|
1211 | 1211 | |
|
1212 | 1212 | # Mark directories in input list by appending '/' to their names. |
|
1213 | 1213 | return [x+'/' if os.path.isdir(x) else x for x in matches] |
|
1214 | 1214 | |
|
1215 | 1215 | def magic_matches(self, text): |
|
1216 | 1216 | """Match magics""" |
|
1217 | 1217 | # Get all shell magics now rather than statically, so magics loaded at |
|
1218 | 1218 | # runtime show up too. |
|
1219 | 1219 | lsm = self.shell.magics_manager.lsmagic() |
|
1220 | 1220 | line_magics = lsm['line'] |
|
1221 | 1221 | cell_magics = lsm['cell'] |
|
1222 | 1222 | pre = self.magic_escape |
|
1223 | 1223 | pre2 = pre+pre |
|
1224 | 1224 | |
|
1225 | 1225 | explicit_magic = text.startswith(pre) |
|
1226 | 1226 | |
|
1227 | 1227 | # Completion logic: |
|
1228 | 1228 | # - user gives %%: only do cell magics |
|
1229 | 1229 | # - user gives %: do both line and cell magics |
|
1230 | 1230 | # - no prefix: do both |
|
1231 | 1231 | # In other words, line magics are skipped if the user gives %% explicitly |
|
1232 | 1232 | # |
|
1233 | 1233 | # We also exclude magics that match any currently visible names: |
|
1234 | 1234 | # https://github.com/ipython/ipython/issues/4877, unless the user has |
|
1235 | 1235 | # typed a %: |
|
1236 | 1236 | # https://github.com/ipython/ipython/issues/10754 |
|
1237 | 1237 | bare_text = text.lstrip(pre) |
|
1238 | 1238 | global_matches = self.global_matches(bare_text) |
|
1239 | 1239 | if not explicit_magic: |
|
1240 | 1240 | def matches(magic): |
|
1241 | 1241 | """ |
|
1242 | 1242 | Filter magics, in particular remove magics that match |
|
1243 | 1243 | a name present in global namespace. |
|
1244 | 1244 | """ |
|
1245 | 1245 | return ( magic.startswith(bare_text) and |
|
1246 | 1246 | magic not in global_matches ) |
|
1247 | 1247 | else: |
|
1248 | 1248 | def matches(magic): |
|
1249 | 1249 | return magic.startswith(bare_text) |
|
1250 | 1250 | |
|
1251 | 1251 | comp = [ pre2+m for m in cell_magics if matches(m)] |
|
1252 | 1252 | if not text.startswith(pre2): |
|
1253 | 1253 | comp += [ pre+m for m in line_magics if matches(m)] |
|
1254 | 1254 | |
|
1255 | 1255 | return comp |
|
1256 | 1256 | |
|
1257 | 1257 | def magic_config_matches(self, text:str) -> List[str]: |
|
1258 | 1258 | """ Match class names and attributes for %config magic """ |
|
1259 | 1259 | texts = text.strip().split() |
|
1260 | 1260 | |
|
1261 | 1261 | if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'): |
|
1262 | 1262 | # get all configuration classes |
|
1263 | 1263 | classes = sorted(set([ c for c in self.shell.configurables |
|
1264 | 1264 | if c.__class__.class_traits(config=True) |
|
1265 | 1265 | ]), key=lambda x: x.__class__.__name__) |
|
1266 | 1266 | classnames = [ c.__class__.__name__ for c in classes ] |
|
1267 | 1267 | |
|
1268 | 1268 | # return all classnames if config or %config is given |
|
1269 | 1269 | if len(texts) == 1: |
|
1270 | 1270 | return classnames |
|
1271 | 1271 | |
|
1272 | 1272 | # match classname |
|
1273 | 1273 | classname_texts = texts[1].split('.') |
|
1274 | 1274 | classname = classname_texts[0] |
|
1275 | 1275 | classname_matches = [ c for c in classnames |
|
1276 | 1276 | if c.startswith(classname) ] |
|
1277 | 1277 | |
|
1278 | 1278 | # return matched classes or the matched class with attributes |
|
1279 | 1279 | if texts[1].find('.') < 0: |
|
1280 | 1280 | return classname_matches |
|
1281 | 1281 | elif len(classname_matches) == 1 and \ |
|
1282 | 1282 | classname_matches[0] == classname: |
|
1283 | 1283 | cls = classes[classnames.index(classname)].__class__ |
|
1284 | 1284 | help = cls.class_get_help() |
|
1285 | 1285 | # strip leading '--' from cl-args: |
|
1286 | 1286 | help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) |
|
1287 | 1287 | return [ attr.split('=')[0] |
|
1288 | 1288 | for attr in help.strip().splitlines() |
|
1289 | 1289 | if attr.startswith(texts[1]) ] |
|
1290 | 1290 | return [] |
|
1291 | 1291 | |
|
1292 | 1292 | def magic_color_matches(self, text:str) -> List[str] : |
|
1293 | 1293 | """ Match color schemes for %colors magic""" |
|
1294 | 1294 | texts = text.split() |
|
1295 | 1295 | if text.endswith(' '): |
|
1296 | 1296 | # .split() strips off the trailing whitespace. Add '' back |
|
1297 | 1297 | # so that: '%colors ' -> ['%colors', ''] |
|
1298 | 1298 | texts.append('') |
|
1299 | 1299 | |
|
1300 | 1300 | if len(texts) == 2 and (texts[0] == 'colors' or texts[0] == '%colors'): |
|
1301 | 1301 | prefix = texts[1] |
|
1302 | 1302 | return [ color for color in InspectColors.keys() |
|
1303 | 1303 | if color.startswith(prefix) ] |
|
1304 | 1304 | return [] |
|
1305 | 1305 | |
|
1306 | 1306 | def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str): |
|
1307 | 1307 | """ |
|
1308 | 1308 | |
|
1309 | 1309 | Return a list of :any:`jedi.api.Completions` object from a ``text`` and |
|
1310 | 1310 | cursor position. |
|
1311 | 1311 | |
|
1312 | 1312 | Parameters |
|
1313 | 1313 | ---------- |
|
1314 | 1314 | cursor_column : int |
|
1315 | 1315 | column position of the cursor in ``text``, 0-indexed. |
|
1316 | 1316 | cursor_line : int |
|
1317 | 1317 | line position of the cursor in ``text``, 0-indexed |
|
1318 | 1318 | text : str |
|
1319 | 1319 | text to complete |
|
1320 | 1320 | |
|
1321 | 1321 | Debugging |
|
1322 | 1322 | --------- |
|
1323 | 1323 | |
|
1324 | 1324 | If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion` |
|
1325 | 1325 | object containing a string with the Jedi debug information attached. |
|
1326 | 1326 | """ |
|
1327 | 1327 | namespaces = [self.namespace] |
|
1328 | 1328 | if self.global_namespace is not None: |
|
1329 | 1329 | namespaces.append(self.global_namespace) |
|
1330 | 1330 | |
|
1331 | 1331 | completion_filter = lambda x:x |
|
1332 | 1332 | # cursor_pos is an it, jedi wants line and column |
|
1333 | 1333 | offset = cursor_to_position(text, cursor_line, cursor_column) |
|
1334 | 1334 | # filter output if we are completing for object members |
|
1335 | 1335 | if offset: |
|
1336 | 1336 | pre = text[offset-1] |
|
1337 | 1337 | if pre == '.': |
|
1338 | 1338 | if self.omit__names == 2: |
|
1339 | 1339 | completion_filter = lambda c:not c.name.startswith('_') |
|
1340 | 1340 | elif self.omit__names == 1: |
|
1341 | 1341 | completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__')) |
|
1342 | 1342 | elif self.omit__names == 0: |
|
1343 | 1343 | completion_filter = lambda x:x |
|
1344 | 1344 | else: |
|
1345 | 1345 | raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names)) |
|
1346 | 1346 | |
|
1347 | 1347 | interpreter = jedi.Interpreter( |
|
1348 | 1348 | text, namespaces, column=cursor_column, line=cursor_line + 1) |
|
1349 | 1349 | try_jedi = True |
|
1350 | 1350 | |
|
1351 | 1351 | try: |
|
1352 | 1352 | # should we check the type of the node is Error ? |
|
1353 | 1353 | try: |
|
1354 | 1354 | # jedi >= 0.11 |
|
1355 | 1355 | from parso.tree import ErrorLeaf |
|
1356 | 1356 | except ImportError: |
|
1357 | 1357 | # jedi < 0.11 |
|
1358 | 1358 | from jedi.parser.tree import ErrorLeaf |
|
1359 | 1359 | |
|
1360 | 1360 | next_to_last_tree = interpreter._get_module().tree_node.children[-2] |
|
1361 | 1361 | completing_string = False |
|
1362 | 1362 | if isinstance(next_to_last_tree, ErrorLeaf): |
|
1363 | 1363 | completing_string = next_to_last_tree.value[0] in {'"', "'"} |
|
1364 | 1364 | # if we are in a string jedi is likely not the right candidate for |
|
1365 | 1365 | # now. Skip it. |
|
1366 | 1366 | try_jedi = not completing_string |
|
1367 | 1367 | except Exception as e: |
|
1368 | 1368 | # many of things can go wrong, we are using private API just don't crash. |
|
1369 | 1369 | if self.debug: |
|
1370 | 1370 | print("Error detecting if completing a non-finished string :", e, '|') |
|
1371 | 1371 | |
|
1372 | 1372 | if not try_jedi: |
|
1373 | 1373 | return [] |
|
1374 | 1374 | try: |
|
1375 | 1375 | return filter(completion_filter, interpreter.completions()) |
|
1376 | 1376 | except Exception as e: |
|
1377 | 1377 | if self.debug: |
|
1378 | 1378 | return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))] |
|
1379 | 1379 | else: |
|
1380 | 1380 | return [] |
|
1381 | 1381 | |
|
1382 | 1382 | def python_matches(self, text): |
|
1383 | 1383 | """Match attributes or global python names""" |
|
1384 | 1384 | if "." in text: |
|
1385 | 1385 | try: |
|
1386 | 1386 | matches = self.attr_matches(text) |
|
1387 | 1387 | if text.endswith('.') and self.omit__names: |
|
1388 | 1388 | if self.omit__names == 1: |
|
1389 | 1389 | # true if txt is _not_ a __ name, false otherwise: |
|
1390 | 1390 | no__name = (lambda txt: |
|
1391 | 1391 | re.match(r'.*\.__.*?__',txt) is None) |
|
1392 | 1392 | else: |
|
1393 | 1393 | # true if txt is _not_ a _ name, false otherwise: |
|
1394 | 1394 | no__name = (lambda txt: |
|
1395 | 1395 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
1396 | 1396 | matches = filter(no__name, matches) |
|
1397 | 1397 | except NameError: |
|
1398 | 1398 | # catches <undefined attributes>.<tab> |
|
1399 | 1399 | matches = [] |
|
1400 | 1400 | else: |
|
1401 | 1401 | matches = self.global_matches(text) |
|
1402 | 1402 | return matches |
|
1403 | 1403 | |
|
1404 | 1404 | def _default_arguments_from_docstring(self, doc): |
|
1405 | 1405 | """Parse the first line of docstring for call signature. |
|
1406 | 1406 | |
|
1407 | 1407 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
1408 | 1408 | It can also parse cython docstring of the form |
|
1409 | 1409 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
1410 | 1410 | """ |
|
1411 | 1411 | if doc is None: |
|
1412 | 1412 | return [] |
|
1413 | 1413 | |
|
1414 | 1414 | #care only the firstline |
|
1415 | 1415 | line = doc.lstrip().splitlines()[0] |
|
1416 | 1416 | |
|
1417 | 1417 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
1418 | 1418 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
1419 | 1419 | sig = self.docstring_sig_re.search(line) |
|
1420 | 1420 | if sig is None: |
|
1421 | 1421 | return [] |
|
1422 | 1422 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
1423 | 1423 | sig = sig.groups()[0].split(',') |
|
1424 | 1424 | ret = [] |
|
1425 | 1425 | for s in sig: |
|
1426 | 1426 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
1427 | 1427 | ret += self.docstring_kwd_re.findall(s) |
|
1428 | 1428 | return ret |
|
1429 | 1429 | |
|
1430 | 1430 | def _default_arguments(self, obj): |
|
1431 | 1431 | """Return the list of default arguments of obj if it is callable, |
|
1432 | 1432 | or empty list otherwise.""" |
|
1433 | 1433 | call_obj = obj |
|
1434 | 1434 | ret = [] |
|
1435 | 1435 | if inspect.isbuiltin(obj): |
|
1436 | 1436 | pass |
|
1437 | 1437 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
1438 | 1438 | if inspect.isclass(obj): |
|
1439 | 1439 | #for cython embedsignature=True the constructor docstring |
|
1440 | 1440 | #belongs to the object itself not __init__ |
|
1441 | 1441 | ret += self._default_arguments_from_docstring( |
|
1442 | 1442 | getattr(obj, '__doc__', '')) |
|
1443 | 1443 | # for classes, check for __init__,__new__ |
|
1444 | 1444 | call_obj = (getattr(obj, '__init__', None) or |
|
1445 | 1445 | getattr(obj, '__new__', None)) |
|
1446 | 1446 | # for all others, check if they are __call__able |
|
1447 | 1447 | elif hasattr(obj, '__call__'): |
|
1448 | 1448 | call_obj = obj.__call__ |
|
1449 | 1449 | ret += self._default_arguments_from_docstring( |
|
1450 | 1450 | getattr(call_obj, '__doc__', '')) |
|
1451 | 1451 | |
|
1452 | 1452 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
1453 | 1453 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
1454 | 1454 | |
|
1455 | 1455 | try: |
|
1456 | 1456 | sig = inspect.signature(call_obj) |
|
1457 | 1457 | ret.extend(k for k, v in sig.parameters.items() if |
|
1458 | 1458 | v.kind in _keeps) |
|
1459 | 1459 | except ValueError: |
|
1460 | 1460 | pass |
|
1461 | 1461 | |
|
1462 | 1462 | return list(set(ret)) |
|
1463 | 1463 | |
|
1464 | 1464 | def python_func_kw_matches(self,text): |
|
1465 | 1465 | """Match named parameters (kwargs) of the last open function""" |
|
1466 | 1466 | |
|
1467 | 1467 | if "." in text: # a parameter cannot be dotted |
|
1468 | 1468 | return [] |
|
1469 | 1469 | try: regexp = self.__funcParamsRegex |
|
1470 | 1470 | except AttributeError: |
|
1471 | 1471 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
1472 | 1472 | '.*?(?<!\\)' | # single quoted strings or |
|
1473 | 1473 | ".*?(?<!\\)" | # double quoted strings or |
|
1474 | 1474 | \w+ | # identifier |
|
1475 | 1475 | \S # other characters |
|
1476 | 1476 | ''', re.VERBOSE | re.DOTALL) |
|
1477 | 1477 | # 1. find the nearest identifier that comes before an unclosed |
|
1478 | 1478 | # parenthesis before the cursor |
|
1479 | 1479 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
1480 | 1480 | tokens = regexp.findall(self.text_until_cursor) |
|
1481 | 1481 | iterTokens = reversed(tokens); openPar = 0 |
|
1482 | 1482 | |
|
1483 | 1483 | for token in iterTokens: |
|
1484 | 1484 | if token == ')': |
|
1485 | 1485 | openPar -= 1 |
|
1486 | 1486 | elif token == '(': |
|
1487 | 1487 | openPar += 1 |
|
1488 | 1488 | if openPar > 0: |
|
1489 | 1489 | # found the last unclosed parenthesis |
|
1490 | 1490 | break |
|
1491 | 1491 | else: |
|
1492 | 1492 | return [] |
|
1493 | 1493 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
1494 | 1494 | ids = [] |
|
1495 | 1495 | isId = re.compile(r'\w+$').match |
|
1496 | 1496 | |
|
1497 | 1497 | while True: |
|
1498 | 1498 | try: |
|
1499 | 1499 | ids.append(next(iterTokens)) |
|
1500 | 1500 | if not isId(ids[-1]): |
|
1501 | 1501 | ids.pop(); break |
|
1502 | 1502 | if not next(iterTokens) == '.': |
|
1503 | 1503 | break |
|
1504 | 1504 | except StopIteration: |
|
1505 | 1505 | break |
|
1506 | 1506 | |
|
1507 | 1507 | # Find all named arguments already assigned to, as to avoid suggesting |
|
1508 | 1508 | # them again |
|
1509 | 1509 | usedNamedArgs = set() |
|
1510 | 1510 | par_level = -1 |
|
1511 | 1511 | for token, next_token in zip(tokens, tokens[1:]): |
|
1512 | 1512 | if token == '(': |
|
1513 | 1513 | par_level += 1 |
|
1514 | 1514 | elif token == ')': |
|
1515 | 1515 | par_level -= 1 |
|
1516 | 1516 | |
|
1517 | 1517 | if par_level != 0: |
|
1518 | 1518 | continue |
|
1519 | 1519 | |
|
1520 | 1520 | if next_token != '=': |
|
1521 | 1521 | continue |
|
1522 | 1522 | |
|
1523 | 1523 | usedNamedArgs.add(token) |
|
1524 | 1524 | |
|
1525 | 1525 | # lookup the candidate callable matches either using global_matches |
|
1526 | 1526 | # or attr_matches for dotted names |
|
1527 | 1527 | if len(ids) == 1: |
|
1528 | 1528 | callableMatches = self.global_matches(ids[0]) |
|
1529 | 1529 | else: |
|
1530 | 1530 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
1531 | 1531 | argMatches = [] |
|
1532 | 1532 | for callableMatch in callableMatches: |
|
1533 | 1533 | try: |
|
1534 | 1534 | namedArgs = self._default_arguments(eval(callableMatch, |
|
1535 | 1535 | self.namespace)) |
|
1536 | 1536 | except: |
|
1537 | 1537 | continue |
|
1538 | 1538 | |
|
1539 | 1539 | # Remove used named arguments from the list, no need to show twice |
|
1540 | 1540 | for namedArg in set(namedArgs) - usedNamedArgs: |
|
1541 | 1541 | if namedArg.startswith(text): |
|
1542 | 1542 | argMatches.append(u"%s=" %namedArg) |
|
1543 | 1543 | return argMatches |
|
1544 | 1544 | |
|
1545 | 1545 | def dict_key_matches(self, text): |
|
1546 | 1546 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
1547 | 1547 | def get_keys(obj): |
|
1548 | 1548 | # Objects can define their own completions by defining an |
|
1549 | 1549 | # _ipy_key_completions_() method. |
|
1550 | 1550 | method = get_real_method(obj, '_ipython_key_completions_') |
|
1551 | 1551 | if method is not None: |
|
1552 | 1552 | return method() |
|
1553 | 1553 | |
|
1554 | 1554 | # Special case some common in-memory dict-like types |
|
1555 | 1555 | if isinstance(obj, dict) or\ |
|
1556 | 1556 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
1557 | 1557 | try: |
|
1558 | 1558 | return list(obj.keys()) |
|
1559 | 1559 | except Exception: |
|
1560 | 1560 | return [] |
|
1561 | 1561 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
1562 | 1562 | _safe_isinstance(obj, 'numpy', 'void'): |
|
1563 | 1563 | return obj.dtype.names or [] |
|
1564 | 1564 | return [] |
|
1565 | 1565 | |
|
1566 | 1566 | try: |
|
1567 | 1567 | regexps = self.__dict_key_regexps |
|
1568 | 1568 | except AttributeError: |
|
1569 | 1569 | dict_key_re_fmt = r'''(?x) |
|
1570 | 1570 | ( # match dict-referring expression wrt greedy setting |
|
1571 | 1571 | %s |
|
1572 | 1572 | ) |
|
1573 | 1573 | \[ # open bracket |
|
1574 | 1574 | \s* # and optional whitespace |
|
1575 | 1575 | ([uUbB]? # string prefix (r not handled) |
|
1576 | 1576 | (?: # unclosed string |
|
1577 | 1577 | '(?:[^']|(?<!\\)\\')* |
|
1578 | 1578 | | |
|
1579 | 1579 | "(?:[^"]|(?<!\\)\\")* |
|
1580 | 1580 | ) |
|
1581 | 1581 | )? |
|
1582 | 1582 | $ |
|
1583 | 1583 | ''' |
|
1584 | 1584 | regexps = self.__dict_key_regexps = { |
|
1585 | 1585 | False: re.compile(dict_key_re_fmt % ''' |
|
1586 | 1586 | # identifiers separated by . |
|
1587 | 1587 | (?!\d)\w+ |
|
1588 | 1588 | (?:\.(?!\d)\w+)* |
|
1589 | 1589 | '''), |
|
1590 | 1590 | True: re.compile(dict_key_re_fmt % ''' |
|
1591 | 1591 | .+ |
|
1592 | 1592 | ''') |
|
1593 | 1593 | } |
|
1594 | 1594 | |
|
1595 | 1595 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
1596 | 1596 | if match is None: |
|
1597 | 1597 | return [] |
|
1598 | 1598 | |
|
1599 | 1599 | expr, prefix = match.groups() |
|
1600 | 1600 | try: |
|
1601 | 1601 | obj = eval(expr, self.namespace) |
|
1602 | 1602 | except Exception: |
|
1603 | 1603 | try: |
|
1604 | 1604 | obj = eval(expr, self.global_namespace) |
|
1605 | 1605 | except Exception: |
|
1606 | 1606 | return [] |
|
1607 | 1607 | |
|
1608 | 1608 | keys = get_keys(obj) |
|
1609 | 1609 | if not keys: |
|
1610 | 1610 | return keys |
|
1611 | 1611 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
1612 | 1612 | if not matches: |
|
1613 | 1613 | return matches |
|
1614 | 1614 | |
|
1615 | 1615 | # get the cursor position of |
|
1616 | 1616 | # - the text being completed |
|
1617 | 1617 | # - the start of the key text |
|
1618 | 1618 | # - the start of the completion |
|
1619 | 1619 | text_start = len(self.text_until_cursor) - len(text) |
|
1620 | 1620 | if prefix: |
|
1621 | 1621 | key_start = match.start(2) |
|
1622 | 1622 | completion_start = key_start + token_offset |
|
1623 | 1623 | else: |
|
1624 | 1624 | key_start = completion_start = match.end() |
|
1625 | 1625 | |
|
1626 | 1626 | # grab the leading prefix, to make sure all completions start with `text` |
|
1627 | 1627 | if text_start > key_start: |
|
1628 | 1628 | leading = '' |
|
1629 | 1629 | else: |
|
1630 | 1630 | leading = text[text_start:completion_start] |
|
1631 | 1631 | |
|
1632 | 1632 | # the index of the `[` character |
|
1633 | 1633 | bracket_idx = match.end(1) |
|
1634 | 1634 | |
|
1635 | 1635 | # append closing quote and bracket as appropriate |
|
1636 | 1636 | # this is *not* appropriate if the opening quote or bracket is outside |
|
1637 | 1637 | # the text given to this method |
|
1638 | 1638 | suf = '' |
|
1639 | 1639 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
1640 | 1640 | if key_start > text_start and closing_quote: |
|
1641 | 1641 | # quotes were opened inside text, maybe close them |
|
1642 | 1642 | if continuation.startswith(closing_quote): |
|
1643 | 1643 | continuation = continuation[len(closing_quote):] |
|
1644 | 1644 | else: |
|
1645 | 1645 | suf += closing_quote |
|
1646 | 1646 | if bracket_idx > text_start: |
|
1647 | 1647 | # brackets were opened inside text, maybe close them |
|
1648 | 1648 | if not continuation.startswith(']'): |
|
1649 | 1649 | suf += ']' |
|
1650 | 1650 | |
|
1651 | 1651 | return [leading + k + suf for k in matches] |
|
1652 | 1652 | |
|
1653 | 1653 | def unicode_name_matches(self, text): |
|
1654 | 1654 | u"""Match Latex-like syntax for unicode characters base |
|
1655 | 1655 | on the name of the character. |
|
1656 | 1656 | |
|
1657 | 1657 | This does ``\\GREEK SMALL LETTER ETA`` -> ``η`` |
|
1658 | 1658 | |
|
1659 | 1659 | Works only on valid python 3 identifier, or on combining characters that |
|
1660 | 1660 | will combine to form a valid identifier. |
|
1661 | 1661 | |
|
1662 | 1662 | Used on Python 3 only. |
|
1663 | 1663 | """ |
|
1664 | 1664 | slashpos = text.rfind('\\') |
|
1665 | 1665 | if slashpos > -1: |
|
1666 | 1666 | s = text[slashpos+1:] |
|
1667 | 1667 | try : |
|
1668 | 1668 | unic = unicodedata.lookup(s) |
|
1669 | 1669 | # allow combining chars |
|
1670 | 1670 | if ('a'+unic).isidentifier(): |
|
1671 | 1671 | return '\\'+s,[unic] |
|
1672 | 1672 | except KeyError: |
|
1673 | 1673 | pass |
|
1674 | 1674 | return u'', [] |
|
1675 | 1675 | |
|
1676 | 1676 | |
|
1677 | 1677 | def latex_matches(self, text): |
|
1678 | 1678 | u"""Match Latex syntax for unicode characters. |
|
1679 | 1679 | |
|
1680 | 1680 | This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``α`` |
|
1681 | 1681 | |
|
1682 | 1682 | Used on Python 3 only. |
|
1683 | 1683 | """ |
|
1684 | 1684 | slashpos = text.rfind('\\') |
|
1685 | 1685 | if slashpos > -1: |
|
1686 | 1686 | s = text[slashpos:] |
|
1687 | 1687 | if s in latex_symbols: |
|
1688 | 1688 | # Try to complete a full latex symbol to unicode |
|
1689 | 1689 | # \\alpha -> α |
|
1690 | 1690 | return s, [latex_symbols[s]] |
|
1691 | 1691 | else: |
|
1692 | 1692 | # If a user has partially typed a latex symbol, give them |
|
1693 | 1693 | # a full list of options \al -> [\aleph, \alpha] |
|
1694 | 1694 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1695 | 1695 | return s, matches |
|
1696 | 1696 | return u'', [] |
|
1697 | 1697 | |
|
1698 | 1698 | def dispatch_custom_completer(self, text): |
|
1699 | 1699 | if not self.custom_completers: |
|
1700 | 1700 | return |
|
1701 | 1701 | |
|
1702 | 1702 | line = self.line_buffer |
|
1703 | 1703 | if not line.strip(): |
|
1704 | 1704 | return None |
|
1705 | 1705 | |
|
1706 | 1706 | # Create a little structure to pass all the relevant information about |
|
1707 | 1707 | # the current completion to any custom completer. |
|
1708 | 1708 | event = SimpleNamespace() |
|
1709 | 1709 | event.line = line |
|
1710 | 1710 | event.symbol = text |
|
1711 | 1711 | cmd = line.split(None,1)[0] |
|
1712 | 1712 | event.command = cmd |
|
1713 | 1713 | event.text_until_cursor = self.text_until_cursor |
|
1714 | 1714 | |
|
1715 | 1715 | # for foo etc, try also to find completer for %foo |
|
1716 | 1716 | if not cmd.startswith(self.magic_escape): |
|
1717 | 1717 | try_magic = self.custom_completers.s_matches( |
|
1718 | 1718 | self.magic_escape + cmd) |
|
1719 | 1719 | else: |
|
1720 | 1720 | try_magic = [] |
|
1721 | 1721 | |
|
1722 | 1722 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1723 | 1723 | try_magic, |
|
1724 | 1724 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1725 | 1725 | try: |
|
1726 | 1726 | res = c(event) |
|
1727 | 1727 | if res: |
|
1728 | 1728 | # first, try case sensitive match |
|
1729 | 1729 | withcase = [r for r in res if r.startswith(text)] |
|
1730 | 1730 | if withcase: |
|
1731 | 1731 | return withcase |
|
1732 | 1732 | # if none, then case insensitive ones are ok too |
|
1733 | 1733 | text_low = text.lower() |
|
1734 | 1734 | return [r for r in res if r.lower().startswith(text_low)] |
|
1735 | 1735 | except TryNext: |
|
1736 | 1736 | pass |
|
1737 | 1737 | except KeyboardInterrupt: |
|
1738 | 1738 | """ |
|
1739 | 1739 | If custom completer take too long, |
|
1740 | 1740 | let keyboard interrupt abort and return nothing. |
|
1741 | 1741 | """ |
|
1742 | 1742 | break |
|
1743 | 1743 | |
|
1744 | 1744 | return None |
|
1745 | 1745 | |
|
1746 | 1746 | def completions(self, text: str, offset: int)->Iterator[Completion]: |
|
1747 | 1747 | """ |
|
1748 | 1748 | Returns an iterator over the possible completions |
|
1749 | 1749 | |
|
1750 | 1750 | .. warning:: Unstable |
|
1751 | 1751 | |
|
1752 | 1752 | This function is unstable, API may change without warning. |
|
1753 | 1753 | It will also raise unless use in proper context manager. |
|
1754 | 1754 | |
|
1755 | 1755 | Parameters |
|
1756 | 1756 | ---------- |
|
1757 | 1757 | |
|
1758 | 1758 | text:str |
|
1759 | 1759 | Full text of the current input, multi line string. |
|
1760 | 1760 | offset:int |
|
1761 | 1761 | Integer representing the position of the cursor in ``text``. Offset |
|
1762 | 1762 | is 0-based indexed. |
|
1763 | 1763 | |
|
1764 | 1764 | Yields |
|
1765 | 1765 | ------ |
|
1766 | 1766 | :any:`Completion` object |
|
1767 | 1767 | |
|
1768 | 1768 | |
|
1769 | 1769 | The cursor on a text can either be seen as being "in between" |
|
1770 | 1770 | characters or "On" a character depending on the interface visible to |
|
1771 | 1771 | the user. For consistency the cursor being on "in between" characters X |
|
1772 | 1772 | and Y is equivalent to the cursor being "on" character Y, that is to say |
|
1773 | 1773 | the character the cursor is on is considered as being after the cursor. |
|
1774 | 1774 | |
|
1775 | 1775 | Combining characters may span more that one position in the |
|
1776 | 1776 | text. |
|
1777 | 1777 | |
|
1778 | 1778 | |
|
1779 | 1779 | .. note:: |
|
1780 | 1780 | |
|
1781 | 1781 | If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--`` |
|
1782 | 1782 | fake Completion token to distinguish completion returned by Jedi |
|
1783 | 1783 | and usual IPython completion. |
|
1784 | 1784 | |
|
1785 | 1785 | .. note:: |
|
1786 | 1786 | |
|
1787 | 1787 | Completions are not completely deduplicated yet. If identical |
|
1788 | 1788 | completions are coming from different sources this function does not |
|
1789 | 1789 | ensure that each completion object will only be present once. |
|
1790 | 1790 | """ |
|
1791 | 1791 | warnings.warn("_complete is a provisional API (as of IPython 6.0). " |
|
1792 | 1792 | "It may change without warnings. " |
|
1793 | 1793 | "Use in corresponding context manager.", |
|
1794 | 1794 | category=ProvisionalCompleterWarning, stacklevel=2) |
|
1795 | 1795 | |
|
1796 | 1796 | seen = set() |
|
1797 | 1797 | for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000): |
|
1798 | 1798 | if c and (c in seen): |
|
1799 | 1799 | continue |
|
1800 | 1800 | yield c |
|
1801 | 1801 | seen.add(c) |
|
1802 | 1802 | |
|
1803 | 1803 | def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]: |
|
1804 | 1804 | """ |
|
1805 | 1805 | Core completion module.Same signature as :any:`completions`, with the |
|
1806 | 1806 | extra `timeout` parameter (in seconds). |
|
1807 | 1807 | |
|
1808 | 1808 | |
|
1809 | 1809 | Computing jedi's completion ``.type`` can be quite expensive (it is a |
|
1810 | 1810 | lazy property) and can require some warm-up, more warm up than just |
|
1811 | 1811 | computing the ``name`` of a completion. The warm-up can be : |
|
1812 | 1812 | |
|
1813 | 1813 | - Long warm-up the first time a module is encountered after |
|
1814 | 1814 | install/update: actually build parse/inference tree. |
|
1815 | 1815 | |
|
1816 | 1816 | - first time the module is encountered in a session: load tree from |
|
1817 | 1817 | disk. |
|
1818 | 1818 | |
|
1819 | 1819 | We don't want to block completions for tens of seconds so we give the |
|
1820 | 1820 | completer a "budget" of ``_timeout`` seconds per invocation to compute |
|
1821 | 1821 | completions types, the completions that have not yet been computed will |
|
1822 | 1822 | be marked as "unknown" an will have a chance to be computed next round |
|
1823 | 1823 | are things get cached. |
|
1824 | 1824 | |
|
1825 | 1825 | Keep in mind that Jedi is not the only thing treating the completion so |
|
1826 | 1826 | keep the timeout short-ish as if we take more than 0.3 second we still |
|
1827 | 1827 | have lots of processing to do. |
|
1828 | 1828 | |
|
1829 | 1829 | """ |
|
1830 | 1830 | deadline = time.monotonic() + _timeout |
|
1831 | 1831 | |
|
1832 | 1832 | |
|
1833 | 1833 | before = full_text[:offset] |
|
1834 | 1834 | cursor_line, cursor_column = position_to_cursor(full_text, offset) |
|
1835 | 1835 | |
|
1836 | 1836 | matched_text, matches, matches_origin, jedi_matches = self._complete( |
|
1837 | 1837 | full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column) |
|
1838 | 1838 | |
|
1839 | 1839 | iter_jm = iter(jedi_matches) |
|
1840 | 1840 | if _timeout: |
|
1841 | 1841 | for jm in iter_jm: |
|
1842 | 1842 | try: |
|
1843 | 1843 | type_ = jm.type |
|
1844 | 1844 | except Exception: |
|
1845 | 1845 | if self.debug: |
|
1846 | 1846 | print("Error in Jedi getting type of ", jm) |
|
1847 | 1847 | type_ = None |
|
1848 | 1848 | delta = len(jm.name_with_symbols) - len(jm.complete) |
|
1849 | 1849 | if type_ == 'function': |
|
1850 | 1850 | signature = _make_signature(jm) |
|
1851 | 1851 | else: |
|
1852 | 1852 | signature = '' |
|
1853 | 1853 | yield Completion(start=offset - delta, |
|
1854 | 1854 | end=offset, |
|
1855 | 1855 | text=jm.name_with_symbols, |
|
1856 | 1856 | type=type_, |
|
1857 | 1857 | signature=signature, |
|
1858 | 1858 | _origin='jedi') |
|
1859 | 1859 | |
|
1860 | 1860 | if time.monotonic() > deadline: |
|
1861 | 1861 | break |
|
1862 | 1862 | |
|
1863 | 1863 | for jm in iter_jm: |
|
1864 | 1864 | delta = len(jm.name_with_symbols) - len(jm.complete) |
|
1865 | 1865 | yield Completion(start=offset - delta, |
|
1866 | 1866 | end=offset, |
|
1867 | 1867 | text=jm.name_with_symbols, |
|
1868 | 1868 | type='<unknown>', # don't compute type for speed |
|
1869 | 1869 | _origin='jedi', |
|
1870 | 1870 | signature='') |
|
1871 | 1871 | |
|
1872 | 1872 | |
|
1873 | 1873 | start_offset = before.rfind(matched_text) |
|
1874 | 1874 | |
|
1875 | 1875 | # TODO: |
|
1876 | # Supress this, right now just for debug. | |
|
1876 | # Suppress this, right now just for debug. | |
|
1877 | 1877 | if jedi_matches and matches and self.debug: |
|
1878 | 1878 | yield Completion(start=start_offset, end=offset, text='--jedi/ipython--', |
|
1879 | 1879 | _origin='debug', type='none', signature='') |
|
1880 | 1880 | |
|
1881 | 1881 | # I'm unsure if this is always true, so let's assert and see if it |
|
1882 | 1882 | # crash |
|
1883 | 1883 | assert before.endswith(matched_text) |
|
1884 | 1884 | for m, t in zip(matches, matches_origin): |
|
1885 | 1885 | yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>') |
|
1886 | 1886 | |
|
1887 | 1887 | |
|
1888 | 1888 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1889 | 1889 | """Find completions for the given text and line context. |
|
1890 | 1890 | |
|
1891 | 1891 | Note that both the text and the line_buffer are optional, but at least |
|
1892 | 1892 | one of them must be given. |
|
1893 | 1893 | |
|
1894 | 1894 | Parameters |
|
1895 | 1895 | ---------- |
|
1896 | 1896 | text : string, optional |
|
1897 | 1897 | Text to perform the completion on. If not given, the line buffer |
|
1898 | 1898 | is split using the instance's CompletionSplitter object. |
|
1899 | 1899 | |
|
1900 | 1900 | line_buffer : string, optional |
|
1901 | 1901 | If not given, the completer attempts to obtain the current line |
|
1902 | 1902 | buffer via readline. This keyword allows clients which are |
|
1903 | 1903 | requesting for text completions in non-readline contexts to inform |
|
1904 | 1904 | the completer of the entire text. |
|
1905 | 1905 | |
|
1906 | 1906 | cursor_pos : int, optional |
|
1907 | 1907 | Index of the cursor in the full line buffer. Should be provided by |
|
1908 | 1908 | remote frontends where kernel has no access to frontend state. |
|
1909 | 1909 | |
|
1910 | 1910 | Returns |
|
1911 | 1911 | ------- |
|
1912 | 1912 | text : str |
|
1913 | 1913 | Text that was actually used in the completion. |
|
1914 | 1914 | |
|
1915 | 1915 | matches : list |
|
1916 | 1916 | A list of completion matches. |
|
1917 | 1917 | |
|
1918 | 1918 | |
|
1919 | 1919 | .. note:: |
|
1920 | 1920 | |
|
1921 | 1921 | This API is likely to be deprecated and replaced by |
|
1922 | 1922 | :any:`IPCompleter.completions` in the future. |
|
1923 | 1923 | |
|
1924 | 1924 | |
|
1925 | 1925 | """ |
|
1926 | 1926 | warnings.warn('`Completer.complete` is pending deprecation since ' |
|
1927 | 1927 | 'IPython 6.0 and will be replaced by `Completer.completions`.', |
|
1928 | 1928 | PendingDeprecationWarning) |
|
1929 | 1929 | # potential todo, FOLD the 3rd throw away argument of _complete |
|
1930 | 1930 | # into the first 2 one. |
|
1931 | 1931 | return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2] |
|
1932 | 1932 | |
|
1933 | 1933 | def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None, |
|
1934 | 1934 | full_text=None, return_jedi_results=True) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]: |
|
1935 | 1935 | """ |
|
1936 | 1936 | |
|
1937 | 1937 | Like complete but can also returns raw jedi completions as well as the |
|
1938 | 1938 | origin of the completion text. This could (and should) be made much |
|
1939 | 1939 | cleaner but that will be simpler once we drop the old (and stateful) |
|
1940 | 1940 | :any:`complete` API. |
|
1941 | 1941 | |
|
1942 | 1942 | |
|
1943 | 1943 | With current provisional API, cursor_pos act both (depending on the |
|
1944 | 1944 | caller) as the offset in the ``text`` or ``line_buffer``, or as the |
|
1945 | 1945 | ``column`` when passing multiline strings this could/should be renamed |
|
1946 | 1946 | but would add extra noise. |
|
1947 | 1947 | """ |
|
1948 | 1948 | |
|
1949 | 1949 | # if the cursor position isn't given, the only sane assumption we can |
|
1950 | 1950 | # make is that it's at the end of the line (the common case) |
|
1951 | 1951 | if cursor_pos is None: |
|
1952 | 1952 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1953 | 1953 | |
|
1954 | 1954 | if self.use_main_ns: |
|
1955 | 1955 | self.namespace = __main__.__dict__ |
|
1956 | 1956 | |
|
1957 | 1957 | # if text is either None or an empty string, rely on the line buffer |
|
1958 | 1958 | if (not line_buffer) and full_text: |
|
1959 | 1959 | line_buffer = full_text.split('\n')[cursor_line] |
|
1960 | 1960 | if not text: |
|
1961 | 1961 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1962 | 1962 | |
|
1963 | 1963 | if self.backslash_combining_completions: |
|
1964 | 1964 | # allow deactivation of these on windows. |
|
1965 | 1965 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1966 | 1966 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1967 | 1967 | if latex_matches: |
|
1968 | 1968 | return latex_text, latex_matches, ['latex_matches']*len(latex_matches), () |
|
1969 | 1969 | name_text = '' |
|
1970 | 1970 | name_matches = [] |
|
1971 | 1971 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1972 | 1972 | name_text, name_matches = meth(base_text) |
|
1973 | 1973 | if name_text: |
|
1974 | 1974 | return name_text, name_matches[:MATCHES_LIMIT], \ |
|
1975 | 1975 | [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), () |
|
1976 | 1976 | |
|
1977 | 1977 | |
|
1978 | 1978 | # If no line buffer is given, assume the input text is all there was |
|
1979 | 1979 | if line_buffer is None: |
|
1980 | 1980 | line_buffer = text |
|
1981 | 1981 | |
|
1982 | 1982 | self.line_buffer = line_buffer |
|
1983 | 1983 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1984 | 1984 | |
|
1985 | 1985 | # Do magic arg matches |
|
1986 | 1986 | for matcher in self.magic_arg_matchers: |
|
1987 | 1987 | matches = list(matcher(line_buffer))[:MATCHES_LIMIT] |
|
1988 | 1988 | if matches: |
|
1989 | 1989 | origins = [matcher.__qualname__] * len(matches) |
|
1990 | 1990 | return text, matches, origins, () |
|
1991 | 1991 | |
|
1992 | 1992 | # Start with a clean slate of completions |
|
1993 | 1993 | matches = [] |
|
1994 | 1994 | custom_res = self.dispatch_custom_completer(text) |
|
1995 | 1995 | # FIXME: we should extend our api to return a dict with completions for |
|
1996 | 1996 | # different types of objects. The rlcomplete() method could then |
|
1997 | 1997 | # simply collapse the dict into a list for readline, but we'd have |
|
1998 | 1998 | # richer completion semantics in other evironments. |
|
1999 | 1999 | completions = () |
|
2000 | 2000 | if self.use_jedi and return_jedi_results: |
|
2001 | 2001 | if not full_text: |
|
2002 | 2002 | full_text = line_buffer |
|
2003 | 2003 | completions = self._jedi_matches( |
|
2004 | 2004 | cursor_pos, cursor_line, full_text) |
|
2005 | 2005 | if custom_res is not None: |
|
2006 | 2006 | # did custom completers produce something? |
|
2007 | 2007 | matches = [(m, 'custom') for m in custom_res] |
|
2008 | 2008 | else: |
|
2009 | 2009 | # Extend the list of completions with the results of each |
|
2010 | 2010 | # matcher, so we return results to the user from all |
|
2011 | 2011 | # namespaces. |
|
2012 | 2012 | if self.merge_completions: |
|
2013 | 2013 | matches = [] |
|
2014 | 2014 | for matcher in self.matchers: |
|
2015 | 2015 | try: |
|
2016 | 2016 | matches.extend([(m, matcher.__qualname__) |
|
2017 | 2017 | for m in matcher(text)]) |
|
2018 | 2018 | except: |
|
2019 | 2019 | # Show the ugly traceback if the matcher causes an |
|
2020 | 2020 | # exception, but do NOT crash the kernel! |
|
2021 | 2021 | sys.excepthook(*sys.exc_info()) |
|
2022 | 2022 | else: |
|
2023 | 2023 | for matcher in self.matchers: |
|
2024 | 2024 | matches = [(m, matcher.__qualname__) |
|
2025 | 2025 | for m in matcher(text)] |
|
2026 | 2026 | if matches: |
|
2027 | 2027 | break |
|
2028 | 2028 | seen = set() |
|
2029 | 2029 | filtered_matches = set() |
|
2030 | 2030 | for m in matches: |
|
2031 | 2031 | t, c = m |
|
2032 | 2032 | if t not in seen: |
|
2033 | 2033 | filtered_matches.add(m) |
|
2034 | 2034 | seen.add(t) |
|
2035 | 2035 | |
|
2036 | 2036 | _filtered_matches = sorted( |
|
2037 | 2037 | set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\ |
|
2038 | 2038 | [:MATCHES_LIMIT] |
|
2039 | 2039 | |
|
2040 | 2040 | _matches = [m[0] for m in _filtered_matches] |
|
2041 | 2041 | origins = [m[1] for m in _filtered_matches] |
|
2042 | 2042 | |
|
2043 | 2043 | self.matches = _matches |
|
2044 | 2044 | |
|
2045 | 2045 | return text, _matches, origins, completions |
@@ -1,229 +1,229 b'' | |||
|
1 | 1 | """Hooks for IPython. |
|
2 | 2 | |
|
3 | 3 | In Python, it is possible to overwrite any method of any object if you really |
|
4 | 4 | want to. But IPython exposes a few 'hooks', methods which are *designed* to |
|
5 | 5 | be overwritten by users for customization purposes. This module defines the |
|
6 | 6 | default versions of all such hooks, which get used by IPython if not |
|
7 | 7 | overridden by the user. |
|
8 | 8 | |
|
9 | 9 | Hooks are simple functions, but they should be declared with ``self`` as their |
|
10 | 10 | first argument, because when activated they are registered into IPython as |
|
11 | 11 | instance methods. The self argument will be the IPython running instance |
|
12 | 12 | itself, so hooks have full access to the entire IPython object. |
|
13 | 13 | |
|
14 | 14 | If you wish to define a new hook and activate it, you can make an :doc:`extension |
|
15 | 15 | </config/extensions/index>` or a :ref:`startup script <startup_files>`. For |
|
16 | 16 | example, you could use a startup file like this:: |
|
17 | 17 | |
|
18 | 18 | import os |
|
19 | 19 | |
|
20 | 20 | def calljed(self,filename, linenum): |
|
21 | 21 | "My editor hook calls the jed editor directly." |
|
22 | 22 | print "Calling my own editor, jed ..." |
|
23 | 23 | if os.system('jed +%d %s' % (linenum,filename)) != 0: |
|
24 | 24 | raise TryNext() |
|
25 | 25 | |
|
26 | 26 | def load_ipython_extension(ip): |
|
27 | 27 | ip.set_hook('editor', calljed) |
|
28 | 28 | |
|
29 | 29 | """ |
|
30 | 30 | |
|
31 | 31 | #***************************************************************************** |
|
32 | 32 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> |
|
33 | 33 | # |
|
34 | 34 | # Distributed under the terms of the BSD License. The full license is in |
|
35 | 35 | # the file COPYING, distributed as part of this software. |
|
36 | 36 | #***************************************************************************** |
|
37 | 37 | |
|
38 | 38 | import os |
|
39 | 39 | import subprocess |
|
40 | 40 | import warnings |
|
41 | 41 | import sys |
|
42 | 42 | |
|
43 | 43 | from IPython.core.error import TryNext |
|
44 | 44 | |
|
45 | 45 | # List here all the default hooks. For now it's just the editor functions |
|
46 | 46 | # but over time we'll move here all the public API for user-accessible things. |
|
47 | 47 | |
|
48 | 48 | __all__ = ['editor', 'synchronize_with_editor', |
|
49 | 49 | 'shutdown_hook', 'late_startup_hook', |
|
50 | 50 | 'show_in_pager','pre_prompt_hook', |
|
51 | 51 | 'pre_run_code_hook', 'clipboard_get'] |
|
52 | 52 | |
|
53 | 53 | deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event", |
|
54 | 54 | 'late_startup_hook': "a callback for the 'shell_initialized' event", |
|
55 | 55 | 'shutdown_hook': "the atexit module", |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | def editor(self, filename, linenum=None, wait=True): |
|
59 | 59 | """Open the default editor at the given filename and linenumber. |
|
60 | 60 | |
|
61 | 61 | This is IPython's default editor hook, you can use it as an example to |
|
62 | 62 | write your own modified one. To set your own editor function as the |
|
63 | 63 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
64 | 64 | |
|
65 | 65 | # IPython configures a default editor at startup by reading $EDITOR from |
|
66 | 66 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
67 | 67 | editor = self.editor |
|
68 | 68 | |
|
69 | 69 | # marker for at which line to open the file (for existing objects) |
|
70 | 70 | if linenum is None or editor=='notepad': |
|
71 | 71 | linemark = '' |
|
72 | 72 | else: |
|
73 | 73 | linemark = '+%d' % int(linenum) |
|
74 | 74 | |
|
75 | 75 | # Enclose in quotes if necessary and legal |
|
76 | 76 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
77 | 77 | editor = '"%s"' % editor |
|
78 | 78 | |
|
79 | 79 | # Call the actual editor |
|
80 | 80 | proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename), |
|
81 | 81 | shell=True) |
|
82 | 82 | if wait and proc.wait() != 0: |
|
83 | 83 | raise TryNext() |
|
84 | 84 | |
|
85 | 85 | import tempfile |
|
86 | 86 | from IPython.utils.decorators import undoc |
|
87 | 87 | |
|
88 | 88 | @undoc |
|
89 | 89 | def fix_error_editor(self,filename,linenum,column,msg): |
|
90 | 90 | """DEPRECATED |
|
91 | 91 | |
|
92 | 92 | Open the editor at the given filename, linenumber, column and |
|
93 | 93 | show an error message. This is used for correcting syntax errors. |
|
94 | 94 | The current implementation only has special support for the VIM editor, |
|
95 | 95 | and falls back on the 'editor' hook if VIM is not used. |
|
96 | 96 | |
|
97 | 97 | Call ip.set_hook('fix_error_editor',yourfunc) to use your own function, |
|
98 | 98 | """ |
|
99 | 99 | |
|
100 | 100 | warnings.warn(""" |
|
101 | 101 | `fix_error_editor` is deprecated as of IPython 6.0 and will be removed |
|
102 | 102 | in future versions. It appears to be used only for automatically fixing syntax |
|
103 | 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 | 105 | the mailing list ipython-dev@python.org , or on the GitHub Issue tracker: |
|
106 | 106 | https://github.com/ipython/ipython/issues/9649 """, UserWarning) |
|
107 | 107 | |
|
108 | 108 | def vim_quickfix_file(): |
|
109 | 109 | t = tempfile.NamedTemporaryFile() |
|
110 | 110 | t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg)) |
|
111 | 111 | t.flush() |
|
112 | 112 | return t |
|
113 | 113 | if os.path.basename(self.editor) != 'vim': |
|
114 | 114 | self.hooks.editor(filename,linenum) |
|
115 | 115 | return |
|
116 | 116 | t = vim_quickfix_file() |
|
117 | 117 | try: |
|
118 | 118 | if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name): |
|
119 | 119 | raise TryNext() |
|
120 | 120 | finally: |
|
121 | 121 | t.close() |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def synchronize_with_editor(self, filename, linenum, column): |
|
125 | 125 | pass |
|
126 | 126 | |
|
127 | 127 | |
|
128 | 128 | class CommandChainDispatcher: |
|
129 | 129 | """ Dispatch calls to a chain of commands until some func can handle it |
|
130 | 130 | |
|
131 | 131 | Usage: instantiate, execute "add" to add commands (with optional |
|
132 | 132 | priority), execute normally via f() calling mechanism. |
|
133 | 133 | |
|
134 | 134 | """ |
|
135 | 135 | def __init__(self,commands=None): |
|
136 | 136 | if commands is None: |
|
137 | 137 | self.chain = [] |
|
138 | 138 | else: |
|
139 | 139 | self.chain = commands |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | def __call__(self,*args, **kw): |
|
143 | 143 | """ Command chain is called just like normal func. |
|
144 | 144 | |
|
145 | 145 | This will call all funcs in chain with the same args as were given to |
|
146 | 146 | this function, and return the result of first func that didn't raise |
|
147 | 147 | TryNext""" |
|
148 | 148 | last_exc = TryNext() |
|
149 | 149 | for prio,cmd in self.chain: |
|
150 | 150 | #print "prio",prio,"cmd",cmd #dbg |
|
151 | 151 | try: |
|
152 | 152 | return cmd(*args, **kw) |
|
153 | 153 | except TryNext as exc: |
|
154 | 154 | last_exc = exc |
|
155 | 155 | # if no function will accept it, raise TryNext up to the caller |
|
156 | 156 | raise last_exc |
|
157 | 157 | |
|
158 | 158 | def __str__(self): |
|
159 | 159 | return str(self.chain) |
|
160 | 160 | |
|
161 | 161 | def add(self, func, priority=0): |
|
162 | 162 | """ Add a func to the cmd chain with given priority """ |
|
163 | 163 | self.chain.append((priority, func)) |
|
164 | 164 | self.chain.sort(key=lambda x: x[0]) |
|
165 | 165 | |
|
166 | 166 | def __iter__(self): |
|
167 | 167 | """ Return all objects in chain. |
|
168 | 168 | |
|
169 | 169 | Handy if the objects are not callable. |
|
170 | 170 | """ |
|
171 | 171 | return iter(self.chain) |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | def shutdown_hook(self): |
|
175 | 175 | """ default shutdown hook |
|
176 | 176 | |
|
177 | 177 | Typically, shotdown hooks should raise TryNext so all shutdown ops are done |
|
178 | 178 | """ |
|
179 | 179 | |
|
180 | 180 | #print "default shutdown hook ok" # dbg |
|
181 | 181 | return |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | def late_startup_hook(self): |
|
185 | 185 | """ Executed after ipython has been constructed and configured |
|
186 | 186 | |
|
187 | 187 | """ |
|
188 | 188 | #print "default startup hook ok" # dbg |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | def show_in_pager(self, data, start, screen_lines): |
|
192 | 192 | """ Run a string through pager """ |
|
193 | 193 | # raising TryNext here will use the default paging functionality |
|
194 | 194 | raise TryNext |
|
195 | 195 | |
|
196 | 196 | |
|
197 | 197 | def pre_prompt_hook(self): |
|
198 | 198 | """ Run before displaying the next prompt |
|
199 | 199 | |
|
200 | 200 | Use this e.g. to display output from asynchronous operations (in order |
|
201 | 201 | to not mess up text entry) |
|
202 | 202 | """ |
|
203 | 203 | |
|
204 | 204 | return None |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | def pre_run_code_hook(self): |
|
208 | 208 | """ Executed before running the (prefiltered) code in IPython """ |
|
209 | 209 | return None |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def clipboard_get(self): |
|
213 | 213 | """ Get text from the clipboard. |
|
214 | 214 | """ |
|
215 | 215 | from IPython.lib.clipboard import ( |
|
216 | 216 | osx_clipboard_get, tkinter_clipboard_get, |
|
217 | 217 | win32_clipboard_get |
|
218 | 218 | ) |
|
219 | 219 | if sys.platform == 'win32': |
|
220 | 220 | chain = [win32_clipboard_get, tkinter_clipboard_get] |
|
221 | 221 | elif sys.platform == 'darwin': |
|
222 | 222 | chain = [osx_clipboard_get, tkinter_clipboard_get] |
|
223 | 223 | else: |
|
224 | 224 | chain = [tkinter_clipboard_get] |
|
225 | 225 | dispatcher = CommandChainDispatcher() |
|
226 | 226 | for func in chain: |
|
227 | 227 | dispatcher.add(func) |
|
228 | 228 | text = dispatcher() |
|
229 | 229 | return text |
@@ -1,3322 +1,3322 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Main IPython class.""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | import abc |
|
15 | 15 | import ast |
|
16 | 16 | import atexit |
|
17 | 17 | import builtins as builtin_mod |
|
18 | 18 | import functools |
|
19 | 19 | import os |
|
20 | 20 | import re |
|
21 | 21 | import runpy |
|
22 | 22 | import sys |
|
23 | 23 | import tempfile |
|
24 | 24 | import traceback |
|
25 | 25 | import types |
|
26 | 26 | import subprocess |
|
27 | 27 | import warnings |
|
28 | 28 | from io import open as io_open |
|
29 | 29 | |
|
30 | 30 | from pickleshare import PickleShareDB |
|
31 | 31 | |
|
32 | 32 | from traitlets.config.configurable import SingletonConfigurable |
|
33 | 33 | from IPython.core import oinspect |
|
34 | 34 | from IPython.core import magic |
|
35 | 35 | from IPython.core import page |
|
36 | 36 | from IPython.core import prefilter |
|
37 | 37 | from IPython.core import ultratb |
|
38 | 38 | from IPython.core.alias import Alias, AliasManager |
|
39 | 39 | from IPython.core.autocall import ExitAutocall |
|
40 | 40 | from IPython.core.builtin_trap import BuiltinTrap |
|
41 | 41 | from IPython.core.events import EventManager, available_events |
|
42 | 42 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
43 | 43 | from IPython.core.debugger import Pdb |
|
44 | 44 | from IPython.core.display_trap import DisplayTrap |
|
45 | 45 | from IPython.core.displayhook import DisplayHook |
|
46 | 46 | from IPython.core.displaypub import DisplayPublisher |
|
47 | 47 | from IPython.core.error import InputRejected, UsageError |
|
48 | 48 | from IPython.core.extensions import ExtensionManager |
|
49 | 49 | from IPython.core.formatters import DisplayFormatter |
|
50 | 50 | from IPython.core.history import HistoryManager |
|
51 | 51 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 |
|
52 | 52 | from IPython.core.logger import Logger |
|
53 | 53 | from IPython.core.macro import Macro |
|
54 | 54 | from IPython.core.payload import PayloadManager |
|
55 | 55 | from IPython.core.prefilter import PrefilterManager |
|
56 | 56 | from IPython.core.profiledir import ProfileDir |
|
57 | 57 | from IPython.core.usage import default_banner |
|
58 | 58 | from IPython.display import display |
|
59 | 59 | from IPython.testing.skipdoctest import skip_doctest |
|
60 | 60 | from IPython.utils import PyColorize |
|
61 | 61 | from IPython.utils import io |
|
62 | 62 | from IPython.utils import py3compat |
|
63 | 63 | from IPython.utils import openpy |
|
64 | 64 | from IPython.utils.decorators import undoc |
|
65 | 65 | from IPython.utils.io import ask_yes_no |
|
66 | 66 | from IPython.utils.ipstruct import Struct |
|
67 | 67 | from IPython.paths import get_ipython_dir |
|
68 | 68 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists |
|
69 | 69 | from IPython.utils.process import system, getoutput |
|
70 | 70 | from IPython.utils.strdispatch import StrDispatch |
|
71 | 71 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
72 | 72 | from IPython.utils.text import format_screen, LSString, SList, DollarFormatter |
|
73 | 73 | from IPython.utils.tempdir import TemporaryDirectory |
|
74 | 74 | from traitlets import ( |
|
75 | 75 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, |
|
76 | 76 | observe, default, |
|
77 | 77 | ) |
|
78 | 78 | from warnings import warn |
|
79 | 79 | from logging import error |
|
80 | 80 | import IPython.core.hooks |
|
81 | 81 | |
|
82 | 82 | from typing import List as ListType |
|
83 | 83 | from ast import AST |
|
84 | 84 | |
|
85 | 85 | # NoOpContext is deprecated, but ipykernel imports it from here. |
|
86 | 86 | # See https://github.com/ipython/ipykernel/issues/157 |
|
87 | 87 | from IPython.utils.contexts import NoOpContext |
|
88 | 88 | |
|
89 | 89 | try: |
|
90 | 90 | import docrepr.sphinxify as sphx |
|
91 | 91 | |
|
92 | 92 | def sphinxify(doc): |
|
93 | 93 | with TemporaryDirectory() as dirname: |
|
94 | 94 | return { |
|
95 | 95 | 'text/html': sphx.sphinxify(doc, dirname), |
|
96 | 96 | 'text/plain': doc |
|
97 | 97 | } |
|
98 | 98 | except ImportError: |
|
99 | 99 | sphinxify = None |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | class ProvisionalWarning(DeprecationWarning): |
|
103 | 103 | """ |
|
104 | 104 | Warning class for unstable features |
|
105 | 105 | """ |
|
106 | 106 | pass |
|
107 | 107 | |
|
108 | 108 | if sys.version_info > (3,6): |
|
109 | 109 | _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) |
|
110 | 110 | _single_targets_nodes = (ast.AugAssign, ast.AnnAssign) |
|
111 | 111 | else: |
|
112 | 112 | _assign_nodes = (ast.AugAssign, ast.Assign ) |
|
113 | 113 | _single_targets_nodes = (ast.AugAssign, ) |
|
114 | 114 | |
|
115 | 115 | #----------------------------------------------------------------------------- |
|
116 | 116 | # Globals |
|
117 | 117 | #----------------------------------------------------------------------------- |
|
118 | 118 | |
|
119 | 119 | # compiled regexps for autoindent management |
|
120 | 120 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
121 | 121 | |
|
122 | 122 | #----------------------------------------------------------------------------- |
|
123 | 123 | # Utilities |
|
124 | 124 | #----------------------------------------------------------------------------- |
|
125 | 125 | |
|
126 | 126 | @undoc |
|
127 | 127 | def softspace(file, newvalue): |
|
128 | 128 | """Copied from code.py, to remove the dependency""" |
|
129 | 129 | |
|
130 | 130 | oldvalue = 0 |
|
131 | 131 | try: |
|
132 | 132 | oldvalue = file.softspace |
|
133 | 133 | except AttributeError: |
|
134 | 134 | pass |
|
135 | 135 | try: |
|
136 | 136 | file.softspace = newvalue |
|
137 | 137 | except (AttributeError, TypeError): |
|
138 | 138 | # "attribute-less object" or "read-only attributes" |
|
139 | 139 | pass |
|
140 | 140 | return oldvalue |
|
141 | 141 | |
|
142 | 142 | @undoc |
|
143 | 143 | def no_op(*a, **kw): |
|
144 | 144 | pass |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | class SpaceInInput(Exception): pass |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | def get_default_colors(): |
|
151 | 151 | "DEPRECATED" |
|
152 | 152 | warn('get_default_color is deprecated since IPython 5.0, and returns `Neutral` on all platforms.', |
|
153 | 153 | DeprecationWarning, stacklevel=2) |
|
154 | 154 | return 'Neutral' |
|
155 | 155 | |
|
156 | 156 | |
|
157 | 157 | class SeparateUnicode(Unicode): |
|
158 | 158 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
159 | 159 | |
|
160 | 160 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
161 | 161 | """ |
|
162 | 162 | |
|
163 | 163 | def validate(self, obj, value): |
|
164 | 164 | if value == '0': value = '' |
|
165 | 165 | value = value.replace('\\n','\n') |
|
166 | 166 | return super(SeparateUnicode, self).validate(obj, value) |
|
167 | 167 | |
|
168 | 168 | |
|
169 | 169 | @undoc |
|
170 | 170 | class DummyMod(object): |
|
171 | 171 | """A dummy module used for IPython's interactive module when |
|
172 | 172 | a namespace must be assigned to the module's __dict__.""" |
|
173 | 173 | pass |
|
174 | 174 | |
|
175 | 175 | |
|
176 | 176 | class ExecutionInfo(object): |
|
177 | 177 | """The arguments used for a call to :meth:`InteractiveShell.run_cell` |
|
178 | 178 | |
|
179 | 179 | Stores information about what is going to happen. |
|
180 | 180 | """ |
|
181 | 181 | raw_cell = None |
|
182 | 182 | store_history = False |
|
183 | 183 | silent = False |
|
184 | 184 | shell_futures = True |
|
185 | 185 | |
|
186 | 186 | def __init__(self, raw_cell, store_history, silent, shell_futures): |
|
187 | 187 | self.raw_cell = raw_cell |
|
188 | 188 | self.store_history = store_history |
|
189 | 189 | self.silent = silent |
|
190 | 190 | self.shell_futures = shell_futures |
|
191 | 191 | |
|
192 | 192 | def __repr__(self): |
|
193 | 193 | name = self.__class__.__qualname__ |
|
194 | 194 | raw_cell = ((self.raw_cell[:50] + '..') |
|
195 | 195 | if len(self.raw_cell) > 50 else self.raw_cell) |
|
196 | 196 | return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s result=%s>' %\ |
|
197 | 197 | (name, id(self), raw_cell, store_history, silent, shell_futures, repr(self.result)) |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | class ExecutionResult(object): |
|
201 | 201 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
202 | 202 | |
|
203 | 203 | Stores information about what took place. |
|
204 | 204 | """ |
|
205 | 205 | execution_count = None |
|
206 | 206 | error_before_exec = None |
|
207 | 207 | error_in_exec = None |
|
208 | 208 | info = None |
|
209 | 209 | result = None |
|
210 | 210 | |
|
211 | 211 | def __init__(self, info): |
|
212 | 212 | self.info = info |
|
213 | 213 | |
|
214 | 214 | @property |
|
215 | 215 | def success(self): |
|
216 | 216 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
217 | 217 | |
|
218 | 218 | def raise_error(self): |
|
219 | 219 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
220 | 220 | if self.error_before_exec is not None: |
|
221 | 221 | raise self.error_before_exec |
|
222 | 222 | if self.error_in_exec is not None: |
|
223 | 223 | raise self.error_in_exec |
|
224 | 224 | |
|
225 | 225 | def __repr__(self): |
|
226 | 226 | name = self.__class__.__qualname__ |
|
227 | 227 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\ |
|
228 | 228 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result)) |
|
229 | 229 | |
|
230 | 230 | |
|
231 | 231 | class InteractiveShell(SingletonConfigurable): |
|
232 | 232 | """An enhanced, interactive shell for Python.""" |
|
233 | 233 | |
|
234 | 234 | _instance = None |
|
235 | 235 | |
|
236 | 236 | ast_transformers = List([], help= |
|
237 | 237 | """ |
|
238 | 238 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
239 | 239 | to user input before code is run. |
|
240 | 240 | """ |
|
241 | 241 | ).tag(config=True) |
|
242 | 242 | |
|
243 | 243 | autocall = Enum((0,1,2), default_value=0, help= |
|
244 | 244 | """ |
|
245 | 245 | Make IPython automatically call any callable object even if you didn't |
|
246 | 246 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
247 | 247 | automatically. The value can be '0' to disable the feature, '1' for |
|
248 | 248 | 'smart' autocall, where it is not applied if there are no more |
|
249 | 249 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
250 | 250 | objects are automatically called (even if no arguments are present). |
|
251 | 251 | """ |
|
252 | 252 | ).tag(config=True) |
|
253 | 253 | # TODO: remove all autoindent logic and put into frontends. |
|
254 | 254 | # We can't do this yet because even runlines uses the autoindent. |
|
255 | 255 | autoindent = Bool(True, help= |
|
256 | 256 | """ |
|
257 | 257 | Autoindent IPython code entered interactively. |
|
258 | 258 | """ |
|
259 | 259 | ).tag(config=True) |
|
260 | 260 | |
|
261 | 261 | automagic = Bool(True, help= |
|
262 | 262 | """ |
|
263 | 263 | Enable magic commands to be called without the leading %. |
|
264 | 264 | """ |
|
265 | 265 | ).tag(config=True) |
|
266 | 266 | |
|
267 | 267 | banner1 = Unicode(default_banner, |
|
268 | 268 | help="""The part of the banner to be printed before the profile""" |
|
269 | 269 | ).tag(config=True) |
|
270 | 270 | banner2 = Unicode('', |
|
271 | 271 | help="""The part of the banner to be printed after the profile""" |
|
272 | 272 | ).tag(config=True) |
|
273 | 273 | |
|
274 | 274 | cache_size = Integer(1000, help= |
|
275 | 275 | """ |
|
276 | 276 | Set the size of the output cache. The default is 1000, you can |
|
277 | 277 | change it permanently in your config file. Setting it to 0 completely |
|
278 | 278 | disables the caching system, and the minimum value accepted is 3 (if |
|
279 | 279 | you provide a value less than 3, it is reset to 0 and a warning is |
|
280 | 280 | issued). This limit is defined because otherwise you'll spend more |
|
281 | 281 | time re-flushing a too small cache than working |
|
282 | 282 | """ |
|
283 | 283 | ).tag(config=True) |
|
284 | 284 | color_info = Bool(True, help= |
|
285 | 285 | """ |
|
286 | 286 | Use colors for displaying information about objects. Because this |
|
287 | 287 | information is passed through a pager (like 'less'), and some pagers |
|
288 | 288 | get confused with color codes, this capability can be turned off. |
|
289 | 289 | """ |
|
290 | 290 | ).tag(config=True) |
|
291 | 291 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), |
|
292 | 292 | default_value='Neutral', |
|
293 | 293 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." |
|
294 | 294 | ).tag(config=True) |
|
295 | 295 | debug = Bool(False).tag(config=True) |
|
296 | 296 | disable_failing_post_execute = Bool(False, |
|
297 | 297 | help="Don't call post-execute functions that have failed in the past." |
|
298 | 298 | ).tag(config=True) |
|
299 | 299 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
300 | 300 | displayhook_class = Type(DisplayHook) |
|
301 | 301 | display_pub_class = Type(DisplayPublisher) |
|
302 | 302 | |
|
303 | 303 | sphinxify_docstring = Bool(False, help= |
|
304 | 304 | """ |
|
305 | 305 | Enables rich html representation of docstrings. (This requires the |
|
306 | 306 | docrepr module). |
|
307 | 307 | """).tag(config=True) |
|
308 | 308 | |
|
309 | 309 | @observe("sphinxify_docstring") |
|
310 | 310 | def _sphinxify_docstring_changed(self, change): |
|
311 | 311 | if change['new']: |
|
312 | 312 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) |
|
313 | 313 | |
|
314 | 314 | enable_html_pager = Bool(False, help= |
|
315 | 315 | """ |
|
316 | 316 | (Provisional API) enables html representation in mime bundles sent |
|
317 | 317 | to pagers. |
|
318 | 318 | """).tag(config=True) |
|
319 | 319 | |
|
320 | 320 | @observe("enable_html_pager") |
|
321 | 321 | def _enable_html_pager_changed(self, change): |
|
322 | 322 | if change['new']: |
|
323 | 323 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) |
|
324 | 324 | |
|
325 | 325 | data_pub_class = None |
|
326 | 326 | |
|
327 | 327 | exit_now = Bool(False) |
|
328 | 328 | exiter = Instance(ExitAutocall) |
|
329 | 329 | @default('exiter') |
|
330 | 330 | def _exiter_default(self): |
|
331 | 331 | return ExitAutocall(self) |
|
332 | 332 | # Monotonically increasing execution counter |
|
333 | 333 | execution_count = Integer(1) |
|
334 | 334 | filename = Unicode("<ipython console>") |
|
335 | 335 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
336 | 336 | |
|
337 | 337 | # Input splitter, to transform input line by line and detect when a block |
|
338 | 338 | # is ready to be executed. |
|
339 | 339 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
340 | 340 | (), {'line_input_checker': True}) |
|
341 | 341 | |
|
342 | 342 | # This InputSplitter instance is used to transform completed cells before |
|
343 | 343 | # running them. It allows cell magics to contain blank lines. |
|
344 | 344 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
345 | 345 | (), {'line_input_checker': False}) |
|
346 | 346 | |
|
347 | 347 | logstart = Bool(False, help= |
|
348 | 348 | """ |
|
349 | 349 | Start logging to the default log file in overwrite mode. |
|
350 | 350 | Use `logappend` to specify a log file to **append** logs to. |
|
351 | 351 | """ |
|
352 | 352 | ).tag(config=True) |
|
353 | 353 | logfile = Unicode('', help= |
|
354 | 354 | """ |
|
355 | 355 | The name of the logfile to use. |
|
356 | 356 | """ |
|
357 | 357 | ).tag(config=True) |
|
358 | 358 | logappend = Unicode('', help= |
|
359 | 359 | """ |
|
360 | 360 | Start logging to the given file in append mode. |
|
361 | 361 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
362 | 362 | """ |
|
363 | 363 | ).tag(config=True) |
|
364 | 364 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
365 | 365 | ).tag(config=True) |
|
366 | 366 | pdb = Bool(False, help= |
|
367 | 367 | """ |
|
368 | 368 | Automatically call the pdb debugger after every exception. |
|
369 | 369 | """ |
|
370 | 370 | ).tag(config=True) |
|
371 | 371 | display_page = Bool(False, |
|
372 | 372 | help="""If True, anything that would be passed to the pager |
|
373 | 373 | will be displayed as regular output instead.""" |
|
374 | 374 | ).tag(config=True) |
|
375 | 375 | |
|
376 | 376 | # deprecated prompt traits: |
|
377 | 377 | |
|
378 | 378 | prompt_in1 = Unicode('In [\\#]: ', |
|
379 | 379 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
380 | 380 | ).tag(config=True) |
|
381 | 381 | prompt_in2 = Unicode(' .\\D.: ', |
|
382 | 382 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
383 | 383 | ).tag(config=True) |
|
384 | 384 | prompt_out = Unicode('Out[\\#]: ', |
|
385 | 385 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
386 | 386 | ).tag(config=True) |
|
387 | 387 | prompts_pad_left = Bool(True, |
|
388 | 388 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
389 | 389 | ).tag(config=True) |
|
390 | 390 | |
|
391 | 391 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') |
|
392 | 392 | def _prompt_trait_changed(self, change): |
|
393 | 393 | name = change['name'] |
|
394 | 394 | warn("InteractiveShell.{name} is deprecated since IPython 4.0" |
|
395 | 395 | " and ignored since 5.0, set TerminalInteractiveShell.prompts" |
|
396 | 396 | " object directly.".format(name=name)) |
|
397 | 397 | |
|
398 | 398 | # protect against weird cases where self.config may not exist: |
|
399 | 399 | |
|
400 | 400 | show_rewritten_input = Bool(True, |
|
401 | 401 | help="Show rewritten input, e.g. for autocall." |
|
402 | 402 | ).tag(config=True) |
|
403 | 403 | |
|
404 | 404 | quiet = Bool(False).tag(config=True) |
|
405 | 405 | |
|
406 | 406 | history_length = Integer(10000, |
|
407 | 407 | help='Total length of command history' |
|
408 | 408 | ).tag(config=True) |
|
409 | 409 | |
|
410 | 410 | history_load_length = Integer(1000, help= |
|
411 | 411 | """ |
|
412 | 412 | The number of saved history entries to be loaded |
|
413 | 413 | into the history buffer at startup. |
|
414 | 414 | """ |
|
415 | 415 | ).tag(config=True) |
|
416 | 416 | |
|
417 | 417 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'], |
|
418 | 418 | default_value='last_expr', |
|
419 | 419 | help=""" |
|
420 | 420 | 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying |
|
421 | 421 | which nodes should be run interactively (displaying output from expressions). |
|
422 | 422 | """ |
|
423 | 423 | ).tag(config=True) |
|
424 | 424 | |
|
425 | 425 | # TODO: this part of prompt management should be moved to the frontends. |
|
426 | 426 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
427 | 427 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
428 | 428 | separate_out = SeparateUnicode('').tag(config=True) |
|
429 | 429 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
430 | 430 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
431 | 431 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
432 | 432 | default_value='Context', |
|
433 | 433 | help="Switch modes for the IPython exception handlers." |
|
434 | 434 | ).tag(config=True) |
|
435 | 435 | |
|
436 | 436 | # Subcomponents of InteractiveShell |
|
437 | 437 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
438 | 438 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
439 | 439 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
440 | 440 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
441 | 441 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
442 | 442 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
443 | 443 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
444 | 444 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
445 | 445 | |
|
446 | 446 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
447 | 447 | @property |
|
448 | 448 | def profile(self): |
|
449 | 449 | if self.profile_dir is not None: |
|
450 | 450 | name = os.path.basename(self.profile_dir.location) |
|
451 | 451 | return name.replace('profile_','') |
|
452 | 452 | |
|
453 | 453 | |
|
454 | 454 | # Private interface |
|
455 | 455 | _post_execute = Dict() |
|
456 | 456 | |
|
457 | 457 | # Tracks any GUI loop loaded for pylab |
|
458 | 458 | pylab_gui_select = None |
|
459 | 459 | |
|
460 | 460 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') |
|
461 | 461 | |
|
462 | 462 | last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True) |
|
463 | 463 | |
|
464 | 464 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
465 | 465 | user_module=None, user_ns=None, |
|
466 | 466 | custom_exceptions=((), None), **kwargs): |
|
467 | 467 | |
|
468 | 468 | # This is where traits with a config_key argument are updated |
|
469 | 469 | # from the values on config. |
|
470 | 470 | super(InteractiveShell, self).__init__(**kwargs) |
|
471 | 471 | if 'PromptManager' in self.config: |
|
472 | 472 | warn('As of IPython 5.0 `PromptManager` config will have no effect' |
|
473 | 473 | ' and has been replaced by TerminalInteractiveShell.prompts_class') |
|
474 | 474 | self.configurables = [self] |
|
475 | 475 | |
|
476 | 476 | # These are relatively independent and stateless |
|
477 | 477 | self.init_ipython_dir(ipython_dir) |
|
478 | 478 | self.init_profile_dir(profile_dir) |
|
479 | 479 | self.init_instance_attrs() |
|
480 | 480 | self.init_environment() |
|
481 | 481 | |
|
482 | 482 | # Check if we're in a virtualenv, and set up sys.path. |
|
483 | 483 | self.init_virtualenv() |
|
484 | 484 | |
|
485 | 485 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
486 | 486 | self.init_create_namespaces(user_module, user_ns) |
|
487 | 487 | # This has to be done after init_create_namespaces because it uses |
|
488 | 488 | # something in self.user_ns, but before init_sys_modules, which |
|
489 | 489 | # is the first thing to modify sys. |
|
490 | 490 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
491 | 491 | # is created, we are saving the overridden ones here. Not sure if this |
|
492 | 492 | # is what we want to do. |
|
493 | 493 | self.save_sys_module_state() |
|
494 | 494 | self.init_sys_modules() |
|
495 | 495 | |
|
496 | 496 | # While we're trying to have each part of the code directly access what |
|
497 | 497 | # it needs without keeping redundant references to objects, we have too |
|
498 | 498 | # much legacy code that expects ip.db to exist. |
|
499 | 499 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
500 | 500 | |
|
501 | 501 | self.init_history() |
|
502 | 502 | self.init_encoding() |
|
503 | 503 | self.init_prefilter() |
|
504 | 504 | |
|
505 | 505 | self.init_syntax_highlighting() |
|
506 | 506 | self.init_hooks() |
|
507 | 507 | self.init_events() |
|
508 | 508 | self.init_pushd_popd_magic() |
|
509 | 509 | self.init_user_ns() |
|
510 | 510 | self.init_logger() |
|
511 | 511 | self.init_builtins() |
|
512 | 512 | |
|
513 | 513 | # The following was in post_config_initialization |
|
514 | 514 | self.init_inspector() |
|
515 | 515 | self.raw_input_original = input |
|
516 | 516 | self.init_completer() |
|
517 | 517 | # TODO: init_io() needs to happen before init_traceback handlers |
|
518 | 518 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
519 | 519 | # This logic in in debugger.Pdb and should eventually be changed. |
|
520 | 520 | self.init_io() |
|
521 | 521 | self.init_traceback_handlers(custom_exceptions) |
|
522 | 522 | self.init_prompts() |
|
523 | 523 | self.init_display_formatter() |
|
524 | 524 | self.init_display_pub() |
|
525 | 525 | self.init_data_pub() |
|
526 | 526 | self.init_displayhook() |
|
527 | 527 | self.init_magics() |
|
528 | 528 | self.init_alias() |
|
529 | 529 | self.init_logstart() |
|
530 | 530 | self.init_pdb() |
|
531 | 531 | self.init_extension_manager() |
|
532 | 532 | self.init_payload() |
|
533 | 533 | self.init_deprecation_warnings() |
|
534 | 534 | self.hooks.late_startup_hook() |
|
535 | 535 | self.events.trigger('shell_initialized', self) |
|
536 | 536 | atexit.register(self.atexit_operations) |
|
537 | 537 | |
|
538 | 538 | def get_ipython(self): |
|
539 | 539 | """Return the currently running IPython instance.""" |
|
540 | 540 | return self |
|
541 | 541 | |
|
542 | 542 | #------------------------------------------------------------------------- |
|
543 | 543 | # Trait changed handlers |
|
544 | 544 | #------------------------------------------------------------------------- |
|
545 | 545 | @observe('ipython_dir') |
|
546 | 546 | def _ipython_dir_changed(self, change): |
|
547 | 547 | ensure_dir_exists(change['new']) |
|
548 | 548 | |
|
549 | 549 | def set_autoindent(self,value=None): |
|
550 | 550 | """Set the autoindent flag. |
|
551 | 551 | |
|
552 | 552 | If called with no arguments, it acts as a toggle.""" |
|
553 | 553 | if value is None: |
|
554 | 554 | self.autoindent = not self.autoindent |
|
555 | 555 | else: |
|
556 | 556 | self.autoindent = value |
|
557 | 557 | |
|
558 | 558 | #------------------------------------------------------------------------- |
|
559 | 559 | # init_* methods called by __init__ |
|
560 | 560 | #------------------------------------------------------------------------- |
|
561 | 561 | |
|
562 | 562 | def init_ipython_dir(self, ipython_dir): |
|
563 | 563 | if ipython_dir is not None: |
|
564 | 564 | self.ipython_dir = ipython_dir |
|
565 | 565 | return |
|
566 | 566 | |
|
567 | 567 | self.ipython_dir = get_ipython_dir() |
|
568 | 568 | |
|
569 | 569 | def init_profile_dir(self, profile_dir): |
|
570 | 570 | if profile_dir is not None: |
|
571 | 571 | self.profile_dir = profile_dir |
|
572 | 572 | return |
|
573 | 573 | self.profile_dir =\ |
|
574 | 574 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
575 | 575 | |
|
576 | 576 | def init_instance_attrs(self): |
|
577 | 577 | self.more = False |
|
578 | 578 | |
|
579 | 579 | # command compiler |
|
580 | 580 | self.compile = CachingCompiler() |
|
581 | 581 | |
|
582 | 582 | # Make an empty namespace, which extension writers can rely on both |
|
583 | 583 | # existing and NEVER being used by ipython itself. This gives them a |
|
584 | 584 | # convenient location for storing additional information and state |
|
585 | 585 | # their extensions may require, without fear of collisions with other |
|
586 | 586 | # ipython names that may develop later. |
|
587 | 587 | self.meta = Struct() |
|
588 | 588 | |
|
589 | 589 | # Temporary files used for various purposes. Deleted at exit. |
|
590 | 590 | self.tempfiles = [] |
|
591 | 591 | self.tempdirs = [] |
|
592 | 592 | |
|
593 | 593 | # keep track of where we started running (mainly for crash post-mortem) |
|
594 | 594 | # This is not being used anywhere currently. |
|
595 | 595 | self.starting_dir = os.getcwd() |
|
596 | 596 | |
|
597 | 597 | # Indentation management |
|
598 | 598 | self.indent_current_nsp = 0 |
|
599 | 599 | |
|
600 | 600 | # Dict to track post-execution functions that have been registered |
|
601 | 601 | self._post_execute = {} |
|
602 | 602 | |
|
603 | 603 | def init_environment(self): |
|
604 | 604 | """Any changes we need to make to the user's environment.""" |
|
605 | 605 | pass |
|
606 | 606 | |
|
607 | 607 | def init_encoding(self): |
|
608 | 608 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
609 | 609 | # under Win32 have it set to None, and we need to have a known valid |
|
610 | 610 | # encoding to use in the raw_input() method |
|
611 | 611 | try: |
|
612 | 612 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
613 | 613 | except AttributeError: |
|
614 | 614 | self.stdin_encoding = 'ascii' |
|
615 | 615 | |
|
616 | 616 | |
|
617 | 617 | @observe('colors') |
|
618 | 618 | def init_syntax_highlighting(self, changes=None): |
|
619 | 619 | # Python source parser/formatter for syntax highlighting |
|
620 | 620 | pyformat = PyColorize.Parser(style=self.colors, parent=self).format |
|
621 | 621 | self.pycolorize = lambda src: pyformat(src,'str') |
|
622 | 622 | |
|
623 | 623 | def refresh_style(self): |
|
624 | 624 | # No-op here, used in subclass |
|
625 | 625 | pass |
|
626 | 626 | |
|
627 | 627 | def init_pushd_popd_magic(self): |
|
628 | 628 | # for pushd/popd management |
|
629 | 629 | self.home_dir = get_home_dir() |
|
630 | 630 | |
|
631 | 631 | self.dir_stack = [] |
|
632 | 632 | |
|
633 | 633 | def init_logger(self): |
|
634 | 634 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
635 | 635 | logmode='rotate') |
|
636 | 636 | |
|
637 | 637 | def init_logstart(self): |
|
638 | 638 | """Initialize logging in case it was requested at the command line. |
|
639 | 639 | """ |
|
640 | 640 | if self.logappend: |
|
641 | 641 | self.magic('logstart %s append' % self.logappend) |
|
642 | 642 | elif self.logfile: |
|
643 | 643 | self.magic('logstart %s' % self.logfile) |
|
644 | 644 | elif self.logstart: |
|
645 | 645 | self.magic('logstart') |
|
646 | 646 | |
|
647 | 647 | def init_deprecation_warnings(self): |
|
648 | 648 | """ |
|
649 | 649 | register default filter for deprecation warning. |
|
650 | 650 | |
|
651 | 651 | This will allow deprecation warning of function used interactively to show |
|
652 | 652 | warning to users, and still hide deprecation warning from libraries import. |
|
653 | 653 | """ |
|
654 | 654 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) |
|
655 | 655 | |
|
656 | 656 | def init_builtins(self): |
|
657 | 657 | # A single, static flag that we set to True. Its presence indicates |
|
658 | 658 | # that an IPython shell has been created, and we make no attempts at |
|
659 | 659 | # removing on exit or representing the existence of more than one |
|
660 | 660 | # IPython at a time. |
|
661 | 661 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
662 | 662 | builtin_mod.__dict__['display'] = display |
|
663 | 663 | |
|
664 | 664 | self.builtin_trap = BuiltinTrap(shell=self) |
|
665 | 665 | |
|
666 | 666 | @observe('colors') |
|
667 | 667 | def init_inspector(self, changes=None): |
|
668 | 668 | # Object inspector |
|
669 | 669 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
670 | 670 | PyColorize.ANSICodeColors, |
|
671 | 671 | self.colors, |
|
672 | 672 | self.object_info_string_level) |
|
673 | 673 | |
|
674 | 674 | def init_io(self): |
|
675 | 675 | # This will just use sys.stdout and sys.stderr. If you want to |
|
676 | 676 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
677 | 677 | # *before* instantiating this class, because io holds onto |
|
678 | 678 | # references to the underlying streams. |
|
679 | 679 | # io.std* are deprecated, but don't show our own deprecation warnings |
|
680 | 680 | # during initialization of the deprecated API. |
|
681 | 681 | with warnings.catch_warnings(): |
|
682 | 682 | warnings.simplefilter('ignore', DeprecationWarning) |
|
683 | 683 | io.stdout = io.IOStream(sys.stdout) |
|
684 | 684 | io.stderr = io.IOStream(sys.stderr) |
|
685 | 685 | |
|
686 | 686 | def init_prompts(self): |
|
687 | 687 | # Set system prompts, so that scripts can decide if they are running |
|
688 | 688 | # interactively. |
|
689 | 689 | sys.ps1 = 'In : ' |
|
690 | 690 | sys.ps2 = '...: ' |
|
691 | 691 | sys.ps3 = 'Out: ' |
|
692 | 692 | |
|
693 | 693 | def init_display_formatter(self): |
|
694 | 694 | self.display_formatter = DisplayFormatter(parent=self) |
|
695 | 695 | self.configurables.append(self.display_formatter) |
|
696 | 696 | |
|
697 | 697 | def init_display_pub(self): |
|
698 | 698 | self.display_pub = self.display_pub_class(parent=self) |
|
699 | 699 | self.configurables.append(self.display_pub) |
|
700 | 700 | |
|
701 | 701 | def init_data_pub(self): |
|
702 | 702 | if not self.data_pub_class: |
|
703 | 703 | self.data_pub = None |
|
704 | 704 | return |
|
705 | 705 | self.data_pub = self.data_pub_class(parent=self) |
|
706 | 706 | self.configurables.append(self.data_pub) |
|
707 | 707 | |
|
708 | 708 | def init_displayhook(self): |
|
709 | 709 | # Initialize displayhook, set in/out prompts and printing system |
|
710 | 710 | self.displayhook = self.displayhook_class( |
|
711 | 711 | parent=self, |
|
712 | 712 | shell=self, |
|
713 | 713 | cache_size=self.cache_size, |
|
714 | 714 | ) |
|
715 | 715 | self.configurables.append(self.displayhook) |
|
716 | 716 | # This is a context manager that installs/revmoes the displayhook at |
|
717 | 717 | # the appropriate time. |
|
718 | 718 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
719 | 719 | |
|
720 | 720 | def init_virtualenv(self): |
|
721 | 721 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
722 | 722 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
723 | 723 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
724 | 724 | warning will appear suggesting the user installs IPython in the |
|
725 | 725 | virtualenv, but for many cases, it probably works well enough. |
|
726 | 726 | |
|
727 | 727 | Adapted from code snippets online. |
|
728 | 728 | |
|
729 | 729 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
730 | 730 | """ |
|
731 | 731 | if 'VIRTUAL_ENV' not in os.environ: |
|
732 | 732 | # Not in a virtualenv |
|
733 | 733 | return |
|
734 | 734 | |
|
735 | 735 | # venv detection: |
|
736 | 736 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
737 | 737 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
738 | 738 | # So we just check every item in the symlink tree (generally <= 3) |
|
739 | 739 | p = os.path.normcase(sys.executable) |
|
740 | 740 | paths = [p] |
|
741 | 741 | while os.path.islink(p): |
|
742 | 742 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
|
743 | 743 | paths.append(p) |
|
744 | 744 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
|
745 | 745 | |
|
746 | 746 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible |
|
747 | 747 | if p_venv.startswith('\\cygdrive'): |
|
748 | 748 | p_venv = p_venv[11:] |
|
749 | 749 | elif len(p_venv) >= 2 and p_venv[1] == ':': |
|
750 | 750 | p_venv = p_venv[2:] |
|
751 | 751 | |
|
752 | 752 | if any(p_venv in p for p in paths): |
|
753 | 753 | # Running properly in the virtualenv, don't need to do anything |
|
754 | 754 | return |
|
755 | 755 | |
|
756 | 756 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
757 | 757 | "install IPython inside the virtualenv.") |
|
758 | 758 | if sys.platform == "win32": |
|
759 | 759 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
760 | 760 | else: |
|
761 | 761 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
762 | 762 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
763 | 763 | |
|
764 | 764 | import site |
|
765 | 765 | sys.path.insert(0, virtual_env) |
|
766 | 766 | site.addsitedir(virtual_env) |
|
767 | 767 | |
|
768 | 768 | #------------------------------------------------------------------------- |
|
769 | 769 | # Things related to injections into the sys module |
|
770 | 770 | #------------------------------------------------------------------------- |
|
771 | 771 | |
|
772 | 772 | def save_sys_module_state(self): |
|
773 | 773 | """Save the state of hooks in the sys module. |
|
774 | 774 | |
|
775 | 775 | This has to be called after self.user_module is created. |
|
776 | 776 | """ |
|
777 | 777 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
778 | 778 | 'stdout': sys.stdout, |
|
779 | 779 | 'stderr': sys.stderr, |
|
780 | 780 | 'excepthook': sys.excepthook} |
|
781 | 781 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
782 | 782 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
783 | 783 | |
|
784 | 784 | def restore_sys_module_state(self): |
|
785 | 785 | """Restore the state of the sys module.""" |
|
786 | 786 | try: |
|
787 | 787 | for k, v in self._orig_sys_module_state.items(): |
|
788 | 788 | setattr(sys, k, v) |
|
789 | 789 | except AttributeError: |
|
790 | 790 | pass |
|
791 | 791 | # Reset what what done in self.init_sys_modules |
|
792 | 792 | if self._orig_sys_modules_main_mod is not None: |
|
793 | 793 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
794 | 794 | |
|
795 | 795 | #------------------------------------------------------------------------- |
|
796 | 796 | # Things related to the banner |
|
797 | 797 | #------------------------------------------------------------------------- |
|
798 | 798 | |
|
799 | 799 | @property |
|
800 | 800 | def banner(self): |
|
801 | 801 | banner = self.banner1 |
|
802 | 802 | if self.profile and self.profile != 'default': |
|
803 | 803 | banner += '\nIPython profile: %s\n' % self.profile |
|
804 | 804 | if self.banner2: |
|
805 | 805 | banner += '\n' + self.banner2 |
|
806 | 806 | return banner |
|
807 | 807 | |
|
808 | 808 | def show_banner(self, banner=None): |
|
809 | 809 | if banner is None: |
|
810 | 810 | banner = self.banner |
|
811 | 811 | sys.stdout.write(banner) |
|
812 | 812 | |
|
813 | 813 | #------------------------------------------------------------------------- |
|
814 | 814 | # Things related to hooks |
|
815 | 815 | #------------------------------------------------------------------------- |
|
816 | 816 | |
|
817 | 817 | def init_hooks(self): |
|
818 | 818 | # hooks holds pointers used for user-side customizations |
|
819 | 819 | self.hooks = Struct() |
|
820 | 820 | |
|
821 | 821 | self.strdispatchers = {} |
|
822 | 822 | |
|
823 | 823 | # Set all default hooks, defined in the IPython.hooks module. |
|
824 | 824 | hooks = IPython.core.hooks |
|
825 | 825 | for hook_name in hooks.__all__: |
|
826 | 826 | # default hooks have priority 100, i.e. low; user hooks should have |
|
827 | 827 | # 0-100 priority |
|
828 | 828 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
829 | 829 | |
|
830 | 830 | if self.display_page: |
|
831 | 831 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
832 | 832 | |
|
833 | 833 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
834 | 834 | _warn_deprecated=True): |
|
835 | 835 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
836 | 836 | |
|
837 | 837 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
838 | 838 | adding your function to one of these hooks, you can modify IPython's |
|
839 | 839 | behavior to call at runtime your own routines.""" |
|
840 | 840 | |
|
841 | 841 | # At some point in the future, this should validate the hook before it |
|
842 | 842 | # accepts it. Probably at least check that the hook takes the number |
|
843 | 843 | # of args it's supposed to. |
|
844 | 844 | |
|
845 | 845 | f = types.MethodType(hook,self) |
|
846 | 846 | |
|
847 | 847 | # check if the hook is for strdispatcher first |
|
848 | 848 | if str_key is not None: |
|
849 | 849 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
850 | 850 | sdp.add_s(str_key, f, priority ) |
|
851 | 851 | self.strdispatchers[name] = sdp |
|
852 | 852 | return |
|
853 | 853 | if re_key is not None: |
|
854 | 854 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
855 | 855 | sdp.add_re(re.compile(re_key), f, priority ) |
|
856 | 856 | self.strdispatchers[name] = sdp |
|
857 | 857 | return |
|
858 | 858 | |
|
859 | 859 | dp = getattr(self.hooks, name, None) |
|
860 | 860 | if name not in IPython.core.hooks.__all__: |
|
861 | 861 | print("Warning! Hook '%s' is not one of %s" % \ |
|
862 | 862 | (name, IPython.core.hooks.__all__ )) |
|
863 | 863 | |
|
864 | 864 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
865 | 865 | alternative = IPython.core.hooks.deprecated[name] |
|
866 | 866 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative), stacklevel=2) |
|
867 | 867 | |
|
868 | 868 | if not dp: |
|
869 | 869 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
870 | 870 | |
|
871 | 871 | try: |
|
872 | 872 | dp.add(f,priority) |
|
873 | 873 | except AttributeError: |
|
874 | 874 | # it was not commandchain, plain old func - replace |
|
875 | 875 | dp = f |
|
876 | 876 | |
|
877 | 877 | setattr(self.hooks,name, dp) |
|
878 | 878 | |
|
879 | 879 | #------------------------------------------------------------------------- |
|
880 | 880 | # Things related to events |
|
881 | 881 | #------------------------------------------------------------------------- |
|
882 | 882 | |
|
883 | 883 | def init_events(self): |
|
884 | 884 | self.events = EventManager(self, available_events) |
|
885 | 885 | |
|
886 | 886 | self.events.register("pre_execute", self._clear_warning_registry) |
|
887 | 887 | |
|
888 | 888 | def register_post_execute(self, func): |
|
889 | 889 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
890 | 890 | |
|
891 | 891 | Register a function for calling after code execution. |
|
892 | 892 | """ |
|
893 | 893 | warn("ip.register_post_execute is deprecated, use " |
|
894 | 894 | "ip.events.register('post_run_cell', func) instead.", stacklevel=2) |
|
895 | 895 | self.events.register('post_run_cell', func) |
|
896 | 896 | |
|
897 | 897 | def _clear_warning_registry(self): |
|
898 | 898 | # clear the warning registry, so that different code blocks with |
|
899 | 899 | # overlapping line number ranges don't cause spurious suppression of |
|
900 | 900 | # warnings (see gh-6611 for details) |
|
901 | 901 | if "__warningregistry__" in self.user_global_ns: |
|
902 | 902 | del self.user_global_ns["__warningregistry__"] |
|
903 | 903 | |
|
904 | 904 | #------------------------------------------------------------------------- |
|
905 | 905 | # Things related to the "main" module |
|
906 | 906 | #------------------------------------------------------------------------- |
|
907 | 907 | |
|
908 | 908 | def new_main_mod(self, filename, modname): |
|
909 | 909 | """Return a new 'main' module object for user code execution. |
|
910 | 910 | |
|
911 | 911 | ``filename`` should be the path of the script which will be run in the |
|
912 | 912 | module. Requests with the same filename will get the same module, with |
|
913 | 913 | its namespace cleared. |
|
914 | 914 | |
|
915 | 915 | ``modname`` should be the module name - normally either '__main__' or |
|
916 | 916 | the basename of the file without the extension. |
|
917 | 917 | |
|
918 | 918 | When scripts are executed via %run, we must keep a reference to their |
|
919 | 919 | __main__ module around so that Python doesn't |
|
920 | 920 | clear it, rendering references to module globals useless. |
|
921 | 921 | |
|
922 | 922 | This method keeps said reference in a private dict, keyed by the |
|
923 | 923 | absolute path of the script. This way, for multiple executions of the |
|
924 | 924 | same script we only keep one copy of the namespace (the last one), |
|
925 | 925 | thus preventing memory leaks from old references while allowing the |
|
926 | 926 | objects from the last execution to be accessible. |
|
927 | 927 | """ |
|
928 | 928 | filename = os.path.abspath(filename) |
|
929 | 929 | try: |
|
930 | 930 | main_mod = self._main_mod_cache[filename] |
|
931 | 931 | except KeyError: |
|
932 | 932 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
933 | 933 | modname, |
|
934 | 934 | doc="Module created for script run in IPython") |
|
935 | 935 | else: |
|
936 | 936 | main_mod.__dict__.clear() |
|
937 | 937 | main_mod.__name__ = modname |
|
938 | 938 | |
|
939 | 939 | main_mod.__file__ = filename |
|
940 | 940 | # It seems pydoc (and perhaps others) needs any module instance to |
|
941 | 941 | # implement a __nonzero__ method |
|
942 | 942 | main_mod.__nonzero__ = lambda : True |
|
943 | 943 | |
|
944 | 944 | return main_mod |
|
945 | 945 | |
|
946 | 946 | def clear_main_mod_cache(self): |
|
947 | 947 | """Clear the cache of main modules. |
|
948 | 948 | |
|
949 | 949 | Mainly for use by utilities like %reset. |
|
950 | 950 | |
|
951 | 951 | Examples |
|
952 | 952 | -------- |
|
953 | 953 | |
|
954 | 954 | In [15]: import IPython |
|
955 | 955 | |
|
956 | 956 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
957 | 957 | |
|
958 | 958 | In [17]: len(_ip._main_mod_cache) > 0 |
|
959 | 959 | Out[17]: True |
|
960 | 960 | |
|
961 | 961 | In [18]: _ip.clear_main_mod_cache() |
|
962 | 962 | |
|
963 | 963 | In [19]: len(_ip._main_mod_cache) == 0 |
|
964 | 964 | Out[19]: True |
|
965 | 965 | """ |
|
966 | 966 | self._main_mod_cache.clear() |
|
967 | 967 | |
|
968 | 968 | #------------------------------------------------------------------------- |
|
969 | 969 | # Things related to debugging |
|
970 | 970 | #------------------------------------------------------------------------- |
|
971 | 971 | |
|
972 | 972 | def init_pdb(self): |
|
973 | 973 | # Set calling of pdb on exceptions |
|
974 | 974 | # self.call_pdb is a property |
|
975 | 975 | self.call_pdb = self.pdb |
|
976 | 976 | |
|
977 | 977 | def _get_call_pdb(self): |
|
978 | 978 | return self._call_pdb |
|
979 | 979 | |
|
980 | 980 | def _set_call_pdb(self,val): |
|
981 | 981 | |
|
982 | 982 | if val not in (0,1,False,True): |
|
983 | 983 | raise ValueError('new call_pdb value must be boolean') |
|
984 | 984 | |
|
985 | 985 | # store value in instance |
|
986 | 986 | self._call_pdb = val |
|
987 | 987 | |
|
988 | 988 | # notify the actual exception handlers |
|
989 | 989 | self.InteractiveTB.call_pdb = val |
|
990 | 990 | |
|
991 | 991 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
992 | 992 | 'Control auto-activation of pdb at exceptions') |
|
993 | 993 | |
|
994 | 994 | def debugger(self,force=False): |
|
995 | 995 | """Call the pdb debugger. |
|
996 | 996 | |
|
997 | 997 | Keywords: |
|
998 | 998 | |
|
999 | 999 | - force(False): by default, this routine checks the instance call_pdb |
|
1000 | 1000 | flag and does not actually invoke the debugger if the flag is false. |
|
1001 | 1001 | The 'force' option forces the debugger to activate even if the flag |
|
1002 | 1002 | is false. |
|
1003 | 1003 | """ |
|
1004 | 1004 | |
|
1005 | 1005 | if not (force or self.call_pdb): |
|
1006 | 1006 | return |
|
1007 | 1007 | |
|
1008 | 1008 | if not hasattr(sys,'last_traceback'): |
|
1009 | 1009 | error('No traceback has been produced, nothing to debug.') |
|
1010 | 1010 | return |
|
1011 | 1011 | |
|
1012 | 1012 | self.InteractiveTB.debugger(force=True) |
|
1013 | 1013 | |
|
1014 | 1014 | #------------------------------------------------------------------------- |
|
1015 | 1015 | # Things related to IPython's various namespaces |
|
1016 | 1016 | #------------------------------------------------------------------------- |
|
1017 | 1017 | default_user_namespaces = True |
|
1018 | 1018 | |
|
1019 | 1019 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
1020 | 1020 | # Create the namespace where the user will operate. user_ns is |
|
1021 | 1021 | # normally the only one used, and it is passed to the exec calls as |
|
1022 | 1022 | # the locals argument. But we do carry a user_global_ns namespace |
|
1023 | 1023 | # given as the exec 'globals' argument, This is useful in embedding |
|
1024 | 1024 | # situations where the ipython shell opens in a context where the |
|
1025 | 1025 | # distinction between locals and globals is meaningful. For |
|
1026 | 1026 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1027 | 1027 | |
|
1028 | 1028 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1029 | 1029 | # level as a dict instead of a module. This is a manual fix, but I |
|
1030 | 1030 | # should really track down where the problem is coming from. Alex |
|
1031 | 1031 | # Schmolck reported this problem first. |
|
1032 | 1032 | |
|
1033 | 1033 | # A useful post by Alex Martelli on this topic: |
|
1034 | 1034 | # Re: inconsistent value from __builtins__ |
|
1035 | 1035 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1036 | 1036 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1037 | 1037 | # Gruppen: comp.lang.python |
|
1038 | 1038 | |
|
1039 | 1039 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1040 | 1040 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1041 | 1041 | # > <type 'dict'> |
|
1042 | 1042 | # > >>> print type(__builtins__) |
|
1043 | 1043 | # > <type 'module'> |
|
1044 | 1044 | # > Is this difference in return value intentional? |
|
1045 | 1045 | |
|
1046 | 1046 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1047 | 1047 | # or a module, and it's been that way for a long time. Whether it's |
|
1048 | 1048 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1049 | 1049 | # that if you need to access the built-in namespace directly, you |
|
1050 | 1050 | # should start with "import __builtin__" (note, no 's') which will |
|
1051 | 1051 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1052 | 1052 | |
|
1053 | 1053 | # These routines return a properly built module and dict as needed by |
|
1054 | 1054 | # the rest of the code, and can also be used by extension writers to |
|
1055 | 1055 | # generate properly initialized namespaces. |
|
1056 | 1056 | if (user_ns is not None) or (user_module is not None): |
|
1057 | 1057 | self.default_user_namespaces = False |
|
1058 | 1058 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1059 | 1059 | |
|
1060 | 1060 | # A record of hidden variables we have added to the user namespace, so |
|
1061 | 1061 | # we can list later only variables defined in actual interactive use. |
|
1062 | 1062 | self.user_ns_hidden = {} |
|
1063 | 1063 | |
|
1064 | 1064 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1065 | 1065 | # problem: after script execution (via %run), the module where the user |
|
1066 | 1066 | # code ran is deleted. Now that this object is a true module (needed |
|
1067 | 1067 | # so doctest and other tools work correctly), the Python module |
|
1068 | 1068 | # teardown mechanism runs over it, and sets to None every variable |
|
1069 | 1069 | # present in that module. Top-level references to objects from the |
|
1070 | 1070 | # script survive, because the user_ns is updated with them. However, |
|
1071 | 1071 | # calling functions defined in the script that use other things from |
|
1072 | 1072 | # the script will fail, because the function's closure had references |
|
1073 | 1073 | # to the original objects, which are now all None. So we must protect |
|
1074 | 1074 | # these modules from deletion by keeping a cache. |
|
1075 | 1075 | # |
|
1076 | 1076 | # To avoid keeping stale modules around (we only need the one from the |
|
1077 | 1077 | # last run), we use a dict keyed with the full path to the script, so |
|
1078 | 1078 | # only the last version of the module is held in the cache. Note, |
|
1079 | 1079 | # however, that we must cache the module *namespace contents* (their |
|
1080 | 1080 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1081 | 1081 | # (uncached) could be destroyed while still holding references (such as |
|
1082 | 1082 | # those held by GUI objects that tend to be long-lived)> |
|
1083 | 1083 | # |
|
1084 | 1084 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1085 | 1085 | # and clear_main_mod_cache() methods for details on use. |
|
1086 | 1086 | |
|
1087 | 1087 | # This is the cache used for 'main' namespaces |
|
1088 | 1088 | self._main_mod_cache = {} |
|
1089 | 1089 | |
|
1090 | 1090 | # A table holding all the namespaces IPython deals with, so that |
|
1091 | 1091 | # introspection facilities can search easily. |
|
1092 | 1092 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1093 | 1093 | 'user_local':self.user_ns, |
|
1094 | 1094 | 'builtin':builtin_mod.__dict__ |
|
1095 | 1095 | } |
|
1096 | 1096 | |
|
1097 | 1097 | @property |
|
1098 | 1098 | def user_global_ns(self): |
|
1099 | 1099 | return self.user_module.__dict__ |
|
1100 | 1100 | |
|
1101 | 1101 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1102 | 1102 | """Prepare the module and namespace in which user code will be run. |
|
1103 | 1103 | |
|
1104 | 1104 | When IPython is started normally, both parameters are None: a new module |
|
1105 | 1105 | is created automatically, and its __dict__ used as the namespace. |
|
1106 | 1106 | |
|
1107 | 1107 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1108 | 1108 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1109 | 1109 | becomes the global namespace. If both are provided (as they may be |
|
1110 | 1110 | when embedding), user_ns is the local namespace, and user_module |
|
1111 | 1111 | provides the global namespace. |
|
1112 | 1112 | |
|
1113 | 1113 | Parameters |
|
1114 | 1114 | ---------- |
|
1115 | 1115 | user_module : module, optional |
|
1116 | 1116 | The current user module in which IPython is being run. If None, |
|
1117 | 1117 | a clean module will be created. |
|
1118 | 1118 | user_ns : dict, optional |
|
1119 | 1119 | A namespace in which to run interactive commands. |
|
1120 | 1120 | |
|
1121 | 1121 | Returns |
|
1122 | 1122 | ------- |
|
1123 | 1123 | A tuple of user_module and user_ns, each properly initialised. |
|
1124 | 1124 | """ |
|
1125 | 1125 | if user_module is None and user_ns is not None: |
|
1126 | 1126 | user_ns.setdefault("__name__", "__main__") |
|
1127 | 1127 | user_module = DummyMod() |
|
1128 | 1128 | user_module.__dict__ = user_ns |
|
1129 | 1129 | |
|
1130 | 1130 | if user_module is None: |
|
1131 | 1131 | user_module = types.ModuleType("__main__", |
|
1132 | 1132 | doc="Automatically created module for IPython interactive environment") |
|
1133 | 1133 | |
|
1134 | 1134 | # We must ensure that __builtin__ (without the final 's') is always |
|
1135 | 1135 | # available and pointing to the __builtin__ *module*. For more details: |
|
1136 | 1136 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1137 | 1137 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1138 | 1138 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1139 | 1139 | |
|
1140 | 1140 | if user_ns is None: |
|
1141 | 1141 | user_ns = user_module.__dict__ |
|
1142 | 1142 | |
|
1143 | 1143 | return user_module, user_ns |
|
1144 | 1144 | |
|
1145 | 1145 | def init_sys_modules(self): |
|
1146 | 1146 | # We need to insert into sys.modules something that looks like a |
|
1147 | 1147 | # module but which accesses the IPython namespace, for shelve and |
|
1148 | 1148 | # pickle to work interactively. Normally they rely on getting |
|
1149 | 1149 | # everything out of __main__, but for embedding purposes each IPython |
|
1150 | 1150 | # instance has its own private namespace, so we can't go shoving |
|
1151 | 1151 | # everything into __main__. |
|
1152 | 1152 | |
|
1153 | 1153 | # note, however, that we should only do this for non-embedded |
|
1154 | 1154 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1155 | 1155 | # namespace. Embedded instances, on the other hand, should not do |
|
1156 | 1156 | # this because they need to manage the user local/global namespaces |
|
1157 | 1157 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1158 | 1158 | # shouldn't overtake the execution environment of the script they're |
|
1159 | 1159 | # embedded in). |
|
1160 | 1160 | |
|
1161 | 1161 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1162 | 1162 | main_name = self.user_module.__name__ |
|
1163 | 1163 | sys.modules[main_name] = self.user_module |
|
1164 | 1164 | |
|
1165 | 1165 | def init_user_ns(self): |
|
1166 | 1166 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1167 | 1167 | |
|
1168 | 1168 | Certain history lists are also initialized here, as they effectively |
|
1169 | 1169 | act as user namespaces. |
|
1170 | 1170 | |
|
1171 | 1171 | Notes |
|
1172 | 1172 | ----- |
|
1173 | 1173 | All data structures here are only filled in, they are NOT reset by this |
|
1174 | 1174 | method. If they were not empty before, data will simply be added to |
|
1175 | 1175 | therm. |
|
1176 | 1176 | """ |
|
1177 | 1177 | # This function works in two parts: first we put a few things in |
|
1178 | 1178 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1179 | 1179 | # initial variables aren't shown by %who. After the sync, we add the |
|
1180 | 1180 | # rest of what we *do* want the user to see with %who even on a new |
|
1181 | 1181 | # session (probably nothing, so they really only see their own stuff) |
|
1182 | 1182 | |
|
1183 | 1183 | # The user dict must *always* have a __builtin__ reference to the |
|
1184 | 1184 | # Python standard __builtin__ namespace, which must be imported. |
|
1185 | 1185 | # This is so that certain operations in prompt evaluation can be |
|
1186 | 1186 | # reliably executed with builtins. Note that we can NOT use |
|
1187 | 1187 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1188 | 1188 | # module, and can even mutate at runtime, depending on the context |
|
1189 | 1189 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1190 | 1190 | # always a module object, though it must be explicitly imported. |
|
1191 | 1191 | |
|
1192 | 1192 | # For more details: |
|
1193 | 1193 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1194 | 1194 | ns = {} |
|
1195 | 1195 | |
|
1196 | 1196 | # make global variables for user access to the histories |
|
1197 | 1197 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1198 | 1198 | ns['_oh'] = self.history_manager.output_hist |
|
1199 | 1199 | ns['_dh'] = self.history_manager.dir_hist |
|
1200 | 1200 | |
|
1201 | 1201 | # user aliases to input and output histories. These shouldn't show up |
|
1202 | 1202 | # in %who, as they can have very large reprs. |
|
1203 | 1203 | ns['In'] = self.history_manager.input_hist_parsed |
|
1204 | 1204 | ns['Out'] = self.history_manager.output_hist |
|
1205 | 1205 | |
|
1206 | 1206 | # Store myself as the public api!!! |
|
1207 | 1207 | ns['get_ipython'] = self.get_ipython |
|
1208 | 1208 | |
|
1209 | 1209 | ns['exit'] = self.exiter |
|
1210 | 1210 | ns['quit'] = self.exiter |
|
1211 | 1211 | |
|
1212 | 1212 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1213 | 1213 | # by %who |
|
1214 | 1214 | self.user_ns_hidden.update(ns) |
|
1215 | 1215 | |
|
1216 | 1216 | # Anything put into ns now would show up in %who. Think twice before |
|
1217 | 1217 | # putting anything here, as we really want %who to show the user their |
|
1218 | 1218 | # stuff, not our variables. |
|
1219 | 1219 | |
|
1220 | 1220 | # Finally, update the real user's namespace |
|
1221 | 1221 | self.user_ns.update(ns) |
|
1222 | 1222 | |
|
1223 | 1223 | @property |
|
1224 | 1224 | def all_ns_refs(self): |
|
1225 | 1225 | """Get a list of references to all the namespace dictionaries in which |
|
1226 | 1226 | IPython might store a user-created object. |
|
1227 | 1227 | |
|
1228 | 1228 | Note that this does not include the displayhook, which also caches |
|
1229 | 1229 | objects from the output.""" |
|
1230 | 1230 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1231 | 1231 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1232 | 1232 | |
|
1233 | 1233 | def reset(self, new_session=True): |
|
1234 | 1234 | """Clear all internal namespaces, and attempt to release references to |
|
1235 | 1235 | user objects. |
|
1236 | 1236 | |
|
1237 | 1237 | If new_session is True, a new history session will be opened. |
|
1238 | 1238 | """ |
|
1239 | 1239 | # Clear histories |
|
1240 | 1240 | self.history_manager.reset(new_session) |
|
1241 | 1241 | # Reset counter used to index all histories |
|
1242 | 1242 | if new_session: |
|
1243 | 1243 | self.execution_count = 1 |
|
1244 | 1244 | |
|
1245 | 1245 | # Reset last execution result |
|
1246 | 1246 | self.last_execution_succeeded = True |
|
1247 | 1247 | self.last_execution_result = None |
|
1248 | 1248 | |
|
1249 | 1249 | # Flush cached output items |
|
1250 | 1250 | if self.displayhook.do_full_cache: |
|
1251 | 1251 | self.displayhook.flush() |
|
1252 | 1252 | |
|
1253 | 1253 | # The main execution namespaces must be cleared very carefully, |
|
1254 | 1254 | # skipping the deletion of the builtin-related keys, because doing so |
|
1255 | 1255 | # would cause errors in many object's __del__ methods. |
|
1256 | 1256 | if self.user_ns is not self.user_global_ns: |
|
1257 | 1257 | self.user_ns.clear() |
|
1258 | 1258 | ns = self.user_global_ns |
|
1259 | 1259 | drop_keys = set(ns.keys()) |
|
1260 | 1260 | drop_keys.discard('__builtin__') |
|
1261 | 1261 | drop_keys.discard('__builtins__') |
|
1262 | 1262 | drop_keys.discard('__name__') |
|
1263 | 1263 | for k in drop_keys: |
|
1264 | 1264 | del ns[k] |
|
1265 | 1265 | |
|
1266 | 1266 | self.user_ns_hidden.clear() |
|
1267 | 1267 | |
|
1268 | 1268 | # Restore the user namespaces to minimal usability |
|
1269 | 1269 | self.init_user_ns() |
|
1270 | 1270 | |
|
1271 | 1271 | # Restore the default and user aliases |
|
1272 | 1272 | self.alias_manager.clear_aliases() |
|
1273 | 1273 | self.alias_manager.init_aliases() |
|
1274 | 1274 | |
|
1275 | 1275 | # Flush the private list of module references kept for script |
|
1276 | 1276 | # execution protection |
|
1277 | 1277 | self.clear_main_mod_cache() |
|
1278 | 1278 | |
|
1279 | 1279 | def del_var(self, varname, by_name=False): |
|
1280 | 1280 | """Delete a variable from the various namespaces, so that, as |
|
1281 | 1281 | far as possible, we're not keeping any hidden references to it. |
|
1282 | 1282 | |
|
1283 | 1283 | Parameters |
|
1284 | 1284 | ---------- |
|
1285 | 1285 | varname : str |
|
1286 | 1286 | The name of the variable to delete. |
|
1287 | 1287 | by_name : bool |
|
1288 | 1288 | If True, delete variables with the given name in each |
|
1289 | 1289 | namespace. If False (default), find the variable in the user |
|
1290 | 1290 | namespace, and delete references to it. |
|
1291 | 1291 | """ |
|
1292 | 1292 | if varname in ('__builtin__', '__builtins__'): |
|
1293 | 1293 | raise ValueError("Refusing to delete %s" % varname) |
|
1294 | 1294 | |
|
1295 | 1295 | ns_refs = self.all_ns_refs |
|
1296 | 1296 | |
|
1297 | 1297 | if by_name: # Delete by name |
|
1298 | 1298 | for ns in ns_refs: |
|
1299 | 1299 | try: |
|
1300 | 1300 | del ns[varname] |
|
1301 | 1301 | except KeyError: |
|
1302 | 1302 | pass |
|
1303 | 1303 | else: # Delete by object |
|
1304 | 1304 | try: |
|
1305 | 1305 | obj = self.user_ns[varname] |
|
1306 | 1306 | except KeyError: |
|
1307 | 1307 | raise NameError("name '%s' is not defined" % varname) |
|
1308 | 1308 | # Also check in output history |
|
1309 | 1309 | ns_refs.append(self.history_manager.output_hist) |
|
1310 | 1310 | for ns in ns_refs: |
|
1311 | 1311 | to_delete = [n for n, o in ns.items() if o is obj] |
|
1312 | 1312 | for name in to_delete: |
|
1313 | 1313 | del ns[name] |
|
1314 | 1314 | |
|
1315 | 1315 | # Ensure it is removed from the last execution result |
|
1316 | 1316 | if self.last_execution_result.result is obj: |
|
1317 | 1317 | self.last_execution_result = None |
|
1318 | 1318 | |
|
1319 | 1319 | # displayhook keeps extra references, but not in a dictionary |
|
1320 | 1320 | for name in ('_', '__', '___'): |
|
1321 | 1321 | if getattr(self.displayhook, name) is obj: |
|
1322 | 1322 | setattr(self.displayhook, name, None) |
|
1323 | 1323 | |
|
1324 | 1324 | def reset_selective(self, regex=None): |
|
1325 | 1325 | """Clear selective variables from internal namespaces based on a |
|
1326 | 1326 | specified regular expression. |
|
1327 | 1327 | |
|
1328 | 1328 | Parameters |
|
1329 | 1329 | ---------- |
|
1330 | 1330 | regex : string or compiled pattern, optional |
|
1331 | 1331 | A regular expression pattern that will be used in searching |
|
1332 | 1332 | variable names in the users namespaces. |
|
1333 | 1333 | """ |
|
1334 | 1334 | if regex is not None: |
|
1335 | 1335 | try: |
|
1336 | 1336 | m = re.compile(regex) |
|
1337 | 1337 | except TypeError: |
|
1338 | 1338 | raise TypeError('regex must be a string or compiled pattern') |
|
1339 | 1339 | # Search for keys in each namespace that match the given regex |
|
1340 | 1340 | # If a match is found, delete the key/value pair. |
|
1341 | 1341 | for ns in self.all_ns_refs: |
|
1342 | 1342 | for var in ns: |
|
1343 | 1343 | if m.search(var): |
|
1344 | 1344 | del ns[var] |
|
1345 | 1345 | |
|
1346 | 1346 | def push(self, variables, interactive=True): |
|
1347 | 1347 | """Inject a group of variables into the IPython user namespace. |
|
1348 | 1348 | |
|
1349 | 1349 | Parameters |
|
1350 | 1350 | ---------- |
|
1351 | 1351 | variables : dict, str or list/tuple of str |
|
1352 | 1352 | The variables to inject into the user's namespace. If a dict, a |
|
1353 | 1353 | simple update is done. If a str, the string is assumed to have |
|
1354 | 1354 | variable names separated by spaces. A list/tuple of str can also |
|
1355 | 1355 | be used to give the variable names. If just the variable names are |
|
1356 | 1356 | give (list/tuple/str) then the variable values looked up in the |
|
1357 | 1357 | callers frame. |
|
1358 | 1358 | interactive : bool |
|
1359 | 1359 | If True (default), the variables will be listed with the ``who`` |
|
1360 | 1360 | magic. |
|
1361 | 1361 | """ |
|
1362 | 1362 | vdict = None |
|
1363 | 1363 | |
|
1364 | 1364 | # We need a dict of name/value pairs to do namespace updates. |
|
1365 | 1365 | if isinstance(variables, dict): |
|
1366 | 1366 | vdict = variables |
|
1367 | 1367 | elif isinstance(variables, (str, list, tuple)): |
|
1368 | 1368 | if isinstance(variables, str): |
|
1369 | 1369 | vlist = variables.split() |
|
1370 | 1370 | else: |
|
1371 | 1371 | vlist = variables |
|
1372 | 1372 | vdict = {} |
|
1373 | 1373 | cf = sys._getframe(1) |
|
1374 | 1374 | for name in vlist: |
|
1375 | 1375 | try: |
|
1376 | 1376 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1377 | 1377 | except: |
|
1378 | 1378 | print('Could not get variable %s from %s' % |
|
1379 | 1379 | (name,cf.f_code.co_name)) |
|
1380 | 1380 | else: |
|
1381 | 1381 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1382 | 1382 | |
|
1383 | 1383 | # Propagate variables to user namespace |
|
1384 | 1384 | self.user_ns.update(vdict) |
|
1385 | 1385 | |
|
1386 | 1386 | # And configure interactive visibility |
|
1387 | 1387 | user_ns_hidden = self.user_ns_hidden |
|
1388 | 1388 | if interactive: |
|
1389 | 1389 | for name in vdict: |
|
1390 | 1390 | user_ns_hidden.pop(name, None) |
|
1391 | 1391 | else: |
|
1392 | 1392 | user_ns_hidden.update(vdict) |
|
1393 | 1393 | |
|
1394 | 1394 | def drop_by_id(self, variables): |
|
1395 | 1395 | """Remove a dict of variables from the user namespace, if they are the |
|
1396 | 1396 | same as the values in the dictionary. |
|
1397 | 1397 | |
|
1398 | 1398 | This is intended for use by extensions: variables that they've added can |
|
1399 | 1399 | be taken back out if they are unloaded, without removing any that the |
|
1400 | 1400 | user has overwritten. |
|
1401 | 1401 | |
|
1402 | 1402 | Parameters |
|
1403 | 1403 | ---------- |
|
1404 | 1404 | variables : dict |
|
1405 | 1405 | A dictionary mapping object names (as strings) to the objects. |
|
1406 | 1406 | """ |
|
1407 | 1407 | for name, obj in variables.items(): |
|
1408 | 1408 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1409 | 1409 | del self.user_ns[name] |
|
1410 | 1410 | self.user_ns_hidden.pop(name, None) |
|
1411 | 1411 | |
|
1412 | 1412 | #------------------------------------------------------------------------- |
|
1413 | 1413 | # Things related to object introspection |
|
1414 | 1414 | #------------------------------------------------------------------------- |
|
1415 | 1415 | |
|
1416 | 1416 | def _ofind(self, oname, namespaces=None): |
|
1417 | 1417 | """Find an object in the available namespaces. |
|
1418 | 1418 | |
|
1419 | 1419 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1420 | 1420 | |
|
1421 | 1421 | Has special code to detect magic functions. |
|
1422 | 1422 | """ |
|
1423 | 1423 | oname = oname.strip() |
|
1424 | 1424 | if not oname.startswith(ESC_MAGIC) and \ |
|
1425 | 1425 | not oname.startswith(ESC_MAGIC2) and \ |
|
1426 | 1426 | not all(a.isidentifier() for a in oname.split(".")): |
|
1427 | 1427 | return {'found': False} |
|
1428 | 1428 | |
|
1429 | 1429 | if namespaces is None: |
|
1430 | 1430 | # Namespaces to search in: |
|
1431 | 1431 | # Put them in a list. The order is important so that we |
|
1432 | 1432 | # find things in the same order that Python finds them. |
|
1433 | 1433 | namespaces = [ ('Interactive', self.user_ns), |
|
1434 | 1434 | ('Interactive (global)', self.user_global_ns), |
|
1435 | 1435 | ('Python builtin', builtin_mod.__dict__), |
|
1436 | 1436 | ] |
|
1437 | 1437 | |
|
1438 | 1438 | ismagic = False |
|
1439 | 1439 | isalias = False |
|
1440 | 1440 | found = False |
|
1441 | 1441 | ospace = None |
|
1442 | 1442 | parent = None |
|
1443 | 1443 | obj = None |
|
1444 | 1444 | |
|
1445 | 1445 | # Look for the given name by splitting it in parts. If the head is |
|
1446 | 1446 | # found, then we look for all the remaining parts as members, and only |
|
1447 | 1447 | # declare success if we can find them all. |
|
1448 | 1448 | oname_parts = oname.split('.') |
|
1449 | 1449 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1450 | 1450 | for nsname,ns in namespaces: |
|
1451 | 1451 | try: |
|
1452 | 1452 | obj = ns[oname_head] |
|
1453 | 1453 | except KeyError: |
|
1454 | 1454 | continue |
|
1455 | 1455 | else: |
|
1456 | 1456 | for idx, part in enumerate(oname_rest): |
|
1457 | 1457 | try: |
|
1458 | 1458 | parent = obj |
|
1459 | 1459 | # The last part is looked up in a special way to avoid |
|
1460 | 1460 | # descriptor invocation as it may raise or have side |
|
1461 | 1461 | # effects. |
|
1462 | 1462 | if idx == len(oname_rest) - 1: |
|
1463 | 1463 | obj = self._getattr_property(obj, part) |
|
1464 | 1464 | else: |
|
1465 | 1465 | obj = getattr(obj, part) |
|
1466 | 1466 | except: |
|
1467 | 1467 | # Blanket except b/c some badly implemented objects |
|
1468 | 1468 | # allow __getattr__ to raise exceptions other than |
|
1469 | 1469 | # AttributeError, which then crashes IPython. |
|
1470 | 1470 | break |
|
1471 | 1471 | else: |
|
1472 | 1472 | # If we finish the for loop (no break), we got all members |
|
1473 | 1473 | found = True |
|
1474 | 1474 | ospace = nsname |
|
1475 | 1475 | break # namespace loop |
|
1476 | 1476 | |
|
1477 | 1477 | # Try to see if it's magic |
|
1478 | 1478 | if not found: |
|
1479 | 1479 | obj = None |
|
1480 | 1480 | if oname.startswith(ESC_MAGIC2): |
|
1481 | 1481 | oname = oname.lstrip(ESC_MAGIC2) |
|
1482 | 1482 | obj = self.find_cell_magic(oname) |
|
1483 | 1483 | elif oname.startswith(ESC_MAGIC): |
|
1484 | 1484 | oname = oname.lstrip(ESC_MAGIC) |
|
1485 | 1485 | obj = self.find_line_magic(oname) |
|
1486 | 1486 | else: |
|
1487 | 1487 | # search without prefix, so run? will find %run? |
|
1488 | 1488 | obj = self.find_line_magic(oname) |
|
1489 | 1489 | if obj is None: |
|
1490 | 1490 | obj = self.find_cell_magic(oname) |
|
1491 | 1491 | if obj is not None: |
|
1492 | 1492 | found = True |
|
1493 | 1493 | ospace = 'IPython internal' |
|
1494 | 1494 | ismagic = True |
|
1495 | 1495 | isalias = isinstance(obj, Alias) |
|
1496 | 1496 | |
|
1497 | 1497 | # Last try: special-case some literals like '', [], {}, etc: |
|
1498 | 1498 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1499 | 1499 | obj = eval(oname_head) |
|
1500 | 1500 | found = True |
|
1501 | 1501 | ospace = 'Interactive' |
|
1502 | 1502 | |
|
1503 | 1503 | return { |
|
1504 | 1504 | 'obj':obj, |
|
1505 | 1505 | 'found':found, |
|
1506 | 1506 | 'parent':parent, |
|
1507 | 1507 | 'ismagic':ismagic, |
|
1508 | 1508 | 'isalias':isalias, |
|
1509 | 1509 | 'namespace':ospace |
|
1510 | 1510 | } |
|
1511 | 1511 | |
|
1512 | 1512 | @staticmethod |
|
1513 | 1513 | def _getattr_property(obj, attrname): |
|
1514 | 1514 | """Property-aware getattr to use in object finding. |
|
1515 | 1515 | |
|
1516 | 1516 | If attrname represents a property, return it unevaluated (in case it has |
|
1517 | 1517 | side effects or raises an error. |
|
1518 | 1518 | |
|
1519 | 1519 | """ |
|
1520 | 1520 | if not isinstance(obj, type): |
|
1521 | 1521 | try: |
|
1522 | 1522 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1523 | 1523 | # `obj`, but does so for property: |
|
1524 | 1524 | # |
|
1525 | 1525 | # property.__get__(self, None, cls) -> self |
|
1526 | 1526 | # |
|
1527 | 1527 | # The universal alternative is to traverse the mro manually |
|
1528 | 1528 | # searching for attrname in class dicts. |
|
1529 | 1529 | attr = getattr(type(obj), attrname) |
|
1530 | 1530 | except AttributeError: |
|
1531 | 1531 | pass |
|
1532 | 1532 | else: |
|
1533 | 1533 | # This relies on the fact that data descriptors (with both |
|
1534 | 1534 | # __get__ & __set__ magic methods) take precedence over |
|
1535 | 1535 | # instance-level attributes: |
|
1536 | 1536 | # |
|
1537 | 1537 | # class A(object): |
|
1538 | 1538 | # @property |
|
1539 | 1539 | # def foobar(self): return 123 |
|
1540 | 1540 | # a = A() |
|
1541 | 1541 | # a.__dict__['foobar'] = 345 |
|
1542 | 1542 | # a.foobar # == 123 |
|
1543 | 1543 | # |
|
1544 | 1544 | # So, a property may be returned right away. |
|
1545 | 1545 | if isinstance(attr, property): |
|
1546 | 1546 | return attr |
|
1547 | 1547 | |
|
1548 | 1548 | # Nothing helped, fall back. |
|
1549 | 1549 | return getattr(obj, attrname) |
|
1550 | 1550 | |
|
1551 | 1551 | def _object_find(self, oname, namespaces=None): |
|
1552 | 1552 | """Find an object and return a struct with info about it.""" |
|
1553 | 1553 | return Struct(self._ofind(oname, namespaces)) |
|
1554 | 1554 | |
|
1555 | 1555 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1556 | 1556 | """Generic interface to the inspector system. |
|
1557 | 1557 | |
|
1558 | 1558 | This function is meant to be called by pdef, pdoc & friends. |
|
1559 | 1559 | """ |
|
1560 | 1560 | info = self._object_find(oname, namespaces) |
|
1561 | 1561 | docformat = sphinxify if self.sphinxify_docstring else None |
|
1562 | 1562 | if info.found: |
|
1563 | 1563 | pmethod = getattr(self.inspector, meth) |
|
1564 | 1564 | # TODO: only apply format_screen to the plain/text repr of the mime |
|
1565 | 1565 | # bundle. |
|
1566 | 1566 | formatter = format_screen if info.ismagic else docformat |
|
1567 | 1567 | if meth == 'pdoc': |
|
1568 | 1568 | pmethod(info.obj, oname, formatter) |
|
1569 | 1569 | elif meth == 'pinfo': |
|
1570 | 1570 | pmethod(info.obj, oname, formatter, info, |
|
1571 | 1571 | enable_html_pager=self.enable_html_pager, **kw) |
|
1572 | 1572 | else: |
|
1573 | 1573 | pmethod(info.obj, oname) |
|
1574 | 1574 | else: |
|
1575 | 1575 | print('Object `%s` not found.' % oname) |
|
1576 | 1576 | return 'not found' # so callers can take other action |
|
1577 | 1577 | |
|
1578 | 1578 | def object_inspect(self, oname, detail_level=0): |
|
1579 | 1579 | """Get object info about oname""" |
|
1580 | 1580 | with self.builtin_trap: |
|
1581 | 1581 | info = self._object_find(oname) |
|
1582 | 1582 | if info.found: |
|
1583 | 1583 | return self.inspector.info(info.obj, oname, info=info, |
|
1584 | 1584 | detail_level=detail_level |
|
1585 | 1585 | ) |
|
1586 | 1586 | else: |
|
1587 | 1587 | return oinspect.object_info(name=oname, found=False) |
|
1588 | 1588 | |
|
1589 | 1589 | def object_inspect_text(self, oname, detail_level=0): |
|
1590 | 1590 | """Get object info as formatted text""" |
|
1591 | 1591 | return self.object_inspect_mime(oname, detail_level)['text/plain'] |
|
1592 | 1592 | |
|
1593 | 1593 | def object_inspect_mime(self, oname, detail_level=0): |
|
1594 | 1594 | """Get object info as a mimebundle of formatted representations. |
|
1595 | 1595 | |
|
1596 | 1596 | A mimebundle is a dictionary, keyed by mime-type. |
|
1597 | 1597 | It must always have the key `'text/plain'`. |
|
1598 | 1598 | """ |
|
1599 | 1599 | with self.builtin_trap: |
|
1600 | 1600 | info = self._object_find(oname) |
|
1601 | 1601 | if info.found: |
|
1602 | 1602 | return self.inspector._get_info(info.obj, oname, info=info, |
|
1603 | 1603 | detail_level=detail_level |
|
1604 | 1604 | ) |
|
1605 | 1605 | else: |
|
1606 | 1606 | raise KeyError(oname) |
|
1607 | 1607 | |
|
1608 | 1608 | #------------------------------------------------------------------------- |
|
1609 | 1609 | # Things related to history management |
|
1610 | 1610 | #------------------------------------------------------------------------- |
|
1611 | 1611 | |
|
1612 | 1612 | def init_history(self): |
|
1613 | 1613 | """Sets up the command history, and starts regular autosaves.""" |
|
1614 | 1614 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1615 | 1615 | self.configurables.append(self.history_manager) |
|
1616 | 1616 | |
|
1617 | 1617 | #------------------------------------------------------------------------- |
|
1618 | 1618 | # Things related to exception handling and tracebacks (not debugging) |
|
1619 | 1619 | #------------------------------------------------------------------------- |
|
1620 | 1620 | |
|
1621 | 1621 | debugger_cls = Pdb |
|
1622 | 1622 | |
|
1623 | 1623 | def init_traceback_handlers(self, custom_exceptions): |
|
1624 | 1624 | # Syntax error handler. |
|
1625 | 1625 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self) |
|
1626 | 1626 | |
|
1627 | 1627 | # The interactive one is initialized with an offset, meaning we always |
|
1628 | 1628 | # want to remove the topmost item in the traceback, which is our own |
|
1629 | 1629 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1630 | 1630 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1631 | 1631 | color_scheme='NoColor', |
|
1632 | 1632 | tb_offset = 1, |
|
1633 | 1633 | check_cache=check_linecache_ipython, |
|
1634 | 1634 | debugger_cls=self.debugger_cls, parent=self) |
|
1635 | 1635 | |
|
1636 | 1636 | # The instance will store a pointer to the system-wide exception hook, |
|
1637 | 1637 | # so that runtime code (such as magics) can access it. This is because |
|
1638 | 1638 | # during the read-eval loop, it may get temporarily overwritten. |
|
1639 | 1639 | self.sys_excepthook = sys.excepthook |
|
1640 | 1640 | |
|
1641 | 1641 | # and add any custom exception handlers the user may have specified |
|
1642 | 1642 | self.set_custom_exc(*custom_exceptions) |
|
1643 | 1643 | |
|
1644 | 1644 | # Set the exception mode |
|
1645 | 1645 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1646 | 1646 | |
|
1647 | 1647 | def set_custom_exc(self, exc_tuple, handler): |
|
1648 | 1648 | """set_custom_exc(exc_tuple, handler) |
|
1649 | 1649 | |
|
1650 | 1650 | Set a custom exception handler, which will be called if any of the |
|
1651 | 1651 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1652 | 1652 | run_code() method). |
|
1653 | 1653 | |
|
1654 | 1654 | Parameters |
|
1655 | 1655 | ---------- |
|
1656 | 1656 | |
|
1657 | 1657 | exc_tuple : tuple of exception classes |
|
1658 | 1658 | A *tuple* of exception classes, for which to call the defined |
|
1659 | 1659 | handler. It is very important that you use a tuple, and NOT A |
|
1660 | 1660 | LIST here, because of the way Python's except statement works. If |
|
1661 | 1661 | you only want to trap a single exception, use a singleton tuple:: |
|
1662 | 1662 | |
|
1663 | 1663 | exc_tuple == (MyCustomException,) |
|
1664 | 1664 | |
|
1665 | 1665 | handler : callable |
|
1666 | 1666 | handler must have the following signature:: |
|
1667 | 1667 | |
|
1668 | 1668 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1669 | 1669 | ... |
|
1670 | 1670 | return structured_traceback |
|
1671 | 1671 | |
|
1672 | 1672 | Your handler must return a structured traceback (a list of strings), |
|
1673 | 1673 | or None. |
|
1674 | 1674 | |
|
1675 | 1675 | This will be made into an instance method (via types.MethodType) |
|
1676 | 1676 | of IPython itself, and it will be called if any of the exceptions |
|
1677 | 1677 | listed in the exc_tuple are caught. If the handler is None, an |
|
1678 | 1678 | internal basic one is used, which just prints basic info. |
|
1679 | 1679 | |
|
1680 | 1680 | To protect IPython from crashes, if your handler ever raises an |
|
1681 | 1681 | exception or returns an invalid result, it will be immediately |
|
1682 | 1682 | disabled. |
|
1683 | 1683 | |
|
1684 | 1684 | WARNING: by putting in your own exception handler into IPython's main |
|
1685 | 1685 | execution loop, you run a very good chance of nasty crashes. This |
|
1686 | 1686 | facility should only be used if you really know what you are doing.""" |
|
1687 | 1687 | if not isinstance(exc_tuple, tuple): |
|
1688 | 1688 | raise TypeError("The custom exceptions must be given as a tuple.") |
|
1689 | 1689 | |
|
1690 | 1690 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
|
1691 | 1691 | print('*** Simple custom exception handler ***') |
|
1692 | 1692 | print('Exception type :', etype) |
|
1693 | 1693 | print('Exception value:', value) |
|
1694 | 1694 | print('Traceback :', tb) |
|
1695 | 1695 | |
|
1696 | 1696 | def validate_stb(stb): |
|
1697 | 1697 | """validate structured traceback return type |
|
1698 | 1698 | |
|
1699 | 1699 | return type of CustomTB *should* be a list of strings, but allow |
|
1700 | 1700 | single strings or None, which are harmless. |
|
1701 | 1701 | |
|
1702 | 1702 | This function will *always* return a list of strings, |
|
1703 | 1703 | and will raise a TypeError if stb is inappropriate. |
|
1704 | 1704 | """ |
|
1705 | 1705 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1706 | 1706 | if stb is None: |
|
1707 | 1707 | return [] |
|
1708 | 1708 | elif isinstance(stb, str): |
|
1709 | 1709 | return [stb] |
|
1710 | 1710 | elif not isinstance(stb, list): |
|
1711 | 1711 | raise TypeError(msg) |
|
1712 | 1712 | # it's a list |
|
1713 | 1713 | for line in stb: |
|
1714 | 1714 | # check every element |
|
1715 | 1715 | if not isinstance(line, str): |
|
1716 | 1716 | raise TypeError(msg) |
|
1717 | 1717 | return stb |
|
1718 | 1718 | |
|
1719 | 1719 | if handler is None: |
|
1720 | 1720 | wrapped = dummy_handler |
|
1721 | 1721 | else: |
|
1722 | 1722 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1723 | 1723 | """wrap CustomTB handler, to protect IPython from user code |
|
1724 | 1724 | |
|
1725 | 1725 | This makes it harder (but not impossible) for custom exception |
|
1726 | 1726 | handlers to crash IPython. |
|
1727 | 1727 | """ |
|
1728 | 1728 | try: |
|
1729 | 1729 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1730 | 1730 | return validate_stb(stb) |
|
1731 | 1731 | except: |
|
1732 | 1732 | # clear custom handler immediately |
|
1733 | 1733 | self.set_custom_exc((), None) |
|
1734 | 1734 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1735 | 1735 | # show the exception in handler first |
|
1736 | 1736 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1737 | 1737 | print(self.InteractiveTB.stb2text(stb)) |
|
1738 | 1738 | print("The original exception:") |
|
1739 | 1739 | stb = self.InteractiveTB.structured_traceback( |
|
1740 | 1740 | (etype,value,tb), tb_offset=tb_offset |
|
1741 | 1741 | ) |
|
1742 | 1742 | return stb |
|
1743 | 1743 | |
|
1744 | 1744 | self.CustomTB = types.MethodType(wrapped,self) |
|
1745 | 1745 | self.custom_exceptions = exc_tuple |
|
1746 | 1746 | |
|
1747 | 1747 | def excepthook(self, etype, value, tb): |
|
1748 | 1748 | """One more defense for GUI apps that call sys.excepthook. |
|
1749 | 1749 | |
|
1750 | 1750 | GUI frameworks like wxPython trap exceptions and call |
|
1751 | 1751 | sys.excepthook themselves. I guess this is a feature that |
|
1752 | 1752 | enables them to keep running after exceptions that would |
|
1753 | 1753 | otherwise kill their mainloop. This is a bother for IPython |
|
1754 | 1754 | which excepts to catch all of the program exceptions with a try: |
|
1755 | 1755 | except: statement. |
|
1756 | 1756 | |
|
1757 | 1757 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1758 | 1758 | any app directly invokes sys.excepthook, it will look to the user like |
|
1759 | 1759 | IPython crashed. In order to work around this, we can disable the |
|
1760 | 1760 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1761 | 1761 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1762 | 1762 | call sys.excepthook will generate a regular-looking exception from |
|
1763 | 1763 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1764 | 1764 | crashes. |
|
1765 | 1765 | |
|
1766 | 1766 | This hook should be used sparingly, only in places which are not likely |
|
1767 | 1767 | to be true IPython errors. |
|
1768 | 1768 | """ |
|
1769 | 1769 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1770 | 1770 | |
|
1771 | 1771 | def _get_exc_info(self, exc_tuple=None): |
|
1772 | 1772 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1773 | 1773 | |
|
1774 | 1774 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1775 | 1775 | from whichever source. |
|
1776 | 1776 | |
|
1777 | 1777 | raises ValueError if none of these contain any information |
|
1778 | 1778 | """ |
|
1779 | 1779 | if exc_tuple is None: |
|
1780 | 1780 | etype, value, tb = sys.exc_info() |
|
1781 | 1781 | else: |
|
1782 | 1782 | etype, value, tb = exc_tuple |
|
1783 | 1783 | |
|
1784 | 1784 | if etype is None: |
|
1785 | 1785 | if hasattr(sys, 'last_type'): |
|
1786 | 1786 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1787 | 1787 | sys.last_traceback |
|
1788 | 1788 | |
|
1789 | 1789 | if etype is None: |
|
1790 | 1790 | raise ValueError("No exception to find") |
|
1791 | 1791 | |
|
1792 | 1792 | # Now store the exception info in sys.last_type etc. |
|
1793 | 1793 | # WARNING: these variables are somewhat deprecated and not |
|
1794 | 1794 | # necessarily safe to use in a threaded environment, but tools |
|
1795 | 1795 | # like pdb depend on their existence, so let's set them. If we |
|
1796 | 1796 | # find problems in the field, we'll need to revisit their use. |
|
1797 | 1797 | sys.last_type = etype |
|
1798 | 1798 | sys.last_value = value |
|
1799 | 1799 | sys.last_traceback = tb |
|
1800 | 1800 | |
|
1801 | 1801 | return etype, value, tb |
|
1802 | 1802 | |
|
1803 | 1803 | def show_usage_error(self, exc): |
|
1804 | 1804 | """Show a short message for UsageErrors |
|
1805 | 1805 | |
|
1806 | 1806 | These are special exceptions that shouldn't show a traceback. |
|
1807 | 1807 | """ |
|
1808 | 1808 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1809 | 1809 | |
|
1810 | 1810 | def get_exception_only(self, exc_tuple=None): |
|
1811 | 1811 | """ |
|
1812 | 1812 | Return as a string (ending with a newline) the exception that |
|
1813 | 1813 | just occurred, without any traceback. |
|
1814 | 1814 | """ |
|
1815 | 1815 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1816 | 1816 | msg = traceback.format_exception_only(etype, value) |
|
1817 | 1817 | return ''.join(msg) |
|
1818 | 1818 | |
|
1819 | 1819 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1820 | 1820 | exception_only=False, running_compiled_code=False): |
|
1821 | 1821 | """Display the exception that just occurred. |
|
1822 | 1822 | |
|
1823 | 1823 | If nothing is known about the exception, this is the method which |
|
1824 | 1824 | should be used throughout the code for presenting user tracebacks, |
|
1825 | 1825 | rather than directly invoking the InteractiveTB object. |
|
1826 | 1826 | |
|
1827 | 1827 | A specific showsyntaxerror() also exists, but this method can take |
|
1828 | 1828 | care of calling it if needed, so unless you are explicitly catching a |
|
1829 | 1829 | SyntaxError exception, don't try to analyze the stack manually and |
|
1830 | 1830 | simply call this method.""" |
|
1831 | 1831 | |
|
1832 | 1832 | try: |
|
1833 | 1833 | try: |
|
1834 | 1834 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1835 | 1835 | except ValueError: |
|
1836 | 1836 | print('No traceback available to show.', file=sys.stderr) |
|
1837 | 1837 | return |
|
1838 | 1838 | |
|
1839 | 1839 | if issubclass(etype, SyntaxError): |
|
1840 | 1840 | # Though this won't be called by syntax errors in the input |
|
1841 | 1841 | # line, there may be SyntaxError cases with imported code. |
|
1842 | 1842 | self.showsyntaxerror(filename, running_compiled_code) |
|
1843 | 1843 | elif etype is UsageError: |
|
1844 | 1844 | self.show_usage_error(value) |
|
1845 | 1845 | else: |
|
1846 | 1846 | if exception_only: |
|
1847 | 1847 | stb = ['An exception has occurred, use %tb to see ' |
|
1848 | 1848 | 'the full traceback.\n'] |
|
1849 | 1849 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1850 | 1850 | value)) |
|
1851 | 1851 | else: |
|
1852 | 1852 | try: |
|
1853 | 1853 | # Exception classes can customise their traceback - we |
|
1854 | 1854 | # use this in IPython.parallel for exceptions occurring |
|
1855 | 1855 | # in the engines. This should return a list of strings. |
|
1856 | 1856 | stb = value._render_traceback_() |
|
1857 | 1857 | except Exception: |
|
1858 | 1858 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1859 | 1859 | value, tb, tb_offset=tb_offset) |
|
1860 | 1860 | |
|
1861 | 1861 | self._showtraceback(etype, value, stb) |
|
1862 | 1862 | if self.call_pdb: |
|
1863 | 1863 | # drop into debugger |
|
1864 | 1864 | self.debugger(force=True) |
|
1865 | 1865 | return |
|
1866 | 1866 | |
|
1867 | 1867 | # Actually show the traceback |
|
1868 | 1868 | self._showtraceback(etype, value, stb) |
|
1869 | 1869 | |
|
1870 | 1870 | except KeyboardInterrupt: |
|
1871 | 1871 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1872 | 1872 | |
|
1873 | 1873 | def _showtraceback(self, etype, evalue, stb): |
|
1874 | 1874 | """Actually show a traceback. |
|
1875 | 1875 | |
|
1876 | 1876 | Subclasses may override this method to put the traceback on a different |
|
1877 | 1877 | place, like a side channel. |
|
1878 | 1878 | """ |
|
1879 | 1879 | print(self.InteractiveTB.stb2text(stb)) |
|
1880 | 1880 | |
|
1881 | 1881 | def showsyntaxerror(self, filename=None, running_compiled_code=False): |
|
1882 | 1882 | """Display the syntax error that just occurred. |
|
1883 | 1883 | |
|
1884 | 1884 | This doesn't display a stack trace because there isn't one. |
|
1885 | 1885 | |
|
1886 | 1886 | If a filename is given, it is stuffed in the exception instead |
|
1887 | 1887 | of what was there before (because Python's parser always uses |
|
1888 | 1888 | "<string>" when reading from a string). |
|
1889 | 1889 | |
|
1890 | 1890 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), |
|
1891 | 1891 | longer stack trace will be displayed. |
|
1892 | 1892 | """ |
|
1893 | 1893 | etype, value, last_traceback = self._get_exc_info() |
|
1894 | 1894 | |
|
1895 | 1895 | if filename and issubclass(etype, SyntaxError): |
|
1896 | 1896 | try: |
|
1897 | 1897 | value.filename = filename |
|
1898 | 1898 | except: |
|
1899 | 1899 | # Not the format we expect; leave it alone |
|
1900 | 1900 | pass |
|
1901 | 1901 | |
|
1902 | # If the error occured when executing compiled code, we should provide full stacktrace. | |
|
1902 | # If the error occurred when executing compiled code, we should provide full stacktrace. | |
|
1903 | 1903 | elist = traceback.extract_tb(last_traceback) if running_compiled_code else [] |
|
1904 | 1904 | stb = self.SyntaxTB.structured_traceback(etype, value, elist) |
|
1905 | 1905 | self._showtraceback(etype, value, stb) |
|
1906 | 1906 | |
|
1907 | 1907 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1908 | 1908 | # the %paste magic. |
|
1909 | 1909 | def showindentationerror(self): |
|
1910 | 1910 | """Called by _run_cell when there's an IndentationError in code entered |
|
1911 | 1911 | at the prompt. |
|
1912 | 1912 | |
|
1913 | 1913 | This is overridden in TerminalInteractiveShell to show a message about |
|
1914 | 1914 | the %paste magic.""" |
|
1915 | 1915 | self.showsyntaxerror() |
|
1916 | 1916 | |
|
1917 | 1917 | #------------------------------------------------------------------------- |
|
1918 | 1918 | # Things related to readline |
|
1919 | 1919 | #------------------------------------------------------------------------- |
|
1920 | 1920 | |
|
1921 | 1921 | def init_readline(self): |
|
1922 | 1922 | """DEPRECATED |
|
1923 | 1923 | |
|
1924 | 1924 | Moved to terminal subclass, here only to simplify the init logic.""" |
|
1925 | 1925 | # Set a number of methods that depend on readline to be no-op |
|
1926 | 1926 | warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated', |
|
1927 | 1927 | DeprecationWarning, stacklevel=2) |
|
1928 | 1928 | self.set_custom_completer = no_op |
|
1929 | 1929 | |
|
1930 | 1930 | @skip_doctest |
|
1931 | 1931 | def set_next_input(self, s, replace=False): |
|
1932 | 1932 | """ Sets the 'default' input string for the next command line. |
|
1933 | 1933 | |
|
1934 | 1934 | Example:: |
|
1935 | 1935 | |
|
1936 | 1936 | In [1]: _ip.set_next_input("Hello Word") |
|
1937 | 1937 | In [2]: Hello Word_ # cursor is here |
|
1938 | 1938 | """ |
|
1939 | 1939 | self.rl_next_input = s |
|
1940 | 1940 | |
|
1941 | 1941 | def _indent_current_str(self): |
|
1942 | 1942 | """return the current level of indentation as a string""" |
|
1943 | 1943 | return self.input_splitter.get_indent_spaces() * ' ' |
|
1944 | 1944 | |
|
1945 | 1945 | #------------------------------------------------------------------------- |
|
1946 | 1946 | # Things related to text completion |
|
1947 | 1947 | #------------------------------------------------------------------------- |
|
1948 | 1948 | |
|
1949 | 1949 | def init_completer(self): |
|
1950 | 1950 | """Initialize the completion machinery. |
|
1951 | 1951 | |
|
1952 | 1952 | This creates completion machinery that can be used by client code, |
|
1953 | 1953 | either interactively in-process (typically triggered by the readline |
|
1954 | 1954 | library), programmatically (such as in test suites) or out-of-process |
|
1955 | 1955 | (typically over the network by remote frontends). |
|
1956 | 1956 | """ |
|
1957 | 1957 | from IPython.core.completer import IPCompleter |
|
1958 | 1958 | from IPython.core.completerlib import (module_completer, |
|
1959 | 1959 | magic_run_completer, cd_completer, reset_completer) |
|
1960 | 1960 | |
|
1961 | 1961 | self.Completer = IPCompleter(shell=self, |
|
1962 | 1962 | namespace=self.user_ns, |
|
1963 | 1963 | global_namespace=self.user_global_ns, |
|
1964 | 1964 | parent=self, |
|
1965 | 1965 | ) |
|
1966 | 1966 | self.configurables.append(self.Completer) |
|
1967 | 1967 | |
|
1968 | 1968 | # Add custom completers to the basic ones built into IPCompleter |
|
1969 | 1969 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1970 | 1970 | self.strdispatchers['complete_command'] = sdisp |
|
1971 | 1971 | self.Completer.custom_completers = sdisp |
|
1972 | 1972 | |
|
1973 | 1973 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1974 | 1974 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1975 | 1975 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
1976 | 1976 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1977 | 1977 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1978 | 1978 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1979 | 1979 | |
|
1980 | 1980 | |
|
1981 | 1981 | def complete(self, text, line=None, cursor_pos=None): |
|
1982 | 1982 | """Return the completed text and a list of completions. |
|
1983 | 1983 | |
|
1984 | 1984 | Parameters |
|
1985 | 1985 | ---------- |
|
1986 | 1986 | |
|
1987 | 1987 | text : string |
|
1988 | 1988 | A string of text to be completed on. It can be given as empty and |
|
1989 | 1989 | instead a line/position pair are given. In this case, the |
|
1990 | 1990 | completer itself will split the line like readline does. |
|
1991 | 1991 | |
|
1992 | 1992 | line : string, optional |
|
1993 | 1993 | The complete line that text is part of. |
|
1994 | 1994 | |
|
1995 | 1995 | cursor_pos : int, optional |
|
1996 | 1996 | The position of the cursor on the input line. |
|
1997 | 1997 | |
|
1998 | 1998 | Returns |
|
1999 | 1999 | ------- |
|
2000 | 2000 | text : string |
|
2001 | 2001 | The actual text that was completed. |
|
2002 | 2002 | |
|
2003 | 2003 | matches : list |
|
2004 | 2004 | A sorted list with all possible completions. |
|
2005 | 2005 | |
|
2006 | 2006 | The optional arguments allow the completion to take more context into |
|
2007 | 2007 | account, and are part of the low-level completion API. |
|
2008 | 2008 | |
|
2009 | 2009 | This is a wrapper around the completion mechanism, similar to what |
|
2010 | 2010 | readline does at the command line when the TAB key is hit. By |
|
2011 | 2011 | exposing it as a method, it can be used by other non-readline |
|
2012 | 2012 | environments (such as GUIs) for text completion. |
|
2013 | 2013 | |
|
2014 | 2014 | Simple usage example: |
|
2015 | 2015 | |
|
2016 | 2016 | In [1]: x = 'hello' |
|
2017 | 2017 | |
|
2018 | 2018 | In [2]: _ip.complete('x.l') |
|
2019 | 2019 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
2020 | 2020 | """ |
|
2021 | 2021 | |
|
2022 | 2022 | # Inject names into __builtin__ so we can complete on the added names. |
|
2023 | 2023 | with self.builtin_trap: |
|
2024 | 2024 | return self.Completer.complete(text, line, cursor_pos) |
|
2025 | 2025 | |
|
2026 | 2026 | def set_custom_completer(self, completer, pos=0): |
|
2027 | 2027 | """Adds a new custom completer function. |
|
2028 | 2028 | |
|
2029 | 2029 | The position argument (defaults to 0) is the index in the completers |
|
2030 | 2030 | list where you want the completer to be inserted.""" |
|
2031 | 2031 | |
|
2032 | 2032 | newcomp = types.MethodType(completer,self.Completer) |
|
2033 | 2033 | self.Completer.matchers.insert(pos,newcomp) |
|
2034 | 2034 | |
|
2035 | 2035 | def set_completer_frame(self, frame=None): |
|
2036 | 2036 | """Set the frame of the completer.""" |
|
2037 | 2037 | if frame: |
|
2038 | 2038 | self.Completer.namespace = frame.f_locals |
|
2039 | 2039 | self.Completer.global_namespace = frame.f_globals |
|
2040 | 2040 | else: |
|
2041 | 2041 | self.Completer.namespace = self.user_ns |
|
2042 | 2042 | self.Completer.global_namespace = self.user_global_ns |
|
2043 | 2043 | |
|
2044 | 2044 | #------------------------------------------------------------------------- |
|
2045 | 2045 | # Things related to magics |
|
2046 | 2046 | #------------------------------------------------------------------------- |
|
2047 | 2047 | |
|
2048 | 2048 | def init_magics(self): |
|
2049 | 2049 | from IPython.core import magics as m |
|
2050 | 2050 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2051 | 2051 | parent=self, |
|
2052 | 2052 | user_magics=m.UserMagics(self)) |
|
2053 | 2053 | self.configurables.append(self.magics_manager) |
|
2054 | 2054 | |
|
2055 | 2055 | # Expose as public API from the magics manager |
|
2056 | 2056 | self.register_magics = self.magics_manager.register |
|
2057 | 2057 | |
|
2058 | 2058 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2059 | 2059 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2060 | 2060 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2061 | 2061 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2062 | 2062 | ) |
|
2063 | 2063 | |
|
2064 | 2064 | # Register Magic Aliases |
|
2065 | 2065 | mman = self.magics_manager |
|
2066 | 2066 | # FIXME: magic aliases should be defined by the Magics classes |
|
2067 | 2067 | # or in MagicsManager, not here |
|
2068 | 2068 | mman.register_alias('ed', 'edit') |
|
2069 | 2069 | mman.register_alias('hist', 'history') |
|
2070 | 2070 | mman.register_alias('rep', 'recall') |
|
2071 | 2071 | mman.register_alias('SVG', 'svg', 'cell') |
|
2072 | 2072 | mman.register_alias('HTML', 'html', 'cell') |
|
2073 | 2073 | mman.register_alias('file', 'writefile', 'cell') |
|
2074 | 2074 | |
|
2075 | 2075 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2076 | 2076 | # should be split into a prompt manager and displayhook. We probably |
|
2077 | 2077 | # even need a centralize colors management object. |
|
2078 | 2078 | self.run_line_magic('colors', self.colors) |
|
2079 | 2079 | |
|
2080 | 2080 | # Defined here so that it's included in the documentation |
|
2081 | 2081 | @functools.wraps(magic.MagicsManager.register_function) |
|
2082 | 2082 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2083 | 2083 | self.magics_manager.register_function(func, |
|
2084 | 2084 | magic_kind=magic_kind, magic_name=magic_name) |
|
2085 | 2085 | |
|
2086 | 2086 | def run_line_magic(self, magic_name, line, _stack_depth=1): |
|
2087 | 2087 | """Execute the given line magic. |
|
2088 | 2088 | |
|
2089 | 2089 | Parameters |
|
2090 | 2090 | ---------- |
|
2091 | 2091 | magic_name : str |
|
2092 | 2092 | Name of the desired magic function, without '%' prefix. |
|
2093 | 2093 | |
|
2094 | 2094 | line : str |
|
2095 | 2095 | The rest of the input line as a single string. |
|
2096 | 2096 | |
|
2097 | 2097 | _stack_depth : int |
|
2098 | 2098 | If run_line_magic() is called from magic() then _stack_depth=2. |
|
2099 | 2099 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' |
|
2100 | 2100 | """ |
|
2101 | 2101 | fn = self.find_line_magic(magic_name) |
|
2102 | 2102 | if fn is None: |
|
2103 | 2103 | cm = self.find_cell_magic(magic_name) |
|
2104 | 2104 | etpl = "Line magic function `%%%s` not found%s." |
|
2105 | 2105 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2106 | 2106 | 'did you mean that instead?)' % magic_name ) |
|
2107 | 2107 | raise UsageError(etpl % (magic_name, extra)) |
|
2108 | 2108 | else: |
|
2109 | 2109 | # Note: this is the distance in the stack to the user's frame. |
|
2110 | 2110 | # This will need to be updated if the internal calling logic gets |
|
2111 | 2111 | # refactored, or else we'll be expanding the wrong variables. |
|
2112 | 2112 | |
|
2113 | 2113 | # Determine stack_depth depending on where run_line_magic() has been called |
|
2114 | 2114 | stack_depth = _stack_depth |
|
2115 | 2115 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2116 | 2116 | # Put magic args in a list so we can call with f(*a) syntax |
|
2117 | 2117 | args = [magic_arg_s] |
|
2118 | 2118 | kwargs = {} |
|
2119 | 2119 | # Grab local namespace if we need it: |
|
2120 | 2120 | if getattr(fn, "needs_local_scope", False): |
|
2121 | 2121 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2122 | 2122 | with self.builtin_trap: |
|
2123 | 2123 | result = fn(*args,**kwargs) |
|
2124 | 2124 | return result |
|
2125 | 2125 | |
|
2126 | 2126 | def run_cell_magic(self, magic_name, line, cell): |
|
2127 | 2127 | """Execute the given cell magic. |
|
2128 | 2128 | |
|
2129 | 2129 | Parameters |
|
2130 | 2130 | ---------- |
|
2131 | 2131 | magic_name : str |
|
2132 | 2132 | Name of the desired magic function, without '%' prefix. |
|
2133 | 2133 | |
|
2134 | 2134 | line : str |
|
2135 | 2135 | The rest of the first input line as a single string. |
|
2136 | 2136 | |
|
2137 | 2137 | cell : str |
|
2138 | 2138 | The body of the cell as a (possibly multiline) string. |
|
2139 | 2139 | """ |
|
2140 | 2140 | fn = self.find_cell_magic(magic_name) |
|
2141 | 2141 | if fn is None: |
|
2142 | 2142 | lm = self.find_line_magic(magic_name) |
|
2143 | 2143 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2144 | 2144 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2145 | 2145 | 'did you mean that instead?)'.format(magic_name)) |
|
2146 | 2146 | raise UsageError(etpl.format(magic_name, extra)) |
|
2147 | 2147 | elif cell == '': |
|
2148 | 2148 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2149 | 2149 | if self.find_line_magic(magic_name) is not None: |
|
2150 | 2150 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2151 | 2151 | raise UsageError(message) |
|
2152 | 2152 | else: |
|
2153 | 2153 | # Note: this is the distance in the stack to the user's frame. |
|
2154 | 2154 | # This will need to be updated if the internal calling logic gets |
|
2155 | 2155 | # refactored, or else we'll be expanding the wrong variables. |
|
2156 | 2156 | stack_depth = 2 |
|
2157 | 2157 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2158 | 2158 | with self.builtin_trap: |
|
2159 | 2159 | result = fn(magic_arg_s, cell) |
|
2160 | 2160 | return result |
|
2161 | 2161 | |
|
2162 | 2162 | def find_line_magic(self, magic_name): |
|
2163 | 2163 | """Find and return a line magic by name. |
|
2164 | 2164 | |
|
2165 | 2165 | Returns None if the magic isn't found.""" |
|
2166 | 2166 | return self.magics_manager.magics['line'].get(magic_name) |
|
2167 | 2167 | |
|
2168 | 2168 | def find_cell_magic(self, magic_name): |
|
2169 | 2169 | """Find and return a cell magic by name. |
|
2170 | 2170 | |
|
2171 | 2171 | Returns None if the magic isn't found.""" |
|
2172 | 2172 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2173 | 2173 | |
|
2174 | 2174 | def find_magic(self, magic_name, magic_kind='line'): |
|
2175 | 2175 | """Find and return a magic of the given type by name. |
|
2176 | 2176 | |
|
2177 | 2177 | Returns None if the magic isn't found.""" |
|
2178 | 2178 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2179 | 2179 | |
|
2180 | 2180 | def magic(self, arg_s): |
|
2181 | 2181 | """DEPRECATED. Use run_line_magic() instead. |
|
2182 | 2182 | |
|
2183 | 2183 | Call a magic function by name. |
|
2184 | 2184 | |
|
2185 | 2185 | Input: a string containing the name of the magic function to call and |
|
2186 | 2186 | any additional arguments to be passed to the magic. |
|
2187 | 2187 | |
|
2188 | 2188 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2189 | 2189 | prompt: |
|
2190 | 2190 | |
|
2191 | 2191 | In[1]: %name -opt foo bar |
|
2192 | 2192 | |
|
2193 | 2193 | To call a magic without arguments, simply use magic('name'). |
|
2194 | 2194 | |
|
2195 | 2195 | This provides a proper Python function to call IPython's magics in any |
|
2196 | 2196 | valid Python code you can type at the interpreter, including loops and |
|
2197 | 2197 | compound statements. |
|
2198 | 2198 | """ |
|
2199 | 2199 | # TODO: should we issue a loud deprecation warning here? |
|
2200 | 2200 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2201 | 2201 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2202 | 2202 | return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) |
|
2203 | 2203 | |
|
2204 | 2204 | #------------------------------------------------------------------------- |
|
2205 | 2205 | # Things related to macros |
|
2206 | 2206 | #------------------------------------------------------------------------- |
|
2207 | 2207 | |
|
2208 | 2208 | def define_macro(self, name, themacro): |
|
2209 | 2209 | """Define a new macro |
|
2210 | 2210 | |
|
2211 | 2211 | Parameters |
|
2212 | 2212 | ---------- |
|
2213 | 2213 | name : str |
|
2214 | 2214 | The name of the macro. |
|
2215 | 2215 | themacro : str or Macro |
|
2216 | 2216 | The action to do upon invoking the macro. If a string, a new |
|
2217 | 2217 | Macro object is created by passing the string to it. |
|
2218 | 2218 | """ |
|
2219 | 2219 | |
|
2220 | 2220 | from IPython.core import macro |
|
2221 | 2221 | |
|
2222 | 2222 | if isinstance(themacro, str): |
|
2223 | 2223 | themacro = macro.Macro(themacro) |
|
2224 | 2224 | if not isinstance(themacro, macro.Macro): |
|
2225 | 2225 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2226 | 2226 | self.user_ns[name] = themacro |
|
2227 | 2227 | |
|
2228 | 2228 | #------------------------------------------------------------------------- |
|
2229 | 2229 | # Things related to the running of system commands |
|
2230 | 2230 | #------------------------------------------------------------------------- |
|
2231 | 2231 | |
|
2232 | 2232 | def system_piped(self, cmd): |
|
2233 | 2233 | """Call the given cmd in a subprocess, piping stdout/err |
|
2234 | 2234 | |
|
2235 | 2235 | Parameters |
|
2236 | 2236 | ---------- |
|
2237 | 2237 | cmd : str |
|
2238 | 2238 | Command to execute (can not end in '&', as background processes are |
|
2239 | 2239 | not supported. Should not be a command that expects input |
|
2240 | 2240 | other than simple text. |
|
2241 | 2241 | """ |
|
2242 | 2242 | if cmd.rstrip().endswith('&'): |
|
2243 | 2243 | # this is *far* from a rigorous test |
|
2244 | 2244 | # We do not support backgrounding processes because we either use |
|
2245 | 2245 | # pexpect or pipes to read from. Users can always just call |
|
2246 | 2246 | # os.system() or use ip.system=ip.system_raw |
|
2247 | 2247 | # if they really want a background process. |
|
2248 | 2248 | raise OSError("Background processes not supported.") |
|
2249 | 2249 | |
|
2250 | 2250 | # we explicitly do NOT return the subprocess status code, because |
|
2251 | 2251 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2252 | 2252 | # Instead, we store the exit_code in user_ns. |
|
2253 | 2253 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2254 | 2254 | |
|
2255 | 2255 | def system_raw(self, cmd): |
|
2256 | 2256 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2257 | 2257 | subprocess.call using the system shell on other platforms. |
|
2258 | 2258 | |
|
2259 | 2259 | Parameters |
|
2260 | 2260 | ---------- |
|
2261 | 2261 | cmd : str |
|
2262 | 2262 | Command to execute. |
|
2263 | 2263 | """ |
|
2264 | 2264 | cmd = self.var_expand(cmd, depth=1) |
|
2265 | 2265 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2266 | 2266 | if sys.platform == 'win32': |
|
2267 | 2267 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2268 | 2268 | with AvoidUNCPath() as path: |
|
2269 | 2269 | if path is not None: |
|
2270 | 2270 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2271 | 2271 | try: |
|
2272 | 2272 | ec = os.system(cmd) |
|
2273 | 2273 | except KeyboardInterrupt: |
|
2274 | 2274 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2275 | 2275 | ec = -2 |
|
2276 | 2276 | else: |
|
2277 | 2277 | # For posix the result of the subprocess.call() below is an exit |
|
2278 | 2278 | # code, which by convention is zero for success, positive for |
|
2279 | 2279 | # program failure. Exit codes above 128 are reserved for signals, |
|
2280 | 2280 | # and the formula for converting a signal to an exit code is usually |
|
2281 | 2281 | # signal_number+128. To more easily differentiate between exit |
|
2282 | 2282 | # codes and signals, ipython uses negative numbers. For instance |
|
2283 | 2283 | # since control-c is signal 2 but exit code 130, ipython's |
|
2284 | 2284 | # _exit_code variable will read -2. Note that some shells like |
|
2285 | 2285 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2286 | 2286 | executable = os.environ.get('SHELL', None) |
|
2287 | 2287 | try: |
|
2288 | 2288 | # Use env shell instead of default /bin/sh |
|
2289 | 2289 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2290 | 2290 | except KeyboardInterrupt: |
|
2291 | 2291 | # intercept control-C; a long traceback is not useful here |
|
2292 | 2292 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2293 | 2293 | ec = 130 |
|
2294 | 2294 | if ec > 128: |
|
2295 | 2295 | ec = -(ec - 128) |
|
2296 | 2296 | |
|
2297 | 2297 | # We explicitly do NOT return the subprocess status code, because |
|
2298 | 2298 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2299 | 2299 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2300 | 2300 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2301 | 2301 | # but raising SystemExit(_exit_code) will give status 254! |
|
2302 | 2302 | self.user_ns['_exit_code'] = ec |
|
2303 | 2303 | |
|
2304 | 2304 | # use piped system by default, because it is better behaved |
|
2305 | 2305 | system = system_piped |
|
2306 | 2306 | |
|
2307 | 2307 | def getoutput(self, cmd, split=True, depth=0): |
|
2308 | 2308 | """Get output (possibly including stderr) from a subprocess. |
|
2309 | 2309 | |
|
2310 | 2310 | Parameters |
|
2311 | 2311 | ---------- |
|
2312 | 2312 | cmd : str |
|
2313 | 2313 | Command to execute (can not end in '&', as background processes are |
|
2314 | 2314 | not supported. |
|
2315 | 2315 | split : bool, optional |
|
2316 | 2316 | If True, split the output into an IPython SList. Otherwise, an |
|
2317 | 2317 | IPython LSString is returned. These are objects similar to normal |
|
2318 | 2318 | lists and strings, with a few convenience attributes for easier |
|
2319 | 2319 | manipulation of line-based output. You can use '?' on them for |
|
2320 | 2320 | details. |
|
2321 | 2321 | depth : int, optional |
|
2322 | 2322 | How many frames above the caller are the local variables which should |
|
2323 | 2323 | be expanded in the command string? The default (0) assumes that the |
|
2324 | 2324 | expansion variables are in the stack frame calling this function. |
|
2325 | 2325 | """ |
|
2326 | 2326 | if cmd.rstrip().endswith('&'): |
|
2327 | 2327 | # this is *far* from a rigorous test |
|
2328 | 2328 | raise OSError("Background processes not supported.") |
|
2329 | 2329 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2330 | 2330 | if split: |
|
2331 | 2331 | out = SList(out.splitlines()) |
|
2332 | 2332 | else: |
|
2333 | 2333 | out = LSString(out) |
|
2334 | 2334 | return out |
|
2335 | 2335 | |
|
2336 | 2336 | #------------------------------------------------------------------------- |
|
2337 | 2337 | # Things related to aliases |
|
2338 | 2338 | #------------------------------------------------------------------------- |
|
2339 | 2339 | |
|
2340 | 2340 | def init_alias(self): |
|
2341 | 2341 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2342 | 2342 | self.configurables.append(self.alias_manager) |
|
2343 | 2343 | |
|
2344 | 2344 | #------------------------------------------------------------------------- |
|
2345 | 2345 | # Things related to extensions |
|
2346 | 2346 | #------------------------------------------------------------------------- |
|
2347 | 2347 | |
|
2348 | 2348 | def init_extension_manager(self): |
|
2349 | 2349 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2350 | 2350 | self.configurables.append(self.extension_manager) |
|
2351 | 2351 | |
|
2352 | 2352 | #------------------------------------------------------------------------- |
|
2353 | 2353 | # Things related to payloads |
|
2354 | 2354 | #------------------------------------------------------------------------- |
|
2355 | 2355 | |
|
2356 | 2356 | def init_payload(self): |
|
2357 | 2357 | self.payload_manager = PayloadManager(parent=self) |
|
2358 | 2358 | self.configurables.append(self.payload_manager) |
|
2359 | 2359 | |
|
2360 | 2360 | #------------------------------------------------------------------------- |
|
2361 | 2361 | # Things related to the prefilter |
|
2362 | 2362 | #------------------------------------------------------------------------- |
|
2363 | 2363 | |
|
2364 | 2364 | def init_prefilter(self): |
|
2365 | 2365 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2366 | 2366 | self.configurables.append(self.prefilter_manager) |
|
2367 | 2367 | # Ultimately this will be refactored in the new interpreter code, but |
|
2368 | 2368 | # for now, we should expose the main prefilter method (there's legacy |
|
2369 | 2369 | # code out there that may rely on this). |
|
2370 | 2370 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2371 | 2371 | |
|
2372 | 2372 | def auto_rewrite_input(self, cmd): |
|
2373 | 2373 | """Print to the screen the rewritten form of the user's command. |
|
2374 | 2374 | |
|
2375 | 2375 | This shows visual feedback by rewriting input lines that cause |
|
2376 | 2376 | automatic calling to kick in, like:: |
|
2377 | 2377 | |
|
2378 | 2378 | /f x |
|
2379 | 2379 | |
|
2380 | 2380 | into:: |
|
2381 | 2381 | |
|
2382 | 2382 | ------> f(x) |
|
2383 | 2383 | |
|
2384 | 2384 | after the user's input prompt. This helps the user understand that the |
|
2385 | 2385 | input line was transformed automatically by IPython. |
|
2386 | 2386 | """ |
|
2387 | 2387 | if not self.show_rewritten_input: |
|
2388 | 2388 | return |
|
2389 | 2389 | |
|
2390 | 2390 | # This is overridden in TerminalInteractiveShell to use fancy prompts |
|
2391 | 2391 | print("------> " + cmd) |
|
2392 | 2392 | |
|
2393 | 2393 | #------------------------------------------------------------------------- |
|
2394 | 2394 | # Things related to extracting values/expressions from kernel and user_ns |
|
2395 | 2395 | #------------------------------------------------------------------------- |
|
2396 | 2396 | |
|
2397 | 2397 | def _user_obj_error(self): |
|
2398 | 2398 | """return simple exception dict |
|
2399 | 2399 | |
|
2400 | 2400 | for use in user_expressions |
|
2401 | 2401 | """ |
|
2402 | 2402 | |
|
2403 | 2403 | etype, evalue, tb = self._get_exc_info() |
|
2404 | 2404 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2405 | 2405 | |
|
2406 | 2406 | exc_info = { |
|
2407 | 2407 | u'status' : 'error', |
|
2408 | 2408 | u'traceback' : stb, |
|
2409 | 2409 | u'ename' : etype.__name__, |
|
2410 | 2410 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2411 | 2411 | } |
|
2412 | 2412 | |
|
2413 | 2413 | return exc_info |
|
2414 | 2414 | |
|
2415 | 2415 | def _format_user_obj(self, obj): |
|
2416 | 2416 | """format a user object to display dict |
|
2417 | 2417 | |
|
2418 | 2418 | for use in user_expressions |
|
2419 | 2419 | """ |
|
2420 | 2420 | |
|
2421 | 2421 | data, md = self.display_formatter.format(obj) |
|
2422 | 2422 | value = { |
|
2423 | 2423 | 'status' : 'ok', |
|
2424 | 2424 | 'data' : data, |
|
2425 | 2425 | 'metadata' : md, |
|
2426 | 2426 | } |
|
2427 | 2427 | return value |
|
2428 | 2428 | |
|
2429 | 2429 | def user_expressions(self, expressions): |
|
2430 | 2430 | """Evaluate a dict of expressions in the user's namespace. |
|
2431 | 2431 | |
|
2432 | 2432 | Parameters |
|
2433 | 2433 | ---------- |
|
2434 | 2434 | expressions : dict |
|
2435 | 2435 | A dict with string keys and string values. The expression values |
|
2436 | 2436 | should be valid Python expressions, each of which will be evaluated |
|
2437 | 2437 | in the user namespace. |
|
2438 | 2438 | |
|
2439 | 2439 | Returns |
|
2440 | 2440 | ------- |
|
2441 | 2441 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2442 | 2442 | display_data of each value. |
|
2443 | 2443 | """ |
|
2444 | 2444 | out = {} |
|
2445 | 2445 | user_ns = self.user_ns |
|
2446 | 2446 | global_ns = self.user_global_ns |
|
2447 | 2447 | |
|
2448 | 2448 | for key, expr in expressions.items(): |
|
2449 | 2449 | try: |
|
2450 | 2450 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2451 | 2451 | except: |
|
2452 | 2452 | value = self._user_obj_error() |
|
2453 | 2453 | out[key] = value |
|
2454 | 2454 | return out |
|
2455 | 2455 | |
|
2456 | 2456 | #------------------------------------------------------------------------- |
|
2457 | 2457 | # Things related to the running of code |
|
2458 | 2458 | #------------------------------------------------------------------------- |
|
2459 | 2459 | |
|
2460 | 2460 | def ex(self, cmd): |
|
2461 | 2461 | """Execute a normal python statement in user namespace.""" |
|
2462 | 2462 | with self.builtin_trap: |
|
2463 | 2463 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2464 | 2464 | |
|
2465 | 2465 | def ev(self, expr): |
|
2466 | 2466 | """Evaluate python expression expr in user namespace. |
|
2467 | 2467 | |
|
2468 | 2468 | Returns the result of evaluation |
|
2469 | 2469 | """ |
|
2470 | 2470 | with self.builtin_trap: |
|
2471 | 2471 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2472 | 2472 | |
|
2473 | 2473 | def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False): |
|
2474 | 2474 | """A safe version of the builtin execfile(). |
|
2475 | 2475 | |
|
2476 | 2476 | This version will never throw an exception, but instead print |
|
2477 | 2477 | helpful error messages to the screen. This only works on pure |
|
2478 | 2478 | Python files with the .py extension. |
|
2479 | 2479 | |
|
2480 | 2480 | Parameters |
|
2481 | 2481 | ---------- |
|
2482 | 2482 | fname : string |
|
2483 | 2483 | The name of the file to be executed. |
|
2484 | 2484 | where : tuple |
|
2485 | 2485 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2486 | 2486 | If only one is given, it is passed as both. |
|
2487 | 2487 | exit_ignore : bool (False) |
|
2488 | 2488 | If True, then silence SystemExit for non-zero status (it is always |
|
2489 | 2489 | silenced for zero status, as it is so common). |
|
2490 | 2490 | raise_exceptions : bool (False) |
|
2491 | 2491 | If True raise exceptions everywhere. Meant for testing. |
|
2492 | 2492 | shell_futures : bool (False) |
|
2493 | 2493 | If True, the code will share future statements with the interactive |
|
2494 | 2494 | shell. It will both be affected by previous __future__ imports, and |
|
2495 | 2495 | any __future__ imports in the code will affect the shell. If False, |
|
2496 | 2496 | __future__ imports are not shared in either direction. |
|
2497 | 2497 | |
|
2498 | 2498 | """ |
|
2499 | 2499 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2500 | 2500 | |
|
2501 | 2501 | # Make sure we can open the file |
|
2502 | 2502 | try: |
|
2503 | 2503 | with open(fname): |
|
2504 | 2504 | pass |
|
2505 | 2505 | except: |
|
2506 | 2506 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2507 | 2507 | return |
|
2508 | 2508 | |
|
2509 | 2509 | # Find things also in current directory. This is needed to mimic the |
|
2510 | 2510 | # behavior of running a script from the system command line, where |
|
2511 | 2511 | # Python inserts the script's directory into sys.path |
|
2512 | 2512 | dname = os.path.dirname(fname) |
|
2513 | 2513 | |
|
2514 | 2514 | with prepended_to_syspath(dname), self.builtin_trap: |
|
2515 | 2515 | try: |
|
2516 | 2516 | glob, loc = (where + (None, ))[:2] |
|
2517 | 2517 | py3compat.execfile( |
|
2518 | 2518 | fname, glob, loc, |
|
2519 | 2519 | self.compile if shell_futures else None) |
|
2520 | 2520 | except SystemExit as status: |
|
2521 | 2521 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2522 | 2522 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2523 | 2523 | # these are considered normal by the OS: |
|
2524 | 2524 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2525 | 2525 | # 0 |
|
2526 | 2526 | # > python -c'import sys;sys.exit()'; echo $? |
|
2527 | 2527 | # 0 |
|
2528 | 2528 | # For other exit status, we show the exception unless |
|
2529 | 2529 | # explicitly silenced, but only in short form. |
|
2530 | 2530 | if status.code: |
|
2531 | 2531 | if raise_exceptions: |
|
2532 | 2532 | raise |
|
2533 | 2533 | if not exit_ignore: |
|
2534 | 2534 | self.showtraceback(exception_only=True) |
|
2535 | 2535 | except: |
|
2536 | 2536 | if raise_exceptions: |
|
2537 | 2537 | raise |
|
2538 | 2538 | # tb offset is 2 because we wrap execfile |
|
2539 | 2539 | self.showtraceback(tb_offset=2) |
|
2540 | 2540 | |
|
2541 | 2541 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2542 | 2542 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2543 | 2543 | |
|
2544 | 2544 | Parameters |
|
2545 | 2545 | ---------- |
|
2546 | 2546 | fname : str |
|
2547 | 2547 | The name of the file to execute. The filename must have a |
|
2548 | 2548 | .ipy or .ipynb extension. |
|
2549 | 2549 | shell_futures : bool (False) |
|
2550 | 2550 | If True, the code will share future statements with the interactive |
|
2551 | 2551 | shell. It will both be affected by previous __future__ imports, and |
|
2552 | 2552 | any __future__ imports in the code will affect the shell. If False, |
|
2553 | 2553 | __future__ imports are not shared in either direction. |
|
2554 | 2554 | raise_exceptions : bool (False) |
|
2555 | 2555 | If True raise exceptions everywhere. Meant for testing. |
|
2556 | 2556 | """ |
|
2557 | 2557 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2558 | 2558 | |
|
2559 | 2559 | # Make sure we can open the file |
|
2560 | 2560 | try: |
|
2561 | 2561 | with open(fname): |
|
2562 | 2562 | pass |
|
2563 | 2563 | except: |
|
2564 | 2564 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2565 | 2565 | return |
|
2566 | 2566 | |
|
2567 | 2567 | # Find things also in current directory. This is needed to mimic the |
|
2568 | 2568 | # behavior of running a script from the system command line, where |
|
2569 | 2569 | # Python inserts the script's directory into sys.path |
|
2570 | 2570 | dname = os.path.dirname(fname) |
|
2571 | 2571 | |
|
2572 | 2572 | def get_cells(): |
|
2573 | 2573 | """generator for sequence of code blocks to run""" |
|
2574 | 2574 | if fname.endswith('.ipynb'): |
|
2575 | 2575 | from nbformat import read |
|
2576 | 2576 | nb = read(fname, as_version=4) |
|
2577 | 2577 | if not nb.cells: |
|
2578 | 2578 | return |
|
2579 | 2579 | for cell in nb.cells: |
|
2580 | 2580 | if cell.cell_type == 'code': |
|
2581 | 2581 | yield cell.source |
|
2582 | 2582 | else: |
|
2583 | 2583 | with open(fname) as f: |
|
2584 | 2584 | yield f.read() |
|
2585 | 2585 | |
|
2586 | 2586 | with prepended_to_syspath(dname): |
|
2587 | 2587 | try: |
|
2588 | 2588 | for cell in get_cells(): |
|
2589 | 2589 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2590 | 2590 | if raise_exceptions: |
|
2591 | 2591 | result.raise_error() |
|
2592 | 2592 | elif not result.success: |
|
2593 | 2593 | break |
|
2594 | 2594 | except: |
|
2595 | 2595 | if raise_exceptions: |
|
2596 | 2596 | raise |
|
2597 | 2597 | self.showtraceback() |
|
2598 | 2598 | warn('Unknown failure executing file: <%s>' % fname) |
|
2599 | 2599 | |
|
2600 | 2600 | def safe_run_module(self, mod_name, where): |
|
2601 | 2601 | """A safe version of runpy.run_module(). |
|
2602 | 2602 | |
|
2603 | 2603 | This version will never throw an exception, but instead print |
|
2604 | 2604 | helpful error messages to the screen. |
|
2605 | 2605 | |
|
2606 | 2606 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2607 | 2607 | |
|
2608 | 2608 | Parameters |
|
2609 | 2609 | ---------- |
|
2610 | 2610 | mod_name : string |
|
2611 | 2611 | The name of the module to be executed. |
|
2612 | 2612 | where : dict |
|
2613 | 2613 | The globals namespace. |
|
2614 | 2614 | """ |
|
2615 | 2615 | try: |
|
2616 | 2616 | try: |
|
2617 | 2617 | where.update( |
|
2618 | 2618 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2619 | 2619 | alter_sys=True) |
|
2620 | 2620 | ) |
|
2621 | 2621 | except SystemExit as status: |
|
2622 | 2622 | if status.code: |
|
2623 | 2623 | raise |
|
2624 | 2624 | except: |
|
2625 | 2625 | self.showtraceback() |
|
2626 | 2626 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2627 | 2627 | |
|
2628 | 2628 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2629 | 2629 | """Run a complete IPython cell. |
|
2630 | 2630 | |
|
2631 | 2631 | Parameters |
|
2632 | 2632 | ---------- |
|
2633 | 2633 | raw_cell : str |
|
2634 | 2634 | The code (including IPython code such as %magic functions) to run. |
|
2635 | 2635 | store_history : bool |
|
2636 | 2636 | If True, the raw and translated cell will be stored in IPython's |
|
2637 | 2637 | history. For user code calling back into IPython's machinery, this |
|
2638 | 2638 | should be set to False. |
|
2639 | 2639 | silent : bool |
|
2640 | 2640 | If True, avoid side-effects, such as implicit displayhooks and |
|
2641 | 2641 | and logging. silent=True forces store_history=False. |
|
2642 | 2642 | shell_futures : bool |
|
2643 | 2643 | If True, the code will share future statements with the interactive |
|
2644 | 2644 | shell. It will both be affected by previous __future__ imports, and |
|
2645 | 2645 | any __future__ imports in the code will affect the shell. If False, |
|
2646 | 2646 | __future__ imports are not shared in either direction. |
|
2647 | 2647 | |
|
2648 | 2648 | Returns |
|
2649 | 2649 | ------- |
|
2650 | 2650 | result : :class:`ExecutionResult` |
|
2651 | 2651 | """ |
|
2652 | 2652 | try: |
|
2653 | 2653 | result = self._run_cell( |
|
2654 | 2654 | raw_cell, store_history, silent, shell_futures) |
|
2655 | 2655 | finally: |
|
2656 | 2656 | self.events.trigger('post_execute') |
|
2657 | 2657 | if not silent: |
|
2658 | 2658 | self.events.trigger('post_run_cell', result) |
|
2659 | 2659 | return result |
|
2660 | 2660 | |
|
2661 | 2661 | def _run_cell(self, raw_cell, store_history, silent, shell_futures): |
|
2662 | 2662 | """Internal method to run a complete IPython cell. |
|
2663 | 2663 | |
|
2664 | 2664 | Parameters |
|
2665 | 2665 | ---------- |
|
2666 | 2666 | raw_cell : str |
|
2667 | 2667 | store_history : bool |
|
2668 | 2668 | silent : bool |
|
2669 | 2669 | shell_futures : bool |
|
2670 | 2670 | |
|
2671 | 2671 | Returns |
|
2672 | 2672 | ------- |
|
2673 | 2673 | result : :class:`ExecutionResult` |
|
2674 | 2674 | """ |
|
2675 | 2675 | info = ExecutionInfo( |
|
2676 | 2676 | raw_cell, store_history, silent, shell_futures) |
|
2677 | 2677 | result = ExecutionResult(info) |
|
2678 | 2678 | |
|
2679 | 2679 | if (not raw_cell) or raw_cell.isspace(): |
|
2680 | 2680 | self.last_execution_succeeded = True |
|
2681 | 2681 | self.last_execution_result = result |
|
2682 | 2682 | return result |
|
2683 | 2683 | |
|
2684 | 2684 | if silent: |
|
2685 | 2685 | store_history = False |
|
2686 | 2686 | |
|
2687 | 2687 | if store_history: |
|
2688 | 2688 | result.execution_count = self.execution_count |
|
2689 | 2689 | |
|
2690 | 2690 | def error_before_exec(value): |
|
2691 | 2691 | result.error_before_exec = value |
|
2692 | 2692 | self.last_execution_succeeded = False |
|
2693 | 2693 | self.last_execution_result = result |
|
2694 | 2694 | return result |
|
2695 | 2695 | |
|
2696 | 2696 | self.events.trigger('pre_execute') |
|
2697 | 2697 | if not silent: |
|
2698 | 2698 | self.events.trigger('pre_run_cell', info) |
|
2699 | 2699 | |
|
2700 | 2700 | # If any of our input transformation (input_transformer_manager or |
|
2701 | 2701 | # prefilter_manager) raises an exception, we store it in this variable |
|
2702 | 2702 | # so that we can display the error after logging the input and storing |
|
2703 | 2703 | # it in the history. |
|
2704 | 2704 | preprocessing_exc_tuple = None |
|
2705 | 2705 | try: |
|
2706 | 2706 | # Static input transformations |
|
2707 | 2707 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
2708 | 2708 | except SyntaxError: |
|
2709 | 2709 | preprocessing_exc_tuple = sys.exc_info() |
|
2710 | 2710 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2711 | 2711 | else: |
|
2712 | 2712 | if len(cell.splitlines()) == 1: |
|
2713 | 2713 | # Dynamic transformations - only applied for single line commands |
|
2714 | 2714 | with self.builtin_trap: |
|
2715 | 2715 | try: |
|
2716 | 2716 | # use prefilter_lines to handle trailing newlines |
|
2717 | 2717 | # restore trailing newline for ast.parse |
|
2718 | 2718 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2719 | 2719 | except Exception: |
|
2720 | 2720 | # don't allow prefilter errors to crash IPython |
|
2721 | 2721 | preprocessing_exc_tuple = sys.exc_info() |
|
2722 | 2722 | |
|
2723 | 2723 | # Store raw and processed history |
|
2724 | 2724 | if store_history: |
|
2725 | 2725 | self.history_manager.store_inputs(self.execution_count, |
|
2726 | 2726 | cell, raw_cell) |
|
2727 | 2727 | if not silent: |
|
2728 | 2728 | self.logger.log(cell, raw_cell) |
|
2729 | 2729 | |
|
2730 | 2730 | # Display the exception if input processing failed. |
|
2731 | 2731 | if preprocessing_exc_tuple is not None: |
|
2732 | 2732 | self.showtraceback(preprocessing_exc_tuple) |
|
2733 | 2733 | if store_history: |
|
2734 | 2734 | self.execution_count += 1 |
|
2735 | 2735 | return error_before_exec(preprocessing_exc_tuple[2]) |
|
2736 | 2736 | |
|
2737 | 2737 | # Our own compiler remembers the __future__ environment. If we want to |
|
2738 | 2738 | # run code with a separate __future__ environment, use the default |
|
2739 | 2739 | # compiler |
|
2740 | 2740 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2741 | 2741 | |
|
2742 | 2742 | with self.builtin_trap: |
|
2743 | 2743 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2744 | 2744 | |
|
2745 | 2745 | with self.display_trap: |
|
2746 | 2746 | # Compile to bytecode |
|
2747 | 2747 | try: |
|
2748 | 2748 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2749 | 2749 | except self.custom_exceptions as e: |
|
2750 | 2750 | etype, value, tb = sys.exc_info() |
|
2751 | 2751 | self.CustomTB(etype, value, tb) |
|
2752 | 2752 | return error_before_exec(e) |
|
2753 | 2753 | except IndentationError as e: |
|
2754 | 2754 | self.showindentationerror() |
|
2755 | 2755 | if store_history: |
|
2756 | 2756 | self.execution_count += 1 |
|
2757 | 2757 | return error_before_exec(e) |
|
2758 | 2758 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2759 | 2759 | MemoryError) as e: |
|
2760 | 2760 | self.showsyntaxerror() |
|
2761 | 2761 | if store_history: |
|
2762 | 2762 | self.execution_count += 1 |
|
2763 | 2763 | return error_before_exec(e) |
|
2764 | 2764 | |
|
2765 | 2765 | # Apply AST transformations |
|
2766 | 2766 | try: |
|
2767 | 2767 | code_ast = self.transform_ast(code_ast) |
|
2768 | 2768 | except InputRejected as e: |
|
2769 | 2769 | self.showtraceback() |
|
2770 | 2770 | if store_history: |
|
2771 | 2771 | self.execution_count += 1 |
|
2772 | 2772 | return error_before_exec(e) |
|
2773 | 2773 | |
|
2774 | 2774 | # Give the displayhook a reference to our ExecutionResult so it |
|
2775 | 2775 | # can fill in the output value. |
|
2776 | 2776 | self.displayhook.exec_result = result |
|
2777 | 2777 | |
|
2778 | 2778 | # Execute the user code |
|
2779 | 2779 | interactivity = 'none' if silent else self.ast_node_interactivity |
|
2780 | 2780 | has_raised = self.run_ast_nodes(code_ast.body, cell_name, |
|
2781 | 2781 | interactivity=interactivity, compiler=compiler, result=result) |
|
2782 | 2782 | |
|
2783 | 2783 | self.last_execution_succeeded = not has_raised |
|
2784 | 2784 | self.last_execution_result = result |
|
2785 | 2785 | |
|
2786 | 2786 | # Reset this so later displayed values do not modify the |
|
2787 | 2787 | # ExecutionResult |
|
2788 | 2788 | self.displayhook.exec_result = None |
|
2789 | 2789 | |
|
2790 | 2790 | if store_history: |
|
2791 | 2791 | # Write output to the database. Does nothing unless |
|
2792 | 2792 | # history output logging is enabled. |
|
2793 | 2793 | self.history_manager.store_output(self.execution_count) |
|
2794 | 2794 | # Each cell is a *single* input, regardless of how many lines it has |
|
2795 | 2795 | self.execution_count += 1 |
|
2796 | 2796 | |
|
2797 | 2797 | return result |
|
2798 | 2798 | |
|
2799 | 2799 | def transform_ast(self, node): |
|
2800 | 2800 | """Apply the AST transformations from self.ast_transformers |
|
2801 | 2801 | |
|
2802 | 2802 | Parameters |
|
2803 | 2803 | ---------- |
|
2804 | 2804 | node : ast.Node |
|
2805 | 2805 | The root node to be transformed. Typically called with the ast.Module |
|
2806 | 2806 | produced by parsing user input. |
|
2807 | 2807 | |
|
2808 | 2808 | Returns |
|
2809 | 2809 | ------- |
|
2810 | 2810 | An ast.Node corresponding to the node it was called with. Note that it |
|
2811 | 2811 | may also modify the passed object, so don't rely on references to the |
|
2812 | 2812 | original AST. |
|
2813 | 2813 | """ |
|
2814 | 2814 | for transformer in self.ast_transformers: |
|
2815 | 2815 | try: |
|
2816 | 2816 | node = transformer.visit(node) |
|
2817 | 2817 | except InputRejected: |
|
2818 | 2818 | # User-supplied AST transformers can reject an input by raising |
|
2819 | 2819 | # an InputRejected. Short-circuit in this case so that we |
|
2820 | 2820 | # don't unregister the transform. |
|
2821 | 2821 | raise |
|
2822 | 2822 | except Exception: |
|
2823 | 2823 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2824 | 2824 | self.ast_transformers.remove(transformer) |
|
2825 | 2825 | |
|
2826 | 2826 | if self.ast_transformers: |
|
2827 | 2827 | ast.fix_missing_locations(node) |
|
2828 | 2828 | return node |
|
2829 | 2829 | |
|
2830 | 2830 | |
|
2831 | 2831 | def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr', |
|
2832 | 2832 | compiler=compile, result=None): |
|
2833 | 2833 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2834 | 2834 | interactivity parameter. |
|
2835 | 2835 | |
|
2836 | 2836 | Parameters |
|
2837 | 2837 | ---------- |
|
2838 | 2838 | nodelist : list |
|
2839 | 2839 | A sequence of AST nodes to run. |
|
2840 | 2840 | cell_name : str |
|
2841 | 2841 | Will be passed to the compiler as the filename of the cell. Typically |
|
2842 | 2842 | the value returned by ip.compile.cache(cell). |
|
2843 | 2843 | interactivity : str |
|
2844 | 2844 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', |
|
2845 | 2845 | specifying which nodes should be run interactively (displaying output |
|
2846 | 2846 | from expressions). 'last_expr' will run the last node interactively |
|
2847 | 2847 | only if it is an expression (i.e. expressions in loops or other blocks |
|
2848 | 2848 | are not displayed) 'last_expr_or_assign' will run the last expression |
|
2849 | 2849 | or the last assignment. Other values for this parameter will raise a |
|
2850 | 2850 | ValueError. |
|
2851 | 2851 | compiler : callable |
|
2852 | 2852 | A function with the same interface as the built-in compile(), to turn |
|
2853 | 2853 | the AST nodes into code objects. Default is the built-in compile(). |
|
2854 | 2854 | result : ExecutionResult, optional |
|
2855 | 2855 | An object to store exceptions that occur during execution. |
|
2856 | 2856 | |
|
2857 | 2857 | Returns |
|
2858 | 2858 | ------- |
|
2859 | 2859 | True if an exception occurred while running code, False if it finished |
|
2860 | 2860 | running. |
|
2861 | 2861 | """ |
|
2862 | 2862 | if not nodelist: |
|
2863 | 2863 | return |
|
2864 | 2864 | |
|
2865 | 2865 | if interactivity == 'last_expr_or_assign': |
|
2866 | 2866 | if isinstance(nodelist[-1], _assign_nodes): |
|
2867 | 2867 | asg = nodelist[-1] |
|
2868 | 2868 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: |
|
2869 | 2869 | target = asg.targets[0] |
|
2870 | 2870 | elif isinstance(asg, _single_targets_nodes): |
|
2871 | 2871 | target = asg.target |
|
2872 | 2872 | else: |
|
2873 | 2873 | target = None |
|
2874 | 2874 | if isinstance(target, ast.Name): |
|
2875 | 2875 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) |
|
2876 | 2876 | ast.fix_missing_locations(nnode) |
|
2877 | 2877 | nodelist.append(nnode) |
|
2878 | 2878 | interactivity = 'last_expr' |
|
2879 | 2879 | |
|
2880 | 2880 | if interactivity == 'last_expr': |
|
2881 | 2881 | if isinstance(nodelist[-1], ast.Expr): |
|
2882 | 2882 | interactivity = "last" |
|
2883 | 2883 | else: |
|
2884 | 2884 | interactivity = "none" |
|
2885 | 2885 | |
|
2886 | 2886 | if interactivity == 'none': |
|
2887 | 2887 | to_run_exec, to_run_interactive = nodelist, [] |
|
2888 | 2888 | elif interactivity == 'last': |
|
2889 | 2889 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2890 | 2890 | elif interactivity == 'all': |
|
2891 | 2891 | to_run_exec, to_run_interactive = [], nodelist |
|
2892 | 2892 | else: |
|
2893 | 2893 | raise ValueError("Interactivity was %r" % interactivity) |
|
2894 | 2894 | |
|
2895 | 2895 | try: |
|
2896 | 2896 | for i, node in enumerate(to_run_exec): |
|
2897 | 2897 | mod = ast.Module([node]) |
|
2898 | 2898 | code = compiler(mod, cell_name, "exec") |
|
2899 | 2899 | if self.run_code(code, result): |
|
2900 | 2900 | return True |
|
2901 | 2901 | |
|
2902 | 2902 | for i, node in enumerate(to_run_interactive): |
|
2903 | 2903 | mod = ast.Interactive([node]) |
|
2904 | 2904 | code = compiler(mod, cell_name, "single") |
|
2905 | 2905 | if self.run_code(code, result): |
|
2906 | 2906 | return True |
|
2907 | 2907 | |
|
2908 | 2908 | # Flush softspace |
|
2909 | 2909 | if softspace(sys.stdout, 0): |
|
2910 | 2910 | print() |
|
2911 | 2911 | |
|
2912 | 2912 | except: |
|
2913 | 2913 | # It's possible to have exceptions raised here, typically by |
|
2914 | 2914 | # compilation of odd code (such as a naked 'return' outside a |
|
2915 | 2915 | # function) that did parse but isn't valid. Typically the exception |
|
2916 | 2916 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2917 | 2917 | # the user a traceback. |
|
2918 | 2918 | |
|
2919 | 2919 | # We do only one try/except outside the loop to minimize the impact |
|
2920 | 2920 | # on runtime, and also because if any node in the node list is |
|
2921 | 2921 | # broken, we should stop execution completely. |
|
2922 | 2922 | if result: |
|
2923 | 2923 | result.error_before_exec = sys.exc_info()[1] |
|
2924 | 2924 | self.showtraceback() |
|
2925 | 2925 | return True |
|
2926 | 2926 | |
|
2927 | 2927 | return False |
|
2928 | 2928 | |
|
2929 | 2929 | def run_code(self, code_obj, result=None): |
|
2930 | 2930 | """Execute a code object. |
|
2931 | 2931 | |
|
2932 | 2932 | When an exception occurs, self.showtraceback() is called to display a |
|
2933 | 2933 | traceback. |
|
2934 | 2934 | |
|
2935 | 2935 | Parameters |
|
2936 | 2936 | ---------- |
|
2937 | 2937 | code_obj : code object |
|
2938 | 2938 | A compiled code object, to be executed |
|
2939 | 2939 | result : ExecutionResult, optional |
|
2940 | 2940 | An object to store exceptions that occur during execution. |
|
2941 | 2941 | |
|
2942 | 2942 | Returns |
|
2943 | 2943 | ------- |
|
2944 | 2944 | False : successful execution. |
|
2945 | 2945 | True : an error occurred. |
|
2946 | 2946 | """ |
|
2947 | 2947 | # Set our own excepthook in case the user code tries to call it |
|
2948 | 2948 | # directly, so that the IPython crash handler doesn't get triggered |
|
2949 | 2949 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
2950 | 2950 | |
|
2951 | 2951 | # we save the original sys.excepthook in the instance, in case config |
|
2952 | 2952 | # code (such as magics) needs access to it. |
|
2953 | 2953 | self.sys_excepthook = old_excepthook |
|
2954 | 2954 | outflag = True # happens in more places, so it's easier as default |
|
2955 | 2955 | try: |
|
2956 | 2956 | try: |
|
2957 | 2957 | self.hooks.pre_run_code_hook() |
|
2958 | 2958 | #rprint('Running code', repr(code_obj)) # dbg |
|
2959 | 2959 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
2960 | 2960 | finally: |
|
2961 | 2961 | # Reset our crash handler in place |
|
2962 | 2962 | sys.excepthook = old_excepthook |
|
2963 | 2963 | except SystemExit as e: |
|
2964 | 2964 | if result is not None: |
|
2965 | 2965 | result.error_in_exec = e |
|
2966 | 2966 | self.showtraceback(exception_only=True) |
|
2967 | 2967 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) |
|
2968 | 2968 | except self.custom_exceptions: |
|
2969 | 2969 | etype, value, tb = sys.exc_info() |
|
2970 | 2970 | if result is not None: |
|
2971 | 2971 | result.error_in_exec = value |
|
2972 | 2972 | self.CustomTB(etype, value, tb) |
|
2973 | 2973 | except: |
|
2974 | 2974 | if result is not None: |
|
2975 | 2975 | result.error_in_exec = sys.exc_info()[1] |
|
2976 | 2976 | self.showtraceback(running_compiled_code=True) |
|
2977 | 2977 | else: |
|
2978 | 2978 | outflag = False |
|
2979 | 2979 | return outflag |
|
2980 | 2980 | |
|
2981 | 2981 | # For backwards compatibility |
|
2982 | 2982 | runcode = run_code |
|
2983 | 2983 | |
|
2984 | 2984 | #------------------------------------------------------------------------- |
|
2985 | 2985 | # Things related to GUI support and pylab |
|
2986 | 2986 | #------------------------------------------------------------------------- |
|
2987 | 2987 | |
|
2988 | 2988 | active_eventloop = None |
|
2989 | 2989 | |
|
2990 | 2990 | def enable_gui(self, gui=None): |
|
2991 | 2991 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2992 | 2992 | |
|
2993 | 2993 | def enable_matplotlib(self, gui=None): |
|
2994 | 2994 | """Enable interactive matplotlib and inline figure support. |
|
2995 | 2995 | |
|
2996 | 2996 | This takes the following steps: |
|
2997 | 2997 | |
|
2998 | 2998 | 1. select the appropriate eventloop and matplotlib backend |
|
2999 | 2999 | 2. set up matplotlib for interactive use with that backend |
|
3000 | 3000 | 3. configure formatters for inline figure display |
|
3001 | 3001 | 4. enable the selected gui eventloop |
|
3002 | 3002 | |
|
3003 | 3003 | Parameters |
|
3004 | 3004 | ---------- |
|
3005 | 3005 | gui : optional, string |
|
3006 | 3006 | If given, dictates the choice of matplotlib GUI backend to use |
|
3007 | 3007 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3008 | 3008 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3009 | 3009 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3010 | 3010 | user's matplotlibrc configuration file). Note that not all backends |
|
3011 | 3011 | make sense in all contexts, for example a terminal ipython can't |
|
3012 | 3012 | display figures inline. |
|
3013 | 3013 | """ |
|
3014 | 3014 | from IPython.core import pylabtools as pt |
|
3015 | 3015 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
3016 | 3016 | |
|
3017 | 3017 | if gui != 'inline': |
|
3018 | 3018 | # If we have our first gui selection, store it |
|
3019 | 3019 | if self.pylab_gui_select is None: |
|
3020 | 3020 | self.pylab_gui_select = gui |
|
3021 | 3021 | # Otherwise if they are different |
|
3022 | 3022 | elif gui != self.pylab_gui_select: |
|
3023 | 3023 | print('Warning: Cannot change to a different GUI toolkit: %s.' |
|
3024 | 3024 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
3025 | 3025 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
3026 | 3026 | |
|
3027 | 3027 | pt.activate_matplotlib(backend) |
|
3028 | 3028 | pt.configure_inline_support(self, backend) |
|
3029 | 3029 | |
|
3030 | 3030 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
3031 | 3031 | # plot updates into account |
|
3032 | 3032 | self.enable_gui(gui) |
|
3033 | 3033 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
3034 | 3034 | pt.mpl_runner(self.safe_execfile) |
|
3035 | 3035 | |
|
3036 | 3036 | return gui, backend |
|
3037 | 3037 | |
|
3038 | 3038 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
3039 | 3039 | """Activate pylab support at runtime. |
|
3040 | 3040 | |
|
3041 | 3041 | This turns on support for matplotlib, preloads into the interactive |
|
3042 | 3042 | namespace all of numpy and pylab, and configures IPython to correctly |
|
3043 | 3043 | interact with the GUI event loop. The GUI backend to be used can be |
|
3044 | 3044 | optionally selected with the optional ``gui`` argument. |
|
3045 | 3045 | |
|
3046 | 3046 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
3047 | 3047 | |
|
3048 | 3048 | Parameters |
|
3049 | 3049 | ---------- |
|
3050 | 3050 | gui : optional, string |
|
3051 | 3051 | If given, dictates the choice of matplotlib GUI backend to use |
|
3052 | 3052 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3053 | 3053 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3054 | 3054 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3055 | 3055 | user's matplotlibrc configuration file). Note that not all backends |
|
3056 | 3056 | make sense in all contexts, for example a terminal ipython can't |
|
3057 | 3057 | display figures inline. |
|
3058 | 3058 | import_all : optional, bool, default: True |
|
3059 | 3059 | Whether to do `from numpy import *` and `from pylab import *` |
|
3060 | 3060 | in addition to module imports. |
|
3061 | 3061 | welcome_message : deprecated |
|
3062 | 3062 | This argument is ignored, no welcome message will be displayed. |
|
3063 | 3063 | """ |
|
3064 | 3064 | from IPython.core.pylabtools import import_pylab |
|
3065 | 3065 | |
|
3066 | 3066 | gui, backend = self.enable_matplotlib(gui) |
|
3067 | 3067 | |
|
3068 | 3068 | # We want to prevent the loading of pylab to pollute the user's |
|
3069 | 3069 | # namespace as shown by the %who* magics, so we execute the activation |
|
3070 | 3070 | # code in an empty namespace, and we update *both* user_ns and |
|
3071 | 3071 | # user_ns_hidden with this information. |
|
3072 | 3072 | ns = {} |
|
3073 | 3073 | import_pylab(ns, import_all) |
|
3074 | 3074 | # warn about clobbered names |
|
3075 | 3075 | ignored = {"__builtins__"} |
|
3076 | 3076 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
3077 | 3077 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
3078 | 3078 | self.user_ns.update(ns) |
|
3079 | 3079 | self.user_ns_hidden.update(ns) |
|
3080 | 3080 | return gui, backend, clobbered |
|
3081 | 3081 | |
|
3082 | 3082 | #------------------------------------------------------------------------- |
|
3083 | 3083 | # Utilities |
|
3084 | 3084 | #------------------------------------------------------------------------- |
|
3085 | 3085 | |
|
3086 | 3086 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
3087 | 3087 | """Expand python variables in a string. |
|
3088 | 3088 | |
|
3089 | 3089 | The depth argument indicates how many frames above the caller should |
|
3090 | 3090 | be walked to look for the local namespace where to expand variables. |
|
3091 | 3091 | |
|
3092 | 3092 | The global namespace for expansion is always the user's interactive |
|
3093 | 3093 | namespace. |
|
3094 | 3094 | """ |
|
3095 | 3095 | ns = self.user_ns.copy() |
|
3096 | 3096 | try: |
|
3097 | 3097 | frame = sys._getframe(depth+1) |
|
3098 | 3098 | except ValueError: |
|
3099 | 3099 | # This is thrown if there aren't that many frames on the stack, |
|
3100 | 3100 | # e.g. if a script called run_line_magic() directly. |
|
3101 | 3101 | pass |
|
3102 | 3102 | else: |
|
3103 | 3103 | ns.update(frame.f_locals) |
|
3104 | 3104 | |
|
3105 | 3105 | try: |
|
3106 | 3106 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3107 | 3107 | # name, and expanding **ns for .format() would make it collide with |
|
3108 | 3108 | # the 'self' argument of the method. |
|
3109 | 3109 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3110 | 3110 | except Exception: |
|
3111 | 3111 | # if formatter couldn't format, just let it go untransformed |
|
3112 | 3112 | pass |
|
3113 | 3113 | return cmd |
|
3114 | 3114 | |
|
3115 | 3115 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3116 | 3116 | """Make a new tempfile and return its filename. |
|
3117 | 3117 | |
|
3118 | 3118 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3119 | 3119 | but it registers the created filename internally so ipython cleans it up |
|
3120 | 3120 | at exit time. |
|
3121 | 3121 | |
|
3122 | 3122 | Optional inputs: |
|
3123 | 3123 | |
|
3124 | 3124 | - data(None): if data is given, it gets written out to the temp file |
|
3125 | 3125 | immediately, and the file is closed again.""" |
|
3126 | 3126 | |
|
3127 | 3127 | dirname = tempfile.mkdtemp(prefix=prefix) |
|
3128 | 3128 | self.tempdirs.append(dirname) |
|
3129 | 3129 | |
|
3130 | 3130 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) |
|
3131 | 3131 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3132 | 3132 | self.tempfiles.append(filename) |
|
3133 | 3133 | |
|
3134 | 3134 | if data: |
|
3135 | 3135 | tmp_file = open(filename,'w') |
|
3136 | 3136 | tmp_file.write(data) |
|
3137 | 3137 | tmp_file.close() |
|
3138 | 3138 | return filename |
|
3139 | 3139 | |
|
3140 | 3140 | @undoc |
|
3141 | 3141 | def write(self,data): |
|
3142 | 3142 | """DEPRECATED: Write a string to the default output""" |
|
3143 | 3143 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', |
|
3144 | 3144 | DeprecationWarning, stacklevel=2) |
|
3145 | 3145 | sys.stdout.write(data) |
|
3146 | 3146 | |
|
3147 | 3147 | @undoc |
|
3148 | 3148 | def write_err(self,data): |
|
3149 | 3149 | """DEPRECATED: Write a string to the default error output""" |
|
3150 | 3150 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', |
|
3151 | 3151 | DeprecationWarning, stacklevel=2) |
|
3152 | 3152 | sys.stderr.write(data) |
|
3153 | 3153 | |
|
3154 | 3154 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3155 | 3155 | if self.quiet: |
|
3156 | 3156 | return True |
|
3157 | 3157 | return ask_yes_no(prompt,default,interrupt) |
|
3158 | 3158 | |
|
3159 | 3159 | def show_usage(self): |
|
3160 | 3160 | """Show a usage message""" |
|
3161 | 3161 | page.page(IPython.core.usage.interactive_usage) |
|
3162 | 3162 | |
|
3163 | 3163 | def extract_input_lines(self, range_str, raw=False): |
|
3164 | 3164 | """Return as a string a set of input history slices. |
|
3165 | 3165 | |
|
3166 | 3166 | Parameters |
|
3167 | 3167 | ---------- |
|
3168 | 3168 | range_str : string |
|
3169 | 3169 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3170 | 3170 | since this function is for use by magic functions which get their |
|
3171 | 3171 | arguments as strings. The number before the / is the session |
|
3172 | 3172 | number: ~n goes n back from the current session. |
|
3173 | 3173 | |
|
3174 | 3174 | raw : bool, optional |
|
3175 | 3175 | By default, the processed input is used. If this is true, the raw |
|
3176 | 3176 | input history is used instead. |
|
3177 | 3177 | |
|
3178 | 3178 | Notes |
|
3179 | 3179 | ----- |
|
3180 | 3180 | |
|
3181 | 3181 | Slices can be described with two notations: |
|
3182 | 3182 | |
|
3183 | 3183 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3184 | 3184 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3185 | 3185 | """ |
|
3186 | 3186 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3187 | 3187 | return "\n".join(x for _, _, x in lines) |
|
3188 | 3188 | |
|
3189 | 3189 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3190 | 3190 | """Get a code string from history, file, url, or a string or macro. |
|
3191 | 3191 | |
|
3192 | 3192 | This is mainly used by magic functions. |
|
3193 | 3193 | |
|
3194 | 3194 | Parameters |
|
3195 | 3195 | ---------- |
|
3196 | 3196 | |
|
3197 | 3197 | target : str |
|
3198 | 3198 | |
|
3199 | 3199 | A string specifying code to retrieve. This will be tried respectively |
|
3200 | 3200 | as: ranges of input history (see %history for syntax), url, |
|
3201 | 3201 | corresponding .py file, filename, or an expression evaluating to a |
|
3202 | 3202 | string or Macro in the user namespace. |
|
3203 | 3203 | |
|
3204 | 3204 | raw : bool |
|
3205 | 3205 | If true (default), retrieve raw history. Has no effect on the other |
|
3206 | 3206 | retrieval mechanisms. |
|
3207 | 3207 | |
|
3208 | 3208 | py_only : bool (default False) |
|
3209 | 3209 | Only try to fetch python code, do not try alternative methods to decode file |
|
3210 | 3210 | if unicode fails. |
|
3211 | 3211 | |
|
3212 | 3212 | Returns |
|
3213 | 3213 | ------- |
|
3214 | 3214 | A string of code. |
|
3215 | 3215 | |
|
3216 | 3216 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3217 | 3217 | to an object of another type. In each case, .args[0] is a printable |
|
3218 | 3218 | message. |
|
3219 | 3219 | """ |
|
3220 | 3220 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3221 | 3221 | if code: |
|
3222 | 3222 | return code |
|
3223 | 3223 | try: |
|
3224 | 3224 | if target.startswith(('http://', 'https://')): |
|
3225 | 3225 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) |
|
3226 | 3226 | except UnicodeDecodeError: |
|
3227 | 3227 | if not py_only : |
|
3228 | 3228 | # Deferred import |
|
3229 | 3229 | from urllib.request import urlopen |
|
3230 | 3230 | response = urlopen(target) |
|
3231 | 3231 | return response.read().decode('latin1') |
|
3232 | 3232 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3233 | 3233 | |
|
3234 | 3234 | potential_target = [target] |
|
3235 | 3235 | try : |
|
3236 | 3236 | potential_target.insert(0,get_py_filename(target)) |
|
3237 | 3237 | except IOError: |
|
3238 | 3238 | pass |
|
3239 | 3239 | |
|
3240 | 3240 | for tgt in potential_target : |
|
3241 | 3241 | if os.path.isfile(tgt): # Read file |
|
3242 | 3242 | try : |
|
3243 | 3243 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3244 | 3244 | except UnicodeDecodeError : |
|
3245 | 3245 | if not py_only : |
|
3246 | 3246 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3247 | 3247 | return f.read() |
|
3248 | 3248 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3249 | 3249 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3250 | 3250 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3251 | 3251 | |
|
3252 | 3252 | if search_ns: |
|
3253 | 3253 | # Inspect namespace to load object source |
|
3254 | 3254 | object_info = self.object_inspect(target, detail_level=1) |
|
3255 | 3255 | if object_info['found'] and object_info['source']: |
|
3256 | 3256 | return object_info['source'] |
|
3257 | 3257 | |
|
3258 | 3258 | try: # User namespace |
|
3259 | 3259 | codeobj = eval(target, self.user_ns) |
|
3260 | 3260 | except Exception: |
|
3261 | 3261 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3262 | 3262 | "nor in the user namespace.") % target) |
|
3263 | 3263 | |
|
3264 | 3264 | if isinstance(codeobj, str): |
|
3265 | 3265 | return codeobj |
|
3266 | 3266 | elif isinstance(codeobj, Macro): |
|
3267 | 3267 | return codeobj.value |
|
3268 | 3268 | |
|
3269 | 3269 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3270 | 3270 | codeobj) |
|
3271 | 3271 | |
|
3272 | 3272 | #------------------------------------------------------------------------- |
|
3273 | 3273 | # Things related to IPython exiting |
|
3274 | 3274 | #------------------------------------------------------------------------- |
|
3275 | 3275 | def atexit_operations(self): |
|
3276 | 3276 | """This will be executed at the time of exit. |
|
3277 | 3277 | |
|
3278 | 3278 | Cleanup operations and saving of persistent data that is done |
|
3279 | 3279 | unconditionally by IPython should be performed here. |
|
3280 | 3280 | |
|
3281 | 3281 | For things that may depend on startup flags or platform specifics (such |
|
3282 | 3282 | as having readline or not), register a separate atexit function in the |
|
3283 | 3283 | code that has the appropriate information, rather than trying to |
|
3284 | 3284 | clutter |
|
3285 | 3285 | """ |
|
3286 | 3286 | # Close the history session (this stores the end time and line count) |
|
3287 | 3287 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3288 | 3288 | # history db |
|
3289 | 3289 | self.history_manager.end_session() |
|
3290 | 3290 | |
|
3291 | 3291 | # Cleanup all tempfiles and folders left around |
|
3292 | 3292 | for tfile in self.tempfiles: |
|
3293 | 3293 | try: |
|
3294 | 3294 | os.unlink(tfile) |
|
3295 | 3295 | except OSError: |
|
3296 | 3296 | pass |
|
3297 | 3297 | |
|
3298 | 3298 | for tdir in self.tempdirs: |
|
3299 | 3299 | try: |
|
3300 | 3300 | os.rmdir(tdir) |
|
3301 | 3301 | except OSError: |
|
3302 | 3302 | pass |
|
3303 | 3303 | |
|
3304 | 3304 | # Clear all user namespaces to release all references cleanly. |
|
3305 | 3305 | self.reset(new_session=False) |
|
3306 | 3306 | |
|
3307 | 3307 | # Run user hooks |
|
3308 | 3308 | self.hooks.shutdown_hook() |
|
3309 | 3309 | |
|
3310 | 3310 | def cleanup(self): |
|
3311 | 3311 | self.restore_sys_module_state() |
|
3312 | 3312 | |
|
3313 | 3313 | |
|
3314 | 3314 | # Overridden in terminal subclass to change prompts |
|
3315 | 3315 | def switch_doctest_mode(self, mode): |
|
3316 | 3316 | pass |
|
3317 | 3317 | |
|
3318 | 3318 | |
|
3319 | 3319 | class InteractiveShellABC(metaclass=abc.ABCMeta): |
|
3320 | 3320 | """An abstract base class for InteractiveShell.""" |
|
3321 | 3321 | |
|
3322 | 3322 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,614 +1,614 b'' | |||
|
1 | 1 | """Implementation of basic magic functions.""" |
|
2 | 2 | |
|
3 | 3 | |
|
4 | 4 | import argparse |
|
5 | 5 | import textwrap |
|
6 | 6 | import io |
|
7 | 7 | import sys |
|
8 | 8 | from pprint import pformat |
|
9 | 9 | |
|
10 | 10 | from IPython.core import magic_arguments, page |
|
11 | 11 | from IPython.core.error import UsageError |
|
12 | 12 | from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes |
|
13 | 13 | from IPython.utils.text import format_screen, dedent, indent |
|
14 | 14 | from IPython.testing.skipdoctest import skip_doctest |
|
15 | 15 | from IPython.utils.ipstruct import Struct |
|
16 | 16 | from warnings import warn |
|
17 | 17 | from logging import error |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | class MagicsDisplay(object): |
|
21 | 21 | def __init__(self, magics_manager, ignore=None): |
|
22 | 22 | self.ignore = ignore if ignore else [] |
|
23 | 23 | self.magics_manager = magics_manager |
|
24 | 24 | |
|
25 | 25 | def _lsmagic(self): |
|
26 | 26 | """The main implementation of the %lsmagic""" |
|
27 | 27 | mesc = magic_escapes['line'] |
|
28 | 28 | cesc = magic_escapes['cell'] |
|
29 | 29 | mman = self.magics_manager |
|
30 | 30 | magics = mman.lsmagic() |
|
31 | 31 | out = ['Available line magics:', |
|
32 | 32 | mesc + (' '+mesc).join(sorted([m for m,v in magics['line'].items() if (v not in self.ignore)])), |
|
33 | 33 | '', |
|
34 | 34 | 'Available cell magics:', |
|
35 | 35 | cesc + (' '+cesc).join(sorted([m for m,v in magics['cell'].items() if (v not in self.ignore)])), |
|
36 | 36 | '', |
|
37 | 37 | mman.auto_status()] |
|
38 | 38 | return '\n'.join(out) |
|
39 | 39 | |
|
40 | 40 | def _repr_pretty_(self, p, cycle): |
|
41 | 41 | p.text(self._lsmagic()) |
|
42 | 42 | |
|
43 | 43 | def __str__(self): |
|
44 | 44 | return self._lsmagic() |
|
45 | 45 | |
|
46 | 46 | def _jsonable(self): |
|
47 | 47 | """turn magics dict into jsonable dict of the same structure |
|
48 | 48 | |
|
49 | 49 | replaces object instances with their class names as strings |
|
50 | 50 | """ |
|
51 | 51 | magic_dict = {} |
|
52 | 52 | mman = self.magics_manager |
|
53 | 53 | magics = mman.lsmagic() |
|
54 | 54 | for key, subdict in magics.items(): |
|
55 | 55 | d = {} |
|
56 | 56 | magic_dict[key] = d |
|
57 | 57 | for name, obj in subdict.items(): |
|
58 | 58 | try: |
|
59 | 59 | classname = obj.__self__.__class__.__name__ |
|
60 | 60 | except AttributeError: |
|
61 | 61 | classname = 'Other' |
|
62 | 62 | |
|
63 | 63 | d[name] = classname |
|
64 | 64 | return magic_dict |
|
65 | 65 | |
|
66 | 66 | def _repr_json_(self): |
|
67 | 67 | return self._jsonable() |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | @magics_class |
|
71 | 71 | class BasicMagics(Magics): |
|
72 | 72 | """Magics that provide central IPython functionality. |
|
73 | 73 | |
|
74 | 74 | These are various magics that don't fit into specific categories but that |
|
75 | 75 | are all part of the base 'IPython experience'.""" |
|
76 | 76 | |
|
77 | 77 | @magic_arguments.magic_arguments() |
|
78 | 78 | @magic_arguments.argument( |
|
79 | 79 | '-l', '--line', action='store_true', |
|
80 | 80 | help="""Create a line magic alias.""" |
|
81 | 81 | ) |
|
82 | 82 | @magic_arguments.argument( |
|
83 | 83 | '-c', '--cell', action='store_true', |
|
84 | 84 | help="""Create a cell magic alias.""" |
|
85 | 85 | ) |
|
86 | 86 | @magic_arguments.argument( |
|
87 | 87 | 'name', |
|
88 | 88 | help="""Name of the magic to be created.""" |
|
89 | 89 | ) |
|
90 | 90 | @magic_arguments.argument( |
|
91 | 91 | 'target', |
|
92 | 92 | help="""Name of the existing line or cell magic.""" |
|
93 | 93 | ) |
|
94 | 94 | @magic_arguments.argument( |
|
95 | 95 | '-p', '--params', default=None, |
|
96 | 96 | help="""Parameters passed to the magic function.""" |
|
97 | 97 | ) |
|
98 | 98 | @line_magic |
|
99 | 99 | def alias_magic(self, line=''): |
|
100 | 100 | """Create an alias for an existing line or cell magic. |
|
101 | 101 | |
|
102 | 102 | Examples |
|
103 | 103 | -------- |
|
104 | 104 | :: |
|
105 | 105 | |
|
106 | 106 | In [1]: %alias_magic t timeit |
|
107 | 107 | Created `%t` as an alias for `%timeit`. |
|
108 | 108 | Created `%%t` as an alias for `%%timeit`. |
|
109 | 109 | |
|
110 | 110 | In [2]: %t -n1 pass |
|
111 | 111 | 1 loops, best of 3: 954 ns per loop |
|
112 | 112 | |
|
113 | 113 | In [3]: %%t -n1 |
|
114 | 114 | ...: pass |
|
115 | 115 | ...: |
|
116 | 116 | 1 loops, best of 3: 954 ns per loop |
|
117 | 117 | |
|
118 | 118 | In [4]: %alias_magic --cell whereami pwd |
|
119 | 119 | UsageError: Cell magic function `%%pwd` not found. |
|
120 | 120 | In [5]: %alias_magic --line whereami pwd |
|
121 | 121 | Created `%whereami` as an alias for `%pwd`. |
|
122 | 122 | |
|
123 | 123 | In [6]: %whereami |
|
124 | 124 | Out[6]: u'/home/testuser' |
|
125 | 125 | |
|
126 | 126 | In [7]: %alias_magic h history -p "-l 30" --line |
|
127 | 127 | Created `%h` as an alias for `%history -l 30`. |
|
128 | 128 | """ |
|
129 | 129 | |
|
130 | 130 | args = magic_arguments.parse_argstring(self.alias_magic, line) |
|
131 | 131 | shell = self.shell |
|
132 | 132 | mman = self.shell.magics_manager |
|
133 | 133 | escs = ''.join(magic_escapes.values()) |
|
134 | 134 | |
|
135 | 135 | target = args.target.lstrip(escs) |
|
136 | 136 | name = args.name.lstrip(escs) |
|
137 | 137 | |
|
138 | 138 | params = args.params |
|
139 | 139 | if (params and |
|
140 | 140 | ((params.startswith('"') and params.endswith('"')) |
|
141 | 141 | or (params.startswith("'") and params.endswith("'")))): |
|
142 | 142 | params = params[1:-1] |
|
143 | 143 | |
|
144 | 144 | # Find the requested magics. |
|
145 | 145 | m_line = shell.find_magic(target, 'line') |
|
146 | 146 | m_cell = shell.find_magic(target, 'cell') |
|
147 | 147 | if args.line and m_line is None: |
|
148 | 148 | raise UsageError('Line magic function `%s%s` not found.' % |
|
149 | 149 | (magic_escapes['line'], target)) |
|
150 | 150 | if args.cell and m_cell is None: |
|
151 | 151 | raise UsageError('Cell magic function `%s%s` not found.' % |
|
152 | 152 | (magic_escapes['cell'], target)) |
|
153 | 153 | |
|
154 | 154 | # If --line and --cell are not specified, default to the ones |
|
155 | 155 | # that are available. |
|
156 | 156 | if not args.line and not args.cell: |
|
157 | 157 | if not m_line and not m_cell: |
|
158 | 158 | raise UsageError( |
|
159 | 159 | 'No line or cell magic with name `%s` found.' % target |
|
160 | 160 | ) |
|
161 | 161 | args.line = bool(m_line) |
|
162 | 162 | args.cell = bool(m_cell) |
|
163 | 163 | |
|
164 | 164 | params_str = "" if params is None else " " + params |
|
165 | 165 | |
|
166 | 166 | if args.line: |
|
167 | 167 | mman.register_alias(name, target, 'line', params) |
|
168 | 168 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( |
|
169 | 169 | magic_escapes['line'], name, |
|
170 | 170 | magic_escapes['line'], target, params_str)) |
|
171 | 171 | |
|
172 | 172 | if args.cell: |
|
173 | 173 | mman.register_alias(name, target, 'cell', params) |
|
174 | 174 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( |
|
175 | 175 | magic_escapes['cell'], name, |
|
176 | 176 | magic_escapes['cell'], target, params_str)) |
|
177 | 177 | |
|
178 | 178 | @line_magic |
|
179 | 179 | def lsmagic(self, parameter_s=''): |
|
180 | 180 | """List currently available magic functions.""" |
|
181 | 181 | return MagicsDisplay(self.shell.magics_manager, ignore=[self.pip]) |
|
182 | 182 | |
|
183 | 183 | def _magic_docs(self, brief=False, rest=False): |
|
184 | 184 | """Return docstrings from magic functions.""" |
|
185 | 185 | mman = self.shell.magics_manager |
|
186 | 186 | docs = mman.lsmagic_docs(brief, missing='No documentation') |
|
187 | 187 | |
|
188 | 188 | if rest: |
|
189 | 189 | format_string = '**%s%s**::\n\n%s\n\n' |
|
190 | 190 | else: |
|
191 | 191 | format_string = '%s%s:\n%s\n' |
|
192 | 192 | |
|
193 | 193 | return ''.join( |
|
194 | 194 | [format_string % (magic_escapes['line'], fname, |
|
195 | 195 | indent(dedent(fndoc))) |
|
196 | 196 | for fname, fndoc in sorted(docs['line'].items())] |
|
197 | 197 | + |
|
198 | 198 | [format_string % (magic_escapes['cell'], fname, |
|
199 | 199 | indent(dedent(fndoc))) |
|
200 | 200 | for fname, fndoc in sorted(docs['cell'].items())] |
|
201 | 201 | ) |
|
202 | 202 | |
|
203 | 203 | @line_magic |
|
204 | 204 | def magic(self, parameter_s=''): |
|
205 | 205 | """Print information about the magic function system. |
|
206 | 206 | |
|
207 | 207 | Supported formats: -latex, -brief, -rest |
|
208 | 208 | """ |
|
209 | 209 | |
|
210 | 210 | mode = '' |
|
211 | 211 | try: |
|
212 | 212 | mode = parameter_s.split()[0][1:] |
|
213 | 213 | except IndexError: |
|
214 | 214 | pass |
|
215 | 215 | |
|
216 | 216 | brief = (mode == 'brief') |
|
217 | 217 | rest = (mode == 'rest') |
|
218 | 218 | magic_docs = self._magic_docs(brief, rest) |
|
219 | 219 | |
|
220 | 220 | if mode == 'latex': |
|
221 | 221 | print(self.format_latex(magic_docs)) |
|
222 | 222 | return |
|
223 | 223 | else: |
|
224 | 224 | magic_docs = format_screen(magic_docs) |
|
225 | 225 | |
|
226 | 226 | out = [""" |
|
227 | 227 | IPython's 'magic' functions |
|
228 | 228 | =========================== |
|
229 | 229 | |
|
230 | 230 | The magic function system provides a series of functions which allow you to |
|
231 | 231 | control the behavior of IPython itself, plus a lot of system-type |
|
232 | 232 | features. There are two kinds of magics, line-oriented and cell-oriented. |
|
233 | 233 | |
|
234 | 234 | Line magics are prefixed with the % character and work much like OS |
|
235 | 235 | command-line calls: they get as an argument the rest of the line, where |
|
236 | 236 | arguments are passed without parentheses or quotes. For example, this will |
|
237 | 237 | time the given statement:: |
|
238 | 238 | |
|
239 | 239 | %timeit range(1000) |
|
240 | 240 | |
|
241 | 241 | Cell magics are prefixed with a double %%, and they are functions that get as |
|
242 | 242 | an argument not only the rest of the line, but also the lines below it in a |
|
243 | 243 | separate argument. These magics are called with two arguments: the rest of the |
|
244 | 244 | call line and the body of the cell, consisting of the lines below the first. |
|
245 | 245 | For example:: |
|
246 | 246 | |
|
247 | 247 | %%timeit x = numpy.random.randn((100, 100)) |
|
248 | 248 | numpy.linalg.svd(x) |
|
249 | 249 | |
|
250 | 250 | will time the execution of the numpy svd routine, running the assignment of x |
|
251 | 251 | as part of the setup phase, which is not timed. |
|
252 | 252 | |
|
253 | 253 | In a line-oriented client (the terminal or Qt console IPython), starting a new |
|
254 | 254 | input with %% will automatically enter cell mode, and IPython will continue |
|
255 | 255 | reading input until a blank line is given. In the notebook, simply type the |
|
256 | 256 | whole cell as one entity, but keep in mind that the %% escape can only be at |
|
257 | 257 | the very start of the cell. |
|
258 | 258 | |
|
259 | 259 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
260 | 260 | %automagic function), you don't need to type in the % explicitly for line |
|
261 | 261 | magics; cell magics always require an explicit '%%' escape. By default, |
|
262 | 262 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
263 | 263 | |
|
264 | 264 | Example: typing '%cd mydir' (without the quotes) changes your working directory |
|
265 | 265 | to 'mydir', if it exists. |
|
266 | 266 | |
|
267 | 267 | For a list of the available magic functions, use %lsmagic. For a description |
|
268 | 268 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
269 | 269 | |
|
270 | 270 | Currently the magic system has the following functions:""", |
|
271 | 271 | magic_docs, |
|
272 | 272 | "Summary of magic functions (from %slsmagic):" % magic_escapes['line'], |
|
273 | 273 | str(self.lsmagic()), |
|
274 | 274 | ] |
|
275 | 275 | page.page('\n'.join(out)) |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | @line_magic |
|
279 | 279 | def page(self, parameter_s=''): |
|
280 | 280 | """Pretty print the object and display it through a pager. |
|
281 | 281 | |
|
282 | 282 | %page [options] OBJECT |
|
283 | 283 | |
|
284 | 284 | If no object is given, use _ (last output). |
|
285 | 285 | |
|
286 | 286 | Options: |
|
287 | 287 | |
|
288 | 288 | -r: page str(object), don't pretty-print it.""" |
|
289 | 289 | |
|
290 | 290 | # After a function contributed by Olivier Aubert, slightly modified. |
|
291 | 291 | |
|
292 | 292 | # Process options/args |
|
293 | 293 | opts, args = self.parse_options(parameter_s, 'r') |
|
294 | 294 | raw = 'r' in opts |
|
295 | 295 | |
|
296 | 296 | oname = args and args or '_' |
|
297 | 297 | info = self.shell._ofind(oname) |
|
298 | 298 | if info['found']: |
|
299 | 299 | txt = (raw and str or pformat)( info['obj'] ) |
|
300 | 300 | page.page(txt) |
|
301 | 301 | else: |
|
302 | 302 | print('Object `%s` not found' % oname) |
|
303 | 303 | |
|
304 | 304 | @line_magic |
|
305 | 305 | def profile(self, parameter_s=''): |
|
306 | 306 | """DEPRECATED since IPython 2.0. |
|
307 | 307 | |
|
308 | 308 | Raise `UsageError`. To profile code use the :magic:`prun` magic. |
|
309 | 309 | |
|
310 | 310 | |
|
311 | 311 | See Also |
|
312 | 312 | -------- |
|
313 | 313 | prun : run code using the Python profiler (:magic:`prun`) |
|
314 | 314 | """ |
|
315 | 315 | raise UsageError("The `%profile` magic has been deprecated since IPython 2.0. " |
|
316 | 316 | "and removed in IPython 6.0. Please use the value of `get_ipython().profile` instead " |
|
317 | 317 | "to see current profile in use. Perhaps you meant to use `%prun` to profile code?") |
|
318 | 318 | |
|
319 | 319 | @line_magic |
|
320 | 320 | def pprint(self, parameter_s=''): |
|
321 | 321 | """Toggle pretty printing on/off.""" |
|
322 | 322 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
323 | 323 | ptformatter.pprint = bool(1 - ptformatter.pprint) |
|
324 | 324 | print('Pretty printing has been turned', |
|
325 | 325 | ['OFF','ON'][ptformatter.pprint]) |
|
326 | 326 | |
|
327 | 327 | @line_magic |
|
328 | 328 | def colors(self, parameter_s=''): |
|
329 | 329 | """Switch color scheme for prompts, info system and exception handlers. |
|
330 | 330 | |
|
331 | 331 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
332 | 332 | |
|
333 | 333 | Color scheme names are not case-sensitive. |
|
334 | 334 | |
|
335 | 335 | Examples |
|
336 | 336 | -------- |
|
337 | 337 | To get a plain black and white terminal:: |
|
338 | 338 | |
|
339 | 339 | %colors nocolor |
|
340 | 340 | """ |
|
341 | 341 | def color_switch_err(name): |
|
342 | 342 | warn('Error changing %s color schemes.\n%s' % |
|
343 | 343 | (name, sys.exc_info()[1]), stacklevel=2) |
|
344 | 344 | |
|
345 | 345 | |
|
346 | 346 | new_scheme = parameter_s.strip() |
|
347 | 347 | if not new_scheme: |
|
348 | 348 | raise UsageError( |
|
349 | 349 | "%colors: you must specify a color scheme. See '%colors?'") |
|
350 | 350 | # local shortcut |
|
351 | 351 | shell = self.shell |
|
352 | 352 | |
|
353 | 353 | # Set shell colour scheme |
|
354 | 354 | try: |
|
355 | 355 | shell.colors = new_scheme |
|
356 | 356 | shell.refresh_style() |
|
357 | 357 | except: |
|
358 | 358 | color_switch_err('shell') |
|
359 | 359 | |
|
360 | 360 | # Set exception colors |
|
361 | 361 | try: |
|
362 | 362 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
363 | 363 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
364 | 364 | except: |
|
365 | 365 | color_switch_err('exception') |
|
366 | 366 | |
|
367 | 367 | # Set info (for 'object?') colors |
|
368 | 368 | if shell.color_info: |
|
369 | 369 | try: |
|
370 | 370 | shell.inspector.set_active_scheme(new_scheme) |
|
371 | 371 | except: |
|
372 | 372 | color_switch_err('object inspector') |
|
373 | 373 | else: |
|
374 | 374 | shell.inspector.set_active_scheme('NoColor') |
|
375 | 375 | |
|
376 | 376 | @line_magic |
|
377 | 377 | def xmode(self, parameter_s=''): |
|
378 | 378 | """Switch modes for the exception handlers. |
|
379 | 379 | |
|
380 | 380 | Valid modes: Plain, Context and Verbose. |
|
381 | 381 | |
|
382 | 382 | If called without arguments, acts as a toggle.""" |
|
383 | 383 | |
|
384 | 384 | def xmode_switch_err(name): |
|
385 | 385 | warn('Error changing %s exception modes.\n%s' % |
|
386 | 386 | (name,sys.exc_info()[1])) |
|
387 | 387 | |
|
388 | 388 | shell = self.shell |
|
389 | 389 | new_mode = parameter_s.strip().capitalize() |
|
390 | 390 | try: |
|
391 | 391 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
392 | 392 | print('Exception reporting mode:',shell.InteractiveTB.mode) |
|
393 | 393 | except: |
|
394 | 394 | xmode_switch_err('user') |
|
395 | 395 | |
|
396 | 396 | @line_magic |
|
397 | 397 | def pip(self, args=''): |
|
398 | 398 | """ |
|
399 | 399 | Intercept usage of ``pip`` in IPython and direct user to run command outside of IPython. |
|
400 | 400 | """ |
|
401 | 401 | print(textwrap.dedent(''' |
|
402 | 402 | The following command must be run outside of the IPython shell: |
|
403 | 403 | |
|
404 | 404 | $ pip {args} |
|
405 | 405 | |
|
406 | 406 | The Python package manager (pip) can only be used from outside of IPython. |
|
407 | 407 | Please reissue the `pip` command in a separate terminal or command prompt. |
|
408 | 408 | |
|
409 |
See the Python documentation for more information |
|
|
409 | See the Python documentation for more information on how to install packages: | |
|
410 | 410 | |
|
411 | 411 | https://docs.python.org/3/installing/'''.format(args=args))) |
|
412 | 412 | |
|
413 | 413 | @line_magic |
|
414 | 414 | def quickref(self, arg): |
|
415 | 415 | """ Show a quick reference sheet """ |
|
416 | 416 | from IPython.core.usage import quick_reference |
|
417 | 417 | qr = quick_reference + self._magic_docs(brief=True) |
|
418 | 418 | page.page(qr) |
|
419 | 419 | |
|
420 | 420 | @line_magic |
|
421 | 421 | def doctest_mode(self, parameter_s=''): |
|
422 | 422 | """Toggle doctest mode on and off. |
|
423 | 423 | |
|
424 | 424 | This mode is intended to make IPython behave as much as possible like a |
|
425 | 425 | plain Python shell, from the perspective of how its prompts, exceptions |
|
426 | 426 | and output look. This makes it easy to copy and paste parts of a |
|
427 | 427 | session into doctests. It does so by: |
|
428 | 428 | |
|
429 | 429 | - Changing the prompts to the classic ``>>>`` ones. |
|
430 | 430 | - Changing the exception reporting mode to 'Plain'. |
|
431 | 431 | - Disabling pretty-printing of output. |
|
432 | 432 | |
|
433 | 433 | Note that IPython also supports the pasting of code snippets that have |
|
434 | 434 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
435 | 435 | doctests from files or docstrings (even if they have leading |
|
436 | 436 | whitespace), and the code will execute correctly. You can then use |
|
437 | 437 | '%history -t' to see the translated history; this will give you the |
|
438 | 438 | input after removal of all the leading prompts and whitespace, which |
|
439 | 439 | can be pasted back into an editor. |
|
440 | 440 | |
|
441 | 441 | With these features, you can switch into this mode easily whenever you |
|
442 | 442 | need to do testing and changes to doctests, without having to leave |
|
443 | 443 | your existing IPython session. |
|
444 | 444 | """ |
|
445 | 445 | |
|
446 | 446 | # Shorthands |
|
447 | 447 | shell = self.shell |
|
448 | 448 | meta = shell.meta |
|
449 | 449 | disp_formatter = self.shell.display_formatter |
|
450 | 450 | ptformatter = disp_formatter.formatters['text/plain'] |
|
451 | 451 | # dstore is a data store kept in the instance metadata bag to track any |
|
452 | 452 | # changes we make, so we can undo them later. |
|
453 | 453 | dstore = meta.setdefault('doctest_mode',Struct()) |
|
454 | 454 | save_dstore = dstore.setdefault |
|
455 | 455 | |
|
456 | 456 | # save a few values we'll need to recover later |
|
457 | 457 | mode = save_dstore('mode',False) |
|
458 | 458 | save_dstore('rc_pprint',ptformatter.pprint) |
|
459 | 459 | save_dstore('xmode',shell.InteractiveTB.mode) |
|
460 | 460 | save_dstore('rc_separate_out',shell.separate_out) |
|
461 | 461 | save_dstore('rc_separate_out2',shell.separate_out2) |
|
462 | 462 | save_dstore('rc_separate_in',shell.separate_in) |
|
463 | 463 | save_dstore('rc_active_types',disp_formatter.active_types) |
|
464 | 464 | |
|
465 | 465 | if not mode: |
|
466 | 466 | # turn on |
|
467 | 467 | |
|
468 | 468 | # Prompt separators like plain python |
|
469 | 469 | shell.separate_in = '' |
|
470 | 470 | shell.separate_out = '' |
|
471 | 471 | shell.separate_out2 = '' |
|
472 | 472 | |
|
473 | 473 | |
|
474 | 474 | ptformatter.pprint = False |
|
475 | 475 | disp_formatter.active_types = ['text/plain'] |
|
476 | 476 | |
|
477 | 477 | shell.magic('xmode Plain') |
|
478 | 478 | else: |
|
479 | 479 | # turn off |
|
480 | 480 | shell.separate_in = dstore.rc_separate_in |
|
481 | 481 | |
|
482 | 482 | shell.separate_out = dstore.rc_separate_out |
|
483 | 483 | shell.separate_out2 = dstore.rc_separate_out2 |
|
484 | 484 | |
|
485 | 485 | ptformatter.pprint = dstore.rc_pprint |
|
486 | 486 | disp_formatter.active_types = dstore.rc_active_types |
|
487 | 487 | |
|
488 | 488 | shell.magic('xmode ' + dstore.xmode) |
|
489 | 489 | |
|
490 | 490 | # mode here is the state before we switch; switch_doctest_mode takes |
|
491 | 491 | # the mode we're switching to. |
|
492 | 492 | shell.switch_doctest_mode(not mode) |
|
493 | 493 | |
|
494 | 494 | # Store new mode and inform |
|
495 | 495 | dstore.mode = bool(not mode) |
|
496 | 496 | mode_label = ['OFF','ON'][dstore.mode] |
|
497 | 497 | print('Doctest mode is:', mode_label) |
|
498 | 498 | |
|
499 | 499 | @line_magic |
|
500 | 500 | def gui(self, parameter_s=''): |
|
501 | 501 | """Enable or disable IPython GUI event loop integration. |
|
502 | 502 | |
|
503 | 503 | %gui [GUINAME] |
|
504 | 504 | |
|
505 | 505 | This magic replaces IPython's threaded shells that were activated |
|
506 | 506 | using the (pylab/wthread/etc.) command line flags. GUI toolkits |
|
507 | 507 | can now be enabled at runtime and keyboard |
|
508 | 508 | interrupts should work without any problems. The following toolkits |
|
509 | 509 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: |
|
510 | 510 | |
|
511 | 511 | %gui wx # enable wxPython event loop integration |
|
512 | 512 | %gui qt4|qt # enable PyQt4 event loop integration |
|
513 | 513 | %gui qt5 # enable PyQt5 event loop integration |
|
514 | 514 | %gui gtk # enable PyGTK event loop integration |
|
515 | 515 | %gui gtk3 # enable Gtk3 event loop integration |
|
516 | 516 | %gui tk # enable Tk event loop integration |
|
517 | 517 | %gui osx # enable Cocoa event loop integration |
|
518 | 518 | # (requires %matplotlib 1.1) |
|
519 | 519 | %gui # disable all event loop integration |
|
520 | 520 | |
|
521 | 521 | WARNING: after any of these has been called you can simply create |
|
522 | 522 | an application object, but DO NOT start the event loop yourself, as |
|
523 | 523 | we have already handled that. |
|
524 | 524 | """ |
|
525 | 525 | opts, arg = self.parse_options(parameter_s, '') |
|
526 | 526 | if arg=='': arg = None |
|
527 | 527 | try: |
|
528 | 528 | return self.shell.enable_gui(arg) |
|
529 | 529 | except Exception as e: |
|
530 | 530 | # print simple error message, rather than traceback if we can't |
|
531 | 531 | # hook up the GUI |
|
532 | 532 | error(str(e)) |
|
533 | 533 | |
|
534 | 534 | @skip_doctest |
|
535 | 535 | @line_magic |
|
536 | 536 | def precision(self, s=''): |
|
537 | 537 | """Set floating point precision for pretty printing. |
|
538 | 538 | |
|
539 | 539 | Can set either integer precision or a format string. |
|
540 | 540 | |
|
541 | 541 | If numpy has been imported and precision is an int, |
|
542 | 542 | numpy display precision will also be set, via ``numpy.set_printoptions``. |
|
543 | 543 | |
|
544 | 544 | If no argument is given, defaults will be restored. |
|
545 | 545 | |
|
546 | 546 | Examples |
|
547 | 547 | -------- |
|
548 | 548 | :: |
|
549 | 549 | |
|
550 | 550 | In [1]: from math import pi |
|
551 | 551 | |
|
552 | 552 | In [2]: %precision 3 |
|
553 | 553 | Out[2]: u'%.3f' |
|
554 | 554 | |
|
555 | 555 | In [3]: pi |
|
556 | 556 | Out[3]: 3.142 |
|
557 | 557 | |
|
558 | 558 | In [4]: %precision %i |
|
559 | 559 | Out[4]: u'%i' |
|
560 | 560 | |
|
561 | 561 | In [5]: pi |
|
562 | 562 | Out[5]: 3 |
|
563 | 563 | |
|
564 | 564 | In [6]: %precision %e |
|
565 | 565 | Out[6]: u'%e' |
|
566 | 566 | |
|
567 | 567 | In [7]: pi**10 |
|
568 | 568 | Out[7]: 9.364805e+04 |
|
569 | 569 | |
|
570 | 570 | In [8]: %precision |
|
571 | 571 | Out[8]: u'%r' |
|
572 | 572 | |
|
573 | 573 | In [9]: pi**10 |
|
574 | 574 | Out[9]: 93648.047476082982 |
|
575 | 575 | """ |
|
576 | 576 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
577 | 577 | ptformatter.float_precision = s |
|
578 | 578 | return ptformatter.float_format |
|
579 | 579 | |
|
580 | 580 | @magic_arguments.magic_arguments() |
|
581 | 581 | @magic_arguments.argument( |
|
582 | 582 | '-e', '--export', action='store_true', default=False, |
|
583 | 583 | help=argparse.SUPPRESS |
|
584 | 584 | ) |
|
585 | 585 | @magic_arguments.argument( |
|
586 | 586 | 'filename', type=str, |
|
587 | 587 | help='Notebook name or filename' |
|
588 | 588 | ) |
|
589 | 589 | @line_magic |
|
590 | 590 | def notebook(self, s): |
|
591 | 591 | """Export and convert IPython notebooks. |
|
592 | 592 | |
|
593 | 593 | This function can export the current IPython history to a notebook file. |
|
594 | 594 | For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb". |
|
595 | 595 | |
|
596 | 596 | The -e or --export flag is deprecated in IPython 5.2, and will be |
|
597 | 597 | removed in the future. |
|
598 | 598 | """ |
|
599 | 599 | args = magic_arguments.parse_argstring(self.notebook, s) |
|
600 | 600 | |
|
601 | 601 | from nbformat import write, v4 |
|
602 | 602 | |
|
603 | 603 | cells = [] |
|
604 | 604 | hist = list(self.shell.history_manager.get_range()) |
|
605 | 605 | if(len(hist)<=1): |
|
606 | 606 | raise ValueError('History is empty, cannot export') |
|
607 | 607 | for session, execution_count, source in hist[:-1]: |
|
608 | 608 | cells.append(v4.new_code_cell( |
|
609 | 609 | execution_count=execution_count, |
|
610 | 610 | source=source |
|
611 | 611 | )) |
|
612 | 612 | nb = v4.new_notebook(cells=cells) |
|
613 | 613 | with io.open(args.filename, 'w', encoding='utf-8') as f: |
|
614 | 614 | write(nb, f, version=4) |
@@ -1,740 +1,740 b'' | |||
|
1 | 1 | """Implementation of code management magic functions. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # Stdlib |
|
16 | 16 | import inspect |
|
17 | 17 | import io |
|
18 | 18 | import os |
|
19 | 19 | import re |
|
20 | 20 | import sys |
|
21 | 21 | import ast |
|
22 | 22 | from itertools import chain |
|
23 | 23 | |
|
24 | 24 | # Our own packages |
|
25 | 25 | from IPython.core.error import TryNext, StdinNotImplementedError, UsageError |
|
26 | 26 | from IPython.core.macro import Macro |
|
27 | 27 | from IPython.core.magic import Magics, magics_class, line_magic |
|
28 | 28 | from IPython.core.oinspect import find_file, find_source_lines |
|
29 | 29 | from IPython.testing.skipdoctest import skip_doctest |
|
30 | 30 | from IPython.utils import py3compat |
|
31 | 31 | from IPython.utils.contexts import preserve_keys |
|
32 | 32 | from IPython.utils.path import get_py_filename |
|
33 | 33 | from warnings import warn |
|
34 | 34 | from logging import error |
|
35 | 35 | from IPython.utils.text import get_text_list |
|
36 | 36 | |
|
37 | 37 | #----------------------------------------------------------------------------- |
|
38 | 38 | # Magic implementation classes |
|
39 | 39 | #----------------------------------------------------------------------------- |
|
40 | 40 | |
|
41 | 41 | # Used for exception handling in magic_edit |
|
42 | 42 | class MacroToEdit(ValueError): pass |
|
43 | 43 | |
|
44 | 44 | ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$") |
|
45 | 45 | |
|
46 | 46 | # To match, e.g. 8-10 1:5 :10 3- |
|
47 | 47 | range_re = re.compile(r""" |
|
48 | 48 | (?P<start>\d+)? |
|
49 | 49 | ((?P<sep>[\-:]) |
|
50 | 50 | (?P<end>\d+)?)? |
|
51 | 51 | $""", re.VERBOSE) |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def extract_code_ranges(ranges_str): |
|
55 | 55 | """Turn a string of range for %%load into 2-tuples of (start, stop) |
|
56 | 56 | ready to use as a slice of the content splitted by lines. |
|
57 | 57 | |
|
58 | 58 | Examples |
|
59 | 59 | -------- |
|
60 | 60 | list(extract_input_ranges("5-10 2")) |
|
61 | 61 | [(4, 10), (1, 2)] |
|
62 | 62 | """ |
|
63 | 63 | for range_str in ranges_str.split(): |
|
64 | 64 | rmatch = range_re.match(range_str) |
|
65 | 65 | if not rmatch: |
|
66 | 66 | continue |
|
67 | 67 | sep = rmatch.group("sep") |
|
68 | 68 | start = rmatch.group("start") |
|
69 | 69 | end = rmatch.group("end") |
|
70 | 70 | |
|
71 | 71 | if sep == '-': |
|
72 | 72 | start = int(start) - 1 if start else None |
|
73 | 73 | end = int(end) if end else None |
|
74 | 74 | elif sep == ':': |
|
75 | 75 | start = int(start) - 1 if start else None |
|
76 | 76 | end = int(end) - 1 if end else None |
|
77 | 77 | else: |
|
78 | 78 | end = int(start) |
|
79 | 79 | start = int(start) - 1 |
|
80 | 80 | yield (start, end) |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | def extract_symbols(code, symbols): |
|
84 | 84 | """ |
|
85 | 85 | Return a tuple (blocks, not_found) |
|
86 | 86 | where ``blocks`` is a list of code fragments |
|
87 | 87 | for each symbol parsed from code, and ``not_found`` are |
|
88 | 88 | symbols not found in the code. |
|
89 | 89 | |
|
90 | 90 | For example:: |
|
91 | 91 | |
|
92 | 92 | In [1]: code = '''a = 10 |
|
93 | 93 | ...: def b(): return 42 |
|
94 | 94 | ...: class A: pass''' |
|
95 | 95 | |
|
96 | 96 | In [2]: extract_symbols(code, 'A,b,z') |
|
97 | 97 | Out[2]: (['class A: pass\\n', 'def b(): return 42\\n'], ['z']) |
|
98 | 98 | """ |
|
99 | 99 | symbols = symbols.split(',') |
|
100 | 100 | |
|
101 | 101 | # this will raise SyntaxError if code isn't valid Python |
|
102 | 102 | py_code = ast.parse(code) |
|
103 | 103 | |
|
104 | 104 | marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body] |
|
105 | 105 | code = code.split('\n') |
|
106 | 106 | |
|
107 | 107 | symbols_lines = {} |
|
108 | 108 | |
|
109 | 109 | # we already know the start_lineno of each symbol (marks). |
|
110 | 110 | # To find each end_lineno, we traverse in reverse order until each |
|
111 | 111 | # non-blank line |
|
112 | 112 | end = len(code) |
|
113 | 113 | for name, start in reversed(marks): |
|
114 | 114 | while not code[end - 1].strip(): |
|
115 | 115 | end -= 1 |
|
116 | 116 | if name: |
|
117 | 117 | symbols_lines[name] = (start - 1, end) |
|
118 | 118 | end = start - 1 |
|
119 | 119 | |
|
120 | 120 | # Now symbols_lines is a map |
|
121 | 121 | # {'symbol_name': (start_lineno, end_lineno), ...} |
|
122 | 122 | |
|
123 | 123 | # fill a list with chunks of codes for each requested symbol |
|
124 | 124 | blocks = [] |
|
125 | 125 | not_found = [] |
|
126 | 126 | for symbol in symbols: |
|
127 | 127 | if symbol in symbols_lines: |
|
128 | 128 | start, end = symbols_lines[symbol] |
|
129 | 129 | blocks.append('\n'.join(code[start:end]) + '\n') |
|
130 | 130 | else: |
|
131 | 131 | not_found.append(symbol) |
|
132 | 132 | |
|
133 | 133 | return blocks, not_found |
|
134 | 134 | |
|
135 | 135 | def strip_initial_indent(lines): |
|
136 | 136 | """For %load, strip indent from lines until finding an unindented line. |
|
137 | 137 | |
|
138 | 138 | https://github.com/ipython/ipython/issues/9775 |
|
139 | 139 | """ |
|
140 | 140 | indent_re = re.compile(r'\s+') |
|
141 | 141 | |
|
142 | 142 | it = iter(lines) |
|
143 | 143 | first_line = next(it) |
|
144 | 144 | indent_match = indent_re.match(first_line) |
|
145 | 145 | |
|
146 | 146 | if indent_match: |
|
147 | 147 | # First line was indented |
|
148 | 148 | indent = indent_match.group() |
|
149 | 149 | yield first_line[len(indent):] |
|
150 | 150 | |
|
151 | 151 | for line in it: |
|
152 | 152 | if line.startswith(indent): |
|
153 | 153 | yield line[len(indent):] |
|
154 | 154 | else: |
|
155 | 155 | # Less indented than the first line - stop dedenting |
|
156 | 156 | yield line |
|
157 | 157 | break |
|
158 | 158 | else: |
|
159 | 159 | yield first_line |
|
160 | 160 | |
|
161 | 161 | # Pass the remaining lines through without dedenting |
|
162 | 162 | for line in it: |
|
163 | 163 | yield line |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | class InteractivelyDefined(Exception): |
|
167 | 167 | """Exception for interactively defined variable in magic_edit""" |
|
168 | 168 | def __init__(self, index): |
|
169 | 169 | self.index = index |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | @magics_class |
|
173 | 173 | class CodeMagics(Magics): |
|
174 | 174 | """Magics related to code management (loading, saving, editing, ...).""" |
|
175 | 175 | |
|
176 | 176 | def __init__(self, *args, **kwargs): |
|
177 | 177 | self._knowntemps = set() |
|
178 | 178 | super(CodeMagics, self).__init__(*args, **kwargs) |
|
179 | 179 | |
|
180 | 180 | @line_magic |
|
181 | 181 | def save(self, parameter_s=''): |
|
182 | 182 | """Save a set of lines or a macro to a given filename. |
|
183 | 183 | |
|
184 | 184 | Usage:\\ |
|
185 | 185 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
186 | 186 | |
|
187 | 187 | Options: |
|
188 | 188 | |
|
189 | 189 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
190 | 190 | so that magics are loaded in their transformed version to valid |
|
191 | 191 | Python. If this option is given, the raw input as typed as the |
|
192 | 192 | command line is used instead. |
|
193 | 193 | |
|
194 | 194 | -f: force overwrite. If file exists, %save will prompt for overwrite |
|
195 | 195 | unless -f is given. |
|
196 | 196 | |
|
197 | 197 | -a: append to the file instead of overwriting it. |
|
198 | 198 | |
|
199 | 199 | This function uses the same syntax as %history for input ranges, |
|
200 | 200 | then saves the lines to the filename you specify. |
|
201 | 201 | |
|
202 | 202 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
203 | 203 | it asks for confirmation before overwriting existing files. |
|
204 | 204 | |
|
205 | 205 | If `-r` option is used, the default extension is `.ipy`. |
|
206 | 206 | """ |
|
207 | 207 | |
|
208 | 208 | opts,args = self.parse_options(parameter_s,'fra',mode='list') |
|
209 | 209 | if not args: |
|
210 | 210 | raise UsageError('Missing filename.') |
|
211 | 211 | raw = 'r' in opts |
|
212 | 212 | force = 'f' in opts |
|
213 | 213 | append = 'a' in opts |
|
214 | 214 | mode = 'a' if append else 'w' |
|
215 | 215 | ext = u'.ipy' if raw else u'.py' |
|
216 | 216 | fname, codefrom = args[0], " ".join(args[1:]) |
|
217 | 217 | if not fname.endswith((u'.py',u'.ipy')): |
|
218 | 218 | fname += ext |
|
219 | 219 | file_exists = os.path.isfile(fname) |
|
220 | 220 | if file_exists and not force and not append: |
|
221 | 221 | try: |
|
222 | 222 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') |
|
223 | 223 | except StdinNotImplementedError: |
|
224 | 224 | print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)) |
|
225 | 225 | return |
|
226 | 226 | if not overwrite : |
|
227 | 227 | print('Operation cancelled.') |
|
228 | 228 | return |
|
229 | 229 | try: |
|
230 | 230 | cmds = self.shell.find_user_code(codefrom,raw) |
|
231 | 231 | except (TypeError, ValueError) as e: |
|
232 | 232 | print(e.args[0]) |
|
233 | 233 | return |
|
234 | 234 | out = py3compat.cast_unicode(cmds) |
|
235 | 235 | with io.open(fname, mode, encoding="utf-8") as f: |
|
236 | 236 | if not file_exists or not append: |
|
237 | 237 | f.write(u"# coding: utf-8\n") |
|
238 | 238 | f.write(out) |
|
239 | 239 | # make sure we end on a newline |
|
240 | 240 | if not out.endswith(u'\n'): |
|
241 | 241 | f.write(u'\n') |
|
242 | 242 | print('The following commands were written to file `%s`:' % fname) |
|
243 | 243 | print(cmds) |
|
244 | 244 | |
|
245 | 245 | @line_magic |
|
246 | 246 | def pastebin(self, parameter_s=''): |
|
247 | 247 | """Upload code to Github's Gist paste bin, returning the URL. |
|
248 | 248 | |
|
249 | 249 | Usage:\\ |
|
250 | 250 | %pastebin [-d "Custom description"] 1-7 |
|
251 | 251 | |
|
252 | 252 | The argument can be an input history range, a filename, or the name of a |
|
253 | 253 | string or macro. |
|
254 | 254 | |
|
255 | 255 | Options: |
|
256 | 256 | |
|
257 | 257 | -d: Pass a custom description for the gist. The default will say |
|
258 | 258 | "Pasted from IPython". |
|
259 | 259 | """ |
|
260 | 260 | opts, args = self.parse_options(parameter_s, 'd:') |
|
261 | 261 | |
|
262 | 262 | try: |
|
263 | 263 | code = self.shell.find_user_code(args) |
|
264 | 264 | except (ValueError, TypeError) as e: |
|
265 | 265 | print(e.args[0]) |
|
266 | 266 | return |
|
267 | 267 | |
|
268 | 268 | # Deferred import |
|
269 | 269 | try: |
|
270 | 270 | from urllib.request import urlopen # Py 3 |
|
271 | 271 | except ImportError: |
|
272 | 272 | from urllib2 import urlopen |
|
273 | 273 | import json |
|
274 | 274 | post_data = json.dumps({ |
|
275 | 275 | "description": opts.get('d', "Pasted from IPython"), |
|
276 | 276 | "public": True, |
|
277 | 277 | "files": { |
|
278 | 278 | "file1.py": { |
|
279 | 279 | "content": code |
|
280 | 280 | } |
|
281 | 281 | } |
|
282 | 282 | }).encode('utf-8') |
|
283 | 283 | |
|
284 | 284 | response = urlopen("https://api.github.com/gists", post_data) |
|
285 | 285 | response_data = json.loads(response.read().decode('utf-8')) |
|
286 | 286 | return response_data['html_url'] |
|
287 | 287 | |
|
288 | 288 | @line_magic |
|
289 | 289 | def loadpy(self, arg_s): |
|
290 | 290 | """Alias of `%load` |
|
291 | 291 | |
|
292 | 292 | `%loadpy` has gained some flexibility and dropped the requirement of a `.py` |
|
293 | 293 | extension. So it has been renamed simply into %load. You can look at |
|
294 | 294 | `%load`'s docstring for more info. |
|
295 | 295 | """ |
|
296 | 296 | self.load(arg_s) |
|
297 | 297 | |
|
298 | 298 | @line_magic |
|
299 | 299 | def load(self, arg_s): |
|
300 | 300 | """Load code into the current frontend. |
|
301 | 301 | |
|
302 | 302 | Usage:\\ |
|
303 | 303 | %load [options] source |
|
304 | 304 | |
|
305 | 305 | where source can be a filename, URL, input history range, macro, or |
|
306 | 306 | element in the user namespace |
|
307 | 307 | |
|
308 | 308 | Options: |
|
309 | 309 | |
|
310 | 310 | -r <lines>: Specify lines or ranges of lines to load from the source. |
|
311 | 311 | Ranges could be specified as x-y (x..y) or in python-style x:y |
|
312 | 312 | (x..(y-1)). Both limits x and y can be left blank (meaning the |
|
313 | 313 | beginning and end of the file, respectively). |
|
314 | 314 | |
|
315 | 315 | -s <symbols>: Specify function or classes to load from python source. |
|
316 | 316 | |
|
317 | 317 | -y : Don't ask confirmation for loading source above 200 000 characters. |
|
318 | 318 | |
|
319 | 319 | -n : Include the user's namespace when searching for source code. |
|
320 | 320 | |
|
321 | 321 | This magic command can either take a local filename, a URL, an history |
|
322 | 322 | range (see %history) or a macro as argument, it will prompt for |
|
323 | 323 | confirmation before loading source with more than 200 000 characters, unless |
|
324 | 324 | -y flag is passed or if the frontend does not support raw_input:: |
|
325 | 325 | |
|
326 | 326 | %load myscript.py |
|
327 | 327 | %load 7-27 |
|
328 | 328 | %load myMacro |
|
329 | 329 | %load http://www.example.com/myscript.py |
|
330 | 330 | %load -r 5-10 myscript.py |
|
331 | 331 | %load -r 10-20,30,40: foo.py |
|
332 | 332 | %load -s MyClass,wonder_function myscript.py |
|
333 | 333 | %load -n MyClass |
|
334 | 334 | %load -n my_module.wonder_function |
|
335 | 335 | """ |
|
336 | 336 | opts,args = self.parse_options(arg_s,'yns:r:') |
|
337 | 337 | |
|
338 | 338 | if not args: |
|
339 | 339 | raise UsageError('Missing filename, URL, input history range, ' |
|
340 | 340 | 'macro, or element in the user namespace.') |
|
341 | 341 | |
|
342 | 342 | search_ns = 'n' in opts |
|
343 | 343 | |
|
344 | 344 | contents = self.shell.find_user_code(args, search_ns=search_ns) |
|
345 | 345 | |
|
346 | 346 | if 's' in opts: |
|
347 | 347 | try: |
|
348 | 348 | blocks, not_found = extract_symbols(contents, opts['s']) |
|
349 | 349 | except SyntaxError: |
|
350 | 350 | # non python code |
|
351 | 351 | error("Unable to parse the input as valid Python code") |
|
352 | 352 | return |
|
353 | 353 | |
|
354 | 354 | if len(not_found) == 1: |
|
355 | 355 | warn('The symbol `%s` was not found' % not_found[0]) |
|
356 | 356 | elif len(not_found) > 1: |
|
357 | 357 | warn('The symbols %s were not found' % get_text_list(not_found, |
|
358 | 358 | wrap_item_with='`') |
|
359 | 359 | ) |
|
360 | 360 | |
|
361 | 361 | contents = '\n'.join(blocks) |
|
362 | 362 | |
|
363 | 363 | if 'r' in opts: |
|
364 | 364 | ranges = opts['r'].replace(',', ' ') |
|
365 | 365 | lines = contents.split('\n') |
|
366 | 366 | slices = extract_code_ranges(ranges) |
|
367 | 367 | contents = [lines[slice(*slc)] for slc in slices] |
|
368 | 368 | contents = '\n'.join(strip_initial_indent(chain.from_iterable(contents))) |
|
369 | 369 | |
|
370 | 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 | 373 | # so in average, more than 5000 lines |
|
374 | 374 | if l > 200000 and 'y' not in opts: |
|
375 | 375 | try: |
|
376 | 376 | ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\ |
|
377 | 377 | " (%d characters). Continue (y/[N]) ?" % l), default='n' ) |
|
378 | 378 | except StdinNotImplementedError: |
|
379 | #asume yes if raw input not implemented | |
|
379 | #assume yes if raw input not implemented | |
|
380 | 380 | ans = True |
|
381 | 381 | |
|
382 | 382 | if ans is False : |
|
383 | 383 | print('Operation cancelled.') |
|
384 | 384 | return |
|
385 | 385 | |
|
386 | 386 | contents = "# %load {}\n".format(arg_s) + contents |
|
387 | 387 | |
|
388 | 388 | self.shell.set_next_input(contents, replace=True) |
|
389 | 389 | |
|
390 | 390 | @staticmethod |
|
391 | 391 | def _find_edit_target(shell, args, opts, last_call): |
|
392 | 392 | """Utility method used by magic_edit to find what to edit.""" |
|
393 | 393 | |
|
394 | 394 | def make_filename(arg): |
|
395 | 395 | "Make a filename from the given args" |
|
396 | 396 | try: |
|
397 | 397 | filename = get_py_filename(arg) |
|
398 | 398 | except IOError: |
|
399 | 399 | # If it ends with .py but doesn't already exist, assume we want |
|
400 | 400 | # a new file. |
|
401 | 401 | if arg.endswith('.py'): |
|
402 | 402 | filename = arg |
|
403 | 403 | else: |
|
404 | 404 | filename = None |
|
405 | 405 | return filename |
|
406 | 406 | |
|
407 | 407 | # Set a few locals from the options for convenience: |
|
408 | 408 | opts_prev = 'p' in opts |
|
409 | 409 | opts_raw = 'r' in opts |
|
410 | 410 | |
|
411 | 411 | # custom exceptions |
|
412 | 412 | class DataIsObject(Exception): pass |
|
413 | 413 | |
|
414 | 414 | # Default line number value |
|
415 | 415 | lineno = opts.get('n',None) |
|
416 | 416 | |
|
417 | 417 | if opts_prev: |
|
418 | 418 | args = '_%s' % last_call[0] |
|
419 | 419 | if args not in shell.user_ns: |
|
420 | 420 | args = last_call[1] |
|
421 | 421 | |
|
422 | 422 | # by default this is done with temp files, except when the given |
|
423 | 423 | # arg is a filename |
|
424 | 424 | use_temp = True |
|
425 | 425 | |
|
426 | 426 | data = '' |
|
427 | 427 | |
|
428 | 428 | # First, see if the arguments should be a filename. |
|
429 | 429 | filename = make_filename(args) |
|
430 | 430 | if filename: |
|
431 | 431 | use_temp = False |
|
432 | 432 | elif args: |
|
433 | 433 | # Mode where user specifies ranges of lines, like in %macro. |
|
434 | 434 | data = shell.extract_input_lines(args, opts_raw) |
|
435 | 435 | if not data: |
|
436 | 436 | try: |
|
437 | 437 | # Load the parameter given as a variable. If not a string, |
|
438 | 438 | # process it as an object instead (below) |
|
439 | 439 | |
|
440 | 440 | #print '*** args',args,'type',type(args) # dbg |
|
441 | 441 | data = eval(args, shell.user_ns) |
|
442 | 442 | if not isinstance(data, str): |
|
443 | 443 | raise DataIsObject |
|
444 | 444 | |
|
445 | 445 | except (NameError,SyntaxError): |
|
446 | 446 | # given argument is not a variable, try as a filename |
|
447 | 447 | filename = make_filename(args) |
|
448 | 448 | if filename is None: |
|
449 | 449 | warn("Argument given (%s) can't be found as a variable " |
|
450 | 450 | "or as a filename." % args) |
|
451 | 451 | return (None, None, None) |
|
452 | 452 | use_temp = False |
|
453 | 453 | |
|
454 | 454 | except DataIsObject: |
|
455 | 455 | # macros have a special edit function |
|
456 | 456 | if isinstance(data, Macro): |
|
457 | 457 | raise MacroToEdit(data) |
|
458 | 458 | |
|
459 | 459 | # For objects, try to edit the file where they are defined |
|
460 | 460 | filename = find_file(data) |
|
461 | 461 | if filename: |
|
462 | 462 | if 'fakemodule' in filename.lower() and \ |
|
463 | 463 | inspect.isclass(data): |
|
464 | 464 | # class created by %edit? Try to find source |
|
465 | 465 | # by looking for method definitions instead, the |
|
466 | 466 | # __module__ in those classes is FakeModule. |
|
467 | 467 | attrs = [getattr(data, aname) for aname in dir(data)] |
|
468 | 468 | for attr in attrs: |
|
469 | 469 | if not inspect.ismethod(attr): |
|
470 | 470 | continue |
|
471 | 471 | filename = find_file(attr) |
|
472 | 472 | if filename and \ |
|
473 | 473 | 'fakemodule' not in filename.lower(): |
|
474 | 474 | # change the attribute to be the edit |
|
475 | 475 | # target instead |
|
476 | 476 | data = attr |
|
477 | 477 | break |
|
478 | 478 | |
|
479 | 479 | m = ipython_input_pat.match(os.path.basename(filename)) |
|
480 | 480 | if m: |
|
481 | 481 | raise InteractivelyDefined(int(m.groups()[0])) |
|
482 | 482 | |
|
483 | 483 | datafile = 1 |
|
484 | 484 | if filename is None: |
|
485 | 485 | filename = make_filename(args) |
|
486 | 486 | datafile = 1 |
|
487 | 487 | if filename is not None: |
|
488 | 488 | # only warn about this if we get a real name |
|
489 | 489 | warn('Could not find file where `%s` is defined.\n' |
|
490 | 490 | 'Opening a file named `%s`' % (args, filename)) |
|
491 | 491 | # Now, make sure we can actually read the source (if it was |
|
492 | 492 | # in a temp file it's gone by now). |
|
493 | 493 | if datafile: |
|
494 | 494 | if lineno is None: |
|
495 | 495 | lineno = find_source_lines(data) |
|
496 | 496 | if lineno is None: |
|
497 | 497 | filename = make_filename(args) |
|
498 | 498 | if filename is None: |
|
499 | 499 | warn('The file where `%s` was defined ' |
|
500 | 500 | 'cannot be read or found.' % data) |
|
501 | 501 | return (None, None, None) |
|
502 | 502 | use_temp = False |
|
503 | 503 | |
|
504 | 504 | if use_temp: |
|
505 | 505 | filename = shell.mktempfile(data) |
|
506 | 506 | print('IPython will make a temporary file named:',filename) |
|
507 | 507 | |
|
508 | 508 | # use last_call to remember the state of the previous call, but don't |
|
509 | 509 | # let it be clobbered by successive '-p' calls. |
|
510 | 510 | try: |
|
511 | 511 | last_call[0] = shell.displayhook.prompt_count |
|
512 | 512 | if not opts_prev: |
|
513 | 513 | last_call[1] = args |
|
514 | 514 | except: |
|
515 | 515 | pass |
|
516 | 516 | |
|
517 | 517 | |
|
518 | 518 | return filename, lineno, use_temp |
|
519 | 519 | |
|
520 | 520 | def _edit_macro(self,mname,macro): |
|
521 | 521 | """open an editor with the macro data in a file""" |
|
522 | 522 | filename = self.shell.mktempfile(macro.value) |
|
523 | 523 | self.shell.hooks.editor(filename) |
|
524 | 524 | |
|
525 | 525 | # and make a new macro object, to replace the old one |
|
526 | 526 | with open(filename) as mfile: |
|
527 | 527 | mvalue = mfile.read() |
|
528 | 528 | self.shell.user_ns[mname] = Macro(mvalue) |
|
529 | 529 | |
|
530 | 530 | @skip_doctest |
|
531 | 531 | @line_magic |
|
532 | 532 | def edit(self, parameter_s='',last_call=['','']): |
|
533 | 533 | """Bring up an editor and execute the resulting code. |
|
534 | 534 | |
|
535 | 535 | Usage: |
|
536 | 536 | %edit [options] [args] |
|
537 | 537 | |
|
538 | 538 | %edit runs IPython's editor hook. The default version of this hook is |
|
539 | 539 | set to call the editor specified by your $EDITOR environment variable. |
|
540 | 540 | If this isn't found, it will default to vi under Linux/Unix and to |
|
541 | 541 | notepad under Windows. See the end of this docstring for how to change |
|
542 | 542 | the editor hook. |
|
543 | 543 | |
|
544 | 544 | You can also set the value of this editor via the |
|
545 | 545 | ``TerminalInteractiveShell.editor`` option in your configuration file. |
|
546 | 546 | This is useful if you wish to use a different editor from your typical |
|
547 | 547 | default with IPython (and for Windows users who typically don't set |
|
548 | 548 | environment variables). |
|
549 | 549 | |
|
550 | 550 | This command allows you to conveniently edit multi-line code right in |
|
551 | 551 | your IPython session. |
|
552 | 552 | |
|
553 | 553 | If called without arguments, %edit opens up an empty editor with a |
|
554 | 554 | temporary file and will execute the contents of this file when you |
|
555 | 555 | close it (don't forget to save it!). |
|
556 | 556 | |
|
557 | 557 | |
|
558 | 558 | Options: |
|
559 | 559 | |
|
560 | 560 | -n <number>: open the editor at a specified line number. By default, |
|
561 | 561 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
562 | 562 | you can configure this by providing your own modified hook if your |
|
563 | 563 | favorite editor supports line-number specifications with a different |
|
564 | 564 | syntax. |
|
565 | 565 | |
|
566 | 566 | -p: this will call the editor with the same data as the previous time |
|
567 | 567 | it was used, regardless of how long ago (in your current session) it |
|
568 | 568 | was. |
|
569 | 569 | |
|
570 | 570 | -r: use 'raw' input. This option only applies to input taken from the |
|
571 | 571 | user's history. By default, the 'processed' history is used, so that |
|
572 | 572 | magics are loaded in their transformed version to valid Python. If |
|
573 | 573 | this option is given, the raw input as typed as the command line is |
|
574 | 574 | used instead. When you exit the editor, it will be executed by |
|
575 | 575 | IPython's own processor. |
|
576 | 576 | |
|
577 | 577 | -x: do not execute the edited code immediately upon exit. This is |
|
578 | 578 | mainly useful if you are editing programs which need to be called with |
|
579 | 579 | command line arguments, which you can then do using %run. |
|
580 | 580 | |
|
581 | 581 | |
|
582 | 582 | Arguments: |
|
583 | 583 | |
|
584 | 584 | If arguments are given, the following possibilities exist: |
|
585 | 585 | |
|
586 | 586 | - If the argument is a filename, IPython will load that into the |
|
587 | 587 | editor. It will execute its contents with execfile() when you exit, |
|
588 | 588 | loading any code in the file into your interactive namespace. |
|
589 | 589 | |
|
590 | 590 | - The arguments are ranges of input history, e.g. "7 ~1/4-6". |
|
591 | 591 | The syntax is the same as in the %history magic. |
|
592 | 592 | |
|
593 | 593 | - If the argument is a string variable, its contents are loaded |
|
594 | 594 | into the editor. You can thus edit any string which contains |
|
595 | 595 | python code (including the result of previous edits). |
|
596 | 596 | |
|
597 | 597 | - If the argument is the name of an object (other than a string), |
|
598 | 598 | IPython will try to locate the file where it was defined and open the |
|
599 | 599 | editor at the point where it is defined. You can use `%edit function` |
|
600 | 600 | to load an editor exactly at the point where 'function' is defined, |
|
601 | 601 | edit it and have the file be executed automatically. |
|
602 | 602 | |
|
603 | 603 | - If the object is a macro (see %macro for details), this opens up your |
|
604 | 604 | specified editor with a temporary file containing the macro's data. |
|
605 | 605 | Upon exit, the macro is reloaded with the contents of the file. |
|
606 | 606 | |
|
607 | 607 | Note: opening at an exact line is only supported under Unix, and some |
|
608 | 608 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
609 | 609 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
610 | 610 | (X)Emacs, vi, jed, pico and joe all do. |
|
611 | 611 | |
|
612 | 612 | After executing your code, %edit will return as output the code you |
|
613 | 613 | typed in the editor (except when it was an existing file). This way |
|
614 | 614 | you can reload the code in further invocations of %edit as a variable, |
|
615 | 615 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
616 | 616 | the output. |
|
617 | 617 | |
|
618 | 618 | Note that %edit is also available through the alias %ed. |
|
619 | 619 | |
|
620 | 620 | This is an example of creating a simple function inside the editor and |
|
621 | 621 | then modifying it. First, start up the editor:: |
|
622 | 622 | |
|
623 | 623 | In [1]: edit |
|
624 | 624 | Editing... done. Executing edited code... |
|
625 | 625 | Out[1]: 'def foo():\\n print "foo() was defined in an editing |
|
626 | 626 | session"\\n' |
|
627 | 627 | |
|
628 | 628 | We can then call the function foo():: |
|
629 | 629 | |
|
630 | 630 | In [2]: foo() |
|
631 | 631 | foo() was defined in an editing session |
|
632 | 632 | |
|
633 | 633 | Now we edit foo. IPython automatically loads the editor with the |
|
634 | 634 | (temporary) file where foo() was previously defined:: |
|
635 | 635 | |
|
636 | 636 | In [3]: edit foo |
|
637 | 637 | Editing... done. Executing edited code... |
|
638 | 638 | |
|
639 | 639 | And if we call foo() again we get the modified version:: |
|
640 | 640 | |
|
641 | 641 | In [4]: foo() |
|
642 | 642 | foo() has now been changed! |
|
643 | 643 | |
|
644 | 644 | Here is an example of how to edit a code snippet successive |
|
645 | 645 | times. First we call the editor:: |
|
646 | 646 | |
|
647 | 647 | In [5]: edit |
|
648 | 648 | Editing... done. Executing edited code... |
|
649 | 649 | hello |
|
650 | 650 | Out[5]: "print 'hello'\\n" |
|
651 | 651 | |
|
652 | 652 | Now we call it again with the previous output (stored in _):: |
|
653 | 653 | |
|
654 | 654 | In [6]: edit _ |
|
655 | 655 | Editing... done. Executing edited code... |
|
656 | 656 | hello world |
|
657 | 657 | Out[6]: "print 'hello world'\\n" |
|
658 | 658 | |
|
659 | 659 | Now we call it with the output #8 (stored in _8, also as Out[8]):: |
|
660 | 660 | |
|
661 | 661 | In [7]: edit _8 |
|
662 | 662 | Editing... done. Executing edited code... |
|
663 | 663 | hello again |
|
664 | 664 | Out[7]: "print 'hello again'\\n" |
|
665 | 665 | |
|
666 | 666 | |
|
667 | 667 | Changing the default editor hook: |
|
668 | 668 | |
|
669 | 669 | If you wish to write your own editor hook, you can put it in a |
|
670 | 670 | configuration file which you load at startup time. The default hook |
|
671 | 671 | is defined in the IPython.core.hooks module, and you can use that as a |
|
672 | 672 | starting example for further modifications. That file also has |
|
673 | 673 | general instructions on how to set a new hook for use once you've |
|
674 | 674 | defined it.""" |
|
675 | 675 | opts,args = self.parse_options(parameter_s,'prxn:') |
|
676 | 676 | |
|
677 | 677 | try: |
|
678 | 678 | filename, lineno, is_temp = self._find_edit_target(self.shell, |
|
679 | 679 | args, opts, last_call) |
|
680 | 680 | except MacroToEdit as e: |
|
681 | 681 | self._edit_macro(args, e.args[0]) |
|
682 | 682 | return |
|
683 | 683 | except InteractivelyDefined as e: |
|
684 | 684 | print("Editing In[%i]" % e.index) |
|
685 | 685 | args = str(e.index) |
|
686 | 686 | filename, lineno, is_temp = self._find_edit_target(self.shell, |
|
687 | 687 | args, opts, last_call) |
|
688 | 688 | if filename is None: |
|
689 | 689 | # nothing was found, warnings have already been issued, |
|
690 | 690 | # just give up. |
|
691 | 691 | return |
|
692 | 692 | |
|
693 | 693 | if is_temp: |
|
694 | 694 | self._knowntemps.add(filename) |
|
695 | 695 | elif (filename in self._knowntemps): |
|
696 | 696 | is_temp = True |
|
697 | 697 | |
|
698 | 698 | |
|
699 | 699 | # do actual editing here |
|
700 | 700 | print('Editing...', end=' ') |
|
701 | 701 | sys.stdout.flush() |
|
702 | 702 | try: |
|
703 | 703 | # Quote filenames that may have spaces in them |
|
704 | 704 | if ' ' in filename: |
|
705 | 705 | filename = "'%s'" % filename |
|
706 | 706 | self.shell.hooks.editor(filename,lineno) |
|
707 | 707 | except TryNext: |
|
708 | 708 | warn('Could not open editor') |
|
709 | 709 | return |
|
710 | 710 | |
|
711 | 711 | # XXX TODO: should this be generalized for all string vars? |
|
712 | 712 | # For now, this is special-cased to blocks created by cpaste |
|
713 | 713 | if args.strip() == 'pasted_block': |
|
714 | 714 | with open(filename, 'r') as f: |
|
715 | 715 | self.shell.user_ns['pasted_block'] = f.read() |
|
716 | 716 | |
|
717 | 717 | if 'x' in opts: # -x prevents actual execution |
|
718 | 718 | print() |
|
719 | 719 | else: |
|
720 | 720 | print('done. Executing edited code...') |
|
721 | 721 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
722 | 722 | if not is_temp: |
|
723 | 723 | self.shell.user_ns['__file__'] = filename |
|
724 | 724 | if 'r' in opts: # Untranslated IPython code |
|
725 | 725 | with open(filename, 'r') as f: |
|
726 | 726 | source = f.read() |
|
727 | 727 | self.shell.run_cell(source, store_history=False) |
|
728 | 728 | else: |
|
729 | 729 | self.shell.safe_execfile(filename, self.shell.user_ns, |
|
730 | 730 | self.shell.user_ns) |
|
731 | 731 | |
|
732 | 732 | if is_temp: |
|
733 | 733 | try: |
|
734 | 734 | return open(filename).read() |
|
735 | 735 | except IOError as msg: |
|
736 | 736 | if msg.filename == filename: |
|
737 | 737 | warn('File not found. Did you forget to save?') |
|
738 | 738 | return |
|
739 | 739 | else: |
|
740 | 740 | self.shell.showtraceback() |
@@ -1,996 +1,996 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for inspecting Python objects. |
|
3 | 3 | |
|
4 | 4 | Uses syntax highlighting for presenting the various information elements. |
|
5 | 5 | |
|
6 | 6 | Similar in spirit to the inspect module, but all calls take a name argument to |
|
7 | 7 | reference the name under which an object is being read. |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | # Copyright (c) IPython Development Team. |
|
11 | 11 | # Distributed under the terms of the Modified BSD License. |
|
12 | 12 | |
|
13 | 13 | __all__ = ['Inspector','InspectColors'] |
|
14 | 14 | |
|
15 | 15 | # stdlib modules |
|
16 | 16 | import inspect |
|
17 | 17 | from inspect import signature |
|
18 | 18 | import linecache |
|
19 | 19 | import warnings |
|
20 | 20 | import os |
|
21 | 21 | from textwrap import dedent |
|
22 | 22 | import types |
|
23 | 23 | import io as stdlib_io |
|
24 | 24 | from itertools import zip_longest |
|
25 | 25 | |
|
26 | 26 | # IPython's own |
|
27 | 27 | from IPython.core import page |
|
28 | 28 | from IPython.lib.pretty import pretty |
|
29 | 29 | from IPython.testing.skipdoctest import skip_doctest |
|
30 | 30 | from IPython.utils import PyColorize |
|
31 | 31 | from IPython.utils import openpy |
|
32 | 32 | from IPython.utils import py3compat |
|
33 | 33 | from IPython.utils.dir2 import safe_hasattr |
|
34 | 34 | from IPython.utils.path import compress_user |
|
35 | 35 | from IPython.utils.text import indent |
|
36 | 36 | from IPython.utils.wildcard import list_namespace |
|
37 | 37 | from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable |
|
38 | 38 | from IPython.utils.py3compat import cast_unicode |
|
39 | 39 | from IPython.utils.colorable import Colorable |
|
40 | 40 | from IPython.utils.decorators import undoc |
|
41 | 41 | |
|
42 | 42 | from pygments import highlight |
|
43 | 43 | from pygments.lexers import PythonLexer |
|
44 | 44 | from pygments.formatters import HtmlFormatter |
|
45 | 45 | |
|
46 | 46 | def pylight(code): |
|
47 | 47 | return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True)) |
|
48 | 48 | |
|
49 | 49 | # builtin docstrings to ignore |
|
50 | 50 | _func_call_docstring = types.FunctionType.__call__.__doc__ |
|
51 | 51 | _object_init_docstring = object.__init__.__doc__ |
|
52 | 52 | _builtin_type_docstrings = { |
|
53 | 53 | inspect.getdoc(t) for t in (types.ModuleType, types.MethodType, |
|
54 | 54 | types.FunctionType, property) |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | _builtin_func_type = type(all) |
|
58 | 58 | _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions |
|
59 | 59 | #**************************************************************************** |
|
60 | 60 | # Builtin color schemes |
|
61 | 61 | |
|
62 | 62 | Colors = TermColors # just a shorthand |
|
63 | 63 | |
|
64 | 64 | InspectColors = PyColorize.ANSICodeColors |
|
65 | 65 | |
|
66 | 66 | #**************************************************************************** |
|
67 | 67 | # Auxiliary functions and objects |
|
68 | 68 | |
|
69 | 69 | # See the messaging spec for the definition of all these fields. This list |
|
70 | 70 | # effectively defines the order of display |
|
71 | 71 | info_fields = ['type_name', 'base_class', 'string_form', 'namespace', |
|
72 | 72 | 'length', 'file', 'definition', 'docstring', 'source', |
|
73 | 73 | 'init_definition', 'class_docstring', 'init_docstring', |
|
74 | 74 | 'call_def', 'call_docstring', |
|
75 | 75 | # These won't be printed but will be used to determine how to |
|
76 | 76 | # format the object |
|
77 | 77 | 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name' |
|
78 | 78 | ] |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def object_info(**kw): |
|
82 | 82 | """Make an object info dict with all fields present.""" |
|
83 | 83 | infodict = dict(zip_longest(info_fields, [None])) |
|
84 | 84 | infodict.update(kw) |
|
85 | 85 | return infodict |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def get_encoding(obj): |
|
89 | 89 | """Get encoding for python source file defining obj |
|
90 | 90 | |
|
91 | 91 | Returns None if obj is not defined in a sourcefile. |
|
92 | 92 | """ |
|
93 | 93 | ofile = find_file(obj) |
|
94 | 94 | # run contents of file through pager starting at line where the object |
|
95 | 95 | # is defined, as long as the file isn't binary and is actually on the |
|
96 | 96 | # filesystem. |
|
97 | 97 | if ofile is None: |
|
98 | 98 | return None |
|
99 | 99 | elif ofile.endswith(('.so', '.dll', '.pyd')): |
|
100 | 100 | return None |
|
101 | 101 | elif not os.path.isfile(ofile): |
|
102 | 102 | return None |
|
103 | 103 | else: |
|
104 | 104 | # Print only text files, not extension binaries. Note that |
|
105 | 105 | # getsourcelines returns lineno with 1-offset and page() uses |
|
106 | 106 | # 0-offset, so we must adjust. |
|
107 | 107 | with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2 |
|
108 | 108 | encoding, lines = openpy.detect_encoding(buffer.readline) |
|
109 | 109 | return encoding |
|
110 | 110 | |
|
111 | 111 | def getdoc(obj): |
|
112 | 112 | """Stable wrapper around inspect.getdoc. |
|
113 | 113 | |
|
114 | 114 | This can't crash because of attribute problems. |
|
115 | 115 | |
|
116 | 116 | It also attempts to call a getdoc() method on the given object. This |
|
117 | 117 | allows objects which provide their docstrings via non-standard mechanisms |
|
118 | 118 | (like Pyro proxies) to still be inspected by ipython's ? system. |
|
119 | 119 | """ |
|
120 | 120 | # Allow objects to offer customized documentation via a getdoc method: |
|
121 | 121 | try: |
|
122 | 122 | ds = obj.getdoc() |
|
123 | 123 | except Exception: |
|
124 | 124 | pass |
|
125 | 125 | else: |
|
126 | 126 | if isinstance(ds, str): |
|
127 | 127 | return inspect.cleandoc(ds) |
|
128 | 128 | docstr = inspect.getdoc(obj) |
|
129 | 129 | encoding = get_encoding(obj) |
|
130 | 130 | return py3compat.cast_unicode(docstr, encoding=encoding) |
|
131 | 131 | |
|
132 | 132 | |
|
133 | 133 | def getsource(obj, oname=''): |
|
134 | 134 | """Wrapper around inspect.getsource. |
|
135 | 135 | |
|
136 | 136 | This can be modified by other projects to provide customized source |
|
137 | 137 | extraction. |
|
138 | 138 | |
|
139 | 139 | Parameters |
|
140 | 140 | ---------- |
|
141 | 141 | obj : object |
|
142 | 142 | an object whose source code we will attempt to extract |
|
143 | 143 | oname : str |
|
144 | 144 | (optional) a name under which the object is known |
|
145 | 145 | |
|
146 | 146 | Returns |
|
147 | 147 | ------- |
|
148 | 148 | src : unicode or None |
|
149 | 149 | |
|
150 | 150 | """ |
|
151 | 151 | |
|
152 | 152 | if isinstance(obj, property): |
|
153 | 153 | sources = [] |
|
154 | 154 | for attrname in ['fget', 'fset', 'fdel']: |
|
155 | 155 | fn = getattr(obj, attrname) |
|
156 | 156 | if fn is not None: |
|
157 | 157 | encoding = get_encoding(fn) |
|
158 | 158 | oname_prefix = ('%s.' % oname) if oname else '' |
|
159 | 159 | sources.append(cast_unicode( |
|
160 | 160 | ''.join(('# ', oname_prefix, attrname)), |
|
161 | 161 | encoding=encoding)) |
|
162 | 162 | if inspect.isfunction(fn): |
|
163 | 163 | sources.append(dedent(getsource(fn))) |
|
164 | 164 | else: |
|
165 | 165 | # Default str/repr only prints function name, |
|
166 | 166 | # pretty.pretty prints module name too. |
|
167 | 167 | sources.append(cast_unicode( |
|
168 | 168 | '%s%s = %s\n' % ( |
|
169 | 169 | oname_prefix, attrname, pretty(fn)), |
|
170 | 170 | encoding=encoding)) |
|
171 | 171 | if sources: |
|
172 | 172 | return '\n'.join(sources) |
|
173 | 173 | else: |
|
174 | 174 | return None |
|
175 | 175 | |
|
176 | 176 | else: |
|
177 | 177 | # Get source for non-property objects. |
|
178 | 178 | |
|
179 | 179 | obj = _get_wrapped(obj) |
|
180 | 180 | |
|
181 | 181 | try: |
|
182 | 182 | src = inspect.getsource(obj) |
|
183 | 183 | except TypeError: |
|
184 | 184 | # The object itself provided no meaningful source, try looking for |
|
185 | 185 | # its class definition instead. |
|
186 | 186 | if hasattr(obj, '__class__'): |
|
187 | 187 | try: |
|
188 | 188 | src = inspect.getsource(obj.__class__) |
|
189 | 189 | except TypeError: |
|
190 | 190 | return None |
|
191 | 191 | |
|
192 | 192 | encoding = get_encoding(obj) |
|
193 | 193 | return cast_unicode(src, encoding=encoding) |
|
194 | 194 | |
|
195 | 195 | |
|
196 | 196 | def is_simple_callable(obj): |
|
197 | 197 | """True if obj is a function ()""" |
|
198 | 198 | return (inspect.isfunction(obj) or inspect.ismethod(obj) or \ |
|
199 | 199 | isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type)) |
|
200 | 200 | |
|
201 | 201 | |
|
202 | 202 | def getargspec(obj): |
|
203 | 203 | """Wrapper around :func:`inspect.getfullargspec` on Python 3, and |
|
204 | 204 | :func:inspect.getargspec` on Python 2. |
|
205 | 205 | |
|
206 | 206 | In addition to functions and methods, this can also handle objects with a |
|
207 | 207 | ``__call__`` attribute. |
|
208 | 208 | """ |
|
209 | 209 | if safe_hasattr(obj, '__call__') and not is_simple_callable(obj): |
|
210 | 210 | obj = obj.__call__ |
|
211 | 211 | |
|
212 | 212 | return inspect.getfullargspec(obj) |
|
213 | 213 | |
|
214 | 214 | |
|
215 | 215 | def format_argspec(argspec): |
|
216 | 216 | """Format argspect, convenience wrapper around inspect's. |
|
217 | 217 | |
|
218 | 218 | This takes a dict instead of ordered arguments and calls |
|
219 | 219 | inspect.format_argspec with the arguments in the necessary order. |
|
220 | 220 | """ |
|
221 | 221 | return inspect.formatargspec(argspec['args'], argspec['varargs'], |
|
222 | 222 | argspec['varkw'], argspec['defaults']) |
|
223 | 223 | |
|
224 | 224 | @undoc |
|
225 | 225 | def call_tip(oinfo, format_call=True): |
|
226 | 226 | """DEPRECATED. Extract call tip data from an oinfo dict. |
|
227 | 227 | """ |
|
228 | 228 | warnings.warn('`call_tip` function is deprecated as of IPython 6.0' |
|
229 | 229 | 'and will be removed in future versions.', DeprecationWarning, stacklevel=2) |
|
230 | 230 | # Get call definition |
|
231 | 231 | argspec = oinfo.get('argspec') |
|
232 | 232 | if argspec is None: |
|
233 | 233 | call_line = None |
|
234 | 234 | else: |
|
235 | 235 | # Callable objects will have 'self' as their first argument, prune |
|
236 | 236 | # it out if it's there for clarity (since users do *not* pass an |
|
237 | 237 | # extra first argument explicitly). |
|
238 | 238 | try: |
|
239 | 239 | has_self = argspec['args'][0] == 'self' |
|
240 | 240 | except (KeyError, IndexError): |
|
241 | 241 | pass |
|
242 | 242 | else: |
|
243 | 243 | if has_self: |
|
244 | 244 | argspec['args'] = argspec['args'][1:] |
|
245 | 245 | |
|
246 | 246 | call_line = oinfo['name']+format_argspec(argspec) |
|
247 | 247 | |
|
248 | 248 | # Now get docstring. |
|
249 | 249 | # The priority is: call docstring, constructor docstring, main one. |
|
250 | 250 | doc = oinfo.get('call_docstring') |
|
251 | 251 | if doc is None: |
|
252 | 252 | doc = oinfo.get('init_docstring') |
|
253 | 253 | if doc is None: |
|
254 | 254 | doc = oinfo.get('docstring','') |
|
255 | 255 | |
|
256 | 256 | return call_line, doc |
|
257 | 257 | |
|
258 | 258 | |
|
259 | 259 | def _get_wrapped(obj): |
|
260 | 260 | """Get the original object if wrapped in one or more @decorators |
|
261 | 261 | |
|
262 | 262 | Some objects automatically construct similar objects on any unrecognised |
|
263 | 263 | attribute access (e.g. unittest.mock.call). To protect against infinite loops, |
|
264 | 264 | this will arbitrarily cut off after 100 levels of obj.__wrapped__ |
|
265 | 265 | attribute access. --TK, Jan 2016 |
|
266 | 266 | """ |
|
267 | 267 | orig_obj = obj |
|
268 | 268 | i = 0 |
|
269 | 269 | while safe_hasattr(obj, '__wrapped__'): |
|
270 | 270 | obj = obj.__wrapped__ |
|
271 | 271 | i += 1 |
|
272 | 272 | if i > 100: |
|
273 | 273 | # __wrapped__ is probably a lie, so return the thing we started with |
|
274 | 274 | return orig_obj |
|
275 | 275 | return obj |
|
276 | 276 | |
|
277 | 277 | def find_file(obj): |
|
278 | 278 | """Find the absolute path to the file where an object was defined. |
|
279 | 279 | |
|
280 | 280 | This is essentially a robust wrapper around `inspect.getabsfile`. |
|
281 | 281 | |
|
282 | 282 | Returns None if no file can be found. |
|
283 | 283 | |
|
284 | 284 | Parameters |
|
285 | 285 | ---------- |
|
286 | 286 | obj : any Python object |
|
287 | 287 | |
|
288 | 288 | Returns |
|
289 | 289 | ------- |
|
290 | 290 | fname : str |
|
291 | 291 | The absolute path to the file where the object was defined. |
|
292 | 292 | """ |
|
293 | 293 | obj = _get_wrapped(obj) |
|
294 | 294 | |
|
295 | 295 | fname = None |
|
296 | 296 | try: |
|
297 | 297 | fname = inspect.getabsfile(obj) |
|
298 | 298 | except TypeError: |
|
299 | 299 | # For an instance, the file that matters is where its class was |
|
300 | 300 | # declared. |
|
301 | 301 | if hasattr(obj, '__class__'): |
|
302 | 302 | try: |
|
303 | 303 | fname = inspect.getabsfile(obj.__class__) |
|
304 | 304 | except TypeError: |
|
305 | 305 | # Can happen for builtins |
|
306 | 306 | pass |
|
307 | 307 | except: |
|
308 | 308 | pass |
|
309 | 309 | return cast_unicode(fname) |
|
310 | 310 | |
|
311 | 311 | |
|
312 | 312 | def find_source_lines(obj): |
|
313 | 313 | """Find the line number in a file where an object was defined. |
|
314 | 314 | |
|
315 | 315 | This is essentially a robust wrapper around `inspect.getsourcelines`. |
|
316 | 316 | |
|
317 | 317 | Returns None if no file can be found. |
|
318 | 318 | |
|
319 | 319 | Parameters |
|
320 | 320 | ---------- |
|
321 | 321 | obj : any Python object |
|
322 | 322 | |
|
323 | 323 | Returns |
|
324 | 324 | ------- |
|
325 | 325 | lineno : int |
|
326 | 326 | The line number where the object definition starts. |
|
327 | 327 | """ |
|
328 | 328 | obj = _get_wrapped(obj) |
|
329 | 329 | |
|
330 | 330 | try: |
|
331 | 331 | try: |
|
332 | 332 | lineno = inspect.getsourcelines(obj)[1] |
|
333 | 333 | except TypeError: |
|
334 | 334 | # For instances, try the class object like getsource() does |
|
335 | 335 | if hasattr(obj, '__class__'): |
|
336 | 336 | lineno = inspect.getsourcelines(obj.__class__)[1] |
|
337 | 337 | else: |
|
338 | 338 | lineno = None |
|
339 | 339 | except: |
|
340 | 340 | return None |
|
341 | 341 | |
|
342 | 342 | return lineno |
|
343 | 343 | |
|
344 | 344 | class Inspector(Colorable): |
|
345 | 345 | |
|
346 | 346 | def __init__(self, color_table=InspectColors, |
|
347 | 347 | code_color_table=PyColorize.ANSICodeColors, |
|
348 | 348 | scheme=None, |
|
349 | 349 | str_detail_level=0, |
|
350 | 350 | parent=None, config=None): |
|
351 | 351 | super(Inspector, self).__init__(parent=parent, config=config) |
|
352 | 352 | self.color_table = color_table |
|
353 | 353 | self.parser = PyColorize.Parser(out='str', parent=self, style=scheme) |
|
354 | 354 | self.format = self.parser.format |
|
355 | 355 | self.str_detail_level = str_detail_level |
|
356 | 356 | self.set_active_scheme(scheme) |
|
357 | 357 | |
|
358 | 358 | def _getdef(self,obj,oname=''): |
|
359 | 359 | """Return the call signature for any callable object. |
|
360 | 360 | |
|
361 | 361 | If any exception is generated, None is returned instead and the |
|
362 | 362 | exception is suppressed.""" |
|
363 | 363 | try: |
|
364 | 364 | hdef = oname + str(signature(obj)) |
|
365 | 365 | return cast_unicode(hdef) |
|
366 | 366 | except: |
|
367 | 367 | return None |
|
368 | 368 | |
|
369 | 369 | def __head(self,h): |
|
370 | 370 | """Return a header string with proper colors.""" |
|
371 | 371 | return '%s%s%s' % (self.color_table.active_colors.header,h, |
|
372 | 372 | self.color_table.active_colors.normal) |
|
373 | 373 | |
|
374 | 374 | def set_active_scheme(self, scheme): |
|
375 | 375 | if scheme is not None: |
|
376 | 376 | self.color_table.set_active_scheme(scheme) |
|
377 | 377 | self.parser.color_table.set_active_scheme(scheme) |
|
378 | 378 | |
|
379 | 379 | def noinfo(self, msg, oname): |
|
380 | 380 | """Generic message when no information is found.""" |
|
381 | 381 | print('No %s found' % msg, end=' ') |
|
382 | 382 | if oname: |
|
383 | 383 | print('for %s' % oname) |
|
384 | 384 | else: |
|
385 | 385 | print() |
|
386 | 386 | |
|
387 | 387 | def pdef(self, obj, oname=''): |
|
388 | 388 | """Print the call signature for any callable object. |
|
389 | 389 | |
|
390 | 390 | If the object is a class, print the constructor information.""" |
|
391 | 391 | |
|
392 | 392 | if not callable(obj): |
|
393 | 393 | print('Object is not callable.') |
|
394 | 394 | return |
|
395 | 395 | |
|
396 | 396 | header = '' |
|
397 | 397 | |
|
398 | 398 | if inspect.isclass(obj): |
|
399 | 399 | header = self.__head('Class constructor information:\n') |
|
400 | 400 | |
|
401 | 401 | |
|
402 | 402 | output = self._getdef(obj,oname) |
|
403 | 403 | if output is None: |
|
404 | 404 | self.noinfo('definition header',oname) |
|
405 | 405 | else: |
|
406 | 406 | print(header,self.format(output), end=' ') |
|
407 | 407 | |
|
408 | 408 | # In Python 3, all classes are new-style, so they all have __init__. |
|
409 | 409 | @skip_doctest |
|
410 | 410 | def pdoc(self, obj, oname='', formatter=None): |
|
411 | 411 | """Print the docstring for any object. |
|
412 | 412 | |
|
413 | 413 | Optional: |
|
414 | 414 | -formatter: a function to run the docstring through for specially |
|
415 | 415 | formatted docstrings. |
|
416 | 416 | |
|
417 | 417 | Examples |
|
418 | 418 | -------- |
|
419 | 419 | |
|
420 | 420 | In [1]: class NoInit: |
|
421 | 421 | ...: pass |
|
422 | 422 | |
|
423 | 423 | In [2]: class NoDoc: |
|
424 | 424 | ...: def __init__(self): |
|
425 | 425 | ...: pass |
|
426 | 426 | |
|
427 | 427 | In [3]: %pdoc NoDoc |
|
428 | 428 | No documentation found for NoDoc |
|
429 | 429 | |
|
430 | 430 | In [4]: %pdoc NoInit |
|
431 | 431 | No documentation found for NoInit |
|
432 | 432 | |
|
433 | 433 | In [5]: obj = NoInit() |
|
434 | 434 | |
|
435 | 435 | In [6]: %pdoc obj |
|
436 | 436 | No documentation found for obj |
|
437 | 437 | |
|
438 | 438 | In [5]: obj2 = NoDoc() |
|
439 | 439 | |
|
440 | 440 | In [6]: %pdoc obj2 |
|
441 | 441 | No documentation found for obj2 |
|
442 | 442 | """ |
|
443 | 443 | |
|
444 | 444 | head = self.__head # For convenience |
|
445 | 445 | lines = [] |
|
446 | 446 | ds = getdoc(obj) |
|
447 | 447 | if formatter: |
|
448 | 448 | ds = formatter(ds).get('plain/text', ds) |
|
449 | 449 | if ds: |
|
450 | 450 | lines.append(head("Class docstring:")) |
|
451 | 451 | lines.append(indent(ds)) |
|
452 | 452 | if inspect.isclass(obj) and hasattr(obj, '__init__'): |
|
453 | 453 | init_ds = getdoc(obj.__init__) |
|
454 | 454 | if init_ds is not None: |
|
455 | 455 | lines.append(head("Init docstring:")) |
|
456 | 456 | lines.append(indent(init_ds)) |
|
457 | 457 | elif hasattr(obj,'__call__'): |
|
458 | 458 | call_ds = getdoc(obj.__call__) |
|
459 | 459 | if call_ds: |
|
460 | 460 | lines.append(head("Call docstring:")) |
|
461 | 461 | lines.append(indent(call_ds)) |
|
462 | 462 | |
|
463 | 463 | if not lines: |
|
464 | 464 | self.noinfo('documentation',oname) |
|
465 | 465 | else: |
|
466 | 466 | page.page('\n'.join(lines)) |
|
467 | 467 | |
|
468 | 468 | def psource(self, obj, oname=''): |
|
469 | 469 | """Print the source code for an object.""" |
|
470 | 470 | |
|
471 | 471 | # Flush the source cache because inspect can return out-of-date source |
|
472 | 472 | linecache.checkcache() |
|
473 | 473 | try: |
|
474 | 474 | src = getsource(obj, oname=oname) |
|
475 | 475 | except Exception: |
|
476 | 476 | src = None |
|
477 | 477 | |
|
478 | 478 | if src is None: |
|
479 | 479 | self.noinfo('source', oname) |
|
480 | 480 | else: |
|
481 | 481 | page.page(self.format(src)) |
|
482 | 482 | |
|
483 | 483 | def pfile(self, obj, oname=''): |
|
484 | 484 | """Show the whole file where an object was defined.""" |
|
485 | 485 | |
|
486 | 486 | lineno = find_source_lines(obj) |
|
487 | 487 | if lineno is None: |
|
488 | 488 | self.noinfo('file', oname) |
|
489 | 489 | return |
|
490 | 490 | |
|
491 | 491 | ofile = find_file(obj) |
|
492 | 492 | # run contents of file through pager starting at line where the object |
|
493 | 493 | # is defined, as long as the file isn't binary and is actually on the |
|
494 | 494 | # filesystem. |
|
495 | 495 | if ofile.endswith(('.so', '.dll', '.pyd')): |
|
496 | 496 | print('File %r is binary, not printing.' % ofile) |
|
497 | 497 | elif not os.path.isfile(ofile): |
|
498 | 498 | print('File %r does not exist, not printing.' % ofile) |
|
499 | 499 | else: |
|
500 | 500 | # Print only text files, not extension binaries. Note that |
|
501 | 501 | # getsourcelines returns lineno with 1-offset and page() uses |
|
502 | 502 | # 0-offset, so we must adjust. |
|
503 | 503 | page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1) |
|
504 | 504 | |
|
505 | 505 | def _format_fields(self, fields, title_width=0): |
|
506 | 506 | """Formats a list of fields for display. |
|
507 | 507 | |
|
508 | 508 | Parameters |
|
509 | 509 | ---------- |
|
510 | 510 | fields : list |
|
511 | 511 | A list of 2-tuples: (field_title, field_content) |
|
512 | 512 | title_width : int |
|
513 | 513 | How many characters to pad titles to. Default to longest title. |
|
514 | 514 | """ |
|
515 | 515 | out = [] |
|
516 | 516 | header = self.__head |
|
517 | 517 | if title_width == 0: |
|
518 | 518 | title_width = max(len(title) + 2 for title, _ in fields) |
|
519 | 519 | for title, content in fields: |
|
520 | 520 | if len(content.splitlines()) > 1: |
|
521 | 521 | title = header(title + ':') + '\n' |
|
522 | 522 | else: |
|
523 | 523 | title = header((title + ':').ljust(title_width)) |
|
524 | 524 | out.append(cast_unicode(title) + cast_unicode(content)) |
|
525 | 525 | return "\n".join(out) |
|
526 | 526 | |
|
527 | 527 | def _mime_format(self, text, formatter=None): |
|
528 | 528 | """Return a mime bundle representation of the input text. |
|
529 | 529 | |
|
530 | 530 | - if `formatter` is None, the returned mime bundle has |
|
531 | 531 | a `text/plain` field, with the input text. |
|
532 | 532 | a `text/html` field with a `<pre>` tag containing the input text. |
|
533 | 533 | |
|
534 | 534 | - if `formatter` is not None, it must be a callable transforming the |
|
535 | 535 | input text into a mime bundle. Default values for `text/plain` and |
|
536 | 536 | `text/html` representations are the ones described above. |
|
537 | 537 | |
|
538 | 538 | Note: |
|
539 | 539 | |
|
540 | 540 | Formatters returning strings are supported but this behavior is deprecated. |
|
541 | 541 | |
|
542 | 542 | """ |
|
543 | 543 | text = cast_unicode(text) |
|
544 | 544 | defaults = { |
|
545 | 545 | 'text/plain': text, |
|
546 | 546 | 'text/html': '<pre>' + text + '</pre>' |
|
547 | 547 | } |
|
548 | 548 | |
|
549 | 549 | if formatter is None: |
|
550 | 550 | return defaults |
|
551 | 551 | else: |
|
552 | 552 | formatted = formatter(text) |
|
553 | 553 | |
|
554 | 554 | if not isinstance(formatted, dict): |
|
555 | 555 | # Handle the deprecated behavior of a formatter returning |
|
556 | 556 | # a string instead of a mime bundle. |
|
557 | 557 | return { |
|
558 | 558 | 'text/plain': formatted, |
|
559 | 559 | 'text/html': '<pre>' + formatted + '</pre>' |
|
560 | 560 | } |
|
561 | 561 | |
|
562 | 562 | else: |
|
563 | 563 | return dict(defaults, **formatted) |
|
564 | 564 | |
|
565 | 565 | |
|
566 | 566 | def format_mime(self, bundle): |
|
567 | 567 | |
|
568 | 568 | text_plain = bundle['text/plain'] |
|
569 | 569 | |
|
570 | 570 | text = '' |
|
571 | 571 | heads, bodies = list(zip(*text_plain)) |
|
572 | 572 | _len = max(len(h) for h in heads) |
|
573 | 573 | |
|
574 | 574 | for head, body in zip(heads, bodies): |
|
575 | 575 | body = body.strip('\n') |
|
576 | 576 | delim = '\n' if '\n' in body else ' ' |
|
577 | 577 | text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n' |
|
578 | 578 | |
|
579 | 579 | bundle['text/plain'] = text |
|
580 | 580 | return bundle |
|
581 | 581 | |
|
582 | 582 | def _get_info(self, obj, oname='', formatter=None, info=None, detail_level=0): |
|
583 | 583 | """Retrieve an info dict and format it. |
|
584 | 584 | |
|
585 | 585 | Parameters |
|
586 | 586 | ========== |
|
587 | 587 | |
|
588 | 588 | obj: any |
|
589 | 589 | Object to inspect and return info from |
|
590 | 590 | oname: str (default: ''): |
|
591 | 591 | Name of the variable pointing to `obj`. |
|
592 | 592 | formatter: callable |
|
593 | 593 | info: |
|
594 |
already computed information |
|
|
594 | already computed information | |
|
595 | 595 | detail_level: integer |
|
596 |
Granularity of detail level, if set to 1, give more information |
|
|
596 | Granularity of detail level, if set to 1, give more information. | |
|
597 | 597 | """ |
|
598 | 598 | |
|
599 | 599 | info = self._info(obj, oname=oname, info=info, detail_level=detail_level) |
|
600 | 600 | |
|
601 | 601 | _mime = { |
|
602 | 602 | 'text/plain': [], |
|
603 | 603 | 'text/html': '', |
|
604 | 604 | } |
|
605 | 605 | |
|
606 | 606 | def append_field(bundle, title, key, formatter=None): |
|
607 | 607 | field = info[key] |
|
608 | 608 | if field is not None: |
|
609 | 609 | formatted_field = self._mime_format(field, formatter) |
|
610 | 610 | bundle['text/plain'].append((title, formatted_field['text/plain'])) |
|
611 | 611 | bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n' |
|
612 | 612 | |
|
613 | 613 | def code_formatter(text): |
|
614 | 614 | return { |
|
615 | 615 | 'text/plain': self.format(text), |
|
616 | 616 | 'text/html': pylight(text) |
|
617 | 617 | } |
|
618 | 618 | |
|
619 | 619 | if info['isalias']: |
|
620 | 620 | append_field(_mime, 'Repr', 'string_form') |
|
621 | 621 | |
|
622 | 622 | elif info['ismagic']: |
|
623 | 623 | if detail_level > 0: |
|
624 | 624 | append_field(_mime, 'Source', 'source', code_formatter) |
|
625 | 625 | else: |
|
626 | 626 | append_field(_mime, 'Docstring', 'docstring', formatter) |
|
627 | 627 | append_field(_mime, 'File', 'file') |
|
628 | 628 | |
|
629 | 629 | elif info['isclass'] or is_simple_callable(obj): |
|
630 | 630 | # Functions, methods, classes |
|
631 | 631 | append_field(_mime, 'Signature', 'definition', code_formatter) |
|
632 | 632 | append_field(_mime, 'Init signature', 'init_definition', code_formatter) |
|
633 | 633 | if detail_level > 0 and info['source']: |
|
634 | 634 | append_field(_mime, 'Source', 'source', code_formatter) |
|
635 | 635 | else: |
|
636 | 636 | append_field(_mime, 'Docstring', 'docstring', formatter) |
|
637 | 637 | append_field(_mime, 'Init docstring', 'init_docstring', formatter) |
|
638 | 638 | |
|
639 | 639 | append_field(_mime, 'File', 'file') |
|
640 | 640 | append_field(_mime, 'Type', 'type_name') |
|
641 | 641 | |
|
642 | 642 | else: |
|
643 | 643 | # General Python objects |
|
644 | 644 | append_field(_mime, 'Signature', 'definition', code_formatter) |
|
645 | 645 | append_field(_mime, 'Call signature', 'call_def', code_formatter) |
|
646 | 646 | append_field(_mime, 'Type', 'type_name') |
|
647 | 647 | append_field(_mime, 'String form', 'string_form') |
|
648 | 648 | |
|
649 | 649 | # Namespace |
|
650 | 650 | if info['namespace'] != 'Interactive': |
|
651 | 651 | append_field(_mime, 'Namespace', 'namespace') |
|
652 | 652 | |
|
653 | 653 | append_field(_mime, 'Length', 'length') |
|
654 | 654 | append_field(_mime, 'File', 'file') |
|
655 | 655 | |
|
656 | 656 | # Source or docstring, depending on detail level and whether |
|
657 | 657 | # source found. |
|
658 | 658 | if detail_level > 0 and info['source']: |
|
659 | 659 | append_field(_mime, 'Source', 'source', code_formatter) |
|
660 | 660 | else: |
|
661 | 661 | append_field(_mime, 'Docstring', 'docstring', formatter) |
|
662 | 662 | |
|
663 | 663 | append_field(_mime, 'Class docstring', 'class_docstring', formatter) |
|
664 | 664 | append_field(_mime, 'Init docstring', 'init_docstring', formatter) |
|
665 | 665 | append_field(_mime, 'Call docstring', 'call_docstring', formatter) |
|
666 | 666 | |
|
667 | 667 | |
|
668 | 668 | return self.format_mime(_mime) |
|
669 | 669 | |
|
670 | 670 | def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True): |
|
671 | 671 | """Show detailed information about an object. |
|
672 | 672 | |
|
673 | 673 | Optional arguments: |
|
674 | 674 | |
|
675 | 675 | - oname: name of the variable pointing to the object. |
|
676 | 676 | |
|
677 | 677 | - formatter: callable (optional) |
|
678 | 678 | A special formatter for docstrings. |
|
679 | 679 | |
|
680 | 680 | The formatter is a callable that takes a string as an input |
|
681 | 681 | and returns either a formatted string or a mime type bundle |
|
682 |
in the form of a diction |
|
|
682 | in the form of a dictionary. | |
|
683 | 683 | |
|
684 | 684 | Although the support of custom formatter returning a string |
|
685 | 685 | instead of a mime type bundle is deprecated. |
|
686 | 686 | |
|
687 | 687 | - info: a structure with some information fields which may have been |
|
688 | 688 | precomputed already. |
|
689 | 689 | |
|
690 | 690 | - detail_level: if set to 1, more information is given. |
|
691 | 691 | """ |
|
692 | 692 | info = self._get_info(obj, oname, formatter, info, detail_level) |
|
693 | 693 | if not enable_html_pager: |
|
694 | 694 | del info['text/html'] |
|
695 | 695 | page.page(info) |
|
696 | 696 | |
|
697 | 697 | def info(self, obj, oname='', formatter=None, info=None, detail_level=0): |
|
698 | 698 | """DEPRECATED. Compute a dict with detailed information about an object. |
|
699 | 699 | """ |
|
700 | 700 | if formatter is not None: |
|
701 | 701 | warnings.warn('The `formatter` keyword argument to `Inspector.info`' |
|
702 | 702 | 'is deprecated as of IPython 5.0 and will have no effects.', |
|
703 | 703 | DeprecationWarning, stacklevel=2) |
|
704 | 704 | return self._info(obj, oname=oname, info=info, detail_level=detail_level) |
|
705 | 705 | |
|
706 | 706 | def _info(self, obj, oname='', info=None, detail_level=0) -> dict: |
|
707 | 707 | """Compute a dict with detailed information about an object. |
|
708 | 708 | |
|
709 | 709 | Parameters |
|
710 | 710 | ========== |
|
711 | 711 | |
|
712 | 712 | obj: any |
|
713 | 713 | An object to find information about |
|
714 | 714 | oname: str (default: ''): |
|
715 | 715 | Name of the variable pointing to `obj`. |
|
716 | 716 | info: (default: None) |
|
717 | 717 | A struct (dict like with attr access) with some information fields |
|
718 | 718 | which may have been precomputed already. |
|
719 | 719 | detail_level: int (default:0) |
|
720 | 720 | If set to 1, more information is given. |
|
721 | 721 | |
|
722 | 722 | Returns |
|
723 | 723 | ======= |
|
724 | 724 | |
|
725 | 725 | An object info dict with known fields from `info_fields`. |
|
726 | 726 | """ |
|
727 | 727 | |
|
728 | 728 | if info is None: |
|
729 | 729 | ismagic = False |
|
730 | 730 | isalias = False |
|
731 | 731 | ospace = '' |
|
732 | 732 | else: |
|
733 | 733 | ismagic = info.ismagic |
|
734 | 734 | isalias = info.isalias |
|
735 | 735 | ospace = info.namespace |
|
736 | 736 | |
|
737 | 737 | # Get docstring, special-casing aliases: |
|
738 | 738 | if isalias: |
|
739 | 739 | if not callable(obj): |
|
740 | 740 | try: |
|
741 | 741 | ds = "Alias to the system command:\n %s" % obj[1] |
|
742 | 742 | except: |
|
743 | 743 | ds = "Alias: " + str(obj) |
|
744 | 744 | else: |
|
745 | 745 | ds = "Alias to " + str(obj) |
|
746 | 746 | if obj.__doc__: |
|
747 | 747 | ds += "\nDocstring:\n" + obj.__doc__ |
|
748 | 748 | else: |
|
749 | 749 | ds = getdoc(obj) |
|
750 | 750 | if ds is None: |
|
751 | 751 | ds = '<no docstring>' |
|
752 | 752 | |
|
753 | 753 | # store output in a dict, we initialize it here and fill it as we go |
|
754 | 754 | out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic) |
|
755 | 755 | |
|
756 | 756 | string_max = 200 # max size of strings to show (snipped if longer) |
|
757 | 757 | shalf = int((string_max - 5) / 2) |
|
758 | 758 | |
|
759 | 759 | if ismagic: |
|
760 | 760 | out['type_name'] = 'Magic function' |
|
761 | 761 | elif isalias: |
|
762 | 762 | out['type_name'] = 'System alias' |
|
763 | 763 | else: |
|
764 | 764 | out['type_name'] = type(obj).__name__ |
|
765 | 765 | |
|
766 | 766 | try: |
|
767 | 767 | bclass = obj.__class__ |
|
768 | 768 | out['base_class'] = str(bclass) |
|
769 | 769 | except: |
|
770 | 770 | pass |
|
771 | 771 | |
|
772 | 772 | # String form, but snip if too long in ? form (full in ??) |
|
773 | 773 | if detail_level >= self.str_detail_level: |
|
774 | 774 | try: |
|
775 | 775 | ostr = str(obj) |
|
776 | 776 | str_head = 'string_form' |
|
777 | 777 | if not detail_level and len(ostr)>string_max: |
|
778 | 778 | ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] |
|
779 | 779 | ostr = ("\n" + " " * len(str_head.expandtabs())).\ |
|
780 | 780 | join(q.strip() for q in ostr.split("\n")) |
|
781 | 781 | out[str_head] = ostr |
|
782 | 782 | except: |
|
783 | 783 | pass |
|
784 | 784 | |
|
785 | 785 | if ospace: |
|
786 | 786 | out['namespace'] = ospace |
|
787 | 787 | |
|
788 | 788 | # Length (for strings and lists) |
|
789 | 789 | try: |
|
790 | 790 | out['length'] = str(len(obj)) |
|
791 | 791 | except Exception: |
|
792 | 792 | pass |
|
793 | 793 | |
|
794 | 794 | # Filename where object was defined |
|
795 | 795 | binary_file = False |
|
796 | 796 | fname = find_file(obj) |
|
797 | 797 | if fname is None: |
|
798 | 798 | # if anything goes wrong, we don't want to show source, so it's as |
|
799 | 799 | # if the file was binary |
|
800 | 800 | binary_file = True |
|
801 | 801 | else: |
|
802 | 802 | if fname.endswith(('.so', '.dll', '.pyd')): |
|
803 | 803 | binary_file = True |
|
804 | 804 | elif fname.endswith('<string>'): |
|
805 | 805 | fname = 'Dynamically generated function. No source code available.' |
|
806 | 806 | out['file'] = compress_user(fname) |
|
807 | 807 | |
|
808 | 808 | # Original source code for a callable, class or property. |
|
809 | 809 | if detail_level: |
|
810 | 810 | # Flush the source cache because inspect can return out-of-date |
|
811 | 811 | # source |
|
812 | 812 | linecache.checkcache() |
|
813 | 813 | try: |
|
814 | 814 | if isinstance(obj, property) or not binary_file: |
|
815 | 815 | src = getsource(obj, oname) |
|
816 | 816 | if src is not None: |
|
817 | 817 | src = src.rstrip() |
|
818 | 818 | out['source'] = src |
|
819 | 819 | |
|
820 | 820 | except Exception: |
|
821 | 821 | pass |
|
822 | 822 | |
|
823 | 823 | # Add docstring only if no source is to be shown (avoid repetitions). |
|
824 | 824 | if ds and out.get('source', None) is None: |
|
825 | 825 | out['docstring'] = ds |
|
826 | 826 | |
|
827 | 827 | # Constructor docstring for classes |
|
828 | 828 | if inspect.isclass(obj): |
|
829 | 829 | out['isclass'] = True |
|
830 | 830 | |
|
831 | 831 | # get the init signature: |
|
832 | 832 | try: |
|
833 | 833 | init_def = self._getdef(obj, oname) |
|
834 | 834 | except AttributeError: |
|
835 | 835 | init_def = None |
|
836 | 836 | |
|
837 | 837 | # get the __init__ docstring |
|
838 | 838 | try: |
|
839 | 839 | obj_init = obj.__init__ |
|
840 | 840 | except AttributeError: |
|
841 | 841 | init_ds = None |
|
842 | 842 | else: |
|
843 | 843 | if init_def is None: |
|
844 | 844 | # Get signature from init if top-level sig failed. |
|
845 | 845 | # Can happen for built-in types (list, etc.). |
|
846 | 846 | try: |
|
847 | 847 | init_def = self._getdef(obj_init, oname) |
|
848 | 848 | except AttributeError: |
|
849 | 849 | pass |
|
850 | 850 | init_ds = getdoc(obj_init) |
|
851 | 851 | # Skip Python's auto-generated docstrings |
|
852 | 852 | if init_ds == _object_init_docstring: |
|
853 | 853 | init_ds = None |
|
854 | 854 | |
|
855 | 855 | if init_def: |
|
856 | 856 | out['init_definition'] = init_def |
|
857 | 857 | |
|
858 | 858 | if init_ds: |
|
859 | 859 | out['init_docstring'] = init_ds |
|
860 | 860 | |
|
861 | 861 | # and class docstring for instances: |
|
862 | 862 | else: |
|
863 | 863 | # reconstruct the function definition and print it: |
|
864 | 864 | defln = self._getdef(obj, oname) |
|
865 | 865 | if defln: |
|
866 | 866 | out['definition'] = defln |
|
867 | 867 | |
|
868 | 868 | # First, check whether the instance docstring is identical to the |
|
869 | 869 | # class one, and print it separately if they don't coincide. In |
|
870 | 870 | # most cases they will, but it's nice to print all the info for |
|
871 | 871 | # objects which use instance-customized docstrings. |
|
872 | 872 | if ds: |
|
873 | 873 | try: |
|
874 | 874 | cls = getattr(obj,'__class__') |
|
875 | 875 | except: |
|
876 | 876 | class_ds = None |
|
877 | 877 | else: |
|
878 | 878 | class_ds = getdoc(cls) |
|
879 | 879 | # Skip Python's auto-generated docstrings |
|
880 | 880 | if class_ds in _builtin_type_docstrings: |
|
881 | 881 | class_ds = None |
|
882 | 882 | if class_ds and ds != class_ds: |
|
883 | 883 | out['class_docstring'] = class_ds |
|
884 | 884 | |
|
885 | 885 | # Next, try to show constructor docstrings |
|
886 | 886 | try: |
|
887 | 887 | init_ds = getdoc(obj.__init__) |
|
888 | 888 | # Skip Python's auto-generated docstrings |
|
889 | 889 | if init_ds == _object_init_docstring: |
|
890 | 890 | init_ds = None |
|
891 | 891 | except AttributeError: |
|
892 | 892 | init_ds = None |
|
893 | 893 | if init_ds: |
|
894 | 894 | out['init_docstring'] = init_ds |
|
895 | 895 | |
|
896 | 896 | # Call form docstring for callable instances |
|
897 | 897 | if safe_hasattr(obj, '__call__') and not is_simple_callable(obj): |
|
898 | 898 | call_def = self._getdef(obj.__call__, oname) |
|
899 | 899 | if call_def and (call_def != out.get('definition')): |
|
900 | 900 | # it may never be the case that call def and definition differ, |
|
901 | 901 | # but don't include the same signature twice |
|
902 | 902 | out['call_def'] = call_def |
|
903 | 903 | call_ds = getdoc(obj.__call__) |
|
904 | 904 | # Skip Python's auto-generated docstrings |
|
905 | 905 | if call_ds == _func_call_docstring: |
|
906 | 906 | call_ds = None |
|
907 | 907 | if call_ds: |
|
908 | 908 | out['call_docstring'] = call_ds |
|
909 | 909 | |
|
910 | 910 | # Compute the object's argspec as a callable. The key is to decide |
|
911 | 911 | # whether to pull it from the object itself, from its __init__ or |
|
912 | 912 | # from its __call__ method. |
|
913 | 913 | |
|
914 | 914 | if inspect.isclass(obj): |
|
915 | 915 | # Old-style classes need not have an __init__ |
|
916 | 916 | callable_obj = getattr(obj, "__init__", None) |
|
917 | 917 | elif callable(obj): |
|
918 | 918 | callable_obj = obj |
|
919 | 919 | else: |
|
920 | 920 | callable_obj = None |
|
921 | 921 | |
|
922 | 922 | if callable_obj is not None: |
|
923 | 923 | try: |
|
924 | 924 | argspec = getargspec(callable_obj) |
|
925 | 925 | except Exception: |
|
926 | 926 | # For extensions/builtins we can't retrieve the argspec |
|
927 | 927 | pass |
|
928 | 928 | else: |
|
929 | 929 | # named tuples' _asdict() method returns an OrderedDict, but we |
|
930 | 930 | # we want a normal |
|
931 | 931 | out['argspec'] = argspec_dict = dict(argspec._asdict()) |
|
932 | 932 | # We called this varkw before argspec became a named tuple. |
|
933 | 933 | # With getfullargspec it's also called varkw. |
|
934 | 934 | if 'varkw' not in argspec_dict: |
|
935 | 935 | argspec_dict['varkw'] = argspec_dict.pop('keywords') |
|
936 | 936 | |
|
937 | 937 | return object_info(**out) |
|
938 | 938 | |
|
939 | 939 | def psearch(self,pattern,ns_table,ns_search=[], |
|
940 | 940 | ignore_case=False,show_all=False): |
|
941 | 941 | """Search namespaces with wildcards for objects. |
|
942 | 942 | |
|
943 | 943 | Arguments: |
|
944 | 944 | |
|
945 | 945 | - pattern: string containing shell-like wildcards to use in namespace |
|
946 | 946 | searches and optionally a type specification to narrow the search to |
|
947 | 947 | objects of that type. |
|
948 | 948 | |
|
949 | 949 | - ns_table: dict of name->namespaces for search. |
|
950 | 950 | |
|
951 | 951 | Optional arguments: |
|
952 | 952 | |
|
953 | 953 | - ns_search: list of namespace names to include in search. |
|
954 | 954 | |
|
955 | 955 | - ignore_case(False): make the search case-insensitive. |
|
956 | 956 | |
|
957 | 957 | - show_all(False): show all names, including those starting with |
|
958 | 958 | underscores. |
|
959 | 959 | """ |
|
960 | 960 | #print 'ps pattern:<%r>' % pattern # dbg |
|
961 | 961 | |
|
962 | 962 | # defaults |
|
963 | 963 | type_pattern = 'all' |
|
964 | 964 | filter = '' |
|
965 | 965 | |
|
966 | 966 | cmds = pattern.split() |
|
967 | 967 | len_cmds = len(cmds) |
|
968 | 968 | if len_cmds == 1: |
|
969 | 969 | # Only filter pattern given |
|
970 | 970 | filter = cmds[0] |
|
971 | 971 | elif len_cmds == 2: |
|
972 | 972 | # Both filter and type specified |
|
973 | 973 | filter,type_pattern = cmds |
|
974 | 974 | else: |
|
975 | 975 | raise ValueError('invalid argument string for psearch: <%s>' % |
|
976 | 976 | pattern) |
|
977 | 977 | |
|
978 | 978 | # filter search namespaces |
|
979 | 979 | for name in ns_search: |
|
980 | 980 | if name not in ns_table: |
|
981 | 981 | raise ValueError('invalid namespace <%s>. Valid names: %s' % |
|
982 | 982 | (name,ns_table.keys())) |
|
983 | 983 | |
|
984 | 984 | #print 'type_pattern:',type_pattern # dbg |
|
985 | 985 | search_result, namespaces_seen = set(), set() |
|
986 | 986 | for ns_name in ns_search: |
|
987 | 987 | ns = ns_table[ns_name] |
|
988 | 988 | # Normally, locals and globals are the same, so we just check one. |
|
989 | 989 | if id(ns) in namespaces_seen: |
|
990 | 990 | continue |
|
991 | 991 | namespaces_seen.add(id(ns)) |
|
992 | 992 | tmp_res = list_namespace(ns, type_pattern, filter, |
|
993 | 993 | ignore_case=ignore_case, show_all=show_all) |
|
994 | 994 | search_result.update(tmp_res) |
|
995 | 995 | |
|
996 | 996 | page.page('\n'.join(sorted(search_result))) |
@@ -1,709 +1,709 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Prefiltering components. |
|
4 | 4 | |
|
5 | 5 | Prefilters transform user input before it is exec'd by Python. These |
|
6 | 6 | transforms are used to implement additional syntax such as !ls and %magic. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | # Copyright (c) IPython Development Team. |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | |
|
12 | 12 | from keyword import iskeyword |
|
13 | 13 | import re |
|
14 | 14 | |
|
15 | 15 | from IPython.core.autocall import IPyAutocall |
|
16 | 16 | from traitlets.config.configurable import Configurable |
|
17 | 17 | from IPython.core.inputsplitter import ( |
|
18 | 18 | ESC_MAGIC, |
|
19 | 19 | ESC_QUOTE, |
|
20 | 20 | ESC_QUOTE2, |
|
21 | 21 | ESC_PAREN, |
|
22 | 22 | ) |
|
23 | 23 | from IPython.core.macro import Macro |
|
24 | 24 | from IPython.core.splitinput import LineInfo |
|
25 | 25 | |
|
26 | 26 | from traitlets import ( |
|
27 | 27 | List, Integer, Unicode, Bool, Instance, CRegExp |
|
28 | 28 | ) |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | # Global utilities, errors and constants |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class PrefilterError(Exception): |
|
36 | 36 | pass |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | # RegExp to identify potential function names |
|
40 | 40 | re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
41 | 41 | |
|
42 | 42 | # RegExp to exclude strings with this start from autocalling. In |
|
43 | 43 | # particular, all binary operators should be excluded, so that if foo is |
|
44 | 44 | # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The |
|
45 | 45 | # characters '!=()' don't need to be checked for, as the checkPythonChars |
|
46 |
# routine explicit |
|
|
46 | # routine explicitly does so, to catch direct calls and rebindings of | |
|
47 | 47 | # existing names. |
|
48 | 48 | |
|
49 | 49 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
50 | 50 | # it affects the rest of the group in square brackets. |
|
51 | 51 | re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]' |
|
52 | 52 | r'|^is |^not |^in |^and |^or ') |
|
53 | 53 | |
|
54 | 54 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
55 | 55 | # (experimental). For this to work, the line_split regexp would need |
|
56 | 56 | # to be modified so it wouldn't break things at '['. That line is |
|
57 | 57 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
58 | 58 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | # Handler Check Utilities |
|
62 | 62 | def is_shadowed(identifier, ip): |
|
63 | 63 | """Is the given identifier defined in one of the namespaces which shadow |
|
64 | 64 | the alias and magic namespaces? Note that an identifier is different |
|
65 | 65 | than ifun, because it can not contain a '.' character.""" |
|
66 | 66 | # This is much safer than calling ofind, which can change state |
|
67 | 67 | return (identifier in ip.user_ns \ |
|
68 | 68 | or identifier in ip.user_global_ns \ |
|
69 | 69 | or identifier in ip.ns_table['builtin']\ |
|
70 | 70 | or iskeyword(identifier)) |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | #----------------------------------------------------------------------------- |
|
74 | 74 | # Main Prefilter manager |
|
75 | 75 | #----------------------------------------------------------------------------- |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | class PrefilterManager(Configurable): |
|
79 | 79 | """Main prefilter component. |
|
80 | 80 | |
|
81 | 81 | The IPython prefilter is run on all user input before it is run. The |
|
82 | 82 | prefilter consumes lines of input and produces transformed lines of |
|
83 | 83 | input. |
|
84 | 84 | |
|
85 | 85 | The iplementation consists of two phases: |
|
86 | 86 | |
|
87 | 87 | 1. Transformers |
|
88 | 88 | 2. Checkers and handlers |
|
89 | 89 | |
|
90 | 90 | Over time, we plan on deprecating the checkers and handlers and doing |
|
91 | 91 | everything in the transformers. |
|
92 | 92 | |
|
93 | 93 | The transformers are instances of :class:`PrefilterTransformer` and have |
|
94 | 94 | a single method :meth:`transform` that takes a line and returns a |
|
95 | 95 | transformed line. The transformation can be accomplished using any |
|
96 | 96 | tool, but our current ones use regular expressions for speed. |
|
97 | 97 | |
|
98 | 98 | After all the transformers have been run, the line is fed to the checkers, |
|
99 | 99 | which are instances of :class:`PrefilterChecker`. The line is passed to |
|
100 | 100 | the :meth:`check` method, which either returns `None` or a |
|
101 | 101 | :class:`PrefilterHandler` instance. If `None` is returned, the other |
|
102 | 102 | checkers are tried. If an :class:`PrefilterHandler` instance is returned, |
|
103 | 103 | the line is passed to the :meth:`handle` method of the returned |
|
104 | 104 | handler and no further checkers are tried. |
|
105 | 105 | |
|
106 | 106 | Both transformers and checkers have a `priority` attribute, that determines |
|
107 | 107 | the order in which they are called. Smaller priorities are tried first. |
|
108 | 108 | |
|
109 | 109 | Both transformers and checkers also have `enabled` attribute, which is |
|
110 | 110 | a boolean that determines if the instance is used. |
|
111 | 111 | |
|
112 | 112 | Users or developers can change the priority or enabled attribute of |
|
113 | 113 | transformers or checkers, but they must call the :meth:`sort_checkers` |
|
114 | 114 | or :meth:`sort_transformers` method after changing the priority. |
|
115 | 115 | """ |
|
116 | 116 | |
|
117 | 117 | multi_line_specials = Bool(True).tag(config=True) |
|
118 | 118 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
119 | 119 | |
|
120 | 120 | def __init__(self, shell=None, **kwargs): |
|
121 | 121 | super(PrefilterManager, self).__init__(shell=shell, **kwargs) |
|
122 | 122 | self.shell = shell |
|
123 | 123 | self.init_transformers() |
|
124 | 124 | self.init_handlers() |
|
125 | 125 | self.init_checkers() |
|
126 | 126 | |
|
127 | 127 | #------------------------------------------------------------------------- |
|
128 | 128 | # API for managing transformers |
|
129 | 129 | #------------------------------------------------------------------------- |
|
130 | 130 | |
|
131 | 131 | def init_transformers(self): |
|
132 | 132 | """Create the default transformers.""" |
|
133 | 133 | self._transformers = [] |
|
134 | 134 | for transformer_cls in _default_transformers: |
|
135 | 135 | transformer_cls( |
|
136 | 136 | shell=self.shell, prefilter_manager=self, parent=self |
|
137 | 137 | ) |
|
138 | 138 | |
|
139 | 139 | def sort_transformers(self): |
|
140 | 140 | """Sort the transformers by priority. |
|
141 | 141 | |
|
142 | 142 | This must be called after the priority of a transformer is changed. |
|
143 | 143 | The :meth:`register_transformer` method calls this automatically. |
|
144 | 144 | """ |
|
145 | 145 | self._transformers.sort(key=lambda x: x.priority) |
|
146 | 146 | |
|
147 | 147 | @property |
|
148 | 148 | def transformers(self): |
|
149 | 149 | """Return a list of checkers, sorted by priority.""" |
|
150 | 150 | return self._transformers |
|
151 | 151 | |
|
152 | 152 | def register_transformer(self, transformer): |
|
153 | 153 | """Register a transformer instance.""" |
|
154 | 154 | if transformer not in self._transformers: |
|
155 | 155 | self._transformers.append(transformer) |
|
156 | 156 | self.sort_transformers() |
|
157 | 157 | |
|
158 | 158 | def unregister_transformer(self, transformer): |
|
159 | 159 | """Unregister a transformer instance.""" |
|
160 | 160 | if transformer in self._transformers: |
|
161 | 161 | self._transformers.remove(transformer) |
|
162 | 162 | |
|
163 | 163 | #------------------------------------------------------------------------- |
|
164 | 164 | # API for managing checkers |
|
165 | 165 | #------------------------------------------------------------------------- |
|
166 | 166 | |
|
167 | 167 | def init_checkers(self): |
|
168 | 168 | """Create the default checkers.""" |
|
169 | 169 | self._checkers = [] |
|
170 | 170 | for checker in _default_checkers: |
|
171 | 171 | checker( |
|
172 | 172 | shell=self.shell, prefilter_manager=self, parent=self |
|
173 | 173 | ) |
|
174 | 174 | |
|
175 | 175 | def sort_checkers(self): |
|
176 | 176 | """Sort the checkers by priority. |
|
177 | 177 | |
|
178 | 178 | This must be called after the priority of a checker is changed. |
|
179 | 179 | The :meth:`register_checker` method calls this automatically. |
|
180 | 180 | """ |
|
181 | 181 | self._checkers.sort(key=lambda x: x.priority) |
|
182 | 182 | |
|
183 | 183 | @property |
|
184 | 184 | def checkers(self): |
|
185 | 185 | """Return a list of checkers, sorted by priority.""" |
|
186 | 186 | return self._checkers |
|
187 | 187 | |
|
188 | 188 | def register_checker(self, checker): |
|
189 | 189 | """Register a checker instance.""" |
|
190 | 190 | if checker not in self._checkers: |
|
191 | 191 | self._checkers.append(checker) |
|
192 | 192 | self.sort_checkers() |
|
193 | 193 | |
|
194 | 194 | def unregister_checker(self, checker): |
|
195 | 195 | """Unregister a checker instance.""" |
|
196 | 196 | if checker in self._checkers: |
|
197 | 197 | self._checkers.remove(checker) |
|
198 | 198 | |
|
199 | 199 | #------------------------------------------------------------------------- |
|
200 | 200 | # API for managing handlers |
|
201 | 201 | #------------------------------------------------------------------------- |
|
202 | 202 | |
|
203 | 203 | def init_handlers(self): |
|
204 | 204 | """Create the default handlers.""" |
|
205 | 205 | self._handlers = {} |
|
206 | 206 | self._esc_handlers = {} |
|
207 | 207 | for handler in _default_handlers: |
|
208 | 208 | handler( |
|
209 | 209 | shell=self.shell, prefilter_manager=self, parent=self |
|
210 | 210 | ) |
|
211 | 211 | |
|
212 | 212 | @property |
|
213 | 213 | def handlers(self): |
|
214 | 214 | """Return a dict of all the handlers.""" |
|
215 | 215 | return self._handlers |
|
216 | 216 | |
|
217 | 217 | def register_handler(self, name, handler, esc_strings): |
|
218 | 218 | """Register a handler instance by name with esc_strings.""" |
|
219 | 219 | self._handlers[name] = handler |
|
220 | 220 | for esc_str in esc_strings: |
|
221 | 221 | self._esc_handlers[esc_str] = handler |
|
222 | 222 | |
|
223 | 223 | def unregister_handler(self, name, handler, esc_strings): |
|
224 | 224 | """Unregister a handler instance by name with esc_strings.""" |
|
225 | 225 | try: |
|
226 | 226 | del self._handlers[name] |
|
227 | 227 | except KeyError: |
|
228 | 228 | pass |
|
229 | 229 | for esc_str in esc_strings: |
|
230 | 230 | h = self._esc_handlers.get(esc_str) |
|
231 | 231 | if h is handler: |
|
232 | 232 | del self._esc_handlers[esc_str] |
|
233 | 233 | |
|
234 | 234 | def get_handler_by_name(self, name): |
|
235 | 235 | """Get a handler by its name.""" |
|
236 | 236 | return self._handlers.get(name) |
|
237 | 237 | |
|
238 | 238 | def get_handler_by_esc(self, esc_str): |
|
239 | 239 | """Get a handler by its escape string.""" |
|
240 | 240 | return self._esc_handlers.get(esc_str) |
|
241 | 241 | |
|
242 | 242 | #------------------------------------------------------------------------- |
|
243 | 243 | # Main prefiltering API |
|
244 | 244 | #------------------------------------------------------------------------- |
|
245 | 245 | |
|
246 | 246 | def prefilter_line_info(self, line_info): |
|
247 | 247 | """Prefilter a line that has been converted to a LineInfo object. |
|
248 | 248 | |
|
249 | 249 | This implements the checker/handler part of the prefilter pipe. |
|
250 | 250 | """ |
|
251 | 251 | # print "prefilter_line_info: ", line_info |
|
252 | 252 | handler = self.find_handler(line_info) |
|
253 | 253 | return handler.handle(line_info) |
|
254 | 254 | |
|
255 | 255 | def find_handler(self, line_info): |
|
256 | 256 | """Find a handler for the line_info by trying checkers.""" |
|
257 | 257 | for checker in self.checkers: |
|
258 | 258 | if checker.enabled: |
|
259 | 259 | handler = checker.check(line_info) |
|
260 | 260 | if handler: |
|
261 | 261 | return handler |
|
262 | 262 | return self.get_handler_by_name('normal') |
|
263 | 263 | |
|
264 | 264 | def transform_line(self, line, continue_prompt): |
|
265 | 265 | """Calls the enabled transformers in order of increasing priority.""" |
|
266 | 266 | for transformer in self.transformers: |
|
267 | 267 | if transformer.enabled: |
|
268 | 268 | line = transformer.transform(line, continue_prompt) |
|
269 | 269 | return line |
|
270 | 270 | |
|
271 | 271 | def prefilter_line(self, line, continue_prompt=False): |
|
272 | 272 | """Prefilter a single input line as text. |
|
273 | 273 | |
|
274 | 274 | This method prefilters a single line of text by calling the |
|
275 | 275 | transformers and then the checkers/handlers. |
|
276 | 276 | """ |
|
277 | 277 | |
|
278 | 278 | # print "prefilter_line: ", line, continue_prompt |
|
279 | 279 | # All handlers *must* return a value, even if it's blank (''). |
|
280 | 280 | |
|
281 | 281 | # save the line away in case we crash, so the post-mortem handler can |
|
282 | 282 | # record it |
|
283 | 283 | self.shell._last_input_line = line |
|
284 | 284 | |
|
285 | 285 | if not line: |
|
286 | 286 | # Return immediately on purely empty lines, so that if the user |
|
287 | 287 | # previously typed some whitespace that started a continuation |
|
288 | 288 | # prompt, he can break out of that loop with just an empty line. |
|
289 | 289 | # This is how the default python prompt works. |
|
290 | 290 | return '' |
|
291 | 291 | |
|
292 | 292 | # At this point, we invoke our transformers. |
|
293 | 293 | if not continue_prompt or (continue_prompt and self.multi_line_specials): |
|
294 | 294 | line = self.transform_line(line, continue_prompt) |
|
295 | 295 | |
|
296 | 296 | # Now we compute line_info for the checkers and handlers |
|
297 | 297 | line_info = LineInfo(line, continue_prompt) |
|
298 | 298 | |
|
299 | 299 | # the input history needs to track even empty lines |
|
300 | 300 | stripped = line.strip() |
|
301 | 301 | |
|
302 | 302 | normal_handler = self.get_handler_by_name('normal') |
|
303 | 303 | if not stripped: |
|
304 | 304 | return normal_handler.handle(line_info) |
|
305 | 305 | |
|
306 | 306 | # special handlers are only allowed for single line statements |
|
307 | 307 | if continue_prompt and not self.multi_line_specials: |
|
308 | 308 | return normal_handler.handle(line_info) |
|
309 | 309 | |
|
310 | 310 | prefiltered = self.prefilter_line_info(line_info) |
|
311 | 311 | # print "prefiltered line: %r" % prefiltered |
|
312 | 312 | return prefiltered |
|
313 | 313 | |
|
314 | 314 | def prefilter_lines(self, lines, continue_prompt=False): |
|
315 | 315 | """Prefilter multiple input lines of text. |
|
316 | 316 | |
|
317 | 317 | This is the main entry point for prefiltering multiple lines of |
|
318 | 318 | input. This simply calls :meth:`prefilter_line` for each line of |
|
319 | 319 | input. |
|
320 | 320 | |
|
321 | 321 | This covers cases where there are multiple lines in the user entry, |
|
322 | 322 | which is the case when the user goes back to a multiline history |
|
323 | 323 | entry and presses enter. |
|
324 | 324 | """ |
|
325 | 325 | llines = lines.rstrip('\n').split('\n') |
|
326 | 326 | # We can get multiple lines in one shot, where multiline input 'blends' |
|
327 | 327 | # into one line, in cases like recalling from the readline history |
|
328 | 328 | # buffer. We need to make sure that in such cases, we correctly |
|
329 | 329 | # communicate downstream which line is first and which are continuation |
|
330 | 330 | # ones. |
|
331 | 331 | if len(llines) > 1: |
|
332 | 332 | out = '\n'.join([self.prefilter_line(line, lnum>0) |
|
333 | 333 | for lnum, line in enumerate(llines) ]) |
|
334 | 334 | else: |
|
335 | 335 | out = self.prefilter_line(llines[0], continue_prompt) |
|
336 | 336 | |
|
337 | 337 | return out |
|
338 | 338 | |
|
339 | 339 | #----------------------------------------------------------------------------- |
|
340 | 340 | # Prefilter transformers |
|
341 | 341 | #----------------------------------------------------------------------------- |
|
342 | 342 | |
|
343 | 343 | |
|
344 | 344 | class PrefilterTransformer(Configurable): |
|
345 | 345 | """Transform a line of user input.""" |
|
346 | 346 | |
|
347 | 347 | priority = Integer(100).tag(config=True) |
|
348 | 348 | # Transformers don't currently use shell or prefilter_manager, but as we |
|
349 | 349 | # move away from checkers and handlers, they will need them. |
|
350 | 350 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
351 | 351 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
352 | 352 | enabled = Bool(True).tag(config=True) |
|
353 | 353 | |
|
354 | 354 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
355 | 355 | super(PrefilterTransformer, self).__init__( |
|
356 | 356 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
357 | 357 | ) |
|
358 | 358 | self.prefilter_manager.register_transformer(self) |
|
359 | 359 | |
|
360 | 360 | def transform(self, line, continue_prompt): |
|
361 | 361 | """Transform a line, returning the new one.""" |
|
362 | 362 | return None |
|
363 | 363 | |
|
364 | 364 | def __repr__(self): |
|
365 | 365 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
366 | 366 | self.__class__.__name__, self.priority, self.enabled) |
|
367 | 367 | |
|
368 | 368 | |
|
369 | 369 | #----------------------------------------------------------------------------- |
|
370 | 370 | # Prefilter checkers |
|
371 | 371 | #----------------------------------------------------------------------------- |
|
372 | 372 | |
|
373 | 373 | |
|
374 | 374 | class PrefilterChecker(Configurable): |
|
375 | 375 | """Inspect an input line and return a handler for that line.""" |
|
376 | 376 | |
|
377 | 377 | priority = Integer(100).tag(config=True) |
|
378 | 378 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
379 | 379 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
380 | 380 | enabled = Bool(True).tag(config=True) |
|
381 | 381 | |
|
382 | 382 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
383 | 383 | super(PrefilterChecker, self).__init__( |
|
384 | 384 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
385 | 385 | ) |
|
386 | 386 | self.prefilter_manager.register_checker(self) |
|
387 | 387 | |
|
388 | 388 | def check(self, line_info): |
|
389 | 389 | """Inspect line_info and return a handler instance or None.""" |
|
390 | 390 | return None |
|
391 | 391 | |
|
392 | 392 | def __repr__(self): |
|
393 | 393 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
394 | 394 | self.__class__.__name__, self.priority, self.enabled) |
|
395 | 395 | |
|
396 | 396 | |
|
397 | 397 | class EmacsChecker(PrefilterChecker): |
|
398 | 398 | |
|
399 | 399 | priority = Integer(100).tag(config=True) |
|
400 | 400 | enabled = Bool(False).tag(config=True) |
|
401 | 401 | |
|
402 | 402 | def check(self, line_info): |
|
403 | 403 | "Emacs ipython-mode tags certain input lines." |
|
404 | 404 | if line_info.line.endswith('# PYTHON-MODE'): |
|
405 | 405 | return self.prefilter_manager.get_handler_by_name('emacs') |
|
406 | 406 | else: |
|
407 | 407 | return None |
|
408 | 408 | |
|
409 | 409 | |
|
410 | 410 | class MacroChecker(PrefilterChecker): |
|
411 | 411 | |
|
412 | 412 | priority = Integer(250).tag(config=True) |
|
413 | 413 | |
|
414 | 414 | def check(self, line_info): |
|
415 | 415 | obj = self.shell.user_ns.get(line_info.ifun) |
|
416 | 416 | if isinstance(obj, Macro): |
|
417 | 417 | return self.prefilter_manager.get_handler_by_name('macro') |
|
418 | 418 | else: |
|
419 | 419 | return None |
|
420 | 420 | |
|
421 | 421 | |
|
422 | 422 | class IPyAutocallChecker(PrefilterChecker): |
|
423 | 423 | |
|
424 | 424 | priority = Integer(300).tag(config=True) |
|
425 | 425 | |
|
426 | 426 | def check(self, line_info): |
|
427 | 427 | "Instances of IPyAutocall in user_ns get autocalled immediately" |
|
428 | 428 | obj = self.shell.user_ns.get(line_info.ifun, None) |
|
429 | 429 | if isinstance(obj, IPyAutocall): |
|
430 | 430 | obj.set_ip(self.shell) |
|
431 | 431 | return self.prefilter_manager.get_handler_by_name('auto') |
|
432 | 432 | else: |
|
433 | 433 | return None |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | class AssignmentChecker(PrefilterChecker): |
|
437 | 437 | |
|
438 | 438 | priority = Integer(600).tag(config=True) |
|
439 | 439 | |
|
440 | 440 | def check(self, line_info): |
|
441 | 441 | """Check to see if user is assigning to a var for the first time, in |
|
442 | 442 | which case we want to avoid any sort of automagic / autocall games. |
|
443 | 443 | |
|
444 | 444 | This allows users to assign to either alias or magic names true python |
|
445 | 445 | variables (the magic/alias systems always take second seat to true |
|
446 | 446 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
447 | 447 | if line_info.the_rest: |
|
448 | 448 | if line_info.the_rest[0] in '=,': |
|
449 | 449 | return self.prefilter_manager.get_handler_by_name('normal') |
|
450 | 450 | else: |
|
451 | 451 | return None |
|
452 | 452 | |
|
453 | 453 | |
|
454 | 454 | class AutoMagicChecker(PrefilterChecker): |
|
455 | 455 | |
|
456 | 456 | priority = Integer(700).tag(config=True) |
|
457 | 457 | |
|
458 | 458 | def check(self, line_info): |
|
459 | 459 | """If the ifun is magic, and automagic is on, run it. Note: normal, |
|
460 | 460 | non-auto magic would already have been triggered via '%' in |
|
461 | 461 | check_esc_chars. This just checks for automagic. Also, before |
|
462 | 462 | triggering the magic handler, make sure that there is nothing in the |
|
463 | 463 | user namespace which could shadow it.""" |
|
464 | 464 | if not self.shell.automagic or not self.shell.find_magic(line_info.ifun): |
|
465 | 465 | return None |
|
466 | 466 | |
|
467 | 467 | # We have a likely magic method. Make sure we should actually call it. |
|
468 | 468 | if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials: |
|
469 | 469 | return None |
|
470 | 470 | |
|
471 | 471 | head = line_info.ifun.split('.',1)[0] |
|
472 | 472 | if is_shadowed(head, self.shell): |
|
473 | 473 | return None |
|
474 | 474 | |
|
475 | 475 | return self.prefilter_manager.get_handler_by_name('magic') |
|
476 | 476 | |
|
477 | 477 | |
|
478 | 478 | class PythonOpsChecker(PrefilterChecker): |
|
479 | 479 | |
|
480 | 480 | priority = Integer(900).tag(config=True) |
|
481 | 481 | |
|
482 | 482 | def check(self, line_info): |
|
483 | 483 | """If the 'rest' of the line begins with a function call or pretty much |
|
484 | 484 | any python operator, we should simply execute the line (regardless of |
|
485 | 485 | whether or not there's a possible autocall expansion). This avoids |
|
486 | 486 | spurious (and very confusing) geattr() accesses.""" |
|
487 | 487 | if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|': |
|
488 | 488 | return self.prefilter_manager.get_handler_by_name('normal') |
|
489 | 489 | else: |
|
490 | 490 | return None |
|
491 | 491 | |
|
492 | 492 | |
|
493 | 493 | class AutocallChecker(PrefilterChecker): |
|
494 | 494 | |
|
495 | 495 | priority = Integer(1000).tag(config=True) |
|
496 | 496 | |
|
497 | 497 | function_name_regexp = CRegExp(re_fun_name, |
|
498 | 498 | help="RegExp to identify potential function names." |
|
499 | 499 | ).tag(config=True) |
|
500 | 500 | exclude_regexp = CRegExp(re_exclude_auto, |
|
501 | 501 | help="RegExp to exclude strings with this start from autocalling." |
|
502 | 502 | ).tag(config=True) |
|
503 | 503 | |
|
504 | 504 | def check(self, line_info): |
|
505 | 505 | "Check if the initial word/function is callable and autocall is on." |
|
506 | 506 | if not self.shell.autocall: |
|
507 | 507 | return None |
|
508 | 508 | |
|
509 | 509 | oinfo = line_info.ofind(self.shell) # This can mutate state via getattr |
|
510 | 510 | if not oinfo['found']: |
|
511 | 511 | return None |
|
512 | 512 | |
|
513 | 513 | ignored_funs = ['b', 'f', 'r', 'u', 'br', 'rb', 'fr', 'rf'] |
|
514 | 514 | ifun = line_info.ifun |
|
515 | 515 | line = line_info.line |
|
516 | 516 | if ifun.lower() in ignored_funs and (line.startswith(ifun + "'") or line.startswith(ifun + '"')): |
|
517 | 517 | return None |
|
518 | 518 | |
|
519 | 519 | if callable(oinfo['obj']) \ |
|
520 | 520 | and (not self.exclude_regexp.match(line_info.the_rest)) \ |
|
521 | 521 | and self.function_name_regexp.match(line_info.ifun): |
|
522 | 522 | return self.prefilter_manager.get_handler_by_name('auto') |
|
523 | 523 | else: |
|
524 | 524 | return None |
|
525 | 525 | |
|
526 | 526 | |
|
527 | 527 | #----------------------------------------------------------------------------- |
|
528 | 528 | # Prefilter handlers |
|
529 | 529 | #----------------------------------------------------------------------------- |
|
530 | 530 | |
|
531 | 531 | |
|
532 | 532 | class PrefilterHandler(Configurable): |
|
533 | 533 | |
|
534 | 534 | handler_name = Unicode('normal') |
|
535 | 535 | esc_strings = List([]) |
|
536 | 536 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
537 | 537 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
538 | 538 | |
|
539 | 539 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
540 | 540 | super(PrefilterHandler, self).__init__( |
|
541 | 541 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
542 | 542 | ) |
|
543 | 543 | self.prefilter_manager.register_handler( |
|
544 | 544 | self.handler_name, |
|
545 | 545 | self, |
|
546 | 546 | self.esc_strings |
|
547 | 547 | ) |
|
548 | 548 | |
|
549 | 549 | def handle(self, line_info): |
|
550 | 550 | # print "normal: ", line_info |
|
551 | 551 | """Handle normal input lines. Use as a template for handlers.""" |
|
552 | 552 | |
|
553 | 553 | # With autoindent on, we need some way to exit the input loop, and I |
|
554 | 554 | # don't want to force the user to have to backspace all the way to |
|
555 | 555 | # clear the line. The rule will be in this case, that either two |
|
556 | 556 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
557 | 557 | # of a size different to the indent level, will exit the input loop. |
|
558 | 558 | line = line_info.line |
|
559 | 559 | continue_prompt = line_info.continue_prompt |
|
560 | 560 | |
|
561 | 561 | if (continue_prompt and |
|
562 | 562 | self.shell.autoindent and |
|
563 | 563 | line.isspace() and |
|
564 | 564 | 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2): |
|
565 | 565 | line = '' |
|
566 | 566 | |
|
567 | 567 | return line |
|
568 | 568 | |
|
569 | 569 | def __str__(self): |
|
570 | 570 | return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name) |
|
571 | 571 | |
|
572 | 572 | |
|
573 | 573 | class MacroHandler(PrefilterHandler): |
|
574 | 574 | handler_name = Unicode("macro") |
|
575 | 575 | |
|
576 | 576 | def handle(self, line_info): |
|
577 | 577 | obj = self.shell.user_ns.get(line_info.ifun) |
|
578 | 578 | pre_space = line_info.pre_whitespace |
|
579 | 579 | line_sep = "\n" + pre_space |
|
580 | 580 | return pre_space + line_sep.join(obj.value.splitlines()) |
|
581 | 581 | |
|
582 | 582 | |
|
583 | 583 | class MagicHandler(PrefilterHandler): |
|
584 | 584 | |
|
585 | 585 | handler_name = Unicode('magic') |
|
586 | 586 | esc_strings = List([ESC_MAGIC]) |
|
587 | 587 | |
|
588 | 588 | def handle(self, line_info): |
|
589 | 589 | """Execute magic functions.""" |
|
590 | 590 | ifun = line_info.ifun |
|
591 | 591 | the_rest = line_info.the_rest |
|
592 | 592 | #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args) |
|
593 | 593 | t_arg_s = ifun + " " + the_rest |
|
594 | 594 | t_magic_name, _, t_magic_arg_s = t_arg_s.partition(' ') |
|
595 | 595 | t_magic_name = t_magic_name.lstrip(ESC_MAGIC) |
|
596 | 596 | cmd = '%sget_ipython().run_line_magic(%r, %r)' % (line_info.pre_whitespace, t_magic_name, t_magic_arg_s) |
|
597 | 597 | return cmd |
|
598 | 598 | |
|
599 | 599 | |
|
600 | 600 | class AutoHandler(PrefilterHandler): |
|
601 | 601 | |
|
602 | 602 | handler_name = Unicode('auto') |
|
603 | 603 | esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2]) |
|
604 | 604 | |
|
605 | 605 | def handle(self, line_info): |
|
606 | 606 | """Handle lines which can be auto-executed, quoting if requested.""" |
|
607 | 607 | line = line_info.line |
|
608 | 608 | ifun = line_info.ifun |
|
609 | 609 | the_rest = line_info.the_rest |
|
610 | 610 | esc = line_info.esc |
|
611 | 611 | continue_prompt = line_info.continue_prompt |
|
612 | 612 | obj = line_info.ofind(self.shell)['obj'] |
|
613 | 613 | |
|
614 | 614 | # This should only be active for single-line input! |
|
615 | 615 | if continue_prompt: |
|
616 | 616 | return line |
|
617 | 617 | |
|
618 | 618 | force_auto = isinstance(obj, IPyAutocall) |
|
619 | 619 | |
|
620 | 620 | # User objects sometimes raise exceptions on attribute access other |
|
621 | 621 | # than AttributeError (we've seen it in the past), so it's safest to be |
|
622 | 622 | # ultra-conservative here and catch all. |
|
623 | 623 | try: |
|
624 | 624 | auto_rewrite = obj.rewrite |
|
625 | 625 | except Exception: |
|
626 | 626 | auto_rewrite = True |
|
627 | 627 | |
|
628 | 628 | if esc == ESC_QUOTE: |
|
629 | 629 | # Auto-quote splitting on whitespace |
|
630 | 630 | newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) ) |
|
631 | 631 | elif esc == ESC_QUOTE2: |
|
632 | 632 | # Auto-quote whole string |
|
633 | 633 | newcmd = '%s("%s")' % (ifun,the_rest) |
|
634 | 634 | elif esc == ESC_PAREN: |
|
635 | 635 | newcmd = '%s(%s)' % (ifun,",".join(the_rest.split())) |
|
636 | 636 | else: |
|
637 | 637 | # Auto-paren. |
|
638 | 638 | if force_auto: |
|
639 | 639 | # Don't rewrite if it is already a call. |
|
640 | 640 | do_rewrite = not the_rest.startswith('(') |
|
641 | 641 | else: |
|
642 | 642 | if not the_rest: |
|
643 | 643 | # We only apply it to argument-less calls if the autocall |
|
644 | 644 | # parameter is set to 2. |
|
645 | 645 | do_rewrite = (self.shell.autocall >= 2) |
|
646 | 646 | elif the_rest.startswith('[') and hasattr(obj, '__getitem__'): |
|
647 | 647 | # Don't autocall in this case: item access for an object |
|
648 | 648 | # which is BOTH callable and implements __getitem__. |
|
649 | 649 | do_rewrite = False |
|
650 | 650 | else: |
|
651 | 651 | do_rewrite = True |
|
652 | 652 | |
|
653 | 653 | # Figure out the rewritten command |
|
654 | 654 | if do_rewrite: |
|
655 | 655 | if the_rest.endswith(';'): |
|
656 | 656 | newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1]) |
|
657 | 657 | else: |
|
658 | 658 | newcmd = '%s(%s)' % (ifun.rstrip(), the_rest) |
|
659 | 659 | else: |
|
660 | 660 | normal_handler = self.prefilter_manager.get_handler_by_name('normal') |
|
661 | 661 | return normal_handler.handle(line_info) |
|
662 | 662 | |
|
663 | 663 | # Display the rewritten call |
|
664 | 664 | if auto_rewrite: |
|
665 | 665 | self.shell.auto_rewrite_input(newcmd) |
|
666 | 666 | |
|
667 | 667 | return newcmd |
|
668 | 668 | |
|
669 | 669 | |
|
670 | 670 | class EmacsHandler(PrefilterHandler): |
|
671 | 671 | |
|
672 | 672 | handler_name = Unicode('emacs') |
|
673 | 673 | esc_strings = List([]) |
|
674 | 674 | |
|
675 | 675 | def handle(self, line_info): |
|
676 | 676 | """Handle input lines marked by python-mode.""" |
|
677 | 677 | |
|
678 | 678 | # Currently, nothing is done. Later more functionality can be added |
|
679 | 679 | # here if needed. |
|
680 | 680 | |
|
681 | 681 | # The input cache shouldn't be updated |
|
682 | 682 | return line_info.line |
|
683 | 683 | |
|
684 | 684 | |
|
685 | 685 | #----------------------------------------------------------------------------- |
|
686 | 686 | # Defaults |
|
687 | 687 | #----------------------------------------------------------------------------- |
|
688 | 688 | |
|
689 | 689 | |
|
690 | 690 | _default_transformers = [ |
|
691 | 691 | ] |
|
692 | 692 | |
|
693 | 693 | _default_checkers = [ |
|
694 | 694 | EmacsChecker, |
|
695 | 695 | MacroChecker, |
|
696 | 696 | IPyAutocallChecker, |
|
697 | 697 | AssignmentChecker, |
|
698 | 698 | AutoMagicChecker, |
|
699 | 699 | PythonOpsChecker, |
|
700 | 700 | AutocallChecker |
|
701 | 701 | ] |
|
702 | 702 | |
|
703 | 703 | _default_handlers = [ |
|
704 | 704 | PrefilterHandler, |
|
705 | 705 | MacroHandler, |
|
706 | 706 | MagicHandler, |
|
707 | 707 | AutoHandler, |
|
708 | 708 | EmacsHandler |
|
709 | 709 | ] |
@@ -1,103 +1,103 b'' | |||
|
1 | 1 | from IPython.testing.tools import AssertPrints, AssertNotPrints |
|
2 | 2 | |
|
3 | 3 | ip = get_ipython() |
|
4 | 4 | |
|
5 | 5 | def test_output_displayed(): |
|
6 | 6 | """Checking to make sure that output is displayed""" |
|
7 | 7 | |
|
8 | 8 | with AssertPrints('2'): |
|
9 | 9 | ip.run_cell('1+1', store_history=True) |
|
10 | 10 | |
|
11 | 11 | with AssertPrints('2'): |
|
12 | 12 | ip.run_cell('1+1 # comment with a semicolon;', store_history=True) |
|
13 | 13 | |
|
14 | 14 | with AssertPrints('2'): |
|
15 | 15 | ip.run_cell('1+1\n#commented_out_function();', store_history=True) |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | def test_output_quiet(): |
|
19 | 19 | """Checking to make sure that output is quiet""" |
|
20 | 20 | |
|
21 | 21 | with AssertNotPrints('2'): |
|
22 | 22 | ip.run_cell('1+1;', store_history=True) |
|
23 | 23 | |
|
24 | 24 | with AssertNotPrints('2'): |
|
25 | 25 | ip.run_cell('1+1; # comment with a semicolon', store_history=True) |
|
26 | 26 | |
|
27 | 27 | with AssertNotPrints('2'): |
|
28 | 28 | ip.run_cell('1+1;\n#commented_out_function()', store_history=True) |
|
29 | 29 | |
|
30 | 30 | def test_underscore_no_overrite_user(): |
|
31 | 31 | ip.run_cell('_ = 42', store_history=True) |
|
32 | 32 | ip.run_cell('1+1', store_history=True) |
|
33 | 33 | |
|
34 | 34 | with AssertPrints('42'): |
|
35 | 35 | ip.run_cell('print(_)', store_history=True) |
|
36 | 36 | |
|
37 | 37 | ip.run_cell('del _', store_history=True) |
|
38 | 38 | ip.run_cell('6+6', store_history=True) |
|
39 | 39 | with AssertPrints('12'): |
|
40 | 40 | ip.run_cell('_', store_history=True) |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | def test_underscore_no_overrite_builtins(): |
|
44 | 44 | ip.run_cell("import gettext ; gettext.install('foo')", store_history=True) |
|
45 | 45 | ip.run_cell('3+3', store_history=True) |
|
46 | 46 | |
|
47 | 47 | with AssertPrints('gettext'): |
|
48 | 48 | ip.run_cell('print(_)', store_history=True) |
|
49 | 49 | |
|
50 | 50 | ip.run_cell('_ = "userset"', store_history=True) |
|
51 | 51 | |
|
52 | 52 | with AssertPrints('userset'): |
|
53 | 53 | ip.run_cell('print(_)', store_history=True) |
|
54 | 54 | ip.run_cell('import builtins; del builtins._') |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 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 | 61 | saved_mode = ip.ast_node_interactivity |
|
62 | 62 | ip.ast_node_interactivity = 'last_expr_or_assign' |
|
63 | 63 | |
|
64 | 64 | try: |
|
65 | 65 | with AssertPrints('2'): |
|
66 | 66 | ip.run_cell('a = 1+1', store_history=True) |
|
67 | 67 | |
|
68 | 68 | with AssertPrints('9'): |
|
69 | 69 | ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False) |
|
70 | 70 | |
|
71 | 71 | with AssertPrints('7'): |
|
72 | 72 | ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True) |
|
73 | 73 | |
|
74 | 74 | ip.run_cell('d = 11', store_history=True) |
|
75 | 75 | with AssertPrints('12'): |
|
76 | 76 | ip.run_cell('d += 1', store_history=True) |
|
77 | 77 | |
|
78 | 78 | with AssertNotPrints('42'): |
|
79 | 79 | ip.run_cell('(u,v) = (41+1, 43-1)') |
|
80 | 80 | |
|
81 | 81 | finally: |
|
82 | 82 | ip.ast_node_interactivity = saved_mode |
|
83 | 83 | |
|
84 | 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 | 87 | by semicolon |
|
88 | 88 | """ |
|
89 | 89 | saved_mode = ip.ast_node_interactivity |
|
90 | 90 | ip.ast_node_interactivity = 'last_expr_or_assign' |
|
91 | 91 | |
|
92 | 92 | try: |
|
93 | 93 | with AssertNotPrints('2'): |
|
94 | 94 | ip.run_cell('x = 1+1;', store_history=True) |
|
95 | 95 | |
|
96 | 96 | with AssertNotPrints('7'): |
|
97 | 97 | ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True) |
|
98 | 98 | |
|
99 | 99 | with AssertNotPrints('9'): |
|
100 | 100 | ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True) |
|
101 | 101 | |
|
102 | 102 | finally: |
|
103 | 103 | ip.ast_node_interactivity = saved_mode |
@@ -1,1073 +1,1073 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for various magic functions. |
|
3 | 3 | |
|
4 | 4 | Needs to be run by nose (to make ipython session available). |
|
5 | 5 | """ |
|
6 | 6 | |
|
7 | 7 | import io |
|
8 | 8 | import os |
|
9 | 9 | import re |
|
10 | 10 | import sys |
|
11 | 11 | import warnings |
|
12 | 12 | from unittest import TestCase |
|
13 | 13 | from importlib import invalidate_caches |
|
14 | 14 | from io import StringIO |
|
15 | 15 | |
|
16 | 16 | import nose.tools as nt |
|
17 | 17 | |
|
18 | 18 | import shlex |
|
19 | 19 | |
|
20 | 20 | from IPython import get_ipython |
|
21 | 21 | from IPython.core import magic |
|
22 | 22 | from IPython.core.error import UsageError |
|
23 | 23 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
24 | 24 | cell_magic, |
|
25 | 25 | register_line_magic, register_cell_magic) |
|
26 | 26 | from IPython.core.magics import execution, script, code, logging |
|
27 | 27 | from IPython.testing import decorators as dec |
|
28 | 28 | from IPython.testing import tools as tt |
|
29 | 29 | from IPython.utils import py3compat |
|
30 | 30 | from IPython.utils.io import capture_output |
|
31 | 31 | from IPython.utils.tempdir import TemporaryDirectory |
|
32 | 32 | from IPython.utils.process import find_cmd |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | _ip = get_ipython() |
|
37 | 37 | |
|
38 | 38 | @magic.magics_class |
|
39 | 39 | class DummyMagics(magic.Magics): pass |
|
40 | 40 | |
|
41 | 41 | def test_extract_code_ranges(): |
|
42 | 42 | instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :" |
|
43 | 43 | expected = [(0, 1), |
|
44 | 44 | (2, 3), |
|
45 | 45 | (4, 6), |
|
46 | 46 | (6, 9), |
|
47 | 47 | (9, 14), |
|
48 | 48 | (16, None), |
|
49 | 49 | (None, 9), |
|
50 | 50 | (9, None), |
|
51 | 51 | (None, 13), |
|
52 | 52 | (None, None)] |
|
53 | 53 | actual = list(code.extract_code_ranges(instr)) |
|
54 | 54 | nt.assert_equal(actual, expected) |
|
55 | 55 | |
|
56 | 56 | def test_extract_symbols(): |
|
57 | 57 | source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n""" |
|
58 | 58 | symbols_args = ["a", "b", "A", "A,b", "A,a", "z"] |
|
59 | 59 | expected = [([], ['a']), |
|
60 | 60 | (["def b():\n return 42\n"], []), |
|
61 | 61 | (["class A: pass\n"], []), |
|
62 | 62 | (["class A: pass\n", "def b():\n return 42\n"], []), |
|
63 | 63 | (["class A: pass\n"], ['a']), |
|
64 | 64 | ([], ['z'])] |
|
65 | 65 | for symbols, exp in zip(symbols_args, expected): |
|
66 | 66 | nt.assert_equal(code.extract_symbols(source, symbols), exp) |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def test_extract_symbols_raises_exception_with_non_python_code(): |
|
70 | 70 | source = ("=begin A Ruby program :)=end\n" |
|
71 | 71 | "def hello\n" |
|
72 | 72 | "puts 'Hello world'\n" |
|
73 | 73 | "end") |
|
74 | 74 | with nt.assert_raises(SyntaxError): |
|
75 | 75 | code.extract_symbols(source, "hello") |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | def test_magic_not_found(): |
|
79 | 79 | # magic not found raises UsageError |
|
80 | 80 | with nt.assert_raises(UsageError): |
|
81 | 81 | _ip.magic('doesntexist') |
|
82 | 82 | |
|
83 | 83 | # ensure result isn't success when a magic isn't found |
|
84 | 84 | result = _ip.run_cell('%doesntexist') |
|
85 | 85 | assert isinstance(result.error_in_exec, UsageError) |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def test_cell_magic_not_found(): |
|
89 | 89 | # magic not found raises UsageError |
|
90 | 90 | with nt.assert_raises(UsageError): |
|
91 | 91 | _ip.run_cell_magic('doesntexist', 'line', 'cell') |
|
92 | 92 | |
|
93 | 93 | # ensure result isn't success when a magic isn't found |
|
94 | 94 | result = _ip.run_cell('%%doesntexist') |
|
95 | 95 | assert isinstance(result.error_in_exec, UsageError) |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | def test_magic_error_status(): |
|
99 | 99 | def fail(shell): |
|
100 | 100 | 1/0 |
|
101 | 101 | _ip.register_magic_function(fail) |
|
102 | 102 | result = _ip.run_cell('%fail') |
|
103 | 103 | assert isinstance(result.error_in_exec, ZeroDivisionError) |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | def test_config(): |
|
107 | 107 | """ test that config magic does not raise |
|
108 | 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 | 110 | magic. |
|
111 | 111 | """ |
|
112 | 112 | ## should not raise. |
|
113 | 113 | _ip.magic('config') |
|
114 | 114 | |
|
115 | 115 | def test_config_available_configs(): |
|
116 | 116 | """ test that config magic prints available configs in unique and |
|
117 | 117 | sorted order. """ |
|
118 | 118 | with capture_output() as captured: |
|
119 | 119 | _ip.magic('config') |
|
120 | 120 | |
|
121 | 121 | stdout = captured.stdout |
|
122 | 122 | config_classes = stdout.strip().split('\n')[1:] |
|
123 | 123 | nt.assert_list_equal(config_classes, sorted(set(config_classes))) |
|
124 | 124 | |
|
125 | 125 | def test_config_print_class(): |
|
126 | 126 | """ test that config with a classname prints the class's options. """ |
|
127 | 127 | with capture_output() as captured: |
|
128 | 128 | _ip.magic('config TerminalInteractiveShell') |
|
129 | 129 | |
|
130 | 130 | stdout = captured.stdout |
|
131 | 131 | if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]): |
|
132 | 132 | print(stdout) |
|
133 | 133 | raise AssertionError("1st line of stdout not like " |
|
134 | 134 | "'TerminalInteractiveShell.* options'") |
|
135 | 135 | |
|
136 | 136 | def test_rehashx(): |
|
137 | 137 | # clear up everything |
|
138 | 138 | _ip.alias_manager.clear_aliases() |
|
139 | 139 | del _ip.db['syscmdlist'] |
|
140 | 140 | |
|
141 | 141 | _ip.magic('rehashx') |
|
142 | 142 | # Practically ALL ipython development systems will have more than 10 aliases |
|
143 | 143 | |
|
144 | 144 | nt.assert_true(len(_ip.alias_manager.aliases) > 10) |
|
145 | 145 | for name, cmd in _ip.alias_manager.aliases: |
|
146 | 146 | # we must strip dots from alias names |
|
147 | 147 | nt.assert_not_in('.', name) |
|
148 | 148 | |
|
149 | 149 | # rehashx must fill up syscmdlist |
|
150 | 150 | scoms = _ip.db['syscmdlist'] |
|
151 | 151 | nt.assert_true(len(scoms) > 10) |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | def test_magic_parse_options(): |
|
155 | 155 | """Test that we don't mangle paths when parsing magic options.""" |
|
156 | 156 | ip = get_ipython() |
|
157 | 157 | path = 'c:\\x' |
|
158 | 158 | m = DummyMagics(ip) |
|
159 | 159 | opts = m.parse_options('-f %s' % path,'f:')[0] |
|
160 | 160 | # argv splitting is os-dependent |
|
161 | 161 | if os.name == 'posix': |
|
162 | 162 | expected = 'c:x' |
|
163 | 163 | else: |
|
164 | 164 | expected = path |
|
165 | 165 | nt.assert_equal(opts['f'], expected) |
|
166 | 166 | |
|
167 | 167 | def test_magic_parse_long_options(): |
|
168 | 168 | """Magic.parse_options can handle --foo=bar long options""" |
|
169 | 169 | ip = get_ipython() |
|
170 | 170 | m = DummyMagics(ip) |
|
171 | 171 | opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=') |
|
172 | 172 | nt.assert_in('foo', opts) |
|
173 | 173 | nt.assert_in('bar', opts) |
|
174 | 174 | nt.assert_equal(opts['bar'], "bubble") |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | @dec.skip_without('sqlite3') |
|
178 | 178 | def doctest_hist_f(): |
|
179 | 179 | """Test %hist -f with temporary filename. |
|
180 | 180 | |
|
181 | 181 | In [9]: import tempfile |
|
182 | 182 | |
|
183 | 183 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
184 | 184 | |
|
185 | 185 | In [11]: %hist -nl -f $tfile 3 |
|
186 | 186 | |
|
187 | 187 | In [13]: import os; os.unlink(tfile) |
|
188 | 188 | """ |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | @dec.skip_without('sqlite3') |
|
192 | 192 | def doctest_hist_r(): |
|
193 | 193 | """Test %hist -r |
|
194 | 194 | |
|
195 | 195 | XXX - This test is not recording the output correctly. For some reason, in |
|
196 | 196 | testing mode the raw history isn't getting populated. No idea why. |
|
197 | 197 | Disabling the output checking for now, though at least we do run it. |
|
198 | 198 | |
|
199 | 199 | In [1]: 'hist' in _ip.lsmagic() |
|
200 | 200 | Out[1]: True |
|
201 | 201 | |
|
202 | 202 | In [2]: x=1 |
|
203 | 203 | |
|
204 | 204 | In [3]: %hist -rl 2 |
|
205 | 205 | x=1 # random |
|
206 | 206 | %hist -r 2 |
|
207 | 207 | """ |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | @dec.skip_without('sqlite3') |
|
211 | 211 | def doctest_hist_op(): |
|
212 | 212 | """Test %hist -op |
|
213 | 213 | |
|
214 | 214 | In [1]: class b(float): |
|
215 | 215 | ...: pass |
|
216 | 216 | ...: |
|
217 | 217 | |
|
218 | 218 | In [2]: class s(object): |
|
219 | 219 | ...: def __str__(self): |
|
220 | 220 | ...: return 's' |
|
221 | 221 | ...: |
|
222 | 222 | |
|
223 | 223 | In [3]: |
|
224 | 224 | |
|
225 | 225 | In [4]: class r(b): |
|
226 | 226 | ...: def __repr__(self): |
|
227 | 227 | ...: return 'r' |
|
228 | 228 | ...: |
|
229 | 229 | |
|
230 | 230 | In [5]: class sr(s,r): pass |
|
231 | 231 | ...: |
|
232 | 232 | |
|
233 | 233 | In [6]: |
|
234 | 234 | |
|
235 | 235 | In [7]: bb=b() |
|
236 | 236 | |
|
237 | 237 | In [8]: ss=s() |
|
238 | 238 | |
|
239 | 239 | In [9]: rr=r() |
|
240 | 240 | |
|
241 | 241 | In [10]: ssrr=sr() |
|
242 | 242 | |
|
243 | 243 | In [11]: 4.5 |
|
244 | 244 | Out[11]: 4.5 |
|
245 | 245 | |
|
246 | 246 | In [12]: str(ss) |
|
247 | 247 | Out[12]: 's' |
|
248 | 248 | |
|
249 | 249 | In [13]: |
|
250 | 250 | |
|
251 | 251 | In [14]: %hist -op |
|
252 | 252 | >>> class b: |
|
253 | 253 | ... pass |
|
254 | 254 | ... |
|
255 | 255 | >>> class s(b): |
|
256 | 256 | ... def __str__(self): |
|
257 | 257 | ... return 's' |
|
258 | 258 | ... |
|
259 | 259 | >>> |
|
260 | 260 | >>> class r(b): |
|
261 | 261 | ... def __repr__(self): |
|
262 | 262 | ... return 'r' |
|
263 | 263 | ... |
|
264 | 264 | >>> class sr(s,r): pass |
|
265 | 265 | >>> |
|
266 | 266 | >>> bb=b() |
|
267 | 267 | >>> ss=s() |
|
268 | 268 | >>> rr=r() |
|
269 | 269 | >>> ssrr=sr() |
|
270 | 270 | >>> 4.5 |
|
271 | 271 | 4.5 |
|
272 | 272 | >>> str(ss) |
|
273 | 273 | 's' |
|
274 | 274 | >>> |
|
275 | 275 | """ |
|
276 | 276 | |
|
277 | 277 | def test_hist_pof(): |
|
278 | 278 | ip = get_ipython() |
|
279 | 279 | ip.run_cell(u"1+2", store_history=True) |
|
280 | 280 | #raise Exception(ip.history_manager.session_number) |
|
281 | 281 | #raise Exception(list(ip.history_manager._get_range_session())) |
|
282 | 282 | with TemporaryDirectory() as td: |
|
283 | 283 | tf = os.path.join(td, 'hist.py') |
|
284 | 284 | ip.run_line_magic('history', '-pof %s' % tf) |
|
285 | 285 | assert os.path.isfile(tf) |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | @dec.skip_without('sqlite3') |
|
289 | 289 | def test_macro(): |
|
290 | 290 | ip = get_ipython() |
|
291 | 291 | ip.history_manager.reset() # Clear any existing history. |
|
292 | 292 | cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] |
|
293 | 293 | for i, cmd in enumerate(cmds, start=1): |
|
294 | 294 | ip.history_manager.store_inputs(i, cmd) |
|
295 | 295 | ip.magic("macro test 1-3") |
|
296 | 296 | nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") |
|
297 | 297 | |
|
298 | 298 | # List macros |
|
299 | 299 | nt.assert_in("test", ip.magic("macro")) |
|
300 | 300 | |
|
301 | 301 | |
|
302 | 302 | @dec.skip_without('sqlite3') |
|
303 | 303 | def test_macro_run(): |
|
304 | 304 | """Test that we can run a multi-line macro successfully.""" |
|
305 | 305 | ip = get_ipython() |
|
306 | 306 | ip.history_manager.reset() |
|
307 | 307 | cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), |
|
308 | 308 | "%macro test 2-3"] |
|
309 | 309 | for cmd in cmds: |
|
310 | 310 | ip.run_cell(cmd, store_history=True) |
|
311 | 311 | nt.assert_equal(ip.user_ns["test"].value, |
|
312 | 312 | py3compat.doctest_refactor_print("a+=1\nprint a\n")) |
|
313 | 313 | with tt.AssertPrints("12"): |
|
314 | 314 | ip.run_cell("test") |
|
315 | 315 | with tt.AssertPrints("13"): |
|
316 | 316 | ip.run_cell("test") |
|
317 | 317 | |
|
318 | 318 | |
|
319 | 319 | def test_magic_magic(): |
|
320 | 320 | """Test %magic""" |
|
321 | 321 | ip = get_ipython() |
|
322 | 322 | with capture_output() as captured: |
|
323 | 323 | ip.magic("magic") |
|
324 | 324 | |
|
325 | 325 | stdout = captured.stdout |
|
326 | 326 | nt.assert_in('%magic', stdout) |
|
327 | 327 | nt.assert_in('IPython', stdout) |
|
328 | 328 | nt.assert_in('Available', stdout) |
|
329 | 329 | |
|
330 | 330 | |
|
331 | 331 | @dec.skipif_not_numpy |
|
332 | 332 | def test_numpy_reset_array_undec(): |
|
333 | 333 | "Test '%reset array' functionality" |
|
334 | 334 | _ip.ex('import numpy as np') |
|
335 | 335 | _ip.ex('a = np.empty(2)') |
|
336 | 336 | nt.assert_in('a', _ip.user_ns) |
|
337 | 337 | _ip.magic('reset -f array') |
|
338 | 338 | nt.assert_not_in('a', _ip.user_ns) |
|
339 | 339 | |
|
340 | 340 | def test_reset_out(): |
|
341 | 341 | "Test '%reset out' magic" |
|
342 | 342 | _ip.run_cell("parrot = 'dead'", store_history=True) |
|
343 | 343 | # test '%reset -f out', make an Out prompt |
|
344 | 344 | _ip.run_cell("parrot", store_history=True) |
|
345 | 345 | nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')]) |
|
346 | 346 | _ip.magic('reset -f out') |
|
347 | 347 | nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')]) |
|
348 | 348 | nt.assert_equal(len(_ip.user_ns['Out']), 0) |
|
349 | 349 | |
|
350 | 350 | def test_reset_in(): |
|
351 | 351 | "Test '%reset in' magic" |
|
352 | 352 | # test '%reset -f in' |
|
353 | 353 | _ip.run_cell("parrot", store_history=True) |
|
354 | 354 | nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')]) |
|
355 | 355 | _ip.magic('%reset -f in') |
|
356 | 356 | nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')]) |
|
357 | 357 | nt.assert_equal(len(set(_ip.user_ns['In'])), 1) |
|
358 | 358 | |
|
359 | 359 | def test_reset_dhist(): |
|
360 | 360 | "Test '%reset dhist' magic" |
|
361 | 361 | _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing |
|
362 | 362 | _ip.magic('cd ' + os.path.dirname(nt.__file__)) |
|
363 | 363 | _ip.magic('cd -') |
|
364 | 364 | nt.assert_true(len(_ip.user_ns['_dh']) > 0) |
|
365 | 365 | _ip.magic('reset -f dhist') |
|
366 | 366 | nt.assert_equal(len(_ip.user_ns['_dh']), 0) |
|
367 | 367 | _ip.run_cell("_dh = [d for d in tmp]") #restore |
|
368 | 368 | |
|
369 | 369 | def test_reset_in_length(): |
|
370 | 370 | "Test that '%reset in' preserves In[] length" |
|
371 | 371 | _ip.run_cell("print 'foo'") |
|
372 | 372 | _ip.run_cell("reset -f in") |
|
373 | 373 | nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1) |
|
374 | 374 | |
|
375 | 375 | def test_tb_syntaxerror(): |
|
376 | 376 | """test %tb after a SyntaxError""" |
|
377 | 377 | ip = get_ipython() |
|
378 | 378 | ip.run_cell("for") |
|
379 | 379 | |
|
380 | 380 | # trap and validate stdout |
|
381 | 381 | save_stdout = sys.stdout |
|
382 | 382 | try: |
|
383 | 383 | sys.stdout = StringIO() |
|
384 | 384 | ip.run_cell("%tb") |
|
385 | 385 | out = sys.stdout.getvalue() |
|
386 | 386 | finally: |
|
387 | 387 | sys.stdout = save_stdout |
|
388 | 388 | # trim output, and only check the last line |
|
389 | 389 | last_line = out.rstrip().splitlines()[-1].strip() |
|
390 | 390 | nt.assert_equal(last_line, "SyntaxError: invalid syntax") |
|
391 | 391 | |
|
392 | 392 | |
|
393 | 393 | def test_time(): |
|
394 | 394 | ip = get_ipython() |
|
395 | 395 | |
|
396 | 396 | with tt.AssertPrints("Wall time: "): |
|
397 | 397 | ip.run_cell("%time None") |
|
398 | 398 | |
|
399 | 399 | ip.run_cell("def f(kmjy):\n" |
|
400 | 400 | " %time print (2*kmjy)") |
|
401 | 401 | |
|
402 | 402 | with tt.AssertPrints("Wall time: "): |
|
403 | 403 | with tt.AssertPrints("hihi", suppress=False): |
|
404 | 404 | ip.run_cell("f('hi')") |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | @dec.skip_win32 |
|
408 | 408 | def test_time2(): |
|
409 | 409 | ip = get_ipython() |
|
410 | 410 | |
|
411 | 411 | with tt.AssertPrints("CPU times: user "): |
|
412 | 412 | ip.run_cell("%time None") |
|
413 | 413 | |
|
414 | 414 | def test_time3(): |
|
415 | 415 | """Erroneous magic function calls, issue gh-3334""" |
|
416 | 416 | ip = get_ipython() |
|
417 | 417 | ip.user_ns.pop('run', None) |
|
418 | 418 | |
|
419 | 419 | with tt.AssertNotPrints("not found", channel='stderr'): |
|
420 | 420 | ip.run_cell("%%time\n" |
|
421 | 421 | "run = 0\n" |
|
422 | 422 | "run += 1") |
|
423 | 423 | |
|
424 | 424 | def test_doctest_mode(): |
|
425 | 425 | "Toggle doctest_mode twice, it should be a no-op and run without error" |
|
426 | 426 | _ip.magic('doctest_mode') |
|
427 | 427 | _ip.magic('doctest_mode') |
|
428 | 428 | |
|
429 | 429 | |
|
430 | 430 | def test_parse_options(): |
|
431 | 431 | """Tests for basic options parsing in magics.""" |
|
432 | 432 | # These are only the most minimal of tests, more should be added later. At |
|
433 | 433 | # the very least we check that basic text/unicode calls work OK. |
|
434 | 434 | m = DummyMagics(_ip) |
|
435 | 435 | nt.assert_equal(m.parse_options('foo', '')[1], 'foo') |
|
436 | 436 | nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo') |
|
437 | 437 | |
|
438 | 438 | |
|
439 | 439 | def test_dirops(): |
|
440 | 440 | """Test various directory handling operations.""" |
|
441 | 441 | # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/') |
|
442 | 442 | curpath = os.getcwd |
|
443 | 443 | startdir = os.getcwd() |
|
444 | 444 | ipdir = os.path.realpath(_ip.ipython_dir) |
|
445 | 445 | try: |
|
446 | 446 | _ip.magic('cd "%s"' % ipdir) |
|
447 | 447 | nt.assert_equal(curpath(), ipdir) |
|
448 | 448 | _ip.magic('cd -') |
|
449 | 449 | nt.assert_equal(curpath(), startdir) |
|
450 | 450 | _ip.magic('pushd "%s"' % ipdir) |
|
451 | 451 | nt.assert_equal(curpath(), ipdir) |
|
452 | 452 | _ip.magic('popd') |
|
453 | 453 | nt.assert_equal(curpath(), startdir) |
|
454 | 454 | finally: |
|
455 | 455 | os.chdir(startdir) |
|
456 | 456 | |
|
457 | 457 | |
|
458 | 458 | def test_xmode(): |
|
459 | 459 | # Calling xmode three times should be a no-op |
|
460 | 460 | xmode = _ip.InteractiveTB.mode |
|
461 | 461 | for i in range(3): |
|
462 | 462 | _ip.magic("xmode") |
|
463 | 463 | nt.assert_equal(_ip.InteractiveTB.mode, xmode) |
|
464 | 464 | |
|
465 | 465 | def test_reset_hard(): |
|
466 | 466 | monitor = [] |
|
467 | 467 | class A(object): |
|
468 | 468 | def __del__(self): |
|
469 | 469 | monitor.append(1) |
|
470 | 470 | def __repr__(self): |
|
471 | 471 | return "<A instance>" |
|
472 | 472 | |
|
473 | 473 | _ip.user_ns["a"] = A() |
|
474 | 474 | _ip.run_cell("a") |
|
475 | 475 | |
|
476 | 476 | nt.assert_equal(monitor, []) |
|
477 | 477 | _ip.magic("reset -f") |
|
478 | 478 | nt.assert_equal(monitor, [1]) |
|
479 | 479 | |
|
480 | 480 | class TestXdel(tt.TempFileMixin): |
|
481 | 481 | def test_xdel(self): |
|
482 | 482 | """Test that references from %run are cleared by xdel.""" |
|
483 | 483 | src = ("class A(object):\n" |
|
484 | 484 | " monitor = []\n" |
|
485 | 485 | " def __del__(self):\n" |
|
486 | 486 | " self.monitor.append(1)\n" |
|
487 | 487 | "a = A()\n") |
|
488 | 488 | self.mktmp(src) |
|
489 | 489 | # %run creates some hidden references... |
|
490 | 490 | _ip.magic("run %s" % self.fname) |
|
491 | 491 | # ... as does the displayhook. |
|
492 | 492 | _ip.run_cell("a") |
|
493 | 493 | |
|
494 | 494 | monitor = _ip.user_ns["A"].monitor |
|
495 | 495 | nt.assert_equal(monitor, []) |
|
496 | 496 | |
|
497 | 497 | _ip.magic("xdel a") |
|
498 | 498 | |
|
499 | 499 | # Check that a's __del__ method has been called. |
|
500 | 500 | nt.assert_equal(monitor, [1]) |
|
501 | 501 | |
|
502 | 502 | def doctest_who(): |
|
503 | 503 | """doctest for %who |
|
504 | 504 | |
|
505 | 505 | In [1]: %reset -f |
|
506 | 506 | |
|
507 | 507 | In [2]: alpha = 123 |
|
508 | 508 | |
|
509 | 509 | In [3]: beta = 'beta' |
|
510 | 510 | |
|
511 | 511 | In [4]: %who int |
|
512 | 512 | alpha |
|
513 | 513 | |
|
514 | 514 | In [5]: %who str |
|
515 | 515 | beta |
|
516 | 516 | |
|
517 | 517 | In [6]: %whos |
|
518 | 518 | Variable Type Data/Info |
|
519 | 519 | ---------------------------- |
|
520 | 520 | alpha int 123 |
|
521 | 521 | beta str beta |
|
522 | 522 | |
|
523 | 523 | In [7]: %who_ls |
|
524 | 524 | Out[7]: ['alpha', 'beta'] |
|
525 | 525 | """ |
|
526 | 526 | |
|
527 | 527 | def test_whos(): |
|
528 | 528 | """Check that whos is protected against objects where repr() fails.""" |
|
529 | 529 | class A(object): |
|
530 | 530 | def __repr__(self): |
|
531 | 531 | raise Exception() |
|
532 | 532 | _ip.user_ns['a'] = A() |
|
533 | 533 | _ip.magic("whos") |
|
534 | 534 | |
|
535 | 535 | @py3compat.u_format |
|
536 | 536 | def doctest_precision(): |
|
537 | 537 | """doctest for %precision |
|
538 | 538 | |
|
539 | 539 | In [1]: f = get_ipython().display_formatter.formatters['text/plain'] |
|
540 | 540 | |
|
541 | 541 | In [2]: %precision 5 |
|
542 | 542 | Out[2]: '%.5f' |
|
543 | 543 | |
|
544 | 544 | In [3]: f.float_format |
|
545 | 545 | Out[3]: '%.5f' |
|
546 | 546 | |
|
547 | 547 | In [4]: %precision %e |
|
548 | 548 | Out[4]: '%e' |
|
549 | 549 | |
|
550 | 550 | In [5]: f(3.1415927) |
|
551 | 551 | Out[5]: '3.141593e+00' |
|
552 | 552 | """ |
|
553 | 553 | |
|
554 | 554 | def test_psearch(): |
|
555 | 555 | with tt.AssertPrints("dict.fromkeys"): |
|
556 | 556 | _ip.run_cell("dict.fr*?") |
|
557 | 557 | |
|
558 | 558 | def test_timeit_shlex(): |
|
559 | 559 | """test shlex issues with timeit (#1109)""" |
|
560 | 560 | _ip.ex("def f(*a,**kw): pass") |
|
561 | 561 | _ip.magic('timeit -n1 "this is a bug".count(" ")') |
|
562 | 562 | _ip.magic('timeit -r1 -n1 f(" ", 1)') |
|
563 | 563 | _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")') |
|
564 | 564 | _ip.magic('timeit -r1 -n1 ("a " + "b")') |
|
565 | 565 | _ip.magic('timeit -r1 -n1 f("a " + "b")') |
|
566 | 566 | _ip.magic('timeit -r1 -n1 f("a " + "b ")') |
|
567 | 567 | |
|
568 | 568 | |
|
569 | 569 | def test_timeit_arguments(): |
|
570 | 570 | "Test valid timeit arguments, should not cause SyntaxError (GH #1269)" |
|
571 | 571 | _ip.magic("timeit ('#')") |
|
572 | 572 | |
|
573 | 573 | |
|
574 | 574 | def test_timeit_special_syntax(): |
|
575 | 575 | "Test %%timeit with IPython special syntax" |
|
576 | 576 | @register_line_magic |
|
577 | 577 | def lmagic(line): |
|
578 | 578 | ip = get_ipython() |
|
579 | 579 | ip.user_ns['lmagic_out'] = line |
|
580 | 580 | |
|
581 | 581 | # line mode test |
|
582 | 582 | _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line') |
|
583 | 583 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line') |
|
584 | 584 | # cell mode test |
|
585 | 585 | _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2') |
|
586 | 586 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2') |
|
587 | 587 | |
|
588 | 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 | 593 | res = _ip.run_line_magic('timeit','-n10 -r10 -o 1') |
|
594 | 594 | assert(res is not None) |
|
595 | 595 | |
|
596 | 596 | def test_timeit_quiet(): |
|
597 | 597 | """ |
|
598 | 598 | test quiet option of timeit magic |
|
599 | 599 | """ |
|
600 | 600 | with tt.AssertNotPrints("loops"): |
|
601 | 601 | _ip.run_cell("%timeit -n1 -r1 -q 1") |
|
602 | 602 | |
|
603 | 603 | def test_timeit_return_quiet(): |
|
604 | 604 | with tt.AssertNotPrints("loops"): |
|
605 | 605 | res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1') |
|
606 | 606 | assert (res is not None) |
|
607 | 607 | |
|
608 | 608 | def test_timeit_invalid_return(): |
|
609 | 609 | with nt.assert_raises_regex(SyntaxError, "outside function"): |
|
610 | 610 | _ip.run_line_magic('timeit', 'return') |
|
611 | 611 | |
|
612 | 612 | @dec.skipif(execution.profile is None) |
|
613 | 613 | def test_prun_special_syntax(): |
|
614 | 614 | "Test %%prun with IPython special syntax" |
|
615 | 615 | @register_line_magic |
|
616 | 616 | def lmagic(line): |
|
617 | 617 | ip = get_ipython() |
|
618 | 618 | ip.user_ns['lmagic_out'] = line |
|
619 | 619 | |
|
620 | 620 | # line mode test |
|
621 | 621 | _ip.run_line_magic('prun', '-q %lmagic my line') |
|
622 | 622 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line') |
|
623 | 623 | # cell mode test |
|
624 | 624 | _ip.run_cell_magic('prun', '-q', '%lmagic my line2') |
|
625 | 625 | nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2') |
|
626 | 626 | |
|
627 | 627 | @dec.skipif(execution.profile is None) |
|
628 | 628 | def test_prun_quotes(): |
|
629 | 629 | "Test that prun does not clobber string escapes (GH #1302)" |
|
630 | 630 | _ip.magic(r"prun -q x = '\t'") |
|
631 | 631 | nt.assert_equal(_ip.user_ns['x'], '\t') |
|
632 | 632 | |
|
633 | 633 | def test_extension(): |
|
634 | 634 | # Debugging information for failures of this test |
|
635 | 635 | print('sys.path:') |
|
636 | 636 | for p in sys.path: |
|
637 | 637 | print(' ', p) |
|
638 | 638 | print('CWD', os.getcwd()) |
|
639 | 639 | |
|
640 | 640 | nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension") |
|
641 | 641 | daft_path = os.path.join(os.path.dirname(__file__), "daft_extension") |
|
642 | 642 | sys.path.insert(0, daft_path) |
|
643 | 643 | try: |
|
644 | 644 | _ip.user_ns.pop('arq', None) |
|
645 | 645 | invalidate_caches() # Clear import caches |
|
646 | 646 | _ip.magic("load_ext daft_extension") |
|
647 | 647 | nt.assert_equal(_ip.user_ns['arq'], 185) |
|
648 | 648 | _ip.magic("unload_ext daft_extension") |
|
649 | 649 | assert 'arq' not in _ip.user_ns |
|
650 | 650 | finally: |
|
651 | 651 | sys.path.remove(daft_path) |
|
652 | 652 | |
|
653 | 653 | |
|
654 | 654 | def test_notebook_export_json(): |
|
655 | 655 | _ip = get_ipython() |
|
656 | 656 | _ip.history_manager.reset() # Clear any existing history. |
|
657 | 657 | cmds = [u"a=1", u"def b():\n return a**2", u"print('noël, été', b())"] |
|
658 | 658 | for i, cmd in enumerate(cmds, start=1): |
|
659 | 659 | _ip.history_manager.store_inputs(i, cmd) |
|
660 | 660 | with TemporaryDirectory() as td: |
|
661 | 661 | outfile = os.path.join(td, "nb.ipynb") |
|
662 | 662 | _ip.magic("notebook -e %s" % outfile) |
|
663 | 663 | |
|
664 | 664 | |
|
665 | 665 | class TestEnv(TestCase): |
|
666 | 666 | |
|
667 | 667 | def test_env(self): |
|
668 | 668 | env = _ip.magic("env") |
|
669 | 669 | self.assertTrue(isinstance(env, dict)) |
|
670 | 670 | |
|
671 | 671 | def test_env_get_set_simple(self): |
|
672 | 672 | env = _ip.magic("env var val1") |
|
673 | 673 | self.assertEqual(env, None) |
|
674 | 674 | self.assertEqual(os.environ['var'], 'val1') |
|
675 | 675 | self.assertEqual(_ip.magic("env var"), 'val1') |
|
676 | 676 | env = _ip.magic("env var=val2") |
|
677 | 677 | self.assertEqual(env, None) |
|
678 | 678 | self.assertEqual(os.environ['var'], 'val2') |
|
679 | 679 | |
|
680 | 680 | def test_env_get_set_complex(self): |
|
681 | 681 | env = _ip.magic("env var 'val1 '' 'val2") |
|
682 | 682 | self.assertEqual(env, None) |
|
683 | 683 | self.assertEqual(os.environ['var'], "'val1 '' 'val2") |
|
684 | 684 | self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2") |
|
685 | 685 | env = _ip.magic('env var=val2 val3="val4') |
|
686 | 686 | self.assertEqual(env, None) |
|
687 | 687 | self.assertEqual(os.environ['var'], 'val2 val3="val4') |
|
688 | 688 | |
|
689 | 689 | def test_env_set_bad_input(self): |
|
690 | 690 | self.assertRaises(UsageError, lambda: _ip.magic("set_env var")) |
|
691 | 691 | |
|
692 | 692 | def test_env_set_whitespace(self): |
|
693 | 693 | self.assertRaises(UsageError, lambda: _ip.magic("env var A=B")) |
|
694 | 694 | |
|
695 | 695 | |
|
696 | 696 | class CellMagicTestCase(TestCase): |
|
697 | 697 | |
|
698 | 698 | def check_ident(self, magic): |
|
699 | 699 | # Manually called, we get the result |
|
700 | 700 | out = _ip.run_cell_magic(magic, 'a', 'b') |
|
701 | 701 | nt.assert_equal(out, ('a','b')) |
|
702 | 702 | # Via run_cell, it goes into the user's namespace via displayhook |
|
703 | 703 | _ip.run_cell('%%' + magic +' c\nd') |
|
704 | 704 | nt.assert_equal(_ip.user_ns['_'], ('c','d')) |
|
705 | 705 | |
|
706 | 706 | def test_cell_magic_func_deco(self): |
|
707 | 707 | "Cell magic using simple decorator" |
|
708 | 708 | @register_cell_magic |
|
709 | 709 | def cellm(line, cell): |
|
710 | 710 | return line, cell |
|
711 | 711 | |
|
712 | 712 | self.check_ident('cellm') |
|
713 | 713 | |
|
714 | 714 | def test_cell_magic_reg(self): |
|
715 | 715 | "Cell magic manually registered" |
|
716 | 716 | def cellm(line, cell): |
|
717 | 717 | return line, cell |
|
718 | 718 | |
|
719 | 719 | _ip.register_magic_function(cellm, 'cell', 'cellm2') |
|
720 | 720 | self.check_ident('cellm2') |
|
721 | 721 | |
|
722 | 722 | def test_cell_magic_class(self): |
|
723 | 723 | "Cell magics declared via a class" |
|
724 | 724 | @magics_class |
|
725 | 725 | class MyMagics(Magics): |
|
726 | 726 | |
|
727 | 727 | @cell_magic |
|
728 | 728 | def cellm3(self, line, cell): |
|
729 | 729 | return line, cell |
|
730 | 730 | |
|
731 | 731 | _ip.register_magics(MyMagics) |
|
732 | 732 | self.check_ident('cellm3') |
|
733 | 733 | |
|
734 | 734 | def test_cell_magic_class2(self): |
|
735 | 735 | "Cell magics declared via a class, #2" |
|
736 | 736 | @magics_class |
|
737 | 737 | class MyMagics2(Magics): |
|
738 | 738 | |
|
739 | 739 | @cell_magic('cellm4') |
|
740 | 740 | def cellm33(self, line, cell): |
|
741 | 741 | return line, cell |
|
742 | 742 | |
|
743 | 743 | _ip.register_magics(MyMagics2) |
|
744 | 744 | self.check_ident('cellm4') |
|
745 | 745 | # Check that nothing is registered as 'cellm33' |
|
746 | 746 | c33 = _ip.find_cell_magic('cellm33') |
|
747 | 747 | nt.assert_equal(c33, None) |
|
748 | 748 | |
|
749 | 749 | def test_file(): |
|
750 | 750 | """Basic %%file""" |
|
751 | 751 | ip = get_ipython() |
|
752 | 752 | with TemporaryDirectory() as td: |
|
753 | 753 | fname = os.path.join(td, 'file1') |
|
754 | 754 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
755 | 755 | 'line1', |
|
756 | 756 | 'line2', |
|
757 | 757 | ])) |
|
758 | 758 | with open(fname) as f: |
|
759 | 759 | s = f.read() |
|
760 | 760 | nt.assert_in('line1\n', s) |
|
761 | 761 | nt.assert_in('line2', s) |
|
762 | 762 | |
|
763 | 763 | def test_file_var_expand(): |
|
764 | 764 | """%%file $filename""" |
|
765 | 765 | ip = get_ipython() |
|
766 | 766 | with TemporaryDirectory() as td: |
|
767 | 767 | fname = os.path.join(td, 'file1') |
|
768 | 768 | ip.user_ns['filename'] = fname |
|
769 | 769 | ip.run_cell_magic("file", '$filename', u'\n'.join([ |
|
770 | 770 | 'line1', |
|
771 | 771 | 'line2', |
|
772 | 772 | ])) |
|
773 | 773 | with open(fname) as f: |
|
774 | 774 | s = f.read() |
|
775 | 775 | nt.assert_in('line1\n', s) |
|
776 | 776 | nt.assert_in('line2', s) |
|
777 | 777 | |
|
778 | 778 | def test_file_unicode(): |
|
779 | 779 | """%%file with unicode cell""" |
|
780 | 780 | ip = get_ipython() |
|
781 | 781 | with TemporaryDirectory() as td: |
|
782 | 782 | fname = os.path.join(td, 'file1') |
|
783 | 783 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
784 | 784 | u'liné1', |
|
785 | 785 | u'liné2', |
|
786 | 786 | ])) |
|
787 | 787 | with io.open(fname, encoding='utf-8') as f: |
|
788 | 788 | s = f.read() |
|
789 | 789 | nt.assert_in(u'liné1\n', s) |
|
790 | 790 | nt.assert_in(u'liné2', s) |
|
791 | 791 | |
|
792 | 792 | def test_file_amend(): |
|
793 | 793 | """%%file -a amends files""" |
|
794 | 794 | ip = get_ipython() |
|
795 | 795 | with TemporaryDirectory() as td: |
|
796 | 796 | fname = os.path.join(td, 'file2') |
|
797 | 797 | ip.run_cell_magic("file", fname, u'\n'.join([ |
|
798 | 798 | 'line1', |
|
799 | 799 | 'line2', |
|
800 | 800 | ])) |
|
801 | 801 | ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([ |
|
802 | 802 | 'line3', |
|
803 | 803 | 'line4', |
|
804 | 804 | ])) |
|
805 | 805 | with open(fname) as f: |
|
806 | 806 | s = f.read() |
|
807 | 807 | nt.assert_in('line1\n', s) |
|
808 | 808 | nt.assert_in('line3\n', s) |
|
809 | 809 | |
|
810 | 810 | |
|
811 | 811 | def test_script_config(): |
|
812 | 812 | ip = get_ipython() |
|
813 | 813 | ip.config.ScriptMagics.script_magics = ['whoda'] |
|
814 | 814 | sm = script.ScriptMagics(shell=ip) |
|
815 | 815 | nt.assert_in('whoda', sm.magics['cell']) |
|
816 | 816 | |
|
817 | 817 | @dec.skip_win32 |
|
818 | 818 | def test_script_out(): |
|
819 | 819 | ip = get_ipython() |
|
820 | 820 | ip.run_cell_magic("script", "--out output sh", "echo 'hi'") |
|
821 | 821 | nt.assert_equal(ip.user_ns['output'], 'hi\n') |
|
822 | 822 | |
|
823 | 823 | @dec.skip_win32 |
|
824 | 824 | def test_script_err(): |
|
825 | 825 | ip = get_ipython() |
|
826 | 826 | ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2") |
|
827 | 827 | nt.assert_equal(ip.user_ns['error'], 'hello\n') |
|
828 | 828 | |
|
829 | 829 | @dec.skip_win32 |
|
830 | 830 | def test_script_out_err(): |
|
831 | 831 | ip = get_ipython() |
|
832 | 832 | ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2") |
|
833 | 833 | nt.assert_equal(ip.user_ns['output'], 'hi\n') |
|
834 | 834 | nt.assert_equal(ip.user_ns['error'], 'hello\n') |
|
835 | 835 | |
|
836 | 836 | @dec.skip_win32 |
|
837 | 837 | def test_script_bg_out(): |
|
838 | 838 | ip = get_ipython() |
|
839 | 839 | ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'") |
|
840 | 840 | nt.assert_equal(ip.user_ns['output'].read(), b'hi\n') |
|
841 | 841 | |
|
842 | 842 | @dec.skip_win32 |
|
843 | 843 | def test_script_bg_err(): |
|
844 | 844 | ip = get_ipython() |
|
845 | 845 | ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2") |
|
846 | 846 | nt.assert_equal(ip.user_ns['error'].read(), b'hello\n') |
|
847 | 847 | |
|
848 | 848 | @dec.skip_win32 |
|
849 | 849 | def test_script_bg_out_err(): |
|
850 | 850 | ip = get_ipython() |
|
851 | 851 | ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2") |
|
852 | 852 | nt.assert_equal(ip.user_ns['output'].read(), b'hi\n') |
|
853 | 853 | nt.assert_equal(ip.user_ns['error'].read(), b'hello\n') |
|
854 | 854 | |
|
855 | 855 | def test_script_defaults(): |
|
856 | 856 | ip = get_ipython() |
|
857 | 857 | for cmd in ['sh', 'bash', 'perl', 'ruby']: |
|
858 | 858 | try: |
|
859 | 859 | find_cmd(cmd) |
|
860 | 860 | except Exception: |
|
861 | 861 | pass |
|
862 | 862 | else: |
|
863 | 863 | nt.assert_in(cmd, ip.magics_manager.magics['cell']) |
|
864 | 864 | |
|
865 | 865 | |
|
866 | 866 | @magics_class |
|
867 | 867 | class FooFoo(Magics): |
|
868 | 868 | """class with both %foo and %%foo magics""" |
|
869 | 869 | @line_magic('foo') |
|
870 | 870 | def line_foo(self, line): |
|
871 | 871 | "I am line foo" |
|
872 | 872 | pass |
|
873 | 873 | |
|
874 | 874 | @cell_magic("foo") |
|
875 | 875 | def cell_foo(self, line, cell): |
|
876 | 876 | "I am cell foo, not line foo" |
|
877 | 877 | pass |
|
878 | 878 | |
|
879 | 879 | def test_line_cell_info(): |
|
880 | 880 | """%%foo and %foo magics are distinguishable to inspect""" |
|
881 | 881 | ip = get_ipython() |
|
882 | 882 | ip.magics_manager.register(FooFoo) |
|
883 | 883 | oinfo = ip.object_inspect('foo') |
|
884 | 884 | nt.assert_true(oinfo['found']) |
|
885 | 885 | nt.assert_true(oinfo['ismagic']) |
|
886 | 886 | |
|
887 | 887 | oinfo = ip.object_inspect('%%foo') |
|
888 | 888 | nt.assert_true(oinfo['found']) |
|
889 | 889 | nt.assert_true(oinfo['ismagic']) |
|
890 | 890 | nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__) |
|
891 | 891 | |
|
892 | 892 | oinfo = ip.object_inspect('%foo') |
|
893 | 893 | nt.assert_true(oinfo['found']) |
|
894 | 894 | nt.assert_true(oinfo['ismagic']) |
|
895 | 895 | nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__) |
|
896 | 896 | |
|
897 | 897 | def test_multiple_magics(): |
|
898 | 898 | ip = get_ipython() |
|
899 | 899 | foo1 = FooFoo(ip) |
|
900 | 900 | foo2 = FooFoo(ip) |
|
901 | 901 | mm = ip.magics_manager |
|
902 | 902 | mm.register(foo1) |
|
903 | 903 | nt.assert_true(mm.magics['line']['foo'].__self__ is foo1) |
|
904 | 904 | mm.register(foo2) |
|
905 | 905 | nt.assert_true(mm.magics['line']['foo'].__self__ is foo2) |
|
906 | 906 | |
|
907 | 907 | def test_alias_magic(): |
|
908 | 908 | """Test %alias_magic.""" |
|
909 | 909 | ip = get_ipython() |
|
910 | 910 | mm = ip.magics_manager |
|
911 | 911 | |
|
912 | 912 | # Basic operation: both cell and line magics are created, if possible. |
|
913 | 913 | ip.run_line_magic('alias_magic', 'timeit_alias timeit') |
|
914 | 914 | nt.assert_in('timeit_alias', mm.magics['line']) |
|
915 | 915 | nt.assert_in('timeit_alias', mm.magics['cell']) |
|
916 | 916 | |
|
917 | 917 | # --cell is specified, line magic not created. |
|
918 | 918 | ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit') |
|
919 | 919 | nt.assert_not_in('timeit_cell_alias', mm.magics['line']) |
|
920 | 920 | nt.assert_in('timeit_cell_alias', mm.magics['cell']) |
|
921 | 921 | |
|
922 | 922 | # Test that line alias is created successfully. |
|
923 | 923 | ip.run_line_magic('alias_magic', '--line env_alias env') |
|
924 | 924 | nt.assert_equal(ip.run_line_magic('env', ''), |
|
925 | 925 | ip.run_line_magic('env_alias', '')) |
|
926 | 926 | |
|
927 | 927 | # Test that line alias with parameters passed in is created successfully. |
|
928 | 928 | ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3')) |
|
929 | 929 | nt.assert_in('history_alias', mm.magics['line']) |
|
930 | 930 | |
|
931 | 931 | |
|
932 | 932 | def test_save(): |
|
933 | 933 | """Test %save.""" |
|
934 | 934 | ip = get_ipython() |
|
935 | 935 | ip.history_manager.reset() # Clear any existing history. |
|
936 | 936 | cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"] |
|
937 | 937 | for i, cmd in enumerate(cmds, start=1): |
|
938 | 938 | ip.history_manager.store_inputs(i, cmd) |
|
939 | 939 | with TemporaryDirectory() as tmpdir: |
|
940 | 940 | file = os.path.join(tmpdir, "testsave.py") |
|
941 | 941 | ip.run_line_magic("save", "%s 1-10" % file) |
|
942 | 942 | with open(file) as f: |
|
943 | 943 | content = f.read() |
|
944 | 944 | nt.assert_equal(content.count(cmds[0]), 1) |
|
945 | 945 | nt.assert_in('coding: utf-8', content) |
|
946 | 946 | ip.run_line_magic("save", "-a %s 1-10" % file) |
|
947 | 947 | with open(file) as f: |
|
948 | 948 | content = f.read() |
|
949 | 949 | nt.assert_equal(content.count(cmds[0]), 2) |
|
950 | 950 | nt.assert_in('coding: utf-8', content) |
|
951 | 951 | |
|
952 | 952 | |
|
953 | 953 | def test_store(): |
|
954 | 954 | """Test %store.""" |
|
955 | 955 | ip = get_ipython() |
|
956 | 956 | ip.run_line_magic('load_ext', 'storemagic') |
|
957 | 957 | |
|
958 | 958 | # make sure the storage is empty |
|
959 | 959 | ip.run_line_magic('store', '-z') |
|
960 | 960 | ip.user_ns['var'] = 42 |
|
961 | 961 | ip.run_line_magic('store', 'var') |
|
962 | 962 | ip.user_ns['var'] = 39 |
|
963 | 963 | ip.run_line_magic('store', '-r') |
|
964 | 964 | nt.assert_equal(ip.user_ns['var'], 42) |
|
965 | 965 | |
|
966 | 966 | ip.run_line_magic('store', '-d var') |
|
967 | 967 | ip.user_ns['var'] = 39 |
|
968 | 968 | ip.run_line_magic('store' , '-r') |
|
969 | 969 | nt.assert_equal(ip.user_ns['var'], 39) |
|
970 | 970 | |
|
971 | 971 | |
|
972 | 972 | def _run_edit_test(arg_s, exp_filename=None, |
|
973 | 973 | exp_lineno=-1, |
|
974 | 974 | exp_contents=None, |
|
975 | 975 | exp_is_temp=None): |
|
976 | 976 | ip = get_ipython() |
|
977 | 977 | M = code.CodeMagics(ip) |
|
978 | 978 | last_call = ['',''] |
|
979 | 979 | opts,args = M.parse_options(arg_s,'prxn:') |
|
980 | 980 | filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call) |
|
981 | 981 | |
|
982 | 982 | if exp_filename is not None: |
|
983 | 983 | nt.assert_equal(exp_filename, filename) |
|
984 | 984 | if exp_contents is not None: |
|
985 | 985 | with io.open(filename, 'r', encoding='utf-8') as f: |
|
986 | 986 | contents = f.read() |
|
987 | 987 | nt.assert_equal(exp_contents, contents) |
|
988 | 988 | if exp_lineno != -1: |
|
989 | 989 | nt.assert_equal(exp_lineno, lineno) |
|
990 | 990 | if exp_is_temp is not None: |
|
991 | 991 | nt.assert_equal(exp_is_temp, is_temp) |
|
992 | 992 | |
|
993 | 993 | |
|
994 | 994 | def test_edit_interactive(): |
|
995 | 995 | """%edit on interactively defined objects""" |
|
996 | 996 | ip = get_ipython() |
|
997 | 997 | n = ip.execution_count |
|
998 | 998 | ip.run_cell(u"def foo(): return 1", store_history=True) |
|
999 | 999 | |
|
1000 | 1000 | try: |
|
1001 | 1001 | _run_edit_test("foo") |
|
1002 | 1002 | except code.InteractivelyDefined as e: |
|
1003 | 1003 | nt.assert_equal(e.index, n) |
|
1004 | 1004 | else: |
|
1005 | 1005 | raise AssertionError("Should have raised InteractivelyDefined") |
|
1006 | 1006 | |
|
1007 | 1007 | |
|
1008 | 1008 | def test_edit_cell(): |
|
1009 | 1009 | """%edit [cell id]""" |
|
1010 | 1010 | ip = get_ipython() |
|
1011 | 1011 | |
|
1012 | 1012 | ip.run_cell(u"def foo(): return 1", store_history=True) |
|
1013 | 1013 | |
|
1014 | 1014 | # test |
|
1015 | 1015 | _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True) |
|
1016 | 1016 | |
|
1017 | 1017 | def test_bookmark(): |
|
1018 | 1018 | ip = get_ipython() |
|
1019 | 1019 | ip.run_line_magic('bookmark', 'bmname') |
|
1020 | 1020 | with tt.AssertPrints('bmname'): |
|
1021 | 1021 | ip.run_line_magic('bookmark', '-l') |
|
1022 | 1022 | ip.run_line_magic('bookmark', '-d bmname') |
|
1023 | 1023 | |
|
1024 | 1024 | def test_ls_magic(): |
|
1025 | 1025 | ip = get_ipython() |
|
1026 | 1026 | json_formatter = ip.display_formatter.formatters['application/json'] |
|
1027 | 1027 | json_formatter.enabled = True |
|
1028 | 1028 | lsmagic = ip.magic('lsmagic') |
|
1029 | 1029 | with warnings.catch_warnings(record=True) as w: |
|
1030 | 1030 | j = json_formatter(lsmagic) |
|
1031 | 1031 | nt.assert_equal(sorted(j), ['cell', 'line']) |
|
1032 | 1032 | nt.assert_equal(w, []) # no warnings |
|
1033 | 1033 | |
|
1034 | 1034 | def test_strip_initial_indent(): |
|
1035 | 1035 | def sii(s): |
|
1036 | 1036 | lines = s.splitlines() |
|
1037 | 1037 | return '\n'.join(code.strip_initial_indent(lines)) |
|
1038 | 1038 | |
|
1039 | 1039 | nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2") |
|
1040 | 1040 | nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc") |
|
1041 | 1041 | nt.assert_equal(sii("a\n b"), "a\n b") |
|
1042 | 1042 | |
|
1043 | 1043 | def test_logging_magic_quiet_from_arg(): |
|
1044 | 1044 | _ip.config.LoggingMagics.quiet = False |
|
1045 | 1045 | lm = logging.LoggingMagics(shell=_ip) |
|
1046 | 1046 | with TemporaryDirectory() as td: |
|
1047 | 1047 | try: |
|
1048 | 1048 | with tt.AssertNotPrints(re.compile("Activating.*")): |
|
1049 | 1049 | lm.logstart('-q {}'.format( |
|
1050 | 1050 | os.path.join(td, "quiet_from_arg.log"))) |
|
1051 | 1051 | finally: |
|
1052 | 1052 | _ip.logger.logstop() |
|
1053 | 1053 | |
|
1054 | 1054 | def test_logging_magic_quiet_from_config(): |
|
1055 | 1055 | _ip.config.LoggingMagics.quiet = True |
|
1056 | 1056 | lm = logging.LoggingMagics(shell=_ip) |
|
1057 | 1057 | with TemporaryDirectory() as td: |
|
1058 | 1058 | try: |
|
1059 | 1059 | with tt.AssertNotPrints(re.compile("Activating.*")): |
|
1060 | 1060 | lm.logstart(os.path.join(td, "quiet_from_config.log")) |
|
1061 | 1061 | finally: |
|
1062 | 1062 | _ip.logger.logstop() |
|
1063 | 1063 | |
|
1064 | 1064 | def test_logging_magic_not_quiet(): |
|
1065 | 1065 | _ip.config.LoggingMagics.quiet = False |
|
1066 | 1066 | lm = logging.LoggingMagics(shell=_ip) |
|
1067 | 1067 | with TemporaryDirectory() as td: |
|
1068 | 1068 | try: |
|
1069 | 1069 | with tt.AssertPrints(re.compile("Activating.*")): |
|
1070 | 1070 | lm.logstart(os.path.join(td, "not_quiet.log")) |
|
1071 | 1071 | finally: |
|
1072 | 1072 | _ip.logger.logstop() |
|
1073 | 1073 |
@@ -1,348 +1,348 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Usage information for the main IPython applications. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | import sys |
|
13 | 13 | from IPython.core import release |
|
14 | 14 | |
|
15 | 15 | cl_usage = """\ |
|
16 | 16 | ========= |
|
17 | 17 | IPython |
|
18 | 18 | ========= |
|
19 | 19 | |
|
20 | 20 | Tools for Interactive Computing in Python |
|
21 | 21 | ========================================= |
|
22 | 22 | |
|
23 | 23 | A Python shell with automatic history (input and output), dynamic object |
|
24 | 24 | introspection, easier configuration, command completion, access to the |
|
25 | 25 | system shell and more. IPython can also be embedded in running programs. |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | Usage |
|
29 | 29 | |
|
30 | 30 | ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ... |
|
31 | 31 | |
|
32 | 32 | If invoked with no options, it executes the file and exits, passing the |
|
33 | 33 | remaining arguments to the script, just as if you had specified the same |
|
34 | 34 | command with python. You may need to specify `--` before args to be passed |
|
35 | 35 | to the script, to prevent IPython from attempting to parse them. If you |
|
36 | 36 | specify the option `-i` before the filename, it will enter an interactive |
|
37 | 37 | IPython session after running the script, rather than exiting. Files ending |
|
38 | 38 | in .py will be treated as normal Python, but files ending in .ipy can |
|
39 | 39 | contain special IPython syntax (magic commands, shell expansions, etc.). |
|
40 | 40 | |
|
41 | 41 | Almost all configuration in IPython is available via the command-line. Do |
|
42 | 42 | `ipython --help-all` to see all available options. For persistent |
|
43 | 43 | configuration, look into your `ipython_config.py` configuration file for |
|
44 | 44 | details. |
|
45 | 45 | |
|
46 | 46 | This file is typically installed in the `IPYTHONDIR` directory, and there |
|
47 | 47 | is a separate configuration directory for each profile. The default profile |
|
48 | 48 | directory will be located in $IPYTHONDIR/profile_default. IPYTHONDIR |
|
49 | 49 | defaults to to `$HOME/.ipython`. For Windows users, $HOME resolves to |
|
50 | 50 | C:\\Users\\YourUserName in most instances. |
|
51 | 51 | |
|
52 | 52 | To initialize a profile with the default configuration file, do:: |
|
53 | 53 | |
|
54 | 54 | $> ipython profile create |
|
55 | 55 | |
|
56 | 56 | and start editing `IPYTHONDIR/profile_default/ipython_config.py` |
|
57 | 57 | |
|
58 | 58 | In IPython's documentation, we will refer to this directory as |
|
59 | 59 | `IPYTHONDIR`, you can change its default location by creating an |
|
60 | 60 | environment variable with this name and setting it to the desired path. |
|
61 | 61 | |
|
62 | 62 | For more information, see the manual available in HTML and PDF in your |
|
63 | 63 | installation, or online at http://ipython.org/documentation.html. |
|
64 | 64 | """ |
|
65 | 65 | |
|
66 | 66 | interactive_usage = """ |
|
67 | 67 | IPython -- An enhanced Interactive Python |
|
68 | 68 | ========================================= |
|
69 | 69 | |
|
70 | 70 | IPython offers a fully compatible replacement for the standard Python |
|
71 | 71 | interpreter, with convenient shell features, special commands, command |
|
72 | 72 | history mechanism and output results caching. |
|
73 | 73 | |
|
74 | 74 | At your system command line, type 'ipython -h' to see the command line |
|
75 | 75 | options available. This document only describes interactive features. |
|
76 | 76 | |
|
77 | 77 | GETTING HELP |
|
78 | 78 | ------------ |
|
79 | 79 | |
|
80 | 80 | Within IPython you have various way to access help: |
|
81 | 81 | |
|
82 | 82 | ? -> Introduction and overview of IPython's features (this screen). |
|
83 | 83 | object? -> Details about 'object'. |
|
84 | 84 | object?? -> More detailed, verbose information about 'object'. |
|
85 | 85 | %quickref -> Quick reference of all IPython specific syntax and magics. |
|
86 | 86 | help -> Access Python's own help system. |
|
87 | 87 | |
|
88 | 88 | If you are in terminal IPython you can quit this screen by pressing `q`. |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | MAIN FEATURES |
|
92 | 92 | ------------- |
|
93 | 93 | |
|
94 | 94 | * Access to the standard Python help with object docstrings and the Python |
|
95 | 95 | manuals. Simply type 'help' (no quotes) to invoke it. |
|
96 | 96 | |
|
97 | 97 | * Magic commands: type %magic for information on the magic subsystem. |
|
98 | 98 | |
|
99 | 99 | * System command aliases, via the %alias command or the configuration file(s). |
|
100 | 100 | |
|
101 | 101 | * Dynamic object information: |
|
102 | 102 | |
|
103 | 103 | Typing ?word or word? prints detailed information about an object. Certain |
|
104 | 104 | long strings (code, etc.) get snipped in the center for brevity. |
|
105 | 105 | |
|
106 | 106 | Typing ??word or word?? gives access to the full information without |
|
107 | 107 | snipping long strings. Strings that are longer than the screen are printed |
|
108 | 108 | through the less pager. |
|
109 | 109 | |
|
110 | 110 | The ?/?? system gives access to the full source code for any object (if |
|
111 | 111 | available), shows function prototypes and other useful information. |
|
112 | 112 | |
|
113 | 113 | If you just want to see an object's docstring, type '%pdoc object' (without |
|
114 | 114 | quotes, and without % if you have automagic on). |
|
115 | 115 | |
|
116 | 116 | * Tab completion in the local namespace: |
|
117 | 117 | |
|
118 | 118 | At any time, hitting tab will complete any available python commands or |
|
119 | 119 | variable names, and show you a list of the possible completions if there's |
|
120 | 120 | no unambiguous one. It will also complete filenames in the current directory. |
|
121 | 121 | |
|
122 | 122 | * Search previous command history in multiple ways: |
|
123 | 123 | |
|
124 | 124 | - Start typing, and then use arrow keys up/down or (Ctrl-p/Ctrl-n) to search |
|
125 | 125 | through the history items that match what you've typed so far. |
|
126 | 126 | |
|
127 | 127 | - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches |
|
128 | 128 | your history for lines that match what you've typed so far, completing as |
|
129 | 129 | much as it can. |
|
130 | 130 | |
|
131 | 131 | - %hist: search history by index. |
|
132 | 132 | |
|
133 | 133 | * Persistent command history across sessions. |
|
134 | 134 | |
|
135 | 135 | * Logging of input with the ability to save and restore a working session. |
|
136 | 136 | |
|
137 | 137 | * System shell with !. Typing !ls will run 'ls' in the current directory. |
|
138 | 138 | |
|
139 | 139 | * The reload command does a 'deep' reload of a module: changes made to the |
|
140 | 140 | module since you imported will actually be available without having to exit. |
|
141 | 141 | |
|
142 | 142 | * Verbose and colored exception traceback printouts. See the magic xmode and |
|
143 | 143 | xcolor functions for details (just type %magic). |
|
144 | 144 | |
|
145 | 145 | * Input caching system: |
|
146 | 146 | |
|
147 | 147 | IPython offers numbered prompts (In/Out) with input and output caching. All |
|
148 | 148 | input is saved and can be retrieved as variables (besides the usual arrow |
|
149 | 149 | key recall). |
|
150 | 150 | |
|
151 | 151 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
152 | 152 | _i: stores previous input. |
|
153 | 153 | _ii: next previous. |
|
154 | 154 | _iii: next-next previous. |
|
155 | 155 | _ih : a list of all input _ih[n] is the input from line n. |
|
156 | 156 | |
|
157 | 157 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
158 | 158 | being the prompt counter), such that _i<n> == _ih[<n>] |
|
159 | 159 | |
|
160 | 160 | For example, what you typed at prompt 14 is available as _i14 and _ih[14]. |
|
161 | 161 | |
|
162 | 162 | You can create macros which contain multiple input lines from this history, |
|
163 | 163 | for later re-execution, with the %macro function. |
|
164 | 164 | |
|
165 | 165 | The history function %hist allows you to see any part of your input history |
|
166 | 166 | by printing a range of the _i variables. Note that inputs which contain |
|
167 | 167 | magic functions (%) appear in the history with a prepended comment. This is |
|
168 | 168 | because they aren't really valid Python code, so you can't exec them. |
|
169 | 169 | |
|
170 | 170 | * Output caching system: |
|
171 | 171 | |
|
172 | 172 | For output that is returned from actions, a system similar to the input |
|
173 | 173 | cache exists but using _ instead of _i. Only actions that produce a result |
|
174 | 174 | (NOT assignments, for example) are cached. If you are familiar with |
|
175 | 175 | Mathematica, IPython's _ variables behave exactly like Mathematica's % |
|
176 | 176 | variables. |
|
177 | 177 | |
|
178 | 178 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
179 | 179 | _ (one underscore): previous output. |
|
180 | 180 | __ (two underscores): next previous. |
|
181 | 181 | ___ (three underscores): next-next previous. |
|
182 | 182 | |
|
183 | 183 | Global variables named _<n> are dynamically created (<n> being the prompt |
|
184 | 184 | counter), such that the result of output <n> is always available as _<n>. |
|
185 | 185 | |
|
186 | 186 | Finally, a global dictionary named _oh exists with entries for all lines |
|
187 | 187 | which generated output. |
|
188 | 188 | |
|
189 | 189 | * Directory history: |
|
190 | 190 | |
|
191 | 191 | Your history of visited directories is kept in the global list _dh, and the |
|
192 | 192 | magic %cd command can be used to go to any entry in that list. |
|
193 | 193 | |
|
194 | 194 | * Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython) |
|
195 | 195 | |
|
196 | 196 | 1. Auto-parentheses |
|
197 | 197 | |
|
198 | 198 | Callable objects (i.e. functions, methods, etc) can be invoked like |
|
199 | 199 | this (notice the commas between the arguments):: |
|
200 | 200 | |
|
201 | 201 | In [1]: callable_ob arg1, arg2, arg3 |
|
202 | 202 | |
|
203 | 203 | and the input will be translated to this:: |
|
204 | 204 | |
|
205 | 205 | callable_ob(arg1, arg2, arg3) |
|
206 | 206 | |
|
207 | 207 | This feature is off by default (in rare cases it can produce |
|
208 | 208 | undesirable side-effects), but you can activate it at the command-line |
|
209 | 209 | by starting IPython with `--autocall 1`, set it permanently in your |
|
210 | 210 | configuration file, or turn on at runtime with `%autocall 1`. |
|
211 | 211 | |
|
212 | 212 | You can force auto-parentheses by using '/' as the first character |
|
213 | 213 | of a line. For example:: |
|
214 | 214 | |
|
215 | 215 | In [1]: /globals # becomes 'globals()' |
|
216 | 216 | |
|
217 | 217 | Note that the '/' MUST be the first character on the line! This |
|
218 | 218 | won't work:: |
|
219 | 219 | |
|
220 | 220 | In [2]: print /globals # syntax error |
|
221 | 221 | |
|
222 | 222 | In most cases the automatic algorithm should work, so you should |
|
223 | 223 | rarely need to explicitly invoke /. One notable exception is if you |
|
224 | 224 | are trying to call a function with a list of tuples as arguments (the |
|
225 | 225 | parenthesis will confuse IPython):: |
|
226 | 226 | |
|
227 | 227 | In [1]: zip (1,2,3),(4,5,6) # won't work |
|
228 | 228 | |
|
229 | 229 | but this will work:: |
|
230 | 230 | |
|
231 | 231 | In [2]: /zip (1,2,3),(4,5,6) |
|
232 | 232 | ------> zip ((1,2,3),(4,5,6)) |
|
233 | 233 | Out[2]= [(1, 4), (2, 5), (3, 6)] |
|
234 | 234 | |
|
235 | 235 | IPython tells you that it has altered your command line by |
|
236 | 236 | displaying the new command line preceded by -->. e.g.:: |
|
237 | 237 | |
|
238 | 238 | In [18]: callable list |
|
239 | 239 | -------> callable (list) |
|
240 | 240 | |
|
241 | 241 | 2. Auto-Quoting |
|
242 | 242 | |
|
243 | 243 | You can force auto-quoting of a function's arguments by using ',' as |
|
244 | 244 | the first character of a line. For example:: |
|
245 | 245 | |
|
246 | 246 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
247 | 247 | |
|
248 | 248 | If you use ';' instead, the whole argument is quoted as a single |
|
249 | 249 | string (while ',' splits on whitespace):: |
|
250 | 250 | |
|
251 | 251 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
252 | 252 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
253 | 253 | |
|
254 | 254 | Note that the ',' MUST be the first character on the line! This |
|
255 | 255 | won't work:: |
|
256 | 256 | |
|
257 | 257 | In [4]: x = ,my_function /home/me # syntax error |
|
258 | 258 | """ |
|
259 | 259 | |
|
260 | 260 | interactive_usage_min = """\ |
|
261 | 261 | An enhanced console for Python. |
|
262 | 262 | Some of its features are: |
|
263 | 263 | - Tab completion in the local namespace. |
|
264 | 264 | - Logging of input, see command-line options. |
|
265 | 265 | - System shell escape via ! , eg !ls. |
|
266 | 266 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
267 | 267 | - Keeps track of locally defined variables via %who, %whos. |
|
268 | 268 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
269 | 269 | """ |
|
270 | 270 | |
|
271 | 271 | quick_reference = r""" |
|
272 | 272 | IPython -- An enhanced Interactive Python - Quick Reference Card |
|
273 | 273 | ================================================================ |
|
274 | 274 | |
|
275 | 275 | obj?, obj?? : Get help, or more help for object (also works as |
|
276 | 276 | ?obj, ??obj). |
|
277 | 277 | ?foo.*abc* : List names in 'foo' containing 'abc' in them. |
|
278 | 278 | %magic : Information about IPython's 'magic' % functions. |
|
279 | 279 | |
|
280 | 280 | Magic functions are prefixed by % or %%, and typically take their arguments |
|
281 | 281 | without parentheses, quotes or even commas for convenience. Line magics take a |
|
282 | 282 | single % and cell magics are prefixed with two %%. |
|
283 | 283 | |
|
284 | 284 | Example magic function calls: |
|
285 | 285 | |
|
286 | 286 | %alias d ls -F : 'd' is now an alias for 'ls -F' |
|
287 | 287 | alias d ls -F : Works if 'alias' not a python name |
|
288 | 288 | alist = %alias : Get list of aliases to 'alist' |
|
289 | 289 | cd /usr/share : Obvious. cd -<tab> to choose from visited dirs. |
|
290 | 290 | %cd?? : See help AND source for magic %cd |
|
291 | 291 | %timeit x=10 : time the 'x=10' statement with high precision. |
|
292 | 292 | %%timeit x=2**100 |
|
293 | 293 | x**100 : time 'x**100' with a setup of 'x=2**100'; setup code is not |
|
294 | 294 | counted. This is an example of a cell magic. |
|
295 | 295 | |
|
296 | 296 | System commands: |
|
297 | 297 | |
|
298 | 298 | !cp a.txt b/ : System command escape, calls os.system() |
|
299 | 299 | cp a.txt b/ : after %rehashx, most system commands work without ! |
|
300 | 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 | 302 | files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc' |
|
303 | 303 | |
|
304 | 304 | History: |
|
305 | 305 | |
|
306 | 306 | _i, _ii, _iii : Previous, next previous, next next previous input |
|
307 | 307 | _i4, _ih[2:5] : Input history line 4, lines 2-4 |
|
308 | 308 | exec _i81 : Execute input history line #81 again |
|
309 | 309 | %rep 81 : Edit input history line #81 |
|
310 | 310 | _, __, ___ : previous, next previous, next next previous output |
|
311 | 311 | _dh : Directory history |
|
312 | 312 | _oh : Output history |
|
313 | 313 | %hist : Command history of current session. |
|
314 | 314 | %hist -g foo : Search command history of (almost) all sessions for 'foo'. |
|
315 | 315 | %hist -g : Command history of (almost) all sessions. |
|
316 | 316 | %hist 1/2-8 : Command history containing lines 2-8 of session 1. |
|
317 | 317 | %hist 1/ ~2/ : Command history of session 1 and 2 sessions before current. |
|
318 | 318 | %hist ~8/1-~6/5 : Command history from line 1 of 8 sessions ago to |
|
319 | 319 | line 5 of 6 sessions ago. |
|
320 | 320 | %edit 0/ : Open editor to execute code with history of current session. |
|
321 | 321 | |
|
322 | 322 | Autocall: |
|
323 | 323 | |
|
324 | 324 | f 1,2 : f(1,2) # Off by default, enable with %autocall magic. |
|
325 | 325 | /f 1,2 : f(1,2) (forced autoparen) |
|
326 | 326 | ,f 1 2 : f("1","2") |
|
327 | 327 | ;f 1 2 : f("1 2") |
|
328 | 328 | |
|
329 | 329 | Remember: TAB completion works in many contexts, not just file names |
|
330 | 330 | or python names. |
|
331 | 331 | |
|
332 | 332 | The following magic functions are currently available: |
|
333 | 333 | |
|
334 | 334 | """ |
|
335 | 335 | |
|
336 | 336 | default_banner_parts = ["Python %s\n"%sys.version.split("\n")[0], |
|
337 | 337 | "Type 'copyright', 'credits' or 'license' for more information\n" , |
|
338 | 338 | "IPython {version} -- An enhanced Interactive Python. Type '?' for help.\n".format(version=release.version), |
|
339 | 339 | ] |
|
340 | 340 | |
|
341 | 341 | default_banner = ''.join(default_banner_parts) |
|
342 | 342 | |
|
343 | 343 | # deprecated GUI banner |
|
344 | 344 | |
|
345 | 345 | default_gui_banner = '\n'.join([ |
|
346 | 346 | 'DEPRECATED: IPython.core.usage.default_gui_banner is deprecated and will be removed', |
|
347 | 347 | default_banner, |
|
348 | 348 | ]) |
@@ -1,347 +1,347 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Provides a reload() function that acts recursively. |
|
4 | 4 | |
|
5 | 5 | Python's normal :func:`python:reload` function only reloads the module that it's |
|
6 | 6 | passed. The :func:`reload` function in this module also reloads everything |
|
7 | 7 | imported from that module, which is useful when you're changing files deep |
|
8 | 8 | inside a package. |
|
9 | 9 | |
|
10 | 10 | To use this as your default reload function, type this for Python 2:: |
|
11 | 11 | |
|
12 | 12 | import __builtin__ |
|
13 | 13 | from IPython.lib import deepreload |
|
14 | 14 | __builtin__.reload = deepreload.reload |
|
15 | 15 | |
|
16 | 16 | Or this for Python 3:: |
|
17 | 17 | |
|
18 | 18 | import builtins |
|
19 | 19 | from IPython.lib import deepreload |
|
20 | 20 | builtins.reload = deepreload.reload |
|
21 | 21 | |
|
22 | 22 | A reference to the original :func:`python:reload` is stored in this module as |
|
23 | 23 | :data:`original_reload`, so you can restore it later. |
|
24 | 24 | |
|
25 | 25 | This code is almost entirely based on knee.py, which is a Python |
|
26 | 26 | re-implementation of hierarchical module import. |
|
27 | 27 | """ |
|
28 | 28 | #***************************************************************************** |
|
29 | 29 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
30 | 30 | # |
|
31 | 31 | # Distributed under the terms of the BSD License. The full license is in |
|
32 | 32 | # the file COPYING, distributed as part of this software. |
|
33 | 33 | #***************************************************************************** |
|
34 | 34 | |
|
35 | 35 | import builtins as builtin_mod |
|
36 | 36 | from contextlib import contextmanager |
|
37 | 37 | import imp |
|
38 | 38 | import sys |
|
39 | 39 | |
|
40 | 40 | from types import ModuleType |
|
41 | 41 | from warnings import warn |
|
42 | 42 | import types |
|
43 | 43 | |
|
44 | 44 | original_import = builtin_mod.__import__ |
|
45 | 45 | |
|
46 | 46 | @contextmanager |
|
47 | 47 | def replace_import_hook(new_import): |
|
48 | 48 | saved_import = builtin_mod.__import__ |
|
49 | 49 | builtin_mod.__import__ = new_import |
|
50 | 50 | try: |
|
51 | 51 | yield |
|
52 | 52 | finally: |
|
53 | 53 | builtin_mod.__import__ = saved_import |
|
54 | 54 | |
|
55 | 55 | def get_parent(globals, level): |
|
56 | 56 | """ |
|
57 | 57 | parent, name = get_parent(globals, level) |
|
58 | 58 | |
|
59 | 59 | Return the package that an import is being performed in. If globals comes |
|
60 | 60 | from the module foo.bar.bat (not itself a package), this returns the |
|
61 | 61 | sys.modules entry for foo.bar. If globals is from a package's __init__.py, |
|
62 | 62 | the package's entry in sys.modules is returned. |
|
63 | 63 | |
|
64 | 64 | If globals doesn't come from a package or a module in a package, or a |
|
65 | 65 | corresponding entry is not found in sys.modules, None is returned. |
|
66 | 66 | """ |
|
67 | 67 | orig_level = level |
|
68 | 68 | |
|
69 | 69 | if not level or not isinstance(globals, dict): |
|
70 | 70 | return None, '' |
|
71 | 71 | |
|
72 | 72 | pkgname = globals.get('__package__', None) |
|
73 | 73 | |
|
74 | 74 | if pkgname is not None: |
|
75 | 75 | # __package__ is set, so use it |
|
76 | 76 | if not hasattr(pkgname, 'rindex'): |
|
77 | 77 | raise ValueError('__package__ set to non-string') |
|
78 | 78 | if len(pkgname) == 0: |
|
79 | 79 | if level > 0: |
|
80 | 80 | raise ValueError('Attempted relative import in non-package') |
|
81 | 81 | return None, '' |
|
82 | 82 | name = pkgname |
|
83 | 83 | else: |
|
84 | 84 | # __package__ not set, so figure it out and set it |
|
85 | 85 | if '__name__' not in globals: |
|
86 | 86 | return None, '' |
|
87 | 87 | modname = globals['__name__'] |
|
88 | 88 | |
|
89 | 89 | if '__path__' in globals: |
|
90 | 90 | # __path__ is set, so modname is already the package name |
|
91 | 91 | globals['__package__'] = name = modname |
|
92 | 92 | else: |
|
93 | 93 | # Normal module, so work out the package name if any |
|
94 | 94 | lastdot = modname.rfind('.') |
|
95 | 95 | if lastdot < 0 < level: |
|
96 | 96 | raise ValueError("Attempted relative import in non-package") |
|
97 | 97 | if lastdot < 0: |
|
98 | 98 | globals['__package__'] = None |
|
99 | 99 | return None, '' |
|
100 | 100 | globals['__package__'] = name = modname[:lastdot] |
|
101 | 101 | |
|
102 | 102 | dot = len(name) |
|
103 | 103 | for x in range(level, 1, -1): |
|
104 | 104 | try: |
|
105 | 105 | dot = name.rindex('.', 0, dot) |
|
106 | 106 | except ValueError: |
|
107 | 107 | raise ValueError("attempted relative import beyond top-level " |
|
108 | 108 | "package") |
|
109 | 109 | name = name[:dot] |
|
110 | 110 | |
|
111 | 111 | try: |
|
112 | 112 | parent = sys.modules[name] |
|
113 | 113 | except: |
|
114 | 114 | if orig_level < 1: |
|
115 | 115 | warn("Parent module '%.200s' not found while handling absolute " |
|
116 | 116 | "import" % name) |
|
117 | 117 | parent = None |
|
118 | 118 | else: |
|
119 | 119 | raise SystemError("Parent module '%.200s' not loaded, cannot " |
|
120 | 120 | "perform relative import" % name) |
|
121 | 121 | |
|
122 | 122 | # We expect, but can't guarantee, if parent != None, that: |
|
123 | 123 | # - parent.__name__ == name |
|
124 | 124 | # - parent.__dict__ is globals |
|
125 | 125 | # If this is violated... Who cares? |
|
126 | 126 | return parent, name |
|
127 | 127 | |
|
128 | 128 | def load_next(mod, altmod, name, buf): |
|
129 | 129 | """ |
|
130 | 130 | mod, name, buf = load_next(mod, altmod, name, buf) |
|
131 | 131 | |
|
132 | 132 | altmod is either None or same as mod |
|
133 | 133 | """ |
|
134 | 134 | |
|
135 | 135 | if len(name) == 0: |
|
136 | 136 | # completely empty module name should only happen in |
|
137 | 137 | # 'from . import' (or '__import__("")') |
|
138 | 138 | return mod, None, buf |
|
139 | 139 | |
|
140 | 140 | dot = name.find('.') |
|
141 | 141 | if dot == 0: |
|
142 | 142 | raise ValueError('Empty module name') |
|
143 | 143 | |
|
144 | 144 | if dot < 0: |
|
145 | 145 | subname = name |
|
146 | 146 | next = None |
|
147 | 147 | else: |
|
148 | 148 | subname = name[:dot] |
|
149 | 149 | next = name[dot+1:] |
|
150 | 150 | |
|
151 | 151 | if buf != '': |
|
152 | 152 | buf += '.' |
|
153 | 153 | buf += subname |
|
154 | 154 | |
|
155 | 155 | result = import_submodule(mod, subname, buf) |
|
156 | 156 | if result is None and mod != altmod: |
|
157 | 157 | result = import_submodule(altmod, subname, subname) |
|
158 | 158 | if result is not None: |
|
159 | 159 | buf = subname |
|
160 | 160 | |
|
161 | 161 | if result is None: |
|
162 | 162 | raise ImportError("No module named %.200s" % name) |
|
163 | 163 | |
|
164 | 164 | return result, next, buf |
|
165 | 165 | |
|
166 | 166 | |
|
167 | 167 | # Need to keep track of what we've already reloaded to prevent cyclic evil |
|
168 | 168 | found_now = {} |
|
169 | 169 | |
|
170 | 170 | def import_submodule(mod, subname, fullname): |
|
171 | 171 | """m = import_submodule(mod, subname, fullname)""" |
|
172 | 172 | # Require: |
|
173 | 173 | # if mod == None: subname == fullname |
|
174 | 174 | # else: mod.__name__ + "." + subname == fullname |
|
175 | 175 | |
|
176 | 176 | global found_now |
|
177 | 177 | if fullname in found_now and fullname in sys.modules: |
|
178 | 178 | m = sys.modules[fullname] |
|
179 | 179 | else: |
|
180 | 180 | print('Reloading', fullname) |
|
181 | 181 | found_now[fullname] = 1 |
|
182 | 182 | oldm = sys.modules.get(fullname, None) |
|
183 | 183 | |
|
184 | 184 | if mod is None: |
|
185 | 185 | path = None |
|
186 | 186 | elif hasattr(mod, '__path__'): |
|
187 | 187 | path = mod.__path__ |
|
188 | 188 | else: |
|
189 | 189 | return None |
|
190 | 190 | |
|
191 | 191 | try: |
|
192 | 192 | # This appears to be necessary on Python 3, because imp.find_module() |
|
193 | 193 | # tries to import standard libraries (like io) itself, and we don't |
|
194 | 194 | # want them to be processed by our deep_import_hook. |
|
195 | 195 | with replace_import_hook(original_import): |
|
196 | 196 | fp, filename, stuff = imp.find_module(subname, path) |
|
197 | 197 | except ImportError: |
|
198 | 198 | return None |
|
199 | 199 | |
|
200 | 200 | try: |
|
201 | 201 | m = imp.load_module(fullname, fp, filename, stuff) |
|
202 | 202 | except: |
|
203 | 203 | # load_module probably removed name from modules because of |
|
204 | 204 | # the error. Put back the original module object. |
|
205 | 205 | if oldm: |
|
206 | 206 | sys.modules[fullname] = oldm |
|
207 | 207 | raise |
|
208 | 208 | finally: |
|
209 | 209 | if fp: fp.close() |
|
210 | 210 | |
|
211 | 211 | add_submodule(mod, m, fullname, subname) |
|
212 | 212 | |
|
213 | 213 | return m |
|
214 | 214 | |
|
215 | 215 | def add_submodule(mod, submod, fullname, subname): |
|
216 | 216 | """mod.{subname} = submod""" |
|
217 | 217 | if mod is None: |
|
218 | 218 | return #Nothing to do here. |
|
219 | 219 | |
|
220 | 220 | if submod is None: |
|
221 | 221 | submod = sys.modules[fullname] |
|
222 | 222 | |
|
223 | 223 | setattr(mod, subname, submod) |
|
224 | 224 | |
|
225 | 225 | return |
|
226 | 226 | |
|
227 | 227 | def ensure_fromlist(mod, fromlist, buf, recursive): |
|
228 | 228 | """Handle 'from module import a, b, c' imports.""" |
|
229 | 229 | if not hasattr(mod, '__path__'): |
|
230 | 230 | return |
|
231 | 231 | for item in fromlist: |
|
232 | 232 | if not hasattr(item, 'rindex'): |
|
233 | 233 | raise TypeError("Item in ``from list'' not a string") |
|
234 | 234 | if item == '*': |
|
235 | 235 | if recursive: |
|
236 | 236 | continue # avoid endless recursion |
|
237 | 237 | try: |
|
238 | 238 | all = mod.__all__ |
|
239 | 239 | except AttributeError: |
|
240 | 240 | pass |
|
241 | 241 | else: |
|
242 | 242 | ret = ensure_fromlist(mod, all, buf, 1) |
|
243 | 243 | if not ret: |
|
244 | 244 | return 0 |
|
245 | 245 | elif not hasattr(mod, item): |
|
246 | 246 | import_submodule(mod, item, buf + '.' + item) |
|
247 | 247 | |
|
248 | 248 | def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1): |
|
249 | 249 | """Replacement for __import__()""" |
|
250 | 250 | parent, buf = get_parent(globals, level) |
|
251 | 251 | |
|
252 | 252 | head, name, buf = load_next(parent, None if level < 0 else parent, name, buf) |
|
253 | 253 | |
|
254 | 254 | tail = head |
|
255 | 255 | while name: |
|
256 | 256 | tail, name, buf = load_next(tail, tail, name, buf) |
|
257 | 257 | |
|
258 | 258 | # If tail is None, both get_parent and load_next found |
|
259 | 259 | # an empty module name: someone called __import__("") or |
|
260 | 260 | # doctored faulty bytecode |
|
261 | 261 | if tail is None: |
|
262 | 262 | raise ValueError('Empty module name') |
|
263 | 263 | |
|
264 | 264 | if not fromlist: |
|
265 | 265 | return head |
|
266 | 266 | |
|
267 | 267 | ensure_fromlist(tail, fromlist, buf, 0) |
|
268 | 268 | return tail |
|
269 | 269 | |
|
270 | 270 | modules_reloading = {} |
|
271 | 271 | |
|
272 | 272 | def deep_reload_hook(m): |
|
273 | 273 | """Replacement for reload().""" |
|
274 | 274 | # Hardcode this one as it would raise a NotImplemeentedError from the |
|
275 | 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 | 278 | if m is types: |
|
279 | 279 | return m |
|
280 | 280 | if not isinstance(m, ModuleType): |
|
281 | 281 | raise TypeError("reload() argument must be module") |
|
282 | 282 | |
|
283 | 283 | name = m.__name__ |
|
284 | 284 | |
|
285 | 285 | if name not in sys.modules: |
|
286 | 286 | raise ImportError("reload(): module %.200s not in sys.modules" % name) |
|
287 | 287 | |
|
288 | 288 | global modules_reloading |
|
289 | 289 | try: |
|
290 | 290 | return modules_reloading[name] |
|
291 | 291 | except: |
|
292 | 292 | modules_reloading[name] = m |
|
293 | 293 | |
|
294 | 294 | dot = name.rfind('.') |
|
295 | 295 | if dot < 0: |
|
296 | 296 | subname = name |
|
297 | 297 | path = None |
|
298 | 298 | else: |
|
299 | 299 | try: |
|
300 | 300 | parent = sys.modules[name[:dot]] |
|
301 | 301 | except KeyError: |
|
302 | 302 | modules_reloading.clear() |
|
303 | 303 | raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot]) |
|
304 | 304 | subname = name[dot+1:] |
|
305 | 305 | path = getattr(parent, "__path__", None) |
|
306 | 306 | |
|
307 | 307 | try: |
|
308 | 308 | # This appears to be necessary on Python 3, because imp.find_module() |
|
309 | 309 | # tries to import standard libraries (like io) itself, and we don't |
|
310 | 310 | # want them to be processed by our deep_import_hook. |
|
311 | 311 | with replace_import_hook(original_import): |
|
312 | 312 | fp, filename, stuff = imp.find_module(subname, path) |
|
313 | 313 | finally: |
|
314 | 314 | modules_reloading.clear() |
|
315 | 315 | |
|
316 | 316 | try: |
|
317 | 317 | newm = imp.load_module(name, fp, filename, stuff) |
|
318 | 318 | except: |
|
319 | 319 | # load_module probably removed name from modules because of |
|
320 | 320 | # the error. Put back the original module object. |
|
321 | 321 | sys.modules[name] = m |
|
322 | 322 | raise |
|
323 | 323 | finally: |
|
324 | 324 | if fp: fp.close() |
|
325 | 325 | |
|
326 | 326 | modules_reloading.clear() |
|
327 | 327 | return newm |
|
328 | 328 | |
|
329 | 329 | # Save the original hooks |
|
330 | 330 | original_reload = imp.reload |
|
331 | 331 | |
|
332 | 332 | # Replacement for reload() |
|
333 | 333 | def reload(module, exclude=('sys', 'os.path', 'builtins', '__main__', |
|
334 | 334 | 'numpy', 'numpy._globals')): |
|
335 | 335 | """Recursively reload all modules used in the given module. Optionally |
|
336 | 336 | takes a list of modules to exclude from reloading. The default exclude |
|
337 | 337 | list contains sys, __main__, and __builtin__, to prevent, e.g., resetting |
|
338 | 338 | display, exception, and io hooks. |
|
339 | 339 | """ |
|
340 | 340 | global found_now |
|
341 | 341 | for i in exclude: |
|
342 | 342 | found_now[i] = 1 |
|
343 | 343 | try: |
|
344 | 344 | with replace_import_hook(deep_import_hook): |
|
345 | 345 | return deep_reload_hook(module) |
|
346 | 346 | finally: |
|
347 | 347 | found_now = {} |
@@ -1,667 +1,667 b'' | |||
|
1 | 1 | """Module for interactive demos using IPython. |
|
2 | 2 | |
|
3 | 3 | This module implements a few classes for running Python scripts interactively |
|
4 | 4 | in IPython for demonstrations. With very simple markup (a few tags in |
|
5 | 5 | comments), you can control points where the script stops executing and returns |
|
6 | 6 | control to IPython. |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | Provided classes |
|
10 | 10 | ---------------- |
|
11 | 11 | |
|
12 | 12 | The classes are (see their docstrings for further details): |
|
13 | 13 | |
|
14 | 14 | - Demo: pure python demos |
|
15 | 15 | |
|
16 | 16 | - IPythonDemo: demos with input to be processed by IPython as if it had been |
|
17 | 17 | typed interactively (so magics work, as well as any other special syntax you |
|
18 | 18 | may have added via input prefilters). |
|
19 | 19 | |
|
20 | 20 | - LineDemo: single-line version of the Demo class. These demos are executed |
|
21 | 21 | one line at a time, and require no markup. |
|
22 | 22 | |
|
23 | 23 | - IPythonLineDemo: IPython version of the LineDemo class (the demo is |
|
24 | 24 | executed a line at a time, but processed via IPython). |
|
25 | 25 | |
|
26 | 26 | - ClearMixin: mixin to make Demo classes with less visual clutter. It |
|
27 | 27 | declares an empty marquee and a pre_cmd that clears the screen before each |
|
28 | 28 | block (see Subclassing below). |
|
29 | 29 | |
|
30 | 30 | - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo |
|
31 | 31 | classes. |
|
32 | 32 | |
|
33 | 33 | Inheritance diagram: |
|
34 | 34 | |
|
35 | 35 | .. inheritance-diagram:: IPython.lib.demo |
|
36 | 36 | :parts: 3 |
|
37 | 37 | |
|
38 | 38 | Subclassing |
|
39 | 39 | ----------- |
|
40 | 40 | |
|
41 | 41 | The classes here all include a few methods meant to make customization by |
|
42 | 42 | subclassing more convenient. Their docstrings below have some more details: |
|
43 | 43 | |
|
44 | 44 | - highlight(): format every block and optionally highlight comments and |
|
45 | 45 | docstring content. |
|
46 | 46 | |
|
47 | 47 | - marquee(): generates a marquee to provide visible on-screen markers at each |
|
48 | 48 | block start and end. |
|
49 | 49 | |
|
50 | 50 | - pre_cmd(): run right before the execution of each block. |
|
51 | 51 | |
|
52 | 52 | - post_cmd(): run right after the execution of each block. If the block |
|
53 | 53 | raises an exception, this is NOT called. |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | Operation |
|
57 | 57 | --------- |
|
58 | 58 | |
|
59 | 59 | The file is run in its own empty namespace (though you can pass it a string of |
|
60 | 60 | arguments as if in a command line environment, and it will see those as |
|
61 | 61 | sys.argv). But at each stop, the global IPython namespace is updated with the |
|
62 | 62 | current internal demo namespace, so you can work interactively with the data |
|
63 | 63 | accumulated so far. |
|
64 | 64 | |
|
65 | 65 | By default, each block of code is printed (with syntax highlighting) before |
|
66 | 66 | executing it and you have to confirm execution. This is intended to show the |
|
67 | 67 | code to an audience first so you can discuss it, and only proceed with |
|
68 | 68 | execution once you agree. There are a few tags which allow you to modify this |
|
69 | 69 | behavior. |
|
70 | 70 | |
|
71 | 71 | The supported tags are: |
|
72 | 72 | |
|
73 | 73 | # <demo> stop |
|
74 | 74 | |
|
75 | 75 | Defines block boundaries, the points where IPython stops execution of the |
|
76 | 76 | file and returns to the interactive prompt. |
|
77 | 77 | |
|
78 | 78 | You can optionally mark the stop tag with extra dashes before and after the |
|
79 | 79 | word 'stop', to help visually distinguish the blocks in a text editor: |
|
80 | 80 | |
|
81 | 81 | # <demo> --- stop --- |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | # <demo> silent |
|
85 | 85 | |
|
86 | 86 | Make a block execute silently (and hence automatically). Typically used in |
|
87 | 87 | cases where you have some boilerplate or initialization code which you need |
|
88 | 88 | executed but do not want to be seen in the demo. |
|
89 | 89 | |
|
90 | 90 | # <demo> auto |
|
91 | 91 | |
|
92 | 92 | Make a block execute automatically, but still being printed. Useful for |
|
93 | 93 | simple code which does not warrant discussion, since it avoids the extra |
|
94 | 94 | manual confirmation. |
|
95 | 95 | |
|
96 | 96 | # <demo> auto_all |
|
97 | 97 | |
|
98 | 98 | This tag can _only_ be in the first block, and if given it overrides the |
|
99 | 99 | individual auto tags to make the whole demo fully automatic (no block asks |
|
100 | 100 | for confirmation). It can also be given at creation time (or the attribute |
|
101 | 101 | set later) to override what's in the file. |
|
102 | 102 | |
|
103 | 103 | While _any_ python file can be run as a Demo instance, if there are no stop |
|
104 | 104 | tags the whole file will run in a single block (no different that calling |
|
105 | 105 | first %pycat and then %run). The minimal markup to make this useful is to |
|
106 | 106 | place a set of stop tags; the other tags are only there to let you fine-tune |
|
107 | 107 | the execution. |
|
108 | 108 | |
|
109 | 109 | This is probably best explained with the simple example file below. You can |
|
110 | 110 | copy this into a file named ex_demo.py, and try running it via:: |
|
111 | 111 | |
|
112 | 112 | from IPython.lib.demo import Demo |
|
113 | 113 | d = Demo('ex_demo.py') |
|
114 | 114 | d() |
|
115 | 115 | |
|
116 | 116 | Each time you call the demo object, it runs the next block. The demo object |
|
117 | 117 | has a few useful methods for navigation, like again(), edit(), jump(), seek() |
|
118 | 118 | and back(). It can be reset for a new run via reset() or reloaded from disk |
|
119 | 119 | (in case you've edited the source) via reload(). See their docstrings below. |
|
120 | 120 | |
|
121 | 121 | Note: To make this simpler to explore, a file called "demo-exercizer.py" has |
|
122 | 122 | been added to the "docs/examples/core" directory. Just cd to this directory in |
|
123 | 123 | an IPython session, and type:: |
|
124 | 124 | |
|
125 | 125 | %run demo-exercizer.py |
|
126 | 126 | |
|
127 | 127 | and then follow the directions. |
|
128 | 128 | |
|
129 | 129 | Example |
|
130 | 130 | ------- |
|
131 | 131 | |
|
132 | 132 | The following is a very simple example of a valid demo file. |
|
133 | 133 | |
|
134 | 134 | :: |
|
135 | 135 | |
|
136 | 136 | #################### EXAMPLE DEMO <ex_demo.py> ############################### |
|
137 | 137 | '''A simple interactive demo to illustrate the use of IPython's Demo class.''' |
|
138 | 138 | |
|
139 | 139 | print 'Hello, welcome to an interactive IPython demo.' |
|
140 | 140 | |
|
141 | 141 | # The mark below defines a block boundary, which is a point where IPython will |
|
142 | 142 | # stop execution and return to the interactive prompt. The dashes are actually |
|
143 | 143 | # optional and used only as a visual aid to clearly separate blocks while |
|
144 | 144 | # editing the demo code. |
|
145 | 145 | # <demo> stop |
|
146 | 146 | |
|
147 | 147 | x = 1 |
|
148 | 148 | y = 2 |
|
149 | 149 | |
|
150 | 150 | # <demo> stop |
|
151 | 151 | |
|
152 | 152 | # the mark below makes this block as silent |
|
153 | 153 | # <demo> silent |
|
154 | 154 | |
|
155 | 155 | print 'This is a silent block, which gets executed but not printed.' |
|
156 | 156 | |
|
157 | 157 | # <demo> stop |
|
158 | 158 | # <demo> auto |
|
159 | 159 | print 'This is an automatic block.' |
|
160 | 160 | print 'It is executed without asking for confirmation, but printed.' |
|
161 | 161 | z = x+y |
|
162 | 162 | |
|
163 | 163 | print 'z=',x |
|
164 | 164 | |
|
165 | 165 | # <demo> stop |
|
166 | 166 | # This is just another normal block. |
|
167 | 167 | print 'z is now:', z |
|
168 | 168 | |
|
169 | 169 | print 'bye!' |
|
170 | 170 | ################### END EXAMPLE DEMO <ex_demo.py> ############################ |
|
171 | 171 | """ |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | #***************************************************************************** |
|
175 | 175 | # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu> |
|
176 | 176 | # |
|
177 | 177 | # Distributed under the terms of the BSD License. The full license is in |
|
178 | 178 | # the file COPYING, distributed as part of this software. |
|
179 | 179 | # |
|
180 | 180 | #***************************************************************************** |
|
181 | 181 | |
|
182 | 182 | import os |
|
183 | 183 | import re |
|
184 | 184 | import shlex |
|
185 | 185 | import sys |
|
186 | 186 | import pygments |
|
187 | 187 | |
|
188 | 188 | from IPython.utils.text import marquee |
|
189 | 189 | from IPython.utils import openpy |
|
190 | 190 | from IPython.utils import py3compat |
|
191 | 191 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
192 | 192 | |
|
193 | 193 | class DemoError(Exception): pass |
|
194 | 194 | |
|
195 | 195 | def re_mark(mark): |
|
196 | 196 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
|
197 | 197 | |
|
198 | 198 | class Demo(object): |
|
199 | 199 | |
|
200 | 200 | re_stop = re_mark('-*\s?stop\s?-*') |
|
201 | 201 | re_silent = re_mark('silent') |
|
202 | 202 | re_auto = re_mark('auto') |
|
203 | 203 | re_auto_all = re_mark('auto_all') |
|
204 | 204 | |
|
205 | 205 | def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False, |
|
206 | 206 | formatter='terminal', style='default'): |
|
207 | 207 | """Make a new demo object. To run the demo, simply call the object. |
|
208 | 208 | |
|
209 | 209 | See the module docstring for full details and an example (you can use |
|
210 | 210 | IPython.Demo? in IPython to see it). |
|
211 | 211 | |
|
212 | 212 | Inputs: |
|
213 | 213 | |
|
214 | 214 | - src is either a file, or file-like object, or a |
|
215 | 215 | string that can be resolved to a filename. |
|
216 | 216 | |
|
217 | 217 | Optional inputs: |
|
218 | 218 | |
|
219 | 219 | - title: a string to use as the demo name. Of most use when the demo |
|
220 | 220 | you are making comes from an object that has no filename, or if you |
|
221 | 221 | want an alternate denotation distinct from the filename. |
|
222 | 222 | |
|
223 | 223 | - arg_str(''): a string of arguments, internally converted to a list |
|
224 | 224 | just like sys.argv, so the demo script can see a similar |
|
225 | 225 | environment. |
|
226 | 226 | |
|
227 | 227 | - auto_all(None): global flag to run all blocks automatically without |
|
228 | 228 | confirmation. This attribute overrides the block-level tags and |
|
229 | 229 | applies to the whole demo. It is an attribute of the object, and |
|
230 | 230 | can be changed at runtime simply by reassigning it to a boolean |
|
231 | 231 | value. |
|
232 | 232 | |
|
233 | 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 | 236 | - formatter('terminal'): a string of pygments formatter name to be |
|
237 | 237 | used. Useful values for terminals: terminal, terminal256, |
|
238 | 238 | terminal16m |
|
239 | 239 | |
|
240 | 240 | - style('default'): a string of pygments style name to be used. |
|
241 | 241 | """ |
|
242 | 242 | if hasattr(src, "read"): |
|
243 | 243 | # It seems to be a file or a file-like object |
|
244 | 244 | self.fname = "from a file-like object" |
|
245 | 245 | if title == '': |
|
246 | 246 | self.title = "from a file-like object" |
|
247 | 247 | else: |
|
248 | 248 | self.title = title |
|
249 | 249 | else: |
|
250 | 250 | # Assume it's a string or something that can be converted to one |
|
251 | 251 | self.fname = src |
|
252 | 252 | if title == '': |
|
253 | 253 | (filepath, filename) = os.path.split(src) |
|
254 | 254 | self.title = filename |
|
255 | 255 | else: |
|
256 | 256 | self.title = title |
|
257 | 257 | self.sys_argv = [src] + shlex.split(arg_str) |
|
258 | 258 | self.auto_all = auto_all |
|
259 | 259 | self.src = src |
|
260 | 260 | |
|
261 | 261 | self.inside_ipython = "get_ipython" in globals() |
|
262 | 262 | if self.inside_ipython: |
|
263 | 263 | # get a few things from ipython. While it's a bit ugly design-wise, |
|
264 | 264 | # it ensures that things like color scheme and the like are always in |
|
265 | 265 | # sync with the ipython mode being used. This class is only meant to |
|
266 | 266 | # be used inside ipython anyways, so it's OK. |
|
267 | 267 | ip = get_ipython() # this is in builtins whenever IPython is running |
|
268 | 268 | self.ip_ns = ip.user_ns |
|
269 | 269 | self.ip_colorize = ip.pycolorize |
|
270 | 270 | self.ip_showtb = ip.showtraceback |
|
271 | 271 | self.ip_run_cell = ip.run_cell |
|
272 | 272 | self.shell = ip |
|
273 | 273 | |
|
274 | 274 | self.formatter = pygments.formatters.get_formatter_by_name(formatter, |
|
275 | 275 | style=style) |
|
276 | 276 | self.python_lexer = pygments.lexers.get_lexer_by_name("py3") |
|
277 | 277 | self.format_rst = format_rst |
|
278 | 278 | if format_rst: |
|
279 | 279 | self.rst_lexer = pygments.lexers.get_lexer_by_name("rst") |
|
280 | 280 | |
|
281 | 281 | # load user data and initialize data structures |
|
282 | 282 | self.reload() |
|
283 | 283 | |
|
284 | 284 | def fload(self): |
|
285 | 285 | """Load file object.""" |
|
286 | 286 | # read data and parse into blocks |
|
287 | 287 | if hasattr(self, 'fobj') and self.fobj is not None: |
|
288 | 288 | self.fobj.close() |
|
289 | 289 | if hasattr(self.src, "read"): |
|
290 | 290 | # It seems to be a file or a file-like object |
|
291 | 291 | self.fobj = self.src |
|
292 | 292 | else: |
|
293 | 293 | # Assume it's a string or something that can be converted to one |
|
294 | 294 | self.fobj = openpy.open(self.fname) |
|
295 | 295 | |
|
296 | 296 | def reload(self): |
|
297 | 297 | """Reload source from disk and initialize state.""" |
|
298 | 298 | self.fload() |
|
299 | 299 | |
|
300 | 300 | self.src = "".join(openpy.strip_encoding_cookie(self.fobj)) |
|
301 | 301 | src_b = [b.strip() for b in self.re_stop.split(self.src) if b] |
|
302 | 302 | self._silent = [bool(self.re_silent.findall(b)) for b in src_b] |
|
303 | 303 | self._auto = [bool(self.re_auto.findall(b)) for b in src_b] |
|
304 | 304 | |
|
305 | 305 | # if auto_all is not given (def. None), we read it from the file |
|
306 | 306 | if self.auto_all is None: |
|
307 | 307 | self.auto_all = bool(self.re_auto_all.findall(src_b[0])) |
|
308 | 308 | else: |
|
309 | 309 | self.auto_all = bool(self.auto_all) |
|
310 | 310 | |
|
311 | 311 | # Clean the sources from all markup so it doesn't get displayed when |
|
312 | 312 | # running the demo |
|
313 | 313 | src_blocks = [] |
|
314 | 314 | auto_strip = lambda s: self.re_auto.sub('',s) |
|
315 | 315 | for i,b in enumerate(src_b): |
|
316 | 316 | if self._auto[i]: |
|
317 | 317 | src_blocks.append(auto_strip(b)) |
|
318 | 318 | else: |
|
319 | 319 | src_blocks.append(b) |
|
320 | 320 | # remove the auto_all marker |
|
321 | 321 | src_blocks[0] = self.re_auto_all.sub('',src_blocks[0]) |
|
322 | 322 | |
|
323 | 323 | self.nblocks = len(src_blocks) |
|
324 | 324 | self.src_blocks = src_blocks |
|
325 | 325 | |
|
326 | 326 | # also build syntax-highlighted source |
|
327 | 327 | self.src_blocks_colored = list(map(self.highlight,self.src_blocks)) |
|
328 | 328 | |
|
329 | 329 | # ensure clean namespace and seek offset |
|
330 | 330 | self.reset() |
|
331 | 331 | |
|
332 | 332 | def reset(self): |
|
333 | 333 | """Reset the namespace and seek pointer to restart the demo""" |
|
334 | 334 | self.user_ns = {} |
|
335 | 335 | self.finished = False |
|
336 | 336 | self.block_index = 0 |
|
337 | 337 | |
|
338 | 338 | def _validate_index(self,index): |
|
339 | 339 | if index<0 or index>=self.nblocks: |
|
340 | 340 | raise ValueError('invalid block index %s' % index) |
|
341 | 341 | |
|
342 | 342 | def _get_index(self,index): |
|
343 | 343 | """Get the current block index, validating and checking status. |
|
344 | 344 | |
|
345 | 345 | Returns None if the demo is finished""" |
|
346 | 346 | |
|
347 | 347 | if index is None: |
|
348 | 348 | if self.finished: |
|
349 | 349 | print('Demo finished. Use <demo_name>.reset() if you want to rerun it.') |
|
350 | 350 | return None |
|
351 | 351 | index = self.block_index |
|
352 | 352 | else: |
|
353 | 353 | self._validate_index(index) |
|
354 | 354 | return index |
|
355 | 355 | |
|
356 | 356 | def seek(self,index): |
|
357 | 357 | """Move the current seek pointer to the given block. |
|
358 | 358 | |
|
359 | 359 | You can use negative indices to seek from the end, with identical |
|
360 | 360 | semantics to those of Python lists.""" |
|
361 | 361 | if index<0: |
|
362 | 362 | index = self.nblocks + index |
|
363 | 363 | self._validate_index(index) |
|
364 | 364 | self.block_index = index |
|
365 | 365 | self.finished = False |
|
366 | 366 | |
|
367 | 367 | def back(self,num=1): |
|
368 | 368 | """Move the seek pointer back num blocks (default is 1).""" |
|
369 | 369 | self.seek(self.block_index-num) |
|
370 | 370 | |
|
371 | 371 | def jump(self,num=1): |
|
372 | 372 | """Jump a given number of blocks relative to the current one. |
|
373 | 373 | |
|
374 | 374 | The offset can be positive or negative, defaults to 1.""" |
|
375 | 375 | self.seek(self.block_index+num) |
|
376 | 376 | |
|
377 | 377 | def again(self): |
|
378 | 378 | """Move the seek pointer back one block and re-execute.""" |
|
379 | 379 | self.back(1) |
|
380 | 380 | self() |
|
381 | 381 | |
|
382 | 382 | def edit(self,index=None): |
|
383 | 383 | """Edit a block. |
|
384 | 384 | |
|
385 | 385 | If no number is given, use the last block executed. |
|
386 | 386 | |
|
387 | 387 | This edits the in-memory copy of the demo, it does NOT modify the |
|
388 | 388 | original source file. If you want to do that, simply open the file in |
|
389 | 389 | an editor and use reload() when you make changes to the file. This |
|
390 | 390 | method is meant to let you change a block during a demonstration for |
|
391 | 391 | explanatory purposes, without damaging your original script.""" |
|
392 | 392 | |
|
393 | 393 | index = self._get_index(index) |
|
394 | 394 | if index is None: |
|
395 | 395 | return |
|
396 | 396 | # decrease the index by one (unless we're at the very beginning), so |
|
397 | 397 | # that the default demo.edit() call opens up the sblock we've last run |
|
398 | 398 | if index>0: |
|
399 | 399 | index -= 1 |
|
400 | 400 | |
|
401 | 401 | filename = self.shell.mktempfile(self.src_blocks[index]) |
|
402 | 402 | self.shell.hooks.editor(filename,1) |
|
403 | 403 | with open(filename, 'r') as f: |
|
404 | 404 | new_block = f.read() |
|
405 | 405 | # update the source and colored block |
|
406 | 406 | self.src_blocks[index] = new_block |
|
407 | 407 | self.src_blocks_colored[index] = self.highlight(new_block) |
|
408 | 408 | self.block_index = index |
|
409 | 409 | # call to run with the newly edited index |
|
410 | 410 | self() |
|
411 | 411 | |
|
412 | 412 | def show(self,index=None): |
|
413 | 413 | """Show a single block on screen""" |
|
414 | 414 | |
|
415 | 415 | index = self._get_index(index) |
|
416 | 416 | if index is None: |
|
417 | 417 | return |
|
418 | 418 | |
|
419 | 419 | print(self.marquee('<%s> block # %s (%s remaining)' % |
|
420 | 420 | (self.title,index,self.nblocks-index-1))) |
|
421 | 421 | print(self.src_blocks_colored[index]) |
|
422 | 422 | sys.stdout.flush() |
|
423 | 423 | |
|
424 | 424 | def show_all(self): |
|
425 | 425 | """Show entire demo on screen, block by block""" |
|
426 | 426 | |
|
427 | 427 | fname = self.title |
|
428 | 428 | title = self.title |
|
429 | 429 | nblocks = self.nblocks |
|
430 | 430 | silent = self._silent |
|
431 | 431 | marquee = self.marquee |
|
432 | 432 | for index,block in enumerate(self.src_blocks_colored): |
|
433 | 433 | if silent[index]: |
|
434 | 434 | print(marquee('<%s> SILENT block # %s (%s remaining)' % |
|
435 | 435 | (title,index,nblocks-index-1))) |
|
436 | 436 | else: |
|
437 | 437 | print(marquee('<%s> block # %s (%s remaining)' % |
|
438 | 438 | (title,index,nblocks-index-1))) |
|
439 | 439 | print(block, end=' ') |
|
440 | 440 | sys.stdout.flush() |
|
441 | 441 | |
|
442 | 442 | def run_cell(self,source): |
|
443 | 443 | """Execute a string with one or more lines of code""" |
|
444 | 444 | |
|
445 | 445 | exec(source, self.user_ns) |
|
446 | 446 | |
|
447 | 447 | def __call__(self,index=None): |
|
448 | 448 | """run a block of the demo. |
|
449 | 449 | |
|
450 | 450 | If index is given, it should be an integer >=1 and <= nblocks. This |
|
451 | 451 | means that the calling convention is one off from typical Python |
|
452 | 452 | lists. The reason for the inconsistency is that the demo always |
|
453 | 453 | prints 'Block n/N, and N is the total, so it would be very odd to use |
|
454 | 454 | zero-indexing here.""" |
|
455 | 455 | |
|
456 | 456 | index = self._get_index(index) |
|
457 | 457 | if index is None: |
|
458 | 458 | return |
|
459 | 459 | try: |
|
460 | 460 | marquee = self.marquee |
|
461 | 461 | next_block = self.src_blocks[index] |
|
462 | 462 | self.block_index += 1 |
|
463 | 463 | if self._silent[index]: |
|
464 | 464 | print(marquee('Executing silent block # %s (%s remaining)' % |
|
465 | 465 | (index,self.nblocks-index-1))) |
|
466 | 466 | else: |
|
467 | 467 | self.pre_cmd() |
|
468 | 468 | self.show(index) |
|
469 | 469 | if self.auto_all or self._auto[index]: |
|
470 | 470 | print(marquee('output:')) |
|
471 | 471 | else: |
|
472 | 472 | print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ') |
|
473 | 473 | ans = py3compat.input().strip() |
|
474 | 474 | if ans: |
|
475 | 475 | print(marquee('Block NOT executed')) |
|
476 | 476 | return |
|
477 | 477 | try: |
|
478 | 478 | save_argv = sys.argv |
|
479 | 479 | sys.argv = self.sys_argv |
|
480 | 480 | self.run_cell(next_block) |
|
481 | 481 | self.post_cmd() |
|
482 | 482 | finally: |
|
483 | 483 | sys.argv = save_argv |
|
484 | 484 | |
|
485 | 485 | except: |
|
486 | 486 | if self.inside_ipython: |
|
487 | 487 | self.ip_showtb(filename=self.fname) |
|
488 | 488 | else: |
|
489 | 489 | if self.inside_ipython: |
|
490 | 490 | self.ip_ns.update(self.user_ns) |
|
491 | 491 | |
|
492 | 492 | if self.block_index == self.nblocks: |
|
493 | 493 | mq1 = self.marquee('END OF DEMO') |
|
494 | 494 | if mq1: |
|
495 | 495 | # avoid spurious print if empty marquees are used |
|
496 | 496 | print() |
|
497 | 497 | print(mq1) |
|
498 | 498 | print(self.marquee('Use <demo_name>.reset() if you want to rerun it.')) |
|
499 | 499 | self.finished = True |
|
500 | 500 | |
|
501 | 501 | # These methods are meant to be overridden by subclasses who may wish to |
|
502 | 502 | # customize the behavior of of their demos. |
|
503 | 503 | def marquee(self,txt='',width=78,mark='*'): |
|
504 | 504 | """Return the input string centered in a 'marquee'.""" |
|
505 | 505 | return marquee(txt,width,mark) |
|
506 | 506 | |
|
507 | 507 | def pre_cmd(self): |
|
508 | 508 | """Method called before executing each block.""" |
|
509 | 509 | pass |
|
510 | 510 | |
|
511 | 511 | def post_cmd(self): |
|
512 | 512 | """Method called after executing each block.""" |
|
513 | 513 | pass |
|
514 | 514 | |
|
515 | 515 | def highlight(self, block): |
|
516 | 516 | """Method called on each block to highlight it content""" |
|
517 | 517 | tokens = pygments.lex(block, self.python_lexer) |
|
518 | 518 | if self.format_rst: |
|
519 | 519 | from pygments.token import Token |
|
520 | 520 | toks = [] |
|
521 | 521 | for token in tokens: |
|
522 | 522 | if token[0] == Token.String.Doc and len(token[1]) > 6: |
|
523 | 523 | toks += pygments.lex(token[1][:3], self.python_lexer) |
|
524 | 524 | # parse doc string content by rst lexer |
|
525 | 525 | toks += pygments.lex(token[1][3:-3], self.rst_lexer) |
|
526 | 526 | toks += pygments.lex(token[1][-3:], self.python_lexer) |
|
527 | 527 | elif token[0] == Token.Comment.Single: |
|
528 | 528 | toks.append((Token.Comment.Single, token[1][0])) |
|
529 | 529 | # parse comment content by rst lexer |
|
530 | 530 | # remove the extrat newline added by rst lexer |
|
531 | 531 | toks += list(pygments.lex(token[1][1:], self.rst_lexer))[:-1] |
|
532 | 532 | else: |
|
533 | 533 | toks.append(token) |
|
534 | 534 | tokens = toks |
|
535 | 535 | return pygments.format(tokens, self.formatter) |
|
536 | 536 | |
|
537 | 537 | |
|
538 | 538 | class IPythonDemo(Demo): |
|
539 | 539 | """Class for interactive demos with IPython's input processing applied. |
|
540 | 540 | |
|
541 | 541 | This subclasses Demo, but instead of executing each block by the Python |
|
542 | 542 | interpreter (via exec), it actually calls IPython on it, so that any input |
|
543 | 543 | filters which may be in place are applied to the input block. |
|
544 | 544 | |
|
545 | 545 | If you have an interactive environment which exposes special input |
|
546 | 546 | processing, you can use this class instead to write demo scripts which |
|
547 | 547 | operate exactly as if you had typed them interactively. The default Demo |
|
548 | 548 | class requires the input to be valid, pure Python code. |
|
549 | 549 | """ |
|
550 | 550 | |
|
551 | 551 | def run_cell(self,source): |
|
552 | 552 | """Execute a string with one or more lines of code""" |
|
553 | 553 | |
|
554 | 554 | self.shell.run_cell(source) |
|
555 | 555 | |
|
556 | 556 | class LineDemo(Demo): |
|
557 | 557 | """Demo where each line is executed as a separate block. |
|
558 | 558 | |
|
559 | 559 | The input script should be valid Python code. |
|
560 | 560 | |
|
561 | 561 | This class doesn't require any markup at all, and it's meant for simple |
|
562 | 562 | scripts (with no nesting or any kind of indentation) which consist of |
|
563 | 563 | multiple lines of input to be executed, one at a time, as if they had been |
|
564 | 564 | typed in the interactive prompt. |
|
565 | 565 | |
|
566 | 566 | Note: the input can not have *any* indentation, which means that only |
|
567 | 567 | single-lines of input are accepted, not even function definitions are |
|
568 | 568 | valid.""" |
|
569 | 569 | |
|
570 | 570 | def reload(self): |
|
571 | 571 | """Reload source from disk and initialize state.""" |
|
572 | 572 | # read data and parse into blocks |
|
573 | 573 | self.fload() |
|
574 | 574 | lines = self.fobj.readlines() |
|
575 | 575 | src_b = [l for l in lines if l.strip()] |
|
576 | 576 | nblocks = len(src_b) |
|
577 | 577 | self.src = ''.join(lines) |
|
578 | 578 | self._silent = [False]*nblocks |
|
579 | 579 | self._auto = [True]*nblocks |
|
580 | 580 | self.auto_all = True |
|
581 | 581 | self.nblocks = nblocks |
|
582 | 582 | self.src_blocks = src_b |
|
583 | 583 | |
|
584 | 584 | # also build syntax-highlighted source |
|
585 | 585 | self.src_blocks_colored = list(map(self.highlight,self.src_blocks)) |
|
586 | 586 | |
|
587 | 587 | # ensure clean namespace and seek offset |
|
588 | 588 | self.reset() |
|
589 | 589 | |
|
590 | 590 | |
|
591 | 591 | class IPythonLineDemo(IPythonDemo,LineDemo): |
|
592 | 592 | """Variant of the LineDemo class whose input is processed by IPython.""" |
|
593 | 593 | pass |
|
594 | 594 | |
|
595 | 595 | |
|
596 | 596 | class ClearMixin(object): |
|
597 | 597 | """Use this mixin to make Demo classes with less visual clutter. |
|
598 | 598 | |
|
599 | 599 | Demos using this mixin will clear the screen before every block and use |
|
600 | 600 | blank marquees. |
|
601 | 601 | |
|
602 | 602 | Note that in order for the methods defined here to actually override those |
|
603 | 603 | of the classes it's mixed with, it must go /first/ in the inheritance |
|
604 | 604 | tree. For example: |
|
605 | 605 | |
|
606 | 606 | class ClearIPDemo(ClearMixin,IPythonDemo): pass |
|
607 | 607 | |
|
608 | 608 | will provide an IPythonDemo class with the mixin's features. |
|
609 | 609 | """ |
|
610 | 610 | |
|
611 | 611 | def marquee(self,txt='',width=78,mark='*'): |
|
612 | 612 | """Blank marquee that returns '' no matter what the input.""" |
|
613 | 613 | return '' |
|
614 | 614 | |
|
615 | 615 | def pre_cmd(self): |
|
616 | 616 | """Method called before executing each block. |
|
617 | 617 | |
|
618 | 618 | This one simply clears the screen.""" |
|
619 | 619 | from IPython.utils.terminal import _term_clear |
|
620 | 620 | _term_clear() |
|
621 | 621 | |
|
622 | 622 | class ClearDemo(ClearMixin,Demo): |
|
623 | 623 | pass |
|
624 | 624 | |
|
625 | 625 | |
|
626 | 626 | class ClearIPDemo(ClearMixin,IPythonDemo): |
|
627 | 627 | pass |
|
628 | 628 | |
|
629 | 629 | |
|
630 | 630 | def slide(file_path, noclear=False, format_rst=True, formatter="terminal", |
|
631 | 631 | style="native", auto_all=False, delimiter='...'): |
|
632 | 632 | if noclear: |
|
633 | 633 | demo_class = Demo |
|
634 | 634 | else: |
|
635 | 635 | demo_class = ClearDemo |
|
636 | 636 | demo = demo_class(file_path, format_rst=format_rst, formatter=formatter, |
|
637 | 637 | style=style, auto_all=auto_all) |
|
638 | 638 | while not demo.finished: |
|
639 | 639 | demo() |
|
640 | 640 | try: |
|
641 | 641 | py3compat.input('\n' + delimiter) |
|
642 | 642 | except KeyboardInterrupt: |
|
643 | 643 | exit(1) |
|
644 | 644 | |
|
645 | 645 | if __name__ == '__main__': |
|
646 | 646 | import argparse |
|
647 | 647 | parser = argparse.ArgumentParser(description='Run python demos') |
|
648 | 648 | parser.add_argument('--noclear', '-C', action='store_true', |
|
649 | 649 | help='Do not clear terminal on each slide') |
|
650 | 650 | parser.add_argument('--rst', '-r', action='store_true', |
|
651 | 651 | help='Highlight comments and dostrings as rst') |
|
652 | 652 | parser.add_argument('--formatter', '-f', default='terminal', |
|
653 | 653 | help='pygments formatter name could be: terminal, ' |
|
654 | 654 | 'terminal256, terminal16m') |
|
655 | 655 | parser.add_argument('--style', '-s', default='default', |
|
656 | 656 | help='pygments style name') |
|
657 | 657 | parser.add_argument('--auto', '-a', action='store_true', |
|
658 | 658 | help='Run all blocks automatically without' |
|
659 | 659 | 'confirmation') |
|
660 | 660 | parser.add_argument('--delimiter', '-d', default='...', |
|
661 | 661 | help='slides delimiter added after each slide run') |
|
662 | 662 | parser.add_argument('file', nargs=1, |
|
663 | 663 | help='python demo file') |
|
664 | 664 | args = parser.parse_args() |
|
665 | 665 | slide(args.file[0], noclear=args.noclear, format_rst=args.rst, |
|
666 | 666 | formatter=args.formatter, style=args.style, auto_all=args.auto, |
|
667 | 667 | delimiter=args.delimiter) |
@@ -1,557 +1,557 b'' | |||
|
1 | 1 | """Various display related classes. |
|
2 | 2 | |
|
3 | 3 | Authors : MinRK, gregcaporaso, dannystaple |
|
4 | 4 | """ |
|
5 | 5 | from os.path import exists, isfile, splitext, abspath, join, isdir |
|
6 | 6 | from os import walk, sep |
|
7 | 7 | |
|
8 | 8 | from IPython.core.display import DisplayObject |
|
9 | 9 | |
|
10 | 10 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', |
|
11 | 11 | 'FileLink', 'FileLinks'] |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | class Audio(DisplayObject): |
|
15 | 15 | """Create an audio object. |
|
16 | 16 | |
|
17 | 17 | When this object is returned by an input cell or passed to the |
|
18 | 18 | display function, it will result in Audio controls being displayed |
|
19 | 19 | in the frontend (only works in the notebook). |
|
20 | 20 | |
|
21 | 21 | Parameters |
|
22 | 22 | ---------- |
|
23 | 23 | data : numpy array, list, unicode, str or bytes |
|
24 | 24 | Can be one of |
|
25 | 25 | |
|
26 | 26 | * Numpy 1d array containing the desired waveform (mono) |
|
27 | 27 | * Numpy 2d array containing waveforms for each channel. |
|
28 | 28 | Shape=(NCHAN, NSAMPLES). For the standard channel order, see |
|
29 | 29 | http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx |
|
30 | 30 | * List of float or integer representing the waveform (mono) |
|
31 | 31 | * String containing the filename |
|
32 | 32 | * Bytestring containing raw PCM data or |
|
33 | 33 | * URL pointing to a file on the web. |
|
34 | 34 | |
|
35 | 35 | If the array option is used the waveform will be normalized. |
|
36 | 36 | |
|
37 | 37 | If a filename or url is used the format support will be browser |
|
38 | 38 | dependent. |
|
39 | 39 | url : unicode |
|
40 | 40 | A URL to download the data from. |
|
41 | 41 | filename : unicode |
|
42 | 42 | Path to a local file to load the data from. |
|
43 | 43 | embed : boolean |
|
44 | 44 | Should the audio data be embedded using a data URI (True) or should |
|
45 | 45 | the original source be referenced. Set this to True if you want the |
|
46 | 46 | audio to playable later with no internet connection in the notebook. |
|
47 | 47 | |
|
48 | 48 | Default is `True`, unless the keyword argument `url` is set, then |
|
49 | 49 | default value is `False`. |
|
50 | 50 | rate : integer |
|
51 | 51 | The sampling rate of the raw data. |
|
52 | 52 | Only required when data parameter is being used as an array |
|
53 | 53 | autoplay : bool |
|
54 | 54 | Set to True if the audio should immediately start playing. |
|
55 | 55 | Default is `False`. |
|
56 | 56 | |
|
57 | 57 | Examples |
|
58 | 58 | -------- |
|
59 | 59 | :: |
|
60 | 60 | |
|
61 | 61 | # Generate a sound |
|
62 | 62 | import numpy as np |
|
63 | 63 | framerate = 44100 |
|
64 | 64 | t = np.linspace(0,5,framerate*5) |
|
65 | 65 | data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)) |
|
66 | 66 | Audio(data,rate=framerate) |
|
67 | 67 | |
|
68 | 68 | # Can also do stereo or more channels |
|
69 | 69 | dataleft = np.sin(2*np.pi*220*t) |
|
70 | 70 | dataright = np.sin(2*np.pi*224*t) |
|
71 | 71 | Audio([dataleft, dataright],rate=framerate) |
|
72 | 72 | |
|
73 | 73 | Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # From URL |
|
74 | 74 | Audio(url="http://www.w3schools.com/html/horse.ogg") |
|
75 | 75 | |
|
76 | 76 | Audio('/path/to/sound.wav') # From file |
|
77 | 77 | Audio(filename='/path/to/sound.ogg') |
|
78 | 78 | |
|
79 | 79 | Audio(b'RAW_WAV_DATA..) # From bytes |
|
80 | 80 | Audio(data=b'RAW_WAV_DATA..) |
|
81 | 81 | |
|
82 | 82 | """ |
|
83 | 83 | _read_flags = 'rb' |
|
84 | 84 | |
|
85 | 85 | def __init__(self, data=None, filename=None, url=None, embed=None, rate=None, autoplay=False): |
|
86 | 86 | if filename is None and url is None and data is None: |
|
87 | 87 | raise ValueError("No image data found. Expecting filename, url, or data.") |
|
88 | 88 | if embed is False and url is None: |
|
89 | 89 | raise ValueError("No url found. Expecting url when embed=False") |
|
90 | 90 | |
|
91 | 91 | if url is not None and embed is not True: |
|
92 | 92 | self.embed = False |
|
93 | 93 | else: |
|
94 | 94 | self.embed = True |
|
95 | 95 | self.autoplay = autoplay |
|
96 | 96 | super(Audio, self).__init__(data=data, url=url, filename=filename) |
|
97 | 97 | |
|
98 | 98 | if self.data is not None and not isinstance(self.data, bytes): |
|
99 | 99 | self.data = self._make_wav(data,rate) |
|
100 | 100 | |
|
101 | 101 | def reload(self): |
|
102 | 102 | """Reload the raw data from file or URL.""" |
|
103 | 103 | import mimetypes |
|
104 | 104 | if self.embed: |
|
105 | 105 | super(Audio, self).reload() |
|
106 | 106 | |
|
107 | 107 | if self.filename is not None: |
|
108 | 108 | self.mimetype = mimetypes.guess_type(self.filename)[0] |
|
109 | 109 | elif self.url is not None: |
|
110 | 110 | self.mimetype = mimetypes.guess_type(self.url)[0] |
|
111 | 111 | else: |
|
112 | 112 | self.mimetype = "audio/wav" |
|
113 | 113 | |
|
114 | 114 | def _make_wav(self, data, rate): |
|
115 | 115 | """ Transform a numpy array to a PCM bytestring """ |
|
116 | 116 | import struct |
|
117 | 117 | from io import BytesIO |
|
118 | 118 | import wave |
|
119 | 119 | |
|
120 | 120 | try: |
|
121 | 121 | import numpy as np |
|
122 | 122 | |
|
123 | 123 | data = np.array(data, dtype=float) |
|
124 | 124 | if len(data.shape) == 1: |
|
125 | 125 | nchan = 1 |
|
126 | 126 | elif len(data.shape) == 2: |
|
127 | 127 | # In wave files,channels are interleaved. E.g., |
|
128 | 128 | # "L1R1L2R2..." for stereo. See |
|
129 | 129 | # http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx |
|
130 | 130 | # for channel ordering |
|
131 | 131 | nchan = data.shape[0] |
|
132 | 132 | data = data.T.ravel() |
|
133 | 133 | else: |
|
134 | 134 | raise ValueError('Array audio input must be a 1D or 2D array') |
|
135 | 135 | scaled = np.int16(data/np.max(np.abs(data))*32767).tolist() |
|
136 | 136 | except ImportError: |
|
137 | 137 | # check that it is a "1D" list |
|
138 | 138 | idata = iter(data) # fails if not an iterable |
|
139 | 139 | try: |
|
140 | 140 | iter(idata.next()) |
|
141 | 141 | raise TypeError('Only lists of mono audio are ' |
|
142 | 142 | 'supported if numpy is not installed') |
|
143 | 143 | except TypeError: |
|
144 | 144 | # this means it's not a nested list, which is what we want |
|
145 | 145 | pass |
|
146 | 146 | maxabsvalue = float(max([abs(x) for x in data])) |
|
147 | 147 | scaled = [int(x/maxabsvalue*32767) for x in data] |
|
148 | 148 | nchan = 1 |
|
149 | 149 | |
|
150 | 150 | fp = BytesIO() |
|
151 | 151 | waveobj = wave.open(fp,mode='wb') |
|
152 | 152 | waveobj.setnchannels(nchan) |
|
153 | 153 | waveobj.setframerate(rate) |
|
154 | 154 | waveobj.setsampwidth(2) |
|
155 | 155 | waveobj.setcomptype('NONE','NONE') |
|
156 | 156 | waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled])) |
|
157 | 157 | val = fp.getvalue() |
|
158 | 158 | waveobj.close() |
|
159 | 159 | |
|
160 | 160 | return val |
|
161 | 161 | |
|
162 | 162 | def _data_and_metadata(self): |
|
163 | 163 | """shortcut for returning metadata with url information, if defined""" |
|
164 | 164 | md = {} |
|
165 | 165 | if self.url: |
|
166 | 166 | md['url'] = self.url |
|
167 | 167 | if md: |
|
168 | 168 | return self.data, md |
|
169 | 169 | else: |
|
170 | 170 | return self.data |
|
171 | 171 | |
|
172 | 172 | def _repr_html_(self): |
|
173 | 173 | src = """ |
|
174 | 174 | <audio controls="controls" {autoplay}> |
|
175 | 175 | <source src="{src}" type="{type}" /> |
|
176 | 176 | Your browser does not support the audio element. |
|
177 | 177 | </audio> |
|
178 | 178 | """ |
|
179 | 179 | return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr()) |
|
180 | 180 | |
|
181 | 181 | def src_attr(self): |
|
182 | 182 | import base64 |
|
183 | 183 | if self.embed and (self.data is not None): |
|
184 | 184 | data = base64=base64.b64encode(self.data).decode('ascii') |
|
185 | 185 | return """data:{type};base64,{base64}""".format(type=self.mimetype, |
|
186 | 186 | base64=data) |
|
187 | 187 | elif self.url is not None: |
|
188 | 188 | return self.url |
|
189 | 189 | else: |
|
190 | 190 | return "" |
|
191 | 191 | |
|
192 | 192 | def autoplay_attr(self): |
|
193 | 193 | if(self.autoplay): |
|
194 | 194 | return 'autoplay="autoplay"' |
|
195 | 195 | else: |
|
196 | 196 | return '' |
|
197 | 197 | |
|
198 | 198 | class IFrame(object): |
|
199 | 199 | """ |
|
200 | 200 | Generic class to embed an iframe in an IPython notebook |
|
201 | 201 | """ |
|
202 | 202 | |
|
203 | 203 | iframe = """ |
|
204 | 204 | <iframe |
|
205 | 205 | width="{width}" |
|
206 | 206 | height="{height}" |
|
207 | 207 | src="{src}{params}" |
|
208 | 208 | frameborder="0" |
|
209 | 209 | allowfullscreen |
|
210 | 210 | ></iframe> |
|
211 | 211 | """ |
|
212 | 212 | |
|
213 | 213 | def __init__(self, src, width, height, **kwargs): |
|
214 | 214 | self.src = src |
|
215 | 215 | self.width = width |
|
216 | 216 | self.height = height |
|
217 | 217 | self.params = kwargs |
|
218 | 218 | |
|
219 | 219 | def _repr_html_(self): |
|
220 | 220 | """return the embed iframe""" |
|
221 | 221 | if self.params: |
|
222 | 222 | try: |
|
223 | 223 | from urllib.parse import urlencode # Py 3 |
|
224 | 224 | except ImportError: |
|
225 | 225 | from urllib import urlencode |
|
226 | 226 | params = "?" + urlencode(self.params) |
|
227 | 227 | else: |
|
228 | 228 | params = "" |
|
229 | 229 | return self.iframe.format(src=self.src, |
|
230 | 230 | width=self.width, |
|
231 | 231 | height=self.height, |
|
232 | 232 | params=params) |
|
233 | 233 | |
|
234 | 234 | class YouTubeVideo(IFrame): |
|
235 | 235 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
|
236 | 236 | |
|
237 | 237 | e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would |
|
238 | 238 | do:: |
|
239 | 239 | |
|
240 | 240 | vid = YouTubeVideo("foo") |
|
241 | 241 | display(vid) |
|
242 | 242 | |
|
243 | 243 | To start from 30 seconds:: |
|
244 | 244 | |
|
245 | 245 | vid = YouTubeVideo("abc", start=30) |
|
246 | 246 | display(vid) |
|
247 | 247 | |
|
248 | 248 | To calculate seconds from time as hours, minutes, seconds use |
|
249 | 249 | :class:`datetime.timedelta`:: |
|
250 | 250 | |
|
251 | 251 | start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds()) |
|
252 | 252 | |
|
253 | 253 | Other parameters can be provided as documented at |
|
254 | 254 | https://developers.google.com/youtube/player_parameters#Parameters |
|
255 | 255 | |
|
256 | 256 | When converting the notebook using nbconvert, a jpeg representation of the video |
|
257 | 257 | will be inserted in the document. |
|
258 | 258 | """ |
|
259 | 259 | |
|
260 | 260 | def __init__(self, id, width=400, height=300, **kwargs): |
|
261 | 261 | self.id=id |
|
262 | 262 | src = "https://www.youtube.com/embed/{0}".format(id) |
|
263 | 263 | super(YouTubeVideo, self).__init__(src, width, height, **kwargs) |
|
264 | 264 | |
|
265 | 265 | def _repr_jpeg_(self): |
|
266 | 266 | # Deferred import |
|
267 | 267 | from urllib.request import urlopen |
|
268 | 268 | |
|
269 | 269 | try: |
|
270 | 270 | return urlopen("https://img.youtube.com/vi/{id}/hqdefault.jpg".format(id=self.id)).read() |
|
271 | 271 | except IOError: |
|
272 | 272 | return None |
|
273 | 273 | |
|
274 | 274 | class VimeoVideo(IFrame): |
|
275 | 275 | """ |
|
276 | 276 | Class for embedding a Vimeo video in an IPython session, based on its video id. |
|
277 | 277 | """ |
|
278 | 278 | |
|
279 | 279 | def __init__(self, id, width=400, height=300, **kwargs): |
|
280 | 280 | src="https://player.vimeo.com/video/{0}".format(id) |
|
281 | 281 | super(VimeoVideo, self).__init__(src, width, height, **kwargs) |
|
282 | 282 | |
|
283 | 283 | class ScribdDocument(IFrame): |
|
284 | 284 | """ |
|
285 | 285 | Class for embedding a Scribd document in an IPython session |
|
286 | 286 | |
|
287 | 287 | Use the start_page params to specify a starting point in the document |
|
288 | 288 | Use the view_mode params to specify display type one off scroll | slideshow | book |
|
289 | 289 | |
|
290 | 290 | e.g to Display Wes' foundational paper about PANDAS in book mode from page 3 |
|
291 | 291 | |
|
292 | 292 | ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book") |
|
293 | 293 | """ |
|
294 | 294 | |
|
295 | 295 | def __init__(self, id, width=400, height=300, **kwargs): |
|
296 | 296 | src="https://www.scribd.com/embeds/{0}/content".format(id) |
|
297 | 297 | super(ScribdDocument, self).__init__(src, width, height, **kwargs) |
|
298 | 298 | |
|
299 | 299 | class FileLink(object): |
|
300 | 300 | """Class for embedding a local file link in an IPython session, based on path |
|
301 | 301 | |
|
302 | 302 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt |
|
303 | 303 | |
|
304 | 304 | you would do:: |
|
305 | 305 | |
|
306 | 306 | local_file = FileLink("my/data.txt") |
|
307 | 307 | display(local_file) |
|
308 | 308 | |
|
309 | 309 | or in the HTML notebook, just:: |
|
310 | 310 | |
|
311 | 311 | FileLink("my/data.txt") |
|
312 | 312 | """ |
|
313 | 313 | |
|
314 | 314 | html_link_str = "<a href='%s' target='_blank'>%s</a>" |
|
315 | 315 | |
|
316 | 316 | def __init__(self, |
|
317 | 317 | path, |
|
318 | 318 | url_prefix='', |
|
319 | 319 | result_html_prefix='', |
|
320 | 320 | result_html_suffix='<br>'): |
|
321 | 321 | """ |
|
322 | 322 | Parameters |
|
323 | 323 | ---------- |
|
324 | 324 | path : str |
|
325 | 325 | path to the file or directory that should be formatted |
|
326 | 326 | url_prefix : str |
|
327 | 327 | prefix to be prepended to all files to form a working link [default: |
|
328 | 328 | ''] |
|
329 | 329 | result_html_prefix : str |
|
330 | 330 | text to append to beginning to link [default: ''] |
|
331 | 331 | result_html_suffix : str |
|
332 | 332 | text to append at the end of link [default: '<br>'] |
|
333 | 333 | """ |
|
334 | 334 | if isdir(path): |
|
335 | 335 | raise ValueError("Cannot display a directory using FileLink. " |
|
336 | 336 | "Use FileLinks to display '%s'." % path) |
|
337 | 337 | self.path = path |
|
338 | 338 | self.url_prefix = url_prefix |
|
339 | 339 | self.result_html_prefix = result_html_prefix |
|
340 | 340 | self.result_html_suffix = result_html_suffix |
|
341 | 341 | |
|
342 | 342 | def _format_path(self): |
|
343 | 343 | fp = ''.join([self.url_prefix,self.path]) |
|
344 | 344 | return ''.join([self.result_html_prefix, |
|
345 | 345 | self.html_link_str % (fp, self.path), |
|
346 | 346 | self.result_html_suffix]) |
|
347 | 347 | |
|
348 | 348 | def _repr_html_(self): |
|
349 | 349 | """return html link to file |
|
350 | 350 | """ |
|
351 | 351 | if not exists(self.path): |
|
352 | 352 | return ("Path (<tt>%s</tt>) doesn't exist. " |
|
353 | 353 | "It may still be in the process of " |
|
354 | 354 | "being generated, or you may have the " |
|
355 | 355 | "incorrect path." % self.path) |
|
356 | 356 | |
|
357 | 357 | return self._format_path() |
|
358 | 358 | |
|
359 | 359 | def __repr__(self): |
|
360 | 360 | """return absolute path to file |
|
361 | 361 | """ |
|
362 | 362 | return abspath(self.path) |
|
363 | 363 | |
|
364 | 364 | class FileLinks(FileLink): |
|
365 | 365 | """Class for embedding local file links in an IPython session, based on path |
|
366 | 366 | |
|
367 | 367 | e.g. to embed links to files that were generated in the IPython notebook |
|
368 | 368 | under ``my/data``, you would do:: |
|
369 | 369 | |
|
370 | 370 | local_files = FileLinks("my/data") |
|
371 | 371 | display(local_files) |
|
372 | 372 | |
|
373 | 373 | or in the HTML notebook, just:: |
|
374 | 374 | |
|
375 | 375 | FileLinks("my/data") |
|
376 | 376 | """ |
|
377 | 377 | def __init__(self, |
|
378 | 378 | path, |
|
379 | 379 | url_prefix='', |
|
380 | 380 | included_suffixes=None, |
|
381 | 381 | result_html_prefix='', |
|
382 | 382 | result_html_suffix='<br>', |
|
383 | 383 | notebook_display_formatter=None, |
|
384 | 384 | terminal_display_formatter=None, |
|
385 | 385 | recursive=True): |
|
386 | 386 | """ |
|
387 | 387 | See :class:`FileLink` for the ``path``, ``url_prefix``, |
|
388 | 388 | ``result_html_prefix`` and ``result_html_suffix`` parameters. |
|
389 | 389 | |
|
390 | 390 | included_suffixes : list |
|
391 | 391 | Filename suffixes to include when formatting output [default: include |
|
392 | 392 | all files] |
|
393 | 393 | |
|
394 | 394 | notebook_display_formatter : function |
|
395 | 395 | Used to format links for display in the notebook. See discussion of |
|
396 | 396 | formatter functions below. |
|
397 | 397 | |
|
398 | 398 | terminal_display_formatter : function |
|
399 | 399 | Used to format links for display in the terminal. See discussion of |
|
400 | 400 | formatter functions below. |
|
401 | 401 | |
|
402 | 402 | Formatter functions must be of the form:: |
|
403 | 403 | |
|
404 | 404 | f(dirname, fnames, included_suffixes) |
|
405 | 405 | |
|
406 | 406 | dirname : str |
|
407 | 407 | The name of a directory |
|
408 | 408 | fnames : list |
|
409 | 409 | The files in that directory |
|
410 | 410 | included_suffixes : list |
|
411 | 411 | The file suffixes that should be included in the output (passing None |
|
412 | 412 | meansto include all suffixes in the output in the built-in formatters) |
|
413 | 413 | recursive : boolean |
|
414 | 414 | Whether to recurse into subdirectories. Default is True. |
|
415 | 415 | |
|
416 | 416 | The function should return a list of lines that will be printed in the |
|
417 | 417 | notebook (if passing notebook_display_formatter) or the terminal (if |
|
418 | 418 | passing terminal_display_formatter). This function is iterated over for |
|
419 | 419 | each directory in self.path. Default formatters are in place, can be |
|
420 | 420 | passed here to support alternative formatting. |
|
421 | 421 | |
|
422 | 422 | """ |
|
423 | 423 | if isfile(path): |
|
424 | 424 | raise ValueError("Cannot display a file using FileLinks. " |
|
425 | 425 | "Use FileLink to display '%s'." % path) |
|
426 | 426 | self.included_suffixes = included_suffixes |
|
427 | 427 | # remove trailing slashs for more consistent output formatting |
|
428 | 428 | path = path.rstrip('/') |
|
429 | 429 | |
|
430 | 430 | self.path = path |
|
431 | 431 | self.url_prefix = url_prefix |
|
432 | 432 | self.result_html_prefix = result_html_prefix |
|
433 | 433 | self.result_html_suffix = result_html_suffix |
|
434 | 434 | |
|
435 | 435 | self.notebook_display_formatter = \ |
|
436 | 436 | notebook_display_formatter or self._get_notebook_display_formatter() |
|
437 | 437 | self.terminal_display_formatter = \ |
|
438 | 438 | terminal_display_formatter or self._get_terminal_display_formatter() |
|
439 | 439 | |
|
440 | 440 | self.recursive = recursive |
|
441 | 441 | |
|
442 | 442 | def _get_display_formatter(self, |
|
443 | 443 | dirname_output_format, |
|
444 | 444 | fname_output_format, |
|
445 | 445 | fp_format, |
|
446 | 446 | fp_cleaner=None): |
|
447 | 447 | """ generate built-in formatter function |
|
448 | 448 | |
|
449 | 449 | this is used to define both the notebook and terminal built-in |
|
450 | 450 | formatters as they only differ by some wrapper text for each entry |
|
451 | 451 | |
|
452 | 452 | dirname_output_format: string to use for formatting directory |
|
453 | 453 | names, dirname will be substituted for a single "%s" which |
|
454 | 454 | must appear in this string |
|
455 | 455 | fname_output_format: string to use for formatting file names, |
|
456 | 456 | if a single "%s" appears in the string, fname will be substituted |
|
457 | 457 | if two "%s" appear in the string, the path to fname will be |
|
458 | 458 | substituted for the first and fname will be substituted for the |
|
459 | 459 | second |
|
460 | 460 | fp_format: string to use for formatting filepaths, must contain |
|
461 | 461 | exactly two "%s" and the dirname will be subsituted for the first |
|
462 | 462 | and fname will be substituted for the second |
|
463 | 463 | """ |
|
464 | 464 | def f(dirname, fnames, included_suffixes=None): |
|
465 | 465 | result = [] |
|
466 | 466 | # begin by figuring out which filenames, if any, |
|
467 | 467 | # are going to be displayed |
|
468 | 468 | display_fnames = [] |
|
469 | 469 | for fname in fnames: |
|
470 | 470 | if (isfile(join(dirname,fname)) and |
|
471 | 471 | (included_suffixes is None or |
|
472 | 472 | splitext(fname)[1] in included_suffixes)): |
|
473 | 473 | display_fnames.append(fname) |
|
474 | 474 | |
|
475 | 475 | if len(display_fnames) == 0: |
|
476 | 476 | # if there are no filenames to display, don't print anything |
|
477 | 477 | # (not even the directory name) |
|
478 | 478 | pass |
|
479 | 479 | else: |
|
480 | 480 | # otherwise print the formatted directory name followed by |
|
481 | 481 | # the formatted filenames |
|
482 | 482 | dirname_output_line = dirname_output_format % dirname |
|
483 | 483 | result.append(dirname_output_line) |
|
484 | 484 | for fname in display_fnames: |
|
485 | 485 | fp = fp_format % (dirname,fname) |
|
486 | 486 | if fp_cleaner is not None: |
|
487 | 487 | fp = fp_cleaner(fp) |
|
488 | 488 | try: |
|
489 | 489 | # output can include both a filepath and a filename... |
|
490 | 490 | fname_output_line = fname_output_format % (fp, fname) |
|
491 | 491 | except TypeError: |
|
492 | 492 | # ... or just a single filepath |
|
493 | 493 | fname_output_line = fname_output_format % fname |
|
494 | 494 | result.append(fname_output_line) |
|
495 | 495 | return result |
|
496 | 496 | return f |
|
497 | 497 | |
|
498 | 498 | def _get_notebook_display_formatter(self, |
|
499 | 499 | spacer=" "): |
|
500 | 500 | """ generate function to use for notebook formatting |
|
501 | 501 | """ |
|
502 | 502 | dirname_output_format = \ |
|
503 | 503 | self.result_html_prefix + "%s/" + self.result_html_suffix |
|
504 | 504 | fname_output_format = \ |
|
505 | 505 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix |
|
506 | 506 | fp_format = self.url_prefix + '%s/%s' |
|
507 | 507 | if sep == "\\": |
|
508 | 508 | # Working on a platform where the path separator is "\", so |
|
509 | 509 | # must convert these to "/" for generating a URI |
|
510 | 510 | def fp_cleaner(fp): |
|
511 | # Replace all occurences of backslash ("\") with a forward | |
|
511 | # Replace all occurrences of backslash ("\") with a forward | |
|
512 | 512 | # slash ("/") - this is necessary on windows when a path is |
|
513 | 513 | # provided as input, but we must link to a URI |
|
514 | 514 | return fp.replace('\\','/') |
|
515 | 515 | else: |
|
516 | 516 | fp_cleaner = None |
|
517 | 517 | |
|
518 | 518 | return self._get_display_formatter(dirname_output_format, |
|
519 | 519 | fname_output_format, |
|
520 | 520 | fp_format, |
|
521 | 521 | fp_cleaner) |
|
522 | 522 | |
|
523 | 523 | def _get_terminal_display_formatter(self, |
|
524 | 524 | spacer=" "): |
|
525 | 525 | """ generate function to use for terminal formatting |
|
526 | 526 | """ |
|
527 | 527 | dirname_output_format = "%s/" |
|
528 | 528 | fname_output_format = spacer + "%s" |
|
529 | 529 | fp_format = '%s/%s' |
|
530 | 530 | |
|
531 | 531 | return self._get_display_formatter(dirname_output_format, |
|
532 | 532 | fname_output_format, |
|
533 | 533 | fp_format) |
|
534 | 534 | |
|
535 | 535 | def _format_path(self): |
|
536 | 536 | result_lines = [] |
|
537 | 537 | if self.recursive: |
|
538 | 538 | walked_dir = list(walk(self.path)) |
|
539 | 539 | else: |
|
540 | 540 | walked_dir = [next(walk(self.path))] |
|
541 | 541 | walked_dir.sort() |
|
542 | 542 | for dirname, subdirs, fnames in walked_dir: |
|
543 | 543 | result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes) |
|
544 | 544 | return '\n'.join(result_lines) |
|
545 | 545 | |
|
546 | 546 | def __repr__(self): |
|
547 | 547 | """return newline-separated absolute paths |
|
548 | 548 | """ |
|
549 | 549 | result_lines = [] |
|
550 | 550 | if self.recursive: |
|
551 | 551 | walked_dir = list(walk(self.path)) |
|
552 | 552 | else: |
|
553 | 553 | walked_dir = [next(walk(self.path))] |
|
554 | 554 | walked_dir.sort() |
|
555 | 555 | for dirname, subdirs, fnames in walked_dir: |
|
556 | 556 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) |
|
557 | 557 | return '\n'.join(result_lines) |
@@ -1,128 +1,128 b'' | |||
|
1 | 1 | """ 'editor' hooks for common editors that work well with ipython |
|
2 | 2 | |
|
3 | 3 | They should honor the line number argument, at least. |
|
4 | 4 | |
|
5 | 5 | Contributions are *very* welcome. |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | import os |
|
9 | 9 | import pipes |
|
10 | 10 | import shlex |
|
11 | 11 | import subprocess |
|
12 | 12 | import sys |
|
13 | 13 | |
|
14 | 14 | from IPython import get_ipython |
|
15 | 15 | from IPython.core.error import TryNext |
|
16 | 16 | from IPython.utils import py3compat |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | def install_editor(template, wait=False): |
|
20 | 20 | """Installs the editor that is called by IPython for the %edit magic. |
|
21 | 21 | |
|
22 | 22 | This overrides the default editor, which is generally set by your EDITOR |
|
23 | 23 | environment variable or is notepad (windows) or vi (linux). By supplying a |
|
24 | 24 | template string `run_template`, you can control how the editor is invoked |
|
25 | 25 | by IPython -- (e.g. the format in which it accepts command line options) |
|
26 | 26 | |
|
27 | 27 | Parameters |
|
28 | 28 | ---------- |
|
29 | 29 | template : basestring |
|
30 | 30 | run_template acts as a template for how your editor is invoked by |
|
31 | 31 | the shell. It should contain '{filename}', which will be replaced on |
|
32 | 32 | invokation with the file name, and '{line}', $line by line number |
|
33 | 33 | (or 0) to invoke the file with. |
|
34 | 34 | wait : bool |
|
35 | 35 | If `wait` is true, wait until the user presses enter before returning, |
|
36 | 36 | to facilitate non-blocking editors that exit immediately after |
|
37 | 37 | the call. |
|
38 | 38 | """ |
|
39 | 39 | |
|
40 | 40 | # not all editors support $line, so we'll leave out this check |
|
41 | 41 | # for substitution in ['$file', '$line']: |
|
42 | 42 | # if not substitution in run_template: |
|
43 | 43 | # raise ValueError(('run_template should contain %s' |
|
44 | 44 | # ' for string substitution. You supplied "%s"' % (substitution, |
|
45 | 45 | # run_template))) |
|
46 | 46 | |
|
47 | 47 | def call_editor(self, filename, line=0): |
|
48 | 48 | if line is None: |
|
49 | 49 | line = 0 |
|
50 | 50 | cmd = template.format(filename=pipes.quote(filename), line=line) |
|
51 | 51 | print(">", cmd) |
|
52 | 52 | # pipes.quote doesn't work right on Windows, but it does after splitting |
|
53 | 53 | if sys.platform.startswith('win'): |
|
54 | 54 | cmd = shlex.split(cmd) |
|
55 | 55 | proc = subprocess.Popen(cmd, shell=True) |
|
56 | 56 | if proc.wait() != 0: |
|
57 | 57 | raise TryNext() |
|
58 | 58 | if wait: |
|
59 | 59 | py3compat.input("Press Enter when done editing:") |
|
60 | 60 | |
|
61 | 61 | get_ipython().set_hook('editor', call_editor) |
|
62 | 62 | get_ipython().editor = template |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | # in these, exe is always the path/name of the executable. Useful |
|
66 | 66 | # if you don't have the editor directory in your path |
|
67 | 67 | def komodo(exe=u'komodo'): |
|
68 | 68 | """ Activestate Komodo [Edit] """ |
|
69 | 69 | install_editor(exe + u' -l {line} {filename}', wait=True) |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def scite(exe=u"scite"): |
|
73 | 73 | """ SciTE or Sc1 """ |
|
74 | 74 | install_editor(exe + u' {filename} -goto:{line}') |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | def notepadplusplus(exe=u'notepad++'): |
|
78 | 78 | """ Notepad++ http://notepad-plus.sourceforge.net """ |
|
79 | 79 | install_editor(exe + u' -n{line} {filename}') |
|
80 | 80 | |
|
81 | 81 | |
|
82 | 82 | def jed(exe=u'jed'): |
|
83 | 83 | """ JED, the lightweight emacsish editor """ |
|
84 | 84 | install_editor(exe + u' +{line} {filename}') |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | def idle(exe=u'idle'): |
|
88 | 88 | """ Idle, the editor bundled with python |
|
89 | 89 | |
|
90 | 90 | Parameters |
|
91 | 91 | ---------- |
|
92 | 92 | exe : str, None |
|
93 | 93 | If none, should be pretty smart about finding the executable. |
|
94 | 94 | """ |
|
95 | 95 | if exe is None: |
|
96 | 96 | import idlelib |
|
97 | 97 | p = os.path.dirname(idlelib.__filename__) |
|
98 | 98 | # i'm not sure if this actually works. Is this idle.py script |
|
99 |
# guar |
|
|
99 | # guaranteed to be executable? | |
|
100 | 100 | exe = os.path.join(p, 'idle.py') |
|
101 | 101 | install_editor(exe + u' {filename}') |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def mate(exe=u'mate'): |
|
105 | 105 | """ TextMate, the missing editor""" |
|
106 | 106 | # wait=True is not required since we're using the -w flag to mate |
|
107 | 107 | install_editor(exe + u' -w -l {line} {filename}') |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | # ########################################## |
|
111 | 111 | # these are untested, report any problems |
|
112 | 112 | # ########################################## |
|
113 | 113 | |
|
114 | 114 | |
|
115 | 115 | def emacs(exe=u'emacs'): |
|
116 | 116 | install_editor(exe + u' +{line} {filename}') |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def gnuclient(exe=u'gnuclient'): |
|
120 | 120 | install_editor(exe + u' -nw +{line} {filename}') |
|
121 | 121 | |
|
122 | 122 | |
|
123 | 123 | def crimson_editor(exe=u'cedt.exe'): |
|
124 | 124 | install_editor(exe + u' /L:{line} {filename}') |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | def kate(exe=u'kate'): |
|
128 | 128 | install_editor(exe + u' -u -l {line} {filename}') |
@@ -1,90 +1,90 b'' | |||
|
1 | 1 | # Code borrowed from ptpython |
|
2 | 2 | # https://github.com/jonathanslenders/ptpython/blob/86b71a89626114b18898a0af463978bdb32eeb70/ptpython/eventloop.py |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) 2015, Jonathan Slenders |
|
5 | 5 | # All rights reserved. |
|
6 | 6 | # |
|
7 | 7 | # Redistribution and use in source and binary forms, with or without modification, |
|
8 | 8 | # are permitted provided that the following conditions are met: |
|
9 | 9 | # |
|
10 | 10 | # * Redistributions of source code must retain the above copyright notice, this |
|
11 | 11 | # list of conditions and the following disclaimer. |
|
12 | 12 | # |
|
13 | 13 | # * Redistributions in binary form must reproduce the above copyright notice, this |
|
14 | 14 | # list of conditions and the following disclaimer in the documentation and/or |
|
15 | 15 | # other materials provided with the distribution. |
|
16 | 16 | # |
|
17 | 17 | # * Neither the name of the {organization} nor the names of its |
|
18 | 18 | # contributors may be used to endorse or promote products derived from |
|
19 | 19 | # this software without specific prior written permission. |
|
20 | 20 | # |
|
21 | 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
22 | 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
23 | 23 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
24 | 24 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
|
25 | 25 | # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
26 | 26 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
27 | 27 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
28 | 28 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 | 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 | 30 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 | 31 | |
|
32 | 32 | """ |
|
33 | 33 | Wrapper around the eventloop that gives some time to the Tkinter GUI to process |
|
34 | 34 | events when it's loaded and while we are waiting for input at the REPL. This |
|
35 | 35 | way we don't block the UI of for instance ``turtle`` and other Tk libraries. |
|
36 | 36 | |
|
37 |
(Normally Tkinter register |
|
|
37 | (Normally Tkinter registers it's callbacks in ``PyOS_InputHook`` to integrate | |
|
38 | 38 | in readline. ``prompt-toolkit`` doesn't understand that input hook, but this |
|
39 | 39 | will fix it for Tk.) |
|
40 | 40 | """ |
|
41 | 41 | import time |
|
42 | 42 | |
|
43 | 43 | import _tkinter |
|
44 | 44 | import tkinter |
|
45 | 45 | |
|
46 | 46 | def inputhook(inputhook_context): |
|
47 | 47 | """ |
|
48 | 48 | Inputhook for Tk. |
|
49 | 49 | Run the Tk eventloop until prompt-toolkit needs to process the next input. |
|
50 | 50 | """ |
|
51 | 51 | # Get the current TK application. |
|
52 | 52 | root = tkinter._default_root |
|
53 | 53 | |
|
54 | 54 | def wait_using_filehandler(): |
|
55 | 55 | """ |
|
56 | 56 | Run the TK eventloop until the file handler that we got from the |
|
57 | 57 | inputhook becomes readable. |
|
58 | 58 | """ |
|
59 | 59 | # Add a handler that sets the stop flag when `prompt-toolkit` has input |
|
60 | 60 | # to process. |
|
61 | 61 | stop = [False] |
|
62 | 62 | def done(*a): |
|
63 | 63 | stop[0] = True |
|
64 | 64 | |
|
65 | 65 | root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done) |
|
66 | 66 | |
|
67 | 67 | # Run the TK event loop as long as we don't receive input. |
|
68 | 68 | while root.dooneevent(_tkinter.ALL_EVENTS): |
|
69 | 69 | if stop[0]: |
|
70 | 70 | break |
|
71 | 71 | |
|
72 | 72 | root.deletefilehandler(inputhook_context.fileno()) |
|
73 | 73 | |
|
74 | 74 | def wait_using_polling(): |
|
75 | 75 | """ |
|
76 | 76 | Windows TK doesn't support 'createfilehandler'. |
|
77 | 77 | So, run the TK eventloop and poll until input is ready. |
|
78 | 78 | """ |
|
79 | 79 | while not inputhook_context.input_is_ready(): |
|
80 | 80 | while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT): |
|
81 | 81 | pass |
|
82 | 82 | # Sleep to make the CPU idle, but not too long, so that the UI |
|
83 | 83 | # stays responsive. |
|
84 | 84 | time.sleep(.01) |
|
85 | 85 | |
|
86 | 86 | if root is not None: |
|
87 | 87 | if hasattr(root, 'createfilehandler'): |
|
88 | 88 | wait_using_filehandler() |
|
89 | 89 | else: |
|
90 | 90 | wait_using_polling() |
@@ -1,176 +1,176 b'' | |||
|
1 | 1 | """Experimental code for cleaner support of IPython syntax with unittest. |
|
2 | 2 | |
|
3 | 3 | In IPython up until 0.10, we've used very hacked up nose machinery for running |
|
4 | 4 | tests with IPython special syntax, and this has proved to be extremely slow. |
|
5 | 5 | This module provides decorators to try a different approach, stemming from a |
|
6 | 6 | conversation Brian and I (FP) had about this problem Sept/09. |
|
7 | 7 | |
|
8 | 8 | The goal is to be able to easily write simple functions that can be seen by |
|
9 | 9 | unittest as tests, and ultimately for these to support doctests with full |
|
10 | 10 | IPython syntax. Nose already offers this based on naming conventions and our |
|
11 | 11 | hackish plugins, but we are seeking to move away from nose dependencies if |
|
12 | 12 | possible. |
|
13 | 13 | |
|
14 | 14 | This module follows a different approach, based on decorators. |
|
15 | 15 | |
|
16 | 16 | - A decorator called @ipdoctest can mark any function as having a docstring |
|
17 | 17 | that should be viewed as a doctest, but after syntax conversion. |
|
18 | 18 | |
|
19 | 19 | Authors |
|
20 | 20 | ------- |
|
21 | 21 | |
|
22 | 22 | - Fernando Perez <Fernando.Perez@berkeley.edu> |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Copyright (C) 2009-2011 The IPython Development Team |
|
28 | 28 | # |
|
29 | 29 | # Distributed under the terms of the BSD License. The full license is in |
|
30 | 30 | # the file COPYING, distributed as part of this software. |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | # Imports |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | |
|
37 | 37 | # Stdlib |
|
38 | 38 | import re |
|
39 | 39 | import unittest |
|
40 | 40 | from doctest import DocTestFinder, DocTestRunner, TestResults |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | # Classes and functions |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | |
|
46 | 46 | def count_failures(runner): |
|
47 | 47 | """Count number of failures in a doctest runner. |
|
48 | 48 | |
|
49 | 49 | Code modeled after the summarize() method in doctest. |
|
50 | 50 | """ |
|
51 | 51 | return [TestResults(f, t) for f, t in runner._name2ft.values() if f > 0 ] |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | class IPython2PythonConverter(object): |
|
55 | 55 | """Convert IPython 'syntax' to valid Python. |
|
56 | 56 | |
|
57 | 57 | Eventually this code may grow to be the full IPython syntax conversion |
|
58 |
implementation, but for now it only does prompt conver |
|
|
58 | implementation, but for now it only does prompt conversion.""" | |
|
59 | 59 | |
|
60 | 60 | def __init__(self): |
|
61 | 61 | self.rps1 = re.compile(r'In\ \[\d+\]: ') |
|
62 | 62 | self.rps2 = re.compile(r'\ \ \ \.\.\.+: ') |
|
63 | 63 | self.rout = re.compile(r'Out\[\d+\]: \s*?\n?') |
|
64 | 64 | self.pyps1 = '>>> ' |
|
65 | 65 | self.pyps2 = '... ' |
|
66 | 66 | self.rpyps1 = re.compile ('(\s*%s)(.*)$' % self.pyps1) |
|
67 | 67 | self.rpyps2 = re.compile ('(\s*%s)(.*)$' % self.pyps2) |
|
68 | 68 | |
|
69 | 69 | def __call__(self, ds): |
|
70 | 70 | """Convert IPython prompts to python ones in a string.""" |
|
71 | 71 | from . import globalipapp |
|
72 | 72 | |
|
73 | 73 | pyps1 = '>>> ' |
|
74 | 74 | pyps2 = '... ' |
|
75 | 75 | pyout = '' |
|
76 | 76 | |
|
77 | 77 | dnew = ds |
|
78 | 78 | dnew = self.rps1.sub(pyps1, dnew) |
|
79 | 79 | dnew = self.rps2.sub(pyps2, dnew) |
|
80 | 80 | dnew = self.rout.sub(pyout, dnew) |
|
81 | 81 | ip = globalipapp.get_ipython() |
|
82 | 82 | |
|
83 | 83 | # Convert input IPython source into valid Python. |
|
84 | 84 | out = [] |
|
85 | 85 | newline = out.append |
|
86 | 86 | for line in dnew.splitlines(): |
|
87 | 87 | |
|
88 | 88 | mps1 = self.rpyps1.match(line) |
|
89 | 89 | if mps1 is not None: |
|
90 | 90 | prompt, text = mps1.groups() |
|
91 | 91 | newline(prompt+ip.prefilter(text, False)) |
|
92 | 92 | continue |
|
93 | 93 | |
|
94 | 94 | mps2 = self.rpyps2.match(line) |
|
95 | 95 | if mps2 is not None: |
|
96 | 96 | prompt, text = mps2.groups() |
|
97 | 97 | newline(prompt+ip.prefilter(text, True)) |
|
98 | 98 | continue |
|
99 | 99 | |
|
100 | 100 | newline(line) |
|
101 | 101 | newline('') # ensure a closing newline, needed by doctest |
|
102 | 102 | #print "PYSRC:", '\n'.join(out) # dbg |
|
103 | 103 | return '\n'.join(out) |
|
104 | 104 | |
|
105 | 105 | #return dnew |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | class Doc2UnitTester(object): |
|
109 | 109 | """Class whose instances act as a decorator for docstring testing. |
|
110 | 110 | |
|
111 | 111 | In practice we're only likely to need one instance ever, made below (though |
|
112 | 112 | no attempt is made at turning it into a singleton, there is no need for |
|
113 | 113 | that). |
|
114 | 114 | """ |
|
115 | 115 | def __init__(self, verbose=False): |
|
116 | 116 | """New decorator. |
|
117 | 117 | |
|
118 | 118 | Parameters |
|
119 | 119 | ---------- |
|
120 | 120 | |
|
121 | 121 | verbose : boolean, optional (False) |
|
122 | 122 | Passed to the doctest finder and runner to control verbosity. |
|
123 | 123 | """ |
|
124 | 124 | self.verbose = verbose |
|
125 | 125 | # We can reuse the same finder for all instances |
|
126 | 126 | self.finder = DocTestFinder(verbose=verbose, recurse=False) |
|
127 | 127 | |
|
128 | 128 | def __call__(self, func): |
|
129 | 129 | """Use as a decorator: doctest a function's docstring as a unittest. |
|
130 | 130 | |
|
131 | 131 | This version runs normal doctests, but the idea is to make it later run |
|
132 | 132 | ipython syntax instead.""" |
|
133 | 133 | |
|
134 | 134 | # Capture the enclosing instance with a different name, so the new |
|
135 | 135 | # class below can see it without confusion regarding its own 'self' |
|
136 | 136 | # that will point to the test instance at runtime |
|
137 | 137 | d2u = self |
|
138 | 138 | |
|
139 | 139 | # Rewrite the function's docstring to have python syntax |
|
140 | 140 | if func.__doc__ is not None: |
|
141 | 141 | func.__doc__ = ip2py(func.__doc__) |
|
142 | 142 | |
|
143 | 143 | # Now, create a tester object that is a real unittest instance, so |
|
144 | 144 | # normal unittest machinery (or Nose, or Trial) can find it. |
|
145 | 145 | class Tester(unittest.TestCase): |
|
146 | 146 | def test(self): |
|
147 | 147 | # Make a new runner per function to be tested |
|
148 | 148 | runner = DocTestRunner(verbose=d2u.verbose) |
|
149 | 149 | map(runner.run, d2u.finder.find(func, func.__name__)) |
|
150 | 150 | failed = count_failures(runner) |
|
151 | 151 | if failed: |
|
152 | 152 | # Since we only looked at a single function's docstring, |
|
153 | 153 | # failed should contain at most one item. More than that |
|
154 | 154 | # is a case we can't handle and should error out on |
|
155 | 155 | if len(failed) > 1: |
|
156 | 156 | err = "Invalid number of test results:" % failed |
|
157 | 157 | raise ValueError(err) |
|
158 | 158 | # Report a normal failure. |
|
159 | 159 | self.fail('failed doctests: %s' % str(failed[0])) |
|
160 | 160 | |
|
161 | 161 | # Rename it so test reports have the original signature. |
|
162 | 162 | Tester.__name__ = func.__name__ |
|
163 | 163 | return Tester |
|
164 | 164 | |
|
165 | 165 | |
|
166 | 166 | def ipdocstring(func): |
|
167 | 167 | """Change the function docstring via ip2py. |
|
168 | 168 | """ |
|
169 | 169 | if func.__doc__ is not None: |
|
170 | 170 | func.__doc__ = ip2py(func.__doc__) |
|
171 | 171 | return func |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | # Make an instance of the classes for public use |
|
175 | 175 | ipdoctest = Doc2UnitTester() |
|
176 | 176 | ip2py = IPython2PythonConverter() |
@@ -1,191 +1,191 b'' | |||
|
1 | 1 | """Windows-specific implementation of process utilities. |
|
2 | 2 | |
|
3 | 3 | This file is only meant to be imported by process.py, not by end-users. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2010-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | # stdlib |
|
18 | 18 | import os |
|
19 | 19 | import sys |
|
20 | 20 | import ctypes |
|
21 | 21 | |
|
22 | 22 | from ctypes import c_int, POINTER |
|
23 | 23 | from ctypes.wintypes import LPCWSTR, HLOCAL |
|
24 | 24 | from subprocess import STDOUT |
|
25 | 25 | |
|
26 | 26 | # our own imports |
|
27 | 27 | from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split |
|
28 | 28 | from . import py3compat |
|
29 | 29 | from .encoding import DEFAULT_ENCODING |
|
30 | 30 | |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | # Function definitions |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | |
|
35 | 35 | class AvoidUNCPath(object): |
|
36 | 36 | """A context manager to protect command execution from UNC paths. |
|
37 | 37 | |
|
38 | 38 | In the Win32 API, commands can't be invoked with the cwd being a UNC path. |
|
39 | 39 | This context manager temporarily changes directory to the 'C:' drive on |
|
40 | 40 | entering, and restores the original working directory on exit. |
|
41 | 41 | |
|
42 | 42 | The context manager returns the starting working directory *if* it made a |
|
43 | 43 | change and None otherwise, so that users can apply the necessary adjustment |
|
44 | 44 | to their system calls in the event of a change. |
|
45 | 45 | |
|
46 | 46 | Examples |
|
47 | 47 | -------- |
|
48 | 48 | :: |
|
49 | 49 | cmd = 'dir' |
|
50 | 50 | with AvoidUNCPath() as path: |
|
51 | 51 | if path is not None: |
|
52 | 52 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
53 | 53 | os.system(cmd) |
|
54 | 54 | """ |
|
55 | 55 | def __enter__(self): |
|
56 | 56 | self.path = os.getcwd() |
|
57 | 57 | self.is_unc_path = self.path.startswith(r"\\") |
|
58 | 58 | if self.is_unc_path: |
|
59 | 59 | # change to c drive (as cmd.exe cannot handle UNC addresses) |
|
60 | 60 | os.chdir("C:") |
|
61 | 61 | return self.path |
|
62 | 62 | else: |
|
63 | 63 | # We return None to signal that there was no change in the working |
|
64 | 64 | # directory |
|
65 | 65 | return None |
|
66 | 66 | |
|
67 | 67 | def __exit__(self, exc_type, exc_value, traceback): |
|
68 | 68 | if self.is_unc_path: |
|
69 | 69 | os.chdir(self.path) |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def _find_cmd(cmd): |
|
73 | 73 | """Find the full path to a .bat or .exe using the win32api module.""" |
|
74 | 74 | try: |
|
75 | 75 | from win32api import SearchPath |
|
76 | 76 | except ImportError: |
|
77 | 77 | raise ImportError('you need to have pywin32 installed for this to work') |
|
78 | 78 | else: |
|
79 | 79 | PATH = os.environ['PATH'] |
|
80 | 80 | extensions = ['.exe', '.com', '.bat', '.py'] |
|
81 | 81 | path = None |
|
82 | 82 | for ext in extensions: |
|
83 | 83 | try: |
|
84 | 84 | path = SearchPath(PATH, cmd, ext)[0] |
|
85 | 85 | except: |
|
86 | 86 | pass |
|
87 | 87 | if path is None: |
|
88 | 88 | raise OSError("command %r not found" % cmd) |
|
89 | 89 | else: |
|
90 | 90 | return path |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | def _system_body(p): |
|
94 | 94 | """Callback for _system.""" |
|
95 | 95 | enc = DEFAULT_ENCODING |
|
96 | 96 | for line in read_no_interrupt(p.stdout).splitlines(): |
|
97 | 97 | line = line.decode(enc, 'replace') |
|
98 | 98 | print(line, file=sys.stdout) |
|
99 | 99 | for line in read_no_interrupt(p.stderr).splitlines(): |
|
100 | 100 | line = line.decode(enc, 'replace') |
|
101 | 101 | print(line, file=sys.stderr) |
|
102 | 102 | |
|
103 | 103 | # Wait to finish for returncode |
|
104 | 104 | return p.wait() |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | def system(cmd): |
|
108 | 108 | """Win32 version of os.system() that works with network shares. |
|
109 | 109 | |
|
110 | 110 | Note that this implementation returns None, as meant for use in IPython. |
|
111 | 111 | |
|
112 | 112 | Parameters |
|
113 | 113 | ---------- |
|
114 | 114 | cmd : str or list |
|
115 | 115 | A command to be executed in the system shell. |
|
116 | 116 | |
|
117 | 117 | Returns |
|
118 | 118 | ------- |
|
119 | 119 | None : we explicitly do NOT return the subprocess status code, as this |
|
120 | 120 | utility is meant to be used extensively in IPython, where any return value |
|
121 | 121 | would trigger :func:`sys.displayhook` calls. |
|
122 | 122 | """ |
|
123 | 123 | # The controller provides interactivity with both |
|
124 | 124 | # stdin and stdout |
|
125 | 125 | #import _process_win32_controller |
|
126 | 126 | #_process_win32_controller.system(cmd) |
|
127 | 127 | |
|
128 | 128 | with AvoidUNCPath() as path: |
|
129 | 129 | if path is not None: |
|
130 | 130 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
131 | 131 | return process_handler(cmd, _system_body) |
|
132 | 132 | |
|
133 | 133 | def getoutput(cmd): |
|
134 | 134 | """Return standard output of executing cmd in a shell. |
|
135 | 135 | |
|
136 | 136 | Accepts the same arguments as os.system(). |
|
137 | 137 | |
|
138 | 138 | Parameters |
|
139 | 139 | ---------- |
|
140 | 140 | cmd : str or list |
|
141 | 141 | A command to be executed in the system shell. |
|
142 | 142 | |
|
143 | 143 | Returns |
|
144 | 144 | ------- |
|
145 | 145 | stdout : str |
|
146 | 146 | """ |
|
147 | 147 | |
|
148 | 148 | with AvoidUNCPath() as path: |
|
149 | 149 | if path is not None: |
|
150 | 150 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
151 | 151 | out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT) |
|
152 | 152 | |
|
153 | 153 | if out is None: |
|
154 | 154 | out = b'' |
|
155 | 155 | return py3compat.decode(out) |
|
156 | 156 | |
|
157 | 157 | try: |
|
158 | 158 | CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW |
|
159 | 159 | CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)] |
|
160 | 160 | CommandLineToArgvW.restype = POINTER(LPCWSTR) |
|
161 | 161 | LocalFree = ctypes.windll.kernel32.LocalFree |
|
162 | 162 | LocalFree.res_type = HLOCAL |
|
163 | 163 | LocalFree.arg_types = [HLOCAL] |
|
164 | 164 | |
|
165 | 165 | def arg_split(commandline, posix=False, strict=True): |
|
166 | 166 | """Split a command line's arguments in a shell-like manner. |
|
167 | 167 | |
|
168 | 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 | 171 | If strict=False, process_common.arg_split(...strict=False) is used instead. |
|
172 | 172 | """ |
|
173 | 173 | #CommandLineToArgvW returns path to executable if called with empty string. |
|
174 | 174 | if commandline.strip() == "": |
|
175 | 175 | return [] |
|
176 | 176 | if not strict: |
|
177 | 177 | # not really a cl-arg, fallback on _process_common |
|
178 | 178 | return py_arg_split(commandline, posix=posix, strict=strict) |
|
179 | 179 | argvn = c_int() |
|
180 | 180 | result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn)) |
|
181 | 181 | result_array_type = LPCWSTR * argvn.value |
|
182 | 182 | result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))] |
|
183 | 183 | retval = LocalFree(result_pointer) |
|
184 | 184 | return result |
|
185 | 185 | except AttributeError: |
|
186 | 186 | arg_split = py_arg_split |
|
187 | 187 | |
|
188 | 188 | def check_pid(pid): |
|
189 | 189 | # OpenProcess returns 0 if no such process (of ours) exists |
|
190 | 190 | # positive int otherwise |
|
191 | 191 | return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid)) |
@@ -1,438 +1,438 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Utilities for path handling. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | # Copyright (c) IPython Development Team. |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | |
|
9 | 9 | import os |
|
10 | 10 | import sys |
|
11 | 11 | import errno |
|
12 | 12 | import shutil |
|
13 | 13 | import random |
|
14 | 14 | import glob |
|
15 | 15 | from warnings import warn |
|
16 | 16 | |
|
17 | 17 | from IPython.utils.process import system |
|
18 | 18 | from IPython.utils import py3compat |
|
19 | 19 | from IPython.utils.decorators import undoc |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Code |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | fs_encoding = sys.getfilesystemencoding() |
|
26 | 26 | |
|
27 | 27 | def _writable_dir(path): |
|
28 | 28 | """Whether `path` is a directory, to which the user has write access.""" |
|
29 | 29 | return os.path.isdir(path) and os.access(path, os.W_OK) |
|
30 | 30 | |
|
31 | 31 | if sys.platform == 'win32': |
|
32 | 32 | def _get_long_path_name(path): |
|
33 | 33 | """Get a long path name (expand ~) on Windows using ctypes. |
|
34 | 34 | |
|
35 | 35 | Examples |
|
36 | 36 | -------- |
|
37 | 37 | |
|
38 | 38 | >>> get_long_path_name('c:\\docume~1') |
|
39 | 39 | 'c:\\\\Documents and Settings' |
|
40 | 40 | |
|
41 | 41 | """ |
|
42 | 42 | try: |
|
43 | 43 | import ctypes |
|
44 | 44 | except ImportError: |
|
45 | 45 | raise ImportError('you need to have ctypes installed for this to work') |
|
46 | 46 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
47 | 47 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
48 | 48 | ctypes.c_uint ] |
|
49 | 49 | |
|
50 | 50 | buf = ctypes.create_unicode_buffer(260) |
|
51 | 51 | rv = _GetLongPathName(path, buf, 260) |
|
52 | 52 | if rv == 0 or rv > 260: |
|
53 | 53 | return path |
|
54 | 54 | else: |
|
55 | 55 | return buf.value |
|
56 | 56 | else: |
|
57 | 57 | def _get_long_path_name(path): |
|
58 | 58 | """Dummy no-op.""" |
|
59 | 59 | return path |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | def get_long_path_name(path): |
|
64 | 64 | """Expand a path into its long form. |
|
65 | 65 | |
|
66 | 66 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
67 | 67 | a null operation. |
|
68 | 68 | """ |
|
69 | 69 | return _get_long_path_name(path) |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def unquote_filename(name, win32=(sys.platform=='win32')): |
|
73 | 73 | """ On Windows, remove leading and trailing quotes from filenames. |
|
74 | 74 | |
|
75 | 75 | This function has been deprecated and should not be used any more: |
|
76 | 76 | unquoting is now taken care of by :func:`IPython.utils.process.arg_split`. |
|
77 | 77 | """ |
|
78 | 78 | warn("'unquote_filename' is deprecated since IPython 5.0 and should not " |
|
79 | 79 | "be used anymore", DeprecationWarning, stacklevel=2) |
|
80 | 80 | if win32: |
|
81 | 81 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): |
|
82 | 82 | name = name[1:-1] |
|
83 | 83 | return name |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | def compress_user(path): |
|
87 | 87 | """Reverse of :func:`os.path.expanduser` |
|
88 | 88 | """ |
|
89 | 89 | home = os.path.expanduser('~') |
|
90 | 90 | if path.startswith(home): |
|
91 | 91 | path = "~" + path[len(home):] |
|
92 | 92 | return path |
|
93 | 93 | |
|
94 | 94 | def get_py_filename(name, force_win32=None): |
|
95 | 95 | """Return a valid python filename in the current directory. |
|
96 | 96 | |
|
97 | 97 | If the given name is not a file, it adds '.py' and searches again. |
|
98 | 98 | Raises IOError with an informative message if the file isn't found. |
|
99 | 99 | """ |
|
100 | 100 | |
|
101 | 101 | name = os.path.expanduser(name) |
|
102 | 102 | if force_win32 is not None: |
|
103 | 103 | warn("The 'force_win32' argument to 'get_py_filename' is deprecated " |
|
104 | 104 | "since IPython 5.0 and should not be used anymore", |
|
105 | 105 | DeprecationWarning, stacklevel=2) |
|
106 | 106 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
107 | 107 | name += '.py' |
|
108 | 108 | if os.path.isfile(name): |
|
109 | 109 | return name |
|
110 | 110 | else: |
|
111 | 111 | raise IOError('File `%r` not found.' % name) |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | def filefind(filename, path_dirs=None): |
|
115 | 115 | """Find a file by looking through a sequence of paths. |
|
116 | 116 | |
|
117 | 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 | 119 | path dirs is given, the filename is tested as is, after running through |
|
120 | 120 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
121 | 121 | |
|
122 | 122 | filefind('myfile.txt') |
|
123 | 123 | |
|
124 | 124 | will find the file in the current working dir, but:: |
|
125 | 125 | |
|
126 | 126 | filefind('~/myfile.txt') |
|
127 | 127 | |
|
128 | 128 | Will find the file in the users home directory. This function does not |
|
129 | 129 | automatically try any paths, such as the cwd or the user's home directory. |
|
130 | 130 | |
|
131 | 131 | Parameters |
|
132 | 132 | ---------- |
|
133 | 133 | filename : str |
|
134 | 134 | The filename to look for. |
|
135 | 135 | path_dirs : str, None or sequence of str |
|
136 | 136 | The sequence of paths to look for the file in. If None, the filename |
|
137 | 137 | need to be absolute or be in the cwd. If a string, the string is |
|
138 | 138 | put into a sequence and the searched. If a sequence, walk through |
|
139 | 139 | each element and join with ``filename``, calling :func:`expandvars` |
|
140 | 140 | and :func:`expanduser` before testing for existence. |
|
141 | 141 | |
|
142 | 142 | Returns |
|
143 | 143 | ------- |
|
144 | 144 | Raises :exc:`IOError` or returns absolute path to file. |
|
145 | 145 | """ |
|
146 | 146 | |
|
147 | 147 | # If paths are quoted, abspath gets confused, strip them... |
|
148 | 148 | filename = filename.strip('"').strip("'") |
|
149 | 149 | # If the input is an absolute path, just check it exists |
|
150 | 150 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
151 | 151 | return filename |
|
152 | 152 | |
|
153 | 153 | if path_dirs is None: |
|
154 | 154 | path_dirs = ("",) |
|
155 | 155 | elif isinstance(path_dirs, str): |
|
156 | 156 | path_dirs = (path_dirs,) |
|
157 | 157 | |
|
158 | 158 | for path in path_dirs: |
|
159 | 159 | if path == '.': path = os.getcwd() |
|
160 | 160 | testname = expand_path(os.path.join(path, filename)) |
|
161 | 161 | if os.path.isfile(testname): |
|
162 | 162 | return os.path.abspath(testname) |
|
163 | 163 | |
|
164 | 164 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
165 | 165 | (filename, path_dirs) ) |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | class HomeDirError(Exception): |
|
169 | 169 | pass |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | def get_home_dir(require_writable=False): |
|
173 | 173 | """Return the 'home' directory, as a unicode string. |
|
174 | 174 | |
|
175 | 175 | Uses os.path.expanduser('~'), and checks for writability. |
|
176 | 176 | |
|
177 | 177 | See stdlib docs for how this is determined. |
|
178 | 178 | $HOME is first priority on *ALL* platforms. |
|
179 | 179 | |
|
180 | 180 | Parameters |
|
181 | 181 | ---------- |
|
182 | 182 | |
|
183 | 183 | require_writable : bool [default: False] |
|
184 | 184 | if True: |
|
185 | 185 | guarantees the return value is a writable directory, otherwise |
|
186 | 186 | raises HomeDirError |
|
187 | 187 | if False: |
|
188 | 188 | The path is resolved, but it is not guaranteed to exist or be writable. |
|
189 | 189 | """ |
|
190 | 190 | |
|
191 | 191 | homedir = os.path.expanduser('~') |
|
192 | 192 | # Next line will make things work even when /home/ is a symlink to |
|
193 | 193 | # /usr/home as it is on FreeBSD, for example |
|
194 | 194 | homedir = os.path.realpath(homedir) |
|
195 | 195 | |
|
196 | 196 | if not _writable_dir(homedir) and os.name == 'nt': |
|
197 | 197 | # expanduser failed, use the registry to get the 'My Documents' folder. |
|
198 | 198 | try: |
|
199 | 199 | try: |
|
200 | 200 | import winreg as wreg # Py 3 |
|
201 | 201 | except ImportError: |
|
202 | 202 | import _winreg as wreg # Py 2 |
|
203 | 203 | key = wreg.OpenKey( |
|
204 | 204 | wreg.HKEY_CURRENT_USER, |
|
205 | 205 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |
|
206 | 206 | ) |
|
207 | 207 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
208 | 208 | key.Close() |
|
209 | 209 | except: |
|
210 | 210 | pass |
|
211 | 211 | |
|
212 | 212 | if (not require_writable) or _writable_dir(homedir): |
|
213 | 213 | return py3compat.cast_unicode(homedir, fs_encoding) |
|
214 | 214 | else: |
|
215 | 215 | raise HomeDirError('%s is not a writable dir, ' |
|
216 | 216 | 'set $HOME environment variable to override' % homedir) |
|
217 | 217 | |
|
218 | 218 | def get_xdg_dir(): |
|
219 | 219 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
220 | 220 | |
|
221 | 221 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
222 | 222 | """ |
|
223 | 223 | |
|
224 | 224 | env = os.environ |
|
225 | 225 | |
|
226 | 226 | if os.name == 'posix' and sys.platform != 'darwin': |
|
227 | 227 | # Linux, Unix, AIX, etc. |
|
228 | 228 | # use ~/.config if empty OR not set |
|
229 | 229 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
230 | 230 | if xdg and _writable_dir(xdg): |
|
231 | 231 | return py3compat.cast_unicode(xdg, fs_encoding) |
|
232 | 232 | |
|
233 | 233 | return None |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | def get_xdg_cache_dir(): |
|
237 | 237 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. |
|
238 | 238 | |
|
239 | 239 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
240 | 240 | """ |
|
241 | 241 | |
|
242 | 242 | env = os.environ |
|
243 | 243 | |
|
244 | 244 | if os.name == 'posix' and sys.platform != 'darwin': |
|
245 | 245 | # Linux, Unix, AIX, etc. |
|
246 | 246 | # use ~/.cache if empty OR not set |
|
247 | 247 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') |
|
248 | 248 | if xdg and _writable_dir(xdg): |
|
249 | 249 | return py3compat.cast_unicode(xdg, fs_encoding) |
|
250 | 250 | |
|
251 | 251 | return None |
|
252 | 252 | |
|
253 | 253 | |
|
254 | 254 | @undoc |
|
255 | 255 | def get_ipython_dir(): |
|
256 | 256 | warn("get_ipython_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) |
|
257 | 257 | from IPython.paths import get_ipython_dir |
|
258 | 258 | return get_ipython_dir() |
|
259 | 259 | |
|
260 | 260 | @undoc |
|
261 | 261 | def get_ipython_cache_dir(): |
|
262 | 262 | warn("get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) |
|
263 | 263 | from IPython.paths import get_ipython_cache_dir |
|
264 | 264 | return get_ipython_cache_dir() |
|
265 | 265 | |
|
266 | 266 | @undoc |
|
267 | 267 | def get_ipython_package_dir(): |
|
268 | 268 | warn("get_ipython_package_dir has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) |
|
269 | 269 | from IPython.paths import get_ipython_package_dir |
|
270 | 270 | return get_ipython_package_dir() |
|
271 | 271 | |
|
272 | 272 | @undoc |
|
273 | 273 | def get_ipython_module_path(module_str): |
|
274 | 274 | warn("get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) |
|
275 | 275 | from IPython.paths import get_ipython_module_path |
|
276 | 276 | return get_ipython_module_path(module_str) |
|
277 | 277 | |
|
278 | 278 | @undoc |
|
279 | 279 | def locate_profile(profile='default'): |
|
280 | 280 | warn("locate_profile has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) |
|
281 | 281 | from IPython.paths import locate_profile |
|
282 | 282 | return locate_profile(profile=profile) |
|
283 | 283 | |
|
284 | 284 | def expand_path(s): |
|
285 | 285 | """Expand $VARS and ~names in a string, like a shell |
|
286 | 286 | |
|
287 | 287 | :Examples: |
|
288 | 288 | |
|
289 | 289 | In [2]: os.environ['FOO']='test' |
|
290 | 290 | |
|
291 | 291 | In [3]: expand_path('variable FOO is $FOO') |
|
292 | 292 | Out[3]: 'variable FOO is test' |
|
293 | 293 | """ |
|
294 | 294 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
295 | 295 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
296 | 296 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
297 | 297 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
298 | 298 | # a hidden share). |
|
299 | 299 | if os.name=='nt': |
|
300 | 300 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
301 | 301 | s = os.path.expandvars(os.path.expanduser(s)) |
|
302 | 302 | if os.name=='nt': |
|
303 | 303 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
304 | 304 | return s |
|
305 | 305 | |
|
306 | 306 | |
|
307 | 307 | def unescape_glob(string): |
|
308 | 308 | """Unescape glob pattern in `string`.""" |
|
309 | 309 | def unescape(s): |
|
310 | 310 | for pattern in '*[]!?': |
|
311 | 311 | s = s.replace(r'\{0}'.format(pattern), pattern) |
|
312 | 312 | return s |
|
313 | 313 | return '\\'.join(map(unescape, string.split('\\\\'))) |
|
314 | 314 | |
|
315 | 315 | |
|
316 | 316 | def shellglob(args): |
|
317 | 317 | """ |
|
318 | 318 | Do glob expansion for each element in `args` and return a flattened list. |
|
319 | 319 | |
|
320 | 320 | Unmatched glob pattern will remain as-is in the returned list. |
|
321 | 321 | |
|
322 | 322 | """ |
|
323 | 323 | expanded = [] |
|
324 | 324 | # Do not unescape backslash in Windows as it is interpreted as |
|
325 | 325 | # path separator: |
|
326 | 326 | unescape = unescape_glob if sys.platform != 'win32' else lambda x: x |
|
327 | 327 | for a in args: |
|
328 | 328 | expanded.extend(glob.glob(a) or [unescape(a)]) |
|
329 | 329 | return expanded |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | def target_outdated(target,deps): |
|
333 | 333 | """Determine whether a target is out of date. |
|
334 | 334 | |
|
335 | 335 | target_outdated(target,deps) -> 1/0 |
|
336 | 336 | |
|
337 | 337 | deps: list of filenames which MUST exist. |
|
338 | 338 | target: single filename which may or may not exist. |
|
339 | 339 | |
|
340 | 340 | If target doesn't exist or is older than any file listed in deps, return |
|
341 | 341 | true, otherwise return false. |
|
342 | 342 | """ |
|
343 | 343 | try: |
|
344 | 344 | target_time = os.path.getmtime(target) |
|
345 | 345 | except os.error: |
|
346 | 346 | return 1 |
|
347 | 347 | for dep in deps: |
|
348 | 348 | dep_time = os.path.getmtime(dep) |
|
349 | 349 | if dep_time > target_time: |
|
350 | 350 | #print "For target",target,"Dep failed:",dep # dbg |
|
351 | 351 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
352 | 352 | return 1 |
|
353 | 353 | return 0 |
|
354 | 354 | |
|
355 | 355 | |
|
356 | 356 | def target_update(target,deps,cmd): |
|
357 | 357 | """Update a target with a given command given a list of dependencies. |
|
358 | 358 | |
|
359 | 359 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
360 | 360 | |
|
361 | 361 | This is just a wrapper around target_outdated() which calls the given |
|
362 | 362 | command if target is outdated.""" |
|
363 | 363 | |
|
364 | 364 | if target_outdated(target,deps): |
|
365 | 365 | system(cmd) |
|
366 | 366 | |
|
367 | 367 | |
|
368 | 368 | ENOLINK = 1998 |
|
369 | 369 | |
|
370 | 370 | def link(src, dst): |
|
371 | 371 | """Hard links ``src`` to ``dst``, returning 0 or errno. |
|
372 | 372 | |
|
373 | 373 | Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't |
|
374 | 374 | supported by the operating system. |
|
375 | 375 | """ |
|
376 | 376 | |
|
377 | 377 | if not hasattr(os, "link"): |
|
378 | 378 | return ENOLINK |
|
379 | 379 | link_errno = 0 |
|
380 | 380 | try: |
|
381 | 381 | os.link(src, dst) |
|
382 | 382 | except OSError as e: |
|
383 | 383 | link_errno = e.errno |
|
384 | 384 | return link_errno |
|
385 | 385 | |
|
386 | 386 | |
|
387 | 387 | def link_or_copy(src, dst): |
|
388 | 388 | """Attempts to hardlink ``src`` to ``dst``, copying if the link fails. |
|
389 | 389 | |
|
390 | 390 | Attempts to maintain the semantics of ``shutil.copy``. |
|
391 | 391 | |
|
392 | 392 | Because ``os.link`` does not overwrite files, a unique temporary file |
|
393 | 393 | will be used if the target already exists, then that file will be moved |
|
394 | 394 | into place. |
|
395 | 395 | """ |
|
396 | 396 | |
|
397 | 397 | if os.path.isdir(dst): |
|
398 | 398 | dst = os.path.join(dst, os.path.basename(src)) |
|
399 | 399 | |
|
400 | 400 | link_errno = link(src, dst) |
|
401 | 401 | if link_errno == errno.EEXIST: |
|
402 | 402 | if os.stat(src).st_ino == os.stat(dst).st_ino: |
|
403 | 403 | # dst is already a hard link to the correct file, so we don't need |
|
404 | 404 | # to do anything else. If we try to link and rename the file |
|
405 | 405 | # anyway, we get duplicate files - see http://bugs.python.org/issue21876 |
|
406 | 406 | return |
|
407 | 407 | |
|
408 | 408 | new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), ) |
|
409 | 409 | try: |
|
410 | 410 | link_or_copy(src, new_dst) |
|
411 | 411 | except: |
|
412 | 412 | try: |
|
413 | 413 | os.remove(new_dst) |
|
414 | 414 | except OSError: |
|
415 | 415 | pass |
|
416 | 416 | raise |
|
417 | 417 | os.rename(new_dst, dst) |
|
418 | 418 | elif link_errno != 0: |
|
419 | 419 | # Either link isn't supported, or the filesystem doesn't support |
|
420 | 420 | # linking, or 'src' and 'dst' are on different filesystems. |
|
421 | 421 | shutil.copy(src, dst) |
|
422 | 422 | |
|
423 | 423 | def ensure_dir_exists(path, mode=0o755): |
|
424 | 424 | """ensure that a directory exists |
|
425 | 425 | |
|
426 | 426 | If it doesn't exist, try to create it and protect against a race condition |
|
427 | 427 | if another process is doing the same. |
|
428 | 428 | |
|
429 | 429 | The default permissions are 755, which differ from os.makedirs default of 777. |
|
430 | 430 | """ |
|
431 | 431 | if not os.path.exists(path): |
|
432 | 432 | try: |
|
433 | 433 | os.makedirs(path, mode=mode) |
|
434 | 434 | except OSError as e: |
|
435 | 435 | if e.errno != errno.EEXIST: |
|
436 | 436 | raise |
|
437 | 437 | elif not os.path.isdir(path): |
|
438 | 438 | raise IOError("%r exists but is not a directory" % path) |
@@ -1,481 +1,481 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for IPython.utils.path.py""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | import os |
|
8 | 8 | import shutil |
|
9 | 9 | import sys |
|
10 | 10 | import tempfile |
|
11 | 11 | import unittest |
|
12 | 12 | from contextlib import contextmanager |
|
13 | 13 | from unittest.mock import patch |
|
14 | 14 | from os.path import join, abspath |
|
15 | 15 | from imp import reload |
|
16 | 16 | |
|
17 | 17 | from nose import SkipTest, with_setup |
|
18 | 18 | import nose.tools as nt |
|
19 | 19 | |
|
20 | 20 | import IPython |
|
21 | 21 | from IPython import paths |
|
22 | 22 | from IPython.testing import decorators as dec |
|
23 | 23 | from IPython.testing.decorators import (skip_if_not_win32, skip_win32, |
|
24 | 24 | onlyif_unicode_paths,) |
|
25 | 25 | from IPython.testing.tools import make_tempfile, AssertPrints |
|
26 | 26 | from IPython.utils import path |
|
27 | 27 | from IPython.utils.tempdir import TemporaryDirectory |
|
28 | 28 | |
|
29 | 29 | # Platform-dependent imports |
|
30 | 30 | try: |
|
31 | 31 | import winreg as wreg |
|
32 | 32 | except ImportError: |
|
33 | 33 | #Fake _winreg module on non-windows platforms |
|
34 | 34 | import types |
|
35 | 35 | wr_name = "winreg" |
|
36 | 36 | sys.modules[wr_name] = types.ModuleType(wr_name) |
|
37 | 37 | try: |
|
38 | 38 | import winreg as wreg |
|
39 | 39 | except ImportError: |
|
40 | 40 | import _winreg as wreg |
|
41 | 41 | #Add entries that needs to be stubbed by the testing code |
|
42 | 42 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
43 | 43 | |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | # Globals |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | env = os.environ |
|
48 | 48 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
49 | 49 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
50 | 50 | # |
|
51 | 51 | # Setup/teardown functions/decorators |
|
52 | 52 | # |
|
53 | 53 | |
|
54 | 54 | def setup(): |
|
55 | 55 | """Setup testenvironment for the module: |
|
56 | 56 | |
|
57 | 57 | - Adds dummy home dir tree |
|
58 | 58 | """ |
|
59 | 59 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
60 | 60 | # problem because that exception is only defined on Windows... |
|
61 | 61 | os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython')) |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | def teardown(): |
|
65 | 65 | """Teardown testenvironment for the module: |
|
66 | 66 | |
|
67 | 67 | - Remove dummy home dir tree |
|
68 | 68 | """ |
|
69 | 69 | # Note: we remove the parent test dir, which is the root of all test |
|
70 | 70 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
71 | 71 | # that non-empty directories are all recursively removed. |
|
72 | 72 | shutil.rmtree(TMP_TEST_DIR) |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | def setup_environment(): |
|
76 | 76 | """Setup testenvironment for some functions that are tested |
|
77 | 77 | in this module. In particular this functions stores attributes |
|
78 | 78 | and other things that we need to stub in some test functions. |
|
79 | 79 | This needs to be done on a function level and not module level because |
|
80 | 80 | each testfunction needs a pristine environment. |
|
81 | 81 | """ |
|
82 | 82 | global oldstuff, platformstuff |
|
83 | 83 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) |
|
84 | 84 | |
|
85 | 85 | def teardown_environment(): |
|
86 | 86 | """Restore things that were remembered by the setup_environment function |
|
87 | 87 | """ |
|
88 | 88 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff |
|
89 | 89 | os.chdir(old_wd) |
|
90 | 90 | reload(path) |
|
91 | 91 | |
|
92 | 92 | for key in list(env): |
|
93 | 93 | if key not in oldenv: |
|
94 | 94 | del env[key] |
|
95 | 95 | env.update(oldenv) |
|
96 | 96 | if hasattr(sys, 'frozen'): |
|
97 | 97 | del sys.frozen |
|
98 | 98 | |
|
99 | 99 | # Build decorator that uses the setup_environment/setup_environment |
|
100 | 100 | with_environment = with_setup(setup_environment, teardown_environment) |
|
101 | 101 | |
|
102 | 102 | @skip_if_not_win32 |
|
103 | 103 | @with_environment |
|
104 | 104 | def test_get_home_dir_1(): |
|
105 | 105 | """Testcase for py2exe logic, un-compressed lib |
|
106 | 106 | """ |
|
107 | 107 | unfrozen = path.get_home_dir() |
|
108 | 108 | sys.frozen = True |
|
109 | 109 | |
|
110 | 110 | #fake filename for IPython.__init__ |
|
111 | 111 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
112 | 112 | |
|
113 | 113 | home_dir = path.get_home_dir() |
|
114 | 114 | nt.assert_equal(home_dir, unfrozen) |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | @skip_if_not_win32 |
|
118 | 118 | @with_environment |
|
119 | 119 | def test_get_home_dir_2(): |
|
120 | 120 | """Testcase for py2exe logic, compressed lib |
|
121 | 121 | """ |
|
122 | 122 | unfrozen = path.get_home_dir() |
|
123 | 123 | sys.frozen = True |
|
124 | 124 | #fake filename for IPython.__init__ |
|
125 | 125 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
126 | 126 | |
|
127 | 127 | home_dir = path.get_home_dir(True) |
|
128 | 128 | nt.assert_equal(home_dir, unfrozen) |
|
129 | 129 | |
|
130 | 130 | |
|
131 | 131 | @with_environment |
|
132 | 132 | def test_get_home_dir_3(): |
|
133 | 133 | """get_home_dir() uses $HOME if set""" |
|
134 | 134 | env["HOME"] = HOME_TEST_DIR |
|
135 | 135 | home_dir = path.get_home_dir(True) |
|
136 | 136 | # get_home_dir expands symlinks |
|
137 | 137 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | @with_environment |
|
141 | 141 | def test_get_home_dir_4(): |
|
142 | 142 | """get_home_dir() still works if $HOME is not set""" |
|
143 | 143 | |
|
144 | 144 | if 'HOME' in env: del env['HOME'] |
|
145 | 145 | # this should still succeed, but we don't care what the answer is |
|
146 | 146 | home = path.get_home_dir(False) |
|
147 | 147 | |
|
148 | 148 | @with_environment |
|
149 | 149 | def test_get_home_dir_5(): |
|
150 | 150 | """raise HomeDirError if $HOME is specified, but not a writable dir""" |
|
151 | 151 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') |
|
152 | 152 | # set os.name = posix, to prevent My Documents fallback on Windows |
|
153 | 153 | os.name = 'posix' |
|
154 | 154 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) |
|
155 | 155 | |
|
156 | 156 | # Should we stub wreg fully so we can run the test on all platforms? |
|
157 | 157 | @skip_if_not_win32 |
|
158 | 158 | @with_environment |
|
159 | 159 | def test_get_home_dir_8(): |
|
160 | 160 | """Using registry hack for 'My Documents', os=='nt' |
|
161 | 161 | |
|
162 | 162 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. |
|
163 | 163 | """ |
|
164 | 164 | os.name = 'nt' |
|
165 | 165 | # Remove from stub environment all keys that may be set |
|
166 | 166 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: |
|
167 | 167 | env.pop(key, None) |
|
168 | 168 | |
|
169 | 169 | class key: |
|
170 | 170 | def Close(self): |
|
171 | 171 | pass |
|
172 | 172 | |
|
173 | 173 | with patch.object(wreg, 'OpenKey', return_value=key()), \ |
|
174 | 174 | patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]): |
|
175 | 175 | home_dir = path.get_home_dir() |
|
176 | 176 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
177 | 177 | |
|
178 | 178 | @with_environment |
|
179 | 179 | def test_get_xdg_dir_0(): |
|
180 | 180 | """test_get_xdg_dir_0, check xdg_dir""" |
|
181 | 181 | reload(path) |
|
182 | 182 | path._writable_dir = lambda path: True |
|
183 | 183 | path.get_home_dir = lambda : 'somewhere' |
|
184 | 184 | os.name = "posix" |
|
185 | 185 | sys.platform = "linux2" |
|
186 | 186 | env.pop('IPYTHON_DIR', None) |
|
187 | 187 | env.pop('IPYTHONDIR', None) |
|
188 | 188 | env.pop('XDG_CONFIG_HOME', None) |
|
189 | 189 | |
|
190 | 190 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
191 | 191 | |
|
192 | 192 | |
|
193 | 193 | @with_environment |
|
194 | 194 | def test_get_xdg_dir_1(): |
|
195 |
"""test_get_xdg_dir_1, check nonexist |
|
|
195 | """test_get_xdg_dir_1, check nonexistent xdg_dir""" | |
|
196 | 196 | reload(path) |
|
197 | 197 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
198 | 198 | os.name = "posix" |
|
199 | 199 | sys.platform = "linux2" |
|
200 | 200 | env.pop('IPYTHON_DIR', None) |
|
201 | 201 | env.pop('IPYTHONDIR', None) |
|
202 | 202 | env.pop('XDG_CONFIG_HOME', None) |
|
203 | 203 | nt.assert_equal(path.get_xdg_dir(), None) |
|
204 | 204 | |
|
205 | 205 | @with_environment |
|
206 | 206 | def test_get_xdg_dir_2(): |
|
207 | 207 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
208 | 208 | reload(path) |
|
209 | 209 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
210 | 210 | os.name = "posix" |
|
211 | 211 | sys.platform = "linux2" |
|
212 | 212 | env.pop('IPYTHON_DIR', None) |
|
213 | 213 | env.pop('IPYTHONDIR', None) |
|
214 | 214 | env.pop('XDG_CONFIG_HOME', None) |
|
215 | 215 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
216 | 216 | if not os.path.exists(cfgdir): |
|
217 | 217 | os.makedirs(cfgdir) |
|
218 | 218 | |
|
219 | 219 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
220 | 220 | |
|
221 | 221 | @with_environment |
|
222 | 222 | def test_get_xdg_dir_3(): |
|
223 | 223 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" |
|
224 | 224 | reload(path) |
|
225 | 225 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
226 | 226 | os.name = "posix" |
|
227 | 227 | sys.platform = "darwin" |
|
228 | 228 | env.pop('IPYTHON_DIR', None) |
|
229 | 229 | env.pop('IPYTHONDIR', None) |
|
230 | 230 | env.pop('XDG_CONFIG_HOME', None) |
|
231 | 231 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
232 | 232 | if not os.path.exists(cfgdir): |
|
233 | 233 | os.makedirs(cfgdir) |
|
234 | 234 | |
|
235 | 235 | nt.assert_equal(path.get_xdg_dir(), None) |
|
236 | 236 | |
|
237 | 237 | def test_filefind(): |
|
238 | 238 | """Various tests for filefind""" |
|
239 | 239 | f = tempfile.NamedTemporaryFile() |
|
240 | 240 | # print 'fname:',f.name |
|
241 | 241 | alt_dirs = paths.get_ipython_dir() |
|
242 | 242 | t = path.filefind(f.name, alt_dirs) |
|
243 | 243 | # print 'found:',t |
|
244 | 244 | |
|
245 | 245 | |
|
246 | 246 | @dec.skip_if_not_win32 |
|
247 | 247 | def test_get_long_path_name_win32(): |
|
248 | 248 | with TemporaryDirectory() as tmpdir: |
|
249 | 249 | |
|
250 | 250 | # Make a long path. Expands the path of tmpdir prematurely as it may already have a long |
|
251 | 251 | # path component, so ensure we include the long form of it |
|
252 | 252 | long_path = os.path.join(path.get_long_path_name(tmpdir), 'this is my long path name') |
|
253 | 253 | os.makedirs(long_path) |
|
254 | 254 | |
|
255 | 255 | # Test to see if the short path evaluates correctly. |
|
256 | 256 | short_path = os.path.join(tmpdir, 'THISIS~1') |
|
257 | 257 | evaluated_path = path.get_long_path_name(short_path) |
|
258 | 258 | nt.assert_equal(evaluated_path.lower(), long_path.lower()) |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | @dec.skip_win32 |
|
262 | 262 | def test_get_long_path_name(): |
|
263 | 263 | p = path.get_long_path_name('/usr/local') |
|
264 | 264 | nt.assert_equal(p,'/usr/local') |
|
265 | 265 | |
|
266 | 266 | @dec.skip_win32 # can't create not-user-writable dir on win |
|
267 | 267 | @with_environment |
|
268 | 268 | def test_not_writable_ipdir(): |
|
269 | 269 | tmpdir = tempfile.mkdtemp() |
|
270 | 270 | os.name = "posix" |
|
271 | 271 | env.pop('IPYTHON_DIR', None) |
|
272 | 272 | env.pop('IPYTHONDIR', None) |
|
273 | 273 | env.pop('XDG_CONFIG_HOME', None) |
|
274 | 274 | env['HOME'] = tmpdir |
|
275 | 275 | ipdir = os.path.join(tmpdir, '.ipython') |
|
276 | 276 | os.mkdir(ipdir, 0o555) |
|
277 | 277 | try: |
|
278 | 278 | open(os.path.join(ipdir, "_foo_"), 'w').close() |
|
279 | 279 | except IOError: |
|
280 | 280 | pass |
|
281 | 281 | else: |
|
282 | 282 | # I can still write to an unwritable dir, |
|
283 | 283 | # assume I'm root and skip the test |
|
284 | 284 | raise SkipTest("I can't create directories that I can't write to") |
|
285 | 285 | with AssertPrints('is not a writable location', channel='stderr'): |
|
286 | 286 | ipdir = paths.get_ipython_dir() |
|
287 | 287 | env.pop('IPYTHON_DIR', None) |
|
288 | 288 | |
|
289 | 289 | @with_environment |
|
290 | 290 | def test_get_py_filename(): |
|
291 | 291 | os.chdir(TMP_TEST_DIR) |
|
292 | 292 | with make_tempfile('foo.py'): |
|
293 | 293 | nt.assert_equal(path.get_py_filename('foo.py'), 'foo.py') |
|
294 | 294 | nt.assert_equal(path.get_py_filename('foo'), 'foo.py') |
|
295 | 295 | with make_tempfile('foo'): |
|
296 | 296 | nt.assert_equal(path.get_py_filename('foo'), 'foo') |
|
297 | 297 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py') |
|
298 | 298 | nt.assert_raises(IOError, path.get_py_filename, 'foo') |
|
299 | 299 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py') |
|
300 | 300 | true_fn = 'foo with spaces.py' |
|
301 | 301 | with make_tempfile(true_fn): |
|
302 | 302 | nt.assert_equal(path.get_py_filename('foo with spaces'), true_fn) |
|
303 | 303 | nt.assert_equal(path.get_py_filename('foo with spaces.py'), true_fn) |
|
304 | 304 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"') |
|
305 | 305 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'") |
|
306 | 306 | |
|
307 | 307 | @onlyif_unicode_paths |
|
308 | 308 | def test_unicode_in_filename(): |
|
309 | 309 | """When a file doesn't exist, the exception raised should be safe to call |
|
310 | 310 | str() on - i.e. in Python 2 it must only have ASCII characters. |
|
311 | 311 | |
|
312 | 312 | https://github.com/ipython/ipython/issues/875 |
|
313 | 313 | """ |
|
314 | 314 | try: |
|
315 | 315 | # these calls should not throw unicode encode exceptions |
|
316 | 316 | path.get_py_filename('fooéè.py', force_win32=False) |
|
317 | 317 | except IOError as ex: |
|
318 | 318 | str(ex) |
|
319 | 319 | |
|
320 | 320 | |
|
321 | 321 | class TestShellGlob(unittest.TestCase): |
|
322 | 322 | |
|
323 | 323 | @classmethod |
|
324 | 324 | def setUpClass(cls): |
|
325 | 325 | cls.filenames_start_with_a = ['a0', 'a1', 'a2'] |
|
326 | 326 | cls.filenames_end_with_b = ['0b', '1b', '2b'] |
|
327 | 327 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b |
|
328 | 328 | cls.tempdir = TemporaryDirectory() |
|
329 | 329 | td = cls.tempdir.name |
|
330 | 330 | |
|
331 | 331 | with cls.in_tempdir(): |
|
332 | 332 | # Create empty files |
|
333 | 333 | for fname in cls.filenames: |
|
334 | 334 | open(os.path.join(td, fname), 'w').close() |
|
335 | 335 | |
|
336 | 336 | @classmethod |
|
337 | 337 | def tearDownClass(cls): |
|
338 | 338 | cls.tempdir.cleanup() |
|
339 | 339 | |
|
340 | 340 | @classmethod |
|
341 | 341 | @contextmanager |
|
342 | 342 | def in_tempdir(cls): |
|
343 | 343 | save = os.getcwd() |
|
344 | 344 | try: |
|
345 | 345 | os.chdir(cls.tempdir.name) |
|
346 | 346 | yield |
|
347 | 347 | finally: |
|
348 | 348 | os.chdir(save) |
|
349 | 349 | |
|
350 | 350 | def check_match(self, patterns, matches): |
|
351 | 351 | with self.in_tempdir(): |
|
352 | 352 | # glob returns unordered list. that's why sorted is required. |
|
353 | 353 | nt.assert_equal(sorted(path.shellglob(patterns)), |
|
354 | 354 | sorted(matches)) |
|
355 | 355 | |
|
356 | 356 | def common_cases(self): |
|
357 | 357 | return [ |
|
358 | 358 | (['*'], self.filenames), |
|
359 | 359 | (['a*'], self.filenames_start_with_a), |
|
360 | 360 | (['*c'], ['*c']), |
|
361 | 361 | (['*', 'a*', '*b', '*c'], self.filenames |
|
362 | 362 | + self.filenames_start_with_a |
|
363 | 363 | + self.filenames_end_with_b |
|
364 | 364 | + ['*c']), |
|
365 | 365 | (['a[012]'], self.filenames_start_with_a), |
|
366 | 366 | ] |
|
367 | 367 | |
|
368 | 368 | @skip_win32 |
|
369 | 369 | def test_match_posix(self): |
|
370 | 370 | for (patterns, matches) in self.common_cases() + [ |
|
371 | 371 | ([r'\*'], ['*']), |
|
372 | 372 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), |
|
373 | 373 | ([r'a\[012]'], ['a[012]']), |
|
374 | 374 | ]: |
|
375 | 375 | yield (self.check_match, patterns, matches) |
|
376 | 376 | |
|
377 | 377 | @skip_if_not_win32 |
|
378 | 378 | def test_match_windows(self): |
|
379 | 379 | for (patterns, matches) in self.common_cases() + [ |
|
380 | 380 | # In windows, backslash is interpreted as path |
|
381 | 381 | # separator. Therefore, you can't escape glob |
|
382 | 382 | # using it. |
|
383 | 383 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), |
|
384 | 384 | ([r'a\[012]'], [r'a\[012]']), |
|
385 | 385 | ]: |
|
386 | 386 | yield (self.check_match, patterns, matches) |
|
387 | 387 | |
|
388 | 388 | |
|
389 | 389 | def test_unescape_glob(): |
|
390 | 390 | nt.assert_equal(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') |
|
391 | 391 | nt.assert_equal(path.unescape_glob(r'\\*'), r'\*') |
|
392 | 392 | nt.assert_equal(path.unescape_glob(r'\\\*'), r'\*') |
|
393 | 393 | nt.assert_equal(path.unescape_glob(r'\\a'), r'\a') |
|
394 | 394 | nt.assert_equal(path.unescape_glob(r'\a'), r'\a') |
|
395 | 395 | |
|
396 | 396 | |
|
397 | 397 | @onlyif_unicode_paths |
|
398 | 398 | def test_ensure_dir_exists(): |
|
399 | 399 | with TemporaryDirectory() as td: |
|
400 | 400 | d = os.path.join(td, '∂ir') |
|
401 | 401 | path.ensure_dir_exists(d) # create it |
|
402 | 402 | assert os.path.isdir(d) |
|
403 | 403 | path.ensure_dir_exists(d) # no-op |
|
404 | 404 | f = os.path.join(td, 'ƒile') |
|
405 | 405 | open(f, 'w').close() # touch |
|
406 | 406 | with nt.assert_raises(IOError): |
|
407 | 407 | path.ensure_dir_exists(f) |
|
408 | 408 | |
|
409 | 409 | class TestLinkOrCopy(object): |
|
410 | 410 | def setUp(self): |
|
411 | 411 | self.tempdir = TemporaryDirectory() |
|
412 | 412 | self.src = self.dst("src") |
|
413 | 413 | with open(self.src, "w") as f: |
|
414 | 414 | f.write("Hello, world!") |
|
415 | 415 | |
|
416 | 416 | def tearDown(self): |
|
417 | 417 | self.tempdir.cleanup() |
|
418 | 418 | |
|
419 | 419 | def dst(self, *args): |
|
420 | 420 | return os.path.join(self.tempdir.name, *args) |
|
421 | 421 | |
|
422 | 422 | def assert_inode_not_equal(self, a, b): |
|
423 | 423 | nt.assert_not_equal(os.stat(a).st_ino, os.stat(b).st_ino, |
|
424 | 424 | "%r and %r do reference the same indoes" %(a, b)) |
|
425 | 425 | |
|
426 | 426 | def assert_inode_equal(self, a, b): |
|
427 | 427 | nt.assert_equal(os.stat(a).st_ino, os.stat(b).st_ino, |
|
428 | 428 | "%r and %r do not reference the same indoes" %(a, b)) |
|
429 | 429 | |
|
430 | 430 | def assert_content_equal(self, a, b): |
|
431 | 431 | with open(a) as a_f: |
|
432 | 432 | with open(b) as b_f: |
|
433 | 433 | nt.assert_equal(a_f.read(), b_f.read()) |
|
434 | 434 | |
|
435 | 435 | @skip_win32 |
|
436 | 436 | def test_link_successful(self): |
|
437 | 437 | dst = self.dst("target") |
|
438 | 438 | path.link_or_copy(self.src, dst) |
|
439 | 439 | self.assert_inode_equal(self.src, dst) |
|
440 | 440 | |
|
441 | 441 | @skip_win32 |
|
442 | 442 | def test_link_into_dir(self): |
|
443 | 443 | dst = self.dst("some_dir") |
|
444 | 444 | os.mkdir(dst) |
|
445 | 445 | path.link_or_copy(self.src, dst) |
|
446 | 446 | expected_dst = self.dst("some_dir", os.path.basename(self.src)) |
|
447 | 447 | self.assert_inode_equal(self.src, expected_dst) |
|
448 | 448 | |
|
449 | 449 | @skip_win32 |
|
450 | 450 | def test_target_exists(self): |
|
451 | 451 | dst = self.dst("target") |
|
452 | 452 | open(dst, "w").close() |
|
453 | 453 | path.link_or_copy(self.src, dst) |
|
454 | 454 | self.assert_inode_equal(self.src, dst) |
|
455 | 455 | |
|
456 | 456 | @skip_win32 |
|
457 | 457 | def test_no_link(self): |
|
458 | 458 | real_link = os.link |
|
459 | 459 | try: |
|
460 | 460 | del os.link |
|
461 | 461 | dst = self.dst("target") |
|
462 | 462 | path.link_or_copy(self.src, dst) |
|
463 | 463 | self.assert_content_equal(self.src, dst) |
|
464 | 464 | self.assert_inode_not_equal(self.src, dst) |
|
465 | 465 | finally: |
|
466 | 466 | os.link = real_link |
|
467 | 467 | |
|
468 | 468 | @skip_if_not_win32 |
|
469 | 469 | def test_windows(self): |
|
470 | 470 | dst = self.dst("target") |
|
471 | 471 | path.link_or_copy(self.src, dst) |
|
472 | 472 | self.assert_content_equal(self.src, dst) |
|
473 | 473 | |
|
474 | 474 | def test_link_twice(self): |
|
475 | 475 | # Linking the same file twice shouldn't leave duplicates around. |
|
476 | 476 | # See https://github.com/ipython/ipython/issues/6450 |
|
477 | 477 | dst = self.dst('target') |
|
478 | 478 | path.link_or_copy(self.src, dst) |
|
479 | 479 | path.link_or_copy(self.src, dst) |
|
480 | 480 | self.assert_inode_equal(self.src, dst) |
|
481 | 481 | nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target']) |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | 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