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