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

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

@@ -1,2045 +1,2045 b''
1 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 informations to displayed to
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 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,614 +1,614 b''
1 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 informations on how to install packages:
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 informations
594 already computed information
595 595 detail_level: integer
596 Granularity of detail level, if set to 1, give more informations.
596 Granularity of detail level, if set to 1, give more information.
597 597 """
598 598
599 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 dictionnary.
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 explicitely does so, to catch direct calls and rebindings of
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="&nbsp;&nbsp;"):
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 # guarenteed to be executable?
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 registeres it's callbacks in ``PyOS_InputHook`` to integrate
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 convertion."""
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 nonexistant xdg_dir"""
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