##// END OF EJS Templates
Fernando Perez -
Show More

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

1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,2679 +1,2729 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.4 or newer.
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8 """
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
12 12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #
17 17 # Note: this code originally subclassed code.InteractiveConsole from the
18 18 # Python standard library. Over time, all of that class has been copied
19 19 # verbatim here for modifications which could not be accomplished by
20 20 # subclassing. At this point, there are no dependencies at all on the code
21 21 # module anymore (it is not even imported). The Python License (sec. 2)
22 22 # allows for this, but it's always nice to acknowledge credit where credit is
23 23 # due.
24 24 #*****************************************************************************
25 25
26 26 #****************************************************************************
27 27 # Modules and globals
28 28
29 29 # Python standard modules
30 30 import __main__
31 31 import __builtin__
32 32 import StringIO
33 33 import bdb
34 34 import cPickle as pickle
35 35 import codeop
36 36 import exceptions
37 37 import glob
38 38 import inspect
39 39 import keyword
40 40 import new
41 41 import os
42 42 import pydoc
43 43 import re
44 44 import shutil
45 45 import string
46 46 import sys
47 47 import tempfile
48 48 import traceback
49 49 import types
50 50 import warnings
51 51 warnings.filterwarnings('ignore', r'.*sets module*')
52 52 from sets import Set
53 53 from pprint import pprint, pformat
54 54
55 55 # IPython's own modules
56 56 #import IPython
57 57 from IPython import Debugger,OInspect,PyColorize,ultraTB
58 58 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
59 59 from IPython.Extensions import pickleshare
60 60 from IPython.FakeModule import FakeModule
61 61 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
62 62 from IPython.Logger import Logger
63 63 from IPython.Magic import Magic
64 64 from IPython.Prompts import CachedOutput
65 65 from IPython.ipstruct import Struct
66 66 from IPython.background_jobs import BackgroundJobManager
67 67 from IPython.usage import cmd_line_usage,interactive_usage
68 68 from IPython.genutils import *
69 69 from IPython.strdispatch import StrDispatch
70 70 import IPython.ipapi
71 71 import IPython.history
72 72 import IPython.prefilter as prefilter
73 73 import IPython.shadowns
74 74 # Globals
75 75
76 76 # store the builtin raw_input globally, and use this always, in case user code
77 77 # overwrites it (like wx.py.PyShell does)
78 78 raw_input_original = raw_input
79 79
80 80 # compiled regexps for autoindent management
81 81 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
82 82
83 83
84 84 #****************************************************************************
85 85 # Some utility function definitions
86 86
87 87 ini_spaces_re = re.compile(r'^(\s+)')
88 88
89 89 def num_ini_spaces(strng):
90 90 """Return the number of initial spaces in a string"""
91 91
92 92 ini_spaces = ini_spaces_re.match(strng)
93 93 if ini_spaces:
94 94 return ini_spaces.end()
95 95 else:
96 96 return 0
97 97
98 98 def softspace(file, newvalue):
99 99 """Copied from code.py, to remove the dependency"""
100 100
101 101 oldvalue = 0
102 102 try:
103 103 oldvalue = file.softspace
104 104 except AttributeError:
105 105 pass
106 106 try:
107 107 file.softspace = newvalue
108 108 except (AttributeError, TypeError):
109 109 # "attribute-less object" or "read-only attributes"
110 110 pass
111 111 return oldvalue
112 112
113 113
114 114 #****************************************************************************
115 115 # Local use exceptions
116 116 class SpaceInInput(exceptions.Exception): pass
117 117
118 118
119 119 #****************************************************************************
120 120 # Local use classes
121 121 class Bunch: pass
122 122
123 123 class Undefined: pass
124 124
125 125 class Quitter(object):
126 126 """Simple class to handle exit, similar to Python 2.5's.
127 127
128 128 It handles exiting in an ipython-safe manner, which the one in Python 2.5
129 129 doesn't do (obviously, since it doesn't know about ipython)."""
130 130
131 131 def __init__(self,shell,name):
132 132 self.shell = shell
133 133 self.name = name
134 134
135 135 def __repr__(self):
136 136 return 'Type %s() to exit.' % self.name
137 137 __str__ = __repr__
138 138
139 139 def __call__(self):
140 140 self.shell.exit()
141 141
142 142 class InputList(list):
143 143 """Class to store user input.
144 144
145 145 It's basically a list, but slices return a string instead of a list, thus
146 146 allowing things like (assuming 'In' is an instance):
147 147
148 148 exec In[4:7]
149 149
150 150 or
151 151
152 152 exec In[5:9] + In[14] + In[21:25]"""
153 153
154 154 def __getslice__(self,i,j):
155 155 return ''.join(list.__getslice__(self,i,j))
156 156
157 157 class SyntaxTB(ultraTB.ListTB):
158 158 """Extension which holds some state: the last exception value"""
159 159
160 160 def __init__(self,color_scheme = 'NoColor'):
161 161 ultraTB.ListTB.__init__(self,color_scheme)
162 162 self.last_syntax_error = None
163 163
164 164 def __call__(self, etype, value, elist):
165 165 self.last_syntax_error = value
166 166 ultraTB.ListTB.__call__(self,etype,value,elist)
167 167
168 168 def clear_err_state(self):
169 169 """Return the current error state and clear it"""
170 170 e = self.last_syntax_error
171 171 self.last_syntax_error = None
172 172 return e
173 173
174 174 #****************************************************************************
175 175 # Main IPython class
176 176
177 177 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
178 178 # until a full rewrite is made. I've cleaned all cross-class uses of
179 179 # attributes and methods, but too much user code out there relies on the
180 180 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
181 181 #
182 182 # But at least now, all the pieces have been separated and we could, in
183 183 # principle, stop using the mixin. This will ease the transition to the
184 184 # chainsaw branch.
185 185
186 186 # For reference, the following is the list of 'self.foo' uses in the Magic
187 187 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
188 188 # class, to prevent clashes.
189 189
190 190 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
191 191 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
192 192 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
193 193 # 'self.value']
194 194
195 195 class InteractiveShell(object,Magic):
196 196 """An enhanced console for Python."""
197 197
198 198 # class attribute to indicate whether the class supports threads or not.
199 199 # Subclasses with thread support should override this as needed.
200 200 isthreaded = False
201 201
202 202 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
203 203 user_ns=None,user_global_ns=None,banner2='',
204 204 custom_exceptions=((),None),embedded=False):
205 205
206 206 # log system
207 207 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
208 208
209 209 # Job manager (for jobs run as background threads)
210 210 self.jobs = BackgroundJobManager()
211 211
212 212 # Store the actual shell's name
213 213 self.name = name
214 214 self.more = False
215 215
216 216 # We need to know whether the instance is meant for embedding, since
217 217 # global/local namespaces need to be handled differently in that case
218 218 self.embedded = embedded
219 219 if embedded:
220 220 # Control variable so users can, from within the embedded instance,
221 221 # permanently deactivate it.
222 222 self.embedded_active = True
223 223
224 224 # command compiler
225 225 self.compile = codeop.CommandCompiler()
226 226
227 227 # User input buffer
228 228 self.buffer = []
229 229
230 230 # Default name given in compilation of code
231 231 self.filename = '<ipython console>'
232 232
233 233 # Install our own quitter instead of the builtins. For python2.3-2.4,
234 234 # this brings in behavior like 2.5, and for 2.5 it's identical.
235 235 __builtin__.exit = Quitter(self,'exit')
236 236 __builtin__.quit = Quitter(self,'quit')
237 237
238 238 # Make an empty namespace, which extension writers can rely on both
239 239 # existing and NEVER being used by ipython itself. This gives them a
240 240 # convenient location for storing additional information and state
241 241 # their extensions may require, without fear of collisions with other
242 242 # ipython names that may develop later.
243 243 self.meta = Struct()
244 244
245 245 # Create the namespace where the user will operate. user_ns is
246 246 # normally the only one used, and it is passed to the exec calls as
247 247 # the locals argument. But we do carry a user_global_ns namespace
248 248 # given as the exec 'globals' argument, This is useful in embedding
249 249 # situations where the ipython shell opens in a context where the
250 250 # distinction between locals and globals is meaningful. For
251 251 # non-embedded contexts, it is just the same object as the user_ns dict.
252 252
253 253 # FIXME. For some strange reason, __builtins__ is showing up at user
254 254 # level as a dict instead of a module. This is a manual fix, but I
255 255 # should really track down where the problem is coming from. Alex
256 256 # Schmolck reported this problem first.
257 257
258 258 # A useful post by Alex Martelli on this topic:
259 259 # Re: inconsistent value from __builtins__
260 260 # Von: Alex Martelli <aleaxit@yahoo.com>
261 261 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
262 262 # Gruppen: comp.lang.python
263 263
264 264 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
265 265 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
266 266 # > <type 'dict'>
267 267 # > >>> print type(__builtins__)
268 268 # > <type 'module'>
269 269 # > Is this difference in return value intentional?
270 270
271 271 # Well, it's documented that '__builtins__' can be either a dictionary
272 272 # or a module, and it's been that way for a long time. Whether it's
273 273 # intentional (or sensible), I don't know. In any case, the idea is
274 274 # that if you need to access the built-in namespace directly, you
275 275 # should start with "import __builtin__" (note, no 's') which will
276 276 # definitely give you a module. Yeah, it's somewhat confusing:-(.
277 277
278 278 # These routines return properly built dicts as needed by the rest of
279 279 # the code, and can also be used by extension writers to generate
280 280 # properly initialized namespaces.
281 281 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
282 282 user_global_ns)
283 283
284 284 # Assign namespaces
285 285 # This is the namespace where all normal user variables live
286 286 self.user_ns = user_ns
287 287 self.user_global_ns = user_global_ns
288 288 # A namespace to keep track of internal data structures to prevent
289 289 # them from cluttering user-visible stuff. Will be updated later
290 290 self.internal_ns = {}
291 291
292 292 # Namespace of system aliases. Each entry in the alias
293 293 # table must be a 2-tuple of the form (N,name), where N is the number
294 294 # of positional arguments of the alias.
295 295 self.alias_table = {}
296 296
297 297 # A table holding all the namespaces IPython deals with, so that
298 298 # introspection facilities can search easily.
299 299 self.ns_table = {'user':user_ns,
300 300 'user_global':user_global_ns,
301 301 'alias':self.alias_table,
302 302 'internal':self.internal_ns,
303 303 'builtin':__builtin__.__dict__
304 304 }
305 305 # The user namespace MUST have a pointer to the shell itself.
306 306 self.user_ns[name] = self
307 307
308 308 # We need to insert into sys.modules something that looks like a
309 309 # module but which accesses the IPython namespace, for shelve and
310 310 # pickle to work interactively. Normally they rely on getting
311 311 # everything out of __main__, but for embedding purposes each IPython
312 312 # instance has its own private namespace, so we can't go shoving
313 313 # everything into __main__.
314 314
315 315 # note, however, that we should only do this for non-embedded
316 316 # ipythons, which really mimic the __main__.__dict__ with their own
317 317 # namespace. Embedded instances, on the other hand, should not do
318 318 # this because they need to manage the user local/global namespaces
319 319 # only, but they live within a 'normal' __main__ (meaning, they
320 320 # shouldn't overtake the execution environment of the script they're
321 321 # embedded in).
322 322
323 323 if not embedded:
324 324 try:
325 325 main_name = self.user_ns['__name__']
326 326 except KeyError:
327 327 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
328 328 else:
329 329 #print "pickle hack in place" # dbg
330 330 #print 'main_name:',main_name # dbg
331 331 sys.modules[main_name] = FakeModule(self.user_ns)
332 332
333 333 # Now that FakeModule produces a real module, we've run into a nasty
334 334 # problem: after script execution (via %run), the module where the user
335 335 # code ran is deleted. Now that this object is a true module (needed
336 336 # so docetst and other tools work correctly), the Python module
337 337 # teardown mechanism runs over it, and sets to None every variable
338 # present in that module. This means that later calls to functions
339 # defined in the script (which have become interactively visible after
340 # script exit) fail, because they hold references to objects that have
341 # become overwritten into None. The only solution I see right now is
342 # to protect every FakeModule used by %run by holding an internal
343 # reference to it. This private list will be used for that. The
344 # %reset command will flush it as well.
345 self._user_main_modules = []
346
338 # present in that module. Top-level references to objects from the
339 # script survive, because the user_ns is updated with them. However,
340 # calling functions defined in the script that use other things from
341 # the script will fail, because the function's closure had references
342 # to the original objects, which are now all None. So we must protect
343 # these modules from deletion by keeping a cache. To avoid keeping
344 # stale modules around (we only need the one from the last run), we use
345 # a dict keyed with the full path to the script, so only the last
346 # version of the module is held in the cache. The %reset command will
347 # flush this cache. See the cache_main_mod() and clear_main_mod_cache()
348 # methods for details on use.
349 self._user_main_modules = {}
350
347 351 # List of input with multi-line handling.
348 352 # Fill its zero entry, user counter starts at 1
349 353 self.input_hist = InputList(['\n'])
350 354 # This one will hold the 'raw' input history, without any
351 355 # pre-processing. This will allow users to retrieve the input just as
352 356 # it was exactly typed in by the user, with %hist -r.
353 357 self.input_hist_raw = InputList(['\n'])
354 358
355 359 # list of visited directories
356 360 try:
357 361 self.dir_hist = [os.getcwd()]
358 362 except OSError:
359 363 self.dir_hist = []
360 364
361 365 # dict of output history
362 366 self.output_hist = {}
363 367
364 368 # Get system encoding at startup time. Certain terminals (like Emacs
365 369 # under Win32 have it set to None, and we need to have a known valid
366 370 # encoding to use in the raw_input() method
367 371 try:
368 372 self.stdin_encoding = sys.stdin.encoding or 'ascii'
369 373 except AttributeError:
370 374 self.stdin_encoding = 'ascii'
371 375
372 376 # dict of things NOT to alias (keywords, builtins and some magics)
373 377 no_alias = {}
374 378 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
375 379 for key in keyword.kwlist + no_alias_magics:
376 380 no_alias[key] = 1
377 381 no_alias.update(__builtin__.__dict__)
378 382 self.no_alias = no_alias
379 383
380 384 # make global variables for user access to these
381 385 self.user_ns['_ih'] = self.input_hist
382 386 self.user_ns['_oh'] = self.output_hist
383 387 self.user_ns['_dh'] = self.dir_hist
384 388
385 389 # user aliases to input and output histories
386 390 self.user_ns['In'] = self.input_hist
387 391 self.user_ns['Out'] = self.output_hist
388 392
389 393 self.user_ns['_sh'] = IPython.shadowns
390 394 # Object variable to store code object waiting execution. This is
391 395 # used mainly by the multithreaded shells, but it can come in handy in
392 396 # other situations. No need to use a Queue here, since it's a single
393 397 # item which gets cleared once run.
394 398 self.code_to_run = None
395 399
396 400 # escapes for automatic behavior on the command line
397 401 self.ESC_SHELL = '!'
398 402 self.ESC_SH_CAP = '!!'
399 403 self.ESC_HELP = '?'
400 404 self.ESC_MAGIC = '%'
401 405 self.ESC_QUOTE = ','
402 406 self.ESC_QUOTE2 = ';'
403 407 self.ESC_PAREN = '/'
404 408
405 409 # And their associated handlers
406 410 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
407 411 self.ESC_QUOTE : self.handle_auto,
408 412 self.ESC_QUOTE2 : self.handle_auto,
409 413 self.ESC_MAGIC : self.handle_magic,
410 414 self.ESC_HELP : self.handle_help,
411 415 self.ESC_SHELL : self.handle_shell_escape,
412 416 self.ESC_SH_CAP : self.handle_shell_escape,
413 417 }
414 418
415 419 # class initializations
416 420 Magic.__init__(self,self)
417 421
418 422 # Python source parser/formatter for syntax highlighting
419 423 pyformat = PyColorize.Parser().format
420 424 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
421 425
422 426 # hooks holds pointers used for user-side customizations
423 427 self.hooks = Struct()
424 428
425 429 self.strdispatchers = {}
426 430
427 431 # Set all default hooks, defined in the IPython.hooks module.
428 432 hooks = IPython.hooks
429 433 for hook_name in hooks.__all__:
430 434 # default hooks have priority 100, i.e. low; user hooks should have
431 435 # 0-100 priority
432 436 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
433 437 #print "bound hook",hook_name
434 438
435 439 # Flag to mark unconditional exit
436 440 self.exit_now = False
437 441
438 442 self.usage_min = """\
439 443 An enhanced console for Python.
440 444 Some of its features are:
441 445 - Readline support if the readline library is present.
442 446 - Tab completion in the local namespace.
443 447 - Logging of input, see command-line options.
444 448 - System shell escape via ! , eg !ls.
445 449 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
446 450 - Keeps track of locally defined variables via %who, %whos.
447 451 - Show object information with a ? eg ?x or x? (use ?? for more info).
448 452 """
449 453 if usage: self.usage = usage
450 454 else: self.usage = self.usage_min
451 455
452 456 # Storage
453 457 self.rc = rc # This will hold all configuration information
454 458 self.pager = 'less'
455 459 # temporary files used for various purposes. Deleted at exit.
456 460 self.tempfiles = []
457 461
458 462 # Keep track of readline usage (later set by init_readline)
459 463 self.has_readline = False
460 464
461 465 # template for logfile headers. It gets resolved at runtime by the
462 466 # logstart method.
463 467 self.loghead_tpl = \
464 468 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
465 469 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
466 470 #log# opts = %s
467 471 #log# args = %s
468 472 #log# It is safe to make manual edits below here.
469 473 #log#-----------------------------------------------------------------------
470 474 """
471 475 # for pushd/popd management
472 476 try:
473 477 self.home_dir = get_home_dir()
474 478 except HomeDirError,msg:
475 479 fatal(msg)
476 480
477 481 self.dir_stack = []
478 482
479 483 # Functions to call the underlying shell.
480 484
481 485 # The first is similar to os.system, but it doesn't return a value,
482 486 # and it allows interpolation of variables in the user's namespace.
483 487 self.system = lambda cmd: \
484 488 self.hooks.shell_hook(self.var_expand(cmd,depth=2))
485 489
486 490 # These are for getoutput and getoutputerror:
487 491 self.getoutput = lambda cmd: \
488 492 getoutput(self.var_expand(cmd,depth=2),
489 493 header=self.rc.system_header,
490 494 verbose=self.rc.system_verbose)
491 495
492 496 self.getoutputerror = lambda cmd: \
493 497 getoutputerror(self.var_expand(cmd,depth=2),
494 498 header=self.rc.system_header,
495 499 verbose=self.rc.system_verbose)
496 500
497 501
498 502 # keep track of where we started running (mainly for crash post-mortem)
499 503 self.starting_dir = os.getcwd()
500 504
501 505 # Various switches which can be set
502 506 self.CACHELENGTH = 5000 # this is cheap, it's just text
503 507 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
504 508 self.banner2 = banner2
505 509
506 510 # TraceBack handlers:
507 511
508 512 # Syntax error handler.
509 513 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
510 514
511 515 # The interactive one is initialized with an offset, meaning we always
512 516 # want to remove the topmost item in the traceback, which is our own
513 517 # internal code. Valid modes: ['Plain','Context','Verbose']
514 518 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
515 519 color_scheme='NoColor',
516 520 tb_offset = 1)
517 521
518 522 # IPython itself shouldn't crash. This will produce a detailed
519 523 # post-mortem if it does. But we only install the crash handler for
520 524 # non-threaded shells, the threaded ones use a normal verbose reporter
521 525 # and lose the crash handler. This is because exceptions in the main
522 526 # thread (such as in GUI code) propagate directly to sys.excepthook,
523 527 # and there's no point in printing crash dumps for every user exception.
524 528 if self.isthreaded:
525 529 ipCrashHandler = ultraTB.FormattedTB()
526 530 else:
527 531 from IPython import CrashHandler
528 532 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
529 533 self.set_crash_handler(ipCrashHandler)
530 534
531 535 # and add any custom exception handlers the user may have specified
532 536 self.set_custom_exc(*custom_exceptions)
533 537
534 538 # indentation management
535 539 self.autoindent = False
536 540 self.indent_current_nsp = 0
537 541
538 542 # Make some aliases automatically
539 543 # Prepare list of shell aliases to auto-define
540 544 if os.name == 'posix':
541 545 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
542 546 'mv mv -i','rm rm -i','cp cp -i',
543 547 'cat cat','less less','clear clear',
544 548 # a better ls
545 549 'ls ls -F',
546 550 # long ls
547 551 'll ls -lF')
548 552 # Extra ls aliases with color, which need special treatment on BSD
549 553 # variants
550 554 ls_extra = ( # color ls
551 555 'lc ls -F -o --color',
552 556 # ls normal files only
553 557 'lf ls -F -o --color %l | grep ^-',
554 558 # ls symbolic links
555 559 'lk ls -F -o --color %l | grep ^l',
556 560 # directories or links to directories,
557 561 'ldir ls -F -o --color %l | grep /$',
558 562 # things which are executable
559 563 'lx ls -F -o --color %l | grep ^-..x',
560 564 )
561 565 # The BSDs don't ship GNU ls, so they don't understand the
562 566 # --color switch out of the box
563 567 if 'bsd' in sys.platform:
564 568 ls_extra = ( # ls normal files only
565 569 'lf ls -lF | grep ^-',
566 570 # ls symbolic links
567 571 'lk ls -lF | grep ^l',
568 572 # directories or links to directories,
569 573 'ldir ls -lF | grep /$',
570 574 # things which are executable
571 575 'lx ls -lF | grep ^-..x',
572 576 )
573 577 auto_alias = auto_alias + ls_extra
574 578 elif os.name in ['nt','dos']:
575 579 auto_alias = ('ls dir /on',
576 580 'ddir dir /ad /on', 'ldir dir /ad /on',
577 581 'mkdir mkdir','rmdir rmdir','echo echo',
578 582 'ren ren','cls cls','copy copy')
579 583 else:
580 584 auto_alias = ()
581 585 self.auto_alias = [s.split(None,1) for s in auto_alias]
582 586
583 587
584 588 # Produce a public API instance
585 589 self.api = IPython.ipapi.IPApi(self)
586 590
587 591 # Call the actual (public) initializer
588 592 self.init_auto_alias()
589 593
590 594 # track which builtins we add, so we can clean up later
591 595 self.builtins_added = {}
592 596 # This method will add the necessary builtins for operation, but
593 597 # tracking what it did via the builtins_added dict.
594 598
595 599 #TODO: remove this, redundant
596 600 self.add_builtins()
597
598
599
600
601 601 # end __init__
602 602
603 603 def var_expand(self,cmd,depth=0):
604 604 """Expand python variables in a string.
605 605
606 606 The depth argument indicates how many frames above the caller should
607 607 be walked to look for the local namespace where to expand variables.
608 608
609 609 The global namespace for expansion is always the user's interactive
610 610 namespace.
611 611 """
612 612
613 613 return str(ItplNS(cmd,
614 614 self.user_ns, # globals
615 615 # Skip our own frame in searching for locals:
616 616 sys._getframe(depth+1).f_locals # locals
617 617 ))
618 618
619 619 def pre_config_initialization(self):
620 620 """Pre-configuration init method
621 621
622 622 This is called before the configuration files are processed to
623 623 prepare the services the config files might need.
624 624
625 625 self.rc already has reasonable default values at this point.
626 626 """
627 627 rc = self.rc
628 628 try:
629 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
629 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
630 630 except exceptions.UnicodeDecodeError:
631 631 print "Your ipythondir can't be decoded to unicode!"
632 632 print "Please set HOME environment variable to something that"
633 633 print r"only has ASCII characters, e.g. c:\home"
634 634 print "Now it is",rc.ipythondir
635 635 sys.exit()
636 self.shadowhist = IPython.history.ShadowHist(self.db)
637
638
636 self.shadowhist = IPython.history.ShadowHist(self.db)
637
639 638 def post_config_initialization(self):
640 639 """Post configuration init method
641 640
642 641 This is called after the configuration files have been processed to
643 642 'finalize' the initialization."""
644 643
645 644 rc = self.rc
646 645
647 646 # Object inspector
648 647 self.inspector = OInspect.Inspector(OInspect.InspectColors,
649 648 PyColorize.ANSICodeColors,
650 649 'NoColor',
651 650 rc.object_info_string_level)
652 651
653 652 self.rl_next_input = None
654 653 self.rl_do_indent = False
655 654 # Load readline proper
656 655 if rc.readline:
657 656 self.init_readline()
658 657
659 658
660 659 # local shortcut, this is used a LOT
661 660 self.log = self.logger.log
662 661
663 662 # Initialize cache, set in/out prompts and printing system
664 663 self.outputcache = CachedOutput(self,
665 664 rc.cache_size,
666 665 rc.pprint,
667 666 input_sep = rc.separate_in,
668 667 output_sep = rc.separate_out,
669 668 output_sep2 = rc.separate_out2,
670 669 ps1 = rc.prompt_in1,
671 670 ps2 = rc.prompt_in2,
672 671 ps_out = rc.prompt_out,
673 672 pad_left = rc.prompts_pad_left)
674 673
675 674 # user may have over-ridden the default print hook:
676 675 try:
677 676 self.outputcache.__class__.display = self.hooks.display
678 677 except AttributeError:
679 678 pass
680 679
681 680 # I don't like assigning globally to sys, because it means when
682 681 # embedding instances, each embedded instance overrides the previous
683 682 # choice. But sys.displayhook seems to be called internally by exec,
684 683 # so I don't see a way around it. We first save the original and then
685 684 # overwrite it.
686 685 self.sys_displayhook = sys.displayhook
687 686 sys.displayhook = self.outputcache
688 687
689 688 # Do a proper resetting of doctest, including the necessary displayhook
690 689 # monkeypatching
691 690 try:
692 691 doctest_reload()
693 692 except ImportError:
694 693 warn("doctest module does not exist.")
695 694
696 695 # Set user colors (don't do it in the constructor above so that it
697 696 # doesn't crash if colors option is invalid)
698 697 self.magic_colors(rc.colors)
699 698
700 699 # Set calling of pdb on exceptions
701 700 self.call_pdb = rc.pdb
702 701
703 702 # Load user aliases
704 703 for alias in rc.alias:
705 704 self.magic_alias(alias)
706 705
707 706 self.hooks.late_startup_hook()
708 707
709 708 for cmd in self.rc.autoexec:
710 709 #print "autoexec>",cmd #dbg
711 710 self.api.runlines(cmd)
712 711
713 712 batchrun = False
714 713 for batchfile in [path(arg) for arg in self.rc.args
715 714 if arg.lower().endswith('.ipy')]:
716 715 if not batchfile.isfile():
717 716 print "No such batch file:", batchfile
718 717 continue
719 718 self.api.runlines(batchfile.text())
720 719 batchrun = True
721 720 # without -i option, exit after running the batch file
722 721 if batchrun and not self.rc.interact:
723 722 self.ask_exit()
724 723
725 724 def add_builtins(self):
726 725 """Store ipython references into the builtin namespace.
727 726
728 727 Some parts of ipython operate via builtins injected here, which hold a
729 728 reference to IPython itself."""
730 729
731 730 # TODO: deprecate all of these, they are unsafe
732 731 builtins_new = dict(__IPYTHON__ = self,
733 732 ip_set_hook = self.set_hook,
734 733 jobs = self.jobs,
735 734 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
736 735 ipalias = wrap_deprecated(self.ipalias),
737 736 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
738 737 #_ip = self.api
739 738 )
740 739 for biname,bival in builtins_new.items():
741 740 try:
742 741 # store the orignal value so we can restore it
743 742 self.builtins_added[biname] = __builtin__.__dict__[biname]
744 743 except KeyError:
745 744 # or mark that it wasn't defined, and we'll just delete it at
746 745 # cleanup
747 746 self.builtins_added[biname] = Undefined
748 747 __builtin__.__dict__[biname] = bival
749 748
750 749 # Keep in the builtins a flag for when IPython is active. We set it
751 750 # with setdefault so that multiple nested IPythons don't clobber one
752 751 # another. Each will increase its value by one upon being activated,
753 752 # which also gives us a way to determine the nesting level.
754 753 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
755 754
756 755 def clean_builtins(self):
757 756 """Remove any builtins which might have been added by add_builtins, or
758 757 restore overwritten ones to their previous values."""
759 758 for biname,bival in self.builtins_added.items():
760 759 if bival is Undefined:
761 760 del __builtin__.__dict__[biname]
762 761 else:
763 762 __builtin__.__dict__[biname] = bival
764 763 self.builtins_added.clear()
765 764
766 765 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
767 766 """set_hook(name,hook) -> sets an internal IPython hook.
768 767
769 768 IPython exposes some of its internal API as user-modifiable hooks. By
770 769 adding your function to one of these hooks, you can modify IPython's
771 770 behavior to call at runtime your own routines."""
772 771
773 772 # At some point in the future, this should validate the hook before it
774 773 # accepts it. Probably at least check that the hook takes the number
775 774 # of args it's supposed to.
776 775
777 776 f = new.instancemethod(hook,self,self.__class__)
778 777
779 778 # check if the hook is for strdispatcher first
780 779 if str_key is not None:
781 780 sdp = self.strdispatchers.get(name, StrDispatch())
782 781 sdp.add_s(str_key, f, priority )
783 782 self.strdispatchers[name] = sdp
784 783 return
785 784 if re_key is not None:
786 785 sdp = self.strdispatchers.get(name, StrDispatch())
787 786 sdp.add_re(re.compile(re_key), f, priority )
788 787 self.strdispatchers[name] = sdp
789 788 return
790 789
791 790 dp = getattr(self.hooks, name, None)
792 791 if name not in IPython.hooks.__all__:
793 792 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
794 793 if not dp:
795 794 dp = IPython.hooks.CommandChainDispatcher()
796 795
797 796 try:
798 797 dp.add(f,priority)
799 798 except AttributeError:
800 799 # it was not commandchain, plain old func - replace
801 800 dp = f
802 801
803 802 setattr(self.hooks,name, dp)
804 803
805 804
806 805 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
807 806
808 807 def set_crash_handler(self,crashHandler):
809 808 """Set the IPython crash handler.
810 809
811 810 This must be a callable with a signature suitable for use as
812 811 sys.excepthook."""
813 812
814 813 # Install the given crash handler as the Python exception hook
815 814 sys.excepthook = crashHandler
816 815
817 816 # The instance will store a pointer to this, so that runtime code
818 817 # (such as magics) can access it. This is because during the
819 818 # read-eval loop, it gets temporarily overwritten (to deal with GUI
820 819 # frameworks).
821 820 self.sys_excepthook = sys.excepthook
822 821
823 822
824 823 def set_custom_exc(self,exc_tuple,handler):
825 824 """set_custom_exc(exc_tuple,handler)
826 825
827 826 Set a custom exception handler, which will be called if any of the
828 827 exceptions in exc_tuple occur in the mainloop (specifically, in the
829 828 runcode() method.
830 829
831 830 Inputs:
832 831
833 832 - exc_tuple: a *tuple* of valid exceptions to call the defined
834 833 handler for. It is very important that you use a tuple, and NOT A
835 834 LIST here, because of the way Python's except statement works. If
836 835 you only want to trap a single exception, use a singleton tuple:
837 836
838 837 exc_tuple == (MyCustomException,)
839 838
840 839 - handler: this must be defined as a function with the following
841 840 basic interface: def my_handler(self,etype,value,tb).
842 841
843 842 This will be made into an instance method (via new.instancemethod)
844 843 of IPython itself, and it will be called if any of the exceptions
845 844 listed in the exc_tuple are caught. If the handler is None, an
846 845 internal basic one is used, which just prints basic info.
847 846
848 847 WARNING: by putting in your own exception handler into IPython's main
849 848 execution loop, you run a very good chance of nasty crashes. This
850 849 facility should only be used if you really know what you are doing."""
851 850
852 851 assert type(exc_tuple)==type(()) , \
853 852 "The custom exceptions must be given AS A TUPLE."
854 853
855 854 def dummy_handler(self,etype,value,tb):
856 855 print '*** Simple custom exception handler ***'
857 856 print 'Exception type :',etype
858 857 print 'Exception value:',value
859 858 print 'Traceback :',tb
860 859 print 'Source code :','\n'.join(self.buffer)
861 860
862 861 if handler is None: handler = dummy_handler
863 862
864 863 self.CustomTB = new.instancemethod(handler,self,self.__class__)
865 864 self.custom_exceptions = exc_tuple
866 865
867 866 def set_custom_completer(self,completer,pos=0):
868 867 """set_custom_completer(completer,pos=0)
869 868
870 869 Adds a new custom completer function.
871 870
872 871 The position argument (defaults to 0) is the index in the completers
873 872 list where you want the completer to be inserted."""
874 873
875 874 newcomp = new.instancemethod(completer,self.Completer,
876 875 self.Completer.__class__)
877 876 self.Completer.matchers.insert(pos,newcomp)
878 877
879 878 def set_completer(self):
880 879 """reset readline's completer to be our own."""
881 880 self.readline.set_completer(self.Completer.complete)
882 881
883 882 def _get_call_pdb(self):
884 883 return self._call_pdb
885 884
886 885 def _set_call_pdb(self,val):
887 886
888 887 if val not in (0,1,False,True):
889 888 raise ValueError,'new call_pdb value must be boolean'
890 889
891 890 # store value in instance
892 891 self._call_pdb = val
893 892
894 893 # notify the actual exception handlers
895 894 self.InteractiveTB.call_pdb = val
896 895 if self.isthreaded:
897 896 try:
898 897 self.sys_excepthook.call_pdb = val
899 898 except:
900 899 warn('Failed to activate pdb for threaded exception handler')
901 900
902 901 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
903 902 'Control auto-activation of pdb at exceptions')
904 903
905
906 904 # These special functions get installed in the builtin namespace, to
907 905 # provide programmatic (pure python) access to magics, aliases and system
908 906 # calls. This is important for logging, user scripting, and more.
909 907
910 908 # We are basically exposing, via normal python functions, the three
911 909 # mechanisms in which ipython offers special call modes (magics for
912 910 # internal control, aliases for direct system access via pre-selected
913 911 # names, and !cmd for calling arbitrary system commands).
914 912
915 913 def ipmagic(self,arg_s):
916 914 """Call a magic function by name.
917 915
918 916 Input: a string containing the name of the magic function to call and any
919 917 additional arguments to be passed to the magic.
920 918
921 919 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
922 920 prompt:
923 921
924 922 In[1]: %name -opt foo bar
925 923
926 924 To call a magic without arguments, simply use ipmagic('name').
927 925
928 926 This provides a proper Python function to call IPython's magics in any
929 927 valid Python code you can type at the interpreter, including loops and
930 928 compound statements. It is added by IPython to the Python builtin
931 929 namespace upon initialization."""
932 930
933 931 args = arg_s.split(' ',1)
934 932 magic_name = args[0]
935 933 magic_name = magic_name.lstrip(self.ESC_MAGIC)
936 934
937 935 try:
938 936 magic_args = args[1]
939 937 except IndexError:
940 938 magic_args = ''
941 939 fn = getattr(self,'magic_'+magic_name,None)
942 940 if fn is None:
943 941 error("Magic function `%s` not found." % magic_name)
944 942 else:
945 943 magic_args = self.var_expand(magic_args,1)
946 944 return fn(magic_args)
947 945
948 946 def ipalias(self,arg_s):
949 947 """Call an alias by name.
950 948
951 949 Input: a string containing the name of the alias to call and any
952 950 additional arguments to be passed to the magic.
953 951
954 952 ipalias('name -opt foo bar') is equivalent to typing at the ipython
955 953 prompt:
956 954
957 955 In[1]: name -opt foo bar
958 956
959 957 To call an alias without arguments, simply use ipalias('name').
960 958
961 959 This provides a proper Python function to call IPython's aliases in any
962 960 valid Python code you can type at the interpreter, including loops and
963 961 compound statements. It is added by IPython to the Python builtin
964 962 namespace upon initialization."""
965 963
966 964 args = arg_s.split(' ',1)
967 965 alias_name = args[0]
968 966 try:
969 967 alias_args = args[1]
970 968 except IndexError:
971 969 alias_args = ''
972 970 if alias_name in self.alias_table:
973 971 self.call_alias(alias_name,alias_args)
974 972 else:
975 973 error("Alias `%s` not found." % alias_name)
976 974
977 975 def ipsystem(self,arg_s):
978 976 """Make a system call, using IPython."""
979 977
980 978 self.system(arg_s)
981 979
982 980 def complete(self,text):
983 981 """Return a sorted list of all possible completions on text.
984 982
985 983 Inputs:
986 984
987 985 - text: a string of text to be completed on.
988 986
989 987 This is a wrapper around the completion mechanism, similar to what
990 988 readline does at the command line when the TAB key is hit. By
991 989 exposing it as a method, it can be used by other non-readline
992 990 environments (such as GUIs) for text completion.
993 991
994 992 Simple usage example:
995 993
996 994 In [7]: x = 'hello'
997 995
998 996 In [8]: x
999 997 Out[8]: 'hello'
1000 998
1001 999 In [9]: print x
1002 1000 hello
1003 1001
1004 1002 In [10]: _ip.IP.complete('x.l')
1005 1003 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1006 1004 """
1007 1005
1008 1006 complete = self.Completer.complete
1009 1007 state = 0
1010 1008 # use a dict so we get unique keys, since ipyhton's multiple
1011 1009 # completers can return duplicates. When we make 2.4 a requirement,
1012 1010 # start using sets instead, which are faster.
1013 1011 comps = {}
1014 1012 while True:
1015 1013 newcomp = complete(text,state,line_buffer=text)
1016 1014 if newcomp is None:
1017 1015 break
1018 1016 comps[newcomp] = 1
1019 1017 state += 1
1020 1018 outcomps = comps.keys()
1021 1019 outcomps.sort()
1022 1020 #print "T:",text,"OC:",outcomps # dbg
1023 1021 #print "vars:",self.user_ns.keys()
1024 1022 return outcomps
1025 1023
1026 1024 def set_completer_frame(self, frame=None):
1027 1025 if frame:
1028 1026 self.Completer.namespace = frame.f_locals
1029 1027 self.Completer.global_namespace = frame.f_globals
1030 1028 else:
1031 1029 self.Completer.namespace = self.user_ns
1032 1030 self.Completer.global_namespace = self.user_global_ns
1033 1031
1034 1032 def init_auto_alias(self):
1035 1033 """Define some aliases automatically.
1036 1034
1037 1035 These are ALL parameter-less aliases"""
1038 1036
1039 1037 for alias,cmd in self.auto_alias:
1040 1038 self.getapi().defalias(alias,cmd)
1041 1039
1042 1040
1043 1041 def alias_table_validate(self,verbose=0):
1044 1042 """Update information about the alias table.
1045 1043
1046 1044 In particular, make sure no Python keywords/builtins are in it."""
1047 1045
1048 1046 no_alias = self.no_alias
1049 1047 for k in self.alias_table.keys():
1050 1048 if k in no_alias:
1051 1049 del self.alias_table[k]
1052 1050 if verbose:
1053 1051 print ("Deleting alias <%s>, it's a Python "
1054 1052 "keyword or builtin." % k)
1055 1053
1056 1054 def set_autoindent(self,value=None):
1057 1055 """Set the autoindent flag, checking for readline support.
1058 1056
1059 1057 If called with no arguments, it acts as a toggle."""
1060 1058
1061 1059 if not self.has_readline:
1062 1060 if os.name == 'posix':
1063 1061 warn("The auto-indent feature requires the readline library")
1064 1062 self.autoindent = 0
1065 1063 return
1066 1064 if value is None:
1067 1065 self.autoindent = not self.autoindent
1068 1066 else:
1069 1067 self.autoindent = value
1070 1068
1071 1069 def rc_set_toggle(self,rc_field,value=None):
1072 1070 """Set or toggle a field in IPython's rc config. structure.
1073 1071
1074 1072 If called with no arguments, it acts as a toggle.
1075 1073
1076 1074 If called with a non-existent field, the resulting AttributeError
1077 1075 exception will propagate out."""
1078 1076
1079 1077 rc_val = getattr(self.rc,rc_field)
1080 1078 if value is None:
1081 1079 value = not rc_val
1082 1080 setattr(self.rc,rc_field,value)
1083 1081
1084 1082 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1085 1083 """Install the user configuration directory.
1086 1084
1087 1085 Can be called when running for the first time or to upgrade the user's
1088 1086 .ipython/ directory with the mode parameter. Valid modes are 'install'
1089 1087 and 'upgrade'."""
1090 1088
1091 1089 def wait():
1092 1090 try:
1093 1091 raw_input("Please press <RETURN> to start IPython.")
1094 1092 except EOFError:
1095 1093 print >> Term.cout
1096 1094 print '*'*70
1097 1095
1098 1096 cwd = os.getcwd() # remember where we started
1099 1097 glb = glob.glob
1100 1098 print '*'*70
1101 1099 if mode == 'install':
1102 1100 print \
1103 1101 """Welcome to IPython. I will try to create a personal configuration directory
1104 1102 where you can customize many aspects of IPython's functionality in:\n"""
1105 1103 else:
1106 1104 print 'I am going to upgrade your configuration in:'
1107 1105
1108 1106 print ipythondir
1109 1107
1110 1108 rcdirend = os.path.join('IPython','UserConfig')
1111 1109 cfg = lambda d: os.path.join(d,rcdirend)
1112 1110 try:
1113 1111 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1114 1112 print "Initializing from configuration",rcdir
1115 1113 except IndexError:
1116 1114 warning = """
1117 1115 Installation error. IPython's directory was not found.
1118 1116
1119 1117 Check the following:
1120 1118
1121 1119 The ipython/IPython directory should be in a directory belonging to your
1122 1120 PYTHONPATH environment variable (that is, it should be in a directory
1123 1121 belonging to sys.path). You can copy it explicitly there or just link to it.
1124 1122
1125 1123 IPython will create a minimal default configuration for you.
1126 1124
1127 1125 """
1128 1126 warn(warning)
1129 1127 wait()
1130 1128
1131 1129 if sys.platform =='win32':
1132 1130 inif = 'ipythonrc.ini'
1133 1131 else:
1134 1132 inif = 'ipythonrc'
1135 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' }
1133 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults',
1134 inif : '# intentionally left blank' }
1136 1135 os.makedirs(ipythondir, mode = 0777)
1137 1136 for f, cont in minimal_setup.items():
1138 1137 open(ipythondir + '/' + f,'w').write(cont)
1139 1138
1140 1139 return
1141 1140
1142 1141 if mode == 'install':
1143 1142 try:
1144 1143 shutil.copytree(rcdir,ipythondir)
1145 1144 os.chdir(ipythondir)
1146 1145 rc_files = glb("ipythonrc*")
1147 1146 for rc_file in rc_files:
1148 1147 os.rename(rc_file,rc_file+rc_suffix)
1149 1148 except:
1150 1149 warning = """
1151 1150
1152 1151 There was a problem with the installation:
1153 1152 %s
1154 1153 Try to correct it or contact the developers if you think it's a bug.
1155 1154 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1156 1155 warn(warning)
1157 1156 wait()
1158 1157 return
1159 1158
1160 1159 elif mode == 'upgrade':
1161 1160 try:
1162 1161 os.chdir(ipythondir)
1163 1162 except:
1164 1163 print """
1165 1164 Can not upgrade: changing to directory %s failed. Details:
1166 1165 %s
1167 1166 """ % (ipythondir,sys.exc_info()[1])
1168 1167 wait()
1169 1168 return
1170 1169 else:
1171 1170 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1172 1171 for new_full_path in sources:
1173 1172 new_filename = os.path.basename(new_full_path)
1174 1173 if new_filename.startswith('ipythonrc'):
1175 1174 new_filename = new_filename + rc_suffix
1176 1175 # The config directory should only contain files, skip any
1177 1176 # directories which may be there (like CVS)
1178 1177 if os.path.isdir(new_full_path):
1179 1178 continue
1180 1179 if os.path.exists(new_filename):
1181 1180 old_file = new_filename+'.old'
1182 1181 if os.path.exists(old_file):
1183 1182 os.remove(old_file)
1184 1183 os.rename(new_filename,old_file)
1185 1184 shutil.copy(new_full_path,new_filename)
1186 1185 else:
1187 1186 raise ValueError,'unrecognized mode for install:',`mode`
1188 1187
1189 1188 # Fix line-endings to those native to each platform in the config
1190 1189 # directory.
1191 1190 try:
1192 1191 os.chdir(ipythondir)
1193 1192 except:
1194 1193 print """
1195 1194 Problem: changing to directory %s failed.
1196 1195 Details:
1197 1196 %s
1198 1197
1199 1198 Some configuration files may have incorrect line endings. This should not
1200 1199 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1201 1200 wait()
1202 1201 else:
1203 1202 for fname in glb('ipythonrc*'):
1204 1203 try:
1205 1204 native_line_ends(fname,backup=0)
1206 1205 except IOError:
1207 1206 pass
1208 1207
1209 1208 if mode == 'install':
1210 1209 print """
1211 1210 Successful installation!
1212 1211
1213 1212 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1214 1213 IPython manual (there are both HTML and PDF versions supplied with the
1215 1214 distribution) to make sure that your system environment is properly configured
1216 1215 to take advantage of IPython's features.
1217 1216
1218 1217 Important note: the configuration system has changed! The old system is
1219 1218 still in place, but its setting may be partly overridden by the settings in
1220 1219 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1221 1220 if some of the new settings bother you.
1222 1221
1223 1222 """
1224 1223 else:
1225 1224 print """
1226 1225 Successful upgrade!
1227 1226
1228 1227 All files in your directory:
1229 1228 %(ipythondir)s
1230 1229 which would have been overwritten by the upgrade were backed up with a .old
1231 1230 extension. If you had made particular customizations in those files you may
1232 1231 want to merge them back into the new files.""" % locals()
1233 1232 wait()
1234 1233 os.chdir(cwd)
1235 1234 # end user_setup()
1236 1235
1237 1236 def atexit_operations(self):
1238 1237 """This will be executed at the time of exit.
1239 1238
1240 1239 Saving of persistent data should be performed here. """
1241 1240
1242 1241 #print '*** IPython exit cleanup ***' # dbg
1243 1242 # input history
1244 1243 self.savehist()
1245 1244
1246 1245 # Cleanup all tempfiles left around
1247 1246 for tfile in self.tempfiles:
1248 1247 try:
1249 1248 os.unlink(tfile)
1250 1249 except OSError:
1251 1250 pass
1252 1251
1253 1252 self.hooks.shutdown_hook()
1254 1253
1255 1254 def savehist(self):
1256 1255 """Save input history to a file (via readline library)."""
1257 1256
1258 1257 if not self.has_readline:
1259 1258 return
1260 1259
1261 1260 try:
1262 1261 self.readline.write_history_file(self.histfile)
1263 1262 except:
1264 1263 print 'Unable to save IPython command history to file: ' + \
1265 1264 `self.histfile`
1266 1265
1267 1266 def reloadhist(self):
1268 1267 """Reload the input history from disk file."""
1269 1268
1270 1269 if self.has_readline:
1271 1270 try:
1272 1271 self.readline.clear_history()
1273 1272 self.readline.read_history_file(self.shell.histfile)
1274 1273 except AttributeError:
1275 1274 pass
1276 1275
1277 1276
1278 1277 def history_saving_wrapper(self, func):
1279 1278 """ Wrap func for readline history saving
1280 1279
1281 1280 Convert func into callable that saves & restores
1282 1281 history around the call """
1283 1282
1284 1283 if not self.has_readline:
1285 1284 return func
1286 1285
1287 1286 def wrapper():
1288 1287 self.savehist()
1289 1288 try:
1290 1289 func()
1291 1290 finally:
1292 1291 readline.read_history_file(self.histfile)
1293 1292 return wrapper
1294
1295 1293
1296 1294 def pre_readline(self):
1297 1295 """readline hook to be used at the start of each line.
1298 1296
1299 1297 Currently it handles auto-indent only."""
1300 1298
1301 1299 #debugx('self.indent_current_nsp','pre_readline:')
1302 1300
1303 1301 if self.rl_do_indent:
1304 1302 self.readline.insert_text(self.indent_current_str())
1305 1303 if self.rl_next_input is not None:
1306 1304 self.readline.insert_text(self.rl_next_input)
1307 1305 self.rl_next_input = None
1308 1306
1309 1307 def init_readline(self):
1310 1308 """Command history completion/saving/reloading."""
1311 1309
1312 1310
1313 1311 import IPython.rlineimpl as readline
1314 1312
1315 1313 if not readline.have_readline:
1316 1314 self.has_readline = 0
1317 1315 self.readline = None
1318 1316 # no point in bugging windows users with this every time:
1319 1317 warn('Readline services not available on this platform.')
1320 1318 else:
1321 1319 sys.modules['readline'] = readline
1322 1320 import atexit
1323 1321 from IPython.completer import IPCompleter
1324 1322 self.Completer = IPCompleter(self,
1325 1323 self.user_ns,
1326 1324 self.user_global_ns,
1327 1325 self.rc.readline_omit__names,
1328 1326 self.alias_table)
1329 1327 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1330 1328 self.strdispatchers['complete_command'] = sdisp
1331 1329 self.Completer.custom_completers = sdisp
1332 1330 # Platform-specific configuration
1333 1331 if os.name == 'nt':
1334 1332 self.readline_startup_hook = readline.set_pre_input_hook
1335 1333 else:
1336 1334 self.readline_startup_hook = readline.set_startup_hook
1337 1335
1338 1336 # Load user's initrc file (readline config)
1339 1337 # Or if libedit is used, load editrc.
1340 1338 inputrc_name = os.environ.get('INPUTRC')
1341 1339 if inputrc_name is None:
1342 1340 home_dir = get_home_dir()
1343 1341 if home_dir is not None:
1344 1342 inputrc_name = '.inputrc'
1345 1343 if readline.uses_libedit:
1346 1344 inputrc_name = '.editrc'
1347 1345 inputrc_name = os.path.join(home_dir, inputrc_name)
1348 1346 if os.path.isfile(inputrc_name):
1349 1347 try:
1350 1348 readline.read_init_file(inputrc_name)
1351 1349 except:
1352 1350 warn('Problems reading readline initialization file <%s>'
1353 1351 % inputrc_name)
1354 1352
1355 1353 self.has_readline = 1
1356 1354 self.readline = readline
1357 1355 # save this in sys so embedded copies can restore it properly
1358 1356 sys.ipcompleter = self.Completer.complete
1359 1357 self.set_completer()
1360 1358
1361 1359 # Configure readline according to user's prefs
1362 1360 # This is only done if GNU readline is being used. If libedit
1363 1361 # is being used (as on Leopard) the readline config is
1364 1362 # not run as the syntax for libedit is different.
1365 1363 if not readline.uses_libedit:
1366 1364 for rlcommand in self.rc.readline_parse_and_bind:
1367 1365 readline.parse_and_bind(rlcommand)
1368 1366
1369 1367 # remove some chars from the delimiters list
1370 1368 delims = readline.get_completer_delims()
1371 1369 delims = delims.translate(string._idmap,
1372 1370 self.rc.readline_remove_delims)
1373 1371 readline.set_completer_delims(delims)
1374 1372 # otherwise we end up with a monster history after a while:
1375 1373 readline.set_history_length(1000)
1376 1374 try:
1377 1375 #print '*** Reading readline history' # dbg
1378 1376 readline.read_history_file(self.histfile)
1379 1377 except IOError:
1380 1378 pass # It doesn't exist yet.
1381 1379
1382 1380 atexit.register(self.atexit_operations)
1383 1381 del atexit
1384 1382
1385 1383 # Configure auto-indent for all platforms
1386 1384 self.set_autoindent(self.rc.autoindent)
1387 1385
1388 1386 def ask_yes_no(self,prompt,default=True):
1389 1387 if self.rc.quiet:
1390 1388 return True
1391 1389 return ask_yes_no(prompt,default)
1392
1390
1391 def cache_main_mod(self,mod):
1392 """Cache a main module.
1393
1394 When scripts are executed via %run, we must keep a reference to their
1395 __main__ module (a FakeModule instance) around so that Python doesn't
1396 clear it, rendering objects defined therein useless.
1397
1398 This method keeps said reference in a private dict, keyed by the
1399 absolute path of the module object (which corresponds to the script
1400 path). This way, for multiple executions of the same script we only
1401 keep one copy of __main__ (the last one), thus preventing memory leaks
1402 from old references while allowing the objects from the last execution
1403 to be accessible.
1404
1405 Parameters
1406 ----------
1407 mod : a module object
1408
1409 Examples
1410 --------
1411
1412 In [10]: import IPython
1413
1414 In [11]: _ip.IP.cache_main_mod(IPython)
1415
1416 In [12]: IPython.__file__ in _ip.IP._user_main_modules
1417 Out[12]: True
1418 """
1419 self._user_main_modules[os.path.abspath(mod.__file__) ] = mod
1420
1421 def clear_main_mod_cache(self):
1422 """Clear the cache of main modules.
1423
1424 Mainly for use by utilities like %reset.
1425
1426 Examples
1427 --------
1428
1429 In [15]: import IPython
1430
1431 In [16]: _ip.IP.cache_main_mod(IPython)
1432
1433 In [17]: len(_ip.IP._user_main_modules) > 0
1434 Out[17]: True
1435
1436 In [18]: _ip.IP.clear_main_mod_cache()
1437
1438 In [19]: len(_ip.IP._user_main_modules) == 0
1439 Out[19]: True
1440 """
1441 self._user_main_modules.clear()
1442
1393 1443 def _should_recompile(self,e):
1394 1444 """Utility routine for edit_syntax_error"""
1395 1445
1396 1446 if e.filename in ('<ipython console>','<input>','<string>',
1397 1447 '<console>','<BackgroundJob compilation>',
1398 1448 None):
1399 1449
1400 1450 return False
1401 1451 try:
1402 1452 if (self.rc.autoedit_syntax and
1403 1453 not self.ask_yes_no('Return to editor to correct syntax error? '
1404 1454 '[Y/n] ','y')):
1405 1455 return False
1406 1456 except EOFError:
1407 1457 return False
1408 1458
1409 1459 def int0(x):
1410 1460 try:
1411 1461 return int(x)
1412 1462 except TypeError:
1413 1463 return 0
1414 1464 # always pass integer line and offset values to editor hook
1415 1465 self.hooks.fix_error_editor(e.filename,
1416 1466 int0(e.lineno),int0(e.offset),e.msg)
1417 1467 return True
1418 1468
1419 1469 def edit_syntax_error(self):
1420 1470 """The bottom half of the syntax error handler called in the main loop.
1421 1471
1422 1472 Loop until syntax error is fixed or user cancels.
1423 1473 """
1424 1474
1425 1475 while self.SyntaxTB.last_syntax_error:
1426 1476 # copy and clear last_syntax_error
1427 1477 err = self.SyntaxTB.clear_err_state()
1428 1478 if not self._should_recompile(err):
1429 1479 return
1430 1480 try:
1431 1481 # may set last_syntax_error again if a SyntaxError is raised
1432 1482 self.safe_execfile(err.filename,self.user_ns)
1433 1483 except:
1434 1484 self.showtraceback()
1435 1485 else:
1436 1486 try:
1437 1487 f = file(err.filename)
1438 1488 try:
1439 1489 sys.displayhook(f.read())
1440 1490 finally:
1441 1491 f.close()
1442 1492 except:
1443 1493 self.showtraceback()
1444 1494
1445 1495 def showsyntaxerror(self, filename=None):
1446 1496 """Display the syntax error that just occurred.
1447 1497
1448 1498 This doesn't display a stack trace because there isn't one.
1449 1499
1450 1500 If a filename is given, it is stuffed in the exception instead
1451 1501 of what was there before (because Python's parser always uses
1452 1502 "<string>" when reading from a string).
1453 1503 """
1454 1504 etype, value, last_traceback = sys.exc_info()
1455 1505
1456 1506 # See note about these variables in showtraceback() below
1457 1507 sys.last_type = etype
1458 1508 sys.last_value = value
1459 1509 sys.last_traceback = last_traceback
1460 1510
1461 1511 if filename and etype is SyntaxError:
1462 1512 # Work hard to stuff the correct filename in the exception
1463 1513 try:
1464 1514 msg, (dummy_filename, lineno, offset, line) = value
1465 1515 except:
1466 1516 # Not the format we expect; leave it alone
1467 1517 pass
1468 1518 else:
1469 1519 # Stuff in the right filename
1470 1520 try:
1471 1521 # Assume SyntaxError is a class exception
1472 1522 value = SyntaxError(msg, (filename, lineno, offset, line))
1473 1523 except:
1474 1524 # If that failed, assume SyntaxError is a string
1475 1525 value = msg, (filename, lineno, offset, line)
1476 1526 self.SyntaxTB(etype,value,[])
1477 1527
1478 1528 def debugger(self,force=False):
1479 1529 """Call the pydb/pdb debugger.
1480 1530
1481 1531 Keywords:
1482 1532
1483 1533 - force(False): by default, this routine checks the instance call_pdb
1484 1534 flag and does not actually invoke the debugger if the flag is false.
1485 1535 The 'force' option forces the debugger to activate even if the flag
1486 1536 is false.
1487 1537 """
1488 1538
1489 1539 if not (force or self.call_pdb):
1490 1540 return
1491 1541
1492 1542 if not hasattr(sys,'last_traceback'):
1493 1543 error('No traceback has been produced, nothing to debug.')
1494 1544 return
1495 1545
1496 1546 # use pydb if available
1497 1547 if Debugger.has_pydb:
1498 1548 from pydb import pm
1499 1549 else:
1500 1550 # fallback to our internal debugger
1501 1551 pm = lambda : self.InteractiveTB.debugger(force=True)
1502 1552 self.history_saving_wrapper(pm)()
1503 1553
1504 1554 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1505 1555 """Display the exception that just occurred.
1506 1556
1507 1557 If nothing is known about the exception, this is the method which
1508 1558 should be used throughout the code for presenting user tracebacks,
1509 1559 rather than directly invoking the InteractiveTB object.
1510 1560
1511 1561 A specific showsyntaxerror() also exists, but this method can take
1512 1562 care of calling it if needed, so unless you are explicitly catching a
1513 1563 SyntaxError exception, don't try to analyze the stack manually and
1514 1564 simply call this method."""
1515 1565
1516 1566
1517 1567 # Though this won't be called by syntax errors in the input line,
1518 1568 # there may be SyntaxError cases whith imported code.
1519 1569
1520 1570 try:
1521 1571 if exc_tuple is None:
1522 1572 etype, value, tb = sys.exc_info()
1523 1573 else:
1524 1574 etype, value, tb = exc_tuple
1525 1575
1526 1576 if etype is SyntaxError:
1527 1577 self.showsyntaxerror(filename)
1528 1578 elif etype is IPython.ipapi.UsageError:
1529 1579 print "UsageError:", value
1530 1580 else:
1531 1581 # WARNING: these variables are somewhat deprecated and not
1532 1582 # necessarily safe to use in a threaded environment, but tools
1533 1583 # like pdb depend on their existence, so let's set them. If we
1534 1584 # find problems in the field, we'll need to revisit their use.
1535 1585 sys.last_type = etype
1536 1586 sys.last_value = value
1537 1587 sys.last_traceback = tb
1538 1588
1539 1589 if etype in self.custom_exceptions:
1540 1590 self.CustomTB(etype,value,tb)
1541 1591 else:
1542 1592 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1543 1593 if self.InteractiveTB.call_pdb and self.has_readline:
1544 1594 # pdb mucks up readline, fix it back
1545 1595 self.set_completer()
1546 1596 except KeyboardInterrupt:
1547 1597 self.write("\nKeyboardInterrupt\n")
1548
1549
1550 1598
1551 1599 def mainloop(self,banner=None):
1552 1600 """Creates the local namespace and starts the mainloop.
1553 1601
1554 1602 If an optional banner argument is given, it will override the
1555 1603 internally created default banner."""
1556 1604
1557 1605 if self.rc.c: # Emulate Python's -c option
1558 1606 self.exec_init_cmd()
1559 1607 if banner is None:
1560 1608 if not self.rc.banner:
1561 1609 banner = ''
1562 1610 # banner is string? Use it directly!
1563 1611 elif isinstance(self.rc.banner,basestring):
1564 1612 banner = self.rc.banner
1565 1613 else:
1566 1614 banner = self.BANNER+self.banner2
1567 1615
1568 1616 while 1:
1569 1617 try:
1570 1618 self.interact(banner)
1571 1619 #self.interact_with_readline()
1572 # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above
1620
1621 # XXX for testing of a readline-decoupled repl loop, call
1622 # interact_with_readline above
1573 1623
1574 1624 break
1575 1625 except KeyboardInterrupt:
1576 1626 # this should not be necessary, but KeyboardInterrupt
1577 1627 # handling seems rather unpredictable...
1578 1628 self.write("\nKeyboardInterrupt in interact()\n")
1579 1629
1580 1630 def exec_init_cmd(self):
1581 1631 """Execute a command given at the command line.
1582 1632
1583 1633 This emulates Python's -c option."""
1584 1634
1585 1635 #sys.argv = ['-c']
1586 1636 self.push(self.prefilter(self.rc.c, False))
1587 1637 if not self.rc.interact:
1588 1638 self.ask_exit()
1589 1639
1590 1640 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1591 1641 """Embeds IPython into a running python program.
1592 1642
1593 1643 Input:
1594 1644
1595 1645 - header: An optional header message can be specified.
1596 1646
1597 1647 - local_ns, global_ns: working namespaces. If given as None, the
1598 1648 IPython-initialized one is updated with __main__.__dict__, so that
1599 1649 program variables become visible but user-specific configuration
1600 1650 remains possible.
1601 1651
1602 1652 - stack_depth: specifies how many levels in the stack to go to
1603 1653 looking for namespaces (when local_ns and global_ns are None). This
1604 1654 allows an intermediate caller to make sure that this function gets
1605 1655 the namespace from the intended level in the stack. By default (0)
1606 1656 it will get its locals and globals from the immediate caller.
1607 1657
1608 1658 Warning: it's possible to use this in a program which is being run by
1609 1659 IPython itself (via %run), but some funny things will happen (a few
1610 1660 globals get overwritten). In the future this will be cleaned up, as
1611 1661 there is no fundamental reason why it can't work perfectly."""
1612 1662
1613 1663 # Get locals and globals from caller
1614 1664 if local_ns is None or global_ns is None:
1615 1665 call_frame = sys._getframe(stack_depth).f_back
1616 1666
1617 1667 if local_ns is None:
1618 1668 local_ns = call_frame.f_locals
1619 1669 if global_ns is None:
1620 1670 global_ns = call_frame.f_globals
1621 1671
1622 1672 # Update namespaces and fire up interpreter
1623 1673
1624 1674 # The global one is easy, we can just throw it in
1625 1675 self.user_global_ns = global_ns
1626 1676
1627 1677 # but the user/local one is tricky: ipython needs it to store internal
1628 1678 # data, but we also need the locals. We'll copy locals in the user
1629 1679 # one, but will track what got copied so we can delete them at exit.
1630 1680 # This is so that a later embedded call doesn't see locals from a
1631 1681 # previous call (which most likely existed in a separate scope).
1632 1682 local_varnames = local_ns.keys()
1633 1683 self.user_ns.update(local_ns)
1634 1684 #self.user_ns['local_ns'] = local_ns # dbg
1635 1685
1636 1686 # Patch for global embedding to make sure that things don't overwrite
1637 1687 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1638 1688 # FIXME. Test this a bit more carefully (the if.. is new)
1639 1689 if local_ns is None and global_ns is None:
1640 1690 self.user_global_ns.update(__main__.__dict__)
1641 1691
1642 1692 # make sure the tab-completer has the correct frame information, so it
1643 1693 # actually completes using the frame's locals/globals
1644 1694 self.set_completer_frame()
1645 1695
1646 1696 # before activating the interactive mode, we need to make sure that
1647 1697 # all names in the builtin namespace needed by ipython point to
1648 1698 # ourselves, and not to other instances.
1649 1699 self.add_builtins()
1650 1700
1651 1701 self.interact(header)
1652 1702
1653 1703 # now, purge out the user namespace from anything we might have added
1654 1704 # from the caller's local namespace
1655 1705 delvar = self.user_ns.pop
1656 1706 for var in local_varnames:
1657 1707 delvar(var,None)
1658 1708 # and clean builtins we may have overridden
1659 1709 self.clean_builtins()
1660 1710
1661 1711 def interact_prompt(self):
1662 1712 """ Print the prompt (in read-eval-print loop)
1663 1713
1664 1714 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1665 1715 used in standard IPython flow.
1666 1716 """
1667 1717 if self.more:
1668 1718 try:
1669 1719 prompt = self.hooks.generate_prompt(True)
1670 1720 except:
1671 1721 self.showtraceback()
1672 1722 if self.autoindent:
1673 1723 self.rl_do_indent = True
1674 1724
1675 1725 else:
1676 1726 try:
1677 1727 prompt = self.hooks.generate_prompt(False)
1678 1728 except:
1679 1729 self.showtraceback()
1680 1730 self.write(prompt)
1681 1731
1682 1732 def interact_handle_input(self,line):
1683 1733 """ Handle the input line (in read-eval-print loop)
1684 1734
1685 1735 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1686 1736 used in standard IPython flow.
1687 1737 """
1688 1738 if line.lstrip() == line:
1689 1739 self.shadowhist.add(line.strip())
1690 1740 lineout = self.prefilter(line,self.more)
1691 1741
1692 1742 if line.strip():
1693 1743 if self.more:
1694 1744 self.input_hist_raw[-1] += '%s\n' % line
1695 1745 else:
1696 1746 self.input_hist_raw.append('%s\n' % line)
1697 1747
1698 1748
1699 1749 self.more = self.push(lineout)
1700 1750 if (self.SyntaxTB.last_syntax_error and
1701 1751 self.rc.autoedit_syntax):
1702 1752 self.edit_syntax_error()
1703 1753
1704 1754 def interact_with_readline(self):
1705 1755 """ Demo of using interact_handle_input, interact_prompt
1706 1756
1707 1757 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1708 1758 it should work like this.
1709 1759 """
1710 1760 self.readline_startup_hook(self.pre_readline)
1711 1761 while not self.exit_now:
1712 1762 self.interact_prompt()
1713 1763 if self.more:
1714 1764 self.rl_do_indent = True
1715 1765 else:
1716 1766 self.rl_do_indent = False
1717 1767 line = raw_input_original().decode(self.stdin_encoding)
1718 1768 self.interact_handle_input(line)
1719 1769
1720 1770
1721 1771 def interact(self, banner=None):
1722 1772 """Closely emulate the interactive Python console.
1723 1773
1724 1774 The optional banner argument specify the banner to print
1725 1775 before the first interaction; by default it prints a banner
1726 1776 similar to the one printed by the real Python interpreter,
1727 1777 followed by the current class name in parentheses (so as not
1728 1778 to confuse this with the real interpreter -- since it's so
1729 1779 close!).
1730 1780
1731 1781 """
1732 1782
1733 1783 if self.exit_now:
1734 1784 # batch run -> do not interact
1735 1785 return
1736 1786 cprt = 'Type "copyright", "credits" or "license" for more information.'
1737 1787 if banner is None:
1738 1788 self.write("Python %s on %s\n%s\n(%s)\n" %
1739 1789 (sys.version, sys.platform, cprt,
1740 1790 self.__class__.__name__))
1741 1791 else:
1742 1792 self.write(banner)
1743 1793
1744 1794 more = 0
1745 1795
1746 1796 # Mark activity in the builtins
1747 1797 __builtin__.__dict__['__IPYTHON__active'] += 1
1748 1798
1749 1799 if self.has_readline:
1750 1800 self.readline_startup_hook(self.pre_readline)
1751 1801 # exit_now is set by a call to %Exit or %Quit, through the
1752 1802 # ask_exit callback.
1753 1803
1754 1804 while not self.exit_now:
1755 1805 self.hooks.pre_prompt_hook()
1756 1806 if more:
1757 1807 try:
1758 1808 prompt = self.hooks.generate_prompt(True)
1759 1809 except:
1760 1810 self.showtraceback()
1761 1811 if self.autoindent:
1762 1812 self.rl_do_indent = True
1763 1813
1764 1814 else:
1765 1815 try:
1766 1816 prompt = self.hooks.generate_prompt(False)
1767 1817 except:
1768 1818 self.showtraceback()
1769 1819 try:
1770 1820 line = self.raw_input(prompt,more)
1771 1821 if self.exit_now:
1772 1822 # quick exit on sys.std[in|out] close
1773 1823 break
1774 1824 if self.autoindent:
1775 1825 self.rl_do_indent = False
1776 1826
1777 1827 except KeyboardInterrupt:
1778 1828 #double-guard against keyboardinterrupts during kbdint handling
1779 1829 try:
1780 1830 self.write('\nKeyboardInterrupt\n')
1781 1831 self.resetbuffer()
1782 1832 # keep cache in sync with the prompt counter:
1783 1833 self.outputcache.prompt_count -= 1
1784 1834
1785 1835 if self.autoindent:
1786 1836 self.indent_current_nsp = 0
1787 1837 more = 0
1788 1838 except KeyboardInterrupt:
1789 1839 pass
1790 1840 except EOFError:
1791 1841 if self.autoindent:
1792 1842 self.rl_do_indent = False
1793 1843 self.readline_startup_hook(None)
1794 1844 self.write('\n')
1795 1845 self.exit()
1796 1846 except bdb.BdbQuit:
1797 1847 warn('The Python debugger has exited with a BdbQuit exception.\n'
1798 1848 'Because of how pdb handles the stack, it is impossible\n'
1799 1849 'for IPython to properly format this particular exception.\n'
1800 1850 'IPython will resume normal operation.')
1801 1851 except:
1802 1852 # exceptions here are VERY RARE, but they can be triggered
1803 1853 # asynchronously by signal handlers, for example.
1804 1854 self.showtraceback()
1805 1855 else:
1806 1856 more = self.push(line)
1807 1857 if (self.SyntaxTB.last_syntax_error and
1808 1858 self.rc.autoedit_syntax):
1809 1859 self.edit_syntax_error()
1810 1860
1811 1861 # We are off again...
1812 1862 __builtin__.__dict__['__IPYTHON__active'] -= 1
1813 1863
1814 1864 def excepthook(self, etype, value, tb):
1815 1865 """One more defense for GUI apps that call sys.excepthook.
1816 1866
1817 1867 GUI frameworks like wxPython trap exceptions and call
1818 1868 sys.excepthook themselves. I guess this is a feature that
1819 1869 enables them to keep running after exceptions that would
1820 1870 otherwise kill their mainloop. This is a bother for IPython
1821 1871 which excepts to catch all of the program exceptions with a try:
1822 1872 except: statement.
1823 1873
1824 1874 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1825 1875 any app directly invokes sys.excepthook, it will look to the user like
1826 1876 IPython crashed. In order to work around this, we can disable the
1827 1877 CrashHandler and replace it with this excepthook instead, which prints a
1828 1878 regular traceback using our InteractiveTB. In this fashion, apps which
1829 1879 call sys.excepthook will generate a regular-looking exception from
1830 1880 IPython, and the CrashHandler will only be triggered by real IPython
1831 1881 crashes.
1832 1882
1833 1883 This hook should be used sparingly, only in places which are not likely
1834 1884 to be true IPython errors.
1835 1885 """
1836 1886 self.showtraceback((etype,value,tb),tb_offset=0)
1837 1887
1838 1888 def expand_aliases(self,fn,rest):
1839 1889 """ Expand multiple levels of aliases:
1840 1890
1841 1891 if:
1842 1892
1843 1893 alias foo bar /tmp
1844 1894 alias baz foo
1845 1895
1846 1896 then:
1847 1897
1848 1898 baz huhhahhei -> bar /tmp huhhahhei
1849 1899
1850 1900 """
1851 1901 line = fn + " " + rest
1852 1902
1853 1903 done = Set()
1854 1904 while 1:
1855 1905 pre,fn,rest = prefilter.splitUserInput(line,
1856 1906 prefilter.shell_line_split)
1857 1907 if fn in self.alias_table:
1858 1908 if fn in done:
1859 1909 warn("Cyclic alias definition, repeated '%s'" % fn)
1860 1910 return ""
1861 1911 done.add(fn)
1862 1912
1863 1913 l2 = self.transform_alias(fn,rest)
1864 1914 # dir -> dir
1865 1915 # print "alias",line, "->",l2 #dbg
1866 1916 if l2 == line:
1867 1917 break
1868 1918 # ls -> ls -F should not recurse forever
1869 1919 if l2.split(None,1)[0] == line.split(None,1)[0]:
1870 1920 line = l2
1871 1921 break
1872 1922
1873 1923 line=l2
1874 1924
1875 1925
1876 1926 # print "al expand to",line #dbg
1877 1927 else:
1878 1928 break
1879 1929
1880 1930 return line
1881 1931
1882 1932 def transform_alias(self, alias,rest=''):
1883 1933 """ Transform alias to system command string.
1884 1934 """
1885 1935 trg = self.alias_table[alias]
1886 1936
1887 1937 nargs,cmd = trg
1888 1938 # print trg #dbg
1889 1939 if ' ' in cmd and os.path.isfile(cmd):
1890 1940 cmd = '"%s"' % cmd
1891 1941
1892 1942 # Expand the %l special to be the user's input line
1893 1943 if cmd.find('%l') >= 0:
1894 1944 cmd = cmd.replace('%l',rest)
1895 1945 rest = ''
1896 1946 if nargs==0:
1897 1947 # Simple, argument-less aliases
1898 1948 cmd = '%s %s' % (cmd,rest)
1899 1949 else:
1900 1950 # Handle aliases with positional arguments
1901 1951 args = rest.split(None,nargs)
1902 1952 if len(args)< nargs:
1903 1953 error('Alias <%s> requires %s arguments, %s given.' %
1904 1954 (alias,nargs,len(args)))
1905 1955 return None
1906 1956 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1907 1957 # Now call the macro, evaluating in the user's namespace
1908 1958 #print 'new command: <%r>' % cmd # dbg
1909 1959 return cmd
1910 1960
1911 1961 def call_alias(self,alias,rest=''):
1912 1962 """Call an alias given its name and the rest of the line.
1913 1963
1914 1964 This is only used to provide backwards compatibility for users of
1915 1965 ipalias(), use of which is not recommended for anymore."""
1916 1966
1917 1967 # Now call the macro, evaluating in the user's namespace
1918 1968 cmd = self.transform_alias(alias, rest)
1919 1969 try:
1920 1970 self.system(cmd)
1921 1971 except:
1922 1972 self.showtraceback()
1923 1973
1924 1974 def indent_current_str(self):
1925 1975 """return the current level of indentation as a string"""
1926 1976 return self.indent_current_nsp * ' '
1927 1977
1928 1978 def autoindent_update(self,line):
1929 1979 """Keep track of the indent level."""
1930 1980
1931 1981 #debugx('line')
1932 1982 #debugx('self.indent_current_nsp')
1933 1983 if self.autoindent:
1934 1984 if line:
1935 1985 inisp = num_ini_spaces(line)
1936 1986 if inisp < self.indent_current_nsp:
1937 1987 self.indent_current_nsp = inisp
1938 1988
1939 1989 if line[-1] == ':':
1940 1990 self.indent_current_nsp += 4
1941 1991 elif dedent_re.match(line):
1942 1992 self.indent_current_nsp -= 4
1943 1993 else:
1944 1994 self.indent_current_nsp = 0
1945 1995
1946 1996 def runlines(self,lines):
1947 1997 """Run a string of one or more lines of source.
1948 1998
1949 1999 This method is capable of running a string containing multiple source
1950 2000 lines, as if they had been entered at the IPython prompt. Since it
1951 2001 exposes IPython's processing machinery, the given strings can contain
1952 2002 magic calls (%magic), special shell access (!cmd), etc."""
1953 2003
1954 2004 # We must start with a clean buffer, in case this is run from an
1955 2005 # interactive IPython session (via a magic, for example).
1956 2006 self.resetbuffer()
1957 2007 lines = lines.split('\n')
1958 2008 more = 0
1959 2009
1960 2010 for line in lines:
1961 2011 # skip blank lines so we don't mess up the prompt counter, but do
1962 2012 # NOT skip even a blank line if we are in a code block (more is
1963 2013 # true)
1964 2014
1965 2015
1966 2016 if line or more:
1967 2017 # push to raw history, so hist line numbers stay in sync
1968 2018 self.input_hist_raw.append("# " + line + "\n")
1969 2019 more = self.push(self.prefilter(line,more))
1970 2020 # IPython's runsource returns None if there was an error
1971 2021 # compiling the code. This allows us to stop processing right
1972 2022 # away, so the user gets the error message at the right place.
1973 2023 if more is None:
1974 2024 break
1975 2025 else:
1976 2026 self.input_hist_raw.append("\n")
1977 2027 # final newline in case the input didn't have it, so that the code
1978 2028 # actually does get executed
1979 2029 if more:
1980 2030 self.push('\n')
1981 2031
1982 2032 def runsource(self, source, filename='<input>', symbol='single'):
1983 2033 """Compile and run some source in the interpreter.
1984 2034
1985 2035 Arguments are as for compile_command().
1986 2036
1987 2037 One several things can happen:
1988 2038
1989 2039 1) The input is incorrect; compile_command() raised an
1990 2040 exception (SyntaxError or OverflowError). A syntax traceback
1991 2041 will be printed by calling the showsyntaxerror() method.
1992 2042
1993 2043 2) The input is incomplete, and more input is required;
1994 2044 compile_command() returned None. Nothing happens.
1995 2045
1996 2046 3) The input is complete; compile_command() returned a code
1997 2047 object. The code is executed by calling self.runcode() (which
1998 2048 also handles run-time exceptions, except for SystemExit).
1999 2049
2000 2050 The return value is:
2001 2051
2002 2052 - True in case 2
2003 2053
2004 2054 - False in the other cases, unless an exception is raised, where
2005 2055 None is returned instead. This can be used by external callers to
2006 2056 know whether to continue feeding input or not.
2007 2057
2008 2058 The return value can be used to decide whether to use sys.ps1 or
2009 2059 sys.ps2 to prompt the next line."""
2010 2060
2011 2061 # if the source code has leading blanks, add 'if 1:\n' to it
2012 2062 # this allows execution of indented pasted code. It is tempting
2013 2063 # to add '\n' at the end of source to run commands like ' a=1'
2014 2064 # directly, but this fails for more complicated scenarios
2015 2065 source=source.encode(self.stdin_encoding)
2016 2066 if source[:1] in [' ', '\t']:
2017 2067 source = 'if 1:\n%s' % source
2018 2068
2019 2069 try:
2020 2070 code = self.compile(source,filename,symbol)
2021 2071 except (OverflowError, SyntaxError, ValueError, TypeError):
2022 2072 # Case 1
2023 2073 self.showsyntaxerror(filename)
2024 2074 return None
2025 2075
2026 2076 if code is None:
2027 2077 # Case 2
2028 2078 return True
2029 2079
2030 2080 # Case 3
2031 2081 # We store the code object so that threaded shells and
2032 2082 # custom exception handlers can access all this info if needed.
2033 2083 # The source corresponding to this can be obtained from the
2034 2084 # buffer attribute as '\n'.join(self.buffer).
2035 2085 self.code_to_run = code
2036 2086 # now actually execute the code object
2037 2087 if self.runcode(code) == 0:
2038 2088 return False
2039 2089 else:
2040 2090 return None
2041 2091
2042 2092 def runcode(self,code_obj):
2043 2093 """Execute a code object.
2044 2094
2045 2095 When an exception occurs, self.showtraceback() is called to display a
2046 2096 traceback.
2047 2097
2048 2098 Return value: a flag indicating whether the code to be run completed
2049 2099 successfully:
2050 2100
2051 2101 - 0: successful execution.
2052 2102 - 1: an error occurred.
2053 2103 """
2054 2104
2055 2105 # Set our own excepthook in case the user code tries to call it
2056 2106 # directly, so that the IPython crash handler doesn't get triggered
2057 2107 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2058 2108
2059 2109 # we save the original sys.excepthook in the instance, in case config
2060 2110 # code (such as magics) needs access to it.
2061 2111 self.sys_excepthook = old_excepthook
2062 2112 outflag = 1 # happens in more places, so it's easier as default
2063 2113 try:
2064 2114 try:
2065 2115 self.hooks.pre_runcode_hook()
2066 2116 exec code_obj in self.user_global_ns, self.user_ns
2067 2117 finally:
2068 2118 # Reset our crash handler in place
2069 2119 sys.excepthook = old_excepthook
2070 2120 except SystemExit:
2071 2121 self.resetbuffer()
2072 2122 self.showtraceback()
2073 2123 warn("Type %exit or %quit to exit IPython "
2074 2124 "(%Exit or %Quit do so unconditionally).",level=1)
2075 2125 except self.custom_exceptions:
2076 2126 etype,value,tb = sys.exc_info()
2077 2127 self.CustomTB(etype,value,tb)
2078 2128 except:
2079 2129 self.showtraceback()
2080 2130 else:
2081 2131 outflag = 0
2082 2132 if softspace(sys.stdout, 0):
2083 2133 print
2084 2134 # Flush out code object which has been run (and source)
2085 2135 self.code_to_run = None
2086 2136 return outflag
2087 2137
2088 2138 def push(self, line):
2089 2139 """Push a line to the interpreter.
2090 2140
2091 2141 The line should not have a trailing newline; it may have
2092 2142 internal newlines. The line is appended to a buffer and the
2093 2143 interpreter's runsource() method is called with the
2094 2144 concatenated contents of the buffer as source. If this
2095 2145 indicates that the command was executed or invalid, the buffer
2096 2146 is reset; otherwise, the command is incomplete, and the buffer
2097 2147 is left as it was after the line was appended. The return
2098 2148 value is 1 if more input is required, 0 if the line was dealt
2099 2149 with in some way (this is the same as runsource()).
2100 2150 """
2101 2151
2102 2152 # autoindent management should be done here, and not in the
2103 2153 # interactive loop, since that one is only seen by keyboard input. We
2104 2154 # need this done correctly even for code run via runlines (which uses
2105 2155 # push).
2106 2156
2107 2157 #print 'push line: <%s>' % line # dbg
2108 2158 for subline in line.splitlines():
2109 2159 self.autoindent_update(subline)
2110 2160 self.buffer.append(line)
2111 2161 more = self.runsource('\n'.join(self.buffer), self.filename)
2112 2162 if not more:
2113 2163 self.resetbuffer()
2114 2164 return more
2115 2165
2116 2166 def split_user_input(self, line):
2117 2167 # This is really a hold-over to support ipapi and some extensions
2118 2168 return prefilter.splitUserInput(line)
2119 2169
2120 2170 def resetbuffer(self):
2121 2171 """Reset the input buffer."""
2122 2172 self.buffer[:] = []
2123 2173
2124 2174 def raw_input(self,prompt='',continue_prompt=False):
2125 2175 """Write a prompt and read a line.
2126 2176
2127 2177 The returned line does not include the trailing newline.
2128 2178 When the user enters the EOF key sequence, EOFError is raised.
2129 2179
2130 2180 Optional inputs:
2131 2181
2132 2182 - prompt(''): a string to be printed to prompt the user.
2133 2183
2134 2184 - continue_prompt(False): whether this line is the first one or a
2135 2185 continuation in a sequence of inputs.
2136 2186 """
2137 2187
2138 2188 # Code run by the user may have modified the readline completer state.
2139 2189 # We must ensure that our completer is back in place.
2140 2190 if self.has_readline:
2141 2191 self.set_completer()
2142 2192
2143 2193 try:
2144 2194 line = raw_input_original(prompt).decode(self.stdin_encoding)
2145 2195 except ValueError:
2146 2196 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2147 2197 " or sys.stdout.close()!\nExiting IPython!")
2148 2198 self.ask_exit()
2149 2199 return ""
2150 2200
2151 2201 # Try to be reasonably smart about not re-indenting pasted input more
2152 2202 # than necessary. We do this by trimming out the auto-indent initial
2153 2203 # spaces, if the user's actual input started itself with whitespace.
2154 2204 #debugx('self.buffer[-1]')
2155 2205
2156 2206 if self.autoindent:
2157 2207 if num_ini_spaces(line) > self.indent_current_nsp:
2158 2208 line = line[self.indent_current_nsp:]
2159 2209 self.indent_current_nsp = 0
2160 2210
2161 2211 # store the unfiltered input before the user has any chance to modify
2162 2212 # it.
2163 2213 if line.strip():
2164 2214 if continue_prompt:
2165 2215 self.input_hist_raw[-1] += '%s\n' % line
2166 2216 if self.has_readline: # and some config option is set?
2167 2217 try:
2168 2218 histlen = self.readline.get_current_history_length()
2169 2219 if histlen > 1:
2170 2220 newhist = self.input_hist_raw[-1].rstrip()
2171 2221 self.readline.remove_history_item(histlen-1)
2172 2222 self.readline.replace_history_item(histlen-2,
2173 2223 newhist.encode(self.stdin_encoding))
2174 2224 except AttributeError:
2175 2225 pass # re{move,place}_history_item are new in 2.4.
2176 2226 else:
2177 2227 self.input_hist_raw.append('%s\n' % line)
2178 2228 # only entries starting at first column go to shadow history
2179 2229 if line.lstrip() == line:
2180 2230 self.shadowhist.add(line.strip())
2181 2231 elif not continue_prompt:
2182 2232 self.input_hist_raw.append('\n')
2183 2233 try:
2184 2234 lineout = self.prefilter(line,continue_prompt)
2185 2235 except:
2186 2236 # blanket except, in case a user-defined prefilter crashes, so it
2187 2237 # can't take all of ipython with it.
2188 2238 self.showtraceback()
2189 2239 return ''
2190 2240 else:
2191 2241 return lineout
2192 2242
2193 2243 def _prefilter(self, line, continue_prompt):
2194 2244 """Calls different preprocessors, depending on the form of line."""
2195 2245
2196 2246 # All handlers *must* return a value, even if it's blank ('').
2197 2247
2198 2248 # Lines are NOT logged here. Handlers should process the line as
2199 2249 # needed, update the cache AND log it (so that the input cache array
2200 2250 # stays synced).
2201 2251
2202 2252 #.....................................................................
2203 2253 # Code begins
2204 2254
2205 2255 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2206 2256
2207 2257 # save the line away in case we crash, so the post-mortem handler can
2208 2258 # record it
2209 2259 self._last_input_line = line
2210 2260
2211 2261 #print '***line: <%s>' % line # dbg
2212 2262
2213 2263 if not line:
2214 2264 # Return immediately on purely empty lines, so that if the user
2215 2265 # previously typed some whitespace that started a continuation
2216 2266 # prompt, he can break out of that loop with just an empty line.
2217 2267 # This is how the default python prompt works.
2218 2268
2219 2269 # Only return if the accumulated input buffer was just whitespace!
2220 2270 if ''.join(self.buffer).isspace():
2221 2271 self.buffer[:] = []
2222 2272 return ''
2223 2273
2224 2274 line_info = prefilter.LineInfo(line, continue_prompt)
2225 2275
2226 2276 # the input history needs to track even empty lines
2227 2277 stripped = line.strip()
2228 2278
2229 2279 if not stripped:
2230 2280 if not continue_prompt:
2231 2281 self.outputcache.prompt_count -= 1
2232 2282 return self.handle_normal(line_info)
2233 2283
2234 2284 # print '***cont',continue_prompt # dbg
2235 2285 # special handlers are only allowed for single line statements
2236 2286 if continue_prompt and not self.rc.multi_line_specials:
2237 2287 return self.handle_normal(line_info)
2238 2288
2239 2289
2240 2290 # See whether any pre-existing handler can take care of it
2241 2291 rewritten = self.hooks.input_prefilter(stripped)
2242 2292 if rewritten != stripped: # ok, some prefilter did something
2243 2293 rewritten = line_info.pre + rewritten # add indentation
2244 2294 return self.handle_normal(prefilter.LineInfo(rewritten,
2245 2295 continue_prompt))
2246 2296
2247 2297 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2248 2298
2249 2299 return prefilter.prefilter(line_info, self)
2250 2300
2251 2301
2252 2302 def _prefilter_dumb(self, line, continue_prompt):
2253 2303 """simple prefilter function, for debugging"""
2254 2304 return self.handle_normal(line,continue_prompt)
2255 2305
2256 2306
2257 2307 def multiline_prefilter(self, line, continue_prompt):
2258 2308 """ Run _prefilter for each line of input
2259 2309
2260 2310 Covers cases where there are multiple lines in the user entry,
2261 2311 which is the case when the user goes back to a multiline history
2262 2312 entry and presses enter.
2263 2313
2264 2314 """
2265 2315 out = []
2266 2316 for l in line.rstrip('\n').split('\n'):
2267 2317 out.append(self._prefilter(l, continue_prompt))
2268 2318 return '\n'.join(out)
2269 2319
2270 2320 # Set the default prefilter() function (this can be user-overridden)
2271 2321 prefilter = multiline_prefilter
2272 2322
2273 2323 def handle_normal(self,line_info):
2274 2324 """Handle normal input lines. Use as a template for handlers."""
2275 2325
2276 2326 # With autoindent on, we need some way to exit the input loop, and I
2277 2327 # don't want to force the user to have to backspace all the way to
2278 2328 # clear the line. The rule will be in this case, that either two
2279 2329 # lines of pure whitespace in a row, or a line of pure whitespace but
2280 2330 # of a size different to the indent level, will exit the input loop.
2281 2331 line = line_info.line
2282 2332 continue_prompt = line_info.continue_prompt
2283 2333
2284 2334 if (continue_prompt and self.autoindent and line.isspace() and
2285 2335 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2286 2336 (self.buffer[-1]).isspace() )):
2287 2337 line = ''
2288 2338
2289 2339 self.log(line,line,continue_prompt)
2290 2340 return line
2291 2341
2292 2342 def handle_alias(self,line_info):
2293 2343 """Handle alias input lines. """
2294 2344 tgt = self.alias_table[line_info.iFun]
2295 2345 # print "=>",tgt #dbg
2296 2346 if callable(tgt):
2297 2347 if '$' in line_info.line:
2298 2348 call_meth = '(_ip, _ip.itpl(%s))'
2299 2349 else:
2300 2350 call_meth = '(_ip,%s)'
2301 2351 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2302 2352 line_info.iFun,
2303 2353 make_quoted_expr(line_info.line))
2304 2354 else:
2305 2355 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2306 2356
2307 2357 # pre is needed, because it carries the leading whitespace. Otherwise
2308 2358 # aliases won't work in indented sections.
2309 2359 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2310 2360 make_quoted_expr( transformed ))
2311 2361
2312 2362 self.log(line_info.line,line_out,line_info.continue_prompt)
2313 2363 #print 'line out:',line_out # dbg
2314 2364 return line_out
2315 2365
2316 2366 def handle_shell_escape(self, line_info):
2317 2367 """Execute the line in a shell, empty return value"""
2318 2368 #print 'line in :', `line` # dbg
2319 2369 line = line_info.line
2320 2370 if line.lstrip().startswith('!!'):
2321 2371 # rewrite LineInfo's line, iFun and theRest to properly hold the
2322 2372 # call to %sx and the actual command to be executed, so
2323 2373 # handle_magic can work correctly. Note that this works even if
2324 2374 # the line is indented, so it handles multi_line_specials
2325 2375 # properly.
2326 2376 new_rest = line.lstrip()[2:]
2327 2377 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2328 2378 line_info.iFun = 'sx'
2329 2379 line_info.theRest = new_rest
2330 2380 return self.handle_magic(line_info)
2331 2381 else:
2332 2382 cmd = line.lstrip().lstrip('!')
2333 2383 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2334 2384 make_quoted_expr(cmd))
2335 2385 # update cache/log and return
2336 2386 self.log(line,line_out,line_info.continue_prompt)
2337 2387 return line_out
2338 2388
2339 2389 def handle_magic(self, line_info):
2340 2390 """Execute magic functions."""
2341 2391 iFun = line_info.iFun
2342 2392 theRest = line_info.theRest
2343 2393 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2344 2394 make_quoted_expr(iFun + " " + theRest))
2345 2395 self.log(line_info.line,cmd,line_info.continue_prompt)
2346 2396 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2347 2397 return cmd
2348 2398
2349 2399 def handle_auto(self, line_info):
2350 2400 """Hande lines which can be auto-executed, quoting if requested."""
2351 2401
2352 2402 line = line_info.line
2353 2403 iFun = line_info.iFun
2354 2404 theRest = line_info.theRest
2355 2405 pre = line_info.pre
2356 2406 continue_prompt = line_info.continue_prompt
2357 2407 obj = line_info.ofind(self)['obj']
2358 2408
2359 2409 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2360 2410
2361 2411 # This should only be active for single-line input!
2362 2412 if continue_prompt:
2363 2413 self.log(line,line,continue_prompt)
2364 2414 return line
2365 2415
2366 2416 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2367 2417 auto_rewrite = True
2368 2418
2369 2419 if pre == self.ESC_QUOTE:
2370 2420 # Auto-quote splitting on whitespace
2371 2421 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2372 2422 elif pre == self.ESC_QUOTE2:
2373 2423 # Auto-quote whole string
2374 2424 newcmd = '%s("%s")' % (iFun,theRest)
2375 2425 elif pre == self.ESC_PAREN:
2376 2426 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2377 2427 else:
2378 2428 # Auto-paren.
2379 2429 # We only apply it to argument-less calls if the autocall
2380 2430 # parameter is set to 2. We only need to check that autocall is <
2381 2431 # 2, since this function isn't called unless it's at least 1.
2382 2432 if not theRest and (self.rc.autocall < 2) and not force_auto:
2383 2433 newcmd = '%s %s' % (iFun,theRest)
2384 2434 auto_rewrite = False
2385 2435 else:
2386 2436 if not force_auto and theRest.startswith('['):
2387 2437 if hasattr(obj,'__getitem__'):
2388 2438 # Don't autocall in this case: item access for an object
2389 2439 # which is BOTH callable and implements __getitem__.
2390 2440 newcmd = '%s %s' % (iFun,theRest)
2391 2441 auto_rewrite = False
2392 2442 else:
2393 2443 # if the object doesn't support [] access, go ahead and
2394 2444 # autocall
2395 2445 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2396 2446 elif theRest.endswith(';'):
2397 2447 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2398 2448 else:
2399 2449 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2400 2450
2401 2451 if auto_rewrite:
2402 2452 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2403 2453
2404 2454 try:
2405 2455 # plain ascii works better w/ pyreadline, on some machines, so
2406 2456 # we use it and only print uncolored rewrite if we have unicode
2407 2457 rw = str(rw)
2408 2458 print >>Term.cout, rw
2409 2459 except UnicodeEncodeError:
2410 2460 print "-------------->" + newcmd
2411 2461
2412 2462 # log what is now valid Python, not the actual user input (without the
2413 2463 # final newline)
2414 2464 self.log(line,newcmd,continue_prompt)
2415 2465 return newcmd
2416 2466
2417 2467 def handle_help(self, line_info):
2418 2468 """Try to get some help for the object.
2419 2469
2420 2470 obj? or ?obj -> basic information.
2421 2471 obj?? or ??obj -> more details.
2422 2472 """
2423 2473
2424 2474 line = line_info.line
2425 2475 # We need to make sure that we don't process lines which would be
2426 2476 # otherwise valid python, such as "x=1 # what?"
2427 2477 try:
2428 2478 codeop.compile_command(line)
2429 2479 except SyntaxError:
2430 2480 # We should only handle as help stuff which is NOT valid syntax
2431 2481 if line[0]==self.ESC_HELP:
2432 2482 line = line[1:]
2433 2483 elif line[-1]==self.ESC_HELP:
2434 2484 line = line[:-1]
2435 2485 self.log(line,'#?'+line,line_info.continue_prompt)
2436 2486 if line:
2437 2487 #print 'line:<%r>' % line # dbg
2438 2488 self.magic_pinfo(line)
2439 2489 else:
2440 2490 page(self.usage,screen_lines=self.rc.screen_length)
2441 2491 return '' # Empty string is needed here!
2442 2492 except:
2443 2493 # Pass any other exceptions through to the normal handler
2444 2494 return self.handle_normal(line_info)
2445 2495 else:
2446 2496 # If the code compiles ok, we should handle it normally
2447 2497 return self.handle_normal(line_info)
2448 2498
2449 2499 def getapi(self):
2450 2500 """ Get an IPApi object for this shell instance
2451 2501
2452 2502 Getting an IPApi object is always preferable to accessing the shell
2453 2503 directly, but this holds true especially for extensions.
2454 2504
2455 2505 It should always be possible to implement an extension with IPApi
2456 2506 alone. If not, contact maintainer to request an addition.
2457 2507
2458 2508 """
2459 2509 return self.api
2460 2510
2461 2511 def handle_emacs(self, line_info):
2462 2512 """Handle input lines marked by python-mode."""
2463 2513
2464 2514 # Currently, nothing is done. Later more functionality can be added
2465 2515 # here if needed.
2466 2516
2467 2517 # The input cache shouldn't be updated
2468 2518 return line_info.line
2469 2519
2470 2520
2471 2521 def mktempfile(self,data=None):
2472 2522 """Make a new tempfile and return its filename.
2473 2523
2474 2524 This makes a call to tempfile.mktemp, but it registers the created
2475 2525 filename internally so ipython cleans it up at exit time.
2476 2526
2477 2527 Optional inputs:
2478 2528
2479 2529 - data(None): if data is given, it gets written out to the temp file
2480 2530 immediately, and the file is closed again."""
2481 2531
2482 2532 filename = tempfile.mktemp('.py','ipython_edit_')
2483 2533 self.tempfiles.append(filename)
2484 2534
2485 2535 if data:
2486 2536 tmp_file = open(filename,'w')
2487 2537 tmp_file.write(data)
2488 2538 tmp_file.close()
2489 2539 return filename
2490 2540
2491 2541 def write(self,data):
2492 2542 """Write a string to the default output"""
2493 2543 Term.cout.write(data)
2494 2544
2495 2545 def write_err(self,data):
2496 2546 """Write a string to the default error output"""
2497 2547 Term.cerr.write(data)
2498 2548
2499 2549 def ask_exit(self):
2500 2550 """ Call for exiting. Can be overiden and used as a callback. """
2501 2551 self.exit_now = True
2502 2552
2503 2553 def exit(self):
2504 2554 """Handle interactive exit.
2505 2555
2506 2556 This method calls the ask_exit callback."""
2507 2557
2508 2558 if self.rc.confirm_exit:
2509 2559 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2510 2560 self.ask_exit()
2511 2561 else:
2512 2562 self.ask_exit()
2513 2563
2514 2564 def safe_execfile(self,fname,*where,**kw):
2515 2565 """A safe version of the builtin execfile().
2516 2566
2517 2567 This version will never throw an exception, and knows how to handle
2518 2568 ipython logs as well.
2519 2569
2520 2570 :Parameters:
2521 2571 fname : string
2522 2572 Name of the file to be executed.
2523 2573
2524 2574 where : tuple
2525 2575 One or two namespaces, passed to execfile() as (globals,locals).
2526 2576 If only one is given, it is passed as both.
2527 2577
2528 2578 :Keywords:
2529 2579 islog : boolean (False)
2530 2580
2531 2581 quiet : boolean (True)
2532 2582
2533 2583 exit_ignore : boolean (False)
2534 2584 """
2535 2585
2536 2586 def syspath_cleanup():
2537 2587 """Internal cleanup routine for sys.path."""
2538 2588 if add_dname:
2539 2589 try:
2540 2590 sys.path.remove(dname)
2541 2591 except ValueError:
2542 2592 # For some reason the user has already removed it, ignore.
2543 2593 pass
2544 2594
2545 2595 fname = os.path.expanduser(fname)
2546 2596
2547 2597 # Find things also in current directory. This is needed to mimic the
2548 2598 # behavior of running a script from the system command line, where
2549 2599 # Python inserts the script's directory into sys.path
2550 2600 dname = os.path.dirname(os.path.abspath(fname))
2551 2601 add_dname = False
2552 2602 if dname not in sys.path:
2553 2603 sys.path.insert(0,dname)
2554 2604 add_dname = True
2555 2605
2556 2606 try:
2557 2607 xfile = open(fname)
2558 2608 except:
2559 2609 print >> Term.cerr, \
2560 2610 'Could not open file <%s> for safe execution.' % fname
2561 2611 syspath_cleanup()
2562 2612 return None
2563 2613
2564 2614 kw.setdefault('islog',0)
2565 2615 kw.setdefault('quiet',1)
2566 2616 kw.setdefault('exit_ignore',0)
2567 2617
2568 2618 first = xfile.readline()
2569 2619 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2570 2620 xfile.close()
2571 2621 # line by line execution
2572 2622 if first.startswith(loghead) or kw['islog']:
2573 2623 print 'Loading log file <%s> one line at a time...' % fname
2574 2624 if kw['quiet']:
2575 2625 stdout_save = sys.stdout
2576 2626 sys.stdout = StringIO.StringIO()
2577 2627 try:
2578 2628 globs,locs = where[0:2]
2579 2629 except:
2580 2630 try:
2581 2631 globs = locs = where[0]
2582 2632 except:
2583 2633 globs = locs = globals()
2584 2634 badblocks = []
2585 2635
2586 2636 # we also need to identify indented blocks of code when replaying
2587 2637 # logs and put them together before passing them to an exec
2588 2638 # statement. This takes a bit of regexp and look-ahead work in the
2589 2639 # file. It's easiest if we swallow the whole thing in memory
2590 2640 # first, and manually walk through the lines list moving the
2591 2641 # counter ourselves.
2592 2642 indent_re = re.compile('\s+\S')
2593 2643 xfile = open(fname)
2594 2644 filelines = xfile.readlines()
2595 2645 xfile.close()
2596 2646 nlines = len(filelines)
2597 2647 lnum = 0
2598 2648 while lnum < nlines:
2599 2649 line = filelines[lnum]
2600 2650 lnum += 1
2601 2651 # don't re-insert logger status info into cache
2602 2652 if line.startswith('#log#'):
2603 2653 continue
2604 2654 else:
2605 2655 # build a block of code (maybe a single line) for execution
2606 2656 block = line
2607 2657 try:
2608 2658 next = filelines[lnum] # lnum has already incremented
2609 2659 except:
2610 2660 next = None
2611 2661 while next and indent_re.match(next):
2612 2662 block += next
2613 2663 lnum += 1
2614 2664 try:
2615 2665 next = filelines[lnum]
2616 2666 except:
2617 2667 next = None
2618 2668 # now execute the block of one or more lines
2619 2669 try:
2620 2670 exec block in globs,locs
2621 2671 except SystemExit:
2622 2672 pass
2623 2673 except:
2624 2674 badblocks.append(block.rstrip())
2625 2675 if kw['quiet']: # restore stdout
2626 2676 sys.stdout.close()
2627 2677 sys.stdout = stdout_save
2628 2678 print 'Finished replaying log file <%s>' % fname
2629 2679 if badblocks:
2630 2680 print >> sys.stderr, ('\nThe following lines/blocks in file '
2631 2681 '<%s> reported errors:' % fname)
2632 2682
2633 2683 for badline in badblocks:
2634 2684 print >> sys.stderr, badline
2635 2685 else: # regular file execution
2636 2686 try:
2637 2687 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2638 2688 # Work around a bug in Python for Windows. The bug was
2639 2689 # fixed in in Python 2.5 r54159 and 54158, but that's still
2640 2690 # SVN Python as of March/07. For details, see:
2641 2691 # http://projects.scipy.org/ipython/ipython/ticket/123
2642 2692 try:
2643 2693 globs,locs = where[0:2]
2644 2694 except:
2645 2695 try:
2646 2696 globs = locs = where[0]
2647 2697 except:
2648 2698 globs = locs = globals()
2649 2699 exec file(fname) in globs,locs
2650 2700 else:
2651 2701 execfile(fname,*where)
2652 2702 except SyntaxError:
2653 2703 self.showsyntaxerror()
2654 2704 warn('Failure executing file: <%s>' % fname)
2655 2705 except SystemExit,status:
2656 2706 # Code that correctly sets the exit status flag to success (0)
2657 2707 # shouldn't be bothered with a traceback. Note that a plain
2658 2708 # sys.exit() does NOT set the message to 0 (it's empty) so that
2659 2709 # will still get a traceback. Note that the structure of the
2660 2710 # SystemExit exception changed between Python 2.4 and 2.5, so
2661 2711 # the checks must be done in a version-dependent way.
2662 2712 show = False
2663 2713
2664 2714 if sys.version_info[:2] > (2,5):
2665 2715 if status.message!=0 and not kw['exit_ignore']:
2666 2716 show = True
2667 2717 else:
2668 2718 if status.code and not kw['exit_ignore']:
2669 2719 show = True
2670 2720 if show:
2671 2721 self.showtraceback()
2672 2722 warn('Failure executing file: <%s>' % fname)
2673 2723 except:
2674 2724 self.showtraceback()
2675 2725 warn('Failure executing file: <%s>' % fname)
2676 2726
2677 2727 syspath_cleanup()
2678 2728
2679 2729 #************************* end of file <iplib.py> *****************************
@@ -1,6 +1,26 b''
1 1 """Simple script to instantiate a class for testing %run"""
2 2
3 import sys
4
5 # An external test will check that calls to f() work after %run
3 6 class foo: pass
4 7
5 8 def f():
6 foo()
9 return foo()
10
11 # We also want to ensure that while objects remain available for immediate
12 # access, objects from *previous* runs of the same script get collected, to
13 # avoid accumulating massive amounts of old references.
14 class C(object):
15 def __init__(self,name):
16 self.name = name
17
18 def __del__(self):
19 print 'Deleting object:',self.name
20
21 try:
22 name = sys.argv[1]
23 except IndexError:
24 pass
25 else:
26 c = C(name)
@@ -1,115 +1,127 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 # Standard library imports
7 7 import os
8 8 import sys
9 9
10 10 # Third-party imports
11 11 import nose.tools as nt
12 12
13 13 # From our own code
14 14 from IPython.testing import decorators as dec
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Test functions begin
18 18
19 19 def test_rehashx():
20 20 # clear up everything
21 21 _ip.IP.alias_table.clear()
22 22 del _ip.db['syscmdlist']
23 23
24 24 _ip.magic('rehashx')
25 25 # Practically ALL ipython development systems will have more than 10 aliases
26 26
27 27 assert len(_ip.IP.alias_table) > 10
28 28 for key, val in _ip.IP.alias_table.items():
29 29 # we must strip dots from alias names
30 30 assert '.' not in key
31 31
32 32 # rehashx must fill up syscmdlist
33 33 scoms = _ip.db['syscmdlist']
34 34 assert len(scoms) > 10
35 35
36 36
37 37 def doctest_run_ns():
38 38 """Classes declared %run scripts must be instantiable afterwards.
39 39
40 In [11]: run tclass
41
42 In [12]: isinstance(f(),foo)
43 Out[12]: True
44 """
45
46
47 def doctest_run_ns2():
48 """Classes declared %run scripts must be instantiable afterwards.
49
40 50 In [3]: run tclass.py
41 51
42 In [4]: f()
52 In [4]: run tclass first_pass
53
54 In [5]: run tclass second_pass
55 Deleting object: first_pass
43 56 """
44 pass # doctest only
45 57
46 58
47 59 def doctest_hist_f():
48 60 """Test %hist -f with temporary filename.
49 61
50 62 In [9]: import tempfile
51 63
52 64 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
53 65
54 66 In [11]: %history -n -f $tfile 3
55 67 """
56 68
57 69
58 70 def doctest_hist_r():
59 71 """Test %hist -r
60 72
61 73 XXX - This test is not recording the output correctly. Not sure why...
62 74
63 75 In [6]: x=1
64 76
65 77 In [7]: hist -n -r 2
66 78 x=1 # random
67 79 hist -n -r 2 # random
68 80 """
69 81
70 82
71 83 def test_shist():
72 84 # Simple tests of ShadowHist class - test generator.
73 85 import os, shutil, tempfile
74 86
75 87 from IPython.Extensions import pickleshare
76 88 from IPython.history import ShadowHist
77 89
78 90 tfile = tempfile.mktemp('','tmp-ipython-')
79 91
80 92 db = pickleshare.PickleShareDB(tfile)
81 93 s = ShadowHist(db)
82 94 s.add('hello')
83 95 s.add('world')
84 96 s.add('hello')
85 97 s.add('hello')
86 98 s.add('karhu')
87 99
88 100 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
89 101
90 102 yield nt.assert_equal,s.get(2),'world'
91 103
92 104 shutil.rmtree(tfile)
93 105
94 106 @dec.skipif_not_numpy
95 107 def test_numpy_clear_array_undec():
96 108 _ip.ex('import numpy as np')
97 109 _ip.ex('a = np.empty(2)')
98 110
99 111 yield nt.assert_true,'a' in _ip.user_ns
100 112 _ip.magic('clear array')
101 113 yield nt.assert_false,'a' in _ip.user_ns
102 114
103 115
104 116 @dec.skip()
105 117 def test_fail_dec(*a,**k):
106 118 yield nt.assert_true, False
107 119
108 120 @dec.skip('This one shouldn not run')
109 121 def test_fail_dec2(*a,**k):
110 122 yield nt.assert_true, False
111 123
112 124 @dec.skipknownfailure
113 125 def test_fail_dec3(*a,**k):
114 126 yield nt.assert_true, False
115 127
General Comments 0
You need to be logged in to leave comments. Login now