##// END OF EJS Templates
More work to address review comments....
Brian Granger -
Show More

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

@@ -1,2558 +1,2561 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Main IPython Component
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 9 # Copyright (C) 2008-2009 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from __future__ import with_statement
20 20 from __future__ import absolute_import
21 21
22 22 import __builtin__
23 23 import bdb
24 24 import codeop
25 25 import exceptions
26 26 import new
27 27 import os
28 28 import re
29 29 import string
30 30 import sys
31 31 import tempfile
32 32 from contextlib import nested
33 33
34 34 from IPython.core import debugger, oinspect
35 35 from IPython.core import history as ipcorehist
36 36 from IPython.core import prefilter
37 37 from IPython.core import shadowns
38 38 from IPython.core import ultratb
39 39 from IPython.core.alias import AliasManager
40 40 from IPython.core.builtin_trap import BuiltinTrap
41 41 from IPython.core.component import Component
42 42 from IPython.core.display_trap import DisplayTrap
43 43 from IPython.core.error import TryNext, UsageError
44 44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 45 from IPython.core.logger import Logger
46 46 from IPython.core.magic import Magic
47 47 from IPython.core.prefilter import PrefilterManager
48 48 from IPython.core.prompts import CachedOutput
49 49 from IPython.core.usage import interactive_usage, default_banner
50 50 import IPython.core.hooks
51 51 from IPython.external.Itpl import ItplNS
52 52 from IPython.lib.inputhook import enable_gui
53 53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 54 from IPython.lib.pylabtools import pylab_activate
55 55 from IPython.utils import PyColorize
56 56 from IPython.utils import pickleshare
57 57 from IPython.utils.doctestreload import doctest_reload
58 58 from IPython.utils.ipstruct import Struct
59 59 from IPython.utils.io import Term, ask_yes_no
60 60 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
61 61 from IPython.utils.process import (
62 62 abbrev_cwd,
63 63 getoutput,
64 64 getoutputerror
65 65 )
66 66 # import IPython.utils.rlineimpl as readline
67 67 from IPython.utils.strdispatch import StrDispatch
68 68 from IPython.utils.syspathcontext import prepended_to_syspath
69 69 from IPython.utils.terminal import toggle_set_term_title, set_term_title
70 70 from IPython.utils.warn import warn, error, fatal
71 71 from IPython.utils.traitlets import (
72 72 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
73 73 )
74 74
75 75 # from IPython.utils import growl
76 76 # growl.start("IPython")
77 77
78 78 #-----------------------------------------------------------------------------
79 79 # Globals
80 80 #-----------------------------------------------------------------------------
81 81
82 82 # store the builtin raw_input globally, and use this always, in case user code
83 83 # overwrites it (like wx.py.PyShell does)
84 84 raw_input_original = raw_input
85 85
86 86 # compiled regexps for autoindent management
87 87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88 88
89 89 #-----------------------------------------------------------------------------
90 90 # Utilities
91 91 #-----------------------------------------------------------------------------
92 92
93 93 ini_spaces_re = re.compile(r'^(\s+)')
94 94
95 95
96 96 def num_ini_spaces(strng):
97 97 """Return the number of initial spaces in a string"""
98 98
99 99 ini_spaces = ini_spaces_re.match(strng)
100 100 if ini_spaces:
101 101 return ini_spaces.end()
102 102 else:
103 103 return 0
104 104
105 105
106 106 def softspace(file, newvalue):
107 107 """Copied from code.py, to remove the dependency"""
108 108
109 109 oldvalue = 0
110 110 try:
111 111 oldvalue = file.softspace
112 112 except AttributeError:
113 113 pass
114 114 try:
115 115 file.softspace = newvalue
116 116 except (AttributeError, TypeError):
117 117 # "attribute-less object" or "read-only attributes"
118 118 pass
119 119 return oldvalue
120 120
121 121
122 122 def no_op(*a, **kw): pass
123 123
124 124 class SpaceInInput(exceptions.Exception): pass
125 125
126 126 class Bunch: pass
127 127
128 128 class InputList(list):
129 129 """Class to store user input.
130 130
131 131 It's basically a list, but slices return a string instead of a list, thus
132 132 allowing things like (assuming 'In' is an instance):
133 133
134 134 exec In[4:7]
135 135
136 136 or
137 137
138 138 exec In[5:9] + In[14] + In[21:25]"""
139 139
140 140 def __getslice__(self,i,j):
141 141 return ''.join(list.__getslice__(self,i,j))
142 142
143 143
144 144 class SyntaxTB(ultratb.ListTB):
145 145 """Extension which holds some state: the last exception value"""
146 146
147 147 def __init__(self,color_scheme = 'NoColor'):
148 148 ultratb.ListTB.__init__(self,color_scheme)
149 149 self.last_syntax_error = None
150 150
151 151 def __call__(self, etype, value, elist):
152 152 self.last_syntax_error = value
153 153 ultratb.ListTB.__call__(self,etype,value,elist)
154 154
155 155 def clear_err_state(self):
156 156 """Return the current error state and clear it"""
157 157 e = self.last_syntax_error
158 158 self.last_syntax_error = None
159 159 return e
160 160
161 161
162 162 def get_default_editor():
163 163 try:
164 164 ed = os.environ['EDITOR']
165 165 except KeyError:
166 166 if os.name == 'posix':
167 167 ed = 'vi' # the only one guaranteed to be there!
168 168 else:
169 169 ed = 'notepad' # same in Windows!
170 170 return ed
171 171
172 172
173 173 def get_default_colors():
174 174 if sys.platform=='darwin':
175 175 return "LightBG"
176 176 elif os.name=='nt':
177 177 return 'Linux'
178 178 else:
179 179 return 'Linux'
180 180
181 181
182 182 class SeparateStr(Str):
183 183 """A Str subclass to validate separate_in, separate_out, etc.
184 184
185 185 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
186 186 """
187 187
188 188 def validate(self, obj, value):
189 189 if value == '0': value = ''
190 190 value = value.replace('\\n','\n')
191 191 return super(SeparateStr, self).validate(obj, value)
192 192
193 193
194 194 #-----------------------------------------------------------------------------
195 195 # Main IPython class
196 196 #-----------------------------------------------------------------------------
197 197
198 198
199 199 class InteractiveShell(Component, Magic):
200 200 """An enhanced, interactive shell for Python."""
201 201
202 202 autocall = Enum((0,1,2), default_value=1, config=True)
203 203 autoedit_syntax = CBool(False, config=True)
204 204 autoindent = CBool(True, config=True)
205 205 automagic = CBool(True, config=True)
206 206 banner = Str('')
207 207 banner1 = Str(default_banner, config=True)
208 208 banner2 = Str('', config=True)
209 209 cache_size = Int(1000, config=True)
210 210 color_info = CBool(True, config=True)
211 211 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
212 212 default_value=get_default_colors(), config=True)
213 213 confirm_exit = CBool(True, config=True)
214 214 debug = CBool(False, config=True)
215 215 deep_reload = CBool(False, config=True)
216 216 # This display_banner only controls whether or not self.show_banner()
217 217 # is called when mainloop/interact are called. The default is False
218 218 # because for the terminal based application, the banner behavior
219 219 # is controlled by Global.display_banner, which IPythonApp looks at
220 220 # to determine if *it* should call show_banner() by hand or not.
221 221 display_banner = CBool(False) # This isn't configurable!
222 222 embedded = CBool(False)
223 223 embedded_active = CBool(False)
224 224 editor = Str(get_default_editor(), config=True)
225 225 filename = Str("<ipython console>")
226 226 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
227 227 logstart = CBool(False, config=True)
228 228 logfile = Str('', config=True)
229 229 logappend = Str('', config=True)
230 230 object_info_string_level = Enum((0,1,2), default_value=0,
231 231 config=True)
232 232 pager = Str('less', config=True)
233 233 pdb = CBool(False, config=True)
234 234 pprint = CBool(True, config=True)
235 235 profile = Str('', config=True)
236 236 prompt_in1 = Str('In [\\#]: ', config=True)
237 237 prompt_in2 = Str(' .\\D.: ', config=True)
238 238 prompt_out = Str('Out[\\#]: ', config=True)
239 239 prompts_pad_left = CBool(True, config=True)
240 240 quiet = CBool(False, config=True)
241 241
242 242 readline_use = CBool(True, config=True)
243 243 readline_merge_completions = CBool(True, config=True)
244 244 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
245 245 readline_remove_delims = Str('-/~', config=True)
246 246 readline_parse_and_bind = List([
247 247 'tab: complete',
248 248 '"\C-l": possible-completions',
249 249 'set show-all-if-ambiguous on',
250 250 '"\C-o": tab-insert',
251 251 '"\M-i": " "',
252 252 '"\M-o": "\d\d\d\d"',
253 253 '"\M-I": "\d\d\d\d"',
254 254 '"\C-r": reverse-search-history',
255 255 '"\C-s": forward-search-history',
256 256 '"\C-p": history-search-backward',
257 257 '"\C-n": history-search-forward',
258 258 '"\e[A": history-search-backward',
259 259 '"\e[B": history-search-forward',
260 260 '"\C-k": kill-line',
261 261 '"\C-u": unix-line-discard',
262 262 ], allow_none=False, config=True)
263 263
264 264 screen_length = Int(0, config=True)
265 265
266 266 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
267 267 separate_in = SeparateStr('\n', config=True)
268 268 separate_out = SeparateStr('', config=True)
269 269 separate_out2 = SeparateStr('', config=True)
270 270
271 271 system_header = Str('IPython system call: ', config=True)
272 272 system_verbose = CBool(False, config=True)
273 273 term_title = CBool(False, config=True)
274 274 wildcards_case_sensitive = CBool(True, config=True)
275 275 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
276 276 default_value='Context', config=True)
277 277
278 278 autoexec = List(allow_none=False)
279 279
280 280 # class attribute to indicate whether the class supports threads or not.
281 281 # Subclasses with thread support should override this as needed.
282 282 isthreaded = False
283 283
284 284 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
285 285 user_ns=None, user_global_ns=None,
286 286 banner1=None, banner2=None, display_banner=None,
287 287 custom_exceptions=((),None)):
288 288
289 289 # This is where traitlets with a config_key argument are updated
290 290 # from the values on config.
291 291 super(InteractiveShell, self).__init__(parent, config=config)
292 292
293 293 # These are relatively independent and stateless
294 294 self.init_ipython_dir(ipython_dir)
295 295 self.init_instance_attrs()
296 296 self.init_term_title()
297 297 self.init_usage(usage)
298 298 self.init_banner(banner1, banner2, display_banner)
299 299
300 300 # Create namespaces (user_ns, user_global_ns, etc.)
301 301 self.init_create_namespaces(user_ns, user_global_ns)
302 302 # This has to be done after init_create_namespaces because it uses
303 303 # something in self.user_ns, but before init_sys_modules, which
304 304 # is the first thing to modify sys.
305 305 self.save_sys_module_state()
306 306 self.init_sys_modules()
307 307
308 308 self.init_history()
309 309 self.init_encoding()
310 310 self.init_prefilter()
311 311
312 312 Magic.__init__(self, self)
313 313
314 314 self.init_syntax_highlighting()
315 315 self.init_hooks()
316 316 self.init_pushd_popd_magic()
317 317 self.init_traceback_handlers(custom_exceptions)
318 318 self.init_user_ns()
319 319 self.init_logger()
320 320 self.init_alias()
321 321 self.init_builtins()
322 322
323 323 # pre_config_initialization
324 324 self.init_shadow_hist()
325 325
326 326 # The next section should contain averything that was in ipmaker.
327 327 self.init_logstart()
328 328
329 329 # The following was in post_config_initialization
330 330 self.init_inspector()
331 331 self.init_readline()
332 332 self.init_prompts()
333 333 self.init_displayhook()
334 334 self.init_reload_doctest()
335 335 self.init_magics()
336 336 self.init_pdb()
337 337 self.hooks.late_startup_hook()
338 338
339 339 def get_ipython(self):
340 340 """Return the currently running IPython instance."""
341 341 return self
342 342
343 343 #-------------------------------------------------------------------------
344 344 # Traitlet changed handlers
345 345 #-------------------------------------------------------------------------
346 346
347 347 def _banner1_changed(self):
348 348 self.compute_banner()
349 349
350 350 def _banner2_changed(self):
351 351 self.compute_banner()
352 352
353 353 def _ipython_dir_changed(self, name, new):
354 354 if not os.path.isdir(new):
355 355 os.makedirs(new, mode = 0777)
356 356 if not os.path.isdir(self.ipython_extension_dir):
357 357 os.makedirs(self.ipython_extension_dir, mode = 0777)
358 358
359 359 @property
360 360 def ipython_extension_dir(self):
361 361 return os.path.join(self.ipython_dir, 'extensions')
362 362
363 363 @property
364 364 def usable_screen_length(self):
365 365 if self.screen_length == 0:
366 366 return 0
367 367 else:
368 368 num_lines_bot = self.separate_in.count('\n')+1
369 369 return self.screen_length - num_lines_bot
370 370
371 371 def _term_title_changed(self, name, new_value):
372 372 self.init_term_title()
373 373
374 374 def set_autoindent(self,value=None):
375 375 """Set the autoindent flag, checking for readline support.
376 376
377 377 If called with no arguments, it acts as a toggle."""
378 378
379 379 if not self.has_readline:
380 380 if os.name == 'posix':
381 381 warn("The auto-indent feature requires the readline library")
382 382 self.autoindent = 0
383 383 return
384 384 if value is None:
385 385 self.autoindent = not self.autoindent
386 386 else:
387 387 self.autoindent = value
388 388
389 389 #-------------------------------------------------------------------------
390 390 # init_* methods called by __init__
391 391 #-------------------------------------------------------------------------
392 392
393 393 def init_ipython_dir(self, ipython_dir):
394 394 if ipython_dir is not None:
395 395 self.ipython_dir = ipython_dir
396 396 self.config.Global.ipython_dir = self.ipython_dir
397 397 return
398 398
399 399 if hasattr(self.config.Global, 'ipython_dir'):
400 400 self.ipython_dir = self.config.Global.ipython_dir
401 401 else:
402 402 self.ipython_dir = get_ipython_dir()
403 403
404 404 # All children can just read this
405 405 self.config.Global.ipython_dir = self.ipython_dir
406 406
407 407 def init_instance_attrs(self):
408 408 self.jobs = BackgroundJobManager()
409 409 self.more = False
410 410
411 411 # command compiler
412 412 self.compile = codeop.CommandCompiler()
413 413
414 414 # User input buffer
415 415 self.buffer = []
416 416
417 417 # Make an empty namespace, which extension writers can rely on both
418 418 # existing and NEVER being used by ipython itself. This gives them a
419 419 # convenient location for storing additional information and state
420 420 # their extensions may require, without fear of collisions with other
421 421 # ipython names that may develop later.
422 422 self.meta = Struct()
423 423
424 424 # Object variable to store code object waiting execution. This is
425 425 # used mainly by the multithreaded shells, but it can come in handy in
426 426 # other situations. No need to use a Queue here, since it's a single
427 427 # item which gets cleared once run.
428 428 self.code_to_run = None
429 429
430 430 # Flag to mark unconditional exit
431 431 self.exit_now = False
432 432
433 433 # Temporary files used for various purposes. Deleted at exit.
434 434 self.tempfiles = []
435 435
436 436 # Keep track of readline usage (later set by init_readline)
437 437 self.has_readline = False
438 438
439 439 # keep track of where we started running (mainly for crash post-mortem)
440 440 # This is not being used anywhere currently.
441 441 self.starting_dir = os.getcwd()
442 442
443 443 # Indentation management
444 444 self.indent_current_nsp = 0
445 445
446 446 def init_term_title(self):
447 447 # Enable or disable the terminal title.
448 448 if self.term_title:
449 449 toggle_set_term_title(True)
450 450 set_term_title('IPython: ' + abbrev_cwd())
451 451 else:
452 452 toggle_set_term_title(False)
453 453
454 454 def init_usage(self, usage=None):
455 455 if usage is None:
456 456 self.usage = interactive_usage
457 457 else:
458 458 self.usage = usage
459 459
460 460 def init_encoding(self):
461 461 # Get system encoding at startup time. Certain terminals (like Emacs
462 462 # under Win32 have it set to None, and we need to have a known valid
463 463 # encoding to use in the raw_input() method
464 464 try:
465 465 self.stdin_encoding = sys.stdin.encoding or 'ascii'
466 466 except AttributeError:
467 467 self.stdin_encoding = 'ascii'
468 468
469 469 def init_syntax_highlighting(self):
470 470 # Python source parser/formatter for syntax highlighting
471 471 pyformat = PyColorize.Parser().format
472 472 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
473 473
474 474 def init_pushd_popd_magic(self):
475 475 # for pushd/popd management
476 476 try:
477 477 self.home_dir = get_home_dir()
478 478 except HomeDirError, msg:
479 479 fatal(msg)
480 480
481 481 self.dir_stack = []
482 482
483 483 def init_logger(self):
484 484 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
485 485 # local shortcut, this is used a LOT
486 486 self.log = self.logger.log
487 487
488 488 def init_logstart(self):
489 489 if self.logappend:
490 490 self.magic_logstart(self.logappend + ' append')
491 491 elif self.logfile:
492 492 self.magic_logstart(self.logfile)
493 493 elif self.logstart:
494 494 self.magic_logstart()
495 495
496 496 def init_builtins(self):
497 497 self.builtin_trap = BuiltinTrap(self)
498 498
499 499 def init_inspector(self):
500 500 # Object inspector
501 501 self.inspector = oinspect.Inspector(oinspect.InspectColors,
502 502 PyColorize.ANSICodeColors,
503 503 'NoColor',
504 504 self.object_info_string_level)
505 505
506 506 def init_prompts(self):
507 507 # Initialize cache, set in/out prompts and printing system
508 508 self.outputcache = CachedOutput(self,
509 509 self.cache_size,
510 510 self.pprint,
511 511 input_sep = self.separate_in,
512 512 output_sep = self.separate_out,
513 513 output_sep2 = self.separate_out2,
514 514 ps1 = self.prompt_in1,
515 515 ps2 = self.prompt_in2,
516 516 ps_out = self.prompt_out,
517 517 pad_left = self.prompts_pad_left)
518 518
519 519 # user may have over-ridden the default print hook:
520 520 try:
521 521 self.outputcache.__class__.display = self.hooks.display
522 522 except AttributeError:
523 523 pass
524 524
525 525 def init_displayhook(self):
526 526 self.display_trap = DisplayTrap(self, self.outputcache)
527 527
528 528 def init_reload_doctest(self):
529 529 # Do a proper resetting of doctest, including the necessary displayhook
530 530 # monkeypatching
531 531 try:
532 532 doctest_reload()
533 533 except ImportError:
534 534 warn("doctest module does not exist.")
535 535
536 536 #-------------------------------------------------------------------------
537 537 # Things related to the banner
538 538 #-------------------------------------------------------------------------
539 539
540 540 def init_banner(self, banner1, banner2, display_banner):
541 541 if banner1 is not None:
542 542 self.banner1 = banner1
543 543 if banner2 is not None:
544 544 self.banner2 = banner2
545 545 if display_banner is not None:
546 546 self.display_banner = display_banner
547 547 self.compute_banner()
548 548
549 549 def show_banner(self, banner=None):
550 550 if banner is None:
551 551 banner = self.banner
552 552 self.write(banner)
553 553
554 554 def compute_banner(self):
555 555 self.banner = self.banner1 + '\n'
556 556 if self.profile:
557 557 self.banner += '\nIPython profile: %s\n' % self.profile
558 558 if self.banner2:
559 559 self.banner += '\n' + self.banner2 + '\n'
560 560
561 561 #-------------------------------------------------------------------------
562 562 # Things related to injections into the sys module
563 563 #-------------------------------------------------------------------------
564 564
565 565 def save_sys_module_state(self):
566 566 """Save the state of hooks in the sys module.
567 567
568 568 This has to be called after self.user_ns is created.
569 569 """
570 570 self._orig_sys_module_state = {}
571 571 self._orig_sys_module_state['stdin'] = sys.stdin
572 572 self._orig_sys_module_state['stdout'] = sys.stdout
573 573 self._orig_sys_module_state['stderr'] = sys.stderr
574 574 self._orig_sys_module_state['excepthook'] = sys.excepthook
575 575 try:
576 576 self._orig_sys_modules_main_name = self.user_ns['__name__']
577 577 except KeyError:
578 578 pass
579 579
580 580 def restore_sys_module_state(self):
581 581 """Restore the state of the sys module."""
582 582 try:
583 583 for k, v in self._orig_sys_module_state.items():
584 584 setattr(sys, k, v)
585 585 except AttributeError:
586 586 pass
587 587 try:
588 588 delattr(sys, 'ipcompleter')
589 589 except AttributeError:
590 590 pass
591 591 # Reset what what done in self.init_sys_modules
592 592 try:
593 593 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
594 594 except (AttributeError, KeyError):
595 595 pass
596 596
597 597 #-------------------------------------------------------------------------
598 598 # Things related to hooks
599 599 #-------------------------------------------------------------------------
600 600
601 601 def init_hooks(self):
602 602 # hooks holds pointers used for user-side customizations
603 603 self.hooks = Struct()
604 604
605 605 self.strdispatchers = {}
606 606
607 607 # Set all default hooks, defined in the IPython.hooks module.
608 608 hooks = IPython.core.hooks
609 609 for hook_name in hooks.__all__:
610 610 # default hooks have priority 100, i.e. low; user hooks should have
611 611 # 0-100 priority
612 612 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
613 613
614 614 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
615 615 """set_hook(name,hook) -> sets an internal IPython hook.
616 616
617 617 IPython exposes some of its internal API as user-modifiable hooks. By
618 618 adding your function to one of these hooks, you can modify IPython's
619 619 behavior to call at runtime your own routines."""
620 620
621 621 # At some point in the future, this should validate the hook before it
622 622 # accepts it. Probably at least check that the hook takes the number
623 623 # of args it's supposed to.
624 624
625 625 f = new.instancemethod(hook,self,self.__class__)
626 626
627 627 # check if the hook is for strdispatcher first
628 628 if str_key is not None:
629 629 sdp = self.strdispatchers.get(name, StrDispatch())
630 630 sdp.add_s(str_key, f, priority )
631 631 self.strdispatchers[name] = sdp
632 632 return
633 633 if re_key is not None:
634 634 sdp = self.strdispatchers.get(name, StrDispatch())
635 635 sdp.add_re(re.compile(re_key), f, priority )
636 636 self.strdispatchers[name] = sdp
637 637 return
638 638
639 639 dp = getattr(self.hooks, name, None)
640 640 if name not in IPython.core.hooks.__all__:
641 641 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
642 642 if not dp:
643 643 dp = IPython.core.hooks.CommandChainDispatcher()
644 644
645 645 try:
646 646 dp.add(f,priority)
647 647 except AttributeError:
648 648 # it was not commandchain, plain old func - replace
649 649 dp = f
650 650
651 651 setattr(self.hooks,name, dp)
652 652
653 653 #-------------------------------------------------------------------------
654 654 # Things related to the "main" module
655 655 #-------------------------------------------------------------------------
656 656
657 657 def new_main_mod(self,ns=None):
658 658 """Return a new 'main' module object for user code execution.
659 659 """
660 660 main_mod = self._user_main_module
661 661 init_fakemod_dict(main_mod,ns)
662 662 return main_mod
663 663
664 664 def cache_main_mod(self,ns,fname):
665 665 """Cache a main module's namespace.
666 666
667 667 When scripts are executed via %run, we must keep a reference to the
668 668 namespace of their __main__ module (a FakeModule instance) around so
669 669 that Python doesn't clear it, rendering objects defined therein
670 670 useless.
671 671
672 672 This method keeps said reference in a private dict, keyed by the
673 673 absolute path of the module object (which corresponds to the script
674 674 path). This way, for multiple executions of the same script we only
675 675 keep one copy of the namespace (the last one), thus preventing memory
676 676 leaks from old references while allowing the objects from the last
677 677 execution to be accessible.
678 678
679 679 Note: we can not allow the actual FakeModule instances to be deleted,
680 680 because of how Python tears down modules (it hard-sets all their
681 681 references to None without regard for reference counts). This method
682 682 must therefore make a *copy* of the given namespace, to allow the
683 683 original module's __dict__ to be cleared and reused.
684 684
685 685
686 686 Parameters
687 687 ----------
688 688 ns : a namespace (a dict, typically)
689 689
690 690 fname : str
691 691 Filename associated with the namespace.
692 692
693 693 Examples
694 694 --------
695 695
696 696 In [10]: import IPython
697 697
698 698 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
699 699
700 700 In [12]: IPython.__file__ in _ip._main_ns_cache
701 701 Out[12]: True
702 702 """
703 703 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
704 704
705 705 def clear_main_mod_cache(self):
706 706 """Clear the cache of main modules.
707 707
708 708 Mainly for use by utilities like %reset.
709 709
710 710 Examples
711 711 --------
712 712
713 713 In [15]: import IPython
714 714
715 715 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
716 716
717 717 In [17]: len(_ip._main_ns_cache) > 0
718 718 Out[17]: True
719 719
720 720 In [18]: _ip.clear_main_mod_cache()
721 721
722 722 In [19]: len(_ip._main_ns_cache) == 0
723 723 Out[19]: True
724 724 """
725 725 self._main_ns_cache.clear()
726 726
727 727 #-------------------------------------------------------------------------
728 728 # Things related to debugging
729 729 #-------------------------------------------------------------------------
730 730
731 731 def init_pdb(self):
732 732 # Set calling of pdb on exceptions
733 733 # self.call_pdb is a property
734 734 self.call_pdb = self.pdb
735 735
736 736 def _get_call_pdb(self):
737 737 return self._call_pdb
738 738
739 739 def _set_call_pdb(self,val):
740 740
741 741 if val not in (0,1,False,True):
742 742 raise ValueError,'new call_pdb value must be boolean'
743 743
744 744 # store value in instance
745 745 self._call_pdb = val
746 746
747 747 # notify the actual exception handlers
748 748 self.InteractiveTB.call_pdb = val
749 749 if self.isthreaded:
750 750 try:
751 751 self.sys_excepthook.call_pdb = val
752 752 except:
753 753 warn('Failed to activate pdb for threaded exception handler')
754 754
755 755 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
756 756 'Control auto-activation of pdb at exceptions')
757 757
758 758 def debugger(self,force=False):
759 759 """Call the pydb/pdb debugger.
760 760
761 761 Keywords:
762 762
763 763 - force(False): by default, this routine checks the instance call_pdb
764 764 flag and does not actually invoke the debugger if the flag is false.
765 765 The 'force' option forces the debugger to activate even if the flag
766 766 is false.
767 767 """
768 768
769 769 if not (force or self.call_pdb):
770 770 return
771 771
772 772 if not hasattr(sys,'last_traceback'):
773 773 error('No traceback has been produced, nothing to debug.')
774 774 return
775 775
776 776 # use pydb if available
777 777 if debugger.has_pydb:
778 778 from pydb import pm
779 779 else:
780 780 # fallback to our internal debugger
781 781 pm = lambda : self.InteractiveTB.debugger(force=True)
782 782 self.history_saving_wrapper(pm)()
783 783
784 784 #-------------------------------------------------------------------------
785 785 # Things related to IPython's various namespaces
786 786 #-------------------------------------------------------------------------
787 787
788 788 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
789 789 # Create the namespace where the user will operate. user_ns is
790 790 # normally the only one used, and it is passed to the exec calls as
791 791 # the locals argument. But we do carry a user_global_ns namespace
792 792 # given as the exec 'globals' argument, This is useful in embedding
793 793 # situations where the ipython shell opens in a context where the
794 794 # distinction between locals and globals is meaningful. For
795 795 # non-embedded contexts, it is just the same object as the user_ns dict.
796 796
797 797 # FIXME. For some strange reason, __builtins__ is showing up at user
798 798 # level as a dict instead of a module. This is a manual fix, but I
799 799 # should really track down where the problem is coming from. Alex
800 800 # Schmolck reported this problem first.
801 801
802 802 # A useful post by Alex Martelli on this topic:
803 803 # Re: inconsistent value from __builtins__
804 804 # Von: Alex Martelli <aleaxit@yahoo.com>
805 805 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
806 806 # Gruppen: comp.lang.python
807 807
808 808 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
809 809 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
810 810 # > <type 'dict'>
811 811 # > >>> print type(__builtins__)
812 812 # > <type 'module'>
813 813 # > Is this difference in return value intentional?
814 814
815 815 # Well, it's documented that '__builtins__' can be either a dictionary
816 816 # or a module, and it's been that way for a long time. Whether it's
817 817 # intentional (or sensible), I don't know. In any case, the idea is
818 818 # that if you need to access the built-in namespace directly, you
819 819 # should start with "import __builtin__" (note, no 's') which will
820 820 # definitely give you a module. Yeah, it's somewhat confusing:-(.
821 821
822 822 # These routines return properly built dicts as needed by the rest of
823 823 # the code, and can also be used by extension writers to generate
824 824 # properly initialized namespaces.
825 825 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
826 826
827 827 # Assign namespaces
828 828 # This is the namespace where all normal user variables live
829 829 self.user_ns = user_ns
830 830 self.user_global_ns = user_global_ns
831 831
832 832 # An auxiliary namespace that checks what parts of the user_ns were
833 833 # loaded at startup, so we can list later only variables defined in
834 834 # actual interactive use. Since it is always a subset of user_ns, it
835 835 # doesn't need to be separately tracked in the ns_table.
836 self.user_config_ns = {}
836 self.user_ns_hidden = {}
837 837
838 838 # A namespace to keep track of internal data structures to prevent
839 839 # them from cluttering user-visible stuff. Will be updated later
840 840 self.internal_ns = {}
841 841
842 842 # Now that FakeModule produces a real module, we've run into a nasty
843 843 # problem: after script execution (via %run), the module where the user
844 844 # code ran is deleted. Now that this object is a true module (needed
845 845 # so docetst and other tools work correctly), the Python module
846 846 # teardown mechanism runs over it, and sets to None every variable
847 847 # present in that module. Top-level references to objects from the
848 848 # script survive, because the user_ns is updated with them. However,
849 849 # calling functions defined in the script that use other things from
850 850 # the script will fail, because the function's closure had references
851 851 # to the original objects, which are now all None. So we must protect
852 852 # these modules from deletion by keeping a cache.
853 853 #
854 854 # To avoid keeping stale modules around (we only need the one from the
855 855 # last run), we use a dict keyed with the full path to the script, so
856 856 # only the last version of the module is held in the cache. Note,
857 857 # however, that we must cache the module *namespace contents* (their
858 858 # __dict__). Because if we try to cache the actual modules, old ones
859 859 # (uncached) could be destroyed while still holding references (such as
860 860 # those held by GUI objects that tend to be long-lived)>
861 861 #
862 862 # The %reset command will flush this cache. See the cache_main_mod()
863 863 # and clear_main_mod_cache() methods for details on use.
864 864
865 865 # This is the cache used for 'main' namespaces
866 866 self._main_ns_cache = {}
867 867 # And this is the single instance of FakeModule whose __dict__ we keep
868 868 # copying and clearing for reuse on each %run
869 869 self._user_main_module = FakeModule()
870 870
871 871 # A table holding all the namespaces IPython deals with, so that
872 872 # introspection facilities can search easily.
873 873 self.ns_table = {'user':user_ns,
874 874 'user_global':user_global_ns,
875 875 'internal':self.internal_ns,
876 876 'builtin':__builtin__.__dict__
877 877 }
878 878
879 879 # Similarly, track all namespaces where references can be held and that
880 880 # we can safely clear (so it can NOT include builtin). This one can be
881 881 # a simple list.
882 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
882 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
883 883 self.internal_ns, self._main_ns_cache ]
884 884
885 885 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
886 886 """Return a valid local and global user interactive namespaces.
887 887
888 888 This builds a dict with the minimal information needed to operate as a
889 889 valid IPython user namespace, which you can pass to the various
890 890 embedding classes in ipython. The default implementation returns the
891 891 same dict for both the locals and the globals to allow functions to
892 892 refer to variables in the namespace. Customized implementations can
893 893 return different dicts. The locals dictionary can actually be anything
894 894 following the basic mapping protocol of a dict, but the globals dict
895 895 must be a true dict, not even a subclass. It is recommended that any
896 896 custom object for the locals namespace synchronize with the globals
897 897 dict somehow.
898 898
899 899 Raises TypeError if the provided globals namespace is not a true dict.
900 900
901 901 Parameters
902 902 ----------
903 903 user_ns : dict-like, optional
904 904 The current user namespace. The items in this namespace should
905 905 be included in the output. If None, an appropriate blank
906 906 namespace should be created.
907 907 user_global_ns : dict, optional
908 908 The current user global namespace. The items in this namespace
909 909 should be included in the output. If None, an appropriate
910 910 blank namespace should be created.
911 911
912 912 Returns
913 913 -------
914 914 A pair of dictionary-like object to be used as the local namespace
915 915 of the interpreter and a dict to be used as the global namespace.
916 916 """
917 917
918 918
919 919 # We must ensure that __builtin__ (without the final 's') is always
920 920 # available and pointing to the __builtin__ *module*. For more details:
921 921 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
922 922
923 923 if user_ns is None:
924 924 # Set __name__ to __main__ to better match the behavior of the
925 925 # normal interpreter.
926 926 user_ns = {'__name__' :'__main__',
927 927 '__builtin__' : __builtin__,
928 928 '__builtins__' : __builtin__,
929 929 }
930 930 else:
931 931 user_ns.setdefault('__name__','__main__')
932 932 user_ns.setdefault('__builtin__',__builtin__)
933 933 user_ns.setdefault('__builtins__',__builtin__)
934 934
935 935 if user_global_ns is None:
936 936 user_global_ns = user_ns
937 937 if type(user_global_ns) is not dict:
938 938 raise TypeError("user_global_ns must be a true dict; got %r"
939 939 % type(user_global_ns))
940 940
941 941 return user_ns, user_global_ns
942 942
943 943 def init_sys_modules(self):
944 944 # We need to insert into sys.modules something that looks like a
945 945 # module but which accesses the IPython namespace, for shelve and
946 946 # pickle to work interactively. Normally they rely on getting
947 947 # everything out of __main__, but for embedding purposes each IPython
948 948 # instance has its own private namespace, so we can't go shoving
949 949 # everything into __main__.
950 950
951 951 # note, however, that we should only do this for non-embedded
952 952 # ipythons, which really mimic the __main__.__dict__ with their own
953 953 # namespace. Embedded instances, on the other hand, should not do
954 954 # this because they need to manage the user local/global namespaces
955 955 # only, but they live within a 'normal' __main__ (meaning, they
956 956 # shouldn't overtake the execution environment of the script they're
957 957 # embedded in).
958 958
959 959 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
960 960
961 961 try:
962 962 main_name = self.user_ns['__name__']
963 963 except KeyError:
964 964 raise KeyError('user_ns dictionary MUST have a "__name__" key')
965 965 else:
966 966 sys.modules[main_name] = FakeModule(self.user_ns)
967 967
968 968 def init_user_ns(self):
969 969 """Initialize all user-visible namespaces to their minimum defaults.
970 970
971 971 Certain history lists are also initialized here, as they effectively
972 972 act as user namespaces.
973 973
974 974 Notes
975 975 -----
976 976 All data structures here are only filled in, they are NOT reset by this
977 977 method. If they were not empty before, data will simply be added to
978 978 therm.
979 979 """
980 980 # This function works in two parts: first we put a few things in
981 # user_ns, and we sync that contents into user_config_ns so that these
981 # user_ns, and we sync that contents into user_ns_hidden so that these
982 982 # initial variables aren't shown by %who. After the sync, we add the
983 983 # rest of what we *do* want the user to see with %who even on a new
984 984 # session (probably nothing, so theye really only see their own stuff)
985 985
986 986 # The user dict must *always* have a __builtin__ reference to the
987 987 # Python standard __builtin__ namespace, which must be imported.
988 988 # This is so that certain operations in prompt evaluation can be
989 989 # reliably executed with builtins. Note that we can NOT use
990 990 # __builtins__ (note the 's'), because that can either be a dict or a
991 991 # module, and can even mutate at runtime, depending on the context
992 992 # (Python makes no guarantees on it). In contrast, __builtin__ is
993 993 # always a module object, though it must be explicitly imported.
994 994
995 995 # For more details:
996 996 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
997 997 ns = dict(__builtin__ = __builtin__)
998 998
999 999 # Put 'help' in the user namespace
1000 1000 try:
1001 1001 from site import _Helper
1002 1002 ns['help'] = _Helper()
1003 1003 except ImportError:
1004 1004 warn('help() not available - check site.py')
1005 1005
1006 1006 # make global variables for user access to the histories
1007 1007 ns['_ih'] = self.input_hist
1008 1008 ns['_oh'] = self.output_hist
1009 1009 ns['_dh'] = self.dir_hist
1010 1010
1011 1011 ns['_sh'] = shadowns
1012 1012
1013 1013 # user aliases to input and output histories. These shouldn't show up
1014 1014 # in %who, as they can have very large reprs.
1015 1015 ns['In'] = self.input_hist
1016 1016 ns['Out'] = self.output_hist
1017 1017
1018 1018 # Store myself as the public api!!!
1019 1019 ns['get_ipython'] = self.get_ipython
1020 1020
1021 # Sync what we've added so far to user_config_ns so these aren't seen
1021 # Sync what we've added so far to user_ns_hidden so these aren't seen
1022 1022 # by %who
1023 self.user_config_ns.update(ns)
1023 self.user_ns_hidden.update(ns)
1024 1024
1025 1025 # Anything put into ns now would show up in %who. Think twice before
1026 1026 # putting anything here, as we really want %who to show the user their
1027 1027 # stuff, not our variables.
1028 1028
1029 1029 # Finally, update the real user's namespace
1030 1030 self.user_ns.update(ns)
1031 1031
1032 1032
1033 1033 def reset(self):
1034 1034 """Clear all internal namespaces.
1035 1035
1036 1036 Note that this is much more aggressive than %reset, since it clears
1037 1037 fully all namespaces, as well as all input/output lists.
1038 1038 """
1039 1039 for ns in self.ns_refs_table:
1040 1040 ns.clear()
1041 1041
1042 1042 self.alias_manager.clear_aliases()
1043 1043
1044 1044 # Clear input and output histories
1045 1045 self.input_hist[:] = []
1046 1046 self.input_hist_raw[:] = []
1047 1047 self.output_hist.clear()
1048 1048
1049 1049 # Restore the user namespaces to minimal usability
1050 1050 self.init_user_ns()
1051 1051
1052 1052 # Restore the default and user aliases
1053 1053 self.alias_manager.init_aliases()
1054 1054
1055 1055 def push(self, variables, interactive=True):
1056 1056 """Inject a group of variables into the IPython user namespace.
1057 1057
1058 1058 Parameters
1059 1059 ----------
1060 1060 variables : dict, str or list/tuple of str
1061 1061 The variables to inject into the user's namespace. If a dict,
1062 1062 a simple update is done. If a str, the string is assumed to
1063 1063 have variable names separated by spaces. A list/tuple of str
1064 1064 can also be used to give the variable names. If just the variable
1065 1065 names are give (list/tuple/str) then the variable values looked
1066 1066 up in the callers frame.
1067 1067 interactive : bool
1068 1068 If True (default), the variables will be listed with the ``who``
1069 1069 magic.
1070 1070 """
1071 1071 vdict = None
1072 1072
1073 1073 # We need a dict of name/value pairs to do namespace updates.
1074 1074 if isinstance(variables, dict):
1075 1075 vdict = variables
1076 1076 elif isinstance(variables, (basestring, list, tuple)):
1077 1077 if isinstance(variables, basestring):
1078 1078 vlist = variables.split()
1079 1079 else:
1080 1080 vlist = variables
1081 1081 vdict = {}
1082 1082 cf = sys._getframe(1)
1083 1083 for name in vlist:
1084 1084 try:
1085 1085 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1086 1086 except:
1087 1087 print ('Could not get variable %s from %s' %
1088 1088 (name,cf.f_code.co_name))
1089 1089 else:
1090 1090 raise ValueError('variables must be a dict/str/list/tuple')
1091 1091
1092 1092 # Propagate variables to user namespace
1093 1093 self.user_ns.update(vdict)
1094 1094
1095 1095 # And configure interactive visibility
1096 config_ns = self.user_config_ns
1096 config_ns = self.user_ns_hidden
1097 1097 if interactive:
1098 1098 for name, val in vdict.iteritems():
1099 1099 config_ns.pop(name, None)
1100 1100 else:
1101 1101 for name,val in vdict.iteritems():
1102 1102 config_ns[name] = val
1103 1103
1104 1104 #-------------------------------------------------------------------------
1105 1105 # Things related to history management
1106 1106 #-------------------------------------------------------------------------
1107 1107
1108 1108 def init_history(self):
1109 1109 # List of input with multi-line handling.
1110 1110 self.input_hist = InputList()
1111 1111 # This one will hold the 'raw' input history, without any
1112 1112 # pre-processing. This will allow users to retrieve the input just as
1113 1113 # it was exactly typed in by the user, with %hist -r.
1114 1114 self.input_hist_raw = InputList()
1115 1115
1116 1116 # list of visited directories
1117 1117 try:
1118 1118 self.dir_hist = [os.getcwd()]
1119 1119 except OSError:
1120 1120 self.dir_hist = []
1121 1121
1122 1122 # dict of output history
1123 1123 self.output_hist = {}
1124 1124
1125 1125 # Now the history file
1126 1126 if self.profile:
1127 1127 histfname = 'history-%s' % self.profile
1128 1128 else:
1129 1129 histfname = 'history'
1130 1130 self.histfile = os.path.join(self.ipython_dir, histfname)
1131 1131
1132 1132 # Fill the history zero entry, user counter starts at 1
1133 1133 self.input_hist.append('\n')
1134 1134 self.input_hist_raw.append('\n')
1135 1135
1136 1136 def init_shadow_hist(self):
1137 1137 try:
1138 1138 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1139 1139 except exceptions.UnicodeDecodeError:
1140 1140 print "Your ipython_dir can't be decoded to unicode!"
1141 1141 print "Please set HOME environment variable to something that"
1142 1142 print r"only has ASCII characters, e.g. c:\home"
1143 1143 print "Now it is", self.ipython_dir
1144 1144 sys.exit()
1145 1145 self.shadowhist = ipcorehist.ShadowHist(self.db)
1146 1146
1147 1147 def savehist(self):
1148 1148 """Save input history to a file (via readline library)."""
1149 1149
1150 1150 try:
1151 1151 self.readline.write_history_file(self.histfile)
1152 1152 except:
1153 1153 print 'Unable to save IPython command history to file: ' + \
1154 1154 `self.histfile`
1155 1155
1156 1156 def reloadhist(self):
1157 1157 """Reload the input history from disk file."""
1158 1158
1159 1159 try:
1160 1160 self.readline.clear_history()
1161 1161 self.readline.read_history_file(self.shell.histfile)
1162 1162 except AttributeError:
1163 1163 pass
1164 1164
1165 1165 def history_saving_wrapper(self, func):
1166 1166 """ Wrap func for readline history saving
1167 1167
1168 1168 Convert func into callable that saves & restores
1169 1169 history around the call """
1170 1170
1171 1171 if self.has_readline:
1172 1172 from IPython.utils import rlineimpl as readline
1173 1173 else:
1174 1174 return func
1175 1175
1176 1176 def wrapper():
1177 1177 self.savehist()
1178 1178 try:
1179 1179 func()
1180 1180 finally:
1181 1181 readline.read_history_file(self.histfile)
1182 1182 return wrapper
1183 1183
1184 1184 #-------------------------------------------------------------------------
1185 1185 # Things related to exception handling and tracebacks (not debugging)
1186 1186 #-------------------------------------------------------------------------
1187 1187
1188 1188 def init_traceback_handlers(self, custom_exceptions):
1189 1189 # Syntax error handler.
1190 1190 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1191 1191
1192 1192 # The interactive one is initialized with an offset, meaning we always
1193 1193 # want to remove the topmost item in the traceback, which is our own
1194 1194 # internal code. Valid modes: ['Plain','Context','Verbose']
1195 1195 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1196 1196 color_scheme='NoColor',
1197 1197 tb_offset = 1)
1198 1198
1199 1199 # The instance will store a pointer to the system-wide exception hook,
1200 1200 # so that runtime code (such as magics) can access it. This is because
1201 1201 # during the read-eval loop, it may get temporarily overwritten.
1202 1202 self.sys_excepthook = sys.excepthook
1203 1203
1204 1204 # and add any custom exception handlers the user may have specified
1205 1205 self.set_custom_exc(*custom_exceptions)
1206 1206
1207 1207 # Set the exception mode
1208 1208 self.InteractiveTB.set_mode(mode=self.xmode)
1209 1209
1210 1210 def set_custom_exc(self,exc_tuple,handler):
1211 1211 """set_custom_exc(exc_tuple,handler)
1212 1212
1213 1213 Set a custom exception handler, which will be called if any of the
1214 1214 exceptions in exc_tuple occur in the mainloop (specifically, in the
1215 1215 runcode() method.
1216 1216
1217 1217 Inputs:
1218 1218
1219 1219 - exc_tuple: a *tuple* of valid exceptions to call the defined
1220 1220 handler for. It is very important that you use a tuple, and NOT A
1221 1221 LIST here, because of the way Python's except statement works. If
1222 1222 you only want to trap a single exception, use a singleton tuple:
1223 1223
1224 1224 exc_tuple == (MyCustomException,)
1225 1225
1226 1226 - handler: this must be defined as a function with the following
1227 1227 basic interface: def my_handler(self,etype,value,tb).
1228 1228
1229 1229 This will be made into an instance method (via new.instancemethod)
1230 1230 of IPython itself, and it will be called if any of the exceptions
1231 1231 listed in the exc_tuple are caught. If the handler is None, an
1232 1232 internal basic one is used, which just prints basic info.
1233 1233
1234 1234 WARNING: by putting in your own exception handler into IPython's main
1235 1235 execution loop, you run a very good chance of nasty crashes. This
1236 1236 facility should only be used if you really know what you are doing."""
1237 1237
1238 1238 assert type(exc_tuple)==type(()) , \
1239 1239 "The custom exceptions must be given AS A TUPLE."
1240 1240
1241 1241 def dummy_handler(self,etype,value,tb):
1242 1242 print '*** Simple custom exception handler ***'
1243 1243 print 'Exception type :',etype
1244 1244 print 'Exception value:',value
1245 1245 print 'Traceback :',tb
1246 1246 print 'Source code :','\n'.join(self.buffer)
1247 1247
1248 1248 if handler is None: handler = dummy_handler
1249 1249
1250 1250 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1251 1251 self.custom_exceptions = exc_tuple
1252 1252
1253 1253 def excepthook(self, etype, value, tb):
1254 1254 """One more defense for GUI apps that call sys.excepthook.
1255 1255
1256 1256 GUI frameworks like wxPython trap exceptions and call
1257 1257 sys.excepthook themselves. I guess this is a feature that
1258 1258 enables them to keep running after exceptions that would
1259 1259 otherwise kill their mainloop. This is a bother for IPython
1260 1260 which excepts to catch all of the program exceptions with a try:
1261 1261 except: statement.
1262 1262
1263 1263 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1264 1264 any app directly invokes sys.excepthook, it will look to the user like
1265 1265 IPython crashed. In order to work around this, we can disable the
1266 1266 CrashHandler and replace it with this excepthook instead, which prints a
1267 1267 regular traceback using our InteractiveTB. In this fashion, apps which
1268 1268 call sys.excepthook will generate a regular-looking exception from
1269 1269 IPython, and the CrashHandler will only be triggered by real IPython
1270 1270 crashes.
1271 1271
1272 1272 This hook should be used sparingly, only in places which are not likely
1273 1273 to be true IPython errors.
1274 1274 """
1275 1275 self.showtraceback((etype,value,tb),tb_offset=0)
1276 1276
1277 1277 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1278 1278 exception_only=False):
1279 1279 """Display the exception that just occurred.
1280 1280
1281 1281 If nothing is known about the exception, this is the method which
1282 1282 should be used throughout the code for presenting user tracebacks,
1283 1283 rather than directly invoking the InteractiveTB object.
1284 1284
1285 1285 A specific showsyntaxerror() also exists, but this method can take
1286 1286 care of calling it if needed, so unless you are explicitly catching a
1287 1287 SyntaxError exception, don't try to analyze the stack manually and
1288 1288 simply call this method."""
1289 1289
1290 1290 try:
1291 1291 if exc_tuple is None:
1292 1292 etype, value, tb = sys.exc_info()
1293 1293 else:
1294 1294 etype, value, tb = exc_tuple
1295 1295
1296 1296 if etype is None:
1297 1297 if hasattr(sys, 'last_type'):
1298 1298 etype, value, tb = sys.last_type, sys.last_value, \
1299 1299 sys.last_traceback
1300 1300 else:
1301 1301 self.write('No traceback available to show.\n')
1302 1302 return
1303 1303
1304 1304 if etype is SyntaxError:
1305 1305 # Though this won't be called by syntax errors in the input
1306 1306 # line, there may be SyntaxError cases whith imported code.
1307 1307 self.showsyntaxerror(filename)
1308 1308 elif etype is UsageError:
1309 1309 print "UsageError:", value
1310 1310 else:
1311 1311 # WARNING: these variables are somewhat deprecated and not
1312 1312 # necessarily safe to use in a threaded environment, but tools
1313 1313 # like pdb depend on their existence, so let's set them. If we
1314 1314 # find problems in the field, we'll need to revisit their use.
1315 1315 sys.last_type = etype
1316 1316 sys.last_value = value
1317 1317 sys.last_traceback = tb
1318 1318
1319 1319 if etype in self.custom_exceptions:
1320 1320 self.CustomTB(etype,value,tb)
1321 1321 else:
1322 1322 if exception_only:
1323 1323 m = ('An exception has occurred, use %tb to see the '
1324 1324 'full traceback.')
1325 1325 print m
1326 1326 self.InteractiveTB.show_exception_only(etype, value)
1327 1327 else:
1328 1328 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1329 1329 if self.InteractiveTB.call_pdb:
1330 1330 # pdb mucks up readline, fix it back
1331 1331 self.set_completer()
1332 1332
1333 1333 except KeyboardInterrupt:
1334 1334 self.write("\nKeyboardInterrupt\n")
1335 1335
1336 1336
1337 1337 def showsyntaxerror(self, filename=None):
1338 1338 """Display the syntax error that just occurred.
1339 1339
1340 1340 This doesn't display a stack trace because there isn't one.
1341 1341
1342 1342 If a filename is given, it is stuffed in the exception instead
1343 1343 of what was there before (because Python's parser always uses
1344 1344 "<string>" when reading from a string).
1345 1345 """
1346 1346 etype, value, last_traceback = sys.exc_info()
1347 1347
1348 1348 # See note about these variables in showtraceback() above
1349 1349 sys.last_type = etype
1350 1350 sys.last_value = value
1351 1351 sys.last_traceback = last_traceback
1352 1352
1353 1353 if filename and etype is SyntaxError:
1354 1354 # Work hard to stuff the correct filename in the exception
1355 1355 try:
1356 1356 msg, (dummy_filename, lineno, offset, line) = value
1357 1357 except:
1358 1358 # Not the format we expect; leave it alone
1359 1359 pass
1360 1360 else:
1361 1361 # Stuff in the right filename
1362 1362 try:
1363 1363 # Assume SyntaxError is a class exception
1364 1364 value = SyntaxError(msg, (filename, lineno, offset, line))
1365 1365 except:
1366 1366 # If that failed, assume SyntaxError is a string
1367 1367 value = msg, (filename, lineno, offset, line)
1368 1368 self.SyntaxTB(etype,value,[])
1369 1369
1370 1370 def edit_syntax_error(self):
1371 1371 """The bottom half of the syntax error handler called in the main loop.
1372 1372
1373 1373 Loop until syntax error is fixed or user cancels.
1374 1374 """
1375 1375
1376 1376 while self.SyntaxTB.last_syntax_error:
1377 1377 # copy and clear last_syntax_error
1378 1378 err = self.SyntaxTB.clear_err_state()
1379 1379 if not self._should_recompile(err):
1380 1380 return
1381 1381 try:
1382 1382 # may set last_syntax_error again if a SyntaxError is raised
1383 1383 self.safe_execfile(err.filename,self.user_ns)
1384 1384 except:
1385 1385 self.showtraceback()
1386 1386 else:
1387 1387 try:
1388 1388 f = file(err.filename)
1389 1389 try:
1390 1390 # This should be inside a display_trap block and I
1391 1391 # think it is.
1392 1392 sys.displayhook(f.read())
1393 1393 finally:
1394 1394 f.close()
1395 1395 except:
1396 1396 self.showtraceback()
1397 1397
1398 1398 def _should_recompile(self,e):
1399 1399 """Utility routine for edit_syntax_error"""
1400 1400
1401 1401 if e.filename in ('<ipython console>','<input>','<string>',
1402 1402 '<console>','<BackgroundJob compilation>',
1403 1403 None):
1404 1404
1405 1405 return False
1406 1406 try:
1407 1407 if (self.autoedit_syntax and
1408 1408 not self.ask_yes_no('Return to editor to correct syntax error? '
1409 1409 '[Y/n] ','y')):
1410 1410 return False
1411 1411 except EOFError:
1412 1412 return False
1413 1413
1414 1414 def int0(x):
1415 1415 try:
1416 1416 return int(x)
1417 1417 except TypeError:
1418 1418 return 0
1419 1419 # always pass integer line and offset values to editor hook
1420 1420 try:
1421 1421 self.hooks.fix_error_editor(e.filename,
1422 1422 int0(e.lineno),int0(e.offset),e.msg)
1423 1423 except TryNext:
1424 1424 warn('Could not open editor')
1425 1425 return False
1426 1426 return True
1427 1427
1428 1428 #-------------------------------------------------------------------------
1429 1429 # Things related to tab completion
1430 1430 #-------------------------------------------------------------------------
1431 1431
1432 1432 def complete(self, text):
1433 1433 """Return a sorted list of all possible completions on text.
1434 1434
1435 1435 Inputs:
1436 1436
1437 1437 - text: a string of text to be completed on.
1438 1438
1439 1439 This is a wrapper around the completion mechanism, similar to what
1440 1440 readline does at the command line when the TAB key is hit. By
1441 1441 exposing it as a method, it can be used by other non-readline
1442 1442 environments (such as GUIs) for text completion.
1443 1443
1444 1444 Simple usage example:
1445 1445
1446 1446 In [7]: x = 'hello'
1447 1447
1448 1448 In [8]: x
1449 1449 Out[8]: 'hello'
1450 1450
1451 1451 In [9]: print x
1452 1452 hello
1453 1453
1454 1454 In [10]: _ip.complete('x.l')
1455 1455 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1456 1456 """
1457 1457
1458 1458 # Inject names into __builtin__ so we can complete on the added names.
1459 1459 with self.builtin_trap:
1460 1460 complete = self.Completer.complete
1461 1461 state = 0
1462 1462 # use a dict so we get unique keys, since ipyhton's multiple
1463 1463 # completers can return duplicates. When we make 2.4 a requirement,
1464 1464 # start using sets instead, which are faster.
1465 1465 comps = {}
1466 1466 while True:
1467 1467 newcomp = complete(text,state,line_buffer=text)
1468 1468 if newcomp is None:
1469 1469 break
1470 1470 comps[newcomp] = 1
1471 1471 state += 1
1472 1472 outcomps = comps.keys()
1473 1473 outcomps.sort()
1474 1474 #print "T:",text,"OC:",outcomps # dbg
1475 1475 #print "vars:",self.user_ns.keys()
1476 1476 return outcomps
1477 1477
1478 1478 def set_custom_completer(self,completer,pos=0):
1479 1479 """Adds a new custom completer function.
1480 1480
1481 1481 The position argument (defaults to 0) is the index in the completers
1482 1482 list where you want the completer to be inserted."""
1483 1483
1484 1484 newcomp = new.instancemethod(completer,self.Completer,
1485 1485 self.Completer.__class__)
1486 1486 self.Completer.matchers.insert(pos,newcomp)
1487 1487
1488 1488 def set_completer(self):
1489 1489 """Reset readline's completer to be our own."""
1490 1490 self.readline.set_completer(self.Completer.complete)
1491 1491
1492 1492 def set_completer_frame(self, frame=None):
1493 1493 """Set the frame of the completer."""
1494 1494 if frame:
1495 1495 self.Completer.namespace = frame.f_locals
1496 1496 self.Completer.global_namespace = frame.f_globals
1497 1497 else:
1498 1498 self.Completer.namespace = self.user_ns
1499 1499 self.Completer.global_namespace = self.user_global_ns
1500 1500
1501 1501 #-------------------------------------------------------------------------
1502 1502 # Things related to readline
1503 1503 #-------------------------------------------------------------------------
1504 1504
1505 1505 def init_readline(self):
1506 1506 """Command history completion/saving/reloading."""
1507 1507
1508 1508 if self.readline_use:
1509 1509 import IPython.utils.rlineimpl as readline
1510 1510
1511 1511 self.rl_next_input = None
1512 1512 self.rl_do_indent = False
1513 1513
1514 1514 if not self.readline_use or not readline.have_readline:
1515 1515 self.has_readline = False
1516 1516 self.readline = None
1517 1517 # Set a number of methods that depend on readline to be no-op
1518 1518 self.savehist = no_op
1519 1519 self.reloadhist = no_op
1520 1520 self.set_completer = no_op
1521 1521 self.set_custom_completer = no_op
1522 1522 self.set_completer_frame = no_op
1523 1523 warn('Readline services not available or not loaded.')
1524 1524 else:
1525 1525 self.has_readline = True
1526 1526 self.readline = readline
1527 1527 sys.modules['readline'] = readline
1528 1528 import atexit
1529 1529 from IPython.core.completer import IPCompleter
1530 1530 self.Completer = IPCompleter(self,
1531 1531 self.user_ns,
1532 1532 self.user_global_ns,
1533 1533 self.readline_omit__names,
1534 1534 self.alias_manager.alias_table)
1535 1535 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1536 1536 self.strdispatchers['complete_command'] = sdisp
1537 1537 self.Completer.custom_completers = sdisp
1538 1538 # Platform-specific configuration
1539 1539 if os.name == 'nt':
1540 1540 self.readline_startup_hook = readline.set_pre_input_hook
1541 1541 else:
1542 1542 self.readline_startup_hook = readline.set_startup_hook
1543 1543
1544 1544 # Load user's initrc file (readline config)
1545 1545 # Or if libedit is used, load editrc.
1546 1546 inputrc_name = os.environ.get('INPUTRC')
1547 1547 if inputrc_name is None:
1548 1548 home_dir = get_home_dir()
1549 1549 if home_dir is not None:
1550 1550 inputrc_name = '.inputrc'
1551 1551 if readline.uses_libedit:
1552 1552 inputrc_name = '.editrc'
1553 1553 inputrc_name = os.path.join(home_dir, inputrc_name)
1554 1554 if os.path.isfile(inputrc_name):
1555 1555 try:
1556 1556 readline.read_init_file(inputrc_name)
1557 1557 except:
1558 1558 warn('Problems reading readline initialization file <%s>'
1559 1559 % inputrc_name)
1560 1560
1561 1561 # save this in sys so embedded copies can restore it properly
1562 1562 sys.ipcompleter = self.Completer.complete
1563 1563 self.set_completer()
1564 1564
1565 1565 # Configure readline according to user's prefs
1566 1566 # This is only done if GNU readline is being used. If libedit
1567 1567 # is being used (as on Leopard) the readline config is
1568 1568 # not run as the syntax for libedit is different.
1569 1569 if not readline.uses_libedit:
1570 1570 for rlcommand in self.readline_parse_and_bind:
1571 1571 #print "loading rl:",rlcommand # dbg
1572 1572 readline.parse_and_bind(rlcommand)
1573 1573
1574 1574 # Remove some chars from the delimiters list. If we encounter
1575 1575 # unicode chars, discard them.
1576 1576 delims = readline.get_completer_delims().encode("ascii", "ignore")
1577 1577 delims = delims.translate(string._idmap,
1578 1578 self.readline_remove_delims)
1579 1579 readline.set_completer_delims(delims)
1580 1580 # otherwise we end up with a monster history after a while:
1581 1581 readline.set_history_length(1000)
1582 1582 try:
1583 1583 #print '*** Reading readline history' # dbg
1584 1584 readline.read_history_file(self.histfile)
1585 1585 except IOError:
1586 1586 pass # It doesn't exist yet.
1587 1587
1588 1588 atexit.register(self.atexit_operations)
1589 1589 del atexit
1590 1590
1591 1591 # Configure auto-indent for all platforms
1592 1592 self.set_autoindent(self.autoindent)
1593 1593
1594 1594 def set_next_input(self, s):
1595 1595 """ Sets the 'default' input string for the next command line.
1596 1596
1597 1597 Requires readline.
1598 1598
1599 1599 Example:
1600 1600
1601 1601 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1602 1602 [D:\ipython]|2> Hello Word_ # cursor is here
1603 1603 """
1604 1604
1605 1605 self.rl_next_input = s
1606 1606
1607 1607 def pre_readline(self):
1608 1608 """readline hook to be used at the start of each line.
1609 1609
1610 1610 Currently it handles auto-indent only."""
1611 1611
1612 1612 #debugx('self.indent_current_nsp','pre_readline:')
1613 1613
1614 1614 if self.rl_do_indent:
1615 1615 self.readline.insert_text(self._indent_current_str())
1616 1616 if self.rl_next_input is not None:
1617 1617 self.readline.insert_text(self.rl_next_input)
1618 1618 self.rl_next_input = None
1619 1619
1620 1620 def _indent_current_str(self):
1621 1621 """return the current level of indentation as a string"""
1622 1622 return self.indent_current_nsp * ' '
1623 1623
1624 1624 #-------------------------------------------------------------------------
1625 1625 # Things related to magics
1626 1626 #-------------------------------------------------------------------------
1627 1627
1628 1628 def init_magics(self):
1629 1629 # Set user colors (don't do it in the constructor above so that it
1630 1630 # doesn't crash if colors option is invalid)
1631 1631 self.magic_colors(self.colors)
1632 1632 # History was moved to a separate module
1633 1633 from . import history
1634 1634 history.init_ipython(self)
1635 1635
1636 1636 def magic(self,arg_s):
1637 1637 """Call a magic function by name.
1638 1638
1639 1639 Input: a string containing the name of the magic function to call and any
1640 1640 additional arguments to be passed to the magic.
1641 1641
1642 1642 magic('name -opt foo bar') is equivalent to typing at the ipython
1643 1643 prompt:
1644 1644
1645 1645 In[1]: %name -opt foo bar
1646 1646
1647 1647 To call a magic without arguments, simply use magic('name').
1648 1648
1649 1649 This provides a proper Python function to call IPython's magics in any
1650 1650 valid Python code you can type at the interpreter, including loops and
1651 1651 compound statements.
1652 1652 """
1653 1653 args = arg_s.split(' ',1)
1654 1654 magic_name = args[0]
1655 1655 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1656 1656
1657 1657 try:
1658 1658 magic_args = args[1]
1659 1659 except IndexError:
1660 1660 magic_args = ''
1661 1661 fn = getattr(self,'magic_'+magic_name,None)
1662 1662 if fn is None:
1663 1663 error("Magic function `%s` not found." % magic_name)
1664 1664 else:
1665 1665 magic_args = self.var_expand(magic_args,1)
1666 1666 with nested(self.builtin_trap,):
1667 1667 result = fn(magic_args)
1668 1668 return result
1669 1669
1670 1670 def define_magic(self, magicname, func):
1671 1671 """Expose own function as magic function for ipython
1672 1672
1673 1673 def foo_impl(self,parameter_s=''):
1674 1674 'My very own magic!. (Use docstrings, IPython reads them).'
1675 1675 print 'Magic function. Passed parameter is between < >:'
1676 1676 print '<%s>' % parameter_s
1677 1677 print 'The self object is:',self
1678 1678
1679 1679 self.define_magic('foo',foo_impl)
1680 1680 """
1681 1681
1682 1682 import new
1683 1683 im = new.instancemethod(func,self, self.__class__)
1684 1684 old = getattr(self, "magic_" + magicname, None)
1685 1685 setattr(self, "magic_" + magicname, im)
1686 1686 return old
1687 1687
1688 1688 #-------------------------------------------------------------------------
1689 1689 # Things related to macros
1690 1690 #-------------------------------------------------------------------------
1691 1691
1692 1692 def define_macro(self, name, themacro):
1693 1693 """Define a new macro
1694 1694
1695 1695 Parameters
1696 1696 ----------
1697 1697 name : str
1698 1698 The name of the macro.
1699 1699 themacro : str or Macro
1700 1700 The action to do upon invoking the macro. If a string, a new
1701 1701 Macro object is created by passing the string to it.
1702 1702 """
1703 1703
1704 1704 from IPython.core import macro
1705 1705
1706 1706 if isinstance(themacro, basestring):
1707 1707 themacro = macro.Macro(themacro)
1708 1708 if not isinstance(themacro, macro.Macro):
1709 1709 raise ValueError('A macro must be a string or a Macro instance.')
1710 1710 self.user_ns[name] = themacro
1711 1711
1712 1712 #-------------------------------------------------------------------------
1713 1713 # Things related to the running of system commands
1714 1714 #-------------------------------------------------------------------------
1715 1715
1716 1716 def system(self, cmd):
1717 1717 """Make a system call, using IPython."""
1718 1718 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1719 1719
1720 1720 #-------------------------------------------------------------------------
1721 1721 # Things related to aliases
1722 1722 #-------------------------------------------------------------------------
1723 1723
1724 1724 def init_alias(self):
1725 1725 self.alias_manager = AliasManager(self, config=self.config)
1726 1726 self.ns_table['alias'] = self.alias_manager.alias_table,
1727 1727
1728 1728 #-------------------------------------------------------------------------
1729 1729 # Things related to the running of code
1730 1730 #-------------------------------------------------------------------------
1731 1731
1732 1732 def ex(self, cmd):
1733 1733 """Execute a normal python statement in user namespace."""
1734 1734 with nested(self.builtin_trap,):
1735 1735 exec cmd in self.user_global_ns, self.user_ns
1736 1736
1737 1737 def ev(self, expr):
1738 1738 """Evaluate python expression expr in user namespace.
1739 1739
1740 1740 Returns the result of evaluation
1741 1741 """
1742 1742 with nested(self.builtin_trap,):
1743 1743 return eval(expr, self.user_global_ns, self.user_ns)
1744 1744
1745 1745 def mainloop(self, display_banner=None):
1746 1746 """Start the mainloop.
1747 1747
1748 1748 If an optional banner argument is given, it will override the
1749 1749 internally created default banner.
1750 1750 """
1751 1751
1752 1752 with nested(self.builtin_trap, self.display_trap):
1753 1753
1754 1754 # if you run stuff with -c <cmd>, raw hist is not updated
1755 1755 # ensure that it's in sync
1756 1756 if len(self.input_hist) != len (self.input_hist_raw):
1757 1757 self.input_hist_raw = InputList(self.input_hist)
1758 1758
1759 1759 while 1:
1760 1760 try:
1761 1761 self.interact(display_banner=display_banner)
1762 1762 #self.interact_with_readline()
1763 1763 # XXX for testing of a readline-decoupled repl loop, call
1764 1764 # interact_with_readline above
1765 1765 break
1766 1766 except KeyboardInterrupt:
1767 1767 # this should not be necessary, but KeyboardInterrupt
1768 1768 # handling seems rather unpredictable...
1769 1769 self.write("\nKeyboardInterrupt in interact()\n")
1770 1770
1771 1771 def interact_prompt(self):
1772 1772 """ Print the prompt (in read-eval-print loop)
1773 1773
1774 1774 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1775 1775 used in standard IPython flow.
1776 1776 """
1777 1777 if self.more:
1778 1778 try:
1779 1779 prompt = self.hooks.generate_prompt(True)
1780 1780 except:
1781 1781 self.showtraceback()
1782 1782 if self.autoindent:
1783 1783 self.rl_do_indent = True
1784 1784
1785 1785 else:
1786 1786 try:
1787 1787 prompt = self.hooks.generate_prompt(False)
1788 1788 except:
1789 1789 self.showtraceback()
1790 1790 self.write(prompt)
1791 1791
1792 1792 def interact_handle_input(self,line):
1793 1793 """ Handle the input line (in read-eval-print loop)
1794 1794
1795 1795 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1796 1796 used in standard IPython flow.
1797 1797 """
1798 1798 if line.lstrip() == line:
1799 1799 self.shadowhist.add(line.strip())
1800 1800 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1801 1801
1802 1802 if line.strip():
1803 1803 if self.more:
1804 1804 self.input_hist_raw[-1] += '%s\n' % line
1805 1805 else:
1806 1806 self.input_hist_raw.append('%s\n' % line)
1807 1807
1808 1808
1809 1809 self.more = self.push_line(lineout)
1810 1810 if (self.SyntaxTB.last_syntax_error and
1811 1811 self.autoedit_syntax):
1812 1812 self.edit_syntax_error()
1813 1813
1814 1814 def interact_with_readline(self):
1815 1815 """ Demo of using interact_handle_input, interact_prompt
1816 1816
1817 1817 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1818 1818 it should work like this.
1819 1819 """
1820 1820 self.readline_startup_hook(self.pre_readline)
1821 1821 while not self.exit_now:
1822 1822 self.interact_prompt()
1823 1823 if self.more:
1824 1824 self.rl_do_indent = True
1825 1825 else:
1826 1826 self.rl_do_indent = False
1827 1827 line = raw_input_original().decode(self.stdin_encoding)
1828 1828 self.interact_handle_input(line)
1829 1829
1830 1830 def interact(self, display_banner=None):
1831 1831 """Closely emulate the interactive Python console."""
1832 1832
1833 1833 # batch run -> do not interact
1834 1834 if self.exit_now:
1835 1835 return
1836 1836
1837 1837 if display_banner is None:
1838 1838 display_banner = self.display_banner
1839 1839 if display_banner:
1840 1840 self.show_banner()
1841 1841
1842 1842 more = 0
1843 1843
1844 1844 # Mark activity in the builtins
1845 1845 __builtin__.__dict__['__IPYTHON__active'] += 1
1846 1846
1847 1847 if self.has_readline:
1848 1848 self.readline_startup_hook(self.pre_readline)
1849 1849 # exit_now is set by a call to %Exit or %Quit, through the
1850 1850 # ask_exit callback.
1851 1851
1852 1852 while not self.exit_now:
1853 1853 self.hooks.pre_prompt_hook()
1854 1854 if more:
1855 1855 try:
1856 1856 prompt = self.hooks.generate_prompt(True)
1857 1857 except:
1858 1858 self.showtraceback()
1859 1859 if self.autoindent:
1860 1860 self.rl_do_indent = True
1861 1861
1862 1862 else:
1863 1863 try:
1864 1864 prompt = self.hooks.generate_prompt(False)
1865 1865 except:
1866 1866 self.showtraceback()
1867 1867 try:
1868 1868 line = self.raw_input(prompt, more)
1869 1869 if self.exit_now:
1870 1870 # quick exit on sys.std[in|out] close
1871 1871 break
1872 1872 if self.autoindent:
1873 1873 self.rl_do_indent = False
1874 1874
1875 1875 except KeyboardInterrupt:
1876 1876 #double-guard against keyboardinterrupts during kbdint handling
1877 1877 try:
1878 1878 self.write('\nKeyboardInterrupt\n')
1879 1879 self.resetbuffer()
1880 1880 # keep cache in sync with the prompt counter:
1881 1881 self.outputcache.prompt_count -= 1
1882 1882
1883 1883 if self.autoindent:
1884 1884 self.indent_current_nsp = 0
1885 1885 more = 0
1886 1886 except KeyboardInterrupt:
1887 1887 pass
1888 1888 except EOFError:
1889 1889 if self.autoindent:
1890 1890 self.rl_do_indent = False
1891 1891 if self.has_readline:
1892 1892 self.readline_startup_hook(None)
1893 1893 self.write('\n')
1894 1894 self.exit()
1895 1895 except bdb.BdbQuit:
1896 1896 warn('The Python debugger has exited with a BdbQuit exception.\n'
1897 1897 'Because of how pdb handles the stack, it is impossible\n'
1898 1898 'for IPython to properly format this particular exception.\n'
1899 1899 'IPython will resume normal operation.')
1900 1900 except:
1901 1901 # exceptions here are VERY RARE, but they can be triggered
1902 1902 # asynchronously by signal handlers, for example.
1903 1903 self.showtraceback()
1904 1904 else:
1905 1905 more = self.push_line(line)
1906 1906 if (self.SyntaxTB.last_syntax_error and
1907 1907 self.autoedit_syntax):
1908 1908 self.edit_syntax_error()
1909 1909
1910 1910 # We are off again...
1911 1911 __builtin__.__dict__['__IPYTHON__active'] -= 1
1912 1912
1913 1913 # Turn off the exit flag, so the mainloop can be restarted if desired
1914 1914 self.exit_now = False
1915 1915
1916 1916 def safe_execfile(self, fname, *where, **kw):
1917 1917 """A safe version of the builtin execfile().
1918 1918
1919 1919 This version will never throw an exception, but instead print
1920 1920 helpful error messages to the screen. This only works on pure
1921 1921 Python files with the .py extension.
1922 1922
1923 1923 Parameters
1924 1924 ----------
1925 1925 fname : string
1926 1926 The name of the file to be executed.
1927 1927 where : tuple
1928 1928 One or two namespaces, passed to execfile() as (globals,locals).
1929 1929 If only one is given, it is passed as both.
1930 1930 exit_ignore : bool (False)
1931 1931 If True, then silence SystemExit for non-zero status (it is always
1932 1932 silenced for zero status, as it is so common).
1933 1933 """
1934 1934 kw.setdefault('exit_ignore', False)
1935 1935
1936 1936 fname = os.path.abspath(os.path.expanduser(fname))
1937 1937
1938 1938 # Make sure we have a .py file
1939 1939 if not fname.endswith('.py'):
1940 1940 warn('File must end with .py to be run using execfile: <%s>' % fname)
1941 1941
1942 1942 # Make sure we can open the file
1943 1943 try:
1944 1944 with open(fname) as thefile:
1945 1945 pass
1946 1946 except:
1947 1947 warn('Could not open file <%s> for safe execution.' % fname)
1948 1948 return
1949 1949
1950 1950 # Find things also in current directory. This is needed to mimic the
1951 1951 # behavior of running a script from the system command line, where
1952 1952 # Python inserts the script's directory into sys.path
1953 1953 dname = os.path.dirname(fname)
1954 1954
1955 1955 with prepended_to_syspath(dname):
1956 1956 try:
1957 1957 execfile(fname,*where)
1958 1958 except SystemExit, status:
1959 1959 # If the call was made with 0 or None exit status (sys.exit(0)
1960 1960 # or sys.exit() ), don't bother showing a traceback, as both of
1961 1961 # these are considered normal by the OS:
1962 1962 # > python -c'import sys;sys.exit(0)'; echo $?
1963 1963 # 0
1964 1964 # > python -c'import sys;sys.exit()'; echo $?
1965 1965 # 0
1966 1966 # For other exit status, we show the exception unless
1967 1967 # explicitly silenced, but only in short form.
1968 1968 if status.code not in (0, None) and not kw['exit_ignore']:
1969 1969 self.showtraceback(exception_only=True)
1970 1970 except:
1971 1971 self.showtraceback()
1972 1972
1973 1973 def safe_execfile_ipy(self, fname):
1974 1974 """Like safe_execfile, but for .ipy files with IPython syntax.
1975 1975
1976 1976 Parameters
1977 1977 ----------
1978 1978 fname : str
1979 1979 The name of the file to execute. The filename must have a
1980 1980 .ipy extension.
1981 1981 """
1982 1982 fname = os.path.abspath(os.path.expanduser(fname))
1983 1983
1984 1984 # Make sure we have a .py file
1985 1985 if not fname.endswith('.ipy'):
1986 1986 warn('File must end with .py to be run using execfile: <%s>' % fname)
1987 1987
1988 1988 # Make sure we can open the file
1989 1989 try:
1990 1990 with open(fname) as thefile:
1991 1991 pass
1992 1992 except:
1993 1993 warn('Could not open file <%s> for safe execution.' % fname)
1994 1994 return
1995 1995
1996 1996 # Find things also in current directory. This is needed to mimic the
1997 1997 # behavior of running a script from the system command line, where
1998 1998 # Python inserts the script's directory into sys.path
1999 1999 dname = os.path.dirname(fname)
2000 2000
2001 2001 with prepended_to_syspath(dname):
2002 2002 try:
2003 2003 with open(fname) as thefile:
2004 2004 script = thefile.read()
2005 2005 # self.runlines currently captures all exceptions
2006 2006 # raise in user code. It would be nice if there were
2007 2007 # versions of runlines, execfile that did raise, so
2008 2008 # we could catch the errors.
2009 2009 self.runlines(script, clean=True)
2010 2010 except:
2011 2011 self.showtraceback()
2012 2012 warn('Unknown failure executing file: <%s>' % fname)
2013 2013
2014 2014 def _is_secondary_block_start(self, s):
2015 2015 if not s.endswith(':'):
2016 2016 return False
2017 2017 if (s.startswith('elif') or
2018 2018 s.startswith('else') or
2019 2019 s.startswith('except') or
2020 2020 s.startswith('finally')):
2021 2021 return True
2022 2022
2023 2023 def cleanup_ipy_script(self, script):
2024 2024 """Make a script safe for self.runlines()
2025 2025
2026 2026 Currently, IPython is lines based, with blocks being detected by
2027 2027 empty lines. This is a problem for block based scripts that may
2028 2028 not have empty lines after blocks. This script adds those empty
2029 2029 lines to make scripts safe for running in the current line based
2030 2030 IPython.
2031 2031 """
2032 2032 res = []
2033 2033 lines = script.splitlines()
2034 2034 level = 0
2035 2035
2036 2036 for l in lines:
2037 2037 lstripped = l.lstrip()
2038 2038 stripped = l.strip()
2039 2039 if not stripped:
2040 2040 continue
2041 2041 newlevel = len(l) - len(lstripped)
2042 2042 if level > 0 and newlevel == 0 and \
2043 2043 not self._is_secondary_block_start(stripped):
2044 2044 # add empty line
2045 2045 res.append('')
2046 2046 res.append(l)
2047 2047 level = newlevel
2048 2048
2049 2049 return '\n'.join(res) + '\n'
2050 2050
2051 2051 def runlines(self, lines, clean=False):
2052 2052 """Run a string of one or more lines of source.
2053 2053
2054 2054 This method is capable of running a string containing multiple source
2055 2055 lines, as if they had been entered at the IPython prompt. Since it
2056 2056 exposes IPython's processing machinery, the given strings can contain
2057 2057 magic calls (%magic), special shell access (!cmd), etc.
2058 2058 """
2059 2059
2060 2060 if isinstance(lines, (list, tuple)):
2061 2061 lines = '\n'.join(lines)
2062 2062
2063 2063 if clean:
2064 2064 lines = self.cleanup_ipy_script(lines)
2065 2065
2066 2066 # We must start with a clean buffer, in case this is run from an
2067 2067 # interactive IPython session (via a magic, for example).
2068 2068 self.resetbuffer()
2069 2069 lines = lines.splitlines()
2070 2070 more = 0
2071 2071
2072 2072 with nested(self.builtin_trap, self.display_trap):
2073 2073 for line in lines:
2074 2074 # skip blank lines so we don't mess up the prompt counter, but do
2075 2075 # NOT skip even a blank line if we are in a code block (more is
2076 2076 # true)
2077 2077
2078 2078 if line or more:
2079 2079 # push to raw history, so hist line numbers stay in sync
2080 2080 self.input_hist_raw.append("# " + line + "\n")
2081 2081 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2082 2082 more = self.push_line(prefiltered)
2083 2083 # IPython's runsource returns None if there was an error
2084 2084 # compiling the code. This allows us to stop processing right
2085 2085 # away, so the user gets the error message at the right place.
2086 2086 if more is None:
2087 2087 break
2088 2088 else:
2089 2089 self.input_hist_raw.append("\n")
2090 2090 # final newline in case the input didn't have it, so that the code
2091 2091 # actually does get executed
2092 2092 if more:
2093 2093 self.push_line('\n')
2094 2094
2095 2095 def runsource(self, source, filename='<input>', symbol='single'):
2096 2096 """Compile and run some source in the interpreter.
2097 2097
2098 2098 Arguments are as for compile_command().
2099 2099
2100 2100 One several things can happen:
2101 2101
2102 2102 1) The input is incorrect; compile_command() raised an
2103 2103 exception (SyntaxError or OverflowError). A syntax traceback
2104 2104 will be printed by calling the showsyntaxerror() method.
2105 2105
2106 2106 2) The input is incomplete, and more input is required;
2107 2107 compile_command() returned None. Nothing happens.
2108 2108
2109 2109 3) The input is complete; compile_command() returned a code
2110 2110 object. The code is executed by calling self.runcode() (which
2111 2111 also handles run-time exceptions, except for SystemExit).
2112 2112
2113 2113 The return value is:
2114 2114
2115 2115 - True in case 2
2116 2116
2117 2117 - False in the other cases, unless an exception is raised, where
2118 2118 None is returned instead. This can be used by external callers to
2119 2119 know whether to continue feeding input or not.
2120 2120
2121 2121 The return value can be used to decide whether to use sys.ps1 or
2122 2122 sys.ps2 to prompt the next line."""
2123 2123
2124 2124 # if the source code has leading blanks, add 'if 1:\n' to it
2125 2125 # this allows execution of indented pasted code. It is tempting
2126 2126 # to add '\n' at the end of source to run commands like ' a=1'
2127 2127 # directly, but this fails for more complicated scenarios
2128 2128 source=source.encode(self.stdin_encoding)
2129 2129 if source[:1] in [' ', '\t']:
2130 2130 source = 'if 1:\n%s' % source
2131 2131
2132 2132 try:
2133 2133 code = self.compile(source,filename,symbol)
2134 2134 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2135 2135 # Case 1
2136 2136 self.showsyntaxerror(filename)
2137 2137 return None
2138 2138
2139 2139 if code is None:
2140 2140 # Case 2
2141 2141 return True
2142 2142
2143 2143 # Case 3
2144 2144 # We store the code object so that threaded shells and
2145 2145 # custom exception handlers can access all this info if needed.
2146 2146 # The source corresponding to this can be obtained from the
2147 2147 # buffer attribute as '\n'.join(self.buffer).
2148 2148 self.code_to_run = code
2149 2149 # now actually execute the code object
2150 2150 if self.runcode(code) == 0:
2151 2151 return False
2152 2152 else:
2153 2153 return None
2154 2154
2155 2155 def runcode(self,code_obj):
2156 2156 """Execute a code object.
2157 2157
2158 2158 When an exception occurs, self.showtraceback() is called to display a
2159 2159 traceback.
2160 2160
2161 2161 Return value: a flag indicating whether the code to be run completed
2162 2162 successfully:
2163 2163
2164 2164 - 0: successful execution.
2165 2165 - 1: an error occurred.
2166 2166 """
2167 2167
2168 2168 # Set our own excepthook in case the user code tries to call it
2169 2169 # directly, so that the IPython crash handler doesn't get triggered
2170 2170 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2171 2171
2172 2172 # we save the original sys.excepthook in the instance, in case config
2173 2173 # code (such as magics) needs access to it.
2174 2174 self.sys_excepthook = old_excepthook
2175 2175 outflag = 1 # happens in more places, so it's easier as default
2176 2176 try:
2177 2177 try:
2178 2178 self.hooks.pre_runcode_hook()
2179 2179 exec code_obj in self.user_global_ns, self.user_ns
2180 2180 finally:
2181 2181 # Reset our crash handler in place
2182 2182 sys.excepthook = old_excepthook
2183 2183 except SystemExit:
2184 2184 self.resetbuffer()
2185 2185 self.showtraceback(exception_only=True)
2186 2186 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2187 2187 except self.custom_exceptions:
2188 2188 etype,value,tb = sys.exc_info()
2189 2189 self.CustomTB(etype,value,tb)
2190 2190 except:
2191 2191 self.showtraceback()
2192 2192 else:
2193 2193 outflag = 0
2194 2194 if softspace(sys.stdout, 0):
2195 2195 print
2196 2196 # Flush out code object which has been run (and source)
2197 2197 self.code_to_run = None
2198 2198 return outflag
2199 2199
2200 2200 def push_line(self, line):
2201 2201 """Push a line to the interpreter.
2202 2202
2203 2203 The line should not have a trailing newline; it may have
2204 2204 internal newlines. The line is appended to a buffer and the
2205 2205 interpreter's runsource() method is called with the
2206 2206 concatenated contents of the buffer as source. If this
2207 2207 indicates that the command was executed or invalid, the buffer
2208 2208 is reset; otherwise, the command is incomplete, and the buffer
2209 2209 is left as it was after the line was appended. The return
2210 2210 value is 1 if more input is required, 0 if the line was dealt
2211 2211 with in some way (this is the same as runsource()).
2212 2212 """
2213 2213
2214 2214 # autoindent management should be done here, and not in the
2215 2215 # interactive loop, since that one is only seen by keyboard input. We
2216 2216 # need this done correctly even for code run via runlines (which uses
2217 2217 # push).
2218 2218
2219 2219 #print 'push line: <%s>' % line # dbg
2220 2220 for subline in line.splitlines():
2221 2221 self._autoindent_update(subline)
2222 2222 self.buffer.append(line)
2223 2223 more = self.runsource('\n'.join(self.buffer), self.filename)
2224 2224 if not more:
2225 2225 self.resetbuffer()
2226 2226 return more
2227 2227
2228 2228 def _autoindent_update(self,line):
2229 2229 """Keep track of the indent level."""
2230 2230
2231 2231 #debugx('line')
2232 2232 #debugx('self.indent_current_nsp')
2233 2233 if self.autoindent:
2234 2234 if line:
2235 2235 inisp = num_ini_spaces(line)
2236 2236 if inisp < self.indent_current_nsp:
2237 2237 self.indent_current_nsp = inisp
2238 2238
2239 2239 if line[-1] == ':':
2240 2240 self.indent_current_nsp += 4
2241 2241 elif dedent_re.match(line):
2242 2242 self.indent_current_nsp -= 4
2243 2243 else:
2244 2244 self.indent_current_nsp = 0
2245 2245
2246 2246 def resetbuffer(self):
2247 2247 """Reset the input buffer."""
2248 2248 self.buffer[:] = []
2249 2249
2250 2250 def raw_input(self,prompt='',continue_prompt=False):
2251 2251 """Write a prompt and read a line.
2252 2252
2253 2253 The returned line does not include the trailing newline.
2254 2254 When the user enters the EOF key sequence, EOFError is raised.
2255 2255
2256 2256 Optional inputs:
2257 2257
2258 2258 - prompt(''): a string to be printed to prompt the user.
2259 2259
2260 2260 - continue_prompt(False): whether this line is the first one or a
2261 2261 continuation in a sequence of inputs.
2262 2262 """
2263 2263 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2264 2264
2265 2265 # Code run by the user may have modified the readline completer state.
2266 2266 # We must ensure that our completer is back in place.
2267 2267
2268 2268 if self.has_readline:
2269 2269 self.set_completer()
2270 2270
2271 2271 try:
2272 2272 line = raw_input_original(prompt).decode(self.stdin_encoding)
2273 2273 except ValueError:
2274 2274 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2275 2275 " or sys.stdout.close()!\nExiting IPython!")
2276 2276 self.ask_exit()
2277 2277 return ""
2278 2278
2279 2279 # Try to be reasonably smart about not re-indenting pasted input more
2280 2280 # than necessary. We do this by trimming out the auto-indent initial
2281 2281 # spaces, if the user's actual input started itself with whitespace.
2282 2282 #debugx('self.buffer[-1]')
2283 2283
2284 2284 if self.autoindent:
2285 2285 if num_ini_spaces(line) > self.indent_current_nsp:
2286 2286 line = line[self.indent_current_nsp:]
2287 2287 self.indent_current_nsp = 0
2288 2288
2289 2289 # store the unfiltered input before the user has any chance to modify
2290 2290 # it.
2291 2291 if line.strip():
2292 2292 if continue_prompt:
2293 2293 self.input_hist_raw[-1] += '%s\n' % line
2294 2294 if self.has_readline and self.readline_use:
2295 2295 try:
2296 2296 histlen = self.readline.get_current_history_length()
2297 2297 if histlen > 1:
2298 2298 newhist = self.input_hist_raw[-1].rstrip()
2299 2299 self.readline.remove_history_item(histlen-1)
2300 2300 self.readline.replace_history_item(histlen-2,
2301 2301 newhist.encode(self.stdin_encoding))
2302 2302 except AttributeError:
2303 2303 pass # re{move,place}_history_item are new in 2.4.
2304 2304 else:
2305 2305 self.input_hist_raw.append('%s\n' % line)
2306 2306 # only entries starting at first column go to shadow history
2307 2307 if line.lstrip() == line:
2308 2308 self.shadowhist.add(line.strip())
2309 2309 elif not continue_prompt:
2310 2310 self.input_hist_raw.append('\n')
2311 2311 try:
2312 2312 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2313 2313 except:
2314 2314 # blanket except, in case a user-defined prefilter crashes, so it
2315 2315 # can't take all of ipython with it.
2316 2316 self.showtraceback()
2317 2317 return ''
2318 2318 else:
2319 2319 return lineout
2320 2320
2321 2321 #-------------------------------------------------------------------------
2322 2322 # Working with components
2323 2323 #-------------------------------------------------------------------------
2324 2324
2325 2325 def get_component(self, name=None, klass=None):
2326 2326 """Fetch a component by name and klass in my tree."""
2327 2327 c = Component.get_instances(root=self, name=name, klass=klass)
2328 2328 if len(c) == 0:
2329 2329 return None
2330 2330 if len(c) == 1:
2331 2331 return c[0]
2332 2332 else:
2333 2333 return c
2334 2334
2335 2335 #-------------------------------------------------------------------------
2336 2336 # IPython extensions
2337 2337 #-------------------------------------------------------------------------
2338 2338
2339 2339 def load_extension(self, module_str):
2340 2340 """Load an IPython extension by its module name.
2341 2341
2342 2342 An IPython extension is an importable Python module that has
2343 2343 a function with the signature::
2344 2344
2345 2345 def load_ipython_extension(ipython):
2346 2346 # Do things with ipython
2347 2347
2348 2348 This function is called after your extension is imported and the
2349 2349 currently active :class:`InteractiveShell` instance is passed as
2350 2350 the only argument. You can do anything you want with IPython at
2351 2351 that point, including defining new magic and aliases, adding new
2352 2352 components, etc.
2353 2353
2354 2354 The :func:`load_ipython_extension` will be called again is you
2355 2355 load or reload the extension again. It is up to the extension
2356 2356 author to add code to manage that.
2357 2357
2358 2358 You can put your extension modules anywhere you want, as long as
2359 2359 they can be imported by Python's standard import mechanism. However,
2360 2360 to make it easy to write extensions, you can also put your extensions
2361 2361 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2362 2362 is added to ``sys.path`` automatically.
2363
2364 If :func:`load_ipython_extension` returns anything, this function
2365 will return that object.
2363 2366 """
2364 2367 from IPython.utils.syspathcontext import prepended_to_syspath
2365 2368
2366 2369 if module_str not in sys.modules:
2367 2370 with prepended_to_syspath(self.ipython_extension_dir):
2368 2371 __import__(module_str)
2369 2372 mod = sys.modules[module_str]
2370 2373 return self._call_load_ipython_extension(mod)
2371 2374
2372 2375 def unload_extension(self, module_str):
2373 2376 """Unload an IPython extension by its module name.
2374 2377
2375 2378 This function looks up the extension's name in ``sys.modules`` and
2376 2379 simply calls ``mod.unload_ipython_extension(self)``.
2377 2380 """
2378 2381 if module_str in sys.modules:
2379 2382 mod = sys.modules[module_str]
2380 2383 self._call_unload_ipython_extension(mod)
2381 2384
2382 2385 def reload_extension(self, module_str):
2383 2386 """Reload an IPython extension by calling reload.
2384 2387
2385 2388 If the module has not been loaded before,
2386 2389 :meth:`InteractiveShell.load_extension` is called. Otherwise
2387 2390 :func:`reload` is called and then the :func:`load_ipython_extension`
2388 2391 function of the module, if it exists is called.
2389 2392 """
2390 2393 from IPython.utils.syspathcontext import prepended_to_syspath
2391 2394
2392 2395 with prepended_to_syspath(self.ipython_extension_dir):
2393 2396 if module_str in sys.modules:
2394 2397 mod = sys.modules[module_str]
2395 2398 reload(mod)
2396 2399 self._call_load_ipython_extension(mod)
2397 2400 else:
2398 2401 self.load_extension(module_str)
2399 2402
2400 2403 def _call_load_ipython_extension(self, mod):
2401 2404 if hasattr(mod, 'load_ipython_extension'):
2402 2405 return mod.load_ipython_extension(self)
2403 2406
2404 2407 def _call_unload_ipython_extension(self, mod):
2405 2408 if hasattr(mod, 'unload_ipython_extension'):
2406 2409 return mod.unload_ipython_extension(self)
2407 2410
2408 2411 #-------------------------------------------------------------------------
2409 2412 # Things related to the prefilter
2410 2413 #-------------------------------------------------------------------------
2411 2414
2412 2415 def init_prefilter(self):
2413 2416 self.prefilter_manager = PrefilterManager(self, config=self.config)
2414 2417 # Ultimately this will be refactored in the new interpreter code, but
2415 2418 # for now, we should expose the main prefilter method (there's legacy
2416 2419 # code out there that may rely on this).
2417 2420 self.prefilter = self.prefilter_manager.prefilter_lines
2418 2421
2419 2422 #-------------------------------------------------------------------------
2420 2423 # Utilities
2421 2424 #-------------------------------------------------------------------------
2422 2425
2423 2426 def getoutput(self, cmd):
2424 2427 return getoutput(self.var_expand(cmd,depth=2),
2425 2428 header=self.system_header,
2426 2429 verbose=self.system_verbose)
2427 2430
2428 2431 def getoutputerror(self, cmd):
2429 2432 return getoutputerror(self.var_expand(cmd,depth=2),
2430 2433 header=self.system_header,
2431 2434 verbose=self.system_verbose)
2432 2435
2433 2436 def var_expand(self,cmd,depth=0):
2434 2437 """Expand python variables in a string.
2435 2438
2436 2439 The depth argument indicates how many frames above the caller should
2437 2440 be walked to look for the local namespace where to expand variables.
2438 2441
2439 2442 The global namespace for expansion is always the user's interactive
2440 2443 namespace.
2441 2444 """
2442 2445
2443 2446 return str(ItplNS(cmd,
2444 2447 self.user_ns, # globals
2445 2448 # Skip our own frame in searching for locals:
2446 2449 sys._getframe(depth+1).f_locals # locals
2447 2450 ))
2448 2451
2449 2452 def mktempfile(self,data=None):
2450 2453 """Make a new tempfile and return its filename.
2451 2454
2452 2455 This makes a call to tempfile.mktemp, but it registers the created
2453 2456 filename internally so ipython cleans it up at exit time.
2454 2457
2455 2458 Optional inputs:
2456 2459
2457 2460 - data(None): if data is given, it gets written out to the temp file
2458 2461 immediately, and the file is closed again."""
2459 2462
2460 2463 filename = tempfile.mktemp('.py','ipython_edit_')
2461 2464 self.tempfiles.append(filename)
2462 2465
2463 2466 if data:
2464 2467 tmp_file = open(filename,'w')
2465 2468 tmp_file.write(data)
2466 2469 tmp_file.close()
2467 2470 return filename
2468 2471
2469 2472 def write(self,data):
2470 2473 """Write a string to the default output"""
2471 2474 Term.cout.write(data)
2472 2475
2473 2476 def write_err(self,data):
2474 2477 """Write a string to the default error output"""
2475 2478 Term.cerr.write(data)
2476 2479
2477 2480 def ask_yes_no(self,prompt,default=True):
2478 2481 if self.quiet:
2479 2482 return True
2480 2483 return ask_yes_no(prompt,default)
2481 2484
2482 2485 #-------------------------------------------------------------------------
2483 2486 # Things related to GUI support and pylab
2484 2487 #-------------------------------------------------------------------------
2485 2488
2486 2489 def enable_pylab(self, gui=None):
2487 2490 """Activate pylab support at runtime.
2488 2491
2489 2492 This turns on support for matplotlib, preloads into the interactive
2490 2493 namespace all of numpy and pylab, and configures IPython to correcdtly
2491 2494 interact with the GUI event loop. The GUI backend to be used can be
2492 2495 optionally selected with the optional :param:`gui` argument.
2493 2496
2494 2497 Parameters
2495 2498 ----------
2496 2499 gui : optional, string
2497 2500
2498 2501 If given, dictates the choice of matplotlib GUI backend to use
2499 2502 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2500 2503 'gtk'), otherwise we use the default chosen by matplotlib (as
2501 2504 dictated by the matplotlib build-time options plus the user's
2502 2505 matplotlibrc configuration file).
2503 2506 """
2504 2507 # We want to prevent the loading of pylab to pollute the user's
2505 2508 # namespace as shown by the %who* magics, so we execute the activation
2506 2509 # code in an empty namespace, and we update *both* user_ns and
2507 # user_config_ns with this information.
2510 # user_ns_hidden with this information.
2508 2511 ns = {}
2509 2512 gui = pylab_activate(ns, gui)
2510 2513 self.user_ns.update(ns)
2511 self.user_config_ns.update(ns)
2514 self.user_ns_hidden.update(ns)
2512 2515 # Now we must activate the gui pylab wants to use, and fix %run to take
2513 2516 # plot updates into account
2514 2517 enable_gui(gui)
2515 2518 self.magic_run = self._pylab_magic_run
2516 2519
2517 2520 #-------------------------------------------------------------------------
2518 2521 # Things related to IPython exiting
2519 2522 #-------------------------------------------------------------------------
2520 2523
2521 2524 def ask_exit(self):
2522 2525 """ Ask the shell to exit. Can be overiden and used as a callback. """
2523 2526 self.exit_now = True
2524 2527
2525 2528 def exit(self):
2526 2529 """Handle interactive exit.
2527 2530
2528 2531 This method calls the ask_exit callback."""
2529 2532 if self.confirm_exit:
2530 2533 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2531 2534 self.ask_exit()
2532 2535 else:
2533 2536 self.ask_exit()
2534 2537
2535 2538 def atexit_operations(self):
2536 2539 """This will be executed at the time of exit.
2537 2540
2538 2541 Saving of persistent data should be performed here.
2539 2542 """
2540 2543 self.savehist()
2541 2544
2542 2545 # Cleanup all tempfiles left around
2543 2546 for tfile in self.tempfiles:
2544 2547 try:
2545 2548 os.unlink(tfile)
2546 2549 except OSError:
2547 2550 pass
2548 2551
2549 2552 # Clear all user namespaces to release all references cleanly.
2550 2553 self.reset()
2551 2554
2552 2555 # Run user hooks
2553 2556 self.hooks.shutdown_hook()
2554 2557
2555 2558 def cleanup(self):
2556 2559 self.restore_sys_module_state()
2557 2560
2558 2561
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,245 +1,245 b''
1 1 """Tests for the key iplib module, where the main ipython class is defined.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # stdlib
8 8 import os
9 9 import shutil
10 10 import tempfile
11 11
12 12 # third party
13 13 import nose.tools as nt
14 14
15 15 # our own packages
16 16 from IPython.testing import decorators as dec
17 17 from IPython.testing.globalipapp import get_ipython
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Globals
21 21 #-----------------------------------------------------------------------------
22 22
23 23 # Get the public instance of IPython
24 24 ip = get_ipython()
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Test functions
28 28 #-----------------------------------------------------------------------------
29 29
30 30 @dec.parametric
31 31 def test_reset():
32 32 """reset must clear most namespaces."""
33 # The number of variables in the private user_config_ns is not zero, but it
33 # The number of variables in the private user_ns_hidden is not zero, but it
34 34 # should be constant regardless of what we do
35 nvars_config_ns = len(ip.user_config_ns)
35 nvars_config_ns = len(ip.user_ns_hidden)
36 36
37 37 # Check that reset runs without error
38 38 ip.reset()
39 39
40 40 # Once we've reset it (to clear of any junk that might have been there from
41 41 # other tests, we can count how many variables are in the user's namespace
42 42 nvars_user_ns = len(ip.user_ns)
43 43
44 44 # Now add a few variables to user_ns, and check that reset clears them
45 45 ip.user_ns['x'] = 1
46 46 ip.user_ns['y'] = 1
47 47 ip.reset()
48 48
49 49 # Finally, check that all namespaces have only as many variables as we
50 50 # expect to find in them:
51 51 for ns in ip.ns_refs_table:
52 52 if ns is ip.user_ns:
53 53 nvars_expected = nvars_user_ns
54 elif ns is ip.user_config_ns:
54 elif ns is ip.user_ns_hidden:
55 55 nvars_expected = nvars_config_ns
56 56 else:
57 57 nvars_expected = 0
58 58
59 59 yield nt.assert_equals(len(ns), nvars_expected)
60 60
61 61
62 62 # Tests for reporting of exceptions in various modes, handling of SystemExit,
63 63 # and %tb functionality. This is really a mix of testing ultraTB and iplib.
64 64
65 65 def doctest_tb_plain():
66 66 """
67 67 In [18]: xmode plain
68 68 Exception reporting mode: Plain
69 69
70 70 In [19]: run simpleerr.py
71 71 Traceback (most recent call last):
72 72 ...line 32, in <module>
73 73 bar(mode)
74 74 ...line 16, in bar
75 75 div0()
76 76 ...line 8, in div0
77 77 x/y
78 78 ZeroDivisionError: integer division or modulo by zero
79 79 """
80 80
81 81
82 82 def doctest_tb_context():
83 83 """
84 84 In [3]: xmode context
85 85 Exception reporting mode: Context
86 86
87 87 In [4]: run simpleerr.py
88 88 ---------------------------------------------------------------------------
89 89 ZeroDivisionError Traceback (most recent call last)
90 90 <BLANKLINE>
91 91 ... in <module>()
92 92 30 mode = 'div'
93 93 31
94 94 ---> 32 bar(mode)
95 95 33
96 96 34
97 97 <BLANKLINE>
98 98 ... in bar(mode)
99 99 14 "bar"
100 100 15 if mode=='div':
101 101 ---> 16 div0()
102 102 17 elif mode=='exit':
103 103 18 try:
104 104 <BLANKLINE>
105 105 ... in div0()
106 106 6 x = 1
107 107 7 y = 0
108 108 ----> 8 x/y
109 109 9
110 110 10 def sysexit(stat, mode):
111 111 <BLANKLINE>
112 112 ZeroDivisionError: integer division or modulo by zero
113 113 """
114 114
115 115
116 116 def doctest_tb_verbose():
117 117 """
118 118 In [5]: xmode verbose
119 119 Exception reporting mode: Verbose
120 120
121 121 In [6]: run simpleerr.py
122 122 ---------------------------------------------------------------------------
123 123 ZeroDivisionError Traceback (most recent call last)
124 124 <BLANKLINE>
125 125 ... in <module>()
126 126 30 mode = 'div'
127 127 31
128 128 ---> 32 bar(mode)
129 129 global bar = <function bar at ...>
130 130 global mode = 'div'
131 131 33
132 132 34
133 133 <BLANKLINE>
134 134 ... in bar(mode='div')
135 135 14 "bar"
136 136 15 if mode=='div':
137 137 ---> 16 div0()
138 138 global div0 = <function div0 at ...>
139 139 17 elif mode=='exit':
140 140 18 try:
141 141 <BLANKLINE>
142 142 ... in div0()
143 143 6 x = 1
144 144 7 y = 0
145 145 ----> 8 x/y
146 146 x = 1
147 147 y = 0
148 148 9
149 149 10 def sysexit(stat, mode):
150 150 <BLANKLINE>
151 151 ZeroDivisionError: integer division or modulo by zero
152 152 """
153 153
154 154
155 155 def doctest_tb_sysexit():
156 156 """
157 157 In [17]: %xmode plain
158 158 Exception reporting mode: Plain
159 159
160 160 In [18]: %run simpleerr.py exit
161 161 An exception has occurred, use %tb to see the full traceback.
162 162 SystemExit: (1, 'Mode = exit')
163 163
164 164 In [19]: %run simpleerr.py exit 2
165 165 An exception has occurred, use %tb to see the full traceback.
166 166 SystemExit: (2, 'Mode = exit')
167 167
168 168 In [20]: %tb
169 169 Traceback (most recent call last):
170 170 File ... in <module>
171 171 bar(mode)
172 172 File ... line 22, in bar
173 173 sysexit(stat, mode)
174 174 File ... line 11, in sysexit
175 175 raise SystemExit(stat, 'Mode = %s' % mode)
176 176 SystemExit: (2, 'Mode = exit')
177 177
178 178 In [21]: %xmode context
179 179 Exception reporting mode: Context
180 180
181 181 In [22]: %tb
182 182 ---------------------------------------------------------------------------
183 183 SystemExit Traceback (most recent call last)
184 184 <BLANKLINE>
185 185 ...<module>()
186 186 30 mode = 'div'
187 187 31
188 188 ---> 32 bar(mode)
189 189 33
190 190 34
191 191 <BLANKLINE>
192 192 ...bar(mode)
193 193 20 except:
194 194 21 stat = 1
195 195 ---> 22 sysexit(stat, mode)
196 196 23 else:
197 197 24 raise ValueError('Unknown mode')
198 198 <BLANKLINE>
199 199 ...sysexit(stat, mode)
200 200 9
201 201 10 def sysexit(stat, mode):
202 202 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
203 203 12
204 204 13 def bar(mode):
205 205 <BLANKLINE>
206 206 SystemExit: (2, 'Mode = exit')
207 207
208 208 In [23]: %xmode verbose
209 209 Exception reporting mode: Verbose
210 210
211 211 In [24]: %tb
212 212 ---------------------------------------------------------------------------
213 213 SystemExit Traceback (most recent call last)
214 214 <BLANKLINE>
215 215 ... in <module>()
216 216 30 mode = 'div'
217 217 31
218 218 ---> 32 bar(mode)
219 219 global bar = <function bar at ...>
220 220 global mode = 'exit'
221 221 33
222 222 34
223 223 <BLANKLINE>
224 224 ... in bar(mode='exit')
225 225 20 except:
226 226 21 stat = 1
227 227 ---> 22 sysexit(stat, mode)
228 228 global sysexit = <function sysexit at ...>
229 229 stat = 2
230 230 mode = 'exit'
231 231 23 else:
232 232 24 raise ValueError('Unknown mode')
233 233 <BLANKLINE>
234 234 ... in sysexit(stat=2, mode='exit')
235 235 9
236 236 10 def sysexit(stat, mode):
237 237 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
238 238 global SystemExit = undefined
239 239 stat = 2
240 240 mode = 'exit'
241 241 12
242 242 13 def bar(mode):
243 243 <BLANKLINE>
244 244 SystemExit: (2, 'Mode = exit')
245 245 """
@@ -1,207 +1,234 b''
1 1 # encoding: utf-8
2 2
3 3 """Classes and functions for kernel related errors and exceptions."""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 # Tell nose to skip this module
8 8 __test__ = {}
9 9
10 10 #-------------------------------------------------------------------------------
11 11 # Copyright (C) 2008 The IPython Development Team
12 12 #
13 13 # Distributed under the terms of the BSD License. The full license is in
14 14 # the file COPYING, distributed as part of this software.
15 15 #-------------------------------------------------------------------------------
16 16
17 17 #-------------------------------------------------------------------------------
18 18 # Imports
19 19 #-------------------------------------------------------------------------------
20 20
21 from twisted.python import failure
22
21 23 from IPython.kernel.core import error
22 24
23 25 #-------------------------------------------------------------------------------
24 26 # Error classes
25 27 #-------------------------------------------------------------------------------
26 28
27 29 class KernelError(error.IPythonError):
28 30 pass
29 31
32
30 33 class NotDefined(KernelError):
31 34 def __init__(self, name):
32 35 self.name = name
33 36 self.args = (name,)
34 37
35 38 def __repr__(self):
36 39 return '<NotDefined: %s>' % self.name
37 40
38 41 __str__ = __repr__
39 42
43
40 44 class QueueCleared(KernelError):
41 45 pass
42 46
47
43 48 class IdInUse(KernelError):
44 49 pass
45 50
51
46 52 class ProtocolError(KernelError):
47 53 pass
48 54
55
49 56 class ConnectionError(KernelError):
50 57 pass
51 58
59
52 60 class InvalidEngineID(KernelError):
53 61 pass
54
62
63
55 64 class NoEnginesRegistered(KernelError):
56 65 pass
57
66
67
58 68 class InvalidClientID(KernelError):
59 69 pass
60
70
71
61 72 class InvalidDeferredID(KernelError):
62 73 pass
63
74
75
64 76 class SerializationError(KernelError):
65 77 pass
66
78
79
67 80 class MessageSizeError(KernelError):
68 81 pass
69
82
83
70 84 class PBMessageSizeError(MessageSizeError):
71 85 pass
72
86
87
73 88 class ResultNotCompleted(KernelError):
74 89 pass
75
90
91
76 92 class ResultAlreadyRetrieved(KernelError):
77 93 pass
78
94
79 95 class ClientError(KernelError):
80 96 pass
81 97
98
82 99 class TaskAborted(KernelError):
83 100 pass
84 101
102
85 103 class TaskTimeout(KernelError):
86 104 pass
87 105
106
88 107 class NotAPendingResult(KernelError):
89 108 pass
90 109
110
91 111 class UnpickleableException(KernelError):
92 112 pass
93 113
114
94 115 class AbortedPendingDeferredError(KernelError):
95 116 pass
96 117
118
97 119 class InvalidProperty(KernelError):
98 120 pass
99 121
122
100 123 class MissingBlockArgument(KernelError):
101 124 pass
102 125
126
103 127 class StopLocalExecution(KernelError):
104 128 pass
105 129
130
106 131 class SecurityError(KernelError):
107 132 pass
108 133
134
109 135 class FileTimeoutError(KernelError):
110 136 pass
111 137
138
112 139 class TaskRejectError(KernelError):
113 140 """Exception to raise when a task should be rejected by an engine.
114 141
115 142 This exception can be used to allow a task running on an engine to test
116 143 if the engine (or the user's namespace on the engine) has the needed
117 144 task dependencies. If not, the task should raise this exception. For
118 145 the task to be retried on another engine, the task should be created
119 146 with the `retries` argument > 1.
120 147
121 148 The advantage of this approach over our older properties system is that
122 149 tasks have full access to the user's namespace on the engines and the
123 150 properties don't have to be managed or tested by the controller.
124 151 """
125 152
153
126 154 class CompositeError(KernelError):
127 155 def __init__(self, message, elist):
128 156 Exception.__init__(self, *(message, elist))
129 157 # Don't use pack_exception because it will conflict with the .message
130 158 # attribute that is being deprecated in 2.6 and beyond.
131 159 self.msg = message
132 160 self.elist = elist
133 161
134 162 def _get_engine_str(self, ev):
135 163 try:
136 164 ei = ev._ipython_engine_info
137 165 except AttributeError:
138 166 return '[Engine Exception]'
139 167 else:
140 168 return '[%i:%s]: ' % (ei['engineid'], ei['method'])
141 169
142 170 def _get_traceback(self, ev):
143 171 try:
144 172 tb = ev._ipython_traceback_text
145 173 except AttributeError:
146 174 return 'No traceback available'
147 175 else:
148 176 return tb
149 177
150 178 def __str__(self):
151 179 s = str(self.msg)
152 180 for et, ev, etb in self.elist:
153 181 engine_str = self._get_engine_str(ev)
154 182 s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev)
155 183 return s
156 184
157 185 def print_tracebacks(self, excid=None):
158 186 if excid is None:
159 187 for (et,ev,etb) in self.elist:
160 188 print self._get_engine_str(ev)
161 189 print self._get_traceback(ev)
162 190 print
163 191 else:
164 192 try:
165 193 et,ev,etb = self.elist[excid]
166 194 except:
167 195 raise IndexError("an exception with index %i does not exist"%excid)
168 196 else:
169 197 print self._get_engine_str(ev)
170 198 print self._get_traceback(ev)
171 199
172 200 def raise_exception(self, excid=0):
173 201 try:
174 202 et,ev,etb = self.elist[excid]
175 203 except:
176 204 raise IndexError("an exception with index %i does not exist"%excid)
177 205 else:
178 206 raise et, ev, etb
179 207
180 def collect_exceptions(rlist, method):
181 from twisted.python import failure
182 208
209 def collect_exceptions(rlist, method):
183 210 elist = []
184 211 for r in rlist:
185 212 if isinstance(r, failure.Failure):
186 213 r.cleanFailure()
187 214 et, ev, etb = r.type, r.value, r.tb
188 215 # Sometimes we could have CompositeError in our list. Just take
189 216 # the errors out of them and put them in our new list. This
190 217 # has the effect of flattening lists of CompositeErrors into one
191 218 # CompositeError
192 219 if et==CompositeError:
193 220 for e in ev.elist:
194 221 elist.append(e)
195 222 else:
196 223 elist.append((et, ev, etb))
197 224 if len(elist)==0:
198 225 return rlist
199 226 else:
200 227 msg = "one or more exceptions from call to method: %s" % (method)
201 228 # This silliness is needed so the debugger has access to the exception
202 229 # instance (e in this case)
203 230 try:
204 231 raise CompositeError(msg, elist)
205 232 except CompositeError, e:
206 233 raise e
207 234
@@ -1,340 +1,345 b''
1 1 # encoding: utf-8
2 2 """
3 3 Utilities for path handling.
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2009 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 import os
18 18 import sys
19 19
20 20 import IPython
21 21 from IPython.utils.process import xsys
22 22 from IPython.utils.importstring import import_item
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Code
26 26 #-----------------------------------------------------------------------------
27 27
28 28
29 29 def _get_long_path_name(path):
30 30 """Dummy no-op."""
31 31 return path
32 32
33 33
34 34 if sys.platform == 'win32':
35 35 def _get_long_path_name(path):
36 36 """Get a long path name (expand ~) on Windows using ctypes.
37 37
38 38 Examples
39 39 --------
40 40
41 41 >>> get_long_path_name('c:\\docume~1')
42 42 u'c:\\\\Documents and Settings'
43 43
44 44 """
45 45 try:
46 46 import ctypes
47 47 except ImportError:
48 48 raise ImportError('you need to have ctypes installed for this to work')
49 49 _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
50 50 _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
51 51 ctypes.c_uint ]
52 52
53 53 buf = ctypes.create_unicode_buffer(260)
54 54 rv = _GetLongPathName(path, buf, 260)
55 55 if rv == 0 or rv > 260:
56 56 return path
57 57 else:
58 58 return buf.value
59 59
60 60
61 61 def get_long_path_name(path):
62 62 """Expand a path into its long form.
63 63
64 64 On Windows this expands any ~ in the paths. On other platforms, it is
65 65 a null operation.
66 66 """
67 67 return _get_long_path_name(path)
68 68
69 69
70 70 def get_py_filename(name):
71 71 """Return a valid python filename in the current directory.
72 72
73 73 If the given name is not a file, it adds '.py' and searches again.
74 74 Raises IOError with an informative message if the file isn't found."""
75 75
76 76 name = os.path.expanduser(name)
77 77 if not os.path.isfile(name) and not name.endswith('.py'):
78 78 name += '.py'
79 79 if os.path.isfile(name):
80 80 return name
81 81 else:
82 82 raise IOError,'File `%s` not found.' % name
83 83
84 84
85 85 def filefind(filename, path_dirs=None):
86 86 """Find a file by looking through a sequence of paths.
87 87
88 88 This iterates through a sequence of paths looking for a file and returns
89 89 the full, absolute path of the first occurence of the file. If no set of
90 90 path dirs is given, the filename is tested as is, after running through
91 91 :func:`expandvars` and :func:`expanduser`. Thus a simple call::
92 92
93 93 filefind('myfile.txt')
94 94
95 95 will find the file in the current working dir, but::
96 96
97 97 filefind('~/myfile.txt')
98 98
99 99 Will find the file in the users home directory. This function does not
100 100 automatically try any paths, such as the cwd or the user's home directory.
101 101
102 102 Parameters
103 103 ----------
104 104 filename : str
105 105 The filename to look for.
106 106 path_dirs : str, None or sequence of str
107 107 The sequence of paths to look for the file in. If None, the filename
108 108 need to be absolute or be in the cwd. If a string, the string is
109 109 put into a sequence and the searched. If a sequence, walk through
110 110 each element and join with ``filename``, calling :func:`expandvars`
111 111 and :func:`expanduser` before testing for existence.
112 112
113 113 Returns
114 114 -------
115 115 Raises :exc:`IOError` or returns absolute path to file.
116 116 """
117 117
118 118 # If paths are quoted, abspath gets confused, strip them...
119 119 filename = filename.strip('"').strip("'")
120 120 # If the input is an absolute path, just check it exists
121 121 if os.path.isabs(filename) and os.path.isfile(filename):
122 122 return filename
123 123
124 124 if path_dirs is None:
125 125 path_dirs = ("",)
126 126 elif isinstance(path_dirs, basestring):
127 127 path_dirs = (path_dirs,)
128 128
129 129 for path in path_dirs:
130 130 if path == '.': path = os.getcwd()
131 131 testname = expand_path(os.path.join(path, filename))
132 132 if os.path.isfile(testname):
133 133 return os.path.abspath(testname)
134 134
135 135 raise IOError("File %r does not exist in any of the search paths: %r" %
136 136 (filename, path_dirs) )
137 137
138 138
139 139 class HomeDirError(Exception):
140 140 pass
141 141
142 142
143 143 def get_home_dir():
144 144 """Return the closest possible equivalent to a 'home' directory.
145 145
146 146 * On POSIX, we try $HOME.
147 147 * On Windows we try:
148 148 - %HOME%: rare, but some people with unix-like setups may have defined it
149 149 - %HOMESHARE%
150 150 - %HOMEDRIVE\%HOMEPATH%
151 151 - %USERPROFILE%
152 152 - Registry hack
153 153 * On Dos C:\
154 154
155 155 Currently only Posix and NT are implemented, a HomeDirError exception is
156 156 raised for all other OSes.
157 157 """
158 158
159 159 isdir = os.path.isdir
160 160 env = os.environ
161 161
162 162 # first, check py2exe distribution root directory for _ipython.
163 163 # This overrides all. Normally does not exist.
164 164
165 165 if hasattr(sys, "frozen"): #Is frozen by py2exe
166 166 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
167 167 root, rest = IPython.__file__.lower().split('library.zip')
168 168 else:
169 169 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
170 170 root=os.path.abspath(root).rstrip('\\')
171 171 if isdir(os.path.join(root, '_ipython')):
172 172 os.environ["IPYKITROOT"] = root
173 173 return root.decode(sys.getfilesystemencoding())
174 174
175 175 if os.name == 'posix':
176 176 # Linux, Unix, AIX, OS X
177 177 try:
178 178 homedir = env['HOME']
179 179 except KeyError:
180 180 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
181 181 else:
182 182 return homedir.decode(sys.getfilesystemencoding())
183 183 elif os.name == 'nt':
184 184 # Now for win9x, XP, Vista, 7?
185 185 # For some strange reason all of these return 'nt' for os.name.
186 186 # First look for a network home directory. This will return the UNC
187 187 # path (\\server\\Users\%username%) not the mapped path (Z:\). This
188 188 # is needed when running IPython on cluster where all paths have to
189 189 # be UNC.
190 190 try:
191 # A user with a lot of unix tools in win32 may have defined $HOME,
192 # honor it if it exists, but otherwise let the more typical
193 # %HOMESHARE% variable be used.
194 homedir = env.get('HOME')
195 if homedir is None:
196 homedir = env['HOMESHARE']
191 homedir = env['HOMESHARE']
197 192 except KeyError:
198 193 pass
199 194 else:
200 195 if isdir(homedir):
201 196 return homedir.decode(sys.getfilesystemencoding())
202 197
203 198 # Now look for a local home directory
204 199 try:
205 200 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
206 201 except KeyError:
207 202 pass
208 203 else:
209 204 if isdir(homedir):
210 205 return homedir.decode(sys.getfilesystemencoding())
211 206
212 207 # Now the users profile directory
213 208 try:
214 209 homedir = os.path.join(env['USERPROFILE'])
215 210 except KeyError:
216 211 pass
217 212 else:
218 213 if isdir(homedir):
219 214 return homedir.decode(sys.getfilesystemencoding())
220 215
221 216 # Use the registry to get the 'My Documents' folder.
222 217 try:
223 218 import _winreg as wreg
224 219 key = wreg.OpenKey(
225 220 wreg.HKEY_CURRENT_USER,
226 221 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
227 222 )
228 223 homedir = wreg.QueryValueEx(key,'Personal')[0]
229 224 key.Close()
230 225 except:
231 226 pass
232 227 else:
233 228 if isdir(homedir):
234 229 return homedir.decode(sys.getfilesystemencoding())
235 230
231 # A user with a lot of unix tools in win32 may have defined $HOME.
232 # Try this as a last ditch option.
233 try:
234 homedir = env['HOME']
235 except KeyError:
236 pass
237 else:
238 if isdir(homedir):
239 return homedir.decode(sys.getfilesystemencoding())
240
236 241 # If all else fails, raise HomeDirError
237 242 raise HomeDirError('No valid home directory could be found')
238 243 elif os.name == 'dos':
239 244 # Desperate, may do absurd things in classic MacOS. May work under DOS.
240 245 return 'C:\\'.decode(sys.getfilesystemencoding())
241 246 else:
242 247 raise HomeDirError('No valid home directory could be found for your OS')
243 248
244 249
245 250 def get_ipython_dir():
246 251 """Get the IPython directory for this platform and user.
247 252
248 253 This uses the logic in `get_home_dir` to find the home directory
249 254 and the adds .ipython to the end of the path.
250 255 """
251 256 ipdir_def = '.ipython'
252 257 home_dir = get_home_dir()
253 258 # import pdb; pdb.set_trace() # dbg
254 259 ipdir = os.environ.get(
255 260 'IPYTHON_DIR', os.environ.get(
256 261 'IPYTHONDIR', os.path.join(home_dir, ipdir_def)
257 262 )
258 263 )
259 264 return ipdir.decode(sys.getfilesystemencoding())
260 265
261 266
262 267 def get_ipython_package_dir():
263 268 """Get the base directory where IPython itself is installed."""
264 269 ipdir = os.path.dirname(IPython.__file__)
265 270 return ipdir.decode(sys.getfilesystemencoding())
266 271
267 272
268 273 def get_ipython_module_path(module_str):
269 274 """Find the path to an IPython module in this version of IPython.
270 275
271 276 This will always find the version of the module that is in this importable
272 277 IPython package. This will always return the path to the ``.py``
273 278 version of the module.
274 279 """
275 280 if module_str == 'IPython':
276 281 return os.path.join(get_ipython_package_dir(), '__init__.py')
277 282 mod = import_item(module_str)
278 283 the_path = mod.__file__.replace('.pyc', '.py')
279 284 the_path = the_path.replace('.pyo', '.py')
280 285 return the_path.decode(sys.getfilesystemencoding())
281 286
282 287
283 288 def expand_path(s):
284 289 """Expand $VARS and ~names in a string, like a shell
285 290
286 291 :Examples:
287 292
288 293 In [2]: os.environ['FOO']='test'
289 294
290 295 In [3]: expand_path('variable FOO is $FOO')
291 296 Out[3]: 'variable FOO is test'
292 297 """
293 298 # This is a pretty subtle hack. When expand user is given a UNC path
294 299 # on Windows (\\server\share$\%username%), os.path.expandvars, removes
295 300 # the $ to get (\\server\share\%username%). I think it considered $
296 301 # alone an empty var. But, we need the $ to remains there (it indicates
297 302 # a hidden share).
298 303 if os.name=='nt':
299 304 s = s.replace('$\\', 'IPYTHON_TEMP')
300 305 s = os.path.expandvars(os.path.expanduser(s))
301 306 if os.name=='nt':
302 307 s = s.replace('IPYTHON_TEMP', '$\\')
303 308 return s
304 309
305 310
306 311 def target_outdated(target,deps):
307 312 """Determine whether a target is out of date.
308 313
309 314 target_outdated(target,deps) -> 1/0
310 315
311 316 deps: list of filenames which MUST exist.
312 317 target: single filename which may or may not exist.
313 318
314 319 If target doesn't exist or is older than any file listed in deps, return
315 320 true, otherwise return false.
316 321 """
317 322 try:
318 323 target_time = os.path.getmtime(target)
319 324 except os.error:
320 325 return 1
321 326 for dep in deps:
322 327 dep_time = os.path.getmtime(dep)
323 328 if dep_time > target_time:
324 329 #print "For target",target,"Dep failed:",dep # dbg
325 330 #print "times (dep,tar):",dep_time,target_time # dbg
326 331 return 1
327 332 return 0
328 333
329 334
330 335 def target_update(target,deps,cmd):
331 336 """Update a target with a given command given a list of dependencies.
332 337
333 338 target_update(target,deps,cmd) -> runs cmd if target is outdated.
334 339
335 340 This is just a wrapper around target_outdated() which calls the given
336 341 command if target is outdated."""
337 342
338 343 if target_outdated(target,deps):
339 344 xsys(cmd)
340 345
General Comments 0
You need to be logged in to leave comments. Login now