##// END OF EJS Templates
Remove runlines method and calls to it.
Thomas Kluyver -
Show More
@@ -1,2608 +1,2562 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # 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 __future__
22 22 import abc
23 23 import ast
24 24 import atexit
25 25 import codeop
26 26 import inspect
27 27 import os
28 28 import re
29 29 import sys
30 30 import tempfile
31 31 import types
32 32 from contextlib import nested
33 33
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.core import debugger, oinspect
36 36 from IPython.core import history as ipcorehist
37 37 from IPython.core import page
38 38 from IPython.core import prefilter
39 39 from IPython.core import shadowns
40 40 from IPython.core import ultratb
41 41 from IPython.core.alias import AliasManager
42 42 from IPython.core.autocall import ExitAutocall
43 43 from IPython.core.builtin_trap import BuiltinTrap
44 44 from IPython.core.compilerop import CachingCompiler
45 45 from IPython.core.display_trap import DisplayTrap
46 46 from IPython.core.displayhook import DisplayHook
47 47 from IPython.core.displaypub import DisplayPublisher
48 48 from IPython.core.error import TryNext, UsageError
49 49 from IPython.core.extensions import ExtensionManager
50 50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 51 from IPython.core.formatters import DisplayFormatter
52 52 from IPython.core.history import HistoryManager
53 53 from IPython.core.inputsplitter import IPythonInputSplitter
54 54 from IPython.core.logger import Logger
55 55 from IPython.core.macro import Macro
56 56 from IPython.core.magic import Magic
57 57 from IPython.core.payload import PayloadManager
58 58 from IPython.core.plugin import PluginManager
59 59 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
60 60 from IPython.external.Itpl import ItplNS
61 61 from IPython.utils import PyColorize
62 62 from IPython.utils import io
63 63 from IPython.utils.doctestreload import doctest_reload
64 64 from IPython.utils.io import ask_yes_no, rprint
65 65 from IPython.utils.ipstruct import Struct
66 66 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
67 67 from IPython.utils.pickleshare import PickleShareDB
68 68 from IPython.utils.process import system, getoutput
69 69 from IPython.utils.strdispatch import StrDispatch
70 70 from IPython.utils.syspathcontext import prepended_to_syspath
71 71 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
72 72 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
73 73 List, Unicode, Instance, Type)
74 74 from IPython.utils.warn import warn, error, fatal
75 75 import IPython.core.hooks
76 76
77 77 #-----------------------------------------------------------------------------
78 78 # Globals
79 79 #-----------------------------------------------------------------------------
80 80
81 81 # compiled regexps for autoindent management
82 82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
83 83
84 84 #-----------------------------------------------------------------------------
85 85 # Utilities
86 86 #-----------------------------------------------------------------------------
87 87
88 88 # store the builtin raw_input globally, and use this always, in case user code
89 89 # overwrites it (like wx.py.PyShell does)
90 90 raw_input_original = raw_input
91 91
92 92 def softspace(file, newvalue):
93 93 """Copied from code.py, to remove the dependency"""
94 94
95 95 oldvalue = 0
96 96 try:
97 97 oldvalue = file.softspace
98 98 except AttributeError:
99 99 pass
100 100 try:
101 101 file.softspace = newvalue
102 102 except (AttributeError, TypeError):
103 103 # "attribute-less object" or "read-only attributes"
104 104 pass
105 105 return oldvalue
106 106
107 107
108 108 def no_op(*a, **kw): pass
109 109
110 110 class SpaceInInput(Exception): pass
111 111
112 112 class Bunch: pass
113 113
114 114
115 115 def get_default_colors():
116 116 if sys.platform=='darwin':
117 117 return "LightBG"
118 118 elif os.name=='nt':
119 119 return 'Linux'
120 120 else:
121 121 return 'Linux'
122 122
123 123
124 124 class SeparateStr(Str):
125 125 """A Str subclass to validate separate_in, separate_out, etc.
126 126
127 127 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
128 128 """
129 129
130 130 def validate(self, obj, value):
131 131 if value == '0': value = ''
132 132 value = value.replace('\\n','\n')
133 133 return super(SeparateStr, self).validate(obj, value)
134 134
135 135 class MultipleInstanceError(Exception):
136 136 pass
137 137
138 138 class ReadlineNoRecord(object):
139 139 """Context manager to execute some code, then reload readline history
140 140 so that interactive input to the code doesn't appear when pressing up."""
141 141 def __init__(self, shell):
142 142 self.shell = shell
143 143 self._nested_level = 0
144 144
145 145 def __enter__(self):
146 146 if self._nested_level == 0:
147 147 self.orig_length = self.current_length()
148 148 self.readline_tail = self.get_readline_tail()
149 149 self._nested_level += 1
150 150
151 151 def __exit__(self, type, value, traceback):
152 152 self._nested_level -= 1
153 153 if self._nested_level == 0:
154 154 # Try clipping the end if it's got longer
155 155 e = self.current_length() - self.orig_length
156 156 if e > 0:
157 157 for _ in range(e):
158 158 self.shell.readline.remove_history_item(self.orig_length)
159 159
160 160 # If it still doesn't match, just reload readline history.
161 161 if self.current_length() != self.orig_length \
162 162 or self.get_readline_tail() != self.readline_tail:
163 163 self.shell.refill_readline_hist()
164 164 # Returning False will cause exceptions to propagate
165 165 return False
166 166
167 167 def current_length(self):
168 168 return self.shell.readline.get_current_history_length()
169 169
170 170 def get_readline_tail(self, n=10):
171 171 """Get the last n items in readline history."""
172 172 end = self.shell.readline.get_current_history_length() + 1
173 173 start = max(end-n, 1)
174 174 ghi = self.shell.readline.get_history_item
175 175 return [ghi(x) for x in range(start, end)]
176 176
177 177
178 178 #-----------------------------------------------------------------------------
179 179 # Main IPython class
180 180 #-----------------------------------------------------------------------------
181 181
182 182 class InteractiveShell(Configurable, Magic):
183 183 """An enhanced, interactive shell for Python."""
184 184
185 185 _instance = None
186 186 autocall = Enum((0,1,2), default_value=1, config=True)
187 187 # TODO: remove all autoindent logic and put into frontends.
188 188 # We can't do this yet because even runlines uses the autoindent.
189 189 autoindent = CBool(True, config=True)
190 190 automagic = CBool(True, config=True)
191 191 cache_size = Int(1000, config=True)
192 192 color_info = CBool(True, config=True)
193 193 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
194 194 default_value=get_default_colors(), config=True)
195 195 debug = CBool(False, config=True)
196 196 deep_reload = CBool(False, config=True)
197 197 display_formatter = Instance(DisplayFormatter)
198 198 displayhook_class = Type(DisplayHook)
199 199 display_pub_class = Type(DisplayPublisher)
200 200
201 201 exit_now = CBool(False)
202 202 exiter = Instance(ExitAutocall)
203 203 def _exiter_default(self):
204 204 return ExitAutocall(self)
205 205 # Monotonically increasing execution counter
206 206 execution_count = Int(1)
207 207 filename = Unicode("<ipython console>")
208 208 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
209 209
210 210 # Input splitter, to split entire cells of input into either individual
211 211 # interactive statements or whole blocks.
212 212 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
213 213 (), {})
214 214 logstart = CBool(False, config=True)
215 215 logfile = Unicode('', config=True)
216 216 logappend = Unicode('', config=True)
217 217 object_info_string_level = Enum((0,1,2), default_value=0,
218 218 config=True)
219 219 pdb = CBool(False, config=True)
220 220
221 221 profile = Unicode('', config=True)
222 222 prompt_in1 = Str('In [\\#]: ', config=True)
223 223 prompt_in2 = Str(' .\\D.: ', config=True)
224 224 prompt_out = Str('Out[\\#]: ', config=True)
225 225 prompts_pad_left = CBool(True, config=True)
226 226 quiet = CBool(False, config=True)
227 227
228 228 history_length = Int(10000, config=True)
229 229
230 230 # The readline stuff will eventually be moved to the terminal subclass
231 231 # but for now, we can't do that as readline is welded in everywhere.
232 232 readline_use = CBool(True, config=True)
233 233 readline_merge_completions = CBool(True, config=True)
234 234 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
235 235 readline_remove_delims = Str('-/~', config=True)
236 236 readline_parse_and_bind = List([
237 237 'tab: complete',
238 238 '"\C-l": clear-screen',
239 239 'set show-all-if-ambiguous on',
240 240 '"\C-o": tab-insert',
241 241 # See bug gh-58 - with \M-i enabled, chars 0x9000-0x9fff
242 242 # crash IPython.
243 243 '"\M-o": "\d\d\d\d"',
244 244 '"\M-I": "\d\d\d\d"',
245 245 '"\C-r": reverse-search-history',
246 246 '"\C-s": forward-search-history',
247 247 '"\C-p": history-search-backward',
248 248 '"\C-n": history-search-forward',
249 249 '"\e[A": history-search-backward',
250 250 '"\e[B": history-search-forward',
251 251 '"\C-k": kill-line',
252 252 '"\C-u": unix-line-discard',
253 253 ], allow_none=False, config=True)
254 254
255 255 # TODO: this part of prompt management should be moved to the frontends.
256 256 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
257 257 separate_in = SeparateStr('\n', config=True)
258 258 separate_out = SeparateStr('', config=True)
259 259 separate_out2 = SeparateStr('', config=True)
260 260 wildcards_case_sensitive = CBool(True, config=True)
261 261 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
262 262 default_value='Context', config=True)
263 263
264 264 # Subcomponents of InteractiveShell
265 265 alias_manager = Instance('IPython.core.alias.AliasManager')
266 266 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
267 267 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
268 268 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
269 269 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
270 270 plugin_manager = Instance('IPython.core.plugin.PluginManager')
271 271 payload_manager = Instance('IPython.core.payload.PayloadManager')
272 272 history_manager = Instance('IPython.core.history.HistoryManager')
273 273
274 274 # Private interface
275 275 _post_execute = Instance(dict)
276 276
277 277 def __init__(self, config=None, ipython_dir=None,
278 278 user_ns=None, user_global_ns=None,
279 279 custom_exceptions=((), None)):
280 280
281 281 # This is where traits with a config_key argument are updated
282 282 # from the values on config.
283 283 super(InteractiveShell, self).__init__(config=config)
284 284
285 285 # These are relatively independent and stateless
286 286 self.init_ipython_dir(ipython_dir)
287 287 self.init_instance_attrs()
288 288 self.init_environment()
289 289
290 290 # Create namespaces (user_ns, user_global_ns, etc.)
291 291 self.init_create_namespaces(user_ns, user_global_ns)
292 292 # This has to be done after init_create_namespaces because it uses
293 293 # something in self.user_ns, but before init_sys_modules, which
294 294 # is the first thing to modify sys.
295 295 # TODO: When we override sys.stdout and sys.stderr before this class
296 296 # is created, we are saving the overridden ones here. Not sure if this
297 297 # is what we want to do.
298 298 self.save_sys_module_state()
299 299 self.init_sys_modules()
300 300
301 301 # While we're trying to have each part of the code directly access what
302 302 # it needs without keeping redundant references to objects, we have too
303 303 # much legacy code that expects ip.db to exist.
304 304 self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db'))
305 305
306 306 self.init_history()
307 307 self.init_encoding()
308 308 self.init_prefilter()
309 309
310 310 Magic.__init__(self, self)
311 311
312 312 self.init_syntax_highlighting()
313 313 self.init_hooks()
314 314 self.init_pushd_popd_magic()
315 315 # self.init_traceback_handlers use to be here, but we moved it below
316 316 # because it and init_io have to come after init_readline.
317 317 self.init_user_ns()
318 318 self.init_logger()
319 319 self.init_alias()
320 320 self.init_builtins()
321 321
322 322 # pre_config_initialization
323 323
324 324 # The next section should contain everything that was in ipmaker.
325 325 self.init_logstart()
326 326
327 327 # The following was in post_config_initialization
328 328 self.init_inspector()
329 329 # init_readline() must come before init_io(), because init_io uses
330 330 # readline related things.
331 331 self.init_readline()
332 332 # init_completer must come after init_readline, because it needs to
333 333 # know whether readline is present or not system-wide to configure the
334 334 # completers, since the completion machinery can now operate
335 335 # independently of readline (e.g. over the network)
336 336 self.init_completer()
337 337 # TODO: init_io() needs to happen before init_traceback handlers
338 338 # because the traceback handlers hardcode the stdout/stderr streams.
339 339 # This logic in in debugger.Pdb and should eventually be changed.
340 340 self.init_io()
341 341 self.init_traceback_handlers(custom_exceptions)
342 342 self.init_prompts()
343 343 self.init_display_formatter()
344 344 self.init_display_pub()
345 345 self.init_displayhook()
346 346 self.init_reload_doctest()
347 347 self.init_magics()
348 348 self.init_pdb()
349 349 self.init_extension_manager()
350 350 self.init_plugin_manager()
351 351 self.init_payload()
352 352 self.hooks.late_startup_hook()
353 353 atexit.register(self.atexit_operations)
354 354
355 355 @classmethod
356 356 def instance(cls, *args, **kwargs):
357 357 """Returns a global InteractiveShell instance."""
358 358 if cls._instance is None:
359 359 inst = cls(*args, **kwargs)
360 360 # Now make sure that the instance will also be returned by
361 361 # the subclasses instance attribute.
362 362 for subclass in cls.mro():
363 363 if issubclass(cls, subclass) and \
364 364 issubclass(subclass, InteractiveShell):
365 365 subclass._instance = inst
366 366 else:
367 367 break
368 368 if isinstance(cls._instance, cls):
369 369 return cls._instance
370 370 else:
371 371 raise MultipleInstanceError(
372 372 'Multiple incompatible subclass instances of '
373 373 'InteractiveShell are being created.'
374 374 )
375 375
376 376 @classmethod
377 377 def initialized(cls):
378 378 return hasattr(cls, "_instance")
379 379
380 380 def get_ipython(self):
381 381 """Return the currently running IPython instance."""
382 382 return self
383 383
384 384 #-------------------------------------------------------------------------
385 385 # Trait changed handlers
386 386 #-------------------------------------------------------------------------
387 387
388 388 def _ipython_dir_changed(self, name, new):
389 389 if not os.path.isdir(new):
390 390 os.makedirs(new, mode = 0777)
391 391
392 392 def set_autoindent(self,value=None):
393 393 """Set the autoindent flag, checking for readline support.
394 394
395 395 If called with no arguments, it acts as a toggle."""
396 396
397 397 if not self.has_readline:
398 398 if os.name == 'posix':
399 399 warn("The auto-indent feature requires the readline library")
400 400 self.autoindent = 0
401 401 return
402 402 if value is None:
403 403 self.autoindent = not self.autoindent
404 404 else:
405 405 self.autoindent = value
406 406
407 407 #-------------------------------------------------------------------------
408 408 # init_* methods called by __init__
409 409 #-------------------------------------------------------------------------
410 410
411 411 def init_ipython_dir(self, ipython_dir):
412 412 if ipython_dir is not None:
413 413 self.ipython_dir = ipython_dir
414 414 self.config.Global.ipython_dir = self.ipython_dir
415 415 return
416 416
417 417 if hasattr(self.config.Global, 'ipython_dir'):
418 418 self.ipython_dir = self.config.Global.ipython_dir
419 419 else:
420 420 self.ipython_dir = get_ipython_dir()
421 421
422 422 # All children can just read this
423 423 self.config.Global.ipython_dir = self.ipython_dir
424 424
425 425 def init_instance_attrs(self):
426 426 self.more = False
427 427
428 428 # command compiler
429 429 self.compile = CachingCompiler()
430 430
431 431 # User input buffers
432 432 # NOTE: these variables are slated for full removal, once we are 100%
433 433 # sure that the new execution logic is solid. We will delte runlines,
434 434 # push_line and these buffers, as all input will be managed by the
435 435 # frontends via an inputsplitter instance.
436 436 self.buffer = []
437 437 self.buffer_raw = []
438 438
439 439 # Make an empty namespace, which extension writers can rely on both
440 440 # existing and NEVER being used by ipython itself. This gives them a
441 441 # convenient location for storing additional information and state
442 442 # their extensions may require, without fear of collisions with other
443 443 # ipython names that may develop later.
444 444 self.meta = Struct()
445 445
446 446 # Temporary files used for various purposes. Deleted at exit.
447 447 self.tempfiles = []
448 448
449 449 # Keep track of readline usage (later set by init_readline)
450 450 self.has_readline = False
451 451
452 452 # keep track of where we started running (mainly for crash post-mortem)
453 453 # This is not being used anywhere currently.
454 454 self.starting_dir = os.getcwd()
455 455
456 456 # Indentation management
457 457 self.indent_current_nsp = 0
458 458
459 459 # Dict to track post-execution functions that have been registered
460 460 self._post_execute = {}
461 461
462 462 def init_environment(self):
463 463 """Any changes we need to make to the user's environment."""
464 464 pass
465 465
466 466 def init_encoding(self):
467 467 # Get system encoding at startup time. Certain terminals (like Emacs
468 468 # under Win32 have it set to None, and we need to have a known valid
469 469 # encoding to use in the raw_input() method
470 470 try:
471 471 self.stdin_encoding = sys.stdin.encoding or 'ascii'
472 472 except AttributeError:
473 473 self.stdin_encoding = 'ascii'
474 474
475 475 def init_syntax_highlighting(self):
476 476 # Python source parser/formatter for syntax highlighting
477 477 pyformat = PyColorize.Parser().format
478 478 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
479 479
480 480 def init_pushd_popd_magic(self):
481 481 # for pushd/popd management
482 482 try:
483 483 self.home_dir = get_home_dir()
484 484 except HomeDirError, msg:
485 485 fatal(msg)
486 486
487 487 self.dir_stack = []
488 488
489 489 def init_logger(self):
490 490 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
491 491 logmode='rotate')
492 492
493 493 def init_logstart(self):
494 494 """Initialize logging in case it was requested at the command line.
495 495 """
496 496 if self.logappend:
497 497 self.magic_logstart(self.logappend + ' append')
498 498 elif self.logfile:
499 499 self.magic_logstart(self.logfile)
500 500 elif self.logstart:
501 501 self.magic_logstart()
502 502
503 503 def init_builtins(self):
504 504 self.builtin_trap = BuiltinTrap(shell=self)
505 505
506 506 def init_inspector(self):
507 507 # Object inspector
508 508 self.inspector = oinspect.Inspector(oinspect.InspectColors,
509 509 PyColorize.ANSICodeColors,
510 510 'NoColor',
511 511 self.object_info_string_level)
512 512
513 513 def init_io(self):
514 514 # This will just use sys.stdout and sys.stderr. If you want to
515 515 # override sys.stdout and sys.stderr themselves, you need to do that
516 516 # *before* instantiating this class, because Term holds onto
517 517 # references to the underlying streams.
518 518 if sys.platform == 'win32' and self.has_readline:
519 519 Term = io.IOTerm(cout=self.readline._outputfile,
520 520 cerr=self.readline._outputfile)
521 521 else:
522 522 Term = io.IOTerm()
523 523 io.Term = Term
524 524
525 525 def init_prompts(self):
526 526 # TODO: This is a pass for now because the prompts are managed inside
527 527 # the DisplayHook. Once there is a separate prompt manager, this
528 528 # will initialize that object and all prompt related information.
529 529 pass
530 530
531 531 def init_display_formatter(self):
532 532 self.display_formatter = DisplayFormatter(config=self.config)
533 533
534 534 def init_display_pub(self):
535 535 self.display_pub = self.display_pub_class(config=self.config)
536 536
537 537 def init_displayhook(self):
538 538 # Initialize displayhook, set in/out prompts and printing system
539 539 self.displayhook = self.displayhook_class(
540 540 config=self.config,
541 541 shell=self,
542 542 cache_size=self.cache_size,
543 543 input_sep = self.separate_in,
544 544 output_sep = self.separate_out,
545 545 output_sep2 = self.separate_out2,
546 546 ps1 = self.prompt_in1,
547 547 ps2 = self.prompt_in2,
548 548 ps_out = self.prompt_out,
549 549 pad_left = self.prompts_pad_left
550 550 )
551 551 # This is a context manager that installs/revmoes the displayhook at
552 552 # the appropriate time.
553 553 self.display_trap = DisplayTrap(hook=self.displayhook)
554 554
555 555 def init_reload_doctest(self):
556 556 # Do a proper resetting of doctest, including the necessary displayhook
557 557 # monkeypatching
558 558 try:
559 559 doctest_reload()
560 560 except ImportError:
561 561 warn("doctest module does not exist.")
562 562
563 563 #-------------------------------------------------------------------------
564 564 # Things related to injections into the sys module
565 565 #-------------------------------------------------------------------------
566 566
567 567 def save_sys_module_state(self):
568 568 """Save the state of hooks in the sys module.
569 569
570 570 This has to be called after self.user_ns is created.
571 571 """
572 572 self._orig_sys_module_state = {}
573 573 self._orig_sys_module_state['stdin'] = sys.stdin
574 574 self._orig_sys_module_state['stdout'] = sys.stdout
575 575 self._orig_sys_module_state['stderr'] = sys.stderr
576 576 self._orig_sys_module_state['excepthook'] = sys.excepthook
577 577 try:
578 578 self._orig_sys_modules_main_name = self.user_ns['__name__']
579 579 except KeyError:
580 580 pass
581 581
582 582 def restore_sys_module_state(self):
583 583 """Restore the state of the sys module."""
584 584 try:
585 585 for k, v in self._orig_sys_module_state.iteritems():
586 586 setattr(sys, k, v)
587 587 except AttributeError:
588 588 pass
589 589 # Reset what what done in self.init_sys_modules
590 590 try:
591 591 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
592 592 except (AttributeError, KeyError):
593 593 pass
594 594
595 595 #-------------------------------------------------------------------------
596 596 # Things related to hooks
597 597 #-------------------------------------------------------------------------
598 598
599 599 def init_hooks(self):
600 600 # hooks holds pointers used for user-side customizations
601 601 self.hooks = Struct()
602 602
603 603 self.strdispatchers = {}
604 604
605 605 # Set all default hooks, defined in the IPython.hooks module.
606 606 hooks = IPython.core.hooks
607 607 for hook_name in hooks.__all__:
608 608 # default hooks have priority 100, i.e. low; user hooks should have
609 609 # 0-100 priority
610 610 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
611 611
612 612 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
613 613 """set_hook(name,hook) -> sets an internal IPython hook.
614 614
615 615 IPython exposes some of its internal API as user-modifiable hooks. By
616 616 adding your function to one of these hooks, you can modify IPython's
617 617 behavior to call at runtime your own routines."""
618 618
619 619 # At some point in the future, this should validate the hook before it
620 620 # accepts it. Probably at least check that the hook takes the number
621 621 # of args it's supposed to.
622 622
623 623 f = types.MethodType(hook,self)
624 624
625 625 # check if the hook is for strdispatcher first
626 626 if str_key is not None:
627 627 sdp = self.strdispatchers.get(name, StrDispatch())
628 628 sdp.add_s(str_key, f, priority )
629 629 self.strdispatchers[name] = sdp
630 630 return
631 631 if re_key is not None:
632 632 sdp = self.strdispatchers.get(name, StrDispatch())
633 633 sdp.add_re(re.compile(re_key), f, priority )
634 634 self.strdispatchers[name] = sdp
635 635 return
636 636
637 637 dp = getattr(self.hooks, name, None)
638 638 if name not in IPython.core.hooks.__all__:
639 639 print "Warning! Hook '%s' is not one of %s" % \
640 640 (name, IPython.core.hooks.__all__ )
641 641 if not dp:
642 642 dp = IPython.core.hooks.CommandChainDispatcher()
643 643
644 644 try:
645 645 dp.add(f,priority)
646 646 except AttributeError:
647 647 # it was not commandchain, plain old func - replace
648 648 dp = f
649 649
650 650 setattr(self.hooks,name, dp)
651 651
652 652 def register_post_execute(self, func):
653 653 """Register a function for calling after code execution.
654 654 """
655 655 if not callable(func):
656 656 raise ValueError('argument %s must be callable' % func)
657 657 self._post_execute[func] = True
658 658
659 659 #-------------------------------------------------------------------------
660 660 # Things related to the "main" module
661 661 #-------------------------------------------------------------------------
662 662
663 663 def new_main_mod(self,ns=None):
664 664 """Return a new 'main' module object for user code execution.
665 665 """
666 666 main_mod = self._user_main_module
667 667 init_fakemod_dict(main_mod,ns)
668 668 return main_mod
669 669
670 670 def cache_main_mod(self,ns,fname):
671 671 """Cache a main module's namespace.
672 672
673 673 When scripts are executed via %run, we must keep a reference to the
674 674 namespace of their __main__ module (a FakeModule instance) around so
675 675 that Python doesn't clear it, rendering objects defined therein
676 676 useless.
677 677
678 678 This method keeps said reference in a private dict, keyed by the
679 679 absolute path of the module object (which corresponds to the script
680 680 path). This way, for multiple executions of the same script we only
681 681 keep one copy of the namespace (the last one), thus preventing memory
682 682 leaks from old references while allowing the objects from the last
683 683 execution to be accessible.
684 684
685 685 Note: we can not allow the actual FakeModule instances to be deleted,
686 686 because of how Python tears down modules (it hard-sets all their
687 687 references to None without regard for reference counts). This method
688 688 must therefore make a *copy* of the given namespace, to allow the
689 689 original module's __dict__ to be cleared and reused.
690 690
691 691
692 692 Parameters
693 693 ----------
694 694 ns : a namespace (a dict, typically)
695 695
696 696 fname : str
697 697 Filename associated with the namespace.
698 698
699 699 Examples
700 700 --------
701 701
702 702 In [10]: import IPython
703 703
704 704 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
705 705
706 706 In [12]: IPython.__file__ in _ip._main_ns_cache
707 707 Out[12]: True
708 708 """
709 709 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
710 710
711 711 def clear_main_mod_cache(self):
712 712 """Clear the cache of main modules.
713 713
714 714 Mainly for use by utilities like %reset.
715 715
716 716 Examples
717 717 --------
718 718
719 719 In [15]: import IPython
720 720
721 721 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
722 722
723 723 In [17]: len(_ip._main_ns_cache) > 0
724 724 Out[17]: True
725 725
726 726 In [18]: _ip.clear_main_mod_cache()
727 727
728 728 In [19]: len(_ip._main_ns_cache) == 0
729 729 Out[19]: True
730 730 """
731 731 self._main_ns_cache.clear()
732 732
733 733 #-------------------------------------------------------------------------
734 734 # Things related to debugging
735 735 #-------------------------------------------------------------------------
736 736
737 737 def init_pdb(self):
738 738 # Set calling of pdb on exceptions
739 739 # self.call_pdb is a property
740 740 self.call_pdb = self.pdb
741 741
742 742 def _get_call_pdb(self):
743 743 return self._call_pdb
744 744
745 745 def _set_call_pdb(self,val):
746 746
747 747 if val not in (0,1,False,True):
748 748 raise ValueError,'new call_pdb value must be boolean'
749 749
750 750 # store value in instance
751 751 self._call_pdb = val
752 752
753 753 # notify the actual exception handlers
754 754 self.InteractiveTB.call_pdb = val
755 755
756 756 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
757 757 'Control auto-activation of pdb at exceptions')
758 758
759 759 def debugger(self,force=False):
760 760 """Call the pydb/pdb debugger.
761 761
762 762 Keywords:
763 763
764 764 - force(False): by default, this routine checks the instance call_pdb
765 765 flag and does not actually invoke the debugger if the flag is false.
766 766 The 'force' option forces the debugger to activate even if the flag
767 767 is false.
768 768 """
769 769
770 770 if not (force or self.call_pdb):
771 771 return
772 772
773 773 if not hasattr(sys,'last_traceback'):
774 774 error('No traceback has been produced, nothing to debug.')
775 775 return
776 776
777 777 # use pydb if available
778 778 if debugger.has_pydb:
779 779 from pydb import pm
780 780 else:
781 781 # fallback to our internal debugger
782 782 pm = lambda : self.InteractiveTB.debugger(force=True)
783 783
784 784 with self.readline_no_record:
785 785 pm()
786 786
787 787 #-------------------------------------------------------------------------
788 788 # Things related to IPython's various namespaces
789 789 #-------------------------------------------------------------------------
790 790
791 791 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
792 792 # Create the namespace where the user will operate. user_ns is
793 793 # normally the only one used, and it is passed to the exec calls as
794 794 # the locals argument. But we do carry a user_global_ns namespace
795 795 # given as the exec 'globals' argument, This is useful in embedding
796 796 # situations where the ipython shell opens in a context where the
797 797 # distinction between locals and globals is meaningful. For
798 798 # non-embedded contexts, it is just the same object as the user_ns dict.
799 799
800 800 # FIXME. For some strange reason, __builtins__ is showing up at user
801 801 # level as a dict instead of a module. This is a manual fix, but I
802 802 # should really track down where the problem is coming from. Alex
803 803 # Schmolck reported this problem first.
804 804
805 805 # A useful post by Alex Martelli on this topic:
806 806 # Re: inconsistent value from __builtins__
807 807 # Von: Alex Martelli <aleaxit@yahoo.com>
808 808 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
809 809 # Gruppen: comp.lang.python
810 810
811 811 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
812 812 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
813 813 # > <type 'dict'>
814 814 # > >>> print type(__builtins__)
815 815 # > <type 'module'>
816 816 # > Is this difference in return value intentional?
817 817
818 818 # Well, it's documented that '__builtins__' can be either a dictionary
819 819 # or a module, and it's been that way for a long time. Whether it's
820 820 # intentional (or sensible), I don't know. In any case, the idea is
821 821 # that if you need to access the built-in namespace directly, you
822 822 # should start with "import __builtin__" (note, no 's') which will
823 823 # definitely give you a module. Yeah, it's somewhat confusing:-(.
824 824
825 825 # These routines return properly built dicts as needed by the rest of
826 826 # the code, and can also be used by extension writers to generate
827 827 # properly initialized namespaces.
828 828 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
829 829 user_global_ns)
830 830
831 831 # Assign namespaces
832 832 # This is the namespace where all normal user variables live
833 833 self.user_ns = user_ns
834 834 self.user_global_ns = user_global_ns
835 835
836 836 # An auxiliary namespace that checks what parts of the user_ns were
837 837 # loaded at startup, so we can list later only variables defined in
838 838 # actual interactive use. Since it is always a subset of user_ns, it
839 839 # doesn't need to be separately tracked in the ns_table.
840 840 self.user_ns_hidden = {}
841 841
842 842 # A namespace to keep track of internal data structures to prevent
843 843 # them from cluttering user-visible stuff. Will be updated later
844 844 self.internal_ns = {}
845 845
846 846 # Now that FakeModule produces a real module, we've run into a nasty
847 847 # problem: after script execution (via %run), the module where the user
848 848 # code ran is deleted. Now that this object is a true module (needed
849 849 # so docetst and other tools work correctly), the Python module
850 850 # teardown mechanism runs over it, and sets to None every variable
851 851 # present in that module. Top-level references to objects from the
852 852 # script survive, because the user_ns is updated with them. However,
853 853 # calling functions defined in the script that use other things from
854 854 # the script will fail, because the function's closure had references
855 855 # to the original objects, which are now all None. So we must protect
856 856 # these modules from deletion by keeping a cache.
857 857 #
858 858 # To avoid keeping stale modules around (we only need the one from the
859 859 # last run), we use a dict keyed with the full path to the script, so
860 860 # only the last version of the module is held in the cache. Note,
861 861 # however, that we must cache the module *namespace contents* (their
862 862 # __dict__). Because if we try to cache the actual modules, old ones
863 863 # (uncached) could be destroyed while still holding references (such as
864 864 # those held by GUI objects that tend to be long-lived)>
865 865 #
866 866 # The %reset command will flush this cache. See the cache_main_mod()
867 867 # and clear_main_mod_cache() methods for details on use.
868 868
869 869 # This is the cache used for 'main' namespaces
870 870 self._main_ns_cache = {}
871 871 # And this is the single instance of FakeModule whose __dict__ we keep
872 872 # copying and clearing for reuse on each %run
873 873 self._user_main_module = FakeModule()
874 874
875 875 # A table holding all the namespaces IPython deals with, so that
876 876 # introspection facilities can search easily.
877 877 self.ns_table = {'user':user_ns,
878 878 'user_global':user_global_ns,
879 879 'internal':self.internal_ns,
880 880 'builtin':__builtin__.__dict__
881 881 }
882 882
883 883 # Similarly, track all namespaces where references can be held and that
884 884 # we can safely clear (so it can NOT include builtin). This one can be
885 885 # a simple list. Note that the main execution namespaces, user_ns and
886 886 # user_global_ns, can NOT be listed here, as clearing them blindly
887 887 # causes errors in object __del__ methods. Instead, the reset() method
888 888 # clears them manually and carefully.
889 889 self.ns_refs_table = [ self.user_ns_hidden,
890 890 self.internal_ns, self._main_ns_cache ]
891 891
892 892 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
893 893 """Return a valid local and global user interactive namespaces.
894 894
895 895 This builds a dict with the minimal information needed to operate as a
896 896 valid IPython user namespace, which you can pass to the various
897 897 embedding classes in ipython. The default implementation returns the
898 898 same dict for both the locals and the globals to allow functions to
899 899 refer to variables in the namespace. Customized implementations can
900 900 return different dicts. The locals dictionary can actually be anything
901 901 following the basic mapping protocol of a dict, but the globals dict
902 902 must be a true dict, not even a subclass. It is recommended that any
903 903 custom object for the locals namespace synchronize with the globals
904 904 dict somehow.
905 905
906 906 Raises TypeError if the provided globals namespace is not a true dict.
907 907
908 908 Parameters
909 909 ----------
910 910 user_ns : dict-like, optional
911 911 The current user namespace. The items in this namespace should
912 912 be included in the output. If None, an appropriate blank
913 913 namespace should be created.
914 914 user_global_ns : dict, optional
915 915 The current user global namespace. The items in this namespace
916 916 should be included in the output. If None, an appropriate
917 917 blank namespace should be created.
918 918
919 919 Returns
920 920 -------
921 921 A pair of dictionary-like object to be used as the local namespace
922 922 of the interpreter and a dict to be used as the global namespace.
923 923 """
924 924
925 925
926 926 # We must ensure that __builtin__ (without the final 's') is always
927 927 # available and pointing to the __builtin__ *module*. For more details:
928 928 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
929 929
930 930 if user_ns is None:
931 931 # Set __name__ to __main__ to better match the behavior of the
932 932 # normal interpreter.
933 933 user_ns = {'__name__' :'__main__',
934 934 '__builtin__' : __builtin__,
935 935 '__builtins__' : __builtin__,
936 936 }
937 937 else:
938 938 user_ns.setdefault('__name__','__main__')
939 939 user_ns.setdefault('__builtin__',__builtin__)
940 940 user_ns.setdefault('__builtins__',__builtin__)
941 941
942 942 if user_global_ns is None:
943 943 user_global_ns = user_ns
944 944 if type(user_global_ns) is not dict:
945 945 raise TypeError("user_global_ns must be a true dict; got %r"
946 946 % type(user_global_ns))
947 947
948 948 return user_ns, user_global_ns
949 949
950 950 def init_sys_modules(self):
951 951 # We need to insert into sys.modules something that looks like a
952 952 # module but which accesses the IPython namespace, for shelve and
953 953 # pickle to work interactively. Normally they rely on getting
954 954 # everything out of __main__, but for embedding purposes each IPython
955 955 # instance has its own private namespace, so we can't go shoving
956 956 # everything into __main__.
957 957
958 958 # note, however, that we should only do this for non-embedded
959 959 # ipythons, which really mimic the __main__.__dict__ with their own
960 960 # namespace. Embedded instances, on the other hand, should not do
961 961 # this because they need to manage the user local/global namespaces
962 962 # only, but they live within a 'normal' __main__ (meaning, they
963 963 # shouldn't overtake the execution environment of the script they're
964 964 # embedded in).
965 965
966 966 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
967 967
968 968 try:
969 969 main_name = self.user_ns['__name__']
970 970 except KeyError:
971 971 raise KeyError('user_ns dictionary MUST have a "__name__" key')
972 972 else:
973 973 sys.modules[main_name] = FakeModule(self.user_ns)
974 974
975 975 def init_user_ns(self):
976 976 """Initialize all user-visible namespaces to their minimum defaults.
977 977
978 978 Certain history lists are also initialized here, as they effectively
979 979 act as user namespaces.
980 980
981 981 Notes
982 982 -----
983 983 All data structures here are only filled in, they are NOT reset by this
984 984 method. If they were not empty before, data will simply be added to
985 985 therm.
986 986 """
987 987 # This function works in two parts: first we put a few things in
988 988 # user_ns, and we sync that contents into user_ns_hidden so that these
989 989 # initial variables aren't shown by %who. After the sync, we add the
990 990 # rest of what we *do* want the user to see with %who even on a new
991 991 # session (probably nothing, so theye really only see their own stuff)
992 992
993 993 # The user dict must *always* have a __builtin__ reference to the
994 994 # Python standard __builtin__ namespace, which must be imported.
995 995 # This is so that certain operations in prompt evaluation can be
996 996 # reliably executed with builtins. Note that we can NOT use
997 997 # __builtins__ (note the 's'), because that can either be a dict or a
998 998 # module, and can even mutate at runtime, depending on the context
999 999 # (Python makes no guarantees on it). In contrast, __builtin__ is
1000 1000 # always a module object, though it must be explicitly imported.
1001 1001
1002 1002 # For more details:
1003 1003 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1004 1004 ns = dict(__builtin__ = __builtin__)
1005 1005
1006 1006 # Put 'help' in the user namespace
1007 1007 try:
1008 1008 from site import _Helper
1009 1009 ns['help'] = _Helper()
1010 1010 except ImportError:
1011 1011 warn('help() not available - check site.py')
1012 1012
1013 1013 # make global variables for user access to the histories
1014 1014 ns['_ih'] = self.history_manager.input_hist_parsed
1015 1015 ns['_oh'] = self.history_manager.output_hist
1016 1016 ns['_dh'] = self.history_manager.dir_hist
1017 1017
1018 1018 ns['_sh'] = shadowns
1019 1019
1020 1020 # user aliases to input and output histories. These shouldn't show up
1021 1021 # in %who, as they can have very large reprs.
1022 1022 ns['In'] = self.history_manager.input_hist_parsed
1023 1023 ns['Out'] = self.history_manager.output_hist
1024 1024
1025 1025 # Store myself as the public api!!!
1026 1026 ns['get_ipython'] = self.get_ipython
1027 1027
1028 1028 ns['exit'] = self.exiter
1029 1029 ns['quit'] = self.exiter
1030 1030
1031 1031 # Sync what we've added so far to user_ns_hidden so these aren't seen
1032 1032 # by %who
1033 1033 self.user_ns_hidden.update(ns)
1034 1034
1035 1035 # Anything put into ns now would show up in %who. Think twice before
1036 1036 # putting anything here, as we really want %who to show the user their
1037 1037 # stuff, not our variables.
1038 1038
1039 1039 # Finally, update the real user's namespace
1040 1040 self.user_ns.update(ns)
1041 1041
1042 1042 def reset(self, new_session=True):
1043 1043 """Clear all internal namespaces.
1044 1044
1045 1045 Note that this is much more aggressive than %reset, since it clears
1046 1046 fully all namespaces, as well as all input/output lists.
1047 1047
1048 1048 If new_session is True, a new history session will be opened.
1049 1049 """
1050 1050 # Clear histories
1051 1051 self.history_manager.reset(new_session)
1052 1052 # Reset counter used to index all histories
1053 1053 if new_session:
1054 1054 self.execution_count = 1
1055 1055
1056 1056 # Flush cached output items
1057 1057 self.displayhook.flush()
1058 1058
1059 1059 # Restore the user namespaces to minimal usability
1060 1060 for ns in self.ns_refs_table:
1061 1061 ns.clear()
1062 1062
1063 1063 # The main execution namespaces must be cleared very carefully,
1064 1064 # skipping the deletion of the builtin-related keys, because doing so
1065 1065 # would cause errors in many object's __del__ methods.
1066 1066 for ns in [self.user_ns, self.user_global_ns]:
1067 1067 drop_keys = set(ns.keys())
1068 1068 drop_keys.discard('__builtin__')
1069 1069 drop_keys.discard('__builtins__')
1070 1070 for k in drop_keys:
1071 1071 del ns[k]
1072 1072
1073 1073 # Restore the user namespaces to minimal usability
1074 1074 self.init_user_ns()
1075 1075
1076 1076 # Restore the default and user aliases
1077 1077 self.alias_manager.clear_aliases()
1078 1078 self.alias_manager.init_aliases()
1079 1079
1080 1080 # Flush the private list of module references kept for script
1081 1081 # execution protection
1082 1082 self.clear_main_mod_cache()
1083 1083
1084 1084 def reset_selective(self, regex=None):
1085 1085 """Clear selective variables from internal namespaces based on a
1086 1086 specified regular expression.
1087 1087
1088 1088 Parameters
1089 1089 ----------
1090 1090 regex : string or compiled pattern, optional
1091 1091 A regular expression pattern that will be used in searching
1092 1092 variable names in the users namespaces.
1093 1093 """
1094 1094 if regex is not None:
1095 1095 try:
1096 1096 m = re.compile(regex)
1097 1097 except TypeError:
1098 1098 raise TypeError('regex must be a string or compiled pattern')
1099 1099 # Search for keys in each namespace that match the given regex
1100 1100 # If a match is found, delete the key/value pair.
1101 1101 for ns in self.ns_refs_table:
1102 1102 for var in ns:
1103 1103 if m.search(var):
1104 1104 del ns[var]
1105 1105
1106 1106 def push(self, variables, interactive=True):
1107 1107 """Inject a group of variables into the IPython user namespace.
1108 1108
1109 1109 Parameters
1110 1110 ----------
1111 1111 variables : dict, str or list/tuple of str
1112 1112 The variables to inject into the user's namespace. If a dict, a
1113 1113 simple update is done. If a str, the string is assumed to have
1114 1114 variable names separated by spaces. A list/tuple of str can also
1115 1115 be used to give the variable names. If just the variable names are
1116 1116 give (list/tuple/str) then the variable values looked up in the
1117 1117 callers frame.
1118 1118 interactive : bool
1119 1119 If True (default), the variables will be listed with the ``who``
1120 1120 magic.
1121 1121 """
1122 1122 vdict = None
1123 1123
1124 1124 # We need a dict of name/value pairs to do namespace updates.
1125 1125 if isinstance(variables, dict):
1126 1126 vdict = variables
1127 1127 elif isinstance(variables, (basestring, list, tuple)):
1128 1128 if isinstance(variables, basestring):
1129 1129 vlist = variables.split()
1130 1130 else:
1131 1131 vlist = variables
1132 1132 vdict = {}
1133 1133 cf = sys._getframe(1)
1134 1134 for name in vlist:
1135 1135 try:
1136 1136 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1137 1137 except:
1138 1138 print ('Could not get variable %s from %s' %
1139 1139 (name,cf.f_code.co_name))
1140 1140 else:
1141 1141 raise ValueError('variables must be a dict/str/list/tuple')
1142 1142
1143 1143 # Propagate variables to user namespace
1144 1144 self.user_ns.update(vdict)
1145 1145
1146 1146 # And configure interactive visibility
1147 1147 config_ns = self.user_ns_hidden
1148 1148 if interactive:
1149 1149 for name, val in vdict.iteritems():
1150 1150 config_ns.pop(name, None)
1151 1151 else:
1152 1152 for name,val in vdict.iteritems():
1153 1153 config_ns[name] = val
1154 1154
1155 1155 #-------------------------------------------------------------------------
1156 1156 # Things related to object introspection
1157 1157 #-------------------------------------------------------------------------
1158 1158
1159 1159 def _ofind(self, oname, namespaces=None):
1160 1160 """Find an object in the available namespaces.
1161 1161
1162 1162 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1163 1163
1164 1164 Has special code to detect magic functions.
1165 1165 """
1166 1166 #oname = oname.strip()
1167 1167 #print '1- oname: <%r>' % oname # dbg
1168 1168 try:
1169 1169 oname = oname.strip().encode('ascii')
1170 1170 #print '2- oname: <%r>' % oname # dbg
1171 1171 except UnicodeEncodeError:
1172 1172 print 'Python identifiers can only contain ascii characters.'
1173 1173 return dict(found=False)
1174 1174
1175 1175 alias_ns = None
1176 1176 if namespaces is None:
1177 1177 # Namespaces to search in:
1178 1178 # Put them in a list. The order is important so that we
1179 1179 # find things in the same order that Python finds them.
1180 1180 namespaces = [ ('Interactive', self.user_ns),
1181 1181 ('IPython internal', self.internal_ns),
1182 1182 ('Python builtin', __builtin__.__dict__),
1183 1183 ('Alias', self.alias_manager.alias_table),
1184 1184 ]
1185 1185 alias_ns = self.alias_manager.alias_table
1186 1186
1187 1187 # initialize results to 'null'
1188 1188 found = False; obj = None; ospace = None; ds = None;
1189 1189 ismagic = False; isalias = False; parent = None
1190 1190
1191 1191 # We need to special-case 'print', which as of python2.6 registers as a
1192 1192 # function but should only be treated as one if print_function was
1193 1193 # loaded with a future import. In this case, just bail.
1194 1194 if (oname == 'print' and not (self.compile.compiler_flags &
1195 1195 __future__.CO_FUTURE_PRINT_FUNCTION)):
1196 1196 return {'found':found, 'obj':obj, 'namespace':ospace,
1197 1197 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1198 1198
1199 1199 # Look for the given name by splitting it in parts. If the head is
1200 1200 # found, then we look for all the remaining parts as members, and only
1201 1201 # declare success if we can find them all.
1202 1202 oname_parts = oname.split('.')
1203 1203 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1204 1204 for nsname,ns in namespaces:
1205 1205 try:
1206 1206 obj = ns[oname_head]
1207 1207 except KeyError:
1208 1208 continue
1209 1209 else:
1210 1210 #print 'oname_rest:', oname_rest # dbg
1211 1211 for part in oname_rest:
1212 1212 try:
1213 1213 parent = obj
1214 1214 obj = getattr(obj,part)
1215 1215 except:
1216 1216 # Blanket except b/c some badly implemented objects
1217 1217 # allow __getattr__ to raise exceptions other than
1218 1218 # AttributeError, which then crashes IPython.
1219 1219 break
1220 1220 else:
1221 1221 # If we finish the for loop (no break), we got all members
1222 1222 found = True
1223 1223 ospace = nsname
1224 1224 if ns == alias_ns:
1225 1225 isalias = True
1226 1226 break # namespace loop
1227 1227
1228 1228 # Try to see if it's magic
1229 1229 if not found:
1230 1230 if oname.startswith(ESC_MAGIC):
1231 1231 oname = oname[1:]
1232 1232 obj = getattr(self,'magic_'+oname,None)
1233 1233 if obj is not None:
1234 1234 found = True
1235 1235 ospace = 'IPython internal'
1236 1236 ismagic = True
1237 1237
1238 1238 # Last try: special-case some literals like '', [], {}, etc:
1239 1239 if not found and oname_head in ["''",'""','[]','{}','()']:
1240 1240 obj = eval(oname_head)
1241 1241 found = True
1242 1242 ospace = 'Interactive'
1243 1243
1244 1244 return {'found':found, 'obj':obj, 'namespace':ospace,
1245 1245 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1246 1246
1247 1247 def _ofind_property(self, oname, info):
1248 1248 """Second part of object finding, to look for property details."""
1249 1249 if info.found:
1250 1250 # Get the docstring of the class property if it exists.
1251 1251 path = oname.split('.')
1252 1252 root = '.'.join(path[:-1])
1253 1253 if info.parent is not None:
1254 1254 try:
1255 1255 target = getattr(info.parent, '__class__')
1256 1256 # The object belongs to a class instance.
1257 1257 try:
1258 1258 target = getattr(target, path[-1])
1259 1259 # The class defines the object.
1260 1260 if isinstance(target, property):
1261 1261 oname = root + '.__class__.' + path[-1]
1262 1262 info = Struct(self._ofind(oname))
1263 1263 except AttributeError: pass
1264 1264 except AttributeError: pass
1265 1265
1266 1266 # We return either the new info or the unmodified input if the object
1267 1267 # hadn't been found
1268 1268 return info
1269 1269
1270 1270 def _object_find(self, oname, namespaces=None):
1271 1271 """Find an object and return a struct with info about it."""
1272 1272 inf = Struct(self._ofind(oname, namespaces))
1273 1273 return Struct(self._ofind_property(oname, inf))
1274 1274
1275 1275 def _inspect(self, meth, oname, namespaces=None, **kw):
1276 1276 """Generic interface to the inspector system.
1277 1277
1278 1278 This function is meant to be called by pdef, pdoc & friends."""
1279 1279 info = self._object_find(oname)
1280 1280 if info.found:
1281 1281 pmethod = getattr(self.inspector, meth)
1282 1282 formatter = format_screen if info.ismagic else None
1283 1283 if meth == 'pdoc':
1284 1284 pmethod(info.obj, oname, formatter)
1285 1285 elif meth == 'pinfo':
1286 1286 pmethod(info.obj, oname, formatter, info, **kw)
1287 1287 else:
1288 1288 pmethod(info.obj, oname)
1289 1289 else:
1290 1290 print 'Object `%s` not found.' % oname
1291 1291 return 'not found' # so callers can take other action
1292 1292
1293 1293 def object_inspect(self, oname):
1294 1294 with self.builtin_trap:
1295 1295 info = self._object_find(oname)
1296 1296 if info.found:
1297 1297 return self.inspector.info(info.obj, oname, info=info)
1298 1298 else:
1299 1299 return oinspect.object_info(name=oname, found=False)
1300 1300
1301 1301 #-------------------------------------------------------------------------
1302 1302 # Things related to history management
1303 1303 #-------------------------------------------------------------------------
1304 1304
1305 1305 def init_history(self):
1306 1306 """Sets up the command history, and starts regular autosaves."""
1307 1307 self.history_manager = HistoryManager(shell=self, config=self.config)
1308 1308
1309 1309 #-------------------------------------------------------------------------
1310 1310 # Things related to exception handling and tracebacks (not debugging)
1311 1311 #-------------------------------------------------------------------------
1312 1312
1313 1313 def init_traceback_handlers(self, custom_exceptions):
1314 1314 # Syntax error handler.
1315 1315 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1316 1316
1317 1317 # The interactive one is initialized with an offset, meaning we always
1318 1318 # want to remove the topmost item in the traceback, which is our own
1319 1319 # internal code. Valid modes: ['Plain','Context','Verbose']
1320 1320 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1321 1321 color_scheme='NoColor',
1322 1322 tb_offset = 1,
1323 1323 check_cache=self.compile.check_cache)
1324 1324
1325 1325 # The instance will store a pointer to the system-wide exception hook,
1326 1326 # so that runtime code (such as magics) can access it. This is because
1327 1327 # during the read-eval loop, it may get temporarily overwritten.
1328 1328 self.sys_excepthook = sys.excepthook
1329 1329
1330 1330 # and add any custom exception handlers the user may have specified
1331 1331 self.set_custom_exc(*custom_exceptions)
1332 1332
1333 1333 # Set the exception mode
1334 1334 self.InteractiveTB.set_mode(mode=self.xmode)
1335 1335
1336 1336 def set_custom_exc(self, exc_tuple, handler):
1337 1337 """set_custom_exc(exc_tuple,handler)
1338 1338
1339 1339 Set a custom exception handler, which will be called if any of the
1340 1340 exceptions in exc_tuple occur in the mainloop (specifically, in the
1341 1341 run_code() method.
1342 1342
1343 1343 Inputs:
1344 1344
1345 1345 - exc_tuple: a *tuple* of valid exceptions to call the defined
1346 1346 handler for. It is very important that you use a tuple, and NOT A
1347 1347 LIST here, because of the way Python's except statement works. If
1348 1348 you only want to trap a single exception, use a singleton tuple:
1349 1349
1350 1350 exc_tuple == (MyCustomException,)
1351 1351
1352 1352 - handler: this must be defined as a function with the following
1353 1353 basic interface::
1354 1354
1355 1355 def my_handler(self, etype, value, tb, tb_offset=None)
1356 1356 ...
1357 1357 # The return value must be
1358 1358 return structured_traceback
1359 1359
1360 1360 This will be made into an instance method (via types.MethodType)
1361 1361 of IPython itself, and it will be called if any of the exceptions
1362 1362 listed in the exc_tuple are caught. If the handler is None, an
1363 1363 internal basic one is used, which just prints basic info.
1364 1364
1365 1365 WARNING: by putting in your own exception handler into IPython's main
1366 1366 execution loop, you run a very good chance of nasty crashes. This
1367 1367 facility should only be used if you really know what you are doing."""
1368 1368
1369 1369 assert type(exc_tuple)==type(()) , \
1370 1370 "The custom exceptions must be given AS A TUPLE."
1371 1371
1372 1372 def dummy_handler(self,etype,value,tb):
1373 1373 print '*** Simple custom exception handler ***'
1374 1374 print 'Exception type :',etype
1375 1375 print 'Exception value:',value
1376 1376 print 'Traceback :',tb
1377 1377 print 'Source code :','\n'.join(self.buffer)
1378 1378
1379 1379 if handler is None: handler = dummy_handler
1380 1380
1381 1381 self.CustomTB = types.MethodType(handler,self)
1382 1382 self.custom_exceptions = exc_tuple
1383 1383
1384 1384 def excepthook(self, etype, value, tb):
1385 1385 """One more defense for GUI apps that call sys.excepthook.
1386 1386
1387 1387 GUI frameworks like wxPython trap exceptions and call
1388 1388 sys.excepthook themselves. I guess this is a feature that
1389 1389 enables them to keep running after exceptions that would
1390 1390 otherwise kill their mainloop. This is a bother for IPython
1391 1391 which excepts to catch all of the program exceptions with a try:
1392 1392 except: statement.
1393 1393
1394 1394 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1395 1395 any app directly invokes sys.excepthook, it will look to the user like
1396 1396 IPython crashed. In order to work around this, we can disable the
1397 1397 CrashHandler and replace it with this excepthook instead, which prints a
1398 1398 regular traceback using our InteractiveTB. In this fashion, apps which
1399 1399 call sys.excepthook will generate a regular-looking exception from
1400 1400 IPython, and the CrashHandler will only be triggered by real IPython
1401 1401 crashes.
1402 1402
1403 1403 This hook should be used sparingly, only in places which are not likely
1404 1404 to be true IPython errors.
1405 1405 """
1406 1406 self.showtraceback((etype,value,tb),tb_offset=0)
1407 1407
1408 1408 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1409 1409 exception_only=False):
1410 1410 """Display the exception that just occurred.
1411 1411
1412 1412 If nothing is known about the exception, this is the method which
1413 1413 should be used throughout the code for presenting user tracebacks,
1414 1414 rather than directly invoking the InteractiveTB object.
1415 1415
1416 1416 A specific showsyntaxerror() also exists, but this method can take
1417 1417 care of calling it if needed, so unless you are explicitly catching a
1418 1418 SyntaxError exception, don't try to analyze the stack manually and
1419 1419 simply call this method."""
1420 1420
1421 1421 try:
1422 1422 if exc_tuple is None:
1423 1423 etype, value, tb = sys.exc_info()
1424 1424 else:
1425 1425 etype, value, tb = exc_tuple
1426 1426
1427 1427 if etype is None:
1428 1428 if hasattr(sys, 'last_type'):
1429 1429 etype, value, tb = sys.last_type, sys.last_value, \
1430 1430 sys.last_traceback
1431 1431 else:
1432 1432 self.write_err('No traceback available to show.\n')
1433 1433 return
1434 1434
1435 1435 if etype is SyntaxError:
1436 1436 # Though this won't be called by syntax errors in the input
1437 1437 # line, there may be SyntaxError cases whith imported code.
1438 1438 self.showsyntaxerror(filename)
1439 1439 elif etype is UsageError:
1440 1440 print "UsageError:", value
1441 1441 else:
1442 1442 # WARNING: these variables are somewhat deprecated and not
1443 1443 # necessarily safe to use in a threaded environment, but tools
1444 1444 # like pdb depend on their existence, so let's set them. If we
1445 1445 # find problems in the field, we'll need to revisit their use.
1446 1446 sys.last_type = etype
1447 1447 sys.last_value = value
1448 1448 sys.last_traceback = tb
1449 1449 if etype in self.custom_exceptions:
1450 1450 # FIXME: Old custom traceback objects may just return a
1451 1451 # string, in that case we just put it into a list
1452 1452 stb = self.CustomTB(etype, value, tb, tb_offset)
1453 1453 if isinstance(ctb, basestring):
1454 1454 stb = [stb]
1455 1455 else:
1456 1456 if exception_only:
1457 1457 stb = ['An exception has occurred, use %tb to see '
1458 1458 'the full traceback.\n']
1459 1459 stb.extend(self.InteractiveTB.get_exception_only(etype,
1460 1460 value))
1461 1461 else:
1462 1462 stb = self.InteractiveTB.structured_traceback(etype,
1463 1463 value, tb, tb_offset=tb_offset)
1464 1464
1465 1465 if self.call_pdb:
1466 1466 # drop into debugger
1467 1467 self.debugger(force=True)
1468 1468
1469 1469 # Actually show the traceback
1470 1470 self._showtraceback(etype, value, stb)
1471 1471
1472 1472 except KeyboardInterrupt:
1473 1473 self.write_err("\nKeyboardInterrupt\n")
1474 1474
1475 1475 def _showtraceback(self, etype, evalue, stb):
1476 1476 """Actually show a traceback.
1477 1477
1478 1478 Subclasses may override this method to put the traceback on a different
1479 1479 place, like a side channel.
1480 1480 """
1481 1481 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1482 1482
1483 1483 def showsyntaxerror(self, filename=None):
1484 1484 """Display the syntax error that just occurred.
1485 1485
1486 1486 This doesn't display a stack trace because there isn't one.
1487 1487
1488 1488 If a filename is given, it is stuffed in the exception instead
1489 1489 of what was there before (because Python's parser always uses
1490 1490 "<string>" when reading from a string).
1491 1491 """
1492 1492 etype, value, last_traceback = sys.exc_info()
1493 1493
1494 1494 # See note about these variables in showtraceback() above
1495 1495 sys.last_type = etype
1496 1496 sys.last_value = value
1497 1497 sys.last_traceback = last_traceback
1498 1498
1499 1499 if filename and etype is SyntaxError:
1500 1500 # Work hard to stuff the correct filename in the exception
1501 1501 try:
1502 1502 msg, (dummy_filename, lineno, offset, line) = value
1503 1503 except:
1504 1504 # Not the format we expect; leave it alone
1505 1505 pass
1506 1506 else:
1507 1507 # Stuff in the right filename
1508 1508 try:
1509 1509 # Assume SyntaxError is a class exception
1510 1510 value = SyntaxError(msg, (filename, lineno, offset, line))
1511 1511 except:
1512 1512 # If that failed, assume SyntaxError is a string
1513 1513 value = msg, (filename, lineno, offset, line)
1514 1514 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1515 1515 self._showtraceback(etype, value, stb)
1516 1516
1517 1517 #-------------------------------------------------------------------------
1518 1518 # Things related to readline
1519 1519 #-------------------------------------------------------------------------
1520 1520
1521 1521 def init_readline(self):
1522 1522 """Command history completion/saving/reloading."""
1523 1523
1524 1524 if self.readline_use:
1525 1525 import IPython.utils.rlineimpl as readline
1526 1526
1527 1527 self.rl_next_input = None
1528 1528 self.rl_do_indent = False
1529 1529
1530 1530 if not self.readline_use or not readline.have_readline:
1531 1531 self.has_readline = False
1532 1532 self.readline = None
1533 1533 # Set a number of methods that depend on readline to be no-op
1534 1534 self.set_readline_completer = no_op
1535 1535 self.set_custom_completer = no_op
1536 1536 self.set_completer_frame = no_op
1537 1537 warn('Readline services not available or not loaded.')
1538 1538 else:
1539 1539 self.has_readline = True
1540 1540 self.readline = readline
1541 1541 sys.modules['readline'] = readline
1542 1542
1543 1543 # Platform-specific configuration
1544 1544 if os.name == 'nt':
1545 1545 # FIXME - check with Frederick to see if we can harmonize
1546 1546 # naming conventions with pyreadline to avoid this
1547 1547 # platform-dependent check
1548 1548 self.readline_startup_hook = readline.set_pre_input_hook
1549 1549 else:
1550 1550 self.readline_startup_hook = readline.set_startup_hook
1551 1551
1552 1552 # Load user's initrc file (readline config)
1553 1553 # Or if libedit is used, load editrc.
1554 1554 inputrc_name = os.environ.get('INPUTRC')
1555 1555 if inputrc_name is None:
1556 1556 home_dir = get_home_dir()
1557 1557 if home_dir is not None:
1558 1558 inputrc_name = '.inputrc'
1559 1559 if readline.uses_libedit:
1560 1560 inputrc_name = '.editrc'
1561 1561 inputrc_name = os.path.join(home_dir, inputrc_name)
1562 1562 if os.path.isfile(inputrc_name):
1563 1563 try:
1564 1564 readline.read_init_file(inputrc_name)
1565 1565 except:
1566 1566 warn('Problems reading readline initialization file <%s>'
1567 1567 % inputrc_name)
1568 1568
1569 1569 # Configure readline according to user's prefs
1570 1570 # This is only done if GNU readline is being used. If libedit
1571 1571 # is being used (as on Leopard) the readline config is
1572 1572 # not run as the syntax for libedit is different.
1573 1573 if not readline.uses_libedit:
1574 1574 for rlcommand in self.readline_parse_and_bind:
1575 1575 #print "loading rl:",rlcommand # dbg
1576 1576 readline.parse_and_bind(rlcommand)
1577 1577
1578 1578 # Remove some chars from the delimiters list. If we encounter
1579 1579 # unicode chars, discard them.
1580 1580 delims = readline.get_completer_delims().encode("ascii", "ignore")
1581 1581 delims = delims.translate(None, self.readline_remove_delims)
1582 1582 delims = delims.replace(ESC_MAGIC, '')
1583 1583 readline.set_completer_delims(delims)
1584 1584 # otherwise we end up with a monster history after a while:
1585 1585 readline.set_history_length(self.history_length)
1586 1586
1587 1587 self.refill_readline_hist()
1588 1588 self.readline_no_record = ReadlineNoRecord(self)
1589 1589
1590 1590 # Configure auto-indent for all platforms
1591 1591 self.set_autoindent(self.autoindent)
1592 1592
1593 1593 def refill_readline_hist(self):
1594 1594 # Load the last 1000 lines from history
1595 1595 self.readline.clear_history()
1596 1596 stdin_encoding = sys.stdin.encoding or "utf-8"
1597 1597 for _, _, cell in self.history_manager.get_tail(1000,
1598 1598 include_latest=True):
1599 1599 if cell.strip(): # Ignore blank lines
1600 1600 for line in cell.splitlines():
1601 1601 self.readline.add_history(line.encode(stdin_encoding))
1602 1602
1603 1603 def set_next_input(self, s):
1604 1604 """ Sets the 'default' input string for the next command line.
1605 1605
1606 1606 Requires readline.
1607 1607
1608 1608 Example:
1609 1609
1610 1610 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1611 1611 [D:\ipython]|2> Hello Word_ # cursor is here
1612 1612 """
1613 1613
1614 1614 self.rl_next_input = s
1615 1615
1616 1616 # Maybe move this to the terminal subclass?
1617 1617 def pre_readline(self):
1618 1618 """readline hook to be used at the start of each line.
1619 1619
1620 1620 Currently it handles auto-indent only."""
1621 1621
1622 1622 if self.rl_do_indent:
1623 1623 self.readline.insert_text(self._indent_current_str())
1624 1624 if self.rl_next_input is not None:
1625 1625 self.readline.insert_text(self.rl_next_input)
1626 1626 self.rl_next_input = None
1627 1627
1628 1628 def _indent_current_str(self):
1629 1629 """return the current level of indentation as a string"""
1630 1630 return self.input_splitter.indent_spaces * ' '
1631 1631
1632 1632 #-------------------------------------------------------------------------
1633 1633 # Things related to text completion
1634 1634 #-------------------------------------------------------------------------
1635 1635
1636 1636 def init_completer(self):
1637 1637 """Initialize the completion machinery.
1638 1638
1639 1639 This creates completion machinery that can be used by client code,
1640 1640 either interactively in-process (typically triggered by the readline
1641 1641 library), programatically (such as in test suites) or out-of-prcess
1642 1642 (typically over the network by remote frontends).
1643 1643 """
1644 1644 from IPython.core.completer import IPCompleter
1645 1645 from IPython.core.completerlib import (module_completer,
1646 1646 magic_run_completer, cd_completer)
1647 1647
1648 1648 self.Completer = IPCompleter(self,
1649 1649 self.user_ns,
1650 1650 self.user_global_ns,
1651 1651 self.readline_omit__names,
1652 1652 self.alias_manager.alias_table,
1653 1653 self.has_readline)
1654 1654
1655 1655 # Add custom completers to the basic ones built into IPCompleter
1656 1656 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1657 1657 self.strdispatchers['complete_command'] = sdisp
1658 1658 self.Completer.custom_completers = sdisp
1659 1659
1660 1660 self.set_hook('complete_command', module_completer, str_key = 'import')
1661 1661 self.set_hook('complete_command', module_completer, str_key = 'from')
1662 1662 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1663 1663 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1664 1664
1665 1665 # Only configure readline if we truly are using readline. IPython can
1666 1666 # do tab-completion over the network, in GUIs, etc, where readline
1667 1667 # itself may be absent
1668 1668 if self.has_readline:
1669 1669 self.set_readline_completer()
1670 1670
1671 1671 def complete(self, text, line=None, cursor_pos=None):
1672 1672 """Return the completed text and a list of completions.
1673 1673
1674 1674 Parameters
1675 1675 ----------
1676 1676
1677 1677 text : string
1678 1678 A string of text to be completed on. It can be given as empty and
1679 1679 instead a line/position pair are given. In this case, the
1680 1680 completer itself will split the line like readline does.
1681 1681
1682 1682 line : string, optional
1683 1683 The complete line that text is part of.
1684 1684
1685 1685 cursor_pos : int, optional
1686 1686 The position of the cursor on the input line.
1687 1687
1688 1688 Returns
1689 1689 -------
1690 1690 text : string
1691 1691 The actual text that was completed.
1692 1692
1693 1693 matches : list
1694 1694 A sorted list with all possible completions.
1695 1695
1696 1696 The optional arguments allow the completion to take more context into
1697 1697 account, and are part of the low-level completion API.
1698 1698
1699 1699 This is a wrapper around the completion mechanism, similar to what
1700 1700 readline does at the command line when the TAB key is hit. By
1701 1701 exposing it as a method, it can be used by other non-readline
1702 1702 environments (such as GUIs) for text completion.
1703 1703
1704 1704 Simple usage example:
1705 1705
1706 1706 In [1]: x = 'hello'
1707 1707
1708 1708 In [2]: _ip.complete('x.l')
1709 1709 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1710 1710 """
1711 1711
1712 1712 # Inject names into __builtin__ so we can complete on the added names.
1713 1713 with self.builtin_trap:
1714 1714 return self.Completer.complete(text, line, cursor_pos)
1715 1715
1716 1716 def set_custom_completer(self, completer, pos=0):
1717 1717 """Adds a new custom completer function.
1718 1718
1719 1719 The position argument (defaults to 0) is the index in the completers
1720 1720 list where you want the completer to be inserted."""
1721 1721
1722 1722 newcomp = types.MethodType(completer,self.Completer)
1723 1723 self.Completer.matchers.insert(pos,newcomp)
1724 1724
1725 1725 def set_readline_completer(self):
1726 1726 """Reset readline's completer to be our own."""
1727 1727 self.readline.set_completer(self.Completer.rlcomplete)
1728 1728
1729 1729 def set_completer_frame(self, frame=None):
1730 1730 """Set the frame of the completer."""
1731 1731 if frame:
1732 1732 self.Completer.namespace = frame.f_locals
1733 1733 self.Completer.global_namespace = frame.f_globals
1734 1734 else:
1735 1735 self.Completer.namespace = self.user_ns
1736 1736 self.Completer.global_namespace = self.user_global_ns
1737 1737
1738 1738 #-------------------------------------------------------------------------
1739 1739 # Things related to magics
1740 1740 #-------------------------------------------------------------------------
1741 1741
1742 1742 def init_magics(self):
1743 1743 # FIXME: Move the color initialization to the DisplayHook, which
1744 1744 # should be split into a prompt manager and displayhook. We probably
1745 1745 # even need a centralize colors management object.
1746 1746 self.magic_colors(self.colors)
1747 1747 # History was moved to a separate module
1748 1748 from . import history
1749 1749 history.init_ipython(self)
1750 1750
1751 1751 def magic(self,arg_s):
1752 1752 """Call a magic function by name.
1753 1753
1754 1754 Input: a string containing the name of the magic function to call and
1755 1755 any additional arguments to be passed to the magic.
1756 1756
1757 1757 magic('name -opt foo bar') is equivalent to typing at the ipython
1758 1758 prompt:
1759 1759
1760 1760 In[1]: %name -opt foo bar
1761 1761
1762 1762 To call a magic without arguments, simply use magic('name').
1763 1763
1764 1764 This provides a proper Python function to call IPython's magics in any
1765 1765 valid Python code you can type at the interpreter, including loops and
1766 1766 compound statements.
1767 1767 """
1768 1768 args = arg_s.split(' ',1)
1769 1769 magic_name = args[0]
1770 1770 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1771 1771
1772 1772 try:
1773 1773 magic_args = args[1]
1774 1774 except IndexError:
1775 1775 magic_args = ''
1776 1776 fn = getattr(self,'magic_'+magic_name,None)
1777 1777 if fn is None:
1778 1778 error("Magic function `%s` not found." % magic_name)
1779 1779 else:
1780 1780 magic_args = self.var_expand(magic_args,1)
1781 1781 # Grab local namespace if we need it:
1782 1782 if getattr(fn, "needs_local_scope", False):
1783 1783 self._magic_locals = sys._getframe(1).f_locals
1784 1784 with self.builtin_trap:
1785 1785 result = fn(magic_args)
1786 1786 # Ensure we're not keeping object references around:
1787 1787 self._magic_locals = {}
1788 1788 return result
1789 1789
1790 1790 def define_magic(self, magicname, func):
1791 1791 """Expose own function as magic function for ipython
1792 1792
1793 1793 def foo_impl(self,parameter_s=''):
1794 1794 'My very own magic!. (Use docstrings, IPython reads them).'
1795 1795 print 'Magic function. Passed parameter is between < >:'
1796 1796 print '<%s>' % parameter_s
1797 1797 print 'The self object is:',self
1798 1798
1799 1799 self.define_magic('foo',foo_impl)
1800 1800 """
1801 1801
1802 1802 import new
1803 1803 im = types.MethodType(func,self)
1804 1804 old = getattr(self, "magic_" + magicname, None)
1805 1805 setattr(self, "magic_" + magicname, im)
1806 1806 return old
1807 1807
1808 1808 #-------------------------------------------------------------------------
1809 1809 # Things related to macros
1810 1810 #-------------------------------------------------------------------------
1811 1811
1812 1812 def define_macro(self, name, themacro):
1813 1813 """Define a new macro
1814 1814
1815 1815 Parameters
1816 1816 ----------
1817 1817 name : str
1818 1818 The name of the macro.
1819 1819 themacro : str or Macro
1820 1820 The action to do upon invoking the macro. If a string, a new
1821 1821 Macro object is created by passing the string to it.
1822 1822 """
1823 1823
1824 1824 from IPython.core import macro
1825 1825
1826 1826 if isinstance(themacro, basestring):
1827 1827 themacro = macro.Macro(themacro)
1828 1828 if not isinstance(themacro, macro.Macro):
1829 1829 raise ValueError('A macro must be a string or a Macro instance.')
1830 1830 self.user_ns[name] = themacro
1831 1831
1832 1832 #-------------------------------------------------------------------------
1833 1833 # Things related to the running of system commands
1834 1834 #-------------------------------------------------------------------------
1835 1835
1836 1836 def system(self, cmd):
1837 1837 """Call the given cmd in a subprocess.
1838 1838
1839 1839 Parameters
1840 1840 ----------
1841 1841 cmd : str
1842 1842 Command to execute (can not end in '&', as bacground processes are
1843 1843 not supported.
1844 1844 """
1845 1845 # We do not support backgrounding processes because we either use
1846 1846 # pexpect or pipes to read from. Users can always just call
1847 1847 # os.system() if they really want a background process.
1848 1848 if cmd.endswith('&'):
1849 1849 raise OSError("Background processes not supported.")
1850 1850
1851 1851 return system(self.var_expand(cmd, depth=2))
1852 1852
1853 1853 def getoutput(self, cmd, split=True):
1854 1854 """Get output (possibly including stderr) from a subprocess.
1855 1855
1856 1856 Parameters
1857 1857 ----------
1858 1858 cmd : str
1859 1859 Command to execute (can not end in '&', as background processes are
1860 1860 not supported.
1861 1861 split : bool, optional
1862 1862
1863 1863 If True, split the output into an IPython SList. Otherwise, an
1864 1864 IPython LSString is returned. These are objects similar to normal
1865 1865 lists and strings, with a few convenience attributes for easier
1866 1866 manipulation of line-based output. You can use '?' on them for
1867 1867 details.
1868 1868 """
1869 1869 if cmd.endswith('&'):
1870 1870 raise OSError("Background processes not supported.")
1871 1871 out = getoutput(self.var_expand(cmd, depth=2))
1872 1872 if split:
1873 1873 out = SList(out.splitlines())
1874 1874 else:
1875 1875 out = LSString(out)
1876 1876 return out
1877 1877
1878 1878 #-------------------------------------------------------------------------
1879 1879 # Things related to aliases
1880 1880 #-------------------------------------------------------------------------
1881 1881
1882 1882 def init_alias(self):
1883 1883 self.alias_manager = AliasManager(shell=self, config=self.config)
1884 1884 self.ns_table['alias'] = self.alias_manager.alias_table,
1885 1885
1886 1886 #-------------------------------------------------------------------------
1887 1887 # Things related to extensions and plugins
1888 1888 #-------------------------------------------------------------------------
1889 1889
1890 1890 def init_extension_manager(self):
1891 1891 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1892 1892
1893 1893 def init_plugin_manager(self):
1894 1894 self.plugin_manager = PluginManager(config=self.config)
1895 1895
1896 1896 #-------------------------------------------------------------------------
1897 1897 # Things related to payloads
1898 1898 #-------------------------------------------------------------------------
1899 1899
1900 1900 def init_payload(self):
1901 1901 self.payload_manager = PayloadManager(config=self.config)
1902 1902
1903 1903 #-------------------------------------------------------------------------
1904 1904 # Things related to the prefilter
1905 1905 #-------------------------------------------------------------------------
1906 1906
1907 1907 def init_prefilter(self):
1908 1908 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1909 1909 # Ultimately this will be refactored in the new interpreter code, but
1910 1910 # for now, we should expose the main prefilter method (there's legacy
1911 1911 # code out there that may rely on this).
1912 1912 self.prefilter = self.prefilter_manager.prefilter_lines
1913 1913
1914 1914 def auto_rewrite_input(self, cmd):
1915 1915 """Print to the screen the rewritten form of the user's command.
1916 1916
1917 1917 This shows visual feedback by rewriting input lines that cause
1918 1918 automatic calling to kick in, like::
1919 1919
1920 1920 /f x
1921 1921
1922 1922 into::
1923 1923
1924 1924 ------> f(x)
1925 1925
1926 1926 after the user's input prompt. This helps the user understand that the
1927 1927 input line was transformed automatically by IPython.
1928 1928 """
1929 1929 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1930 1930
1931 1931 try:
1932 1932 # plain ascii works better w/ pyreadline, on some machines, so
1933 1933 # we use it and only print uncolored rewrite if we have unicode
1934 1934 rw = str(rw)
1935 1935 print >> IPython.utils.io.Term.cout, rw
1936 1936 except UnicodeEncodeError:
1937 1937 print "------> " + cmd
1938 1938
1939 1939 #-------------------------------------------------------------------------
1940 1940 # Things related to extracting values/expressions from kernel and user_ns
1941 1941 #-------------------------------------------------------------------------
1942 1942
1943 1943 def _simple_error(self):
1944 1944 etype, value = sys.exc_info()[:2]
1945 1945 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1946 1946
1947 1947 def user_variables(self, names):
1948 1948 """Get a list of variable names from the user's namespace.
1949 1949
1950 1950 Parameters
1951 1951 ----------
1952 1952 names : list of strings
1953 1953 A list of names of variables to be read from the user namespace.
1954 1954
1955 1955 Returns
1956 1956 -------
1957 1957 A dict, keyed by the input names and with the repr() of each value.
1958 1958 """
1959 1959 out = {}
1960 1960 user_ns = self.user_ns
1961 1961 for varname in names:
1962 1962 try:
1963 1963 value = repr(user_ns[varname])
1964 1964 except:
1965 1965 value = self._simple_error()
1966 1966 out[varname] = value
1967 1967 return out
1968 1968
1969 1969 def user_expressions(self, expressions):
1970 1970 """Evaluate a dict of expressions in the user's namespace.
1971 1971
1972 1972 Parameters
1973 1973 ----------
1974 1974 expressions : dict
1975 1975 A dict with string keys and string values. The expression values
1976 1976 should be valid Python expressions, each of which will be evaluated
1977 1977 in the user namespace.
1978 1978
1979 1979 Returns
1980 1980 -------
1981 1981 A dict, keyed like the input expressions dict, with the repr() of each
1982 1982 value.
1983 1983 """
1984 1984 out = {}
1985 1985 user_ns = self.user_ns
1986 1986 global_ns = self.user_global_ns
1987 1987 for key, expr in expressions.iteritems():
1988 1988 try:
1989 1989 value = repr(eval(expr, global_ns, user_ns))
1990 1990 except:
1991 1991 value = self._simple_error()
1992 1992 out[key] = value
1993 1993 return out
1994 1994
1995 1995 #-------------------------------------------------------------------------
1996 1996 # Things related to the running of code
1997 1997 #-------------------------------------------------------------------------
1998 1998
1999 1999 def ex(self, cmd):
2000 2000 """Execute a normal python statement in user namespace."""
2001 2001 with self.builtin_trap:
2002 2002 exec cmd in self.user_global_ns, self.user_ns
2003 2003
2004 2004 def ev(self, expr):
2005 2005 """Evaluate python expression expr in user namespace.
2006 2006
2007 2007 Returns the result of evaluation
2008 2008 """
2009 2009 with self.builtin_trap:
2010 2010 return eval(expr, self.user_global_ns, self.user_ns)
2011 2011
2012 2012 def safe_execfile(self, fname, *where, **kw):
2013 2013 """A safe version of the builtin execfile().
2014 2014
2015 2015 This version will never throw an exception, but instead print
2016 2016 helpful error messages to the screen. This only works on pure
2017 2017 Python files with the .py extension.
2018 2018
2019 2019 Parameters
2020 2020 ----------
2021 2021 fname : string
2022 2022 The name of the file to be executed.
2023 2023 where : tuple
2024 2024 One or two namespaces, passed to execfile() as (globals,locals).
2025 2025 If only one is given, it is passed as both.
2026 2026 exit_ignore : bool (False)
2027 2027 If True, then silence SystemExit for non-zero status (it is always
2028 2028 silenced for zero status, as it is so common).
2029 2029 """
2030 2030 kw.setdefault('exit_ignore', False)
2031 2031
2032 2032 fname = os.path.abspath(os.path.expanduser(fname))
2033 2033 # Make sure we have a .py file
2034 2034 if not fname.endswith('.py'):
2035 2035 warn('File must end with .py to be run using execfile: <%s>' % fname)
2036 2036
2037 2037 # Make sure we can open the file
2038 2038 try:
2039 2039 with open(fname) as thefile:
2040 2040 pass
2041 2041 except:
2042 2042 warn('Could not open file <%s> for safe execution.' % fname)
2043 2043 return
2044 2044
2045 2045 # Find things also in current directory. This is needed to mimic the
2046 2046 # behavior of running a script from the system command line, where
2047 2047 # Python inserts the script's directory into sys.path
2048 2048 dname = os.path.dirname(fname)
2049 2049
2050 2050 if isinstance(fname, unicode):
2051 2051 # execfile uses default encoding instead of filesystem encoding
2052 2052 # so unicode filenames will fail
2053 2053 fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())
2054 2054
2055 2055 with prepended_to_syspath(dname):
2056 2056 try:
2057 2057 execfile(fname,*where)
2058 2058 except SystemExit, status:
2059 2059 # If the call was made with 0 or None exit status (sys.exit(0)
2060 2060 # or sys.exit() ), don't bother showing a traceback, as both of
2061 2061 # these are considered normal by the OS:
2062 2062 # > python -c'import sys;sys.exit(0)'; echo $?
2063 2063 # 0
2064 2064 # > python -c'import sys;sys.exit()'; echo $?
2065 2065 # 0
2066 2066 # For other exit status, we show the exception unless
2067 2067 # explicitly silenced, but only in short form.
2068 2068 if status.code not in (0, None) and not kw['exit_ignore']:
2069 2069 self.showtraceback(exception_only=True)
2070 2070 except:
2071 2071 self.showtraceback()
2072 2072
2073 2073 def safe_execfile_ipy(self, fname):
2074 2074 """Like safe_execfile, but for .ipy files with IPython syntax.
2075 2075
2076 2076 Parameters
2077 2077 ----------
2078 2078 fname : str
2079 2079 The name of the file to execute. The filename must have a
2080 2080 .ipy extension.
2081 2081 """
2082 2082 fname = os.path.abspath(os.path.expanduser(fname))
2083 2083
2084 2084 # Make sure we have a .py file
2085 2085 if not fname.endswith('.ipy'):
2086 2086 warn('File must end with .py to be run using execfile: <%s>' % fname)
2087 2087
2088 2088 # Make sure we can open the file
2089 2089 try:
2090 2090 with open(fname) as thefile:
2091 2091 pass
2092 2092 except:
2093 2093 warn('Could not open file <%s> for safe execution.' % fname)
2094 2094 return
2095 2095
2096 2096 # Find things also in current directory. This is needed to mimic the
2097 2097 # behavior of running a script from the system command line, where
2098 2098 # Python inserts the script's directory into sys.path
2099 2099 dname = os.path.dirname(fname)
2100 2100
2101 2101 with prepended_to_syspath(dname):
2102 2102 try:
2103 2103 with open(fname) as thefile:
2104 2104 # self.run_cell currently captures all exceptions
2105 2105 # raised in user code. It would be nice if there were
2106 2106 # versions of runlines, execfile that did raise, so
2107 2107 # we could catch the errors.
2108 2108 self.run_cell(thefile.read(), store_history=False)
2109 2109 except:
2110 2110 self.showtraceback()
2111 2111 warn('Unknown failure executing file: <%s>' % fname)
2112 2112
2113 2113 def run_cell(self, raw_cell, store_history=True):
2114 2114 """Run a complete IPython cell.
2115 2115
2116 2116 Parameters
2117 2117 ----------
2118 2118 raw_cell : str
2119 2119 The code (including IPython code such as %magic functions) to run.
2120 2120 store_history : bool
2121 2121 If True, the raw and translated cell will be stored in IPython's
2122 2122 history. For user code calling back into IPython's machinery, this
2123 2123 should be set to False.
2124 2124 """
2125 2125 if (not raw_cell) or raw_cell.isspace():
2126 2126 return
2127 2127
2128 2128 for line in raw_cell.splitlines():
2129 2129 self.input_splitter.push(line)
2130 2130 cell = self.input_splitter.source_reset()
2131 2131
2132 2132 with self.builtin_trap:
2133 2133 if len(cell.splitlines()) == 1:
2134 2134 cell = self.prefilter_manager.prefilter_lines(cell)
2135 2135
2136 2136 # Store raw and processed history
2137 2137 if store_history:
2138 2138 self.history_manager.store_inputs(self.execution_count,
2139 2139 cell, raw_cell)
2140 2140
2141 2141 self.logger.log(cell, raw_cell)
2142 2142
2143 2143 cell_name = self.compile.cache(cell, self.execution_count)
2144 2144
2145 2145 with self.display_trap:
2146 2146 try:
2147 2147 code_ast = ast.parse(cell, filename=cell_name)
2148 2148 except (OverflowError, SyntaxError, ValueError, TypeError,
2149 2149 MemoryError):
2150 2150 self.showsyntaxerror()
2151 2151 self.execution_count += 1
2152 2152 return None
2153 2153
2154 2154 self.run_ast_nodes(code_ast.body, cell_name,
2155 2155 interactivity="last_expr")
2156 2156
2157 2157 # Execute any registered post-execution functions.
2158 2158 for func, status in self._post_execute.iteritems():
2159 2159 if not status:
2160 2160 continue
2161 2161 try:
2162 2162 func()
2163 2163 except:
2164 2164 self.showtraceback()
2165 2165 # Deactivate failing function
2166 2166 self._post_execute[func] = False
2167 2167
2168 2168 if store_history:
2169 2169 # Write output to the database. Does nothing unless
2170 2170 # history output logging is enabled.
2171 2171 self.history_manager.store_output(self.execution_count)
2172 2172 # Each cell is a *single* input, regardless of how many lines it has
2173 2173 self.execution_count += 1
2174 2174
2175 2175 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2176 2176 """Run a sequence of AST nodes. The execution mode depends on the
2177 2177 interactivity parameter.
2178 2178
2179 2179 Parameters
2180 2180 ----------
2181 2181 nodelist : list
2182 2182 A sequence of AST nodes to run.
2183 2183 cell_name : str
2184 2184 Will be passed to the compiler as the filename of the cell. Typically
2185 2185 the value returned by ip.compile.cache(cell).
2186 2186 interactivity : str
2187 2187 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2188 2188 run interactively (displaying output from expressions). 'last_expr'
2189 2189 will run the last node interactively only if it is an expression (i.e.
2190 2190 expressions in loops or other blocks are not displayed. Other values
2191 2191 for this parameter will raise a ValueError.
2192 2192 """
2193 2193 if not nodelist:
2194 2194 return
2195 2195
2196 2196 if interactivity == 'last_expr':
2197 2197 if isinstance(nodelist[-1], ast.Expr):
2198 2198 interactivity = "last"
2199 2199 else:
2200 2200 interactivity = "none"
2201 2201
2202 2202 if interactivity == 'none':
2203 2203 to_run_exec, to_run_interactive = nodelist, []
2204 2204 elif interactivity == 'last':
2205 2205 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2206 2206 elif interactivity == 'all':
2207 2207 to_run_exec, to_run_interactive = [], nodelist
2208 2208 else:
2209 2209 raise ValueError("Interactivity was %r" % interactivity)
2210 2210
2211 2211 exec_count = self.execution_count
2212 2212
2213 2213 for i, node in enumerate(to_run_exec):
2214 2214 mod = ast.Module([node])
2215 2215 code = self.compile(mod, cell_name, "exec")
2216 2216 if self.run_code(code):
2217 2217 return True
2218 2218
2219 2219 for i, node in enumerate(to_run_interactive):
2220 2220 mod = ast.Interactive([node])
2221 2221 code = self.compile(mod, cell_name, "single")
2222 2222 if self.run_code(code):
2223 2223 return True
2224 2224
2225 2225 return False
2226 2226
2227
2228 # PENDING REMOVAL: this method is slated for deletion, once our new
2229 # input logic has been 100% moved to frontends and is stable.
2230 def runlines(self, lines, clean=False):
2231 """Run a string of one or more lines of source.
2232
2233 This method is capable of running a string containing multiple source
2234 lines, as if they had been entered at the IPython prompt. Since it
2235 exposes IPython's processing machinery, the given strings can contain
2236 magic calls (%magic), special shell access (!cmd), etc.
2237 """
2238
2239 if not isinstance(lines, (list, tuple)):
2240 lines = lines.splitlines()
2241
2242 if clean:
2243 lines = self._cleanup_ipy_script(lines)
2244
2245 # We must start with a clean buffer, in case this is run from an
2246 # interactive IPython session (via a magic, for example).
2247 self.reset_buffer()
2248
2249 # Since we will prefilter all lines, store the user's raw input too
2250 # before we apply any transformations
2251 self.buffer_raw[:] = [ l+'\n' for l in lines]
2252
2253 more = False
2254 prefilter_lines = self.prefilter_manager.prefilter_lines
2255 with nested(self.builtin_trap, self.display_trap):
2256 for line in lines:
2257 # skip blank lines so we don't mess up the prompt counter, but
2258 # do NOT skip even a blank line if we are in a code block (more
2259 # is true)
2260
2261 if line or more:
2262 more = self.push_line(prefilter_lines(line, more))
2263 # IPython's run_source returns None if there was an error
2264 # compiling the code. This allows us to stop processing
2265 # right away, so the user gets the error message at the
2266 # right place.
2267 if more is None:
2268 break
2269 # final newline in case the input didn't have it, so that the code
2270 # actually does get executed
2271 if more:
2272 self.push_line('\n')
2273 2227
2274 2228 def run_source(self, source, filename=None, symbol='single'):
2275 2229 """Compile and run some source in the interpreter.
2276 2230
2277 2231 Arguments are as for compile_command().
2278 2232
2279 2233 One several things can happen:
2280 2234
2281 2235 1) The input is incorrect; compile_command() raised an
2282 2236 exception (SyntaxError or OverflowError). A syntax traceback
2283 2237 will be printed by calling the showsyntaxerror() method.
2284 2238
2285 2239 2) The input is incomplete, and more input is required;
2286 2240 compile_command() returned None. Nothing happens.
2287 2241
2288 2242 3) The input is complete; compile_command() returned a code
2289 2243 object. The code is executed by calling self.run_code() (which
2290 2244 also handles run-time exceptions, except for SystemExit).
2291 2245
2292 2246 The return value is:
2293 2247
2294 2248 - True in case 2
2295 2249
2296 2250 - False in the other cases, unless an exception is raised, where
2297 2251 None is returned instead. This can be used by external callers to
2298 2252 know whether to continue feeding input or not.
2299 2253
2300 2254 The return value can be used to decide whether to use sys.ps1 or
2301 2255 sys.ps2 to prompt the next line."""
2302 2256
2303 2257 # We need to ensure that the source is unicode from here on.
2304 2258 if type(source)==str:
2305 2259 usource = source.decode(self.stdin_encoding)
2306 2260 else:
2307 2261 usource = source
2308 2262
2309 2263 try:
2310 2264 code_name = self.compile.cache(usource, self.execution_count)
2311 2265 code = self.compile(usource, code_name, symbol)
2312 2266 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2313 2267 # Case 1
2314 2268 self.showsyntaxerror(filename)
2315 2269 return None
2316 2270
2317 2271 if code is None:
2318 2272 # Case 2
2319 2273 return True
2320 2274
2321 2275 # Case 3
2322 2276 # now actually execute the code object
2323 2277 if not self.run_code(code):
2324 2278 return False
2325 2279 else:
2326 2280 return None
2327 2281
2328 2282 # For backwards compatibility
2329 2283 runsource = run_source
2330 2284
2331 2285 def run_code(self, code_obj):
2332 2286 """Execute a code object.
2333 2287
2334 2288 When an exception occurs, self.showtraceback() is called to display a
2335 2289 traceback.
2336 2290
2337 2291 Parameters
2338 2292 ----------
2339 2293 code_obj : code object
2340 2294 A compiled code object, to be executed
2341 2295 post_execute : bool [default: True]
2342 2296 whether to call post_execute hooks after this particular execution.
2343 2297
2344 2298 Returns
2345 2299 -------
2346 2300 False : successful execution.
2347 2301 True : an error occurred.
2348 2302 """
2349 2303
2350 2304 # Set our own excepthook in case the user code tries to call it
2351 2305 # directly, so that the IPython crash handler doesn't get triggered
2352 2306 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2353 2307
2354 2308 # we save the original sys.excepthook in the instance, in case config
2355 2309 # code (such as magics) needs access to it.
2356 2310 self.sys_excepthook = old_excepthook
2357 2311 outflag = 1 # happens in more places, so it's easier as default
2358 2312 try:
2359 2313 try:
2360 2314 self.hooks.pre_run_code_hook()
2361 2315 #rprint('Running code', repr(code_obj)) # dbg
2362 2316 exec code_obj in self.user_global_ns, self.user_ns
2363 2317 finally:
2364 2318 # Reset our crash handler in place
2365 2319 sys.excepthook = old_excepthook
2366 2320 except SystemExit:
2367 2321 self.reset_buffer()
2368 2322 self.showtraceback(exception_only=True)
2369 2323 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2370 2324 except self.custom_exceptions:
2371 2325 etype,value,tb = sys.exc_info()
2372 2326 self.CustomTB(etype,value,tb)
2373 2327 except:
2374 2328 self.showtraceback()
2375 2329 else:
2376 2330 outflag = 0
2377 2331 if softspace(sys.stdout, 0):
2378 2332 print
2379 2333
2380 2334 return outflag
2381 2335
2382 2336 # For backwards compatibility
2383 2337 runcode = run_code
2384 2338
2385 2339 # PENDING REMOVAL: this method is slated for deletion, once our new
2386 2340 # input logic has been 100% moved to frontends and is stable.
2387 2341 def push_line(self, line):
2388 2342 """Push a line to the interpreter.
2389 2343
2390 2344 The line should not have a trailing newline; it may have
2391 2345 internal newlines. The line is appended to a buffer and the
2392 2346 interpreter's run_source() method is called with the
2393 2347 concatenated contents of the buffer as source. If this
2394 2348 indicates that the command was executed or invalid, the buffer
2395 2349 is reset; otherwise, the command is incomplete, and the buffer
2396 2350 is left as it was after the line was appended. The return
2397 2351 value is 1 if more input is required, 0 if the line was dealt
2398 2352 with in some way (this is the same as run_source()).
2399 2353 """
2400 2354
2401 2355 # autoindent management should be done here, and not in the
2402 2356 # interactive loop, since that one is only seen by keyboard input. We
2403 2357 # need this done correctly even for code run via runlines (which uses
2404 2358 # push).
2405 2359
2406 2360 #print 'push line: <%s>' % line # dbg
2407 2361 self.buffer.append(line)
2408 2362 full_source = '\n'.join(self.buffer)
2409 2363 more = self.run_source(full_source, self.filename)
2410 2364 if not more:
2411 2365 self.history_manager.store_inputs(self.execution_count,
2412 2366 '\n'.join(self.buffer_raw), full_source)
2413 2367 self.reset_buffer()
2414 2368 self.execution_count += 1
2415 2369 return more
2416 2370
2417 2371 def reset_buffer(self):
2418 2372 """Reset the input buffer."""
2419 2373 self.buffer[:] = []
2420 2374 self.buffer_raw[:] = []
2421 2375 self.input_splitter.reset()
2422 2376
2423 2377 # For backwards compatibility
2424 2378 resetbuffer = reset_buffer
2425 2379
2426 2380 def _is_secondary_block_start(self, s):
2427 2381 if not s.endswith(':'):
2428 2382 return False
2429 2383 if (s.startswith('elif') or
2430 2384 s.startswith('else') or
2431 2385 s.startswith('except') or
2432 2386 s.startswith('finally')):
2433 2387 return True
2434 2388
2435 2389 def _cleanup_ipy_script(self, script):
2436 2390 """Make a script safe for self.runlines()
2437 2391
2438 2392 Currently, IPython is lines based, with blocks being detected by
2439 2393 empty lines. This is a problem for block based scripts that may
2440 2394 not have empty lines after blocks. This script adds those empty
2441 2395 lines to make scripts safe for running in the current line based
2442 2396 IPython.
2443 2397 """
2444 2398 res = []
2445 2399 lines = script.splitlines()
2446 2400 level = 0
2447 2401
2448 2402 for l in lines:
2449 2403 lstripped = l.lstrip()
2450 2404 stripped = l.strip()
2451 2405 if not stripped:
2452 2406 continue
2453 2407 newlevel = len(l) - len(lstripped)
2454 2408 if level > 0 and newlevel == 0 and \
2455 2409 not self._is_secondary_block_start(stripped):
2456 2410 # add empty line
2457 2411 res.append('')
2458 2412 res.append(l)
2459 2413 level = newlevel
2460 2414
2461 2415 return '\n'.join(res) + '\n'
2462 2416
2463 2417 #-------------------------------------------------------------------------
2464 2418 # Things related to GUI support and pylab
2465 2419 #-------------------------------------------------------------------------
2466 2420
2467 2421 def enable_pylab(self, gui=None):
2468 2422 raise NotImplementedError('Implement enable_pylab in a subclass')
2469 2423
2470 2424 #-------------------------------------------------------------------------
2471 2425 # Utilities
2472 2426 #-------------------------------------------------------------------------
2473 2427
2474 2428 def var_expand(self,cmd,depth=0):
2475 2429 """Expand python variables in a string.
2476 2430
2477 2431 The depth argument indicates how many frames above the caller should
2478 2432 be walked to look for the local namespace where to expand variables.
2479 2433
2480 2434 The global namespace for expansion is always the user's interactive
2481 2435 namespace.
2482 2436 """
2483 2437 res = ItplNS(cmd, self.user_ns, # globals
2484 2438 # Skip our own frame in searching for locals:
2485 2439 sys._getframe(depth+1).f_locals # locals
2486 2440 )
2487 2441 return str(res).decode(res.codec)
2488 2442
2489 2443 def mktempfile(self, data=None, prefix='ipython_edit_'):
2490 2444 """Make a new tempfile and return its filename.
2491 2445
2492 2446 This makes a call to tempfile.mktemp, but it registers the created
2493 2447 filename internally so ipython cleans it up at exit time.
2494 2448
2495 2449 Optional inputs:
2496 2450
2497 2451 - data(None): if data is given, it gets written out to the temp file
2498 2452 immediately, and the file is closed again."""
2499 2453
2500 2454 filename = tempfile.mktemp('.py', prefix)
2501 2455 self.tempfiles.append(filename)
2502 2456
2503 2457 if data:
2504 2458 tmp_file = open(filename,'w')
2505 2459 tmp_file.write(data)
2506 2460 tmp_file.close()
2507 2461 return filename
2508 2462
2509 2463 # TODO: This should be removed when Term is refactored.
2510 2464 def write(self,data):
2511 2465 """Write a string to the default output"""
2512 2466 io.Term.cout.write(data)
2513 2467
2514 2468 # TODO: This should be removed when Term is refactored.
2515 2469 def write_err(self,data):
2516 2470 """Write a string to the default error output"""
2517 2471 io.Term.cerr.write(data)
2518 2472
2519 2473 def ask_yes_no(self,prompt,default=True):
2520 2474 if self.quiet:
2521 2475 return True
2522 2476 return ask_yes_no(prompt,default)
2523 2477
2524 2478 def show_usage(self):
2525 2479 """Show a usage message"""
2526 2480 page.page(IPython.core.usage.interactive_usage)
2527 2481
2528 2482 def find_user_code(self, target, raw=True):
2529 2483 """Get a code string from history, file, or a string or macro.
2530 2484
2531 2485 This is mainly used by magic functions.
2532 2486
2533 2487 Parameters
2534 2488 ----------
2535 2489 target : str
2536 2490 A string specifying code to retrieve. This will be tried respectively
2537 2491 as: ranges of input history (see %history for syntax), a filename, or
2538 2492 an expression evaluating to a string or Macro in the user namespace.
2539 2493 raw : bool
2540 2494 If true (default), retrieve raw history. Has no effect on the other
2541 2495 retrieval mechanisms.
2542 2496
2543 2497 Returns
2544 2498 -------
2545 2499 A string of code.
2546 2500
2547 2501 ValueError is raised if nothing is found, and TypeError if it evaluates
2548 2502 to an object of another type. In each case, .args[0] is a printable
2549 2503 message.
2550 2504 """
2551 2505 code = self.extract_input_lines(target, raw=raw) # Grab history
2552 2506 if code:
2553 2507 return code
2554 2508 if os.path.isfile(target): # Read file
2555 2509 return open(target, "r").read()
2556 2510
2557 2511 try: # User namespace
2558 2512 codeobj = eval(target, self.user_ns)
2559 2513 except Exception:
2560 2514 raise ValueError(("'%s' was not found in history, as a file, nor in"
2561 2515 " the user namespace.") % target)
2562 2516 if isinstance(codeobj, basestring):
2563 2517 return codeobj
2564 2518 elif isinstance(codeobj, Macro):
2565 2519 return codeobj.value
2566 2520
2567 2521 raise TypeError("%s is neither a string nor a macro." % target,
2568 2522 codeobj)
2569 2523
2570 2524 #-------------------------------------------------------------------------
2571 2525 # Things related to IPython exiting
2572 2526 #-------------------------------------------------------------------------
2573 2527 def atexit_operations(self):
2574 2528 """This will be executed at the time of exit.
2575 2529
2576 2530 Cleanup operations and saving of persistent data that is done
2577 2531 unconditionally by IPython should be performed here.
2578 2532
2579 2533 For things that may depend on startup flags or platform specifics (such
2580 2534 as having readline or not), register a separate atexit function in the
2581 2535 code that has the appropriate information, rather than trying to
2582 2536 clutter
2583 2537 """
2584 2538 # Cleanup all tempfiles left around
2585 2539 for tfile in self.tempfiles:
2586 2540 try:
2587 2541 os.unlink(tfile)
2588 2542 except OSError:
2589 2543 pass
2590 2544
2591 2545 # Close the history session (this stores the end time and line count)
2592 2546 self.history_manager.end_session()
2593 2547
2594 2548 # Clear all user namespaces to release all references cleanly.
2595 2549 self.reset(new_session=False)
2596 2550
2597 2551 # Run user hooks
2598 2552 self.hooks.shutdown_hook()
2599 2553
2600 2554 def cleanup(self):
2601 2555 self.restore_sys_module_state()
2602 2556
2603 2557
2604 2558 class InteractiveShellABC(object):
2605 2559 """An abstract base class for InteractiveShell."""
2606 2560 __metaclass__ = abc.ABCMeta
2607 2561
2608 2562 InteractiveShellABC.register(InteractiveShell)
@@ -1,271 +1,271 b''
1 1 """Tests for the key interactiveshell module, where the main ipython class is defined.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # stdlib
8 8 import os
9 9 import shutil
10 10 import tempfile
11 11
12 12 # third party
13 13 import nose.tools as nt
14 14
15 15 # our own packages
16 16 from IPython.testing import decorators as dec
17 17 from IPython.testing.globalipapp import get_ipython
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Globals
21 21 #-----------------------------------------------------------------------------
22 22
23 23 # Get the public instance of IPython
24 24 ip = get_ipython()
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Test functions
28 28 #-----------------------------------------------------------------------------
29 29
30 30 @dec.parametric
31 31 def test_reset():
32 32 """reset must clear most namespaces."""
33 33 # The number of variables in the private user_ns_hidden is not zero, but it
34 34 # should be constant regardless of what we do
35 35 nvars_config_ns = len(ip.user_ns_hidden)
36 36
37 37 # Check that reset runs without error
38 38 ip.reset()
39 39
40 40 # Once we've reset it (to clear of any junk that might have been there from
41 41 # other tests, we can count how many variables are in the user's namespace
42 42 nvars_user_ns = len(ip.user_ns)
43 43
44 44 # Now add a few variables to user_ns, and check that reset clears them
45 45 ip.user_ns['x'] = 1
46 46 ip.user_ns['y'] = 1
47 47 ip.reset()
48 48
49 49 # Finally, check that all namespaces have only as many variables as we
50 50 # expect to find in them:
51 51 for ns in ip.ns_refs_table:
52 52 if ns is ip.user_ns:
53 53 nvars_expected = nvars_user_ns
54 54 elif ns is ip.user_ns_hidden:
55 55 nvars_expected = nvars_config_ns
56 56 else:
57 57 nvars_expected = 0
58 58
59 59 yield nt.assert_equals(len(ns), nvars_expected)
60 60
61 61
62 62 # Tests for reporting of exceptions in various modes, handling of SystemExit,
63 63 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
64 64
65 65 def doctest_tb_plain():
66 66 """
67 67 In [18]: xmode plain
68 68 Exception reporting mode: Plain
69 69
70 70 In [19]: run simpleerr.py
71 71 Traceback (most recent call last):
72 72 ...line 32, in <module>
73 73 bar(mode)
74 74 ...line 16, in bar
75 75 div0()
76 76 ...line 8, in div0
77 77 x/y
78 78 ZeroDivisionError: integer division or modulo by zero
79 79 """
80 80
81 81
82 82 def doctest_tb_context():
83 83 """
84 84 In [3]: xmode context
85 85 Exception reporting mode: Context
86 86
87 87 In [4]: run simpleerr.py
88 88 ---------------------------------------------------------------------------
89 89 ZeroDivisionError Traceback (most recent call last)
90 90 <BLANKLINE>
91 91 ... in <module>()
92 92 30 mode = 'div'
93 93 31
94 94 ---> 32 bar(mode)
95 95 <BLANKLINE>
96 96 ... in bar(mode)
97 97 14 "bar"
98 98 15 if mode=='div':
99 99 ---> 16 div0()
100 100 17 elif mode=='exit':
101 101 18 try:
102 102 <BLANKLINE>
103 103 ... in div0()
104 104 6 x = 1
105 105 7 y = 0
106 106 ----> 8 x/y
107 107 9
108 108 10 def sysexit(stat, mode):
109 109 <BLANKLINE>
110 110 ZeroDivisionError: integer division or modulo by zero
111 111 """
112 112
113 113
114 114 def doctest_tb_verbose():
115 115 """
116 116 In [5]: xmode verbose
117 117 Exception reporting mode: Verbose
118 118
119 119 In [6]: run simpleerr.py
120 120 ---------------------------------------------------------------------------
121 121 ZeroDivisionError Traceback (most recent call last)
122 122 <BLANKLINE>
123 123 ... in <module>()
124 124 30 mode = 'div'
125 125 31
126 126 ---> 32 bar(mode)
127 127 global bar = <function bar at ...>
128 128 global mode = 'div'
129 129 <BLANKLINE>
130 130 ... in bar(mode='div')
131 131 14 "bar"
132 132 15 if mode=='div':
133 133 ---> 16 div0()
134 134 global div0 = <function div0 at ...>
135 135 17 elif mode=='exit':
136 136 18 try:
137 137 <BLANKLINE>
138 138 ... in div0()
139 139 6 x = 1
140 140 7 y = 0
141 141 ----> 8 x/y
142 142 x = 1
143 143 y = 0
144 144 9
145 145 10 def sysexit(stat, mode):
146 146 <BLANKLINE>
147 147 ZeroDivisionError: integer division or modulo by zero
148 148 """
149 149
150 150
151 151 def doctest_tb_sysexit():
152 152 """
153 153 In [17]: %xmode plain
154 154 Exception reporting mode: Plain
155 155
156 156 In [18]: %run simpleerr.py exit
157 157 An exception has occurred, use %tb to see the full traceback.
158 158 SystemExit: (1, u'Mode = exit')
159 159
160 160 In [19]: %run simpleerr.py exit 2
161 161 An exception has occurred, use %tb to see the full traceback.
162 162 SystemExit: (2, u'Mode = exit')
163 163
164 164 In [20]: %tb
165 165 Traceback (most recent call last):
166 166 File ... in <module>
167 167 bar(mode)
168 168 File ... line 22, in bar
169 169 sysexit(stat, mode)
170 170 File ... line 11, in sysexit
171 171 raise SystemExit(stat, 'Mode = %s' % mode)
172 172 SystemExit: (2, u'Mode = exit')
173 173
174 174 In [21]: %xmode context
175 175 Exception reporting mode: Context
176 176
177 177 In [22]: %tb
178 178 ---------------------------------------------------------------------------
179 179 SystemExit Traceback (most recent call last)
180 180 <BLANKLINE>
181 181 ...<module>()
182 182 30 mode = 'div'
183 183 31
184 184 ---> 32 bar(mode)
185 185 <BLANKLINE>
186 186 ...bar(mode)
187 187 20 except:
188 188 21 stat = 1
189 189 ---> 22 sysexit(stat, mode)
190 190 23 else:
191 191 24 raise ValueError('Unknown mode')
192 192 <BLANKLINE>
193 193 ...sysexit(stat, mode)
194 194 9
195 195 10 def sysexit(stat, mode):
196 196 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
197 197 12
198 198 13 def bar(mode):
199 199 <BLANKLINE>
200 200 SystemExit: (2, u'Mode = exit')
201 201
202 202 In [23]: %xmode verbose
203 203 Exception reporting mode: Verbose
204 204
205 205 In [24]: %tb
206 206 ---------------------------------------------------------------------------
207 207 SystemExit Traceback (most recent call last)
208 208 <BLANKLINE>
209 209 ... in <module>()
210 210 30 mode = 'div'
211 211 31
212 212 ---> 32 bar(mode)
213 213 global bar = <function bar at ...>
214 214 global mode = u'exit'
215 215 <BLANKLINE>
216 216 ... in bar(mode=u'exit')
217 217 20 except:
218 218 21 stat = 1
219 219 ---> 22 sysexit(stat, mode)
220 220 global sysexit = <function sysexit at ...>
221 221 stat = 2
222 222 mode = u'exit'
223 223 23 else:
224 224 24 raise ValueError('Unknown mode')
225 225 <BLANKLINE>
226 226 ... in sysexit(stat=2, mode=u'exit')
227 227 9
228 228 10 def sysexit(stat, mode):
229 229 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
230 230 global SystemExit = undefined
231 231 stat = 2
232 232 mode = u'exit'
233 233 12
234 234 13 def bar(mode):
235 235 <BLANKLINE>
236 236 SystemExit: (2, u'Mode = exit')
237 237 """
238 238
239 239
240 def test_runlines():
240 def test_run_cell():
241 241 import textwrap
242 ip.runlines(['a = 10', 'a+=1'])
243 ip.runlines('assert a == 11\nassert 1')
242 ip.run_cell('a = 10\na+=1')
243 ip.run_cell('assert a == 11\nassert 1')
244 244
245 245 nt.assert_equals(ip.user_ns['a'], 11)
246 246 complex = textwrap.dedent("""
247 247 if 1:
248 248 print "hello"
249 249 if 1:
250 250 print "world"
251 251
252 252 if 2:
253 253 print "foo"
254 254
255 255 if 3:
256 256 print "bar"
257 257
258 258 if 4:
259 259 print "bar"
260 260
261 261 """)
262 262 # Simply verifies that this kind of input is run
263 ip.runlines(complex)
263 ip.run_cell(complex)
264 264
265 265
266 266 def test_db():
267 267 """Test the internal database used for variable persistence."""
268 268 ip.db['__unittest_'] = 12
269 269 nt.assert_equals(ip.db['__unittest_'], 12)
270 270 del ip.db['__unittest_']
271 271 assert '__unittest_' not in ip.db
@@ -1,206 +1,206 b''
1 1 """Tests for code execution (%run and related), which is particularly tricky.
2 2
3 3 Because of how %run manages namespaces, and the fact that we are trying here to
4 4 verify subtle object deletion and reference counting issues, the %run tests
5 5 will be kept in this separate file. This makes it easier to aggregate in one
6 6 place the tricks needed to handle it; most other magics are much easier to test
7 7 and we do so in a common test_magic file.
8 8 """
9 9 from __future__ import absolute_import
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 import os
16 16 import sys
17 17 import tempfile
18 18
19 19 import nose.tools as nt
20 20
21 21 from IPython.testing import decorators as dec
22 22 from IPython.testing import tools as tt
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Test functions begin
26 26 #-----------------------------------------------------------------------------
27 27
28 28 def doctest_refbug():
29 29 """Very nasty problem with references held by multiple runs of a script.
30 30 See: https://bugs.launchpad.net/ipython/+bug/269966
31 31
32 32 In [1]: _ip.clear_main_mod_cache()
33 33 # random
34 34
35 35 In [2]: %run refbug
36 36
37 37 In [3]: call_f()
38 38 lowercased: hello
39 39
40 40 In [4]: %run refbug
41 41
42 42 In [5]: call_f()
43 43 lowercased: hello
44 44 lowercased: hello
45 45 """
46 46
47 47
48 48 def doctest_run_builtins():
49 49 r"""Check that %run doesn't damage __builtins__.
50 50
51 51 In [1]: import tempfile
52 52
53 53 In [2]: bid1 = id(__builtins__)
54 54
55 55 In [3]: fname = tempfile.mkstemp('.py')[1]
56 56
57 57 In [3]: f = open(fname,'w')
58 58
59 59 In [4]: f.write('pass\n')
60 60
61 61 In [5]: f.flush()
62 62
63 63 In [6]: t1 = type(__builtins__)
64 64
65 65 In [7]: %run $fname
66 66
67 67 In [7]: f.close()
68 68
69 69 In [8]: bid2 = id(__builtins__)
70 70
71 71 In [9]: t2 = type(__builtins__)
72 72
73 73 In [10]: t1 == t2
74 74 Out[10]: True
75 75
76 76 In [10]: bid1 == bid2
77 77 Out[10]: True
78 78
79 79 In [12]: try:
80 80 ....: os.unlink(fname)
81 81 ....: except:
82 82 ....: pass
83 83 ....:
84 84 """
85 85
86 86 def doctest_reset_del():
87 87 """Test that resetting doesn't cause errors in __del__ methods.
88 88
89 89 In [2]: class A(object):
90 90 ...: def __del__(self):
91 91 ...: print str("Hi")
92 92 ...:
93 93
94 94 In [3]: a = A()
95 95
96 96 In [4]: get_ipython().reset()
97 97 Hi
98 98
99 99 In [5]: 1+1
100 100 Out[5]: 2
101 101 """
102 102
103 103 # For some tests, it will be handy to organize them in a class with a common
104 104 # setup that makes a temp file
105 105
106 106 class TestMagicRunPass(tt.TempFileMixin):
107 107
108 108 def setup(self):
109 109 """Make a valid python temp file."""
110 110 self.mktmp('pass\n')
111 111
112 112 def run_tmpfile(self):
113 113 _ip = get_ipython()
114 114 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
115 115 # See below and ticket https://bugs.launchpad.net/bugs/366353
116 116 _ip.magic('run %s' % self.fname)
117 117
118 118 def test_builtins_id(self):
119 119 """Check that %run doesn't damage __builtins__ """
120 120 _ip = get_ipython()
121 121 # Test that the id of __builtins__ is not modified by %run
122 122 bid1 = id(_ip.user_ns['__builtins__'])
123 123 self.run_tmpfile()
124 124 bid2 = id(_ip.user_ns['__builtins__'])
125 125 tt.assert_equals(bid1, bid2)
126 126
127 127 def test_builtins_type(self):
128 128 """Check that the type of __builtins__ doesn't change with %run.
129 129
130 130 However, the above could pass if __builtins__ was already modified to
131 131 be a dict (it should be a module) by a previous use of %run. So we
132 132 also check explicitly that it really is a module:
133 133 """
134 134 _ip = get_ipython()
135 135 self.run_tmpfile()
136 136 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
137 137
138 138 def test_prompts(self):
139 139 """Test that prompts correctly generate after %run"""
140 140 self.run_tmpfile()
141 141 _ip = get_ipython()
142 142 p2 = str(_ip.displayhook.prompt2).strip()
143 143 nt.assert_equals(p2[:3], '...')
144 144
145 145
146 146 class TestMagicRunSimple(tt.TempFileMixin):
147 147
148 148 def test_simpledef(self):
149 149 """Test that simple class definitions work."""
150 150 src = ("class foo: pass\n"
151 151 "def f(): return foo()")
152 152 self.mktmp(src)
153 153 _ip.magic('run %s' % self.fname)
154 _ip.runlines('t = isinstance(f(), foo)')
154 _ip.run_cell('t = isinstance(f(), foo)')
155 155 nt.assert_true(_ip.user_ns['t'])
156 156
157 157 # We have to skip these in win32 because getoutputerr() crashes,
158 158 # due to the fact that subprocess does not support close_fds when
159 159 # redirecting stdout/err. So unless someone who knows more tells us how to
160 160 # implement getoutputerr() in win32, we're stuck avoiding these.
161 161 @dec.skip_win32
162 162 def test_obj_del(self):
163 163 """Test that object's __del__ methods are called on exit."""
164 164
165 165 # This test is known to fail on win32.
166 166 # See ticket https://bugs.launchpad.net/bugs/366334
167 167 src = ("class A(object):\n"
168 168 " def __del__(self):\n"
169 169 " print 'object A deleted'\n"
170 170 "a = A()\n")
171 171 self.mktmp(src)
172 172 tt.ipexec_validate(self.fname, 'object A deleted')
173 173
174 174 @dec.skip_known_failure
175 175 def test_aggressive_namespace_cleanup(self):
176 176 """Test that namespace cleanup is not too aggressive GH-238
177 177
178 178 Returning from another run magic deletes the namespace"""
179 179 # see ticket https://github.com/ipython/ipython/issues/238
180 180 class secondtmp(tt.TempFileMixin): pass
181 181 empty = secondtmp()
182 182 empty.mktmp('')
183 183 src = ("ip = get_ipython()\n"
184 184 "for i in range(5):\n"
185 185 " try:\n"
186 186 " ip.magic('run %s')\n"
187 187 " except NameError, e:\n"
188 188 " print i;break\n" % empty.fname)
189 189 self.mktmp(src)
190 190 _ip.magic('run %s' % self.fname)
191 _ip.runlines('ip == get_ipython()')
191 _ip.run_cell('ip == get_ipython()')
192 192 tt.assert_equals(_ip.user_ns['i'], 5)
193 193
194 194 @dec.skip_win32
195 195 def test_tclass(self):
196 196 mydir = os.path.dirname(__file__)
197 197 tc = os.path.join(mydir, 'tclass')
198 198 src = ("%%run '%s' C-first\n"
199 199 "%%run '%s' C-second\n") % (tc, tc)
200 200 self.mktmp(src, '.ipy')
201 201 out = """\
202 202 ARGV 1-: [u'C-first']
203 203 ARGV 1-: [u'C-second']
204 204 tclass.py: deleting object: C-first
205 205 """
206 206 tt.ipexec_validate(self.fname, out)
@@ -1,658 +1,658 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 from __future__ import print_function
17 17
18 18 # Standard library imports.
19 19 import __builtin__
20 20 import atexit
21 21 import sys
22 22 import time
23 23 import traceback
24 24 import logging
25 25 # System library imports.
26 26 import zmq
27 27
28 28 # Local imports.
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.utils import io
31 31 from IPython.utils.jsonutil import json_clean
32 32 from IPython.lib import pylabtools
33 33 from IPython.utils.traitlets import Instance, Float
34 34 from entry_point import (base_launch_kernel, make_argument_parser, make_kernel,
35 35 start_kernel)
36 36 from iostream import OutStream
37 37 from session import Session, Message
38 38 from zmqshell import ZMQInteractiveShell
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Globals
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # Module-level logger
45 45 logger = logging.getLogger(__name__)
46 46
47 47 # FIXME: this needs to be done more cleanly later, once we have proper
48 48 # configuration support. This is a library, so it shouldn't set a stream
49 49 # handler, see:
50 50 # http://docs.python.org/library/logging.html#configuring-logging-for-a-library
51 51 # But this lets us at least do developer debugging for now by manually turning
52 52 # it on/off. And once we have full config support, the client entry points
53 53 # will select their logging handlers, as well as passing to this library the
54 54 # logging level.
55 55
56 56 if 0: # dbg - set to 1 to actually see the messages.
57 57 logger.addHandler(logging.StreamHandler())
58 58 logger.setLevel(logging.DEBUG)
59 59
60 60 # /FIXME
61 61
62 62 #-----------------------------------------------------------------------------
63 63 # Main kernel class
64 64 #-----------------------------------------------------------------------------
65 65
66 66 class Kernel(Configurable):
67 67
68 68 #---------------------------------------------------------------------------
69 69 # Kernel interface
70 70 #---------------------------------------------------------------------------
71 71
72 72 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
73 73 session = Instance(Session)
74 74 reply_socket = Instance('zmq.Socket')
75 75 pub_socket = Instance('zmq.Socket')
76 76 req_socket = Instance('zmq.Socket')
77 77
78 78 # Private interface
79 79
80 80 # Time to sleep after flushing the stdout/err buffers in each execute
81 81 # cycle. While this introduces a hard limit on the minimal latency of the
82 82 # execute cycle, it helps prevent output synchronization problems for
83 83 # clients.
84 84 # Units are in seconds. The minimum zmq latency on local host is probably
85 85 # ~150 microseconds, set this to 500us for now. We may need to increase it
86 86 # a little if it's not enough after more interactive testing.
87 87 _execute_sleep = Float(0.0005, config=True)
88 88
89 89 # Frequency of the kernel's event loop.
90 90 # Units are in seconds, kernel subclasses for GUI toolkits may need to
91 91 # adapt to milliseconds.
92 92 _poll_interval = Float(0.05, config=True)
93 93
94 94 # If the shutdown was requested over the network, we leave here the
95 95 # necessary reply message so it can be sent by our registered atexit
96 96 # handler. This ensures that the reply is only sent to clients truly at
97 97 # the end of our shutdown process (which happens after the underlying
98 98 # IPython shell's own shutdown).
99 99 _shutdown_message = None
100 100
101 101 # This is a dict of port number that the kernel is listening on. It is set
102 102 # by record_ports and used by connect_request.
103 103 _recorded_ports = None
104 104
105 105
106 106 def __init__(self, **kwargs):
107 107 super(Kernel, self).__init__(**kwargs)
108 108
109 109 # Before we even start up the shell, register *first* our exit handlers
110 110 # so they come before the shell's
111 111 atexit.register(self._at_shutdown)
112 112
113 113 # Initialize the InteractiveShell subclass
114 114 self.shell = ZMQInteractiveShell.instance()
115 115 self.shell.displayhook.session = self.session
116 116 self.shell.displayhook.pub_socket = self.pub_socket
117 117 self.shell.display_pub.session = self.session
118 118 self.shell.display_pub.pub_socket = self.pub_socket
119 119
120 120 # TMP - hack while developing
121 121 self.shell._reply_content = None
122 122
123 123 # Build dict of handlers for message types
124 124 msg_types = [ 'execute_request', 'complete_request',
125 125 'object_info_request', 'history_tail_request',
126 126 'connect_request', 'shutdown_request']
127 127 self.handlers = {}
128 128 for msg_type in msg_types:
129 129 self.handlers[msg_type] = getattr(self, msg_type)
130 130
131 131 def do_one_iteration(self):
132 132 """Do one iteration of the kernel's evaluation loop.
133 133 """
134 134 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
135 135 if msg is None:
136 136 return
137 137
138 138 # This assert will raise in versions of zeromq 2.0.7 and lesser.
139 139 # We now require 2.0.8 or above, so we can uncomment for safety.
140 140 # print(ident,msg, file=sys.__stdout__)
141 141 assert ident is not None, "Missing message part."
142 142
143 143 # Print some info about this message and leave a '--->' marker, so it's
144 144 # easier to trace visually the message chain when debugging. Each
145 145 # handler prints its message at the end.
146 146 # Eventually we'll move these from stdout to a logger.
147 147 logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***')
148 148 logger.debug(' Content: '+str(msg['content'])+'\n --->\n ')
149 149
150 150 # Find and call actual handler for message
151 151 handler = self.handlers.get(msg['msg_type'], None)
152 152 if handler is None:
153 153 logger.error("UNKNOWN MESSAGE TYPE:" +str(msg))
154 154 else:
155 155 handler(ident, msg)
156 156
157 157 # Check whether we should exit, in case the incoming message set the
158 158 # exit flag on
159 159 if self.shell.exit_now:
160 160 logger.debug('\nExiting IPython kernel...')
161 161 # We do a normal, clean exit, which allows any actions registered
162 162 # via atexit (such as history saving) to take place.
163 163 sys.exit(0)
164 164
165 165
166 166 def start(self):
167 167 """ Start the kernel main loop.
168 168 """
169 169 while True:
170 170 time.sleep(self._poll_interval)
171 171 self.do_one_iteration()
172 172
173 173 def record_ports(self, xrep_port, pub_port, req_port, hb_port):
174 174 """Record the ports that this kernel is using.
175 175
176 176 The creator of the Kernel instance must call this methods if they
177 177 want the :meth:`connect_request` method to return the port numbers.
178 178 """
179 179 self._recorded_ports = {
180 180 'xrep_port' : xrep_port,
181 181 'pub_port' : pub_port,
182 182 'req_port' : req_port,
183 183 'hb_port' : hb_port
184 184 }
185 185
186 186 #---------------------------------------------------------------------------
187 187 # Kernel request handlers
188 188 #---------------------------------------------------------------------------
189 189
190 190 def _publish_pyin(self, code, parent):
191 191 """Publish the code request on the pyin stream."""
192 192
193 193 pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent)
194 194
195 195 def execute_request(self, ident, parent):
196 196
197 197 status_msg = self.session.send(self.pub_socket,
198 198 u'status',
199 199 {u'execution_state':u'busy'},
200 200 parent=parent
201 201 )
202 202
203 203 try:
204 204 content = parent[u'content']
205 205 code = content[u'code']
206 206 silent = content[u'silent']
207 207 except:
208 208 logger.error("Got bad msg: ")
209 209 logger.error(str(Message(parent)))
210 210 return
211 211
212 212 shell = self.shell # we'll need this a lot here
213 213
214 214 # Replace raw_input. Note that is not sufficient to replace
215 215 # raw_input in the user namespace.
216 216 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
217 217 __builtin__.raw_input = raw_input
218 218
219 219 # Set the parent message of the display hook and out streams.
220 220 shell.displayhook.set_parent(parent)
221 221 shell.display_pub.set_parent(parent)
222 222 sys.stdout.set_parent(parent)
223 223 sys.stderr.set_parent(parent)
224 224
225 225 # Re-broadcast our input for the benefit of listening clients, and
226 226 # start computing output
227 227 if not silent:
228 228 self._publish_pyin(code, parent)
229 229
230 230 reply_content = {}
231 231 try:
232 232 if silent:
233 233 # run_code uses 'exec' mode, so no displayhook will fire, and it
234 234 # doesn't call logging or history manipulations. Print
235 235 # statements in that code will obviously still execute.
236 236 shell.run_code(code)
237 237 else:
238 238 # FIXME: the shell calls the exception handler itself.
239 239 shell._reply_content = None
240 240 shell.run_cell(code)
241 241 except:
242 242 status = u'error'
243 243 # FIXME: this code right now isn't being used yet by default,
244 # because the runlines() call above directly fires off exception
244 # because the run_cell() call above directly fires off exception
245 245 # reporting. This code, therefore, is only active in the scenario
246 246 # where runlines itself has an unhandled exception. We need to
247 247 # uniformize this, for all exception construction to come from a
248 248 # single location in the codbase.
249 249 etype, evalue, tb = sys.exc_info()
250 250 tb_list = traceback.format_exception(etype, evalue, tb)
251 251 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
252 252 else:
253 253 status = u'ok'
254 254
255 255 reply_content[u'status'] = status
256 256
257 257 # Return the execution counter so clients can display prompts
258 258 reply_content['execution_count'] = shell.execution_count -1
259 259
260 260 # FIXME - fish exception info out of shell, possibly left there by
261 261 # runlines. We'll need to clean up this logic later.
262 262 if shell._reply_content is not None:
263 263 reply_content.update(shell._reply_content)
264 264
265 265 # At this point, we can tell whether the main code execution succeeded
266 266 # or not. If it did, we proceed to evaluate user_variables/expressions
267 267 if reply_content['status'] == 'ok':
268 268 reply_content[u'user_variables'] = \
269 269 shell.user_variables(content[u'user_variables'])
270 270 reply_content[u'user_expressions'] = \
271 271 shell.user_expressions(content[u'user_expressions'])
272 272 else:
273 273 # If there was an error, don't even try to compute variables or
274 274 # expressions
275 275 reply_content[u'user_variables'] = {}
276 276 reply_content[u'user_expressions'] = {}
277 277
278 278 # Payloads should be retrieved regardless of outcome, so we can both
279 279 # recover partial output (that could have been generated early in a
280 280 # block, before an error) and clear the payload system always.
281 281 reply_content[u'payload'] = shell.payload_manager.read_payload()
282 282 # Be agressive about clearing the payload because we don't want
283 283 # it to sit in memory until the next execute_request comes in.
284 284 shell.payload_manager.clear_payload()
285 285
286 286 # Flush output before sending the reply.
287 287 sys.stdout.flush()
288 288 sys.stderr.flush()
289 289 # FIXME: on rare occasions, the flush doesn't seem to make it to the
290 290 # clients... This seems to mitigate the problem, but we definitely need
291 291 # to better understand what's going on.
292 292 if self._execute_sleep:
293 293 time.sleep(self._execute_sleep)
294 294
295 295 # Send the reply.
296 296 reply_msg = self.session.send(self.reply_socket, u'execute_reply',
297 297 reply_content, parent, ident=ident)
298 298 logger.debug(str(reply_msg))
299 299
300 300 if reply_msg['content']['status'] == u'error':
301 301 self._abort_queue()
302 302
303 303 status_msg = self.session.send(self.pub_socket,
304 304 u'status',
305 305 {u'execution_state':u'idle'},
306 306 parent=parent
307 307 )
308 308
309 309 def complete_request(self, ident, parent):
310 310 txt, matches = self._complete(parent)
311 311 matches = {'matches' : matches,
312 312 'matched_text' : txt,
313 313 'status' : 'ok'}
314 314 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
315 315 matches, parent, ident)
316 316 logger.debug(str(completion_msg))
317 317
318 318 def object_info_request(self, ident, parent):
319 319 object_info = self.shell.object_inspect(parent['content']['oname'])
320 320 # Before we send this object over, we scrub it for JSON usage
321 321 oinfo = json_clean(object_info)
322 322 msg = self.session.send(self.reply_socket, 'object_info_reply',
323 323 oinfo, parent, ident)
324 324 logger.debug(msg)
325 325
326 326 def history_tail_request(self, ident, parent):
327 327 # We need to pull these out, as passing **kwargs doesn't work with
328 328 # unicode keys before Python 2.6.5.
329 329 n = parent['content']['n']
330 330 raw = parent['content']['raw']
331 331 output = parent['content']['output']
332 332 hist = self.shell.history_manager.get_tail(n, raw=raw, output=output)
333 333 content = {'history' : list(hist)}
334 334 msg = self.session.send(self.reply_socket, 'history_tail_reply',
335 335 content, parent, ident)
336 336 logger.debug(str(msg))
337 337
338 338 def connect_request(self, ident, parent):
339 339 if self._recorded_ports is not None:
340 340 content = self._recorded_ports.copy()
341 341 else:
342 342 content = {}
343 343 msg = self.session.send(self.reply_socket, 'connect_reply',
344 344 content, parent, ident)
345 345 logger.debug(msg)
346 346
347 347 def shutdown_request(self, ident, parent):
348 348 self.shell.exit_now = True
349 349 self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent)
350 350 sys.exit(0)
351 351
352 352 #---------------------------------------------------------------------------
353 353 # Protected interface
354 354 #---------------------------------------------------------------------------
355 355
356 356 def _abort_queue(self):
357 357 while True:
358 358 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
359 359 if msg is None:
360 360 break
361 361 else:
362 362 assert ident is not None, \
363 363 "Unexpected missing message part."
364 364
365 365 logger.debug("Aborting:\n"+str(Message(msg)))
366 366 msg_type = msg['msg_type']
367 367 reply_type = msg_type.split('_')[0] + '_reply'
368 368 reply_msg = self.session.send(self.reply_socket, reply_type,
369 369 {'status' : 'aborted'}, msg, ident=ident)
370 370 logger.debug(reply_msg)
371 371 # We need to wait a bit for requests to come in. This can probably
372 372 # be set shorter for true asynchronous clients.
373 373 time.sleep(0.1)
374 374
375 375 def _raw_input(self, prompt, ident, parent):
376 376 # Flush output before making the request.
377 377 sys.stderr.flush()
378 378 sys.stdout.flush()
379 379
380 380 # Send the input request.
381 381 content = dict(prompt=prompt)
382 382 msg = self.session.send(self.req_socket, u'input_request', content, parent)
383 383
384 384 # Await a response.
385 385 ident, reply = self.session.recv(self.req_socket, 0)
386 386 try:
387 387 value = reply['content']['value']
388 388 except:
389 389 logger.error("Got bad raw_input reply: ")
390 390 logger.error(str(Message(parent)))
391 391 value = ''
392 392 return value
393 393
394 394 def _complete(self, msg):
395 395 c = msg['content']
396 396 try:
397 397 cpos = int(c['cursor_pos'])
398 398 except:
399 399 # If we don't get something that we can convert to an integer, at
400 400 # least attempt the completion guessing the cursor is at the end of
401 401 # the text, if there's any, and otherwise of the line
402 402 cpos = len(c['text'])
403 403 if cpos==0:
404 404 cpos = len(c['line'])
405 405 return self.shell.complete(c['text'], c['line'], cpos)
406 406
407 407 def _object_info(self, context):
408 408 symbol, leftover = self._symbol_from_context(context)
409 409 if symbol is not None and not leftover:
410 410 doc = getattr(symbol, '__doc__', '')
411 411 else:
412 412 doc = ''
413 413 object_info = dict(docstring = doc)
414 414 return object_info
415 415
416 416 def _symbol_from_context(self, context):
417 417 if not context:
418 418 return None, context
419 419
420 420 base_symbol_string = context[0]
421 421 symbol = self.shell.user_ns.get(base_symbol_string, None)
422 422 if symbol is None:
423 423 symbol = __builtin__.__dict__.get(base_symbol_string, None)
424 424 if symbol is None:
425 425 return None, context
426 426
427 427 context = context[1:]
428 428 for i, name in enumerate(context):
429 429 new_symbol = getattr(symbol, name, None)
430 430 if new_symbol is None:
431 431 return symbol, context[i:]
432 432 else:
433 433 symbol = new_symbol
434 434
435 435 return symbol, []
436 436
437 437 def _at_shutdown(self):
438 438 """Actions taken at shutdown by the kernel, called by python's atexit.
439 439 """
440 440 # io.rprint("Kernel at_shutdown") # dbg
441 441 if self._shutdown_message is not None:
442 442 self.session.send(self.reply_socket, self._shutdown_message)
443 443 self.session.send(self.pub_socket, self._shutdown_message)
444 444 logger.debug(str(self._shutdown_message))
445 445 # A very short sleep to give zmq time to flush its message buffers
446 446 # before Python truly shuts down.
447 447 time.sleep(0.01)
448 448
449 449
450 450 class QtKernel(Kernel):
451 451 """A Kernel subclass with Qt support."""
452 452
453 453 def start(self):
454 454 """Start a kernel with QtPy4 event loop integration."""
455 455
456 456 from PyQt4 import QtCore
457 457 from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
458 458
459 459 self.app = get_app_qt4([" "])
460 460 self.app.setQuitOnLastWindowClosed(False)
461 461 self.timer = QtCore.QTimer()
462 462 self.timer.timeout.connect(self.do_one_iteration)
463 463 # Units for the timer are in milliseconds
464 464 self.timer.start(1000*self._poll_interval)
465 465 start_event_loop_qt4(self.app)
466 466
467 467
468 468 class WxKernel(Kernel):
469 469 """A Kernel subclass with Wx support."""
470 470
471 471 def start(self):
472 472 """Start a kernel with wx event loop support."""
473 473
474 474 import wx
475 475 from IPython.lib.guisupport import start_event_loop_wx
476 476
477 477 doi = self.do_one_iteration
478 478 # Wx uses milliseconds
479 479 poll_interval = int(1000*self._poll_interval)
480 480
481 481 # We have to put the wx.Timer in a wx.Frame for it to fire properly.
482 482 # We make the Frame hidden when we create it in the main app below.
483 483 class TimerFrame(wx.Frame):
484 484 def __init__(self, func):
485 485 wx.Frame.__init__(self, None, -1)
486 486 self.timer = wx.Timer(self)
487 487 # Units for the timer are in milliseconds
488 488 self.timer.Start(poll_interval)
489 489 self.Bind(wx.EVT_TIMER, self.on_timer)
490 490 self.func = func
491 491
492 492 def on_timer(self, event):
493 493 self.func()
494 494
495 495 # We need a custom wx.App to create our Frame subclass that has the
496 496 # wx.Timer to drive the ZMQ event loop.
497 497 class IPWxApp(wx.App):
498 498 def OnInit(self):
499 499 self.frame = TimerFrame(doi)
500 500 self.frame.Show(False)
501 501 return True
502 502
503 503 # The redirect=False here makes sure that wx doesn't replace
504 504 # sys.stdout/stderr with its own classes.
505 505 self.app = IPWxApp(redirect=False)
506 506 start_event_loop_wx(self.app)
507 507
508 508
509 509 class TkKernel(Kernel):
510 510 """A Kernel subclass with Tk support."""
511 511
512 512 def start(self):
513 513 """Start a Tk enabled event loop."""
514 514
515 515 import Tkinter
516 516 doi = self.do_one_iteration
517 517 # Tk uses milliseconds
518 518 poll_interval = int(1000*self._poll_interval)
519 519 # For Tkinter, we create a Tk object and call its withdraw method.
520 520 class Timer(object):
521 521 def __init__(self, func):
522 522 self.app = Tkinter.Tk()
523 523 self.app.withdraw()
524 524 self.func = func
525 525
526 526 def on_timer(self):
527 527 self.func()
528 528 self.app.after(poll_interval, self.on_timer)
529 529
530 530 def start(self):
531 531 self.on_timer() # Call it once to get things going.
532 532 self.app.mainloop()
533 533
534 534 self.timer = Timer(doi)
535 535 self.timer.start()
536 536
537 537
538 538 class GTKKernel(Kernel):
539 539 """A Kernel subclass with GTK support."""
540 540
541 541 def start(self):
542 542 """Start the kernel, coordinating with the GTK event loop"""
543 543 from .gui.gtkembed import GTKEmbed
544 544
545 545 gtk_kernel = GTKEmbed(self)
546 546 gtk_kernel.start()
547 547
548 548
549 549 #-----------------------------------------------------------------------------
550 550 # Kernel main and launch functions
551 551 #-----------------------------------------------------------------------------
552 552
553 553 def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
554 554 independent=False, pylab=False, colors=None):
555 555 """Launches a localhost kernel, binding to the specified ports.
556 556
557 557 Parameters
558 558 ----------
559 559 ip : str, optional
560 560 The ip address the kernel will bind to.
561 561
562 562 xrep_port : int, optional
563 563 The port to use for XREP channel.
564 564
565 565 pub_port : int, optional
566 566 The port to use for the SUB channel.
567 567
568 568 req_port : int, optional
569 569 The port to use for the REQ (raw input) channel.
570 570
571 571 hb_port : int, optional
572 572 The port to use for the hearbeat REP channel.
573 573
574 574 independent : bool, optional (default False)
575 575 If set, the kernel process is guaranteed to survive if this process
576 576 dies. If not set, an effort is made to ensure that the kernel is killed
577 577 when this process dies. Note that in this case it is still good practice
578 578 to kill kernels manually before exiting.
579 579
580 580 pylab : bool or string, optional (default False)
581 581 If not False, the kernel will be launched with pylab enabled. If a
582 582 string is passed, matplotlib will use the specified backend. Otherwise,
583 583 matplotlib's default backend will be used.
584 584
585 585 colors : None or string, optional (default None)
586 586 If not None, specify the color scheme. One of (NoColor, LightBG, Linux)
587 587
588 588 Returns
589 589 -------
590 590 A tuple of form:
591 591 (kernel_process, xrep_port, pub_port, req_port)
592 592 where kernel_process is a Popen object and the ports are integers.
593 593 """
594 594 extra_arguments = []
595 595 if pylab:
596 596 extra_arguments.append('--pylab')
597 597 if isinstance(pylab, basestring):
598 598 extra_arguments.append(pylab)
599 599 if ip is not None:
600 600 extra_arguments.append('--ip')
601 601 if isinstance(ip, basestring):
602 602 extra_arguments.append(ip)
603 603 if colors is not None:
604 604 extra_arguments.append('--colors')
605 605 extra_arguments.append(colors)
606 606 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
607 607 xrep_port, pub_port, req_port, hb_port,
608 608 independent, extra_arguments)
609 609
610 610
611 611 def main():
612 612 """ The IPython kernel main entry point.
613 613 """
614 614 parser = make_argument_parser()
615 615 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
616 616 const='auto', help = \
617 617 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
618 618 given, the GUI backend is matplotlib's, otherwise use one of: \
619 619 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
620 620 parser.add_argument('--colors',
621 621 type=str, dest='colors',
622 622 help="Set the color scheme (NoColor, Linux, and LightBG).",
623 623 metavar='ZMQInteractiveShell.colors')
624 624 namespace = parser.parse_args()
625 625
626 626 kernel_class = Kernel
627 627
628 628 kernel_classes = {
629 629 'qt' : QtKernel,
630 630 'qt4': QtKernel,
631 631 'inline': Kernel,
632 632 'wx' : WxKernel,
633 633 'tk' : TkKernel,
634 634 'gtk': GTKKernel,
635 635 }
636 636 if namespace.pylab:
637 637 if namespace.pylab == 'auto':
638 638 gui, backend = pylabtools.find_gui_and_backend()
639 639 else:
640 640 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
641 641 kernel_class = kernel_classes.get(gui)
642 642 if kernel_class is None:
643 643 raise ValueError('GUI is not supported: %r' % gui)
644 644 pylabtools.activate_matplotlib(backend)
645 645 if namespace.colors:
646 646 ZMQInteractiveShell.colors=namespace.colors
647 647
648 648 kernel = make_kernel(namespace, kernel_class, OutStream)
649 649
650 650 if namespace.pylab:
651 651 pylabtools.import_pylab(kernel.shell.user_ns, backend,
652 652 shell=kernel.shell)
653 653
654 654 start_kernel(namespace, kernel)
655 655
656 656
657 657 if __name__ == '__main__':
658 658 main()
General Comments 0
You need to be logged in to leave comments. Login now