##// END OF EJS Templates
Multiple improvements to tab completion....
Fernando Perez -
Show More
@@ -1,658 +1,722 b''
1 1 """Word completion for IPython.
2 2
3 3 This module is a 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, but we need a lot more
6 6 functionality specific to IPython, so this module will continue to live as an
7 7 IPython-specific utility.
8 8
9 9 Original rlcompleter documentation:
10 10
11 11 This requires the latest extension to the readline module (the
12 12 completes keywords, built-ins and globals in __main__; when completing
13 13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 14 completes its attributes.
15 15
16 16 It's very cool to do "import string" type "string.", hit the
17 17 completion key (twice), and see the list of names defined by the
18 18 string module!
19 19
20 20 Tip: to use the tab key as the completion key, call
21 21
22 22 readline.parse_and_bind("tab: complete")
23 23
24 24 Notes:
25 25
26 26 - Exceptions raised by the completer function are *ignored* (and
27 27 generally cause the completion to fail). This is a feature -- since
28 28 readline sets the tty device in raw (or cbreak) mode, printing a
29 29 traceback wouldn't work well without some complicated hoopla to save,
30 30 reset and restore the tty state.
31 31
32 32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 33 application defined code to be executed if an object with a
34 34 __getattr__ hook is found. Since it is the responsibility of the
35 35 application (or the user) to enable this feature, I consider this an
36 36 acceptable risk. More complicated expressions (e.g. function calls or
37 37 indexing operations) are *not* evaluated.
38 38
39 39 - GNU readline is also used by the built-in functions input() and
40 40 raw_input(), and thus these also benefit/suffer from the completer
41 41 features. Clearly an interactive application can benefit by
42 42 specifying its own completer function and using raw_input() for all
43 43 its input.
44 44
45 45 - When the original stdin is not a tty device, GNU readline is never
46 46 used, and this module (and the readline module) are silently inactive.
47 47 """
48 48
49 49 #*****************************************************************************
50 50 #
51 51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 52 # module which is part of the standard Python distribution, I assume that the
53 53 # proper procedure is to maintain its copyright as belonging to the Python
54 54 # Software Foundation (in addition to my own, for all new code).
55 55 #
56 56 # Copyright (C) 2008-2010 IPython Development Team
57 57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 59 #
60 60 # Distributed under the terms of the BSD License. The full license is in
61 61 # the file COPYING, distributed as part of this software.
62 62 #
63 63 #*****************************************************************************
64 64
65 65 #-----------------------------------------------------------------------------
66 66 # Imports
67 67 #-----------------------------------------------------------------------------
68 68
69 69 import __builtin__
70 70 import __main__
71 71 import glob
72 72 import inspect
73 73 import itertools
74 74 import keyword
75 75 import os
76 76 import re
77 77 import shlex
78 78 import sys
79 79
80 80 from IPython.core.error import TryNext
81 81 from IPython.core.prefilter import ESC_MAGIC
82 82 from IPython.utils import generics
83 83 from IPython.utils.frame import debugx
84 84 from IPython.utils.dir2 import dir2
85 import IPython.utils.rlineimpl as readline
86 85
87 86 #-----------------------------------------------------------------------------
88 87 # Globals
89 88 #-----------------------------------------------------------------------------
90 89
91 90 # Public API
92 91 __all__ = ['Completer','IPCompleter']
93 92
94 93 if sys.platform == 'win32':
95 94 PROTECTABLES = ' '
96 95 else:
97 96 PROTECTABLES = ' ()'
98 97
99 98 #-----------------------------------------------------------------------------
100 99 # Main functions and classes
101 100 #-----------------------------------------------------------------------------
102 101
103 102 def protect_filename(s):
104 103 """Escape a string to protect certain characters."""
105 104
106 105 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
107 106 for ch in s])
108 107
109 108
109 def mark_dirs(matches):
110 """Mark directories in input list by appending '/' to their names."""
111 out = []
112 isdir = os.path.isdir
113 for x in matches:
114 if isdir(x):
115 out.append(x+'/')
116 else:
117 out.append(x)
118 return out
119
120
110 121 def single_dir_expand(matches):
111 122 "Recursively expand match lists containing a single dir."
112 123
113 124 if len(matches) == 1 and os.path.isdir(matches[0]):
114 125 # Takes care of links to directories also. Use '/'
115 126 # explicitly, even under Windows, so that name completions
116 127 # don't end up escaped.
117 128 d = matches[0]
118 129 if d[-1] in ['/','\\']:
119 130 d = d[:-1]
120 131
121 132 subdirs = os.listdir(d)
122 133 if subdirs:
123 134 matches = [ (d + '/' + p) for p in subdirs]
124 135 return single_dir_expand(matches)
125 136 else:
126 137 return matches
127 138 else:
128 139 return matches
129 140
130 141 class Bunch: pass
131 142
132 143 class Completer:
133 144 def __init__(self,namespace=None,global_namespace=None):
134 145 """Create a new completer for the command line.
135 146
136 147 Completer([namespace,global_namespace]) -> completer instance.
137 148
138 149 If unspecified, the default namespace where completions are performed
139 150 is __main__ (technically, __main__.__dict__). Namespaces should be
140 151 given as dictionaries.
141 152
142 153 An optional second namespace can be given. This allows the completer
143 154 to handle cases where both the local and global scopes need to be
144 155 distinguished.
145 156
146 157 Completer instances should be used as the completion mechanism of
147 158 readline via the set_completer() call:
148 159
149 160 readline.set_completer(Completer(my_namespace).complete)
150 161 """
151 162
152 163 # Don't bind to namespace quite yet, but flag whether the user wants a
153 164 # specific namespace or to use __main__.__dict__. This will allow us
154 165 # to bind to __main__.__dict__ at completion time, not now.
155 166 if namespace is None:
156 167 self.use_main_ns = 1
157 168 else:
158 169 self.use_main_ns = 0
159 170 self.namespace = namespace
160 171
161 172 # The global namespace, if given, can be bound directly
162 173 if global_namespace is None:
163 174 self.global_namespace = {}
164 175 else:
165 176 self.global_namespace = global_namespace
166 177
167 178 def complete(self, text, state):
168 179 """Return the next possible completion for 'text'.
169 180
170 181 This is called successively with state == 0, 1, 2, ... until it
171 182 returns None. The completion should begin with 'text'.
172 183
173 184 """
174 185 if self.use_main_ns:
175 186 self.namespace = __main__.__dict__
176 187
177 188 if state == 0:
178 189 if "." in text:
179 190 self.matches = self.attr_matches(text)
180 191 else:
181 192 self.matches = self.global_matches(text)
182 193 try:
183 194 return self.matches[state]
184 195 except IndexError:
185 196 return None
186 197
187 198 def global_matches(self, text):
188 199 """Compute matches when text is a simple name.
189 200
190 201 Return a list of all keywords, built-in functions and names currently
191 202 defined in self.namespace or self.global_namespace that match.
192 203
193 204 """
194 205 #print 'Completer->global_matches, txt=%r' % text # dbg
195 206 matches = []
196 207 match_append = matches.append
197 208 n = len(text)
198 209 for lst in [keyword.kwlist,
199 210 __builtin__.__dict__.keys(),
200 211 self.namespace.keys(),
201 212 self.global_namespace.keys()]:
202 213 for word in lst:
203 214 if word[:n] == text and word != "__builtins__":
204 215 match_append(word)
205 216 return matches
206 217
207 218 def attr_matches(self, text):
208 219 """Compute matches when text contains a dot.
209 220
210 221 Assuming the text is of the form NAME.NAME....[NAME], and is
211 222 evaluatable in self.namespace or self.global_namespace, it will be
212 223 evaluated and its attributes (as revealed by dir()) are used as
213 224 possible completions. (For class instances, class members are are
214 225 also considered.)
215 226
216 227 WARNING: this can still invoke arbitrary C code, if an object
217 228 with a __getattr__ hook is evaluated.
218 229
219 230 """
220 231
221 232 #print 'Completer->attr_matches, txt=%r' % text # dbg
222 233 # Another option, seems to work great. Catches things like ''.<tab>
223 234 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
224 235
225 236 if not m:
226 237 return []
227 238
228 239 expr, attr = m.group(1, 3)
229 240 try:
230 241 obj = eval(expr, self.namespace)
231 242 except:
232 243 try:
233 244 obj = eval(expr, self.global_namespace)
234 245 except:
235 246 return []
236 247
237 248 words = dir2(obj)
238 249
239 250 try:
240 251 words = generics.complete_object(obj, words)
241 252 except TryNext:
242 253 pass
243 254 # Build match list to return
244 255 n = len(attr)
245 256 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
246 257 return res
247 258
248 259
249 260 class IPCompleter(Completer):
250 261 """Extension of the completer class with IPython-specific features"""
251 262
252 def __init__(self,shell,namespace=None,global_namespace=None,
253 omit__names=0,alias_table=None):
263 def __init__(self, shell, namespace=None, global_namespace=None,
264 omit__names=0, alias_table=None, use_readline=True):
254 265 """IPCompleter() -> completer
255 266
256 267 Return a completer object suitable for use by the readline library
257 268 via readline.set_completer().
258 269
259 270 Inputs:
260 271
261 272 - shell: a pointer to the ipython shell itself. This is needed
262 273 because this completer knows about magic functions, and those can
263 274 only be accessed via the ipython instance.
264 275
265 276 - namespace: an optional dict where completions are performed.
266 277
267 278 - global_namespace: secondary optional dict for completions, to
268 279 handle cases (such as IPython embedded inside functions) where
269 280 both Python scopes are visible.
270 281
271 282 - The optional omit__names parameter sets the completer to omit the
272 283 'magic' names (__magicname__) for python objects unless the text
273 284 to be completed explicitly starts with one or more underscores.
274 285
275 286 - If alias_table is supplied, it should be a dictionary of aliases
276 to complete. """
287 to complete.
288
289 use_readline : bool, optional
290 If true, use the readline library. This completer can still function
291 without readline, though in that case callers must provide some extra
292 information on each call about the current line."""
277 293
278 294 Completer.__init__(self,namespace,global_namespace)
279 295
280 296 self.magic_escape = ESC_MAGIC
281 self.readline = readline
282 delims = self.readline.get_completer_delims()
283 delims = delims.replace(self.magic_escape,'')
284 self.readline.set_completer_delims(delims)
285 self.get_line_buffer = self.readline.get_line_buffer
286 self.get_endidx = self.readline.get_endidx
297
298 # Readline-dependent code
299 self.use_readline = use_readline
300 if use_readline:
301 import IPython.utils.rlineimpl as readline
302 self.readline = readline
303 delims = self.readline.get_completer_delims()
304 delims = delims.replace(self.magic_escape,'')
305 self.readline.set_completer_delims(delims)
306 self.get_line_buffer = self.readline.get_line_buffer
307 self.get_endidx = self.readline.get_endidx
308 # /end readline-dependent code
309
310 # List where completion matches will be stored
311 self.matches = []
287 312 self.omit__names = omit__names
288 313 self.merge_completions = shell.readline_merge_completions
289 314 self.shell = shell.shell
290 315 if alias_table is None:
291 316 alias_table = {}
292 317 self.alias_table = alias_table
293 318 # Regexp to split filenames with spaces in them
294 319 self.space_name_re = re.compile(r'([^\\] )')
295 320 # Hold a local ref. to glob.glob for speed
296 321 self.glob = glob.glob
297 322
298 323 # Determine if we are running on 'dumb' terminals, like (X)Emacs
299 324 # buffers, to avoid completion problems.
300 325 term = os.environ.get('TERM','xterm')
301 326 self.dumb_terminal = term in ['dumb','emacs']
302 327
303 328 # Special handling of backslashes needed in win32 platforms
304 329 if sys.platform == "win32":
305 330 self.clean_glob = self._clean_glob_win32
306 331 else:
307 332 self.clean_glob = self._clean_glob
308 333
309 334 # All active matcher routines for completion
310 335 self.matchers = [self.python_matches,
311 336 self.file_matches,
312 337 self.magic_matches,
313 338 self.alias_matches,
314 self.python_func_kw_matches]
339 self.python_func_kw_matches,
340 ]
315 341
316 342 # Code contributed by Alex Schmolck, for ipython/emacs integration
317 343 def all_completions(self, text):
318 344 """Return all possible completions for the benefit of emacs."""
319 345
320 346 completions = []
321 347 comp_append = completions.append
322 348 try:
323 349 for i in xrange(sys.maxint):
324 350 res = self.complete(text, i, text)
325 351 if not res:
326 352 break
327 353 comp_append(res)
328 354 #XXX workaround for ``notDefined.<tab>``
329 355 except NameError:
330 356 pass
331 357 return completions
332 358 # /end Alex Schmolck code.
333 359
334 360 def _clean_glob(self,text):
335 361 return self.glob("%s*" % text)
336 362
337 363 def _clean_glob_win32(self,text):
338 364 return [f.replace("\\","/")
339 365 for f in self.glob("%s*" % text)]
340 366
341 367 def file_matches(self, text):
342 368 """Match filenames, expanding ~USER type strings.
343 369
344 370 Most of the seemingly convoluted logic in this completer is an
345 371 attempt to handle filenames with spaces in them. And yet it's not
346 372 quite perfect, because Python's readline doesn't expose all of the
347 373 GNU readline details needed for this to be done correctly.
348 374
349 375 For a filename with a space in it, the printed completions will be
350 376 only the parts after what's already been typed (instead of the
351 377 full completions, as is normally done). I don't think with the
352 378 current (as of Python 2.3) Python readline it's possible to do
353 379 better."""
354 380
355 381 #print 'Completer->file_matches: <%s>' % text # dbg
356 382
357 383 # chars that require escaping with backslash - i.e. chars
358 384 # that readline treats incorrectly as delimiters, but we
359 385 # don't want to treat as delimiters in filename matching
360 386 # when escaped with backslash
361 387
362 388 if text.startswith('!'):
363 389 text = text[1:]
364 390 text_prefix = '!'
365 391 else:
366 392 text_prefix = ''
367 393
368 394 lbuf = self.lbuf
369 395 open_quotes = 0 # track strings with open quotes
370 396 try:
371 397 lsplit = shlex.split(lbuf)[-1]
372 398 except ValueError:
373 399 # typically an unmatched ", or backslash without escaped char.
374 400 if lbuf.count('"')==1:
375 401 open_quotes = 1
376 402 lsplit = lbuf.split('"')[-1]
377 403 elif lbuf.count("'")==1:
378 404 open_quotes = 1
379 405 lsplit = lbuf.split("'")[-1]
380 406 else:
381 407 return []
382 408 except IndexError:
383 409 # tab pressed on empty line
384 410 lsplit = ""
385 411
386 412 if lsplit != protect_filename(lsplit):
387 413 # if protectables are found, do matching on the whole escaped
388 414 # name
389 415 has_protectables = 1
390 416 text0,text = text,lsplit
391 417 else:
392 418 has_protectables = 0
393 419 text = os.path.expanduser(text)
394 420
395 421 if text == "":
396 422 return [text_prefix + protect_filename(f) for f in self.glob("*")]
397 423
398 424 m0 = self.clean_glob(text.replace('\\',''))
399 425 if has_protectables:
400 426 # If we had protectables, we need to revert our changes to the
401 427 # beginning of filename so that we don't double-write the part
402 428 # of the filename we have so far
403 429 len_lsplit = len(lsplit)
404 430 matches = [text_prefix + text0 +
405 431 protect_filename(f[len_lsplit:]) for f in m0]
406 432 else:
407 433 if open_quotes:
408 434 # if we have a string with an open quote, we don't need to
409 435 # protect the names at all (and we _shouldn't_, as it
410 436 # would cause bugs when the filesystem call is made).
411 437 matches = m0
412 438 else:
413 439 matches = [text_prefix +
414 440 protect_filename(f) for f in m0]
415 441
416 442 #print 'mm',matches # dbg
417 return single_dir_expand(matches)
443 #return single_dir_expand(matches)
444 return mark_dirs(matches)
418 445
419 446 def magic_matches(self, text):
420 447 """Match magics"""
421 448 #print 'Completer->magic_matches:',text,'lb',self.lbuf # dbg
422 449 # Get all shell magics now rather than statically, so magics loaded at
423 450 # runtime show up too
424 451 magics = self.shell.lsmagic()
425 452 pre = self.magic_escape
426 453 baretext = text.lstrip(pre)
427 454 return [ pre+m for m in magics if m.startswith(baretext)]
428 455
429 456 def alias_matches(self, text):
430 457 """Match internal system aliases"""
431 458 #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg
432 459
433 460 # if we are not in the first 'item', alias matching
434 461 # doesn't make sense - unless we are starting with 'sudo' command.
435 462 if ' ' in self.lbuf.lstrip() and \
436 463 not self.lbuf.lstrip().startswith('sudo'):
437 464 return []
438 465 text = os.path.expanduser(text)
439 466 aliases = self.alias_table.keys()
440 467 if text == "":
441 468 return aliases
442 469 else:
443 470 return [alias for alias in aliases if alias.startswith(text)]
444 471
445 472 def python_matches(self,text):
446 473 """Match attributes or global python names"""
447 474
448 475 #print 'Completer->python_matches, txt=%r' % text # dbg
449 476 if "." in text:
450 477 try:
451 478 matches = self.attr_matches(text)
452 479 if text.endswith('.') and self.omit__names:
453 480 if self.omit__names == 1:
454 481 # true if txt is _not_ a __ name, false otherwise:
455 482 no__name = (lambda txt:
456 483 re.match(r'.*\.__.*?__',txt) is None)
457 484 else:
458 485 # true if txt is _not_ a _ name, false otherwise:
459 486 no__name = (lambda txt:
460 487 re.match(r'.*\._.*?',txt) is None)
461 488 matches = filter(no__name, matches)
462 489 except NameError:
463 490 # catches <undefined attributes>.<tab>
464 491 matches = []
465 492 else:
466 493 matches = self.global_matches(text)
467 494
468 495 return matches
469 496
470 497 def _default_arguments(self, obj):
471 498 """Return the list of default arguments of obj if it is callable,
472 499 or empty list otherwise."""
473 500
474 501 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
475 502 # for classes, check for __init__,__new__
476 503 if inspect.isclass(obj):
477 504 obj = (getattr(obj,'__init__',None) or
478 505 getattr(obj,'__new__',None))
479 506 # for all others, check if they are __call__able
480 507 elif hasattr(obj, '__call__'):
481 508 obj = obj.__call__
482 509 # XXX: is there a way to handle the builtins ?
483 510 try:
484 511 args,_,_1,defaults = inspect.getargspec(obj)
485 512 if defaults:
486 513 return args[-len(defaults):]
487 514 except TypeError: pass
488 515 return []
489 516
490 517 def python_func_kw_matches(self,text):
491 518 """Match named parameters (kwargs) of the last open function"""
492 519
493 520 if "." in text: # a parameter cannot be dotted
494 521 return []
495 522 try: regexp = self.__funcParamsRegex
496 523 except AttributeError:
497 524 regexp = self.__funcParamsRegex = re.compile(r'''
498 525 '.*?' | # single quoted strings or
499 526 ".*?" | # double quoted strings or
500 527 \w+ | # identifier
501 528 \S # other characters
502 529 ''', re.VERBOSE | re.DOTALL)
503 530 # 1. find the nearest identifier that comes before an unclosed
504 531 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
505 532 tokens = regexp.findall(self.get_line_buffer())
506 533 tokens.reverse()
507 534 iterTokens = iter(tokens); openPar = 0
508 535 for token in iterTokens:
509 536 if token == ')':
510 537 openPar -= 1
511 538 elif token == '(':
512 539 openPar += 1
513 540 if openPar > 0:
514 541 # found the last unclosed parenthesis
515 542 break
516 543 else:
517 544 return []
518 545 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
519 546 ids = []
520 547 isId = re.compile(r'\w+$').match
521 548 while True:
522 549 try:
523 550 ids.append(iterTokens.next())
524 551 if not isId(ids[-1]):
525 552 ids.pop(); break
526 553 if not iterTokens.next() == '.':
527 554 break
528 555 except StopIteration:
529 556 break
530 557 # lookup the candidate callable matches either using global_matches
531 558 # or attr_matches for dotted names
532 559 if len(ids) == 1:
533 560 callableMatches = self.global_matches(ids[0])
534 561 else:
535 562 callableMatches = self.attr_matches('.'.join(ids[::-1]))
536 563 argMatches = []
537 564 for callableMatch in callableMatches:
538 565 try:
539 566 namedArgs = self._default_arguments(eval(callableMatch,
540 567 self.namespace))
541 568 except:
542 569 continue
543 570 for namedArg in namedArgs:
544 571 if namedArg.startswith(text):
545 572 argMatches.append("%s=" %namedArg)
546 573 return argMatches
547 574
548 575 def dispatch_custom_completer(self,text):
549 576 #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
550 577 line = self.full_lbuf
551 578 if not line.strip():
552 579 return None
553 580
554 581 event = Bunch()
555 582 event.line = line
556 583 event.symbol = text
557 584 cmd = line.split(None,1)[0]
558 585 event.command = cmd
559 586 #print "\ncustom:{%s]\n" % event # dbg
560 587
561 588 # for foo etc, try also to find completer for %foo
562 589 if not cmd.startswith(self.magic_escape):
563 590 try_magic = self.custom_completers.s_matches(
564 591 self.magic_escape + cmd)
565 592 else:
566 593 try_magic = []
567 594
568 595 for c in itertools.chain(self.custom_completers.s_matches(cmd),
569 596 try_magic,
570 597 self.custom_completers.flat_matches(self.lbuf)):
571 598 #print "try",c # dbg
572 599 try:
573 600 res = c(event)
574 601 # first, try case sensitive match
575 602 withcase = [r for r in res if r.startswith(text)]
576 603 if withcase:
577 604 return withcase
578 605 # if none, then case insensitive ones are ok too
579 606 text_low = text.lower()
580 607 return [r for r in res if r.lower().startswith(text_low)]
581 608 except TryNext:
582 609 pass
583 610
584 611 return None
585 612
586 def complete(self, text, state, line_buffer=None):
587 """Return the next possible completion for 'text'.
613 def complete(self, text, line_buffer, cursor_pos=None):
614 """Return the state-th possible completion for 'text'.
588 615
589 616 This is called successively with state == 0, 1, 2, ... until it
590 617 returns None. The completion should begin with 'text'.
591 618
592 :Keywords:
593 - line_buffer: string
594 If not given, the completer attempts to obtain the current line buffer
595 via readline. This keyword allows clients which are requesting for
596 text completions in non-readline contexts to inform the completer of
597 the entire text.
619 Parameters
620 ----------
621 text : string
622 Text to perform the completion on.
623
624 line_buffer : string, optional
625 If not given, the completer attempts to obtain the current line
626 buffer via readline. This keyword allows clients which are
627 requesting for text completions in non-readline contexts to inform
628 the completer of the entire text.
629
630 cursor_pos : int, optional
631 Index of the cursor in the full line buffer. Should be provided by
632 remote frontends where kernel has no access to frontend state.
598 633 """
599 634
600 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
635 magic_escape = self.magic_escape
636 self.full_lbuf = line_buffer
637 self.lbuf = self.full_lbuf[:cursor_pos]
601 638
602 # if there is only a tab on a line with only whitespace, instead
603 # of the mostly useless 'do you want to see all million
604 # completions' message, just do the right thing and give the user
605 # his tab! Incidentally, this enables pasting of tabbed text from
606 # an editor (as long as autoindent is off).
639 if text.startswith('~'):
640 text = os.path.expanduser(text)
607 641
608 # It should be noted that at least pyreadline still shows
609 # file completions - is there a way around it?
610
611 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
612 # don't interfere with their own tab-completion mechanism.
613 if line_buffer is None:
614 self.full_lbuf = self.get_line_buffer()
642 # Start with a clean slate of completions
643 self.matches[:] = []
644 custom_res = self.dispatch_custom_completer(text)
645 if custom_res is not None:
646 # did custom completers produce something?
647 self.matches = custom_res
615 648 else:
616 self.full_lbuf = line_buffer
617
618 if not (self.dumb_terminal or self.full_lbuf.strip()):
619 self.readline.insert_text('\t')
620 return None
621
622 magic_escape = self.magic_escape
649 # Extend the list of completions with the results of each
650 # matcher, so we return results to the user from all
651 # namespaces.
652 if self.merge_completions:
653 self.matches = []
654 for matcher in self.matchers:
655 self.matches.extend(matcher(text))
656 else:
657 for matcher in self.matchers:
658 self.matches = matcher(text)
659 if self.matches:
660 break
661 # FIXME: we should extend our api to return a dict with completions for
662 # different types of objects. The rlcomplete() method could then
663 # simply collapse the dict into a list for readline, but we'd have
664 # richer completion semantics in other evironments.
665 self.matches = sorted(set(self.matches))
666 #from IPython.utils.io import rprint; rprint(self.matches) # dbg
667 return self.matches
668
669 def rlcomplete(self, text, state):
670 """Return the state-th possible completion for 'text'.
623 671
624 self.lbuf = self.full_lbuf[:self.get_endidx()]
672 This is called successively with state == 0, 1, 2, ... until it
673 returns None. The completion should begin with 'text'.
625 674
626 try:
627 if text.startswith('~'):
628 text = os.path.expanduser(text)
629 if state == 0:
630 custom_res = self.dispatch_custom_completer(text)
631 if custom_res is not None:
632 # did custom completers produce something?
633 self.matches = custom_res
634 else:
635 # Extend the list of completions with the results of each
636 # matcher, so we return results to the user from all
637 # namespaces.
638 if self.merge_completions:
639 self.matches = []
640 for matcher in self.matchers:
641 self.matches.extend(matcher(text))
642 else:
643 for matcher in self.matchers:
644 self.matches = matcher(text)
645 if self.matches:
646 break
647 self.matches = list(set(self.matches))
648 try:
649 #print "MATCH: %r" % self.matches[state] # dbg
650 return self.matches[state]
651 except IndexError:
675 Parameters
676 ----------
677 text : string
678 Text to perform the completion on.
679
680 state : int
681 Counter used by readline.
682
683 """
684
685 #print "rlcomplete! '%s' %s" % (text, state) # dbg
686
687 if state==0:
688 self.full_lbuf = line_buffer = self.get_line_buffer()
689 cursor_pos = self.get_endidx()
690
691 # if there is only a tab on a line with only whitespace, instead of
692 # the mostly useless 'do you want to see all million completions'
693 # message, just do the right thing and give the user his tab!
694 # Incidentally, this enables pasting of tabbed text from an editor
695 # (as long as autoindent is off).
696
697 # It should be noted that at least pyreadline still shows file
698 # completions - is there a way around it?
699
700 # don't apply this on 'dumb' terminals, such as emacs buffers, so
701 # we don't interfere with their own tab-completion mechanism.
702 if not (self.dumb_terminal or self.full_lbuf.strip()):
703 self.readline.insert_text('\t')
704 sys.stdout.flush()
652 705 return None
653 except:
654 #from IPython.core.ultratb import AutoFormattedTB; # dbg
655 #tb=AutoFormattedTB('Verbose');tb() #dbg
656
657 # If completion fails, don't annoy the user.
706
707 # This method computes the self.matches array
708 self.complete(text, line_buffer, cursor_pos)
709
710 # Debug version, since readline silences all exceptions making it
711 # impossible to debug any problem in the above code
712
713 ## try:
714 ## self.complete(text, line_buffer, cursor_pos)
715 ## except:
716 ## import traceback; traceback.print_exc()
717
718 try:
719 return self.matches[state]
720 except IndexError:
658 721 return None
722
@@ -1,2150 +1,2147 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2010 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 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import abc
22 22 import codeop
23 23 import exceptions
24 24 import new
25 25 import os
26 26 import re
27 27 import string
28 28 import sys
29 29 import tempfile
30 30 from contextlib import nested
31 31
32 32 from IPython.config.configurable import Configurable
33 33 from IPython.core import debugger, oinspect
34 34 from IPython.core import history as ipcorehist
35 35 from IPython.core import prefilter
36 36 from IPython.core import shadowns
37 37 from IPython.core import ultratb
38 38 from IPython.core.alias import AliasManager
39 39 from IPython.core.builtin_trap import BuiltinTrap
40 40 from IPython.core.display_trap import DisplayTrap
41 41 from IPython.core.displayhook import DisplayHook
42 42 from IPython.core.error import UsageError
43 43 from IPython.core.extensions import ExtensionManager
44 44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 45 from IPython.core.inputlist import InputList
46 46 from IPython.core.logger import Logger
47 47 from IPython.core.magic import Magic
48 48 from IPython.core.payload import PayloadManager
49 49 from IPython.core.plugin import PluginManager
50 50 from IPython.core.prefilter import PrefilterManager
51 51 from IPython.external.Itpl import ItplNS
52 52 from IPython.utils import PyColorize
53 53 from IPython.utils import io
54 54 from IPython.utils import pickleshare
55 55 from IPython.utils.doctestreload import doctest_reload
56 56 from IPython.utils.io import ask_yes_no, rprint
57 57 from IPython.utils.ipstruct import Struct
58 58 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
59 59 from IPython.utils.process import getoutput, getoutputerror
60 60 from IPython.utils.strdispatch import StrDispatch
61 61 from IPython.utils.syspathcontext import prepended_to_syspath
62 62 from IPython.utils.text import num_ini_spaces
63 63 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
64 64 List, Unicode, Instance, Type)
65 65 from IPython.utils.warn import warn, error, fatal
66 66 import IPython.core.hooks
67 67
68 68 # from IPython.utils import growl
69 69 # growl.start("IPython")
70 70
71 71 #-----------------------------------------------------------------------------
72 72 # Globals
73 73 #-----------------------------------------------------------------------------
74 74
75 75 # compiled regexps for autoindent management
76 76 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
77 77
78 78 #-----------------------------------------------------------------------------
79 79 # Utilities
80 80 #-----------------------------------------------------------------------------
81 81
82 82 # store the builtin raw_input globally, and use this always, in case user code
83 83 # overwrites it (like wx.py.PyShell does)
84 84 raw_input_original = raw_input
85 85
86 86 def softspace(file, newvalue):
87 87 """Copied from code.py, to remove the dependency"""
88 88
89 89 oldvalue = 0
90 90 try:
91 91 oldvalue = file.softspace
92 92 except AttributeError:
93 93 pass
94 94 try:
95 95 file.softspace = newvalue
96 96 except (AttributeError, TypeError):
97 97 # "attribute-less object" or "read-only attributes"
98 98 pass
99 99 return oldvalue
100 100
101 101
102 102 def no_op(*a, **kw): pass
103 103
104 104 class SpaceInInput(exceptions.Exception): pass
105 105
106 106 class Bunch: pass
107 107
108 108
109 109 def get_default_colors():
110 110 if sys.platform=='darwin':
111 111 return "LightBG"
112 112 elif os.name=='nt':
113 113 return 'Linux'
114 114 else:
115 115 return 'Linux'
116 116
117 117
118 118 class SeparateStr(Str):
119 119 """A Str subclass to validate separate_in, separate_out, etc.
120 120
121 121 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
122 122 """
123 123
124 124 def validate(self, obj, value):
125 125 if value == '0': value = ''
126 126 value = value.replace('\\n','\n')
127 127 return super(SeparateStr, self).validate(obj, value)
128 128
129 129 class MultipleInstanceError(Exception):
130 130 pass
131 131
132 132
133 133 #-----------------------------------------------------------------------------
134 134 # Main IPython class
135 135 #-----------------------------------------------------------------------------
136 136
137 137
138 138 class InteractiveShell(Configurable, Magic):
139 139 """An enhanced, interactive shell for Python."""
140 140
141 141 _instance = None
142 142 autocall = Enum((0,1,2), default_value=1, config=True)
143 143 # TODO: remove all autoindent logic and put into frontends.
144 144 # We can't do this yet because even runlines uses the autoindent.
145 145 autoindent = CBool(True, config=True)
146 146 automagic = CBool(True, config=True)
147 147 cache_size = Int(1000, config=True)
148 148 color_info = CBool(True, config=True)
149 149 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
150 150 default_value=get_default_colors(), config=True)
151 151 debug = CBool(False, config=True)
152 152 deep_reload = CBool(False, config=True)
153 153 displayhook_class = Type(DisplayHook)
154 154 filename = Str("<ipython console>")
155 155 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
156 156 logstart = CBool(False, config=True)
157 157 logfile = Str('', config=True)
158 158 logappend = Str('', config=True)
159 159 object_info_string_level = Enum((0,1,2), default_value=0,
160 160 config=True)
161 161 pdb = CBool(False, config=True)
162 162 pprint = CBool(True, config=True)
163 163 profile = Str('', config=True)
164 164 prompt_in1 = Str('In [\\#]: ', config=True)
165 165 prompt_in2 = Str(' .\\D.: ', config=True)
166 166 prompt_out = Str('Out[\\#]: ', config=True)
167 167 prompts_pad_left = CBool(True, config=True)
168 168 quiet = CBool(False, config=True)
169 169
170 170 # The readline stuff will eventually be moved to the terminal subclass
171 171 # but for now, we can't do that as readline is welded in everywhere.
172 172 readline_use = CBool(True, config=True)
173 173 readline_merge_completions = CBool(True, config=True)
174 174 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
175 175 readline_remove_delims = Str('-/~', config=True)
176 176 readline_parse_and_bind = List([
177 177 'tab: complete',
178 178 '"\C-l": clear-screen',
179 179 'set show-all-if-ambiguous on',
180 180 '"\C-o": tab-insert',
181 181 '"\M-i": " "',
182 182 '"\M-o": "\d\d\d\d"',
183 183 '"\M-I": "\d\d\d\d"',
184 184 '"\C-r": reverse-search-history',
185 185 '"\C-s": forward-search-history',
186 186 '"\C-p": history-search-backward',
187 187 '"\C-n": history-search-forward',
188 188 '"\e[A": history-search-backward',
189 189 '"\e[B": history-search-forward',
190 190 '"\C-k": kill-line',
191 191 '"\C-u": unix-line-discard',
192 192 ], allow_none=False, config=True)
193 193
194 194 # TODO: this part of prompt management should be moved to the frontends.
195 195 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
196 196 separate_in = SeparateStr('\n', config=True)
197 197 separate_out = SeparateStr('\n', config=True)
198 198 separate_out2 = SeparateStr('\n', config=True)
199 199 system_header = Str('IPython system call: ', config=True)
200 200 system_verbose = CBool(False, config=True)
201 201 wildcards_case_sensitive = CBool(True, config=True)
202 202 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
203 203 default_value='Context', config=True)
204 204
205 205 # Subcomponents of InteractiveShell
206 206 alias_manager = Instance('IPython.core.alias.AliasManager')
207 207 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
208 208 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
209 209 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
210 210 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
211 211 plugin_manager = Instance('IPython.core.plugin.PluginManager')
212 212 payload_manager = Instance('IPython.core.payload.PayloadManager')
213 213
214 214 def __init__(self, config=None, ipython_dir=None,
215 215 user_ns=None, user_global_ns=None,
216 216 custom_exceptions=((),None)):
217 217
218 218 # This is where traits with a config_key argument are updated
219 219 # from the values on config.
220 220 super(InteractiveShell, self).__init__(config=config)
221 221
222 222 # These are relatively independent and stateless
223 223 self.init_ipython_dir(ipython_dir)
224 224 self.init_instance_attrs()
225 225
226 226 # Create namespaces (user_ns, user_global_ns, etc.)
227 227 self.init_create_namespaces(user_ns, user_global_ns)
228 228 # This has to be done after init_create_namespaces because it uses
229 229 # something in self.user_ns, but before init_sys_modules, which
230 230 # is the first thing to modify sys.
231 231 # TODO: When we override sys.stdout and sys.stderr before this class
232 232 # is created, we are saving the overridden ones here. Not sure if this
233 233 # is what we want to do.
234 234 self.save_sys_module_state()
235 235 self.init_sys_modules()
236 236
237 237 self.init_history()
238 238 self.init_encoding()
239 239 self.init_prefilter()
240 240
241 241 Magic.__init__(self, self)
242 242
243 243 self.init_syntax_highlighting()
244 244 self.init_hooks()
245 245 self.init_pushd_popd_magic()
246 246 # self.init_traceback_handlers use to be here, but we moved it below
247 247 # because it and init_io have to come after init_readline.
248 248 self.init_user_ns()
249 249 self.init_logger()
250 250 self.init_alias()
251 251 self.init_builtins()
252 252
253 253 # pre_config_initialization
254 254 self.init_shadow_hist()
255 255
256 256 # The next section should contain averything that was in ipmaker.
257 257 self.init_logstart()
258 258
259 259 # The following was in post_config_initialization
260 260 self.init_inspector()
261 261 # init_readline() must come before init_io(), because init_io uses
262 262 # readline related things.
263 263 self.init_readline()
264 264 # TODO: init_io() needs to happen before init_traceback handlers
265 265 # because the traceback handlers hardcode the stdout/stderr streams.
266 266 # This logic in in debugger.Pdb and should eventually be changed.
267 267 self.init_io()
268 268 self.init_traceback_handlers(custom_exceptions)
269 269 self.init_prompts()
270 270 self.init_displayhook()
271 271 self.init_reload_doctest()
272 272 self.init_magics()
273 273 self.init_pdb()
274 274 self.init_extension_manager()
275 275 self.init_plugin_manager()
276 276 self.init_payload()
277 277 self.hooks.late_startup_hook()
278 278
279 279 @classmethod
280 280 def instance(cls, *args, **kwargs):
281 281 """Returns a global InteractiveShell instance."""
282 282 if cls._instance is None:
283 283 inst = cls(*args, **kwargs)
284 284 # Now make sure that the instance will also be returned by
285 285 # the subclasses instance attribute.
286 286 for subclass in cls.mro():
287 287 if issubclass(cls, subclass) and issubclass(subclass, InteractiveShell):
288 288 subclass._instance = inst
289 289 else:
290 290 break
291 291 if isinstance(cls._instance, cls):
292 292 return cls._instance
293 293 else:
294 294 raise MultipleInstanceError(
295 295 'Multiple incompatible subclass instances of '
296 296 'InteractiveShell are being created.'
297 297 )
298 298
299 299 @classmethod
300 300 def initialized(cls):
301 301 return hasattr(cls, "_instance")
302 302
303 303 def get_ipython(self):
304 304 """Return the currently running IPython instance."""
305 305 return self
306 306
307 307 #-------------------------------------------------------------------------
308 308 # Trait changed handlers
309 309 #-------------------------------------------------------------------------
310 310
311 311 def _ipython_dir_changed(self, name, new):
312 312 if not os.path.isdir(new):
313 313 os.makedirs(new, mode = 0777)
314 314
315 315 def set_autoindent(self,value=None):
316 316 """Set the autoindent flag, checking for readline support.
317 317
318 318 If called with no arguments, it acts as a toggle."""
319 319
320 320 if not self.has_readline:
321 321 if os.name == 'posix':
322 322 warn("The auto-indent feature requires the readline library")
323 323 self.autoindent = 0
324 324 return
325 325 if value is None:
326 326 self.autoindent = not self.autoindent
327 327 else:
328 328 self.autoindent = value
329 329
330 330 #-------------------------------------------------------------------------
331 331 # init_* methods called by __init__
332 332 #-------------------------------------------------------------------------
333 333
334 334 def init_ipython_dir(self, ipython_dir):
335 335 if ipython_dir is not None:
336 336 self.ipython_dir = ipython_dir
337 337 self.config.Global.ipython_dir = self.ipython_dir
338 338 return
339 339
340 340 if hasattr(self.config.Global, 'ipython_dir'):
341 341 self.ipython_dir = self.config.Global.ipython_dir
342 342 else:
343 343 self.ipython_dir = get_ipython_dir()
344 344
345 345 # All children can just read this
346 346 self.config.Global.ipython_dir = self.ipython_dir
347 347
348 348 def init_instance_attrs(self):
349 349 self.more = False
350 350
351 351 # command compiler
352 352 self.compile = codeop.CommandCompiler()
353 353
354 354 # User input buffer
355 355 self.buffer = []
356 356
357 357 # Make an empty namespace, which extension writers can rely on both
358 358 # existing and NEVER being used by ipython itself. This gives them a
359 359 # convenient location for storing additional information and state
360 360 # their extensions may require, without fear of collisions with other
361 361 # ipython names that may develop later.
362 362 self.meta = Struct()
363 363
364 364 # Object variable to store code object waiting execution. This is
365 365 # used mainly by the multithreaded shells, but it can come in handy in
366 366 # other situations. No need to use a Queue here, since it's a single
367 367 # item which gets cleared once run.
368 368 self.code_to_run = None
369 369
370 370 # Temporary files used for various purposes. Deleted at exit.
371 371 self.tempfiles = []
372 372
373 373 # Keep track of readline usage (later set by init_readline)
374 374 self.has_readline = False
375 375
376 376 # keep track of where we started running (mainly for crash post-mortem)
377 377 # This is not being used anywhere currently.
378 378 self.starting_dir = os.getcwd()
379 379
380 380 # Indentation management
381 381 self.indent_current_nsp = 0
382 382
383 383 def init_encoding(self):
384 384 # Get system encoding at startup time. Certain terminals (like Emacs
385 385 # under Win32 have it set to None, and we need to have a known valid
386 386 # encoding to use in the raw_input() method
387 387 try:
388 388 self.stdin_encoding = sys.stdin.encoding or 'ascii'
389 389 except AttributeError:
390 390 self.stdin_encoding = 'ascii'
391 391
392 392 def init_syntax_highlighting(self):
393 393 # Python source parser/formatter for syntax highlighting
394 394 pyformat = PyColorize.Parser().format
395 395 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
396 396
397 397 def init_pushd_popd_magic(self):
398 398 # for pushd/popd management
399 399 try:
400 400 self.home_dir = get_home_dir()
401 401 except HomeDirError, msg:
402 402 fatal(msg)
403 403
404 404 self.dir_stack = []
405 405
406 406 def init_logger(self):
407 407 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
408 408 # local shortcut, this is used a LOT
409 409 self.log = self.logger.log
410 410
411 411 def init_logstart(self):
412 412 if self.logappend:
413 413 self.magic_logstart(self.logappend + ' append')
414 414 elif self.logfile:
415 415 self.magic_logstart(self.logfile)
416 416 elif self.logstart:
417 417 self.magic_logstart()
418 418
419 419 def init_builtins(self):
420 420 self.builtin_trap = BuiltinTrap(shell=self)
421 421
422 422 def init_inspector(self):
423 423 # Object inspector
424 424 self.inspector = oinspect.Inspector(oinspect.InspectColors,
425 425 PyColorize.ANSICodeColors,
426 426 'NoColor',
427 427 self.object_info_string_level)
428 428
429 429 def init_io(self):
430 430 import IPython.utils.io
431 431 if sys.platform == 'win32' and self.has_readline:
432 432 Term = io.IOTerm(
433 433 cout=self.readline._outputfile,cerr=self.readline._outputfile
434 434 )
435 435 else:
436 436 Term = io.IOTerm()
437 437 io.Term = Term
438 438
439 439 def init_prompts(self):
440 440 # TODO: This is a pass for now because the prompts are managed inside
441 441 # the DisplayHook. Once there is a separate prompt manager, this
442 442 # will initialize that object and all prompt related information.
443 443 pass
444 444
445 445 def init_displayhook(self):
446 446 # Initialize displayhook, set in/out prompts and printing system
447 447 self.displayhook = self.displayhook_class(
448 448 shell=self,
449 449 cache_size=self.cache_size,
450 450 input_sep = self.separate_in,
451 451 output_sep = self.separate_out,
452 452 output_sep2 = self.separate_out2,
453 453 ps1 = self.prompt_in1,
454 454 ps2 = self.prompt_in2,
455 455 ps_out = self.prompt_out,
456 456 pad_left = self.prompts_pad_left
457 457 )
458 458 # This is a context manager that installs/revmoes the displayhook at
459 459 # the appropriate time.
460 460 self.display_trap = DisplayTrap(hook=self.displayhook)
461 461
462 462 def init_reload_doctest(self):
463 463 # Do a proper resetting of doctest, including the necessary displayhook
464 464 # monkeypatching
465 465 try:
466 466 doctest_reload()
467 467 except ImportError:
468 468 warn("doctest module does not exist.")
469 469
470 470 #-------------------------------------------------------------------------
471 471 # Things related to injections into the sys module
472 472 #-------------------------------------------------------------------------
473 473
474 474 def save_sys_module_state(self):
475 475 """Save the state of hooks in the sys module.
476 476
477 477 This has to be called after self.user_ns is created.
478 478 """
479 479 self._orig_sys_module_state = {}
480 480 self._orig_sys_module_state['stdin'] = sys.stdin
481 481 self._orig_sys_module_state['stdout'] = sys.stdout
482 482 self._orig_sys_module_state['stderr'] = sys.stderr
483 483 self._orig_sys_module_state['excepthook'] = sys.excepthook
484 484 try:
485 485 self._orig_sys_modules_main_name = self.user_ns['__name__']
486 486 except KeyError:
487 487 pass
488 488
489 489 def restore_sys_module_state(self):
490 490 """Restore the state of the sys module."""
491 491 try:
492 492 for k, v in self._orig_sys_module_state.items():
493 493 setattr(sys, k, v)
494 494 except AttributeError:
495 495 pass
496 496 try:
497 497 delattr(sys, 'ipcompleter')
498 498 except AttributeError:
499 499 pass
500 500 # Reset what what done in self.init_sys_modules
501 501 try:
502 502 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
503 503 except (AttributeError, KeyError):
504 504 pass
505 505
506 506 #-------------------------------------------------------------------------
507 507 # Things related to hooks
508 508 #-------------------------------------------------------------------------
509 509
510 510 def init_hooks(self):
511 511 # hooks holds pointers used for user-side customizations
512 512 self.hooks = Struct()
513 513
514 514 self.strdispatchers = {}
515 515
516 516 # Set all default hooks, defined in the IPython.hooks module.
517 517 hooks = IPython.core.hooks
518 518 for hook_name in hooks.__all__:
519 519 # default hooks have priority 100, i.e. low; user hooks should have
520 520 # 0-100 priority
521 521 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
522 522
523 523 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
524 524 """set_hook(name,hook) -> sets an internal IPython hook.
525 525
526 526 IPython exposes some of its internal API as user-modifiable hooks. By
527 527 adding your function to one of these hooks, you can modify IPython's
528 528 behavior to call at runtime your own routines."""
529 529
530 530 # At some point in the future, this should validate the hook before it
531 531 # accepts it. Probably at least check that the hook takes the number
532 532 # of args it's supposed to.
533 533
534 534 f = new.instancemethod(hook,self,self.__class__)
535 535
536 536 # check if the hook is for strdispatcher first
537 537 if str_key is not None:
538 538 sdp = self.strdispatchers.get(name, StrDispatch())
539 539 sdp.add_s(str_key, f, priority )
540 540 self.strdispatchers[name] = sdp
541 541 return
542 542 if re_key is not None:
543 543 sdp = self.strdispatchers.get(name, StrDispatch())
544 544 sdp.add_re(re.compile(re_key), f, priority )
545 545 self.strdispatchers[name] = sdp
546 546 return
547 547
548 548 dp = getattr(self.hooks, name, None)
549 549 if name not in IPython.core.hooks.__all__:
550 550 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
551 551 if not dp:
552 552 dp = IPython.core.hooks.CommandChainDispatcher()
553 553
554 554 try:
555 555 dp.add(f,priority)
556 556 except AttributeError:
557 557 # it was not commandchain, plain old func - replace
558 558 dp = f
559 559
560 560 setattr(self.hooks,name, dp)
561 561
562 562 #-------------------------------------------------------------------------
563 563 # Things related to the "main" module
564 564 #-------------------------------------------------------------------------
565 565
566 566 def new_main_mod(self,ns=None):
567 567 """Return a new 'main' module object for user code execution.
568 568 """
569 569 main_mod = self._user_main_module
570 570 init_fakemod_dict(main_mod,ns)
571 571 return main_mod
572 572
573 573 def cache_main_mod(self,ns,fname):
574 574 """Cache a main module's namespace.
575 575
576 576 When scripts are executed via %run, we must keep a reference to the
577 577 namespace of their __main__ module (a FakeModule instance) around so
578 578 that Python doesn't clear it, rendering objects defined therein
579 579 useless.
580 580
581 581 This method keeps said reference in a private dict, keyed by the
582 582 absolute path of the module object (which corresponds to the script
583 583 path). This way, for multiple executions of the same script we only
584 584 keep one copy of the namespace (the last one), thus preventing memory
585 585 leaks from old references while allowing the objects from the last
586 586 execution to be accessible.
587 587
588 588 Note: we can not allow the actual FakeModule instances to be deleted,
589 589 because of how Python tears down modules (it hard-sets all their
590 590 references to None without regard for reference counts). This method
591 591 must therefore make a *copy* of the given namespace, to allow the
592 592 original module's __dict__ to be cleared and reused.
593 593
594 594
595 595 Parameters
596 596 ----------
597 597 ns : a namespace (a dict, typically)
598 598
599 599 fname : str
600 600 Filename associated with the namespace.
601 601
602 602 Examples
603 603 --------
604 604
605 605 In [10]: import IPython
606 606
607 607 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
608 608
609 609 In [12]: IPython.__file__ in _ip._main_ns_cache
610 610 Out[12]: True
611 611 """
612 612 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
613 613
614 614 def clear_main_mod_cache(self):
615 615 """Clear the cache of main modules.
616 616
617 617 Mainly for use by utilities like %reset.
618 618
619 619 Examples
620 620 --------
621 621
622 622 In [15]: import IPython
623 623
624 624 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
625 625
626 626 In [17]: len(_ip._main_ns_cache) > 0
627 627 Out[17]: True
628 628
629 629 In [18]: _ip.clear_main_mod_cache()
630 630
631 631 In [19]: len(_ip._main_ns_cache) == 0
632 632 Out[19]: True
633 633 """
634 634 self._main_ns_cache.clear()
635 635
636 636 #-------------------------------------------------------------------------
637 637 # Things related to debugging
638 638 #-------------------------------------------------------------------------
639 639
640 640 def init_pdb(self):
641 641 # Set calling of pdb on exceptions
642 642 # self.call_pdb is a property
643 643 self.call_pdb = self.pdb
644 644
645 645 def _get_call_pdb(self):
646 646 return self._call_pdb
647 647
648 648 def _set_call_pdb(self,val):
649 649
650 650 if val not in (0,1,False,True):
651 651 raise ValueError,'new call_pdb value must be boolean'
652 652
653 653 # store value in instance
654 654 self._call_pdb = val
655 655
656 656 # notify the actual exception handlers
657 657 self.InteractiveTB.call_pdb = val
658 658
659 659 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
660 660 'Control auto-activation of pdb at exceptions')
661 661
662 662 def debugger(self,force=False):
663 663 """Call the pydb/pdb debugger.
664 664
665 665 Keywords:
666 666
667 667 - force(False): by default, this routine checks the instance call_pdb
668 668 flag and does not actually invoke the debugger if the flag is false.
669 669 The 'force' option forces the debugger to activate even if the flag
670 670 is false.
671 671 """
672 672
673 673 if not (force or self.call_pdb):
674 674 return
675 675
676 676 if not hasattr(sys,'last_traceback'):
677 677 error('No traceback has been produced, nothing to debug.')
678 678 return
679 679
680 680 # use pydb if available
681 681 if debugger.has_pydb:
682 682 from pydb import pm
683 683 else:
684 684 # fallback to our internal debugger
685 685 pm = lambda : self.InteractiveTB.debugger(force=True)
686 686 self.history_saving_wrapper(pm)()
687 687
688 688 #-------------------------------------------------------------------------
689 689 # Things related to IPython's various namespaces
690 690 #-------------------------------------------------------------------------
691 691
692 692 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
693 693 # Create the namespace where the user will operate. user_ns is
694 694 # normally the only one used, and it is passed to the exec calls as
695 695 # the locals argument. But we do carry a user_global_ns namespace
696 696 # given as the exec 'globals' argument, This is useful in embedding
697 697 # situations where the ipython shell opens in a context where the
698 698 # distinction between locals and globals is meaningful. For
699 699 # non-embedded contexts, it is just the same object as the user_ns dict.
700 700
701 701 # FIXME. For some strange reason, __builtins__ is showing up at user
702 702 # level as a dict instead of a module. This is a manual fix, but I
703 703 # should really track down where the problem is coming from. Alex
704 704 # Schmolck reported this problem first.
705 705
706 706 # A useful post by Alex Martelli on this topic:
707 707 # Re: inconsistent value from __builtins__
708 708 # Von: Alex Martelli <aleaxit@yahoo.com>
709 709 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
710 710 # Gruppen: comp.lang.python
711 711
712 712 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
713 713 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
714 714 # > <type 'dict'>
715 715 # > >>> print type(__builtins__)
716 716 # > <type 'module'>
717 717 # > Is this difference in return value intentional?
718 718
719 719 # Well, it's documented that '__builtins__' can be either a dictionary
720 720 # or a module, and it's been that way for a long time. Whether it's
721 721 # intentional (or sensible), I don't know. In any case, the idea is
722 722 # that if you need to access the built-in namespace directly, you
723 723 # should start with "import __builtin__" (note, no 's') which will
724 724 # definitely give you a module. Yeah, it's somewhat confusing:-(.
725 725
726 726 # These routines return properly built dicts as needed by the rest of
727 727 # the code, and can also be used by extension writers to generate
728 728 # properly initialized namespaces.
729 729 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
730 730
731 731 # Assign namespaces
732 732 # This is the namespace where all normal user variables live
733 733 self.user_ns = user_ns
734 734 self.user_global_ns = user_global_ns
735 735
736 736 # An auxiliary namespace that checks what parts of the user_ns were
737 737 # loaded at startup, so we can list later only variables defined in
738 738 # actual interactive use. Since it is always a subset of user_ns, it
739 739 # doesn't need to be separately tracked in the ns_table.
740 740 self.user_ns_hidden = {}
741 741
742 742 # A namespace to keep track of internal data structures to prevent
743 743 # them from cluttering user-visible stuff. Will be updated later
744 744 self.internal_ns = {}
745 745
746 746 # Now that FakeModule produces a real module, we've run into a nasty
747 747 # problem: after script execution (via %run), the module where the user
748 748 # code ran is deleted. Now that this object is a true module (needed
749 749 # so docetst and other tools work correctly), the Python module
750 750 # teardown mechanism runs over it, and sets to None every variable
751 751 # present in that module. Top-level references to objects from the
752 752 # script survive, because the user_ns is updated with them. However,
753 753 # calling functions defined in the script that use other things from
754 754 # the script will fail, because the function's closure had references
755 755 # to the original objects, which are now all None. So we must protect
756 756 # these modules from deletion by keeping a cache.
757 757 #
758 758 # To avoid keeping stale modules around (we only need the one from the
759 759 # last run), we use a dict keyed with the full path to the script, so
760 760 # only the last version of the module is held in the cache. Note,
761 761 # however, that we must cache the module *namespace contents* (their
762 762 # __dict__). Because if we try to cache the actual modules, old ones
763 763 # (uncached) could be destroyed while still holding references (such as
764 764 # those held by GUI objects that tend to be long-lived)>
765 765 #
766 766 # The %reset command will flush this cache. See the cache_main_mod()
767 767 # and clear_main_mod_cache() methods for details on use.
768 768
769 769 # This is the cache used for 'main' namespaces
770 770 self._main_ns_cache = {}
771 771 # And this is the single instance of FakeModule whose __dict__ we keep
772 772 # copying and clearing for reuse on each %run
773 773 self._user_main_module = FakeModule()
774 774
775 775 # A table holding all the namespaces IPython deals with, so that
776 776 # introspection facilities can search easily.
777 777 self.ns_table = {'user':user_ns,
778 778 'user_global':user_global_ns,
779 779 'internal':self.internal_ns,
780 780 'builtin':__builtin__.__dict__
781 781 }
782 782
783 783 # Similarly, track all namespaces where references can be held and that
784 784 # we can safely clear (so it can NOT include builtin). This one can be
785 785 # a simple list.
786 786 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
787 787 self.internal_ns, self._main_ns_cache ]
788 788
789 789 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
790 790 """Return a valid local and global user interactive namespaces.
791 791
792 792 This builds a dict with the minimal information needed to operate as a
793 793 valid IPython user namespace, which you can pass to the various
794 794 embedding classes in ipython. The default implementation returns the
795 795 same dict for both the locals and the globals to allow functions to
796 796 refer to variables in the namespace. Customized implementations can
797 797 return different dicts. The locals dictionary can actually be anything
798 798 following the basic mapping protocol of a dict, but the globals dict
799 799 must be a true dict, not even a subclass. It is recommended that any
800 800 custom object for the locals namespace synchronize with the globals
801 801 dict somehow.
802 802
803 803 Raises TypeError if the provided globals namespace is not a true dict.
804 804
805 805 Parameters
806 806 ----------
807 807 user_ns : dict-like, optional
808 808 The current user namespace. The items in this namespace should
809 809 be included in the output. If None, an appropriate blank
810 810 namespace should be created.
811 811 user_global_ns : dict, optional
812 812 The current user global namespace. The items in this namespace
813 813 should be included in the output. If None, an appropriate
814 814 blank namespace should be created.
815 815
816 816 Returns
817 817 -------
818 818 A pair of dictionary-like object to be used as the local namespace
819 819 of the interpreter and a dict to be used as the global namespace.
820 820 """
821 821
822 822
823 823 # We must ensure that __builtin__ (without the final 's') is always
824 824 # available and pointing to the __builtin__ *module*. For more details:
825 825 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
826 826
827 827 if user_ns is None:
828 828 # Set __name__ to __main__ to better match the behavior of the
829 829 # normal interpreter.
830 830 user_ns = {'__name__' :'__main__',
831 831 '__builtin__' : __builtin__,
832 832 '__builtins__' : __builtin__,
833 833 }
834 834 else:
835 835 user_ns.setdefault('__name__','__main__')
836 836 user_ns.setdefault('__builtin__',__builtin__)
837 837 user_ns.setdefault('__builtins__',__builtin__)
838 838
839 839 if user_global_ns is None:
840 840 user_global_ns = user_ns
841 841 if type(user_global_ns) is not dict:
842 842 raise TypeError("user_global_ns must be a true dict; got %r"
843 843 % type(user_global_ns))
844 844
845 845 return user_ns, user_global_ns
846 846
847 847 def init_sys_modules(self):
848 848 # We need to insert into sys.modules something that looks like a
849 849 # module but which accesses the IPython namespace, for shelve and
850 850 # pickle to work interactively. Normally they rely on getting
851 851 # everything out of __main__, but for embedding purposes each IPython
852 852 # instance has its own private namespace, so we can't go shoving
853 853 # everything into __main__.
854 854
855 855 # note, however, that we should only do this for non-embedded
856 856 # ipythons, which really mimic the __main__.__dict__ with their own
857 857 # namespace. Embedded instances, on the other hand, should not do
858 858 # this because they need to manage the user local/global namespaces
859 859 # only, but they live within a 'normal' __main__ (meaning, they
860 860 # shouldn't overtake the execution environment of the script they're
861 861 # embedded in).
862 862
863 863 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
864 864
865 865 try:
866 866 main_name = self.user_ns['__name__']
867 867 except KeyError:
868 868 raise KeyError('user_ns dictionary MUST have a "__name__" key')
869 869 else:
870 870 sys.modules[main_name] = FakeModule(self.user_ns)
871 871
872 872 def init_user_ns(self):
873 873 """Initialize all user-visible namespaces to their minimum defaults.
874 874
875 875 Certain history lists are also initialized here, as they effectively
876 876 act as user namespaces.
877 877
878 878 Notes
879 879 -----
880 880 All data structures here are only filled in, they are NOT reset by this
881 881 method. If they were not empty before, data will simply be added to
882 882 therm.
883 883 """
884 884 # This function works in two parts: first we put a few things in
885 885 # user_ns, and we sync that contents into user_ns_hidden so that these
886 886 # initial variables aren't shown by %who. After the sync, we add the
887 887 # rest of what we *do* want the user to see with %who even on a new
888 888 # session (probably nothing, so theye really only see their own stuff)
889 889
890 890 # The user dict must *always* have a __builtin__ reference to the
891 891 # Python standard __builtin__ namespace, which must be imported.
892 892 # This is so that certain operations in prompt evaluation can be
893 893 # reliably executed with builtins. Note that we can NOT use
894 894 # __builtins__ (note the 's'), because that can either be a dict or a
895 895 # module, and can even mutate at runtime, depending on the context
896 896 # (Python makes no guarantees on it). In contrast, __builtin__ is
897 897 # always a module object, though it must be explicitly imported.
898 898
899 899 # For more details:
900 900 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
901 901 ns = dict(__builtin__ = __builtin__)
902 902
903 903 # Put 'help' in the user namespace
904 904 try:
905 905 from site import _Helper
906 906 ns['help'] = _Helper()
907 907 except ImportError:
908 908 warn('help() not available - check site.py')
909 909
910 910 # make global variables for user access to the histories
911 911 ns['_ih'] = self.input_hist
912 912 ns['_oh'] = self.output_hist
913 913 ns['_dh'] = self.dir_hist
914 914
915 915 ns['_sh'] = shadowns
916 916
917 917 # user aliases to input and output histories. These shouldn't show up
918 918 # in %who, as they can have very large reprs.
919 919 ns['In'] = self.input_hist
920 920 ns['Out'] = self.output_hist
921 921
922 922 # Store myself as the public api!!!
923 923 ns['get_ipython'] = self.get_ipython
924 924
925 925 # Sync what we've added so far to user_ns_hidden so these aren't seen
926 926 # by %who
927 927 self.user_ns_hidden.update(ns)
928 928
929 929 # Anything put into ns now would show up in %who. Think twice before
930 930 # putting anything here, as we really want %who to show the user their
931 931 # stuff, not our variables.
932 932
933 933 # Finally, update the real user's namespace
934 934 self.user_ns.update(ns)
935 935
936 936
937 937 def reset(self):
938 938 """Clear all internal namespaces.
939 939
940 940 Note that this is much more aggressive than %reset, since it clears
941 941 fully all namespaces, as well as all input/output lists.
942 942 """
943 943 for ns in self.ns_refs_table:
944 944 ns.clear()
945 945
946 946 self.alias_manager.clear_aliases()
947 947
948 948 # Clear input and output histories
949 949 self.input_hist[:] = []
950 950 self.input_hist_raw[:] = []
951 951 self.output_hist.clear()
952 952
953 953 # Restore the user namespaces to minimal usability
954 954 self.init_user_ns()
955 955
956 956 # Restore the default and user aliases
957 957 self.alias_manager.init_aliases()
958 958
959 959 def reset_selective(self, regex=None):
960 960 """Clear selective variables from internal namespaces based on a specified regular expression.
961 961
962 962 Parameters
963 963 ----------
964 964 regex : string or compiled pattern, optional
965 965 A regular expression pattern that will be used in searching variable names in the users
966 966 namespaces.
967 967 """
968 968 if regex is not None:
969 969 try:
970 970 m = re.compile(regex)
971 971 except TypeError:
972 972 raise TypeError('regex must be a string or compiled pattern')
973 973 # Search for keys in each namespace that match the given regex
974 974 # If a match is found, delete the key/value pair.
975 975 for ns in self.ns_refs_table:
976 976 for var in ns:
977 977 if m.search(var):
978 978 del ns[var]
979 979
980 980 def push(self, variables, interactive=True):
981 981 """Inject a group of variables into the IPython user namespace.
982 982
983 983 Parameters
984 984 ----------
985 985 variables : dict, str or list/tuple of str
986 986 The variables to inject into the user's namespace. If a dict,
987 987 a simple update is done. If a str, the string is assumed to
988 988 have variable names separated by spaces. A list/tuple of str
989 989 can also be used to give the variable names. If just the variable
990 990 names are give (list/tuple/str) then the variable values looked
991 991 up in the callers frame.
992 992 interactive : bool
993 993 If True (default), the variables will be listed with the ``who``
994 994 magic.
995 995 """
996 996 vdict = None
997 997
998 998 # We need a dict of name/value pairs to do namespace updates.
999 999 if isinstance(variables, dict):
1000 1000 vdict = variables
1001 1001 elif isinstance(variables, (basestring, list, tuple)):
1002 1002 if isinstance(variables, basestring):
1003 1003 vlist = variables.split()
1004 1004 else:
1005 1005 vlist = variables
1006 1006 vdict = {}
1007 1007 cf = sys._getframe(1)
1008 1008 for name in vlist:
1009 1009 try:
1010 1010 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1011 1011 except:
1012 1012 print ('Could not get variable %s from %s' %
1013 1013 (name,cf.f_code.co_name))
1014 1014 else:
1015 1015 raise ValueError('variables must be a dict/str/list/tuple')
1016 1016
1017 1017 # Propagate variables to user namespace
1018 1018 self.user_ns.update(vdict)
1019 1019
1020 1020 # And configure interactive visibility
1021 1021 config_ns = self.user_ns_hidden
1022 1022 if interactive:
1023 1023 for name, val in vdict.iteritems():
1024 1024 config_ns.pop(name, None)
1025 1025 else:
1026 1026 for name,val in vdict.iteritems():
1027 1027 config_ns[name] = val
1028 1028
1029 1029 #-------------------------------------------------------------------------
1030 1030 # Things related to history management
1031 1031 #-------------------------------------------------------------------------
1032 1032
1033 1033 def init_history(self):
1034 1034 # List of input with multi-line handling.
1035 1035 self.input_hist = InputList()
1036 1036 # This one will hold the 'raw' input history, without any
1037 1037 # pre-processing. This will allow users to retrieve the input just as
1038 1038 # it was exactly typed in by the user, with %hist -r.
1039 1039 self.input_hist_raw = InputList()
1040 1040
1041 1041 # list of visited directories
1042 1042 try:
1043 1043 self.dir_hist = [os.getcwd()]
1044 1044 except OSError:
1045 1045 self.dir_hist = []
1046 1046
1047 1047 # dict of output history
1048 1048 self.output_hist = {}
1049 1049
1050 1050 # Now the history file
1051 1051 if self.profile:
1052 1052 histfname = 'history-%s' % self.profile
1053 1053 else:
1054 1054 histfname = 'history'
1055 1055 self.histfile = os.path.join(self.ipython_dir, histfname)
1056 1056
1057 1057 # Fill the history zero entry, user counter starts at 1
1058 1058 self.input_hist.append('\n')
1059 1059 self.input_hist_raw.append('\n')
1060 1060
1061 1061 def init_shadow_hist(self):
1062 1062 try:
1063 1063 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1064 1064 except exceptions.UnicodeDecodeError:
1065 1065 print "Your ipython_dir can't be decoded to unicode!"
1066 1066 print "Please set HOME environment variable to something that"
1067 1067 print r"only has ASCII characters, e.g. c:\home"
1068 1068 print "Now it is", self.ipython_dir
1069 1069 sys.exit()
1070 1070 self.shadowhist = ipcorehist.ShadowHist(self.db)
1071 1071
1072 1072 def savehist(self):
1073 1073 """Save input history to a file (via readline library)."""
1074 1074
1075 1075 try:
1076 1076 self.readline.write_history_file(self.histfile)
1077 1077 except:
1078 1078 print 'Unable to save IPython command history to file: ' + \
1079 1079 `self.histfile`
1080 1080
1081 1081 def reloadhist(self):
1082 1082 """Reload the input history from disk file."""
1083 1083
1084 1084 try:
1085 1085 self.readline.clear_history()
1086 1086 self.readline.read_history_file(self.shell.histfile)
1087 1087 except AttributeError:
1088 1088 pass
1089 1089
1090 1090 def history_saving_wrapper(self, func):
1091 1091 """ Wrap func for readline history saving
1092 1092
1093 1093 Convert func into callable that saves & restores
1094 1094 history around the call """
1095 1095
1096 1096 if self.has_readline:
1097 1097 from IPython.utils import rlineimpl as readline
1098 1098 else:
1099 1099 return func
1100 1100
1101 1101 def wrapper():
1102 1102 self.savehist()
1103 1103 try:
1104 1104 func()
1105 1105 finally:
1106 1106 readline.read_history_file(self.histfile)
1107 1107 return wrapper
1108 1108
1109 1109 def get_history(self, index=None, raw=False, output=True):
1110 1110 """Get the history list.
1111 1111
1112 1112 Get the input and output history.
1113 1113
1114 1114 Parameters
1115 1115 ----------
1116 1116 index : n or (n1, n2) or None
1117 1117 If n, then the last entries. If a tuple, then all in
1118 1118 range(n1, n2). If None, then all entries. Raises IndexError if
1119 1119 the format of index is incorrect.
1120 1120 raw : bool
1121 1121 If True, return the raw input.
1122 1122 output : bool
1123 1123 If True, then return the output as well.
1124 1124
1125 1125 Returns
1126 1126 -------
1127 1127 If output is True, then return a dict of tuples, keyed by the prompt
1128 1128 numbers and with values of (input, output). If output is False, then
1129 1129 a dict, keyed by the prompt number with the values of input. Raises
1130 1130 IndexError if no history is found.
1131 1131 """
1132 1132 if raw:
1133 1133 input_hist = self.input_hist_raw
1134 1134 else:
1135 1135 input_hist = self.input_hist
1136 1136 if output:
1137 1137 output_hist = self.user_ns['Out']
1138 1138 n = len(input_hist)
1139 1139 if index is None:
1140 1140 start=0; stop=n
1141 1141 elif isinstance(index, int):
1142 1142 start=n-index; stop=n
1143 1143 elif isinstance(index, tuple) and len(index) == 2:
1144 1144 start=index[0]; stop=index[1]
1145 1145 else:
1146 1146 raise IndexError('Not a valid index for the input history: %r' % index)
1147 1147 hist = {}
1148 1148 for i in range(start, stop):
1149 1149 if output:
1150 1150 hist[i] = (input_hist[i], output_hist.get(i))
1151 1151 else:
1152 1152 hist[i] = input_hist[i]
1153 1153 if len(hist)==0:
1154 1154 raise IndexError('No history for range of indices: %r' % index)
1155 1155 return hist
1156 1156
1157 1157 #-------------------------------------------------------------------------
1158 1158 # Things related to exception handling and tracebacks (not debugging)
1159 1159 #-------------------------------------------------------------------------
1160 1160
1161 1161 def init_traceback_handlers(self, custom_exceptions):
1162 1162 # Syntax error handler.
1163 1163 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1164 1164
1165 1165 # The interactive one is initialized with an offset, meaning we always
1166 1166 # want to remove the topmost item in the traceback, which is our own
1167 1167 # internal code. Valid modes: ['Plain','Context','Verbose']
1168 1168 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1169 1169 color_scheme='NoColor',
1170 1170 tb_offset = 1)
1171 1171
1172 1172 # The instance will store a pointer to the system-wide exception hook,
1173 1173 # so that runtime code (such as magics) can access it. This is because
1174 1174 # during the read-eval loop, it may get temporarily overwritten.
1175 1175 self.sys_excepthook = sys.excepthook
1176 1176
1177 1177 # and add any custom exception handlers the user may have specified
1178 1178 self.set_custom_exc(*custom_exceptions)
1179 1179
1180 1180 # Set the exception mode
1181 1181 self.InteractiveTB.set_mode(mode=self.xmode)
1182 1182
1183 1183 def set_custom_exc(self, exc_tuple, handler):
1184 1184 """set_custom_exc(exc_tuple,handler)
1185 1185
1186 1186 Set a custom exception handler, which will be called if any of the
1187 1187 exceptions in exc_tuple occur in the mainloop (specifically, in the
1188 1188 runcode() method.
1189 1189
1190 1190 Inputs:
1191 1191
1192 1192 - exc_tuple: a *tuple* of valid exceptions to call the defined
1193 1193 handler for. It is very important that you use a tuple, and NOT A
1194 1194 LIST here, because of the way Python's except statement works. If
1195 1195 you only want to trap a single exception, use a singleton tuple:
1196 1196
1197 1197 exc_tuple == (MyCustomException,)
1198 1198
1199 1199 - handler: this must be defined as a function with the following
1200 1200 basic interface::
1201 1201
1202 1202 def my_handler(self, etype, value, tb, tb_offset=None)
1203 1203 ...
1204 1204 # The return value must be
1205 1205 return structured_traceback
1206 1206
1207 1207 This will be made into an instance method (via new.instancemethod)
1208 1208 of IPython itself, and it will be called if any of the exceptions
1209 1209 listed in the exc_tuple are caught. If the handler is None, an
1210 1210 internal basic one is used, which just prints basic info.
1211 1211
1212 1212 WARNING: by putting in your own exception handler into IPython's main
1213 1213 execution loop, you run a very good chance of nasty crashes. This
1214 1214 facility should only be used if you really know what you are doing."""
1215 1215
1216 1216 assert type(exc_tuple)==type(()) , \
1217 1217 "The custom exceptions must be given AS A TUPLE."
1218 1218
1219 1219 def dummy_handler(self,etype,value,tb):
1220 1220 print '*** Simple custom exception handler ***'
1221 1221 print 'Exception type :',etype
1222 1222 print 'Exception value:',value
1223 1223 print 'Traceback :',tb
1224 1224 print 'Source code :','\n'.join(self.buffer)
1225 1225
1226 1226 if handler is None: handler = dummy_handler
1227 1227
1228 1228 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1229 1229 self.custom_exceptions = exc_tuple
1230 1230
1231 1231 def excepthook(self, etype, value, tb):
1232 1232 """One more defense for GUI apps that call sys.excepthook.
1233 1233
1234 1234 GUI frameworks like wxPython trap exceptions and call
1235 1235 sys.excepthook themselves. I guess this is a feature that
1236 1236 enables them to keep running after exceptions that would
1237 1237 otherwise kill their mainloop. This is a bother for IPython
1238 1238 which excepts to catch all of the program exceptions with a try:
1239 1239 except: statement.
1240 1240
1241 1241 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1242 1242 any app directly invokes sys.excepthook, it will look to the user like
1243 1243 IPython crashed. In order to work around this, we can disable the
1244 1244 CrashHandler and replace it with this excepthook instead, which prints a
1245 1245 regular traceback using our InteractiveTB. In this fashion, apps which
1246 1246 call sys.excepthook will generate a regular-looking exception from
1247 1247 IPython, and the CrashHandler will only be triggered by real IPython
1248 1248 crashes.
1249 1249
1250 1250 This hook should be used sparingly, only in places which are not likely
1251 1251 to be true IPython errors.
1252 1252 """
1253 1253 self.showtraceback((etype,value,tb),tb_offset=0)
1254 1254
1255 1255 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1256 1256 exception_only=False):
1257 1257 """Display the exception that just occurred.
1258 1258
1259 1259 If nothing is known about the exception, this is the method which
1260 1260 should be used throughout the code for presenting user tracebacks,
1261 1261 rather than directly invoking the InteractiveTB object.
1262 1262
1263 1263 A specific showsyntaxerror() also exists, but this method can take
1264 1264 care of calling it if needed, so unless you are explicitly catching a
1265 1265 SyntaxError exception, don't try to analyze the stack manually and
1266 1266 simply call this method."""
1267 1267
1268 1268 try:
1269 1269 if exc_tuple is None:
1270 1270 etype, value, tb = sys.exc_info()
1271 1271 else:
1272 1272 etype, value, tb = exc_tuple
1273 1273
1274 1274 if etype is None:
1275 1275 if hasattr(sys, 'last_type'):
1276 1276 etype, value, tb = sys.last_type, sys.last_value, \
1277 1277 sys.last_traceback
1278 1278 else:
1279 1279 self.write_err('No traceback available to show.\n')
1280 1280 return
1281 1281
1282 1282 if etype is SyntaxError:
1283 1283 # Though this won't be called by syntax errors in the input
1284 1284 # line, there may be SyntaxError cases whith imported code.
1285 1285 self.showsyntaxerror(filename)
1286 1286 elif etype is UsageError:
1287 1287 print "UsageError:", value
1288 1288 else:
1289 1289 # WARNING: these variables are somewhat deprecated and not
1290 1290 # necessarily safe to use in a threaded environment, but tools
1291 1291 # like pdb depend on their existence, so let's set them. If we
1292 1292 # find problems in the field, we'll need to revisit their use.
1293 1293 sys.last_type = etype
1294 1294 sys.last_value = value
1295 1295 sys.last_traceback = tb
1296 1296
1297 1297 if etype in self.custom_exceptions:
1298 1298 # FIXME: Old custom traceback objects may just return a
1299 1299 # string, in that case we just put it into a list
1300 1300 stb = self.CustomTB(etype, value, tb, tb_offset)
1301 1301 if isinstance(ctb, basestring):
1302 1302 stb = [stb]
1303 1303 else:
1304 1304 if exception_only:
1305 1305 stb = ['An exception has occurred, use %tb to see '
1306 'the full traceback.']
1306 'the full traceback.\n']
1307 1307 stb.extend(self.InteractiveTB.get_exception_only(etype,
1308 1308 value))
1309 1309 else:
1310 1310 stb = self.InteractiveTB.structured_traceback(etype,
1311 1311 value, tb, tb_offset=tb_offset)
1312 1312 # FIXME: the pdb calling should be done by us, not by
1313 1313 # the code computing the traceback.
1314 1314 if self.InteractiveTB.call_pdb:
1315 1315 # pdb mucks up readline, fix it back
1316 1316 self.set_completer()
1317 1317
1318 1318 # Actually show the traceback
1319 1319 self._showtraceback(etype, value, stb)
1320 1320
1321 1321 except KeyboardInterrupt:
1322 1322 self.write_err("\nKeyboardInterrupt\n")
1323 1323
1324 1324 def _showtraceback(self, etype, evalue, stb):
1325 1325 """Actually show a traceback.
1326 1326
1327 1327 Subclasses may override this method to put the traceback on a different
1328 1328 place, like a side channel.
1329 1329 """
1330 self.write_err('\n'.join(stb))
1330 # FIXME: this should use the proper write channels, but our test suite
1331 # relies on it coming out of stdout...
1332 print >> sys.stdout, self.InteractiveTB.stb2text(stb)
1331 1333
1332 1334 def showsyntaxerror(self, filename=None):
1333 1335 """Display the syntax error that just occurred.
1334 1336
1335 1337 This doesn't display a stack trace because there isn't one.
1336 1338
1337 1339 If a filename is given, it is stuffed in the exception instead
1338 1340 of what was there before (because Python's parser always uses
1339 1341 "<string>" when reading from a string).
1340 1342 """
1341 1343 etype, value, last_traceback = sys.exc_info()
1342 1344
1343 1345 # See note about these variables in showtraceback() above
1344 1346 sys.last_type = etype
1345 1347 sys.last_value = value
1346 1348 sys.last_traceback = last_traceback
1347 1349
1348 1350 if filename and etype is SyntaxError:
1349 1351 # Work hard to stuff the correct filename in the exception
1350 1352 try:
1351 1353 msg, (dummy_filename, lineno, offset, line) = value
1352 1354 except:
1353 1355 # Not the format we expect; leave it alone
1354 1356 pass
1355 1357 else:
1356 1358 # Stuff in the right filename
1357 1359 try:
1358 1360 # Assume SyntaxError is a class exception
1359 1361 value = SyntaxError(msg, (filename, lineno, offset, line))
1360 1362 except:
1361 1363 # If that failed, assume SyntaxError is a string
1362 1364 value = msg, (filename, lineno, offset, line)
1363 1365 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1364 1366 self._showtraceback(etype, value, stb)
1365 1367
1366 1368 #-------------------------------------------------------------------------
1367 1369 # Things related to tab completion
1368 1370 #-------------------------------------------------------------------------
1369 1371
1370 def complete(self, text):
1372 def complete(self, text, line=None, cursor_pos=None):
1371 1373 """Return a sorted list of all possible completions on text.
1372 1374
1373 Inputs:
1375 Parameters
1376 ----------
1377
1378 text : string
1379 A string of text to be completed on.
1374 1380
1375 - text: a string of text to be completed on.
1381 line : string, optional
1382 The complete line that text is part of.
1376 1383
1384 cursor_pos : int, optional
1385 The position of the cursor on the input line.
1386
1387 The optional arguments allow the completion to take more context into
1388 account, and are part of the low-level completion API.
1389
1377 1390 This is a wrapper around the completion mechanism, similar to what
1378 1391 readline does at the command line when the TAB key is hit. By
1379 1392 exposing it as a method, it can be used by other non-readline
1380 1393 environments (such as GUIs) for text completion.
1381 1394
1382 1395 Simple usage example:
1383 1396
1384 1397 In [7]: x = 'hello'
1385 1398
1386 1399 In [8]: x
1387 1400 Out[8]: 'hello'
1388 1401
1389 1402 In [9]: print x
1390 1403 hello
1391 1404
1392 1405 In [10]: _ip.complete('x.l')
1393 1406 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1394 1407 """
1395 1408
1396 1409 # Inject names into __builtin__ so we can complete on the added names.
1397 1410 with self.builtin_trap:
1398 complete = self.Completer.complete
1399 state = 0
1400 # use a dict so we get unique keys, since ipyhton's multiple
1401 # completers can return duplicates. When we make 2.4 a requirement,
1402 # start using sets instead, which are faster.
1403 comps = {}
1404 while True:
1405 newcomp = complete(text,state,line_buffer=text)
1406 if newcomp is None:
1407 break
1408 comps[newcomp] = 1
1409 state += 1
1410 outcomps = comps.keys()
1411 outcomps.sort()
1412 #print "T:",text,"OC:",outcomps # dbg
1413 #print "vars:",self.user_ns.keys()
1414 return outcomps
1411 return self.Completer.complete(text,line_buffer=text)
1415 1412
1416 1413 def set_custom_completer(self,completer,pos=0):
1417 1414 """Adds a new custom completer function.
1418 1415
1419 1416 The position argument (defaults to 0) is the index in the completers
1420 1417 list where you want the completer to be inserted."""
1421 1418
1422 1419 newcomp = new.instancemethod(completer,self.Completer,
1423 1420 self.Completer.__class__)
1424 1421 self.Completer.matchers.insert(pos,newcomp)
1425 1422
1426 1423 def set_completer(self):
1427 1424 """Reset readline's completer to be our own."""
1428 self.readline.set_completer(self.Completer.complete)
1425 self.readline.set_completer(self.Completer.rlcomplete)
1429 1426
1430 1427 def set_completer_frame(self, frame=None):
1431 1428 """Set the frame of the completer."""
1432 1429 if frame:
1433 1430 self.Completer.namespace = frame.f_locals
1434 1431 self.Completer.global_namespace = frame.f_globals
1435 1432 else:
1436 1433 self.Completer.namespace = self.user_ns
1437 1434 self.Completer.global_namespace = self.user_global_ns
1438 1435
1439 1436 #-------------------------------------------------------------------------
1440 1437 # Things related to readline
1441 1438 #-------------------------------------------------------------------------
1442 1439
1443 1440 def init_readline(self):
1444 1441 """Command history completion/saving/reloading."""
1445 1442
1446 1443 if self.readline_use:
1447 1444 import IPython.utils.rlineimpl as readline
1448 1445
1449 1446 self.rl_next_input = None
1450 1447 self.rl_do_indent = False
1451 1448
1452 1449 if not self.readline_use or not readline.have_readline:
1453 1450 self.has_readline = False
1454 1451 self.readline = None
1455 1452 # Set a number of methods that depend on readline to be no-op
1456 1453 self.savehist = no_op
1457 1454 self.reloadhist = no_op
1458 1455 self.set_completer = no_op
1459 1456 self.set_custom_completer = no_op
1460 1457 self.set_completer_frame = no_op
1461 1458 warn('Readline services not available or not loaded.')
1462 1459 else:
1463 1460 self.has_readline = True
1464 1461 self.readline = readline
1465 1462 sys.modules['readline'] = readline
1466 1463 import atexit
1467 1464 from IPython.core.completer import IPCompleter
1468 1465 self.Completer = IPCompleter(self,
1469 1466 self.user_ns,
1470 1467 self.user_global_ns,
1471 1468 self.readline_omit__names,
1472 1469 self.alias_manager.alias_table)
1473 1470 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1474 1471 self.strdispatchers['complete_command'] = sdisp
1475 1472 self.Completer.custom_completers = sdisp
1476 1473 # Platform-specific configuration
1477 1474 if os.name == 'nt':
1478 1475 self.readline_startup_hook = readline.set_pre_input_hook
1479 1476 else:
1480 1477 self.readline_startup_hook = readline.set_startup_hook
1481 1478
1482 1479 # Load user's initrc file (readline config)
1483 1480 # Or if libedit is used, load editrc.
1484 1481 inputrc_name = os.environ.get('INPUTRC')
1485 1482 if inputrc_name is None:
1486 1483 home_dir = get_home_dir()
1487 1484 if home_dir is not None:
1488 1485 inputrc_name = '.inputrc'
1489 1486 if readline.uses_libedit:
1490 1487 inputrc_name = '.editrc'
1491 1488 inputrc_name = os.path.join(home_dir, inputrc_name)
1492 1489 if os.path.isfile(inputrc_name):
1493 1490 try:
1494 1491 readline.read_init_file(inputrc_name)
1495 1492 except:
1496 1493 warn('Problems reading readline initialization file <%s>'
1497 1494 % inputrc_name)
1498 1495
1499 1496 # save this in sys so embedded copies can restore it properly
1500 sys.ipcompleter = self.Completer.complete
1497 sys.ipcompleter = self.Completer.rlcomplete
1501 1498 self.set_completer()
1502 1499
1503 1500 # Configure readline according to user's prefs
1504 1501 # This is only done if GNU readline is being used. If libedit
1505 1502 # is being used (as on Leopard) the readline config is
1506 1503 # not run as the syntax for libedit is different.
1507 1504 if not readline.uses_libedit:
1508 1505 for rlcommand in self.readline_parse_and_bind:
1509 1506 #print "loading rl:",rlcommand # dbg
1510 1507 readline.parse_and_bind(rlcommand)
1511 1508
1512 1509 # Remove some chars from the delimiters list. If we encounter
1513 1510 # unicode chars, discard them.
1514 1511 delims = readline.get_completer_delims().encode("ascii", "ignore")
1515 1512 delims = delims.translate(string._idmap,
1516 1513 self.readline_remove_delims)
1517 1514 readline.set_completer_delims(delims)
1518 1515 # otherwise we end up with a monster history after a while:
1519 1516 readline.set_history_length(1000)
1520 1517 try:
1521 1518 #print '*** Reading readline history' # dbg
1522 1519 readline.read_history_file(self.histfile)
1523 1520 except IOError:
1524 1521 pass # It doesn't exist yet.
1525 1522
1526 1523 atexit.register(self.atexit_operations)
1527 1524 del atexit
1528 1525
1529 1526 # Configure auto-indent for all platforms
1530 1527 self.set_autoindent(self.autoindent)
1531 1528
1532 1529 def set_next_input(self, s):
1533 1530 """ Sets the 'default' input string for the next command line.
1534 1531
1535 1532 Requires readline.
1536 1533
1537 1534 Example:
1538 1535
1539 1536 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1540 1537 [D:\ipython]|2> Hello Word_ # cursor is here
1541 1538 """
1542 1539
1543 1540 self.rl_next_input = s
1544 1541
1545 1542 # Maybe move this to the terminal subclass?
1546 1543 def pre_readline(self):
1547 1544 """readline hook to be used at the start of each line.
1548 1545
1549 1546 Currently it handles auto-indent only."""
1550 1547
1551 1548 if self.rl_do_indent:
1552 1549 self.readline.insert_text(self._indent_current_str())
1553 1550 if self.rl_next_input is not None:
1554 1551 self.readline.insert_text(self.rl_next_input)
1555 1552 self.rl_next_input = None
1556 1553
1557 1554 def _indent_current_str(self):
1558 1555 """return the current level of indentation as a string"""
1559 1556 return self.indent_current_nsp * ' '
1560 1557
1561 1558 #-------------------------------------------------------------------------
1562 1559 # Things related to magics
1563 1560 #-------------------------------------------------------------------------
1564 1561
1565 1562 def init_magics(self):
1566 1563 # FIXME: Move the color initialization to the DisplayHook, which
1567 1564 # should be split into a prompt manager and displayhook. We probably
1568 1565 # even need a centralize colors management object.
1569 1566 self.magic_colors(self.colors)
1570 1567 # History was moved to a separate module
1571 1568 from . import history
1572 1569 history.init_ipython(self)
1573 1570
1574 1571 def magic(self,arg_s):
1575 1572 """Call a magic function by name.
1576 1573
1577 1574 Input: a string containing the name of the magic function to call and any
1578 1575 additional arguments to be passed to the magic.
1579 1576
1580 1577 magic('name -opt foo bar') is equivalent to typing at the ipython
1581 1578 prompt:
1582 1579
1583 1580 In[1]: %name -opt foo bar
1584 1581
1585 1582 To call a magic without arguments, simply use magic('name').
1586 1583
1587 1584 This provides a proper Python function to call IPython's magics in any
1588 1585 valid Python code you can type at the interpreter, including loops and
1589 1586 compound statements.
1590 1587 """
1591 1588 args = arg_s.split(' ',1)
1592 1589 magic_name = args[0]
1593 1590 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1594 1591
1595 1592 try:
1596 1593 magic_args = args[1]
1597 1594 except IndexError:
1598 1595 magic_args = ''
1599 1596 fn = getattr(self,'magic_'+magic_name,None)
1600 1597 if fn is None:
1601 1598 error("Magic function `%s` not found." % magic_name)
1602 1599 else:
1603 1600 magic_args = self.var_expand(magic_args,1)
1604 1601 with nested(self.builtin_trap,):
1605 1602 result = fn(magic_args)
1606 1603 return result
1607 1604
1608 1605 def define_magic(self, magicname, func):
1609 1606 """Expose own function as magic function for ipython
1610 1607
1611 1608 def foo_impl(self,parameter_s=''):
1612 1609 'My very own magic!. (Use docstrings, IPython reads them).'
1613 1610 print 'Magic function. Passed parameter is between < >:'
1614 1611 print '<%s>' % parameter_s
1615 1612 print 'The self object is:',self
1616 1613
1617 1614 self.define_magic('foo',foo_impl)
1618 1615 """
1619 1616
1620 1617 import new
1621 1618 im = new.instancemethod(func,self, self.__class__)
1622 1619 old = getattr(self, "magic_" + magicname, None)
1623 1620 setattr(self, "magic_" + magicname, im)
1624 1621 return old
1625 1622
1626 1623 #-------------------------------------------------------------------------
1627 1624 # Things related to macros
1628 1625 #-------------------------------------------------------------------------
1629 1626
1630 1627 def define_macro(self, name, themacro):
1631 1628 """Define a new macro
1632 1629
1633 1630 Parameters
1634 1631 ----------
1635 1632 name : str
1636 1633 The name of the macro.
1637 1634 themacro : str or Macro
1638 1635 The action to do upon invoking the macro. If a string, a new
1639 1636 Macro object is created by passing the string to it.
1640 1637 """
1641 1638
1642 1639 from IPython.core import macro
1643 1640
1644 1641 if isinstance(themacro, basestring):
1645 1642 themacro = macro.Macro(themacro)
1646 1643 if not isinstance(themacro, macro.Macro):
1647 1644 raise ValueError('A macro must be a string or a Macro instance.')
1648 1645 self.user_ns[name] = themacro
1649 1646
1650 1647 #-------------------------------------------------------------------------
1651 1648 # Things related to the running of system commands
1652 1649 #-------------------------------------------------------------------------
1653 1650
1654 1651 def system(self, cmd):
1655 1652 """Make a system call, using IPython."""
1656 1653 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1657 1654
1658 1655 #-------------------------------------------------------------------------
1659 1656 # Things related to aliases
1660 1657 #-------------------------------------------------------------------------
1661 1658
1662 1659 def init_alias(self):
1663 1660 self.alias_manager = AliasManager(shell=self, config=self.config)
1664 1661 self.ns_table['alias'] = self.alias_manager.alias_table,
1665 1662
1666 1663 #-------------------------------------------------------------------------
1667 1664 # Things related to extensions and plugins
1668 1665 #-------------------------------------------------------------------------
1669 1666
1670 1667 def init_extension_manager(self):
1671 1668 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1672 1669
1673 1670 def init_plugin_manager(self):
1674 1671 self.plugin_manager = PluginManager(config=self.config)
1675 1672
1676 1673 #-------------------------------------------------------------------------
1677 1674 # Things related to payloads
1678 1675 #-------------------------------------------------------------------------
1679 1676
1680 1677 def init_payload(self):
1681 1678 self.payload_manager = PayloadManager(config=self.config)
1682 1679
1683 1680 #-------------------------------------------------------------------------
1684 1681 # Things related to the prefilter
1685 1682 #-------------------------------------------------------------------------
1686 1683
1687 1684 def init_prefilter(self):
1688 1685 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1689 1686 # Ultimately this will be refactored in the new interpreter code, but
1690 1687 # for now, we should expose the main prefilter method (there's legacy
1691 1688 # code out there that may rely on this).
1692 1689 self.prefilter = self.prefilter_manager.prefilter_lines
1693 1690
1694 1691 #-------------------------------------------------------------------------
1695 1692 # Things related to the running of code
1696 1693 #-------------------------------------------------------------------------
1697 1694
1698 1695 def ex(self, cmd):
1699 1696 """Execute a normal python statement in user namespace."""
1700 1697 with nested(self.builtin_trap,):
1701 1698 exec cmd in self.user_global_ns, self.user_ns
1702 1699
1703 1700 def ev(self, expr):
1704 1701 """Evaluate python expression expr in user namespace.
1705 1702
1706 1703 Returns the result of evaluation
1707 1704 """
1708 1705 with nested(self.builtin_trap,):
1709 1706 return eval(expr, self.user_global_ns, self.user_ns)
1710 1707
1711 1708 def safe_execfile(self, fname, *where, **kw):
1712 1709 """A safe version of the builtin execfile().
1713 1710
1714 1711 This version will never throw an exception, but instead print
1715 1712 helpful error messages to the screen. This only works on pure
1716 1713 Python files with the .py extension.
1717 1714
1718 1715 Parameters
1719 1716 ----------
1720 1717 fname : string
1721 1718 The name of the file to be executed.
1722 1719 where : tuple
1723 1720 One or two namespaces, passed to execfile() as (globals,locals).
1724 1721 If only one is given, it is passed as both.
1725 1722 exit_ignore : bool (False)
1726 1723 If True, then silence SystemExit for non-zero status (it is always
1727 1724 silenced for zero status, as it is so common).
1728 1725 """
1729 1726 kw.setdefault('exit_ignore', False)
1730 1727
1731 1728 fname = os.path.abspath(os.path.expanduser(fname))
1732 1729
1733 1730 # Make sure we have a .py file
1734 1731 if not fname.endswith('.py'):
1735 1732 warn('File must end with .py to be run using execfile: <%s>' % fname)
1736 1733
1737 1734 # Make sure we can open the file
1738 1735 try:
1739 1736 with open(fname) as thefile:
1740 1737 pass
1741 1738 except:
1742 1739 warn('Could not open file <%s> for safe execution.' % fname)
1743 1740 return
1744 1741
1745 1742 # Find things also in current directory. This is needed to mimic the
1746 1743 # behavior of running a script from the system command line, where
1747 1744 # Python inserts the script's directory into sys.path
1748 1745 dname = os.path.dirname(fname)
1749 1746
1750 1747 with prepended_to_syspath(dname):
1751 1748 try:
1752 1749 execfile(fname,*where)
1753 1750 except SystemExit, status:
1754 1751 # If the call was made with 0 or None exit status (sys.exit(0)
1755 1752 # or sys.exit() ), don't bother showing a traceback, as both of
1756 1753 # these are considered normal by the OS:
1757 1754 # > python -c'import sys;sys.exit(0)'; echo $?
1758 1755 # 0
1759 1756 # > python -c'import sys;sys.exit()'; echo $?
1760 1757 # 0
1761 1758 # For other exit status, we show the exception unless
1762 1759 # explicitly silenced, but only in short form.
1763 1760 if status.code not in (0, None) and not kw['exit_ignore']:
1764 1761 self.showtraceback(exception_only=True)
1765 1762 except:
1766 1763 self.showtraceback()
1767 1764
1768 1765 def safe_execfile_ipy(self, fname):
1769 1766 """Like safe_execfile, but for .ipy files with IPython syntax.
1770 1767
1771 1768 Parameters
1772 1769 ----------
1773 1770 fname : str
1774 1771 The name of the file to execute. The filename must have a
1775 1772 .ipy extension.
1776 1773 """
1777 1774 fname = os.path.abspath(os.path.expanduser(fname))
1778 1775
1779 1776 # Make sure we have a .py file
1780 1777 if not fname.endswith('.ipy'):
1781 1778 warn('File must end with .py to be run using execfile: <%s>' % fname)
1782 1779
1783 1780 # Make sure we can open the file
1784 1781 try:
1785 1782 with open(fname) as thefile:
1786 1783 pass
1787 1784 except:
1788 1785 warn('Could not open file <%s> for safe execution.' % fname)
1789 1786 return
1790 1787
1791 1788 # Find things also in current directory. This is needed to mimic the
1792 1789 # behavior of running a script from the system command line, where
1793 1790 # Python inserts the script's directory into sys.path
1794 1791 dname = os.path.dirname(fname)
1795 1792
1796 1793 with prepended_to_syspath(dname):
1797 1794 try:
1798 1795 with open(fname) as thefile:
1799 1796 script = thefile.read()
1800 1797 # self.runlines currently captures all exceptions
1801 1798 # raise in user code. It would be nice if there were
1802 1799 # versions of runlines, execfile that did raise, so
1803 1800 # we could catch the errors.
1804 1801 self.runlines(script, clean=True)
1805 1802 except:
1806 1803 self.showtraceback()
1807 1804 warn('Unknown failure executing file: <%s>' % fname)
1808 1805
1809 1806 def runlines(self, lines, clean=False):
1810 1807 """Run a string of one or more lines of source.
1811 1808
1812 1809 This method is capable of running a string containing multiple source
1813 1810 lines, as if they had been entered at the IPython prompt. Since it
1814 1811 exposes IPython's processing machinery, the given strings can contain
1815 1812 magic calls (%magic), special shell access (!cmd), etc.
1816 1813 """
1817 1814
1818 1815 if isinstance(lines, (list, tuple)):
1819 1816 lines = '\n'.join(lines)
1820 1817
1821 1818 if clean:
1822 1819 lines = self._cleanup_ipy_script(lines)
1823 1820
1824 1821 # We must start with a clean buffer, in case this is run from an
1825 1822 # interactive IPython session (via a magic, for example).
1826 1823 self.resetbuffer()
1827 1824 lines = lines.splitlines()
1828 1825 more = 0
1829 1826
1830 1827 with nested(self.builtin_trap, self.display_trap):
1831 1828 for line in lines:
1832 1829 # skip blank lines so we don't mess up the prompt counter, but do
1833 1830 # NOT skip even a blank line if we are in a code block (more is
1834 1831 # true)
1835 1832
1836 1833 if line or more:
1837 1834 # push to raw history, so hist line numbers stay in sync
1838 1835 self.input_hist_raw.append("# " + line + "\n")
1839 1836 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
1840 1837 more = self.push_line(prefiltered)
1841 1838 # IPython's runsource returns None if there was an error
1842 1839 # compiling the code. This allows us to stop processing right
1843 1840 # away, so the user gets the error message at the right place.
1844 1841 if more is None:
1845 1842 break
1846 1843 else:
1847 1844 self.input_hist_raw.append("\n")
1848 1845 # final newline in case the input didn't have it, so that the code
1849 1846 # actually does get executed
1850 1847 if more:
1851 1848 self.push_line('\n')
1852 1849
1853 1850 def runsource(self, source, filename='<input>', symbol='single'):
1854 1851 """Compile and run some source in the interpreter.
1855 1852
1856 1853 Arguments are as for compile_command().
1857 1854
1858 1855 One several things can happen:
1859 1856
1860 1857 1) The input is incorrect; compile_command() raised an
1861 1858 exception (SyntaxError or OverflowError). A syntax traceback
1862 1859 will be printed by calling the showsyntaxerror() method.
1863 1860
1864 1861 2) The input is incomplete, and more input is required;
1865 1862 compile_command() returned None. Nothing happens.
1866 1863
1867 1864 3) The input is complete; compile_command() returned a code
1868 1865 object. The code is executed by calling self.runcode() (which
1869 1866 also handles run-time exceptions, except for SystemExit).
1870 1867
1871 1868 The return value is:
1872 1869
1873 1870 - True in case 2
1874 1871
1875 1872 - False in the other cases, unless an exception is raised, where
1876 1873 None is returned instead. This can be used by external callers to
1877 1874 know whether to continue feeding input or not.
1878 1875
1879 1876 The return value can be used to decide whether to use sys.ps1 or
1880 1877 sys.ps2 to prompt the next line."""
1881 1878
1882 1879 # if the source code has leading blanks, add 'if 1:\n' to it
1883 1880 # this allows execution of indented pasted code. It is tempting
1884 1881 # to add '\n' at the end of source to run commands like ' a=1'
1885 1882 # directly, but this fails for more complicated scenarios
1886 1883 source=source.encode(self.stdin_encoding)
1887 1884 if source[:1] in [' ', '\t']:
1888 1885 source = 'if 1:\n%s' % source
1889 1886
1890 1887 try:
1891 1888 code = self.compile(source,filename,symbol)
1892 1889 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
1893 1890 # Case 1
1894 1891 self.showsyntaxerror(filename)
1895 1892 return None
1896 1893
1897 1894 if code is None:
1898 1895 # Case 2
1899 1896 return True
1900 1897
1901 1898 # Case 3
1902 1899 # We store the code object so that threaded shells and
1903 1900 # custom exception handlers can access all this info if needed.
1904 1901 # The source corresponding to this can be obtained from the
1905 1902 # buffer attribute as '\n'.join(self.buffer).
1906 1903 self.code_to_run = code
1907 1904 # now actually execute the code object
1908 1905 if self.runcode(code) == 0:
1909 1906 return False
1910 1907 else:
1911 1908 return None
1912 1909
1913 1910 def runcode(self,code_obj):
1914 1911 """Execute a code object.
1915 1912
1916 1913 When an exception occurs, self.showtraceback() is called to display a
1917 1914 traceback.
1918 1915
1919 1916 Return value: a flag indicating whether the code to be run completed
1920 1917 successfully:
1921 1918
1922 1919 - 0: successful execution.
1923 1920 - 1: an error occurred.
1924 1921 """
1925 1922
1926 1923 # Set our own excepthook in case the user code tries to call it
1927 1924 # directly, so that the IPython crash handler doesn't get triggered
1928 1925 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1929 1926
1930 1927 # we save the original sys.excepthook in the instance, in case config
1931 1928 # code (such as magics) needs access to it.
1932 1929 self.sys_excepthook = old_excepthook
1933 1930 outflag = 1 # happens in more places, so it's easier as default
1934 1931 try:
1935 1932 try:
1936 1933 self.hooks.pre_runcode_hook()
1937 1934 #rprint('Running code') # dbg
1938 1935 exec code_obj in self.user_global_ns, self.user_ns
1939 1936 finally:
1940 1937 # Reset our crash handler in place
1941 1938 sys.excepthook = old_excepthook
1942 1939 except SystemExit:
1943 1940 self.resetbuffer()
1944 1941 self.showtraceback(exception_only=True)
1945 1942 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
1946 1943 except self.custom_exceptions:
1947 1944 etype,value,tb = sys.exc_info()
1948 1945 self.CustomTB(etype,value,tb)
1949 1946 except:
1950 1947 self.showtraceback()
1951 1948 else:
1952 1949 outflag = 0
1953 1950 if softspace(sys.stdout, 0):
1954 1951 print
1955 1952 # Flush out code object which has been run (and source)
1956 1953 self.code_to_run = None
1957 1954 return outflag
1958 1955
1959 1956 def push_line(self, line):
1960 1957 """Push a line to the interpreter.
1961 1958
1962 1959 The line should not have a trailing newline; it may have
1963 1960 internal newlines. The line is appended to a buffer and the
1964 1961 interpreter's runsource() method is called with the
1965 1962 concatenated contents of the buffer as source. If this
1966 1963 indicates that the command was executed or invalid, the buffer
1967 1964 is reset; otherwise, the command is incomplete, and the buffer
1968 1965 is left as it was after the line was appended. The return
1969 1966 value is 1 if more input is required, 0 if the line was dealt
1970 1967 with in some way (this is the same as runsource()).
1971 1968 """
1972 1969
1973 1970 # autoindent management should be done here, and not in the
1974 1971 # interactive loop, since that one is only seen by keyboard input. We
1975 1972 # need this done correctly even for code run via runlines (which uses
1976 1973 # push).
1977 1974
1978 1975 #print 'push line: <%s>' % line # dbg
1979 1976 for subline in line.splitlines():
1980 1977 self._autoindent_update(subline)
1981 1978 self.buffer.append(line)
1982 1979 more = self.runsource('\n'.join(self.buffer), self.filename)
1983 1980 if not more:
1984 1981 self.resetbuffer()
1985 1982 return more
1986 1983
1987 1984 def resetbuffer(self):
1988 1985 """Reset the input buffer."""
1989 1986 self.buffer[:] = []
1990 1987
1991 1988 def _is_secondary_block_start(self, s):
1992 1989 if not s.endswith(':'):
1993 1990 return False
1994 1991 if (s.startswith('elif') or
1995 1992 s.startswith('else') or
1996 1993 s.startswith('except') or
1997 1994 s.startswith('finally')):
1998 1995 return True
1999 1996
2000 1997 def _cleanup_ipy_script(self, script):
2001 1998 """Make a script safe for self.runlines()
2002 1999
2003 2000 Currently, IPython is lines based, with blocks being detected by
2004 2001 empty lines. This is a problem for block based scripts that may
2005 2002 not have empty lines after blocks. This script adds those empty
2006 2003 lines to make scripts safe for running in the current line based
2007 2004 IPython.
2008 2005 """
2009 2006 res = []
2010 2007 lines = script.splitlines()
2011 2008 level = 0
2012 2009
2013 2010 for l in lines:
2014 2011 lstripped = l.lstrip()
2015 2012 stripped = l.strip()
2016 2013 if not stripped:
2017 2014 continue
2018 2015 newlevel = len(l) - len(lstripped)
2019 2016 if level > 0 and newlevel == 0 and \
2020 2017 not self._is_secondary_block_start(stripped):
2021 2018 # add empty line
2022 2019 res.append('')
2023 2020 res.append(l)
2024 2021 level = newlevel
2025 2022
2026 2023 return '\n'.join(res) + '\n'
2027 2024
2028 2025 def _autoindent_update(self,line):
2029 2026 """Keep track of the indent level."""
2030 2027
2031 2028 #debugx('line')
2032 2029 #debugx('self.indent_current_nsp')
2033 2030 if self.autoindent:
2034 2031 if line:
2035 2032 inisp = num_ini_spaces(line)
2036 2033 if inisp < self.indent_current_nsp:
2037 2034 self.indent_current_nsp = inisp
2038 2035
2039 2036 if line[-1] == ':':
2040 2037 self.indent_current_nsp += 4
2041 2038 elif dedent_re.match(line):
2042 2039 self.indent_current_nsp -= 4
2043 2040 else:
2044 2041 self.indent_current_nsp = 0
2045 2042
2046 2043 #-------------------------------------------------------------------------
2047 2044 # Things related to GUI support and pylab
2048 2045 #-------------------------------------------------------------------------
2049 2046
2050 2047 def enable_pylab(self, gui=None):
2051 2048 raise NotImplementedError('Implement enable_pylab in a subclass')
2052 2049
2053 2050 #-------------------------------------------------------------------------
2054 2051 # Utilities
2055 2052 #-------------------------------------------------------------------------
2056 2053
2057 2054 def getoutput(self, cmd):
2058 2055 return getoutput(self.var_expand(cmd,depth=2),
2059 2056 header=self.system_header,
2060 2057 verbose=self.system_verbose)
2061 2058
2062 2059 def getoutputerror(self, cmd):
2063 2060 return getoutputerror(self.var_expand(cmd,depth=2),
2064 2061 header=self.system_header,
2065 2062 verbose=self.system_verbose)
2066 2063
2067 2064 def var_expand(self,cmd,depth=0):
2068 2065 """Expand python variables in a string.
2069 2066
2070 2067 The depth argument indicates how many frames above the caller should
2071 2068 be walked to look for the local namespace where to expand variables.
2072 2069
2073 2070 The global namespace for expansion is always the user's interactive
2074 2071 namespace.
2075 2072 """
2076 2073
2077 2074 return str(ItplNS(cmd,
2078 2075 self.user_ns, # globals
2079 2076 # Skip our own frame in searching for locals:
2080 2077 sys._getframe(depth+1).f_locals # locals
2081 2078 ))
2082 2079
2083 2080 def mktempfile(self,data=None):
2084 2081 """Make a new tempfile and return its filename.
2085 2082
2086 2083 This makes a call to tempfile.mktemp, but it registers the created
2087 2084 filename internally so ipython cleans it up at exit time.
2088 2085
2089 2086 Optional inputs:
2090 2087
2091 2088 - data(None): if data is given, it gets written out to the temp file
2092 2089 immediately, and the file is closed again."""
2093 2090
2094 2091 filename = tempfile.mktemp('.py','ipython_edit_')
2095 2092 self.tempfiles.append(filename)
2096 2093
2097 2094 if data:
2098 2095 tmp_file = open(filename,'w')
2099 2096 tmp_file.write(data)
2100 2097 tmp_file.close()
2101 2098 return filename
2102 2099
2103 2100 # TODO: This should be removed when Term is refactored.
2104 2101 def write(self,data):
2105 2102 """Write a string to the default output"""
2106 2103 io.Term.cout.write(data)
2107 2104
2108 2105 # TODO: This should be removed when Term is refactored.
2109 2106 def write_err(self,data):
2110 2107 """Write a string to the default error output"""
2111 2108 io.Term.cerr.write(data)
2112 2109
2113 2110 def ask_yes_no(self,prompt,default=True):
2114 2111 if self.quiet:
2115 2112 return True
2116 2113 return ask_yes_no(prompt,default)
2117 2114
2118 2115 #-------------------------------------------------------------------------
2119 2116 # Things related to IPython exiting
2120 2117 #-------------------------------------------------------------------------
2121 2118
2122 2119 def atexit_operations(self):
2123 2120 """This will be executed at the time of exit.
2124 2121
2125 2122 Saving of persistent data should be performed here.
2126 2123 """
2127 2124 self.savehist()
2128 2125
2129 2126 # Cleanup all tempfiles left around
2130 2127 for tfile in self.tempfiles:
2131 2128 try:
2132 2129 os.unlink(tfile)
2133 2130 except OSError:
2134 2131 pass
2135 2132
2136 2133 # Clear all user namespaces to release all references cleanly.
2137 2134 self.reset()
2138 2135
2139 2136 # Run user hooks
2140 2137 self.hooks.shutdown_hook()
2141 2138
2142 2139 def cleanup(self):
2143 2140 self.restore_sys_module_state()
2144 2141
2145 2142
2146 2143 class InteractiveShellABC(object):
2147 2144 """An abstract base class for InteractiveShell."""
2148 2145 __metaclass__ = abc.ABCMeta
2149 2146
2150 2147 InteractiveShellABC.register(InteractiveShell)
@@ -1,1202 +1,1201 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 ultratb.py -- Spice up your tracebacks!
4 4
5 5 * ColorTB
6 6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 7 ColorTB class is a solution to that problem. It colors the different parts of a
8 8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 9 text editor.
10 10
11 11 Installation instructions for ColorTB:
12 12 import sys,ultratb
13 13 sys.excepthook = ultratb.ColorTB()
14 14
15 15 * VerboseTB
16 16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 18 and intended it for CGI programmers, but why should they have all the fun? I
19 19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 20 but kind of neat, and maybe useful for long-running programs that you believe
21 21 are bug-free. If a crash *does* occur in that type of program you want details.
22 22 Give it a shot--you'll love it or you'll hate it.
23 23
24 24 Note:
25 25
26 26 The Verbose mode prints the variables currently visible where the exception
27 27 happened (shortening their strings if too long). This can potentially be
28 28 very slow, if you happen to have a huge data structure whose string
29 29 representation is complex to compute. Your computer may appear to freeze for
30 30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 31 with Ctrl-C (maybe hitting it more than once).
32 32
33 33 If you encounter this kind of situation often, you may want to use the
34 34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 35 variables (but otherwise includes the information and context given by
36 36 Verbose).
37 37
38 38
39 39 Installation instructions for ColorTB:
40 40 import sys,ultratb
41 41 sys.excepthook = ultratb.VerboseTB()
42 42
43 43 Note: Much of the code in this module was lifted verbatim from the standard
44 44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45 45
46 46 * Color schemes
47 47 The colors are defined in the class TBTools through the use of the
48 48 ColorSchemeTable class. Currently the following exist:
49 49
50 50 - NoColor: allows all of this module to be used in any terminal (the color
51 51 escapes are just dummy blank strings).
52 52
53 53 - Linux: is meant to look good in a terminal like the Linux console (black
54 54 or very dark background).
55 55
56 56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 57 in light background terminals.
58 58
59 59 You can implement other color schemes easily, the syntax is fairly
60 60 self-explanatory. Please send back new schemes you develop to the author for
61 61 possible inclusion in future releases.
62 62 """
63 63
64 64 #*****************************************************************************
65 65 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 66 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
67 67 #
68 68 # Distributed under the terms of the BSD License. The full license is in
69 69 # the file COPYING, distributed as part of this software.
70 70 #*****************************************************************************
71 71
72 72 from __future__ import with_statement
73 73
74 74 import inspect
75 75 import keyword
76 76 import linecache
77 77 import os
78 78 import pydoc
79 79 import re
80 80 import string
81 81 import sys
82 82 import time
83 83 import tokenize
84 84 import traceback
85 85 import types
86 86
87 87 # For purposes of monkeypatching inspect to fix a bug in it.
88 88 from inspect import getsourcefile, getfile, getmodule,\
89 89 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
90 90
91 91 # IPython's own modules
92 92 # Modified pdb which doesn't damage IPython's readline handling
93 93 from IPython.core import debugger, ipapi
94 94 from IPython.core.display_trap import DisplayTrap
95 95 from IPython.core.excolors import exception_colors
96 96 from IPython.utils import PyColorize
97 97 from IPython.utils import io
98 98 from IPython.utils.data import uniq_stable
99 99 from IPython.utils.warn import info, error
100 100
101 101 # Globals
102 102 # amount of space to put line numbers before verbose tracebacks
103 103 INDENT_SIZE = 8
104 104
105 105 # Default color scheme. This is used, for example, by the traceback
106 106 # formatter. When running in an actual IPython instance, the user's rc.colors
107 107 # value is used, but havinga module global makes this functionality available
108 108 # to users of ultratb who are NOT running inside ipython.
109 109 DEFAULT_SCHEME = 'NoColor'
110 110
111 111 #---------------------------------------------------------------------------
112 112 # Code begins
113 113
114 114 # Utility functions
115 115 def inspect_error():
116 116 """Print a message about internal inspect errors.
117 117
118 118 These are unfortunately quite common."""
119 119
120 120 error('Internal Python error in the inspect module.\n'
121 121 'Below is the traceback from this internal error.\n')
122 122
123 123
124 124 def findsource(object):
125 125 """Return the entire source file and starting line number for an object.
126 126
127 127 The argument may be a module, class, method, function, traceback, frame,
128 128 or code object. The source code is returned as a list of all the lines
129 129 in the file and the line number indexes a line in that list. An IOError
130 130 is raised if the source code cannot be retrieved.
131 131
132 132 FIXED version with which we monkeypatch the stdlib to work around a bug."""
133 133
134 134 file = getsourcefile(object) or getfile(object)
135 135 # If the object is a frame, then trying to get the globals dict from its
136 136 # module won't work. Instead, the frame object itself has the globals
137 137 # dictionary.
138 138 globals_dict = None
139 139 if inspect.isframe(object):
140 140 # XXX: can this ever be false?
141 141 globals_dict = object.f_globals
142 142 else:
143 143 module = getmodule(object, file)
144 144 if module:
145 145 globals_dict = module.__dict__
146 146 lines = linecache.getlines(file, globals_dict)
147 147 if not lines:
148 148 raise IOError('could not get source code')
149 149
150 150 if ismodule(object):
151 151 return lines, 0
152 152
153 153 if isclass(object):
154 154 name = object.__name__
155 155 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
156 156 # make some effort to find the best matching class definition:
157 157 # use the one with the least indentation, which is the one
158 158 # that's most probably not inside a function definition.
159 159 candidates = []
160 160 for i in range(len(lines)):
161 161 match = pat.match(lines[i])
162 162 if match:
163 163 # if it's at toplevel, it's already the best one
164 164 if lines[i][0] == 'c':
165 165 return lines, i
166 166 # else add whitespace to candidate list
167 167 candidates.append((match.group(1), i))
168 168 if candidates:
169 169 # this will sort by whitespace, and by line number,
170 170 # less whitespace first
171 171 candidates.sort()
172 172 return lines, candidates[0][1]
173 173 else:
174 174 raise IOError('could not find class definition')
175 175
176 176 if ismethod(object):
177 177 object = object.im_func
178 178 if isfunction(object):
179 179 object = object.func_code
180 180 if istraceback(object):
181 181 object = object.tb_frame
182 182 if isframe(object):
183 183 object = object.f_code
184 184 if iscode(object):
185 185 if not hasattr(object, 'co_firstlineno'):
186 186 raise IOError('could not find function definition')
187 187 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
188 188 pmatch = pat.match
189 189 # fperez - fix: sometimes, co_firstlineno can give a number larger than
190 190 # the length of lines, which causes an error. Safeguard against that.
191 191 lnum = min(object.co_firstlineno,len(lines))-1
192 192 while lnum > 0:
193 193 if pmatch(lines[lnum]): break
194 194 lnum -= 1
195 195
196 196 return lines, lnum
197 197 raise IOError('could not find code object')
198 198
199 199 # Monkeypatch inspect to apply our bugfix. This code only works with py25
200 200 if sys.version_info[:2] >= (2,5):
201 201 inspect.findsource = findsource
202 202
203 203 def fix_frame_records_filenames(records):
204 204 """Try to fix the filenames in each record from inspect.getinnerframes().
205 205
206 206 Particularly, modules loaded from within zip files have useless filenames
207 207 attached to their code object, and inspect.getinnerframes() just uses it.
208 208 """
209 209 fixed_records = []
210 210 for frame, filename, line_no, func_name, lines, index in records:
211 211 # Look inside the frame's globals dictionary for __file__, which should
212 212 # be better.
213 213 better_fn = frame.f_globals.get('__file__', None)
214 214 if isinstance(better_fn, str):
215 215 # Check the type just in case someone did something weird with
216 216 # __file__. It might also be None if the error occurred during
217 217 # import.
218 218 filename = better_fn
219 219 fixed_records.append((frame, filename, line_no, func_name, lines, index))
220 220 return fixed_records
221 221
222 222
223 223 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
224 224 import linecache
225 225 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
226 226
227 227 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
228 228
229 229 # If the error is at the console, don't build any context, since it would
230 230 # otherwise produce 5 blank lines printed out (there is no file at the
231 231 # console)
232 232 rec_check = records[tb_offset:]
233 233 try:
234 234 rname = rec_check[0][1]
235 235 if rname == '<ipython console>' or rname.endswith('<string>'):
236 236 return rec_check
237 237 except IndexError:
238 238 pass
239 239
240 240 aux = traceback.extract_tb(etb)
241 241 assert len(records) == len(aux)
242 242 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
243 243 maybeStart = lnum-1 - context//2
244 244 start = max(maybeStart, 0)
245 245 end = start + context
246 246 lines = linecache.getlines(file)[start:end]
247 247 # pad with empty lines if necessary
248 248 if maybeStart < 0:
249 249 lines = (['\n'] * -maybeStart) + lines
250 250 if len(lines) < context:
251 251 lines += ['\n'] * (context - len(lines))
252 252 buf = list(records[i])
253 253 buf[LNUM_POS] = lnum
254 254 buf[INDEX_POS] = lnum - 1 - start
255 255 buf[LINES_POS] = lines
256 256 records[i] = tuple(buf)
257 257 return records[tb_offset:]
258 258
259 259 # Helper function -- largely belongs to VerboseTB, but we need the same
260 260 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
261 261 # can be recognized properly by ipython.el's py-traceback-line-re
262 262 # (SyntaxErrors have to be treated specially because they have no traceback)
263 263
264 264 _parser = PyColorize.Parser()
265 265
266 266 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):
267 267 numbers_width = INDENT_SIZE - 1
268 268 res = []
269 269 i = lnum - index
270 270
271 271 # This lets us get fully syntax-highlighted tracebacks.
272 272 if scheme is None:
273 273 ipinst = ipapi.get()
274 274 if ipinst is not None:
275 275 scheme = ipinst.colors
276 276 else:
277 277 scheme = DEFAULT_SCHEME
278 278
279 279 _line_format = _parser.format2
280 280
281 281 for line in lines:
282 282 new_line, err = _line_format(line,'str',scheme)
283 283 if not err: line = new_line
284 284
285 285 if i == lnum:
286 286 # This is the line with the error
287 287 pad = numbers_width - len(str(i))
288 288 if pad >= 3:
289 289 marker = '-'*(pad-3) + '-> '
290 290 elif pad == 2:
291 291 marker = '> '
292 292 elif pad == 1:
293 293 marker = '>'
294 294 else:
295 295 marker = ''
296 296 num = marker + str(i)
297 297 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
298 298 Colors.line, line, Colors.Normal)
299 299 else:
300 300 num = '%*s' % (numbers_width,i)
301 301 line = '%s%s%s %s' %(Colors.lineno, num,
302 302 Colors.Normal, line)
303 303
304 304 res.append(line)
305 305 if lvals and i == lnum:
306 306 res.append(lvals + '\n')
307 307 i = i + 1
308 308 return res
309 309
310 310
311 311 #---------------------------------------------------------------------------
312 312 # Module classes
313 313 class TBTools(object):
314 314 """Basic tools used by all traceback printer classes."""
315 315
316 316 # This attribute us used in globalipapp.py to have stdout used for
317 317 # writting exceptions. This is needed so nose can trap them. This attribute
318 318 # should be None (the default, which will use IPython.utils.io.Term) or
319 319 # the string 'stdout' which will cause the override to sys.stdout.
320 320 out_stream = None
321 321
322 322 # Number of frames to skip when reporting tracebacks
323 323 tb_offset = 0
324 324
325 325 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
326 326 # Whether to call the interactive pdb debugger after printing
327 327 # tracebacks or not
328 328 self.call_pdb = call_pdb
329 329
330 330 # Create color table
331 331 self.color_scheme_table = exception_colors()
332 332
333 333 self.set_colors(color_scheme)
334 334 self.old_scheme = color_scheme # save initial value for toggles
335 335
336 336 if call_pdb:
337 337 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
338 338 else:
339 339 self.pdb = None
340 340
341 341 def set_colors(self,*args,**kw):
342 342 """Shorthand access to the color table scheme selector method."""
343 343
344 344 # Set own color table
345 345 self.color_scheme_table.set_active_scheme(*args,**kw)
346 346 # for convenience, set Colors to the active scheme
347 347 self.Colors = self.color_scheme_table.active_colors
348 348 # Also set colors of debugger
349 349 if hasattr(self,'pdb') and self.pdb is not None:
350 350 self.pdb.set_colors(*args,**kw)
351 351
352 352 def color_toggle(self):
353 353 """Toggle between the currently active color scheme and NoColor."""
354 354
355 355 if self.color_scheme_table.active_scheme_name == 'NoColor':
356 356 self.color_scheme_table.set_active_scheme(self.old_scheme)
357 357 self.Colors = self.color_scheme_table.active_colors
358 358 else:
359 359 self.old_scheme = self.color_scheme_table.active_scheme_name
360 360 self.color_scheme_table.set_active_scheme('NoColor')
361 361 self.Colors = self.color_scheme_table.active_colors
362 362
363 def stb2text(self, stb):
364 """Convert a structured traceback (a list) to a string."""
365 return '\n'.join(stb)
366
363 367 def text(self, etype, value, tb, tb_offset=None, context=5):
364 368 """Return formatted traceback.
365 369
366 370 Subclasses may override this if they add extra arguments.
367 371 """
368 372 tb_list = self.structured_traceback(etype, value, tb,
369 373 tb_offset, context)
370 return '\n'.join(tb_list)
374 return self.stb2text(tb_list)
371 375
372 376 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
373 377 context=5, mode=None):
374 378 """Return a list of traceback frames.
375 379
376 380 Must be implemented by each class.
377 381 """
378 382 raise NotImplementedError()
379 383
380 384
381 385 #---------------------------------------------------------------------------
382 386 class ListTB(TBTools):
383 387 """Print traceback information from a traceback list, with optional color.
384 388
385 389 Calling: requires 3 arguments:
386 390 (etype, evalue, elist)
387 391 as would be obtained by:
388 392 etype, evalue, tb = sys.exc_info()
389 393 if tb:
390 394 elist = traceback.extract_tb(tb)
391 395 else:
392 396 elist = None
393 397
394 398 It can thus be used by programs which need to process the traceback before
395 399 printing (such as console replacements based on the code module from the
396 400 standard library).
397 401
398 402 Because they are meant to be called without a full traceback (only a
399 403 list), instances of this class can't call the interactive pdb debugger."""
400 404
401 405 def __init__(self,color_scheme = 'NoColor'):
402 406 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
403 407
404 408 def __call__(self, etype, value, elist):
405 409 io.Term.cout.flush()
406 410 io.Term.cerr.write(self.text(etype, value, elist))
407 411 io.Term.cerr.write('\n')
408 412
409 413 def structured_traceback(self, etype, value, elist, tb_offset=None,
410 414 context=5):
411 415 """Return a color formatted string with the traceback info.
412 416
413 417 Parameters
414 418 ----------
415 419 etype : exception type
416 420 Type of the exception raised.
417 421
418 422 value : object
419 423 Data stored in the exception
420 424
421 425 elist : list
422 426 List of frames, see class docstring for details.
423 427
424 428 tb_offset : int, optional
425 429 Number of frames in the traceback to skip. If not given, the
426 430 instance value is used (set in constructor).
427 431
428 432 context : int, optional
429 433 Number of lines of context information to print.
430 434
431 435 Returns
432 436 -------
433 437 String with formatted exception.
434 438 """
435 439 tb_offset = self.tb_offset if tb_offset is None else tb_offset
436 440 Colors = self.Colors
437 441 out_list = []
438 442 if elist:
439 443
440 444 if tb_offset and len(elist) > tb_offset:
441 445 elist = elist[tb_offset:]
442 446
443 447 out_list.append('Traceback %s(most recent call last)%s:' %
444 448 (Colors.normalEm, Colors.Normal) + '\n')
445 449 out_list.extend(self._format_list(elist))
446 450 # The exception info should be a single entry in the list.
447 451 lines = ''.join(self._format_exception_only(etype, value))
448 452 out_list.append(lines)
449 453
450 454 # Note: this code originally read:
451 455
452 456 ## for line in lines[:-1]:
453 457 ## out_list.append(" "+line)
454 458 ## out_list.append(lines[-1])
455 459
456 460 # This means it was indenting everything but the last line by a little
457 461 # bit. I've disabled this for now, but if we see ugliness somewhre we
458 462 # can restore it.
459 463
460 464 return out_list
461 465
462 466 def _format_list(self, extracted_list):
463 467 """Format a list of traceback entry tuples for printing.
464 468
465 469 Given a list of tuples as returned by extract_tb() or
466 470 extract_stack(), return a list of strings ready for printing.
467 471 Each string in the resulting list corresponds to the item with the
468 472 same index in the argument list. Each string ends in a newline;
469 473 the strings may contain internal newlines as well, for those items
470 474 whose source text line is not None.
471 475
472 476 Lifted almost verbatim from traceback.py
473 477 """
474 478
475 479 Colors = self.Colors
476 480 list = []
477 481 for filename, lineno, name, line in extracted_list[:-1]:
478 482 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
479 483 (Colors.filename, filename, Colors.Normal,
480 484 Colors.lineno, lineno, Colors.Normal,
481 485 Colors.name, name, Colors.Normal)
482 486 if line:
483 487 item = item + ' %s\n' % line.strip()
484 488 list.append(item)
485 489 # Emphasize the last entry
486 490 filename, lineno, name, line = extracted_list[-1]
487 491 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
488 492 (Colors.normalEm,
489 493 Colors.filenameEm, filename, Colors.normalEm,
490 494 Colors.linenoEm, lineno, Colors.normalEm,
491 495 Colors.nameEm, name, Colors.normalEm,
492 496 Colors.Normal)
493 497 if line:
494 498 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
495 499 Colors.Normal)
496 500 list.append(item)
497 501 #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
498 502 return list
499 503
500 504 def _format_exception_only(self, etype, value):
501 505 """Format the exception part of a traceback.
502 506
503 507 The arguments are the exception type and value such as given by
504 508 sys.exc_info()[:2]. The return value is a list of strings, each ending
505 509 in a newline. Normally, the list contains a single string; however,
506 510 for SyntaxError exceptions, it contains several lines that (when
507 511 printed) display detailed information about where the syntax error
508 512 occurred. The message indicating which exception occurred is the
509 513 always last string in the list.
510 514
511 515 Also lifted nearly verbatim from traceback.py
512 516 """
513 517
514 518 have_filedata = False
515 519 Colors = self.Colors
516 520 list = []
517 521 try:
518 522 stype = Colors.excName + etype.__name__ + Colors.Normal
519 523 except AttributeError:
520 524 stype = etype # String exceptions don't get special coloring
521 525 if value is None:
522 526 list.append( str(stype) + '\n')
523 527 else:
524 528 if etype is SyntaxError:
525 529 try:
526 530 msg, (filename, lineno, offset, line) = value
527 531 except:
528 532 have_filedata = False
529 533 else:
530 534 have_filedata = True
531 535 #print 'filename is',filename # dbg
532 536 if not filename: filename = "<string>"
533 537 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
534 538 (Colors.normalEm,
535 539 Colors.filenameEm, filename, Colors.normalEm,
536 540 Colors.linenoEm, lineno, Colors.Normal ))
537 541 if line is not None:
538 542 i = 0
539 543 while i < len(line) and line[i].isspace():
540 544 i = i+1
541 545 list.append('%s %s%s\n' % (Colors.line,
542 546 line.strip(),
543 547 Colors.Normal))
544 548 if offset is not None:
545 549 s = ' '
546 550 for c in line[i:offset-1]:
547 551 if c.isspace():
548 552 s = s + c
549 553 else:
550 554 s = s + ' '
551 555 list.append('%s%s^%s\n' % (Colors.caret, s,
552 556 Colors.Normal) )
553 557 value = msg
554 558 s = self._some_str(value)
555 559 if s:
556 560 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
557 561 Colors.Normal, s))
558 562 else:
559 563 list.append('%s\n' % str(stype))
560 564
561 565 # sync with user hooks
562 566 if have_filedata:
563 567 ipinst = ipapi.get()
564 568 if ipinst is not None:
565 569 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
566 570
567 571 return list
568 572
569 573 def get_exception_only(self, etype, value):
570 574 """Only print the exception type and message, without a traceback.
571 575
572 576 Parameters
573 577 ----------
574 578 etype : exception type
575 579 value : exception value
576 580 """
577 581 return ListTB.structured_traceback(self, etype, value, [])
578 582
579 583
580 584 def show_exception_only(self, etype, value):
581 585 """Only print the exception type and message, without a traceback.
582 586
583 587 Parameters
584 588 ----------
585 589 etype : exception type
586 590 value : exception value
587 591 """
588 592 # This method needs to use __call__ from *this* class, not the one from
589 593 # a subclass whose signature or behavior may be different
590 594 if self.out_stream == 'stdout':
591 595 ostream = sys.stdout
592 596 else:
593 597 ostream = io.Term.cerr
594 598 ostream.flush()
595 599 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
596 600 ostream.flush()
597 601
598 602 def _some_str(self, value):
599 603 # Lifted from traceback.py
600 604 try:
601 605 return str(value)
602 606 except:
603 607 return '<unprintable %s object>' % type(value).__name__
604 608
605 609 #----------------------------------------------------------------------------
606 610 class VerboseTB(TBTools):
607 611 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
608 612 of HTML. Requires inspect and pydoc. Crazy, man.
609 613
610 614 Modified version which optionally strips the topmost entries from the
611 615 traceback, to be used with alternate interpreters (because their own code
612 616 would appear in the traceback)."""
613 617
614 618 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
615 619 call_pdb = 0, include_vars=1):
616 620 """Specify traceback offset, headers and color scheme.
617 621
618 622 Define how many frames to drop from the tracebacks. Calling it with
619 623 tb_offset=1 allows use of this handler in interpreters which will have
620 624 their own code at the top of the traceback (VerboseTB will first
621 625 remove that frame before printing the traceback info)."""
622 626 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
623 627 self.tb_offset = tb_offset
624 628 self.long_header = long_header
625 629 self.include_vars = include_vars
626 630
627 631 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
628 632 context=5):
629 633 """Return a nice text document describing the traceback."""
630 634
631 635 tb_offset = self.tb_offset if tb_offset is None else tb_offset
632 636
633 637 # some locals
634 638 try:
635 639 etype = etype.__name__
636 640 except AttributeError:
637 641 pass
638 642 Colors = self.Colors # just a shorthand + quicker name lookup
639 643 ColorsNormal = Colors.Normal # used a lot
640 644 col_scheme = self.color_scheme_table.active_scheme_name
641 645 indent = ' '*INDENT_SIZE
642 646 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
643 647 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
644 648 exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal)
645 649
646 650 # some internal-use functions
647 651 def text_repr(value):
648 652 """Hopefully pretty robust repr equivalent."""
649 653 # this is pretty horrible but should always return *something*
650 654 try:
651 655 return pydoc.text.repr(value)
652 656 except KeyboardInterrupt:
653 657 raise
654 658 except:
655 659 try:
656 660 return repr(value)
657 661 except KeyboardInterrupt:
658 662 raise
659 663 except:
660 664 try:
661 665 # all still in an except block so we catch
662 666 # getattr raising
663 667 name = getattr(value, '__name__', None)
664 668 if name:
665 669 # ick, recursion
666 670 return text_repr(name)
667 671 klass = getattr(value, '__class__', None)
668 672 if klass:
669 673 return '%s instance' % text_repr(klass)
670 674 except KeyboardInterrupt:
671 675 raise
672 676 except:
673 677 return 'UNRECOVERABLE REPR FAILURE'
674 678 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
675 679 def nullrepr(value, repr=text_repr): return ''
676 680
677 681 # meat of the code begins
678 682 try:
679 683 etype = etype.__name__
680 684 except AttributeError:
681 685 pass
682 686
683 687 if self.long_header:
684 688 # Header with the exception type, python version, and date
685 689 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
686 690 date = time.ctime(time.time())
687 691
688 692 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
689 693 exc, ' '*(75-len(str(etype))-len(pyver)),
690 694 pyver, string.rjust(date, 75) )
691 695 head += "\nA problem occured executing Python code. Here is the sequence of function"\
692 696 "\ncalls leading up to the error, with the most recent (innermost) call last."
693 697 else:
694 698 # Simplified header
695 699 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
696 700 string.rjust('Traceback (most recent call last)',
697 701 75 - len(str(etype)) ) )
698 702 frames = []
699 703 # Flush cache before calling inspect. This helps alleviate some of the
700 704 # problems with python 2.3's inspect.py.
701 705 linecache.checkcache()
702 706 # Drop topmost frames if requested
703 707 try:
704 708 # Try the default getinnerframes and Alex's: Alex's fixes some
705 709 # problems, but it generates empty tracebacks for console errors
706 710 # (5 blanks lines) where none should be returned.
707 711 #records = inspect.getinnerframes(etb, context)[tb_offset:]
708 712 #print 'python records:', records # dbg
709 713 records = _fixed_getinnerframes(etb, context, tb_offset)
710 714 #print 'alex records:', records # dbg
711 715 except:
712 716
713 717 # FIXME: I've been getting many crash reports from python 2.3
714 718 # users, traceable to inspect.py. If I can find a small test-case
715 719 # to reproduce this, I should either write a better workaround or
716 720 # file a bug report against inspect (if that's the real problem).
717 721 # So far, I haven't been able to find an isolated example to
718 722 # reproduce the problem.
719 723 inspect_error()
720 724 traceback.print_exc(file=io.Term.cerr)
721 725 info('\nUnfortunately, your original traceback can not be constructed.\n')
722 726 return ''
723 727
724 728 # build some color string templates outside these nested loops
725 729 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
726 730 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
727 731 ColorsNormal)
728 732 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
729 733 (Colors.vName, Colors.valEm, ColorsNormal)
730 734 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
731 735 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
732 736 Colors.vName, ColorsNormal)
733 737 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
734 738 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
735 739 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
736 740 ColorsNormal)
737 741
738 742 # now, loop over all records printing context and info
739 743 abspath = os.path.abspath
740 744 for frame, file, lnum, func, lines, index in records:
741 745 #print '*** record:',file,lnum,func,lines,index # dbg
742 746 try:
743 747 file = file and abspath(file) or '?'
744 748 except OSError:
745 749 # if file is '<console>' or something not in the filesystem,
746 750 # the abspath call will throw an OSError. Just ignore it and
747 751 # keep the original file string.
748 752 pass
749 753 link = tpl_link % file
750 754 try:
751 755 args, varargs, varkw, locals = inspect.getargvalues(frame)
752 756 except:
753 757 # This can happen due to a bug in python2.3. We should be
754 758 # able to remove this try/except when 2.4 becomes a
755 759 # requirement. Bug details at http://python.org/sf/1005466
756 760 inspect_error()
757 761 traceback.print_exc(file=io.Term.cerr)
758 762 info("\nIPython's exception reporting continues...\n")
759 763
760 764 if func == '?':
761 765 call = ''
762 766 else:
763 767 # Decide whether to include variable details or not
764 768 var_repr = self.include_vars and eqrepr or nullrepr
765 769 try:
766 770 call = tpl_call % (func,inspect.formatargvalues(args,
767 771 varargs, varkw,
768 772 locals,formatvalue=var_repr))
769 773 except KeyError:
770 774 # Very odd crash from inspect.formatargvalues(). The
771 775 # scenario under which it appeared was a call to
772 776 # view(array,scale) in NumTut.view.view(), where scale had
773 777 # been defined as a scalar (it should be a tuple). Somehow
774 778 # inspect messes up resolving the argument list of view()
775 779 # and barfs out. At some point I should dig into this one
776 780 # and file a bug report about it.
777 781 inspect_error()
778 782 traceback.print_exc(file=io.Term.cerr)
779 783 info("\nIPython's exception reporting continues...\n")
780 784 call = tpl_call_fail % func
781 785
782 786 # Initialize a list of names on the current line, which the
783 787 # tokenizer below will populate.
784 788 names = []
785 789
786 790 def tokeneater(token_type, token, start, end, line):
787 791 """Stateful tokeneater which builds dotted names.
788 792
789 793 The list of names it appends to (from the enclosing scope) can
790 794 contain repeated composite names. This is unavoidable, since
791 795 there is no way to disambguate partial dotted structures until
792 796 the full list is known. The caller is responsible for pruning
793 797 the final list of duplicates before using it."""
794 798
795 799 # build composite names
796 800 if token == '.':
797 801 try:
798 802 names[-1] += '.'
799 803 # store state so the next token is added for x.y.z names
800 804 tokeneater.name_cont = True
801 805 return
802 806 except IndexError:
803 807 pass
804 808 if token_type == tokenize.NAME and token not in keyword.kwlist:
805 809 if tokeneater.name_cont:
806 810 # Dotted names
807 811 names[-1] += token
808 812 tokeneater.name_cont = False
809 813 else:
810 814 # Regular new names. We append everything, the caller
811 815 # will be responsible for pruning the list later. It's
812 816 # very tricky to try to prune as we go, b/c composite
813 817 # names can fool us. The pruning at the end is easy
814 818 # to do (or the caller can print a list with repeated
815 819 # names if so desired.
816 820 names.append(token)
817 821 elif token_type == tokenize.NEWLINE:
818 822 raise IndexError
819 823 # we need to store a bit of state in the tokenizer to build
820 824 # dotted names
821 825 tokeneater.name_cont = False
822 826
823 827 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
824 828 line = getline(file, lnum[0])
825 829 lnum[0] += 1
826 830 return line
827 831
828 832 # Build the list of names on this line of code where the exception
829 833 # occurred.
830 834 try:
831 835 # This builds the names list in-place by capturing it from the
832 836 # enclosing scope.
833 837 tokenize.tokenize(linereader, tokeneater)
834 838 except IndexError:
835 839 # signals exit of tokenizer
836 840 pass
837 841 except tokenize.TokenError,msg:
838 842 _m = ("An unexpected error occurred while tokenizing input\n"
839 843 "The following traceback may be corrupted or invalid\n"
840 844 "The error message is: %s\n" % msg)
841 845 error(_m)
842 846
843 847 # prune names list of duplicates, but keep the right order
844 848 unique_names = uniq_stable(names)
845 849
846 850 # Start loop over vars
847 851 lvals = []
848 852 if self.include_vars:
849 853 for name_full in unique_names:
850 854 name_base = name_full.split('.',1)[0]
851 855 if name_base in frame.f_code.co_varnames:
852 856 if locals.has_key(name_base):
853 857 try:
854 858 value = repr(eval(name_full,locals))
855 859 except:
856 860 value = undefined
857 861 else:
858 862 value = undefined
859 863 name = tpl_local_var % name_full
860 864 else:
861 865 if frame.f_globals.has_key(name_base):
862 866 try:
863 867 value = repr(eval(name_full,frame.f_globals))
864 868 except:
865 869 value = undefined
866 870 else:
867 871 value = undefined
868 872 name = tpl_global_var % name_full
869 873 lvals.append(tpl_name_val % (name,value))
870 874 if lvals:
871 875 lvals = '%s%s' % (indent,em_normal.join(lvals))
872 876 else:
873 877 lvals = ''
874 878
875 879 level = '%s %s\n' % (link,call)
876 880
877 881 if index is None:
878 882 frames.append(level)
879 883 else:
880 884 frames.append('%s%s' % (level,''.join(
881 885 _format_traceback_lines(lnum,index,lines,Colors,lvals,
882 886 col_scheme))))
883 887
884 888 # Get (safely) a string form of the exception info
885 889 try:
886 890 etype_str,evalue_str = map(str,(etype,evalue))
887 891 except:
888 892 # User exception is improperly defined.
889 893 etype,evalue = str,sys.exc_info()[:2]
890 894 etype_str,evalue_str = map(str,(etype,evalue))
891 895 # ... and format it
892 896 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
893 897 ColorsNormal, evalue_str)]
894 898 if type(evalue) is types.InstanceType:
895 899 try:
896 900 names = [w for w in dir(evalue) if isinstance(w, basestring)]
897 901 except:
898 902 # Every now and then, an object with funny inernals blows up
899 903 # when dir() is called on it. We do the best we can to report
900 904 # the problem and continue
901 905 _m = '%sException reporting error (object with broken dir())%s:'
902 906 exception.append(_m % (Colors.excName,ColorsNormal))
903 907 etype_str,evalue_str = map(str,sys.exc_info()[:2])
904 908 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
905 909 ColorsNormal, evalue_str))
906 910 names = []
907 911 for name in names:
908 912 value = text_repr(getattr(evalue, name))
909 913 exception.append('\n%s%s = %s' % (indent, name, value))
910 914
911 915 # vds: >>
912 916 if records:
913 917 filepath, lnum = records[-1][1:3]
914 918 #print "file:", str(file), "linenb", str(lnum) # dbg
915 919 filepath = os.path.abspath(filepath)
916 920 ipinst = ipapi.get()
917 921 if ipinst is not None:
918 922 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
919 923 # vds: <<
920 924
921 925 # return all our info assembled as a single string
922 926 # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
923 927 return [head] + frames + [''.join(exception[0])]
924 928
925 929 def debugger(self,force=False):
926 930 """Call up the pdb debugger if desired, always clean up the tb
927 931 reference.
928 932
929 933 Keywords:
930 934
931 935 - force(False): by default, this routine checks the instance call_pdb
932 936 flag and does not actually invoke the debugger if the flag is false.
933 937 The 'force' option forces the debugger to activate even if the flag
934 938 is false.
935 939
936 940 If the call_pdb flag is set, the pdb interactive debugger is
937 941 invoked. In all cases, the self.tb reference to the current traceback
938 942 is deleted to prevent lingering references which hamper memory
939 943 management.
940 944
941 945 Note that each call to pdb() does an 'import readline', so if your app
942 946 requires a special setup for the readline completers, you'll have to
943 947 fix that by hand after invoking the exception handler."""
944 948
945 949 if force or self.call_pdb:
946 950 if self.pdb is None:
947 951 self.pdb = debugger.Pdb(
948 952 self.color_scheme_table.active_scheme_name)
949 953 # the system displayhook may have changed, restore the original
950 954 # for pdb
951 955 display_trap = DisplayTrap(hook=sys.__displayhook__)
952 956 with display_trap:
953 957 self.pdb.reset()
954 958 # Find the right frame so we don't pop up inside ipython itself
955 959 if hasattr(self,'tb') and self.tb is not None:
956 960 etb = self.tb
957 961 else:
958 962 etb = self.tb = sys.last_traceback
959 963 while self.tb is not None and self.tb.tb_next is not None:
960 964 self.tb = self.tb.tb_next
961 965 if etb and etb.tb_next:
962 966 etb = etb.tb_next
963 967 self.pdb.botframe = etb.tb_frame
964 968 self.pdb.interaction(self.tb.tb_frame, self.tb)
965 969
966 970 if hasattr(self,'tb'):
967 971 del self.tb
968 972
969 973 def handler(self, info=None):
970 974 (etype, evalue, etb) = info or sys.exc_info()
971 975 self.tb = etb
972 976 io.Term.cout.flush()
973 977 io.Term.cerr.write(self.text(etype, evalue, etb))
974 978 io.Term.cerr.write('\n')
975 979
976 980 # Changed so an instance can just be called as VerboseTB_inst() and print
977 981 # out the right info on its own.
978 982 def __call__(self, etype=None, evalue=None, etb=None):
979 983 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
980 984 if etb is None:
981 985 self.handler()
982 986 else:
983 987 self.handler((etype, evalue, etb))
984 988 try:
985 989 self.debugger()
986 990 except KeyboardInterrupt:
987 991 print "\nKeyboardInterrupt"
988 992
989 993 #----------------------------------------------------------------------------
990 994 class FormattedTB(VerboseTB, ListTB):
991 995 """Subclass ListTB but allow calling with a traceback.
992 996
993 997 It can thus be used as a sys.excepthook for Python > 2.1.
994 998
995 999 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
996 1000
997 1001 Allows a tb_offset to be specified. This is useful for situations where
998 1002 one needs to remove a number of topmost frames from the traceback (such as
999 1003 occurs with python programs that themselves execute other python code,
1000 1004 like Python shells). """
1001 1005
1002 1006 def __init__(self, mode='Plain', color_scheme='Linux',
1003 1007 tb_offset=0, long_header=0, call_pdb=0, include_vars=0):
1004 1008
1005 1009 # NEVER change the order of this list. Put new modes at the end:
1006 1010 self.valid_modes = ['Plain','Context','Verbose']
1007 1011 self.verbose_modes = self.valid_modes[1:3]
1008 1012
1009 1013 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
1010 1014 call_pdb=call_pdb,include_vars=include_vars)
1015
1016 # Different types of tracebacks are joined with different separators to
1017 # form a single string. They are taken from this dict
1018 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1019 # set_mode also sets the tb_join_char attribute
1011 1020 self.set_mode(mode)
1012 1021
1013 1022 def _extract_tb(self,tb):
1014 1023 if tb:
1015 1024 return traceback.extract_tb(tb)
1016 1025 else:
1017 1026 return None
1018 1027
1019 def structured_traceback(self, etype, value, tb, tb_offset=None,
1020 context=5, mode=None):
1028 def structured_traceback(self, etype, value, tb, tb_offset=None, context=5):
1021 1029 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1022 mode = self.mode if mode is None else mode
1030 mode = self.mode
1023 1031 if mode in self.verbose_modes:
1024 1032 # Verbose modes need a full traceback
1025 1033 return VerboseTB.structured_traceback(
1026 1034 self, etype, value, tb, tb_offset, context
1027 1035 )
1028 1036 else:
1029 1037 # We must check the source cache because otherwise we can print
1030 1038 # out-of-date source code.
1031 1039 linecache.checkcache()
1032 1040 # Now we can extract and format the exception
1033 1041 elist = self._extract_tb(tb)
1034 1042 return ListTB.structured_traceback(
1035 1043 self, etype, value, elist, tb_offset, context
1036 1044 )
1037 1045
1038 def text(self, etype, value, tb, tb_offset=None, context=5, mode=None):
1039 """Return formatted traceback.
1040
1041 If the optional mode parameter is given, it overrides the current
1042 mode."""
1043
1044 mode = self.mode if mode is None else mode
1045 tb_list = self.structured_traceback(etype, value, tb, tb_offset,
1046 context, mode)
1047 return '\n'.join(tb_list)
1046 def stb2text(self, stb):
1047 """Convert a structured traceback (a list) to a string."""
1048 return self.tb_join_char.join(stb)
1048 1049
1049 1050
1050 1051 def set_mode(self,mode=None):
1051 1052 """Switch to the desired mode.
1052 1053
1053 1054 If mode is not specified, cycles through the available modes."""
1054 1055
1055 1056 if not mode:
1056 1057 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
1057 1058 len(self.valid_modes)
1058 1059 self.mode = self.valid_modes[new_idx]
1059 1060 elif mode not in self.valid_modes:
1060 1061 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
1061 1062 'Valid modes: '+str(self.valid_modes)
1062 1063 else:
1063 1064 self.mode = mode
1064 1065 # include variable details only in 'Verbose' mode
1065 1066 self.include_vars = (self.mode == self.valid_modes[2])
1067 # Set the join character for generating text tracebacks
1068 self.tb_join_char = self._join_chars[mode]
1066 1069
1067 1070 # some convenient shorcuts
1068 1071 def plain(self):
1069 1072 self.set_mode(self.valid_modes[0])
1070 1073
1071 1074 def context(self):
1072 1075 self.set_mode(self.valid_modes[1])
1073 1076
1074 1077 def verbose(self):
1075 1078 self.set_mode(self.valid_modes[2])
1076 1079
1077 1080 #----------------------------------------------------------------------------
1078 1081 class AutoFormattedTB(FormattedTB):
1079 1082 """A traceback printer which can be called on the fly.
1080 1083
1081 1084 It will find out about exceptions by itself.
1082 1085
1083 1086 A brief example:
1084 1087
1085 1088 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1086 1089 try:
1087 1090 ...
1088 1091 except:
1089 1092 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1090 1093 """
1091 1094
1092 1095 def __call__(self,etype=None,evalue=None,etb=None,
1093 1096 out=None,tb_offset=None):
1094 1097 """Print out a formatted exception traceback.
1095 1098
1096 1099 Optional arguments:
1097 1100 - out: an open file-like object to direct output to.
1098 1101
1099 1102 - tb_offset: the number of frames to skip over in the stack, on a
1100 1103 per-call basis (this overrides temporarily the instance's tb_offset
1101 1104 given at initialization time. """
1102 1105
1103 1106 if out is None:
1104 1107 if self.out_stream == 'stdout':
1105 1108 out = sys.stdout
1106 1109 else:
1107 1110 out = io.Term.cerr
1108 1111 out.flush()
1109 1112 out.write(self.text(etype, evalue, etb, tb_offset))
1110 1113 out.write('\n')
1111 1114 out.flush()
1112 1115 # FIXME: we should remove the auto pdb behavior from here and leave
1113 1116 # that to the clients.
1114 1117 try:
1115 1118 self.debugger()
1116 1119 except KeyboardInterrupt:
1117 1120 print "\nKeyboardInterrupt"
1118 1121
1119 1122 def structured_traceback(self, etype=None, value=None, tb=None,
1120 tb_offset=None, context=5, mode=None):
1123 tb_offset=None, context=5):
1121 1124 if etype is None:
1122 1125 etype,value,tb = sys.exc_info()
1123 1126 self.tb = tb
1124 1127 return FormattedTB.structured_traceback(
1125 self, etype, value, tb, tb_offset, context, mode )
1128 self, etype, value, tb, tb_offset, context)
1126 1129
1127 1130 #---------------------------------------------------------------------------
1128 1131
1129 1132 # A simple class to preserve Nathan's original functionality.
1130 1133 class ColorTB(FormattedTB):
1131 1134 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1132 1135 def __init__(self,color_scheme='Linux',call_pdb=0):
1133 1136 FormattedTB.__init__(self,color_scheme=color_scheme,
1134 1137 call_pdb=call_pdb)
1135 1138
1136 1139
1137 1140 class SyntaxTB(ListTB):
1138 1141 """Extension which holds some state: the last exception value"""
1139 1142
1140 1143 def __init__(self,color_scheme = 'NoColor'):
1141 1144 ListTB.__init__(self,color_scheme)
1142 1145 self.last_syntax_error = None
1143 1146
1144 1147 def __call__(self, etype, value, elist):
1145 1148 self.last_syntax_error = value
1146 1149 ListTB.__call__(self,etype,value,elist)
1147 1150
1148 1151 def clear_err_state(self):
1149 1152 """Return the current error state and clear it"""
1150 1153 e = self.last_syntax_error
1151 1154 self.last_syntax_error = None
1152 1155 return e
1153 1156
1154 def text(self, etype, value, tb, tb_offset=None, context=5):
1155 """Return formatted traceback.
1157 def stb2text(self, stb):
1158 """Convert a structured traceback (a list) to a string."""
1159 return ''.join(stb)
1156 1160
1157 Subclasses may override this if they add extra arguments.
1158 """
1159 tb_list = self.structured_traceback(etype, value, tb,
1160 tb_offset, context)
1161 return ''.join(tb_list)
1162 1161
1163 1162 #----------------------------------------------------------------------------
1164 1163 # module testing (minimal)
1165 1164 if __name__ == "__main__":
1166 1165 def spam(c, (d, e)):
1167 1166 x = c + d
1168 1167 y = c * d
1169 1168 foo(x, y)
1170 1169
1171 1170 def foo(a, b, bar=1):
1172 1171 eggs(a, b + bar)
1173 1172
1174 1173 def eggs(f, g, z=globals()):
1175 1174 h = f + g
1176 1175 i = f - g
1177 1176 return h / i
1178 1177
1179 1178 print ''
1180 1179 print '*** Before ***'
1181 1180 try:
1182 1181 print spam(1, (2, 3))
1183 1182 except:
1184 1183 traceback.print_exc()
1185 1184 print ''
1186 1185
1187 1186 handler = ColorTB()
1188 1187 print '*** ColorTB ***'
1189 1188 try:
1190 1189 print spam(1, (2, 3))
1191 1190 except:
1192 1191 apply(handler, sys.exc_info() )
1193 1192 print ''
1194 1193
1195 1194 handler = VerboseTB()
1196 1195 print '*** VerboseTB ***'
1197 1196 try:
1198 1197 print spam(1, (2, 3))
1199 1198 except:
1200 1199 apply(handler, sys.exc_info() )
1201 1200 print ''
1202 1201
@@ -1,377 +1,384 b''
1 1 # Standard library imports
2 2 import signal
3 3 import sys
4 4
5 5 # System library imports
6 6 from pygments.lexers import PythonLexer
7 7 from PyQt4 import QtCore, QtGui
8 8 import zmq
9 9
10 10 # Local imports
11 11 from IPython.core.inputsplitter import InputSplitter
12 12 from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin
13 13 from call_tip_widget import CallTipWidget
14 14 from completion_lexer import CompletionLexer
15 15 from console_widget import HistoryConsoleWidget
16 16 from pygments_highlighter import PygmentsHighlighter
17 17
18 18
19 19 class FrontendHighlighter(PygmentsHighlighter):
20 20 """ A PygmentsHighlighter that can be turned on and off and that ignores
21 21 prompts.
22 22 """
23 23
24 24 def __init__(self, frontend):
25 25 super(FrontendHighlighter, self).__init__(frontend._control.document())
26 26 self._current_offset = 0
27 27 self._frontend = frontend
28 28 self.highlighting_on = False
29 29
30 30 def highlightBlock(self, qstring):
31 31 """ Highlight a block of text. Reimplemented to highlight selectively.
32 32 """
33 33 if not self.highlighting_on:
34 34 return
35 35
36 36 # The input to this function is unicode string that may contain
37 37 # paragraph break characters, non-breaking spaces, etc. Here we acquire
38 38 # the string as plain text so we can compare it.
39 39 current_block = self.currentBlock()
40 40 string = self._frontend._get_block_plain_text(current_block)
41 41
42 42 # Decide whether to check for the regular or continuation prompt.
43 43 if current_block.contains(self._frontend._prompt_pos):
44 44 prompt = self._frontend._prompt
45 45 else:
46 46 prompt = self._frontend._continuation_prompt
47 47
48 48 # Don't highlight the part of the string that contains the prompt.
49 49 if string.startswith(prompt):
50 50 self._current_offset = len(prompt)
51 51 qstring.remove(0, len(prompt))
52 52 else:
53 53 self._current_offset = 0
54 54
55 55 PygmentsHighlighter.highlightBlock(self, qstring)
56 56
57 57 def rehighlightBlock(self, block):
58 58 """ Reimplemented to temporarily enable highlighting if disabled.
59 59 """
60 60 old = self.highlighting_on
61 61 self.highlighting_on = True
62 62 super(FrontendHighlighter, self).rehighlightBlock(block)
63 63 self.highlighting_on = old
64 64
65 65 def setFormat(self, start, count, format):
66 66 """ Reimplemented to highlight selectively.
67 67 """
68 68 start += self._current_offset
69 69 PygmentsHighlighter.setFormat(self, start, count, format)
70 70
71 71
72 72 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
73 73 """ A Qt frontend for a generic Python kernel.
74 74 """
75 75
76 76 # Emitted when an 'execute_reply' has been received from the kernel and
77 77 # processed by the FrontendWidget.
78 78 executed = QtCore.pyqtSignal(object)
79 79
80 80 # Protected class attributes.
81 81 _highlighter_class = FrontendHighlighter
82 82 _input_splitter_class = InputSplitter
83 83
84 84 #---------------------------------------------------------------------------
85 85 # 'object' interface
86 86 #---------------------------------------------------------------------------
87 87
88 88 def __init__(self, *args, **kw):
89 89 super(FrontendWidget, self).__init__(*args, **kw)
90 90
91 91 # FrontendWidget protected variables.
92 92 self._call_tip_widget = CallTipWidget(self._control)
93 93 self._completion_lexer = CompletionLexer(PythonLexer())
94 94 self._hidden = False
95 95 self._highlighter = self._highlighter_class(self)
96 96 self._input_splitter = self._input_splitter_class(input_mode='replace')
97 97 self._kernel_manager = None
98 98
99 99 # Configure the ConsoleWidget.
100 100 self.tab_width = 4
101 101 self._set_continuation_prompt('... ')
102 102
103 103 # Connect signal handlers.
104 104 document = self._control.document()
105 105 document.contentsChange.connect(self._document_contents_change)
106 106
107 107 #---------------------------------------------------------------------------
108 108 # 'ConsoleWidget' abstract interface
109 109 #---------------------------------------------------------------------------
110 110
111 111 def _is_complete(self, source, interactive):
112 112 """ Returns whether 'source' can be completely processed and a new
113 113 prompt created. When triggered by an Enter/Return key press,
114 114 'interactive' is True; otherwise, it is False.
115 115 """
116 116 complete = self._input_splitter.push(source.expandtabs(4))
117 117 if interactive:
118 118 complete = not self._input_splitter.push_accepts_more()
119 119 return complete
120 120
121 121 def _execute(self, source, hidden):
122 122 """ Execute 'source'. If 'hidden', do not show any output.
123 123 """
124 124 self.kernel_manager.xreq_channel.execute(source)
125 125 self._hidden = hidden
126 126
127 127 def _execute_interrupt(self):
128 128 """ Attempts to stop execution. Returns whether this method has an
129 129 implementation.
130 130 """
131 131 self._interrupt_kernel()
132 132 return True
133 133
134 134 def _prompt_started_hook(self):
135 135 """ Called immediately after a new prompt is displayed.
136 136 """
137 137 if not self._reading:
138 138 self._highlighter.highlighting_on = True
139 139
140 140 def _prompt_finished_hook(self):
141 141 """ Called immediately after a prompt is finished, i.e. when some input
142 142 will be processed and a new prompt displayed.
143 143 """
144 144 if not self._reading:
145 145 self._highlighter.highlighting_on = False
146 146
147 147 def _tab_pressed(self):
148 148 """ Called when the tab key is pressed. Returns whether to continue
149 149 processing the event.
150 150 """
151 151 self._keep_cursor_in_buffer()
152 152 cursor = self._get_cursor()
153 153 return not self._complete()
154 154
155 155 #---------------------------------------------------------------------------
156 156 # 'ConsoleWidget' protected interface
157 157 #---------------------------------------------------------------------------
158 158
159 159 def _show_continuation_prompt(self):
160 160 """ Reimplemented for auto-indentation.
161 161 """
162 162 super(FrontendWidget, self)._show_continuation_prompt()
163 163 spaces = self._input_splitter.indent_spaces
164 164 self._append_plain_text('\t' * (spaces / self.tab_width))
165 165 self._append_plain_text(' ' * (spaces % self.tab_width))
166 166
167 167 #---------------------------------------------------------------------------
168 168 # 'BaseFrontendMixin' abstract interface
169 169 #---------------------------------------------------------------------------
170 170
171 171 def _handle_complete_reply(self, rep):
172 172 """ Handle replies for tab completion.
173 173 """
174 174 cursor = self._get_cursor()
175 175 if rep['parent_header']['msg_id'] == self._complete_id and \
176 176 cursor.position() == self._complete_pos:
177 177 text = '.'.join(self._get_context())
178 178 cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
179 179 self._complete_with_items(cursor, rep['content']['matches'])
180 180
181 181 def _handle_execute_reply(self, msg):
182 182 """ Handles replies for code execution.
183 183 """
184 184 if not self._hidden:
185 185 # Make sure that all output from the SUB channel has been processed
186 186 # before writing a new prompt.
187 187 self.kernel_manager.sub_channel.flush()
188 188
189 189 content = msg['content']
190 190 status = content['status']
191 191 if status == 'ok':
192 192 self._process_execute_ok(msg)
193 193 elif status == 'error':
194 194 self._process_execute_error(msg)
195 195 elif status == 'abort':
196 196 self._process_execute_abort(msg)
197 197
198 198 self._show_interpreter_prompt_for_reply(msg)
199 199 self.executed.emit(msg)
200 200
201 201 def _handle_input_request(self, msg):
202 202 """ Handle requests for raw_input.
203 203 """
204 204 if self._hidden:
205 205 raise RuntimeError('Request for raw input during hidden execution.')
206 206
207 207 # Make sure that all output from the SUB channel has been processed
208 208 # before entering readline mode.
209 209 self.kernel_manager.sub_channel.flush()
210 210
211 211 def callback(line):
212 212 self.kernel_manager.rep_channel.input(line)
213 213 self._readline(msg['content']['prompt'], callback=callback)
214 214
215 215 def _handle_object_info_reply(self, rep):
216 216 """ Handle replies for call tips.
217 217 """
218 218 cursor = self._get_cursor()
219 219 if rep['parent_header']['msg_id'] == self._call_tip_id and \
220 220 cursor.position() == self._call_tip_pos:
221 221 doc = rep['content']['docstring']
222 222 if doc:
223 223 self._call_tip_widget.show_docstring(doc)
224 224
225 225 def _handle_pyout(self, msg):
226 226 """ Handle display hook output.
227 227 """
228 228 if not self._hidden and self._is_from_this_session(msg):
229 229 self._append_plain_text(msg['content']['data'] + '\n')
230 230
231 231 def _handle_stream(self, msg):
232 232 """ Handle stdout, stderr, and stdin.
233 233 """
234 234 if not self._hidden and self._is_from_this_session(msg):
235 235 self._append_plain_text(msg['content']['data'])
236 236 self._control.moveCursor(QtGui.QTextCursor.End)
237 237
238 238 def _started_channels(self):
239 239 """ Called when the KernelManager channels have started listening or
240 240 when the frontend is assigned an already listening KernelManager.
241 241 """
242 242 self._reset()
243 243 self._append_plain_text(self._get_banner())
244 244 self._show_interpreter_prompt()
245 245
246 246 def _stopped_channels(self):
247 247 """ Called when the KernelManager channels have stopped listening or
248 248 when a listening KernelManager is removed from the frontend.
249 249 """
250 250 # FIXME: Print a message here?
251 251 pass
252 252
253 253 #---------------------------------------------------------------------------
254 254 # 'FrontendWidget' interface
255 255 #---------------------------------------------------------------------------
256 256
257 257 def execute_file(self, path, hidden=False):
258 258 """ Attempts to execute file with 'path'. If 'hidden', no output is
259 259 shown.
260 260 """
261 261 self.execute('execfile("%s")' % path, hidden=hidden)
262 262
263 263 #---------------------------------------------------------------------------
264 264 # 'FrontendWidget' protected interface
265 265 #---------------------------------------------------------------------------
266 266
267 267 def _call_tip(self):
268 268 """ Shows a call tip, if appropriate, at the current cursor location.
269 269 """
270 270 # Decide if it makes sense to show a call tip
271 271 cursor = self._get_cursor()
272 272 cursor.movePosition(QtGui.QTextCursor.Left)
273 273 document = self._control.document()
274 274 if document.characterAt(cursor.position()).toAscii() != '(':
275 275 return False
276 276 context = self._get_context(cursor)
277 277 if not context:
278 278 return False
279 279
280 280 # Send the metadata request to the kernel
281 281 name = '.'.join(context)
282 282 self._call_tip_id = self.kernel_manager.xreq_channel.object_info(name)
283 283 self._call_tip_pos = self._get_cursor().position()
284 284 return True
285 285
286 286 def _complete(self):
287 287 """ Performs completion at the current cursor location.
288 288 """
289 289 # Decide if it makes sense to do completion
290 290 context = self._get_context()
291 291 if not context:
292 292 return False
293 293
294 294 # Send the completion request to the kernel
295 295 text = '.'.join(context)
296
297 # FIXME - Evan: we need the position of the cursor in the current input
298 # buffer. I tried this line below but the numbers I get are bogus. -
299 # Not sure what to do. fperez.
300 cursor_pos = self._get_cursor().position()
301
296 302 self._complete_id = self.kernel_manager.xreq_channel.complete(
297 text, self._get_input_buffer_cursor_line(), self.input_buffer)
303 text, self._get_input_buffer_cursor_line(), cursor_pos,
304 self.input_buffer)
298 305 self._complete_pos = self._get_cursor().position()
299 306 return True
300 307
301 308 def _get_banner(self):
302 309 """ Gets a banner to display at the beginning of a session.
303 310 """
304 311 banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \
305 312 '"license" for more information.'
306 313 return banner % (sys.version, sys.platform)
307 314
308 315 def _get_context(self, cursor=None):
309 316 """ Gets the context at the current cursor location.
310 317 """
311 318 if cursor is None:
312 319 cursor = self._get_cursor()
313 320 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
314 321 QtGui.QTextCursor.KeepAnchor)
315 322 text = str(cursor.selection().toPlainText())
316 323 return self._completion_lexer.get_context(text)
317 324
318 325 def _interrupt_kernel(self):
319 326 """ Attempts to the interrupt the kernel.
320 327 """
321 328 if self.kernel_manager.has_kernel:
322 329 self.kernel_manager.signal_kernel(signal.SIGINT)
323 330 else:
324 331 self._append_plain_text('Kernel process is either remote or '
325 332 'unspecified. Cannot interrupt.\n')
326 333
327 334 def _process_execute_abort(self, msg):
328 335 """ Process a reply for an aborted execution request.
329 336 """
330 337 self._append_plain_text("ERROR: execution aborted\n")
331 338
332 339 def _process_execute_error(self, msg):
333 340 """ Process a reply for an execution request that resulted in an error.
334 341 """
335 342 content = msg['content']
336 343 traceback = ''.join(content['traceback'])
337 344 self._append_plain_text(traceback)
338 345
339 346 def _process_execute_ok(self, msg):
340 347 """ Process a reply for a successful execution equest.
341 348 """
342 349 payload = msg['content']['payload']
343 350 for item in payload:
344 351 if not self._process_execute_payload(item):
345 352 warning = 'Received unknown payload of type %s\n'
346 353 self._append_plain_text(warning % repr(item['source']))
347 354
348 355 def _process_execute_payload(self, item):
349 356 """ Process a single payload item from the list of payload items in an
350 357 execution reply. Returns whether the payload was handled.
351 358 """
352 359 # The basic FrontendWidget doesn't handle payloads, as they are a
353 360 # mechanism for going beyond the standard Python interpreter model.
354 361 return False
355 362
356 363 def _show_interpreter_prompt(self):
357 364 """ Shows a prompt for the interpreter.
358 365 """
359 366 self._show_prompt('>>> ')
360 367
361 368 def _show_interpreter_prompt_for_reply(self, msg):
362 369 """ Shows a prompt for the interpreter given an 'execute_reply' message.
363 370 """
364 371 self._show_interpreter_prompt()
365 372
366 373 #------ Signal handlers ----------------------------------------------------
367 374
368 375 def _document_contents_change(self, position, removed, added):
369 376 """ Called whenever the document's content changes. Display a call tip
370 377 if appropriate.
371 378 """
372 379 # Calculate where the cursor should be *after* the change:
373 380 position += added
374 381
375 382 document = self._control.document()
376 383 if position == self._get_cursor().position():
377 384 self._call_tip()
@@ -1,387 +1,398 b''
1 1 #!/usr/bin/env python
2 2 """A simple interactive kernel that talks to a frontend over 0MQ.
3 3
4 4 Things to do:
5 5
6 6 * Implement `set_parent` logic. Right before doing exec, the Kernel should
7 7 call set_parent on all the PUB objects with the message about to be executed.
8 8 * Implement random port and security key logic.
9 9 * Implement control messages.
10 10 * Implement event loop and poll version.
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 # Standard library imports.
18 18 import __builtin__
19 19 import sys
20 20 import time
21 21 import traceback
22 22
23 23 # System library imports.
24 24 import zmq
25 25
26 26 # Local imports.
27 27 from IPython.config.configurable import Configurable
28 28 from IPython.utils.traitlets import Instance
29 29 from completer import KernelCompleter
30 30 from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \
31 31 start_kernel
32 32 from iostream import OutStream
33 33 from session import Session, Message
34 34 from zmqshell import ZMQInteractiveShell
35 35
36 36 #-----------------------------------------------------------------------------
37 37 # Main kernel class
38 38 #-----------------------------------------------------------------------------
39 39
40 40 class Kernel(Configurable):
41 41
42 42 #---------------------------------------------------------------------------
43 43 # Kernel interface
44 44 #---------------------------------------------------------------------------
45 45
46 46 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
47 47 session = Instance(Session)
48 48 reply_socket = Instance('zmq.Socket')
49 49 pub_socket = Instance('zmq.Socket')
50 50 req_socket = Instance('zmq.Socket')
51 51
52 52 # Maps user-friendly backend names to matplotlib backend identifiers.
53 53 _pylab_map = { 'tk': 'TkAgg',
54 54 'gtk': 'GTKAgg',
55 55 'wx': 'WXAgg',
56 56 'qt': 'Qt4Agg', # qt3 not supported
57 57 'qt4': 'Qt4Agg',
58 58 'payload-svg' : \
59 59 'module://IPython.zmq.pylab.backend_payload_svg' }
60 60
61 61 def __init__(self, **kwargs):
62 62 super(Kernel, self).__init__(**kwargs)
63 63
64 64 # Initialize the InteractiveShell subclass
65 65 self.shell = ZMQInteractiveShell.instance()
66 66 self.shell.displayhook.session = self.session
67 67 self.shell.displayhook.pub_socket = self.pub_socket
68 68
69 69 # TMP - hack while developing
70 70 self.shell._reply_content = None
71 71
72 72 # Build dict of handlers for message types
73 73 msg_types = [ 'execute_request', 'complete_request',
74 74 'object_info_request', 'prompt_request',
75 75 'history_request' ]
76 76 self.handlers = {}
77 77 for msg_type in msg_types:
78 78 self.handlers[msg_type] = getattr(self, msg_type)
79 79
80 80 def activate_pylab(self, backend=None, import_all=True):
81 81 """ Activates pylab in this kernel's namespace.
82 82
83 83 Parameters:
84 84 -----------
85 85 backend : str, optional
86 86 A valid backend name.
87 87
88 88 import_all : bool, optional
89 89 If true, an 'import *' is done from numpy and pylab.
90 90 """
91 91 # FIXME: This is adapted from IPython.lib.pylabtools.pylab_activate.
92 92 # Common functionality should be refactored.
93 93
94 94 # We must set the desired backend before importing pylab.
95 95 import matplotlib
96 96 if backend:
97 97 backend_id = self._pylab_map[backend]
98 98 if backend_id.startswith('module://'):
99 99 # Work around bug in matplotlib: matplotlib.use converts the
100 100 # backend_id to lowercase even if a module name is specified!
101 101 matplotlib.rcParams['backend'] = backend_id
102 102 else:
103 103 matplotlib.use(backend_id)
104 104
105 105 # Import numpy as np/pyplot as plt are conventions we're trying to
106 106 # somewhat standardize on. Making them available to users by default
107 107 # will greatly help this.
108 108 exec ("import numpy\n"
109 109 "import matplotlib\n"
110 110 "from matplotlib import pylab, mlab, pyplot\n"
111 111 "np = numpy\n"
112 112 "plt = pyplot\n"
113 113 ) in self.shell.user_ns
114 114
115 115 if import_all:
116 116 exec("from matplotlib.pylab import *\n"
117 117 "from numpy import *\n") in self.shell.user_ns
118 118
119 119 matplotlib.interactive(True)
120 120
121 121 def start(self):
122 122 """ Start the kernel main loop.
123 123 """
124 124 while True:
125 125 ident = self.reply_socket.recv()
126 126 assert self.reply_socket.rcvmore(), "Missing message part."
127 127 msg = self.reply_socket.recv_json()
128 128 omsg = Message(msg)
129 129 print>>sys.__stdout__
130 130 print>>sys.__stdout__, omsg
131 131 handler = self.handlers.get(omsg.msg_type, None)
132 132 if handler is None:
133 133 print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg
134 134 else:
135 135 handler(ident, omsg)
136 136
137 137 #---------------------------------------------------------------------------
138 138 # Kernel request handlers
139 139 #---------------------------------------------------------------------------
140 140
141 141 def execute_request(self, ident, parent):
142 142 try:
143 143 code = parent[u'content'][u'code']
144 144 except:
145 145 print>>sys.__stderr__, "Got bad msg: "
146 146 print>>sys.__stderr__, Message(parent)
147 147 return
148 148 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
149 149 self.pub_socket.send_json(pyin_msg)
150 150
151 151 try:
152 152 # Replace raw_input. Note that is not sufficient to replace
153 153 # raw_input in the user namespace.
154 154 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
155 155 __builtin__.raw_input = raw_input
156 156
157 157 # Set the parent message of the display hook and out streams.
158 158 self.shell.displayhook.set_parent(parent)
159 159 sys.stdout.set_parent(parent)
160 160 sys.stderr.set_parent(parent)
161 161
162 162 # FIXME: runlines calls the exception handler itself. We should
163 163 # clean this up.
164 164 self.shell._reply_content = None
165 165 self.shell.runlines(code)
166 166 except:
167 167 # FIXME: this code right now isn't being used yet by default,
168 168 # because the runlines() call above directly fires off exception
169 169 # reporting. This code, therefore, is only active in the scenario
170 170 # where runlines itself has an unhandled exception. We need to
171 171 # uniformize this, for all exception construction to come from a
172 172 # single location in the codbase.
173 173 etype, evalue, tb = sys.exc_info()
174 174 tb_list = traceback.format_exception(etype, evalue, tb)
175 175 reply_content = self.shell._showtraceback(etype, evalue, tb_list)
176 176 else:
177 177 payload = self.shell.payload_manager.read_payload()
178 178 # Be agressive about clearing the payload because we don't want
179 179 # it to sit in memory until the next execute_request comes in.
180 180 self.shell.payload_manager.clear_payload()
181 181 reply_content = { 'status' : 'ok', 'payload' : payload }
182 182
183 183 # Compute the prompt information
184 184 prompt_number = self.shell.displayhook.prompt_count
185 185 reply_content['prompt_number'] = prompt_number
186 186 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
187 187 next_prompt = {'prompt_string' : prompt_string,
188 188 'prompt_number' : prompt_number+1,
189 189 'input_sep' : self.shell.displayhook.input_sep}
190 190 reply_content['next_prompt'] = next_prompt
191 191
192 192 # TMP - fish exception info out of shell, possibly left there by
193 193 # runlines
194 194 if self.shell._reply_content is not None:
195 195 reply_content.update(self.shell._reply_content)
196 196
197 197 # Flush output before sending the reply.
198 198 sys.stderr.flush()
199 199 sys.stdout.flush()
200 200
201 201 # Send the reply.
202 202 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
203 203 print>>sys.__stdout__, Message(reply_msg)
204 204 self.reply_socket.send(ident, zmq.SNDMORE)
205 205 self.reply_socket.send_json(reply_msg)
206 206 if reply_msg['content']['status'] == u'error':
207 207 self._abort_queue()
208 208
209 209 def complete_request(self, ident, parent):
210 210 matches = {'matches' : self._complete(parent),
211 211 'status' : 'ok'}
212 212 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
213 213 matches, parent, ident)
214 214 print >> sys.__stdout__, completion_msg
215 215
216 216 def object_info_request(self, ident, parent):
217 217 context = parent['content']['oname'].split('.')
218 218 object_info = self._object_info(context)
219 219 msg = self.session.send(self.reply_socket, 'object_info_reply',
220 220 object_info, parent, ident)
221 221 print >> sys.__stdout__, msg
222 222
223 223 def prompt_request(self, ident, parent):
224 224 prompt_number = self.shell.displayhook.prompt_count
225 225 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
226 226 content = {'prompt_string' : prompt_string,
227 227 'prompt_number' : prompt_number+1}
228 228 msg = self.session.send(self.reply_socket, 'prompt_reply',
229 229 content, parent, ident)
230 230 print >> sys.__stdout__, msg
231 231
232 232 def history_request(self, ident, parent):
233 233 output = parent['content'].get('output', True)
234 234 index = parent['content'].get('index')
235 235 raw = parent['content'].get('raw', False)
236 236 hist = self.shell.get_history(index=index, raw=raw, output=output)
237 237 content = {'history' : hist}
238 238 msg = self.session.send(self.reply_socket, 'history_reply',
239 239 content, parent, ident)
240 240 print >> sys.__stdout__, msg
241 241
242 242 #---------------------------------------------------------------------------
243 243 # Protected interface
244 244 #---------------------------------------------------------------------------
245 245
246 246 def _abort_queue(self):
247 247 while True:
248 248 try:
249 249 ident = self.reply_socket.recv(zmq.NOBLOCK)
250 250 except zmq.ZMQError, e:
251 251 if e.errno == zmq.EAGAIN:
252 252 break
253 253 else:
254 254 assert self.reply_socket.rcvmore(), "Unexpected missing message part."
255 255 msg = self.reply_socket.recv_json()
256 256 print>>sys.__stdout__, "Aborting:"
257 257 print>>sys.__stdout__, Message(msg)
258 258 msg_type = msg['msg_type']
259 259 reply_type = msg_type.split('_')[0] + '_reply'
260 260 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
261 261 print>>sys.__stdout__, Message(reply_msg)
262 262 self.reply_socket.send(ident,zmq.SNDMORE)
263 263 self.reply_socket.send_json(reply_msg)
264 264 # We need to wait a bit for requests to come in. This can probably
265 265 # be set shorter for true asynchronous clients.
266 266 time.sleep(0.1)
267 267
268 268 def _raw_input(self, prompt, ident, parent):
269 269 # Flush output before making the request.
270 270 sys.stderr.flush()
271 271 sys.stdout.flush()
272 272
273 273 # Send the input request.
274 274 content = dict(prompt=prompt)
275 275 msg = self.session.msg(u'input_request', content, parent)
276 276 self.req_socket.send_json(msg)
277 277
278 278 # Await a response.
279 279 reply = self.req_socket.recv_json()
280 280 try:
281 281 value = reply['content']['value']
282 282 except:
283 283 print>>sys.__stderr__, "Got bad raw_input reply: "
284 284 print>>sys.__stderr__, Message(parent)
285 285 value = ''
286 286 return value
287 287
288 288 def _complete(self, msg):
289 return self.shell.complete(msg.content.line)
289 #from IPython.utils.io import rprint # dbg
290 #rprint('\n\n**MSG**\n\n', msg) # dbg
291 #import traceback; rprint(''.join(traceback.format_stack())) # dbg
292 c = msg['content']
293 try:
294 cpos = int(c['cursor_pos'])
295 except:
296 # If we don't get something that we can convert to an integer, at
297 # leasat attempt the completion guessing the cursor is at the end
298 # of the text
299 cpos = len(c['text'])
300 return self.shell.complete(c['text'], c['line'], cpos)
290 301
291 302 def _object_info(self, context):
292 303 symbol, leftover = self._symbol_from_context(context)
293 304 if symbol is not None and not leftover:
294 305 doc = getattr(symbol, '__doc__', '')
295 306 else:
296 307 doc = ''
297 308 object_info = dict(docstring = doc)
298 309 return object_info
299 310
300 311 def _symbol_from_context(self, context):
301 312 if not context:
302 313 return None, context
303 314
304 315 base_symbol_string = context[0]
305 316 symbol = self.shell.user_ns.get(base_symbol_string, None)
306 317 if symbol is None:
307 318 symbol = __builtin__.__dict__.get(base_symbol_string, None)
308 319 if symbol is None:
309 320 return None, context
310 321
311 322 context = context[1:]
312 323 for i, name in enumerate(context):
313 324 new_symbol = getattr(symbol, name, None)
314 325 if new_symbol is None:
315 326 return symbol, context[i:]
316 327 else:
317 328 symbol = new_symbol
318 329
319 330 return symbol, []
320 331
321 332 #-----------------------------------------------------------------------------
322 333 # Kernel main and launch functions
323 334 #-----------------------------------------------------------------------------
324 335
325 336 def launch_kernel(xrep_port=0, pub_port=0, req_port=0, independent=False,
326 337 pylab=False):
327 338 """ Launches a localhost kernel, binding to the specified ports.
328 339
329 340 Parameters
330 341 ----------
331 342 xrep_port : int, optional
332 343 The port to use for XREP channel.
333 344
334 345 pub_port : int, optional
335 346 The port to use for the SUB channel.
336 347
337 348 req_port : int, optional
338 349 The port to use for the REQ (raw input) channel.
339 350
340 351 independent : bool, optional (default False)
341 352 If set, the kernel process is guaranteed to survive if this process
342 353 dies. If not set, an effort is made to ensure that the kernel is killed
343 354 when this process dies. Note that in this case it is still good practice
344 355 to kill kernels manually before exiting.
345 356
346 357 pylab : bool or string, optional (default False)
347 358 If not False, the kernel will be launched with pylab enabled. If a
348 359 string is passed, matplotlib will use the specified backend. Otherwise,
349 360 matplotlib's default backend will be used.
350 361
351 362 Returns
352 363 -------
353 364 A tuple of form:
354 365 (kernel_process, xrep_port, pub_port, req_port)
355 366 where kernel_process is a Popen object and the ports are integers.
356 367 """
357 368 extra_arguments = []
358 369 if pylab:
359 370 extra_arguments.append('--pylab')
360 371 if isinstance(pylab, basestring):
361 372 extra_arguments.append(pylab)
362 373 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
363 374 xrep_port, pub_port, req_port, independent,
364 375 extra_arguments)
365 376
366 377 def main():
367 378 """ The IPython kernel main entry point.
368 379 """
369 380 parser = make_argument_parser()
370 381 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
371 382 const='auto', help = \
372 383 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
373 384 given, the GUI backend is matplotlib's, otherwise use one of: \
374 385 ['tk', 'gtk', 'qt', 'wx', 'payload-svg'].")
375 386 namespace = parser.parse_args()
376 387
377 388 kernel = make_kernel(namespace, Kernel, OutStream)
378 389 if namespace.pylab:
379 390 if namespace.pylab == 'auto':
380 391 kernel.activate_pylab()
381 392 else:
382 393 kernel.activate_pylab(namespace.pylab)
383 394
384 395 start_kernel(namespace, kernel)
385 396
386 397 if __name__ == '__main__':
387 398 main()
@@ -1,578 +1,578 b''
1 1 """Base classes to manage the interaction with a running kernel.
2 2
3 3 Todo
4 4 ====
5 5
6 6 * Create logger to handle debugging and console messages.
7 7 """
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (C) 2008-2010 The IPython Development Team
11 11 #
12 12 # Distributed under the terms of the BSD License. The full license is in
13 13 # the file COPYING, distributed as part of this software.
14 14 #-----------------------------------------------------------------------------
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Imports
18 18 #-----------------------------------------------------------------------------
19 19
20 20 # Standard library imports.
21 21 from Queue import Queue, Empty
22 22 from subprocess import Popen
23 23 from threading import Thread
24 24 import time
25 25
26 26 # System library imports.
27 27 import zmq
28 28 from zmq import POLLIN, POLLOUT, POLLERR
29 29 from zmq.eventloop import ioloop
30 30
31 31 # Local imports.
32 32 from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress
33 33 from session import Session
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Constants and exceptions
37 37 #-----------------------------------------------------------------------------
38 38
39 39 LOCALHOST = '127.0.0.1'
40 40
41 41 class InvalidPortNumber(Exception):
42 42 pass
43 43
44 44 #-----------------------------------------------------------------------------
45 45 # ZMQ Socket Channel classes
46 46 #-----------------------------------------------------------------------------
47 47
48 48 class ZmqSocketChannel(Thread):
49 49 """The base class for the channels that use ZMQ sockets.
50 50 """
51 51 context = None
52 52 session = None
53 53 socket = None
54 54 ioloop = None
55 55 iostate = None
56 56 _address = None
57 57
58 58 def __init__(self, context, session, address):
59 59 """Create a channel
60 60
61 61 Parameters
62 62 ----------
63 63 context : :class:`zmq.Context`
64 64 The ZMQ context to use.
65 65 session : :class:`session.Session`
66 66 The session to use.
67 67 address : tuple
68 68 Standard (ip, port) tuple that the kernel is listening on.
69 69 """
70 70 super(ZmqSocketChannel, self).__init__()
71 71 self.daemon = True
72 72
73 73 self.context = context
74 74 self.session = session
75 75 if address[1] == 0:
76 76 message = 'The port number for a channel cannot be 0.'
77 77 raise InvalidPortNumber(message)
78 78 self._address = address
79 79
80 80 def stop(self):
81 81 """Stop the channel's activity.
82 82
83 83 This calls :method:`Thread.join` and returns when the thread
84 84 terminates. :class:`RuntimeError` will be raised if
85 85 :method:`self.start` is called again.
86 86 """
87 87 self.join()
88 88
89 89 @property
90 90 def address(self):
91 91 """Get the channel's address as an (ip, port) tuple.
92 92
93 93 By the default, the address is (localhost, 0), where 0 means a random
94 94 port.
95 95 """
96 96 return self._address
97 97
98 98 def add_io_state(self, state):
99 99 """Add IO state to the eventloop.
100 100
101 101 Parameters
102 102 ----------
103 103 state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR
104 104 The IO state flag to set.
105 105
106 106 This is thread safe as it uses the thread safe IOLoop.add_callback.
107 107 """
108 108 def add_io_state_callback():
109 109 if not self.iostate & state:
110 110 self.iostate = self.iostate | state
111 111 self.ioloop.update_handler(self.socket, self.iostate)
112 112 self.ioloop.add_callback(add_io_state_callback)
113 113
114 114 def drop_io_state(self, state):
115 115 """Drop IO state from the eventloop.
116 116
117 117 Parameters
118 118 ----------
119 119 state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR
120 120 The IO state flag to set.
121 121
122 122 This is thread safe as it uses the thread safe IOLoop.add_callback.
123 123 """
124 124 def drop_io_state_callback():
125 125 if self.iostate & state:
126 126 self.iostate = self.iostate & (~state)
127 127 self.ioloop.update_handler(self.socket, self.iostate)
128 128 self.ioloop.add_callback(drop_io_state_callback)
129 129
130 130
131 131 class XReqSocketChannel(ZmqSocketChannel):
132 132 """The XREQ channel for issues request/replies to the kernel.
133 133 """
134 134
135 135 command_queue = None
136 136
137 137 def __init__(self, context, session, address):
138 138 self.command_queue = Queue()
139 139 super(XReqSocketChannel, self).__init__(context, session, address)
140 140
141 141 def run(self):
142 142 """The thread's main activity. Call start() instead."""
143 143 self.socket = self.context.socket(zmq.XREQ)
144 144 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
145 145 self.socket.connect('tcp://%s:%i' % self.address)
146 146 self.ioloop = ioloop.IOLoop()
147 147 self.iostate = POLLERR|POLLIN
148 148 self.ioloop.add_handler(self.socket, self._handle_events,
149 149 self.iostate)
150 150 self.ioloop.start()
151 151
152 152 def stop(self):
153 153 self.ioloop.stop()
154 154 super(XReqSocketChannel, self).stop()
155 155
156 156 def call_handlers(self, msg):
157 157 """This method is called in the ioloop thread when a message arrives.
158 158
159 159 Subclasses should override this method to handle incoming messages.
160 160 It is important to remember that this method is called in the thread
161 161 so that some logic must be done to ensure that the application leve
162 162 handlers are called in the application thread.
163 163 """
164 164 raise NotImplementedError('call_handlers must be defined in a subclass.')
165 165
166 166 def execute(self, code):
167 167 """Execute code in the kernel.
168 168
169 169 Parameters
170 170 ----------
171 171 code : str
172 172 A string of Python code.
173 173
174 174 Returns
175 175 -------
176 176 The msg_id of the message sent.
177 177 """
178 178 # Create class for content/msg creation. Related to, but possibly
179 179 # not in Session.
180 180 content = dict(code=code)
181 181 msg = self.session.msg('execute_request', content)
182 182 self._queue_request(msg)
183 183 return msg['header']['msg_id']
184 184
185 def complete(self, text, line, block=None):
185 def complete(self, text, line, cursor_pos, block=None):
186 186 """Tab complete text, line, block in the kernel's namespace.
187 187
188 188 Parameters
189 189 ----------
190 190 text : str
191 191 The text to complete.
192 192 line : str
193 193 The full line of text that is the surrounding context for the
194 194 text to complete.
195 195 block : str
196 196 The full block of code in which the completion is being requested.
197 197
198 198 Returns
199 199 -------
200 200 The msg_id of the message sent.
201 201 """
202 content = dict(text=text, line=line)
202 content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos)
203 203 msg = self.session.msg('complete_request', content)
204 204 self._queue_request(msg)
205 205 return msg['header']['msg_id']
206 206
207 207 def object_info(self, oname):
208 208 """Get metadata information about an object.
209 209
210 210 Parameters
211 211 ----------
212 212 oname : str
213 213 A string specifying the object name.
214 214
215 215 Returns
216 216 -------
217 217 The msg_id of the message sent.
218 218 """
219 219 content = dict(oname=oname)
220 220 msg = self.session.msg('object_info_request', content)
221 221 self._queue_request(msg)
222 222 return msg['header']['msg_id']
223 223
224 224 def _handle_events(self, socket, events):
225 225 if events & POLLERR:
226 226 self._handle_err()
227 227 if events & POLLOUT:
228 228 self._handle_send()
229 229 if events & POLLIN:
230 230 self._handle_recv()
231 231
232 232 def _handle_recv(self):
233 233 msg = self.socket.recv_json()
234 234 self.call_handlers(msg)
235 235
236 236 def _handle_send(self):
237 237 try:
238 238 msg = self.command_queue.get(False)
239 239 except Empty:
240 240 pass
241 241 else:
242 242 self.socket.send_json(msg)
243 243 if self.command_queue.empty():
244 244 self.drop_io_state(POLLOUT)
245 245
246 246 def _handle_err(self):
247 247 # We don't want to let this go silently, so eventually we should log.
248 248 raise zmq.ZMQError()
249 249
250 250 def _queue_request(self, msg):
251 251 self.command_queue.put(msg)
252 252 self.add_io_state(POLLOUT)
253 253
254 254
255 255 class SubSocketChannel(ZmqSocketChannel):
256 256 """The SUB channel which listens for messages that the kernel publishes.
257 257 """
258 258
259 259 def __init__(self, context, session, address):
260 260 super(SubSocketChannel, self).__init__(context, session, address)
261 261
262 262 def run(self):
263 263 """The thread's main activity. Call start() instead."""
264 264 self.socket = self.context.socket(zmq.SUB)
265 265 self.socket.setsockopt(zmq.SUBSCRIBE,'')
266 266 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
267 267 self.socket.connect('tcp://%s:%i' % self.address)
268 268 self.ioloop = ioloop.IOLoop()
269 269 self.iostate = POLLIN|POLLERR
270 270 self.ioloop.add_handler(self.socket, self._handle_events,
271 271 self.iostate)
272 272 self.ioloop.start()
273 273
274 274 def stop(self):
275 275 self.ioloop.stop()
276 276 super(SubSocketChannel, self).stop()
277 277
278 278 def call_handlers(self, msg):
279 279 """This method is called in the ioloop thread when a message arrives.
280 280
281 281 Subclasses should override this method to handle incoming messages.
282 282 It is important to remember that this method is called in the thread
283 283 so that some logic must be done to ensure that the application leve
284 284 handlers are called in the application thread.
285 285 """
286 286 raise NotImplementedError('call_handlers must be defined in a subclass.')
287 287
288 288 def flush(self, timeout=1.0):
289 289 """Immediately processes all pending messages on the SUB channel.
290 290
291 291 Callers should use this method to ensure that :method:`call_handlers`
292 292 has been called for all messages that have been received on the
293 293 0MQ SUB socket of this channel.
294 294
295 295 This method is thread safe.
296 296
297 297 Parameters
298 298 ----------
299 299 timeout : float, optional
300 300 The maximum amount of time to spend flushing, in seconds. The
301 301 default is one second.
302 302 """
303 303 # We do the IOLoop callback process twice to ensure that the IOLoop
304 304 # gets to perform at least one full poll.
305 305 stop_time = time.time() + timeout
306 306 for i in xrange(2):
307 307 self._flushed = False
308 308 self.ioloop.add_callback(self._flush)
309 309 while not self._flushed and time.time() < stop_time:
310 310 time.sleep(0.01)
311 311
312 312 def _handle_events(self, socket, events):
313 313 # Turn on and off POLLOUT depending on if we have made a request
314 314 if events & POLLERR:
315 315 self._handle_err()
316 316 if events & POLLIN:
317 317 self._handle_recv()
318 318
319 319 def _handle_err(self):
320 320 # We don't want to let this go silently, so eventually we should log.
321 321 raise zmq.ZMQError()
322 322
323 323 def _handle_recv(self):
324 324 # Get all of the messages we can
325 325 while True:
326 326 try:
327 327 msg = self.socket.recv_json(zmq.NOBLOCK)
328 328 except zmq.ZMQError:
329 329 # Check the errno?
330 330 # Will this trigger POLLERR?
331 331 break
332 332 else:
333 333 self.call_handlers(msg)
334 334
335 335 def _flush(self):
336 336 """Callback for :method:`self.flush`."""
337 337 self._flushed = True
338 338
339 339
340 340 class RepSocketChannel(ZmqSocketChannel):
341 341 """A reply channel to handle raw_input requests that the kernel makes."""
342 342
343 343 msg_queue = None
344 344
345 345 def __init__(self, context, session, address):
346 346 self.msg_queue = Queue()
347 347 super(RepSocketChannel, self).__init__(context, session, address)
348 348
349 349 def run(self):
350 350 """The thread's main activity. Call start() instead."""
351 351 self.socket = self.context.socket(zmq.XREQ)
352 352 self.socket.setsockopt(zmq.IDENTITY, self.session.session)
353 353 self.socket.connect('tcp://%s:%i' % self.address)
354 354 self.ioloop = ioloop.IOLoop()
355 355 self.iostate = POLLERR|POLLIN
356 356 self.ioloop.add_handler(self.socket, self._handle_events,
357 357 self.iostate)
358 358 self.ioloop.start()
359 359
360 360 def stop(self):
361 361 self.ioloop.stop()
362 362 super(RepSocketChannel, self).stop()
363 363
364 364 def call_handlers(self, msg):
365 365 """This method is called in the ioloop thread when a message arrives.
366 366
367 367 Subclasses should override this method to handle incoming messages.
368 368 It is important to remember that this method is called in the thread
369 369 so that some logic must be done to ensure that the application leve
370 370 handlers are called in the application thread.
371 371 """
372 372 raise NotImplementedError('call_handlers must be defined in a subclass.')
373 373
374 374 def input(self, string):
375 375 """Send a string of raw input to the kernel."""
376 376 content = dict(value=string)
377 377 msg = self.session.msg('input_reply', content)
378 378 self._queue_reply(msg)
379 379
380 380 def _handle_events(self, socket, events):
381 381 if events & POLLERR:
382 382 self._handle_err()
383 383 if events & POLLOUT:
384 384 self._handle_send()
385 385 if events & POLLIN:
386 386 self._handle_recv()
387 387
388 388 def _handle_recv(self):
389 389 msg = self.socket.recv_json()
390 390 self.call_handlers(msg)
391 391
392 392 def _handle_send(self):
393 393 try:
394 394 msg = self.msg_queue.get(False)
395 395 except Empty:
396 396 pass
397 397 else:
398 398 self.socket.send_json(msg)
399 399 if self.msg_queue.empty():
400 400 self.drop_io_state(POLLOUT)
401 401
402 402 def _handle_err(self):
403 403 # We don't want to let this go silently, so eventually we should log.
404 404 raise zmq.ZMQError()
405 405
406 406 def _queue_reply(self, msg):
407 407 self.msg_queue.put(msg)
408 408 self.add_io_state(POLLOUT)
409 409
410 410
411 411 #-----------------------------------------------------------------------------
412 412 # Main kernel manager class
413 413 #-----------------------------------------------------------------------------
414 414
415 415 class KernelManager(HasTraits):
416 416 """ Manages a kernel for a frontend.
417 417
418 418 The SUB channel is for the frontend to receive messages published by the
419 419 kernel.
420 420
421 421 The REQ channel is for the frontend to make requests of the kernel.
422 422
423 423 The REP channel is for the kernel to request stdin (raw_input) from the
424 424 frontend.
425 425 """
426 426 # The PyZMQ Context to use for communication with the kernel.
427 427 context = Instance(zmq.Context,(),{})
428 428
429 429 # The Session to use for communication with the kernel.
430 430 session = Instance(Session,(),{})
431 431
432 432 # The kernel process with which the KernelManager is communicating.
433 433 kernel = Instance(Popen)
434 434
435 435 # The addresses for the communication channels.
436 436 xreq_address = TCPAddress((LOCALHOST, 0))
437 437 sub_address = TCPAddress((LOCALHOST, 0))
438 438 rep_address = TCPAddress((LOCALHOST, 0))
439 439
440 440 # The classes to use for the various channels.
441 441 xreq_channel_class = Type(XReqSocketChannel)
442 442 sub_channel_class = Type(SubSocketChannel)
443 443 rep_channel_class = Type(RepSocketChannel)
444 444
445 445 # Protected traits.
446 446 _xreq_channel = Any
447 447 _sub_channel = Any
448 448 _rep_channel = Any
449 449
450 450 #--------------------------------------------------------------------------
451 451 # Channel management methods:
452 452 #--------------------------------------------------------------------------
453 453
454 454 def start_channels(self):
455 455 """Starts the channels for this kernel.
456 456
457 457 This will create the channels if they do not exist and then start
458 458 them. If port numbers of 0 are being used (random ports) then you
459 459 must first call :method:`start_kernel`. If the channels have been
460 460 stopped and you call this, :class:`RuntimeError` will be raised.
461 461 """
462 462 self.xreq_channel.start()
463 463 self.sub_channel.start()
464 464 self.rep_channel.start()
465 465
466 466 def stop_channels(self):
467 467 """Stops the channels for this kernel.
468 468
469 469 This stops the channels by joining their threads. If the channels
470 470 were not started, :class:`RuntimeError` will be raised.
471 471 """
472 472 self.xreq_channel.stop()
473 473 self.sub_channel.stop()
474 474 self.rep_channel.stop()
475 475
476 476 @property
477 477 def channels_running(self):
478 478 """Are all of the channels created and running?"""
479 479 return self.xreq_channel.is_alive() \
480 480 and self.sub_channel.is_alive() \
481 481 and self.rep_channel.is_alive()
482 482
483 483 #--------------------------------------------------------------------------
484 484 # Kernel process management methods:
485 485 #--------------------------------------------------------------------------
486 486
487 487 def start_kernel(self, ipython=True, **kw):
488 488 """Starts a kernel process and configures the manager to use it.
489 489
490 490 If random ports (port=0) are being used, this method must be called
491 491 before the channels are created.
492 492
493 493 Parameters:
494 494 -----------
495 495 ipython : bool, optional (default True)
496 496 Whether to use an IPython kernel instead of a plain Python kernel.
497 497 """
498 498 xreq, sub, rep = self.xreq_address, self.sub_address, self.rep_address
499 499 if xreq[0] != LOCALHOST or sub[0] != LOCALHOST or rep[0] != LOCALHOST:
500 500 raise RuntimeError("Can only launch a kernel on localhost."
501 501 "Make sure that the '*_address' attributes are "
502 502 "configured properly.")
503 503
504 504 if ipython:
505 505 from ipkernel import launch_kernel as launch
506 506 else:
507 507 from pykernel import launch_kernel as launch
508 508 self.kernel, xrep, pub, req = launch(xrep_port=xreq[1], pub_port=sub[1],
509 509 req_port=rep[1], **kw)
510 510 self.xreq_address = (LOCALHOST, xrep)
511 511 self.sub_address = (LOCALHOST, pub)
512 512 self.rep_address = (LOCALHOST, req)
513 513
514 514 @property
515 515 def has_kernel(self):
516 516 """Returns whether a kernel process has been specified for the kernel
517 517 manager.
518 518 """
519 519 return self.kernel is not None
520 520
521 521 def kill_kernel(self):
522 522 """ Kill the running kernel. """
523 523 if self.kernel is not None:
524 524 self.kernel.kill()
525 525 self.kernel = None
526 526 else:
527 527 raise RuntimeError("Cannot kill kernel. No kernel is running!")
528 528
529 529 def signal_kernel(self, signum):
530 530 """ Sends a signal to the kernel. """
531 531 if self.kernel is not None:
532 532 self.kernel.send_signal(signum)
533 533 else:
534 534 raise RuntimeError("Cannot signal kernel. No kernel is running!")
535 535
536 536 @property
537 537 def is_alive(self):
538 538 """Is the kernel process still running?"""
539 539 if self.kernel is not None:
540 540 if self.kernel.poll() is None:
541 541 return True
542 542 else:
543 543 return False
544 544 else:
545 545 # We didn't start the kernel with this KernelManager so we don't
546 546 # know if it is running. We should use a heartbeat for this case.
547 547 return True
548 548
549 549 #--------------------------------------------------------------------------
550 550 # Channels used for communication with the kernel:
551 551 #--------------------------------------------------------------------------
552 552
553 553 @property
554 554 def xreq_channel(self):
555 555 """Get the REQ socket channel object to make requests of the kernel."""
556 556 if self._xreq_channel is None:
557 557 self._xreq_channel = self.xreq_channel_class(self.context,
558 558 self.session,
559 559 self.xreq_address)
560 560 return self._xreq_channel
561 561
562 562 @property
563 563 def sub_channel(self):
564 564 """Get the SUB socket channel object."""
565 565 if self._sub_channel is None:
566 566 self._sub_channel = self.sub_channel_class(self.context,
567 567 self.session,
568 568 self.sub_address)
569 569 return self._sub_channel
570 570
571 571 @property
572 572 def rep_channel(self):
573 573 """Get the REP socket channel object to handle stdin (raw_input)."""
574 574 if self._rep_channel is None:
575 575 self._rep_channel = self.rep_channel_class(self.context,
576 576 self.session,
577 577 self.rep_address)
578 578 return self._rep_channel
General Comments 0
You need to be logged in to leave comments. Login now