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