##// END OF EJS Templates
Made ! and !! shell escapes work again in multiline statements.
vivainio -
Show More

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

@@ -1,2157 +1,2157 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.1 or newer.
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 1007 2006-01-12 17:15:41Z vivainio $
9 $Id: iplib.py 1012 2006-01-12 21:29:37Z vivainio $
10 10 """
11 11
12 12 #*****************************************************************************
13 13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #
19 19 # Note: this code originally subclassed code.InteractiveConsole from the
20 20 # Python standard library. Over time, all of that class has been copied
21 21 # verbatim here for modifications which could not be accomplished by
22 22 # subclassing. At this point, there are no dependencies at all on the code
23 23 # module anymore (it is not even imported). The Python License (sec. 2)
24 24 # allows for this, but it's always nice to acknowledge credit where credit is
25 25 # due.
26 26 #*****************************************************************************
27 27
28 28 #****************************************************************************
29 29 # Modules and globals
30 30
31 31 from __future__ import generators # for 2.2 backwards-compatibility
32 32
33 33 from IPython import Release
34 34 __author__ = '%s <%s>\n%s <%s>' % \
35 35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 36 __license__ = Release.license
37 37 __version__ = Release.version
38 38
39 39 # Python standard modules
40 40 import __main__
41 41 import __builtin__
42 42 import StringIO
43 43 import bdb
44 44 import cPickle as pickle
45 45 import codeop
46 46 import exceptions
47 47 import glob
48 48 import inspect
49 49 import keyword
50 50 import new
51 51 import os
52 52 import pdb
53 53 import pydoc
54 54 import re
55 55 import shutil
56 56 import string
57 57 import sys
58 58 import tempfile
59 59 import traceback
60 60 import types
61 61
62 62 from pprint import pprint, pformat
63 63
64 64 # IPython's own modules
65 65 import IPython
66 66 from IPython import OInspect,PyColorize,ultraTB
67 67 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
68 68 from IPython.FakeModule import FakeModule
69 69 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
70 70 from IPython.Logger import Logger
71 71 from IPython.Magic import Magic
72 72 from IPython.Prompts import CachedOutput
73 73 from IPython.ipstruct import Struct
74 74 from IPython.background_jobs import BackgroundJobManager
75 75 from IPython.usage import cmd_line_usage,interactive_usage
76 76 from IPython.genutils import *
77 77
78 78 # Globals
79 79
80 80 # store the builtin raw_input globally, and use this always, in case user code
81 81 # overwrites it (like wx.py.PyShell does)
82 82 raw_input_original = raw_input
83 83
84 84 # compiled regexps for autoindent management
85 85 ini_spaces_re = re.compile(r'^(\s+)')
86 86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
87 87
88 88
89 89 #****************************************************************************
90 90 # Some utility function definitions
91 91
92 92 def softspace(file, newvalue):
93 93 """Copied from code.py, to remove the dependency"""
94 94 oldvalue = 0
95 95 try:
96 96 oldvalue = file.softspace
97 97 except AttributeError:
98 98 pass
99 99 try:
100 100 file.softspace = newvalue
101 101 except (AttributeError, TypeError):
102 102 # "attribute-less object" or "read-only attributes"
103 103 pass
104 104 return oldvalue
105 105
106 106
107 107 #****************************************************************************
108 108 # Local use exceptions
109 109 class SpaceInInput(exceptions.Exception): pass
110 110
111 111
112 112 #****************************************************************************
113 113 # Local use classes
114 114 class Bunch: pass
115 115
116 116 class Undefined: pass
117 117
118 118 class InputList(list):
119 119 """Class to store user input.
120 120
121 121 It's basically a list, but slices return a string instead of a list, thus
122 122 allowing things like (assuming 'In' is an instance):
123 123
124 124 exec In[4:7]
125 125
126 126 or
127 127
128 128 exec In[5:9] + In[14] + In[21:25]"""
129 129
130 130 def __getslice__(self,i,j):
131 131 return ''.join(list.__getslice__(self,i,j))
132 132
133 133 class SyntaxTB(ultraTB.ListTB):
134 134 """Extension which holds some state: the last exception value"""
135 135
136 136 def __init__(self,color_scheme = 'NoColor'):
137 137 ultraTB.ListTB.__init__(self,color_scheme)
138 138 self.last_syntax_error = None
139 139
140 140 def __call__(self, etype, value, elist):
141 141 self.last_syntax_error = value
142 142 ultraTB.ListTB.__call__(self,etype,value,elist)
143 143
144 144 def clear_err_state(self):
145 145 """Return the current error state and clear it"""
146 146 e = self.last_syntax_error
147 147 self.last_syntax_error = None
148 148 return e
149 149
150 150 #****************************************************************************
151 151 # Main IPython class
152 152
153 153 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
154 154 # until a full rewrite is made. I've cleaned all cross-class uses of
155 155 # attributes and methods, but too much user code out there relies on the
156 156 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
157 157 #
158 158 # But at least now, all the pieces have been separated and we could, in
159 159 # principle, stop using the mixin. This will ease the transition to the
160 160 # chainsaw branch.
161 161
162 162 # For reference, the following is the list of 'self.foo' uses in the Magic
163 163 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
164 164 # class, to prevent clashes.
165 165
166 166 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
167 167 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
168 168 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
169 169 # 'self.value']
170 170
171 171 class InteractiveShell(object,Magic):
172 172 """An enhanced console for Python."""
173 173
174 174 # class attribute to indicate whether the class supports threads or not.
175 175 # Subclasses with thread support should override this as needed.
176 176 isthreaded = False
177 177
178 178 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
179 179 user_ns = None,user_global_ns=None,banner2='',
180 180 custom_exceptions=((),None),embedded=False):
181 181
182 182 # some minimal strict typechecks. For some core data structures, I
183 183 # want actual basic python types, not just anything that looks like
184 184 # one. This is especially true for namespaces.
185 185 for ns in (user_ns,user_global_ns):
186 186 if ns is not None and type(ns) != types.DictType:
187 187 raise TypeError,'namespace must be a dictionary'
188 188
189 189 # Job manager (for jobs run as background threads)
190 190 self.jobs = BackgroundJobManager()
191 191
192 192 # track which builtins we add, so we can clean up later
193 193 self.builtins_added = {}
194 194 # This method will add the necessary builtins for operation, but
195 195 # tracking what it did via the builtins_added dict.
196 196 self.add_builtins()
197 197
198 198 # Do the intuitively correct thing for quit/exit: we remove the
199 199 # builtins if they exist, and our own magics will deal with this
200 200 try:
201 201 del __builtin__.exit, __builtin__.quit
202 202 except AttributeError:
203 203 pass
204 204
205 205 # Store the actual shell's name
206 206 self.name = name
207 207
208 208 # We need to know whether the instance is meant for embedding, since
209 209 # global/local namespaces need to be handled differently in that case
210 210 self.embedded = embedded
211 211
212 212 # command compiler
213 213 self.compile = codeop.CommandCompiler()
214 214
215 215 # User input buffer
216 216 self.buffer = []
217 217
218 218 # Default name given in compilation of code
219 219 self.filename = '<ipython console>'
220 220
221 221 # Make an empty namespace, which extension writers can rely on both
222 222 # existing and NEVER being used by ipython itself. This gives them a
223 223 # convenient location for storing additional information and state
224 224 # their extensions may require, without fear of collisions with other
225 225 # ipython names that may develop later.
226 226 self.meta = Bunch()
227 227
228 228 # Create the namespace where the user will operate. user_ns is
229 229 # normally the only one used, and it is passed to the exec calls as
230 230 # the locals argument. But we do carry a user_global_ns namespace
231 231 # given as the exec 'globals' argument, This is useful in embedding
232 232 # situations where the ipython shell opens in a context where the
233 233 # distinction between locals and globals is meaningful.
234 234
235 235 # FIXME. For some strange reason, __builtins__ is showing up at user
236 236 # level as a dict instead of a module. This is a manual fix, but I
237 237 # should really track down where the problem is coming from. Alex
238 238 # Schmolck reported this problem first.
239 239
240 240 # A useful post by Alex Martelli on this topic:
241 241 # Re: inconsistent value from __builtins__
242 242 # Von: Alex Martelli <aleaxit@yahoo.com>
243 243 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
244 244 # Gruppen: comp.lang.python
245 245
246 246 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
247 247 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
248 248 # > <type 'dict'>
249 249 # > >>> print type(__builtins__)
250 250 # > <type 'module'>
251 251 # > Is this difference in return value intentional?
252 252
253 253 # Well, it's documented that '__builtins__' can be either a dictionary
254 254 # or a module, and it's been that way for a long time. Whether it's
255 255 # intentional (or sensible), I don't know. In any case, the idea is
256 256 # that if you need to access the built-in namespace directly, you
257 257 # should start with "import __builtin__" (note, no 's') which will
258 258 # definitely give you a module. Yeah, it's somewhat confusing:-(.
259 259
260 260 if user_ns is None:
261 261 # Set __name__ to __main__ to better match the behavior of the
262 262 # normal interpreter.
263 263 user_ns = {'__name__' :'__main__',
264 264 '__builtins__' : __builtin__,
265 265 }
266 266
267 267 if user_global_ns is None:
268 268 user_global_ns = {}
269 269
270 270 # Assign namespaces
271 271 # This is the namespace where all normal user variables live
272 272 self.user_ns = user_ns
273 273 # Embedded instances require a separate namespace for globals.
274 274 # Normally this one is unused by non-embedded instances.
275 275 self.user_global_ns = user_global_ns
276 276 # A namespace to keep track of internal data structures to prevent
277 277 # them from cluttering user-visible stuff. Will be updated later
278 278 self.internal_ns = {}
279 279
280 280 # Namespace of system aliases. Each entry in the alias
281 281 # table must be a 2-tuple of the form (N,name), where N is the number
282 282 # of positional arguments of the alias.
283 283 self.alias_table = {}
284 284
285 285 # A table holding all the namespaces IPython deals with, so that
286 286 # introspection facilities can search easily.
287 287 self.ns_table = {'user':user_ns,
288 288 'user_global':user_global_ns,
289 289 'alias':self.alias_table,
290 290 'internal':self.internal_ns,
291 291 'builtin':__builtin__.__dict__
292 292 }
293 293
294 294 # The user namespace MUST have a pointer to the shell itself.
295 295 self.user_ns[name] = self
296 296
297 297 # We need to insert into sys.modules something that looks like a
298 298 # module but which accesses the IPython namespace, for shelve and
299 299 # pickle to work interactively. Normally they rely on getting
300 300 # everything out of __main__, but for embedding purposes each IPython
301 301 # instance has its own private namespace, so we can't go shoving
302 302 # everything into __main__.
303 303
304 304 # note, however, that we should only do this for non-embedded
305 305 # ipythons, which really mimic the __main__.__dict__ with their own
306 306 # namespace. Embedded instances, on the other hand, should not do
307 307 # this because they need to manage the user local/global namespaces
308 308 # only, but they live within a 'normal' __main__ (meaning, they
309 309 # shouldn't overtake the execution environment of the script they're
310 310 # embedded in).
311 311
312 312 if not embedded:
313 313 try:
314 314 main_name = self.user_ns['__name__']
315 315 except KeyError:
316 316 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
317 317 else:
318 318 #print "pickle hack in place" # dbg
319 319 #print 'main_name:',main_name # dbg
320 320 sys.modules[main_name] = FakeModule(self.user_ns)
321 321
322 322 # List of input with multi-line handling.
323 323 # Fill its zero entry, user counter starts at 1
324 324 self.input_hist = InputList(['\n'])
325 325
326 326 # list of visited directories
327 327 try:
328 328 self.dir_hist = [os.getcwd()]
329 329 except IOError, e:
330 330 self.dir_hist = []
331 331
332 332 # dict of output history
333 333 self.output_hist = {}
334 334
335 335 # dict of things NOT to alias (keywords, builtins and some magics)
336 336 no_alias = {}
337 337 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
338 338 for key in keyword.kwlist + no_alias_magics:
339 339 no_alias[key] = 1
340 340 no_alias.update(__builtin__.__dict__)
341 341 self.no_alias = no_alias
342 342
343 343 # make global variables for user access to these
344 344 self.user_ns['_ih'] = self.input_hist
345 345 self.user_ns['_oh'] = self.output_hist
346 346 self.user_ns['_dh'] = self.dir_hist
347 347
348 348 # user aliases to input and output histories
349 349 self.user_ns['In'] = self.input_hist
350 350 self.user_ns['Out'] = self.output_hist
351 351
352 352 # Object variable to store code object waiting execution. This is
353 353 # used mainly by the multithreaded shells, but it can come in handy in
354 354 # other situations. No need to use a Queue here, since it's a single
355 355 # item which gets cleared once run.
356 356 self.code_to_run = None
357 357
358 358 # escapes for automatic behavior on the command line
359 359 self.ESC_SHELL = '!'
360 360 self.ESC_HELP = '?'
361 361 self.ESC_MAGIC = '%'
362 362 self.ESC_QUOTE = ','
363 363 self.ESC_QUOTE2 = ';'
364 364 self.ESC_PAREN = '/'
365 365
366 366 # And their associated handlers
367 367 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
368 368 self.ESC_QUOTE : self.handle_auto,
369 369 self.ESC_QUOTE2 : self.handle_auto,
370 370 self.ESC_MAGIC : self.handle_magic,
371 371 self.ESC_HELP : self.handle_help,
372 372 self.ESC_SHELL : self.handle_shell_escape,
373 373 }
374 374
375 375 # class initializations
376 376 Magic.__init__(self,self)
377 377
378 378 # Python source parser/formatter for syntax highlighting
379 379 pyformat = PyColorize.Parser().format
380 380 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
381 381
382 382 # hooks holds pointers used for user-side customizations
383 383 self.hooks = Struct()
384 384
385 385 # Set all default hooks, defined in the IPython.hooks module.
386 386 hooks = IPython.hooks
387 387 for hook_name in hooks.__all__:
388 388 self.set_hook(hook_name,getattr(hooks,hook_name))
389 389
390 390 # Flag to mark unconditional exit
391 391 self.exit_now = False
392 392
393 393 self.usage_min = """\
394 394 An enhanced console for Python.
395 395 Some of its features are:
396 396 - Readline support if the readline library is present.
397 397 - Tab completion in the local namespace.
398 398 - Logging of input, see command-line options.
399 399 - System shell escape via ! , eg !ls.
400 400 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
401 401 - Keeps track of locally defined variables via %who, %whos.
402 402 - Show object information with a ? eg ?x or x? (use ?? for more info).
403 403 """
404 404 if usage: self.usage = usage
405 405 else: self.usage = self.usage_min
406 406
407 407 # Storage
408 408 self.rc = rc # This will hold all configuration information
409 409 self.pager = 'less'
410 410 # temporary files used for various purposes. Deleted at exit.
411 411 self.tempfiles = []
412 412
413 413 # Keep track of readline usage (later set by init_readline)
414 414 self.has_readline = False
415 415
416 416 # template for logfile headers. It gets resolved at runtime by the
417 417 # logstart method.
418 418 self.loghead_tpl = \
419 419 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
420 420 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
421 421 #log# opts = %s
422 422 #log# args = %s
423 423 #log# It is safe to make manual edits below here.
424 424 #log#-----------------------------------------------------------------------
425 425 """
426 426 # for pushd/popd management
427 427 try:
428 428 self.home_dir = get_home_dir()
429 429 except HomeDirError,msg:
430 430 fatal(msg)
431 431
432 432 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
433 433
434 434 # Functions to call the underlying shell.
435 435
436 436 # utility to expand user variables via Itpl
437 437 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
438 438 self.user_ns))
439 439 # The first is similar to os.system, but it doesn't return a value,
440 440 # and it allows interpolation of variables in the user's namespace.
441 441 self.system = lambda cmd: shell(self.var_expand(cmd),
442 442 header='IPython system call: ',
443 443 verbose=self.rc.system_verbose)
444 444 # These are for getoutput and getoutputerror:
445 445 self.getoutput = lambda cmd: \
446 446 getoutput(self.var_expand(cmd),
447 447 header='IPython system call: ',
448 448 verbose=self.rc.system_verbose)
449 449 self.getoutputerror = lambda cmd: \
450 450 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
451 451 self.user_ns)),
452 452 header='IPython system call: ',
453 453 verbose=self.rc.system_verbose)
454 454
455 455 # RegExp for splitting line contents into pre-char//first
456 456 # word-method//rest. For clarity, each group in on one line.
457 457
458 458 # WARNING: update the regexp if the above escapes are changed, as they
459 459 # are hardwired in.
460 460
461 461 # Don't get carried away with trying to make the autocalling catch too
462 462 # much: it's better to be conservative rather than to trigger hidden
463 463 # evals() somewhere and end up causing side effects.
464 464
465 465 self.line_split = re.compile(r'^([\s*,;/])'
466 466 r'([\?\w\.]+\w*\s*)'
467 467 r'(\(?.*$)')
468 468
469 469 # Original re, keep around for a while in case changes break something
470 470 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
471 471 # r'(\s*[\?\w\.]+\w*\s*)'
472 472 # r'(\(?.*$)')
473 473
474 474 # RegExp to identify potential function names
475 475 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
476 476 # RegExp to exclude strings with this start from autocalling
477 477 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
478 478
479 479 # try to catch also methods for stuff in lists/tuples/dicts: off
480 480 # (experimental). For this to work, the line_split regexp would need
481 481 # to be modified so it wouldn't break things at '['. That line is
482 482 # nasty enough that I shouldn't change it until I can test it _well_.
483 483 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
484 484
485 485 # keep track of where we started running (mainly for crash post-mortem)
486 486 self.starting_dir = os.getcwd()
487 487
488 488 # Various switches which can be set
489 489 self.CACHELENGTH = 5000 # this is cheap, it's just text
490 490 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
491 491 self.banner2 = banner2
492 492
493 493 # TraceBack handlers:
494 494
495 495 # Syntax error handler.
496 496 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
497 497
498 498 # The interactive one is initialized with an offset, meaning we always
499 499 # want to remove the topmost item in the traceback, which is our own
500 500 # internal code. Valid modes: ['Plain','Context','Verbose']
501 501 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
502 502 color_scheme='NoColor',
503 503 tb_offset = 1)
504 504
505 505 # IPython itself shouldn't crash. This will produce a detailed
506 506 # post-mortem if it does. But we only install the crash handler for
507 507 # non-threaded shells, the threaded ones use a normal verbose reporter
508 508 # and lose the crash handler. This is because exceptions in the main
509 509 # thread (such as in GUI code) propagate directly to sys.excepthook,
510 510 # and there's no point in printing crash dumps for every user exception.
511 511 if self.isthreaded:
512 512 sys.excepthook = ultraTB.FormattedTB()
513 513 else:
514 514 from IPython import CrashHandler
515 515 sys.excepthook = CrashHandler.CrashHandler(self)
516 516
517 517 # The instance will store a pointer to this, so that runtime code
518 518 # (such as magics) can access it. This is because during the
519 519 # read-eval loop, it gets temporarily overwritten (to deal with GUI
520 520 # frameworks).
521 521 self.sys_excepthook = sys.excepthook
522 522
523 523 # and add any custom exception handlers the user may have specified
524 524 self.set_custom_exc(*custom_exceptions)
525 525
526 526 # Object inspector
527 527 self.inspector = OInspect.Inspector(OInspect.InspectColors,
528 528 PyColorize.ANSICodeColors,
529 529 'NoColor')
530 530 # indentation management
531 531 self.autoindent = False
532 532 self.indent_current_nsp = 0
533 533 self.indent_current = '' # actual indent string
534 534
535 535 # Make some aliases automatically
536 536 # Prepare list of shell aliases to auto-define
537 537 if os.name == 'posix':
538 538 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
539 539 'mv mv -i','rm rm -i','cp cp -i',
540 540 'cat cat','less less','clear clear',
541 541 # a better ls
542 542 'ls ls -F',
543 543 # long ls
544 544 'll ls -lF',
545 545 # color ls
546 546 'lc ls -F -o --color',
547 547 # ls normal files only
548 548 'lf ls -F -o --color %l | grep ^-',
549 549 # ls symbolic links
550 550 'lk ls -F -o --color %l | grep ^l',
551 551 # directories or links to directories,
552 552 'ldir ls -F -o --color %l | grep /$',
553 553 # things which are executable
554 554 'lx ls -F -o --color %l | grep ^-..x',
555 555 )
556 556 elif os.name in ['nt','dos']:
557 557 auto_alias = ('dir dir /on', 'ls dir /on',
558 558 'ddir dir /ad /on', 'ldir dir /ad /on',
559 559 'mkdir mkdir','rmdir rmdir','echo echo',
560 560 'ren ren','cls cls','copy copy')
561 561 else:
562 562 auto_alias = ()
563 563 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
564 564 # Call the actual (public) initializer
565 565 self.init_auto_alias()
566 566 # end __init__
567 567
568 568 def post_config_initialization(self):
569 569 """Post configuration init method
570 570
571 571 This is called after the configuration files have been processed to
572 572 'finalize' the initialization."""
573 573
574 574 rc = self.rc
575 575
576 576 # Load readline proper
577 577 if rc.readline:
578 578 self.init_readline()
579 579
580 580 # log system
581 581 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
582 582 # local shortcut, this is used a LOT
583 583 self.log = self.logger.log
584 584
585 585 # Initialize cache, set in/out prompts and printing system
586 586 self.outputcache = CachedOutput(self,
587 587 rc.cache_size,
588 588 rc.pprint,
589 589 input_sep = rc.separate_in,
590 590 output_sep = rc.separate_out,
591 591 output_sep2 = rc.separate_out2,
592 592 ps1 = rc.prompt_in1,
593 593 ps2 = rc.prompt_in2,
594 594 ps_out = rc.prompt_out,
595 595 pad_left = rc.prompts_pad_left)
596 596
597 597 # user may have over-ridden the default print hook:
598 598 try:
599 599 self.outputcache.__class__.display = self.hooks.display
600 600 except AttributeError:
601 601 pass
602 602
603 603 # I don't like assigning globally to sys, because it means when embedding
604 604 # instances, each embedded instance overrides the previous choice. But
605 605 # sys.displayhook seems to be called internally by exec, so I don't see a
606 606 # way around it.
607 607 sys.displayhook = self.outputcache
608 608
609 609 # Set user colors (don't do it in the constructor above so that it
610 610 # doesn't crash if colors option is invalid)
611 611 self.magic_colors(rc.colors)
612 612
613 613 # Set calling of pdb on exceptions
614 614 self.call_pdb = rc.pdb
615 615
616 616 # Load user aliases
617 617 for alias in rc.alias:
618 618 self.magic_alias(alias)
619 619
620 620 # dynamic data that survives through sessions
621 621 # XXX make the filename a config option?
622 622 persist_base = 'persist'
623 623 if rc.profile:
624 624 persist_base += '_%s' % rc.profile
625 625 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
626 626
627 627 try:
628 628 self.persist = pickle.load(file(self.persist_fname))
629 629 except:
630 630 self.persist = {}
631 631
632 632
633 633 for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]:
634 634 try:
635 635 obj = pickle.loads(value)
636 636 except:
637 637
638 638 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key
639 639 print "The error was:",sys.exc_info()[0]
640 640 continue
641 641
642 642
643 643 self.user_ns[key] = obj
644 644
645 645 def add_builtins(self):
646 646 """Store ipython references into the builtin namespace.
647 647
648 648 Some parts of ipython operate via builtins injected here, which hold a
649 649 reference to IPython itself."""
650 650
651 651 builtins_new = dict(__IPYTHON__ = self,
652 652 ip_set_hook = self.set_hook,
653 653 jobs = self.jobs,
654 654 ipmagic = self.ipmagic,
655 655 ipalias = self.ipalias,
656 656 ipsystem = self.ipsystem,
657 657 )
658 658 for biname,bival in builtins_new.items():
659 659 try:
660 660 # store the orignal value so we can restore it
661 661 self.builtins_added[biname] = __builtin__.__dict__[biname]
662 662 except KeyError:
663 663 # or mark that it wasn't defined, and we'll just delete it at
664 664 # cleanup
665 665 self.builtins_added[biname] = Undefined
666 666 __builtin__.__dict__[biname] = bival
667 667
668 668 # Keep in the builtins a flag for when IPython is active. We set it
669 669 # with setdefault so that multiple nested IPythons don't clobber one
670 670 # another. Each will increase its value by one upon being activated,
671 671 # which also gives us a way to determine the nesting level.
672 672 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
673 673
674 674 def clean_builtins(self):
675 675 """Remove any builtins which might have been added by add_builtins, or
676 676 restore overwritten ones to their previous values."""
677 677 for biname,bival in self.builtins_added.items():
678 678 if bival is Undefined:
679 679 del __builtin__.__dict__[biname]
680 680 else:
681 681 __builtin__.__dict__[biname] = bival
682 682 self.builtins_added.clear()
683 683
684 684 def set_hook(self,name,hook):
685 685 """set_hook(name,hook) -> sets an internal IPython hook.
686 686
687 687 IPython exposes some of its internal API as user-modifiable hooks. By
688 688 resetting one of these hooks, you can modify IPython's behavior to
689 689 call at runtime your own routines."""
690 690
691 691 # At some point in the future, this should validate the hook before it
692 692 # accepts it. Probably at least check that the hook takes the number
693 693 # of args it's supposed to.
694 694 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
695 695
696 696 def set_custom_exc(self,exc_tuple,handler):
697 697 """set_custom_exc(exc_tuple,handler)
698 698
699 699 Set a custom exception handler, which will be called if any of the
700 700 exceptions in exc_tuple occur in the mainloop (specifically, in the
701 701 runcode() method.
702 702
703 703 Inputs:
704 704
705 705 - exc_tuple: a *tuple* of valid exceptions to call the defined
706 706 handler for. It is very important that you use a tuple, and NOT A
707 707 LIST here, because of the way Python's except statement works. If
708 708 you only want to trap a single exception, use a singleton tuple:
709 709
710 710 exc_tuple == (MyCustomException,)
711 711
712 712 - handler: this must be defined as a function with the following
713 713 basic interface: def my_handler(self,etype,value,tb).
714 714
715 715 This will be made into an instance method (via new.instancemethod)
716 716 of IPython itself, and it will be called if any of the exceptions
717 717 listed in the exc_tuple are caught. If the handler is None, an
718 718 internal basic one is used, which just prints basic info.
719 719
720 720 WARNING: by putting in your own exception handler into IPython's main
721 721 execution loop, you run a very good chance of nasty crashes. This
722 722 facility should only be used if you really know what you are doing."""
723 723
724 724 assert type(exc_tuple)==type(()) , \
725 725 "The custom exceptions must be given AS A TUPLE."
726 726
727 727 def dummy_handler(self,etype,value,tb):
728 728 print '*** Simple custom exception handler ***'
729 729 print 'Exception type :',etype
730 730 print 'Exception value:',value
731 731 print 'Traceback :',tb
732 732 print 'Source code :','\n'.join(self.buffer)
733 733
734 734 if handler is None: handler = dummy_handler
735 735
736 736 self.CustomTB = new.instancemethod(handler,self,self.__class__)
737 737 self.custom_exceptions = exc_tuple
738 738
739 739 def set_custom_completer(self,completer,pos=0):
740 740 """set_custom_completer(completer,pos=0)
741 741
742 742 Adds a new custom completer function.
743 743
744 744 The position argument (defaults to 0) is the index in the completers
745 745 list where you want the completer to be inserted."""
746 746
747 747 newcomp = new.instancemethod(completer,self.Completer,
748 748 self.Completer.__class__)
749 749 self.Completer.matchers.insert(pos,newcomp)
750 750
751 751 def _get_call_pdb(self):
752 752 return self._call_pdb
753 753
754 754 def _set_call_pdb(self,val):
755 755
756 756 if val not in (0,1,False,True):
757 757 raise ValueError,'new call_pdb value must be boolean'
758 758
759 759 # store value in instance
760 760 self._call_pdb = val
761 761
762 762 # notify the actual exception handlers
763 763 self.InteractiveTB.call_pdb = val
764 764 if self.isthreaded:
765 765 try:
766 766 self.sys_excepthook.call_pdb = val
767 767 except:
768 768 warn('Failed to activate pdb for threaded exception handler')
769 769
770 770 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
771 771 'Control auto-activation of pdb at exceptions')
772 772
773 773
774 774 # These special functions get installed in the builtin namespace, to
775 775 # provide programmatic (pure python) access to magics, aliases and system
776 776 # calls. This is important for logging, user scripting, and more.
777 777
778 778 # We are basically exposing, via normal python functions, the three
779 779 # mechanisms in which ipython offers special call modes (magics for
780 780 # internal control, aliases for direct system access via pre-selected
781 781 # names, and !cmd for calling arbitrary system commands).
782 782
783 783 def ipmagic(self,arg_s):
784 784 """Call a magic function by name.
785 785
786 786 Input: a string containing the name of the magic function to call and any
787 787 additional arguments to be passed to the magic.
788 788
789 789 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
790 790 prompt:
791 791
792 792 In[1]: %name -opt foo bar
793 793
794 794 To call a magic without arguments, simply use ipmagic('name').
795 795
796 796 This provides a proper Python function to call IPython's magics in any
797 797 valid Python code you can type at the interpreter, including loops and
798 798 compound statements. It is added by IPython to the Python builtin
799 799 namespace upon initialization."""
800 800
801 801 args = arg_s.split(' ',1)
802 802 magic_name = args[0]
803 803 if magic_name.startswith(self.ESC_MAGIC):
804 804 magic_name = magic_name[1:]
805 805 try:
806 806 magic_args = args[1]
807 807 except IndexError:
808 808 magic_args = ''
809 809 fn = getattr(self,'magic_'+magic_name,None)
810 810 if fn is None:
811 811 error("Magic function `%s` not found." % magic_name)
812 812 else:
813 813 magic_args = self.var_expand(magic_args)
814 814 return fn(magic_args)
815 815
816 816 def ipalias(self,arg_s):
817 817 """Call an alias by name.
818 818
819 819 Input: a string containing the name of the alias to call and any
820 820 additional arguments to be passed to the magic.
821 821
822 822 ipalias('name -opt foo bar') is equivalent to typing at the ipython
823 823 prompt:
824 824
825 825 In[1]: name -opt foo bar
826 826
827 827 To call an alias without arguments, simply use ipalias('name').
828 828
829 829 This provides a proper Python function to call IPython's aliases in any
830 830 valid Python code you can type at the interpreter, including loops and
831 831 compound statements. It is added by IPython to the Python builtin
832 832 namespace upon initialization."""
833 833
834 834 args = arg_s.split(' ',1)
835 835 alias_name = args[0]
836 836 try:
837 837 alias_args = args[1]
838 838 except IndexError:
839 839 alias_args = ''
840 840 if alias_name in self.alias_table:
841 841 self.call_alias(alias_name,alias_args)
842 842 else:
843 843 error("Alias `%s` not found." % alias_name)
844 844
845 845 def ipsystem(self,arg_s):
846 846 """Make a system call, using IPython."""
847 847
848 848 self.system(arg_s)
849 849
850 850 def complete(self,text):
851 851 """Return a sorted list of all possible completions on text.
852 852
853 853 Inputs:
854 854
855 855 - text: a string of text to be completed on.
856 856
857 857 This is a wrapper around the completion mechanism, similar to what
858 858 readline does at the command line when the TAB key is hit. By
859 859 exposing it as a method, it can be used by other non-readline
860 860 environments (such as GUIs) for text completion.
861 861
862 862 Simple usage example:
863 863
864 864 In [1]: x = 'hello'
865 865
866 866 In [2]: __IP.complete('x.l')
867 867 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
868 868
869 869 complete = self.Completer.complete
870 870 state = 0
871 871 # use a dict so we get unique keys, since ipyhton's multiple
872 872 # completers can return duplicates.
873 873 comps = {}
874 874 while True:
875 875 newcomp = complete(text,state)
876 876 if newcomp is None:
877 877 break
878 878 comps[newcomp] = 1
879 879 state += 1
880 880 outcomps = comps.keys()
881 881 outcomps.sort()
882 882 return outcomps
883 883
884 884 def set_completer_frame(self, frame=None):
885 885 if frame:
886 886 self.Completer.namespace = frame.f_locals
887 887 self.Completer.global_namespace = frame.f_globals
888 888 else:
889 889 self.Completer.namespace = self.user_ns
890 890 self.Completer.global_namespace = self.user_global_ns
891 891
892 892 def init_auto_alias(self):
893 893 """Define some aliases automatically.
894 894
895 895 These are ALL parameter-less aliases"""
896 896
897 897 for alias,cmd in self.auto_alias:
898 898 self.alias_table[alias] = (0,cmd)
899 899
900 900 def alias_table_validate(self,verbose=0):
901 901 """Update information about the alias table.
902 902
903 903 In particular, make sure no Python keywords/builtins are in it."""
904 904
905 905 no_alias = self.no_alias
906 906 for k in self.alias_table.keys():
907 907 if k in no_alias:
908 908 del self.alias_table[k]
909 909 if verbose:
910 910 print ("Deleting alias <%s>, it's a Python "
911 911 "keyword or builtin." % k)
912 912
913 913 def set_autoindent(self,value=None):
914 914 """Set the autoindent flag, checking for readline support.
915 915
916 916 If called with no arguments, it acts as a toggle."""
917 917
918 918 if not self.has_readline:
919 919 if os.name == 'posix':
920 920 warn("The auto-indent feature requires the readline library")
921 921 self.autoindent = 0
922 922 return
923 923 if value is None:
924 924 self.autoindent = not self.autoindent
925 925 else:
926 926 self.autoindent = value
927 927
928 928 def rc_set_toggle(self,rc_field,value=None):
929 929 """Set or toggle a field in IPython's rc config. structure.
930 930
931 931 If called with no arguments, it acts as a toggle.
932 932
933 933 If called with a non-existent field, the resulting AttributeError
934 934 exception will propagate out."""
935 935
936 936 rc_val = getattr(self.rc,rc_field)
937 937 if value is None:
938 938 value = not rc_val
939 939 setattr(self.rc,rc_field,value)
940 940
941 941 def user_setup(self,ipythondir,rc_suffix,mode='install'):
942 942 """Install the user configuration directory.
943 943
944 944 Can be called when running for the first time or to upgrade the user's
945 945 .ipython/ directory with the mode parameter. Valid modes are 'install'
946 946 and 'upgrade'."""
947 947
948 948 def wait():
949 949 try:
950 950 raw_input("Please press <RETURN> to start IPython.")
951 951 except EOFError:
952 952 print >> Term.cout
953 953 print '*'*70
954 954
955 955 cwd = os.getcwd() # remember where we started
956 956 glb = glob.glob
957 957 print '*'*70
958 958 if mode == 'install':
959 959 print \
960 960 """Welcome to IPython. I will try to create a personal configuration directory
961 961 where you can customize many aspects of IPython's functionality in:\n"""
962 962 else:
963 963 print 'I am going to upgrade your configuration in:'
964 964
965 965 print ipythondir
966 966
967 967 rcdirend = os.path.join('IPython','UserConfig')
968 968 cfg = lambda d: os.path.join(d,rcdirend)
969 969 try:
970 970 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
971 971 except IOError:
972 972 warning = """
973 973 Installation error. IPython's directory was not found.
974 974
975 975 Check the following:
976 976
977 977 The ipython/IPython directory should be in a directory belonging to your
978 978 PYTHONPATH environment variable (that is, it should be in a directory
979 979 belonging to sys.path). You can copy it explicitly there or just link to it.
980 980
981 981 IPython will proceed with builtin defaults.
982 982 """
983 983 warn(warning)
984 984 wait()
985 985 return
986 986
987 987 if mode == 'install':
988 988 try:
989 989 shutil.copytree(rcdir,ipythondir)
990 990 os.chdir(ipythondir)
991 991 rc_files = glb("ipythonrc*")
992 992 for rc_file in rc_files:
993 993 os.rename(rc_file,rc_file+rc_suffix)
994 994 except:
995 995 warning = """
996 996
997 997 There was a problem with the installation:
998 998 %s
999 999 Try to correct it or contact the developers if you think it's a bug.
1000 1000 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1001 1001 warn(warning)
1002 1002 wait()
1003 1003 return
1004 1004
1005 1005 elif mode == 'upgrade':
1006 1006 try:
1007 1007 os.chdir(ipythondir)
1008 1008 except:
1009 1009 print """
1010 1010 Can not upgrade: changing to directory %s failed. Details:
1011 1011 %s
1012 1012 """ % (ipythondir,sys.exc_info()[1])
1013 1013 wait()
1014 1014 return
1015 1015 else:
1016 1016 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1017 1017 for new_full_path in sources:
1018 1018 new_filename = os.path.basename(new_full_path)
1019 1019 if new_filename.startswith('ipythonrc'):
1020 1020 new_filename = new_filename + rc_suffix
1021 1021 # The config directory should only contain files, skip any
1022 1022 # directories which may be there (like CVS)
1023 1023 if os.path.isdir(new_full_path):
1024 1024 continue
1025 1025 if os.path.exists(new_filename):
1026 1026 old_file = new_filename+'.old'
1027 1027 if os.path.exists(old_file):
1028 1028 os.remove(old_file)
1029 1029 os.rename(new_filename,old_file)
1030 1030 shutil.copy(new_full_path,new_filename)
1031 1031 else:
1032 1032 raise ValueError,'unrecognized mode for install:',`mode`
1033 1033
1034 1034 # Fix line-endings to those native to each platform in the config
1035 1035 # directory.
1036 1036 try:
1037 1037 os.chdir(ipythondir)
1038 1038 except:
1039 1039 print """
1040 1040 Problem: changing to directory %s failed.
1041 1041 Details:
1042 1042 %s
1043 1043
1044 1044 Some configuration files may have incorrect line endings. This should not
1045 1045 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1046 1046 wait()
1047 1047 else:
1048 1048 for fname in glb('ipythonrc*'):
1049 1049 try:
1050 1050 native_line_ends(fname,backup=0)
1051 1051 except IOError:
1052 1052 pass
1053 1053
1054 1054 if mode == 'install':
1055 1055 print """
1056 1056 Successful installation!
1057 1057
1058 1058 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1059 1059 IPython manual (there are both HTML and PDF versions supplied with the
1060 1060 distribution) to make sure that your system environment is properly configured
1061 1061 to take advantage of IPython's features."""
1062 1062 else:
1063 1063 print """
1064 1064 Successful upgrade!
1065 1065
1066 1066 All files in your directory:
1067 1067 %(ipythondir)s
1068 1068 which would have been overwritten by the upgrade were backed up with a .old
1069 1069 extension. If you had made particular customizations in those files you may
1070 1070 want to merge them back into the new files.""" % locals()
1071 1071 wait()
1072 1072 os.chdir(cwd)
1073 1073 # end user_setup()
1074 1074
1075 1075 def atexit_operations(self):
1076 1076 """This will be executed at the time of exit.
1077 1077
1078 1078 Saving of persistent data should be performed here. """
1079 1079
1080 1080 # input history
1081 1081 self.savehist()
1082 1082
1083 1083 # Cleanup all tempfiles left around
1084 1084 for tfile in self.tempfiles:
1085 1085 try:
1086 1086 os.unlink(tfile)
1087 1087 except OSError:
1088 1088 pass
1089 1089
1090 1090 # save the "persistent data" catch-all dictionary
1091 1091 try:
1092 1092 pickle.dump(self.persist, open(self.persist_fname,"w"))
1093 1093 except:
1094 1094 print "*** ERROR *** persistent data saving failed."
1095 1095
1096 1096 def savehist(self):
1097 1097 """Save input history to a file (via readline library)."""
1098 1098 try:
1099 1099 self.readline.write_history_file(self.histfile)
1100 1100 except:
1101 1101 print 'Unable to save IPython command history to file: ' + \
1102 1102 `self.histfile`
1103 1103
1104 1104 def pre_readline(self):
1105 1105 """readline hook to be used at the start of each line.
1106 1106
1107 1107 Currently it handles auto-indent only."""
1108 1108
1109 1109 self.readline.insert_text(self.indent_current)
1110 1110
1111 1111 def init_readline(self):
1112 1112 """Command history completion/saving/reloading."""
1113 1113 try:
1114 1114 import readline
1115 1115 except ImportError:
1116 1116 self.has_readline = 0
1117 1117 self.readline = None
1118 1118 # no point in bugging windows users with this every time:
1119 1119 if os.name == 'posix':
1120 1120 warn('Readline services not available on this platform.')
1121 1121 else:
1122 1122 import atexit
1123 1123 from IPython.completer import IPCompleter
1124 1124 self.Completer = IPCompleter(self,
1125 1125 self.user_ns,
1126 1126 self.user_global_ns,
1127 1127 self.rc.readline_omit__names,
1128 1128 self.alias_table)
1129 1129
1130 1130 # Platform-specific configuration
1131 1131 if os.name == 'nt':
1132 1132 self.readline_startup_hook = readline.set_pre_input_hook
1133 1133 else:
1134 1134 self.readline_startup_hook = readline.set_startup_hook
1135 1135
1136 1136 # Load user's initrc file (readline config)
1137 1137 inputrc_name = os.environ.get('INPUTRC')
1138 1138 if inputrc_name is None:
1139 1139 home_dir = get_home_dir()
1140 1140 if home_dir is not None:
1141 1141 inputrc_name = os.path.join(home_dir,'.inputrc')
1142 1142 if os.path.isfile(inputrc_name):
1143 1143 try:
1144 1144 readline.read_init_file(inputrc_name)
1145 1145 except:
1146 1146 warn('Problems reading readline initialization file <%s>'
1147 1147 % inputrc_name)
1148 1148
1149 1149 self.has_readline = 1
1150 1150 self.readline = readline
1151 1151 # save this in sys so embedded copies can restore it properly
1152 1152 sys.ipcompleter = self.Completer.complete
1153 1153 readline.set_completer(self.Completer.complete)
1154 1154
1155 1155 # Configure readline according to user's prefs
1156 1156 for rlcommand in self.rc.readline_parse_and_bind:
1157 1157 readline.parse_and_bind(rlcommand)
1158 1158
1159 1159 # remove some chars from the delimiters list
1160 1160 delims = readline.get_completer_delims()
1161 1161 delims = delims.translate(string._idmap,
1162 1162 self.rc.readline_remove_delims)
1163 1163 readline.set_completer_delims(delims)
1164 1164 # otherwise we end up with a monster history after a while:
1165 1165 readline.set_history_length(1000)
1166 1166 try:
1167 1167 #print '*** Reading readline history' # dbg
1168 1168 readline.read_history_file(self.histfile)
1169 1169 except IOError:
1170 1170 pass # It doesn't exist yet.
1171 1171
1172 1172 atexit.register(self.atexit_operations)
1173 1173 del atexit
1174 1174
1175 1175 # Configure auto-indent for all platforms
1176 1176 self.set_autoindent(self.rc.autoindent)
1177 1177
1178 1178 def _should_recompile(self,e):
1179 1179 """Utility routine for edit_syntax_error"""
1180 1180
1181 1181 if e.filename in ('<ipython console>','<input>','<string>',
1182 1182 '<console>',None):
1183 1183
1184 1184 return False
1185 1185 try:
1186 1186 if not ask_yes_no('Return to editor to correct syntax error? '
1187 1187 '[Y/n] ','y'):
1188 1188 return False
1189 1189 except EOFError:
1190 1190 return False
1191 1191
1192 1192 def int0(x):
1193 1193 try:
1194 1194 return int(x)
1195 1195 except TypeError:
1196 1196 return 0
1197 1197 # always pass integer line and offset values to editor hook
1198 1198 self.hooks.fix_error_editor(e.filename,
1199 1199 int0(e.lineno),int0(e.offset),e.msg)
1200 1200 return True
1201 1201
1202 1202 def edit_syntax_error(self):
1203 1203 """The bottom half of the syntax error handler called in the main loop.
1204 1204
1205 1205 Loop until syntax error is fixed or user cancels.
1206 1206 """
1207 1207
1208 1208 while self.SyntaxTB.last_syntax_error:
1209 1209 # copy and clear last_syntax_error
1210 1210 err = self.SyntaxTB.clear_err_state()
1211 1211 if not self._should_recompile(err):
1212 1212 return
1213 1213 try:
1214 1214 # may set last_syntax_error again if a SyntaxError is raised
1215 1215 self.safe_execfile(err.filename,self.shell.user_ns)
1216 1216 except:
1217 1217 self.showtraceback()
1218 1218 else:
1219 1219 f = file(err.filename)
1220 1220 try:
1221 1221 sys.displayhook(f.read())
1222 1222 finally:
1223 1223 f.close()
1224 1224
1225 1225 def showsyntaxerror(self, filename=None):
1226 1226 """Display the syntax error that just occurred.
1227 1227
1228 1228 This doesn't display a stack trace because there isn't one.
1229 1229
1230 1230 If a filename is given, it is stuffed in the exception instead
1231 1231 of what was there before (because Python's parser always uses
1232 1232 "<string>" when reading from a string).
1233 1233 """
1234 1234 etype, value, last_traceback = sys.exc_info()
1235 1235 if filename and etype is SyntaxError:
1236 1236 # Work hard to stuff the correct filename in the exception
1237 1237 try:
1238 1238 msg, (dummy_filename, lineno, offset, line) = value
1239 1239 except:
1240 1240 # Not the format we expect; leave it alone
1241 1241 pass
1242 1242 else:
1243 1243 # Stuff in the right filename
1244 1244 try:
1245 1245 # Assume SyntaxError is a class exception
1246 1246 value = SyntaxError(msg, (filename, lineno, offset, line))
1247 1247 except:
1248 1248 # If that failed, assume SyntaxError is a string
1249 1249 value = msg, (filename, lineno, offset, line)
1250 1250 self.SyntaxTB(etype,value,[])
1251 1251
1252 1252 def debugger(self):
1253 1253 """Call the pdb debugger."""
1254 1254
1255 1255 if not self.rc.pdb:
1256 1256 return
1257 1257 pdb.pm()
1258 1258
1259 1259 def showtraceback(self,exc_tuple = None,filename=None):
1260 1260 """Display the exception that just occurred."""
1261 1261
1262 1262 # Though this won't be called by syntax errors in the input line,
1263 1263 # there may be SyntaxError cases whith imported code.
1264 1264 if exc_tuple is None:
1265 1265 type, value, tb = sys.exc_info()
1266 1266 else:
1267 1267 type, value, tb = exc_tuple
1268 1268 if type is SyntaxError:
1269 1269 self.showsyntaxerror(filename)
1270 1270 else:
1271 1271 self.InteractiveTB()
1272 1272 if self.InteractiveTB.call_pdb and self.has_readline:
1273 1273 # pdb mucks up readline, fix it back
1274 1274 self.readline.set_completer(self.Completer.complete)
1275 1275
1276 1276 def mainloop(self,banner=None):
1277 1277 """Creates the local namespace and starts the mainloop.
1278 1278
1279 1279 If an optional banner argument is given, it will override the
1280 1280 internally created default banner."""
1281 1281
1282 1282 if self.rc.c: # Emulate Python's -c option
1283 1283 self.exec_init_cmd()
1284 1284 if banner is None:
1285 1285 if self.rc.banner:
1286 1286 banner = self.BANNER+self.banner2
1287 1287 else:
1288 1288 banner = ''
1289 1289 self.interact(banner)
1290 1290
1291 1291 def exec_init_cmd(self):
1292 1292 """Execute a command given at the command line.
1293 1293
1294 1294 This emulates Python's -c option."""
1295 1295
1296 1296 sys.argv = ['-c']
1297 1297 self.push(self.rc.c)
1298 1298
1299 1299 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1300 1300 """Embeds IPython into a running python program.
1301 1301
1302 1302 Input:
1303 1303
1304 1304 - header: An optional header message can be specified.
1305 1305
1306 1306 - local_ns, global_ns: working namespaces. If given as None, the
1307 1307 IPython-initialized one is updated with __main__.__dict__, so that
1308 1308 program variables become visible but user-specific configuration
1309 1309 remains possible.
1310 1310
1311 1311 - stack_depth: specifies how many levels in the stack to go to
1312 1312 looking for namespaces (when local_ns and global_ns are None). This
1313 1313 allows an intermediate caller to make sure that this function gets
1314 1314 the namespace from the intended level in the stack. By default (0)
1315 1315 it will get its locals and globals from the immediate caller.
1316 1316
1317 1317 Warning: it's possible to use this in a program which is being run by
1318 1318 IPython itself (via %run), but some funny things will happen (a few
1319 1319 globals get overwritten). In the future this will be cleaned up, as
1320 1320 there is no fundamental reason why it can't work perfectly."""
1321 1321
1322 1322 # Get locals and globals from caller
1323 1323 if local_ns is None or global_ns is None:
1324 1324 call_frame = sys._getframe(stack_depth).f_back
1325 1325
1326 1326 if local_ns is None:
1327 1327 local_ns = call_frame.f_locals
1328 1328 if global_ns is None:
1329 1329 global_ns = call_frame.f_globals
1330 1330
1331 1331 # Update namespaces and fire up interpreter
1332 1332
1333 1333 # The global one is easy, we can just throw it in
1334 1334 self.user_global_ns = global_ns
1335 1335
1336 1336 # but the user/local one is tricky: ipython needs it to store internal
1337 1337 # data, but we also need the locals. We'll copy locals in the user
1338 1338 # one, but will track what got copied so we can delete them at exit.
1339 1339 # This is so that a later embedded call doesn't see locals from a
1340 1340 # previous call (which most likely existed in a separate scope).
1341 1341 local_varnames = local_ns.keys()
1342 1342 self.user_ns.update(local_ns)
1343 1343
1344 1344 # Patch for global embedding to make sure that things don't overwrite
1345 1345 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1346 1346 # FIXME. Test this a bit more carefully (the if.. is new)
1347 1347 if local_ns is None and global_ns is None:
1348 1348 self.user_global_ns.update(__main__.__dict__)
1349 1349
1350 1350 # make sure the tab-completer has the correct frame information, so it
1351 1351 # actually completes using the frame's locals/globals
1352 1352 self.set_completer_frame()
1353 1353
1354 1354 # before activating the interactive mode, we need to make sure that
1355 1355 # all names in the builtin namespace needed by ipython point to
1356 1356 # ourselves, and not to other instances.
1357 1357 self.add_builtins()
1358 1358
1359 1359 self.interact(header)
1360 1360
1361 1361 # now, purge out the user namespace from anything we might have added
1362 1362 # from the caller's local namespace
1363 1363 delvar = self.user_ns.pop
1364 1364 for var in local_varnames:
1365 1365 delvar(var,None)
1366 1366 # and clean builtins we may have overridden
1367 1367 self.clean_builtins()
1368 1368
1369 1369 def interact(self, banner=None):
1370 1370 """Closely emulate the interactive Python console.
1371 1371
1372 1372 The optional banner argument specify the banner to print
1373 1373 before the first interaction; by default it prints a banner
1374 1374 similar to the one printed by the real Python interpreter,
1375 1375 followed by the current class name in parentheses (so as not
1376 1376 to confuse this with the real interpreter -- since it's so
1377 1377 close!).
1378 1378
1379 1379 """
1380 1380 cprt = 'Type "copyright", "credits" or "license" for more information.'
1381 1381 if banner is None:
1382 1382 self.write("Python %s on %s\n%s\n(%s)\n" %
1383 1383 (sys.version, sys.platform, cprt,
1384 1384 self.__class__.__name__))
1385 1385 else:
1386 1386 self.write(banner)
1387 1387
1388 1388 more = 0
1389 1389
1390 1390 # Mark activity in the builtins
1391 1391 __builtin__.__dict__['__IPYTHON__active'] += 1
1392 1392
1393 1393 # exit_now is set by a call to %Exit or %Quit
1394 1394 self.exit_now = False
1395 1395 while not self.exit_now:
1396 1396
1397 1397 try:
1398 1398 if more:
1399 1399 prompt = self.outputcache.prompt2
1400 1400 if self.autoindent:
1401 1401 self.readline_startup_hook(self.pre_readline)
1402 1402 else:
1403 1403 prompt = self.outputcache.prompt1
1404 1404 try:
1405 1405 line = self.raw_input(prompt,more)
1406 1406 if self.autoindent:
1407 1407 self.readline_startup_hook(None)
1408 1408 except EOFError:
1409 1409 if self.autoindent:
1410 1410 self.readline_startup_hook(None)
1411 1411 self.write("\n")
1412 1412 self.exit()
1413 1413 else:
1414 1414 more = self.push(line)
1415 1415
1416 1416 if (self.SyntaxTB.last_syntax_error and
1417 1417 self.rc.autoedit_syntax):
1418 1418 self.edit_syntax_error()
1419 1419
1420 1420 except KeyboardInterrupt:
1421 1421 self.write("\nKeyboardInterrupt\n")
1422 1422 self.resetbuffer()
1423 1423 more = 0
1424 1424 # keep cache in sync with the prompt counter:
1425 1425 self.outputcache.prompt_count -= 1
1426 1426
1427 1427 if self.autoindent:
1428 1428 self.indent_current_nsp = 0
1429 1429 self.indent_current = ' '* self.indent_current_nsp
1430 1430
1431 1431 except bdb.BdbQuit:
1432 1432 warn("The Python debugger has exited with a BdbQuit exception.\n"
1433 1433 "Because of how pdb handles the stack, it is impossible\n"
1434 1434 "for IPython to properly format this particular exception.\n"
1435 1435 "IPython will resume normal operation.")
1436 1436
1437 1437 # We are off again...
1438 1438 __builtin__.__dict__['__IPYTHON__active'] -= 1
1439 1439
1440 1440 def excepthook(self, type, value, tb):
1441 1441 """One more defense for GUI apps that call sys.excepthook.
1442 1442
1443 1443 GUI frameworks like wxPython trap exceptions and call
1444 1444 sys.excepthook themselves. I guess this is a feature that
1445 1445 enables them to keep running after exceptions that would
1446 1446 otherwise kill their mainloop. This is a bother for IPython
1447 1447 which excepts to catch all of the program exceptions with a try:
1448 1448 except: statement.
1449 1449
1450 1450 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1451 1451 any app directly invokes sys.excepthook, it will look to the user like
1452 1452 IPython crashed. In order to work around this, we can disable the
1453 1453 CrashHandler and replace it with this excepthook instead, which prints a
1454 1454 regular traceback using our InteractiveTB. In this fashion, apps which
1455 1455 call sys.excepthook will generate a regular-looking exception from
1456 1456 IPython, and the CrashHandler will only be triggered by real IPython
1457 1457 crashes.
1458 1458
1459 1459 This hook should be used sparingly, only in places which are not likely
1460 1460 to be true IPython errors.
1461 1461 """
1462 1462
1463 1463 self.InteractiveTB(type, value, tb, tb_offset=0)
1464 1464 if self.InteractiveTB.call_pdb and self.has_readline:
1465 1465 self.readline.set_completer(self.Completer.complete)
1466 1466
1467 1467 def call_alias(self,alias,rest=''):
1468 1468 """Call an alias given its name and the rest of the line.
1469 1469
1470 1470 This function MUST be given a proper alias, because it doesn't make
1471 1471 any checks when looking up into the alias table. The caller is
1472 1472 responsible for invoking it only with a valid alias."""
1473 1473
1474 1474 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1475 1475 nargs,cmd = self.alias_table[alias]
1476 1476 # Expand the %l special to be the user's input line
1477 1477 if cmd.find('%l') >= 0:
1478 1478 cmd = cmd.replace('%l',rest)
1479 1479 rest = ''
1480 1480 if nargs==0:
1481 1481 # Simple, argument-less aliases
1482 1482 cmd = '%s %s' % (cmd,rest)
1483 1483 else:
1484 1484 # Handle aliases with positional arguments
1485 1485 args = rest.split(None,nargs)
1486 1486 if len(args)< nargs:
1487 1487 error('Alias <%s> requires %s arguments, %s given.' %
1488 1488 (alias,nargs,len(args)))
1489 1489 return
1490 1490 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1491 1491 # Now call the macro, evaluating in the user's namespace
1492 1492 try:
1493 1493 self.system(cmd)
1494 1494 except:
1495 1495 self.showtraceback()
1496 1496
1497 1497 def autoindent_update(self,line):
1498 1498 """Keep track of the indent level."""
1499 1499 if self.autoindent:
1500 1500 if line:
1501 1501 ini_spaces = ini_spaces_re.match(line)
1502 1502 if ini_spaces:
1503 1503 nspaces = ini_spaces.end()
1504 1504 else:
1505 1505 nspaces = 0
1506 1506 self.indent_current_nsp = nspaces
1507 1507
1508 1508 if line[-1] == ':':
1509 1509 self.indent_current_nsp += 4
1510 1510 elif dedent_re.match(line):
1511 1511 self.indent_current_nsp -= 4
1512 1512 else:
1513 1513 self.indent_current_nsp = 0
1514 1514
1515 1515 # indent_current is the actual string to be inserted
1516 1516 # by the readline hooks for indentation
1517 1517 self.indent_current = ' '* self.indent_current_nsp
1518 1518
1519 1519 def runlines(self,lines):
1520 1520 """Run a string of one or more lines of source.
1521 1521
1522 1522 This method is capable of running a string containing multiple source
1523 1523 lines, as if they had been entered at the IPython prompt. Since it
1524 1524 exposes IPython's processing machinery, the given strings can contain
1525 1525 magic calls (%magic), special shell access (!cmd), etc."""
1526 1526
1527 1527 # We must start with a clean buffer, in case this is run from an
1528 1528 # interactive IPython session (via a magic, for example).
1529 1529 self.resetbuffer()
1530 1530 lines = lines.split('\n')
1531 1531 more = 0
1532 1532 for line in lines:
1533 1533 # skip blank lines so we don't mess up the prompt counter, but do
1534 1534 # NOT skip even a blank line if we are in a code block (more is
1535 1535 # true)
1536 1536 if line or more:
1537 1537 more = self.push(self.prefilter(line,more))
1538 1538 # IPython's runsource returns None if there was an error
1539 1539 # compiling the code. This allows us to stop processing right
1540 1540 # away, so the user gets the error message at the right place.
1541 1541 if more is None:
1542 1542 break
1543 1543 # final newline in case the input didn't have it, so that the code
1544 1544 # actually does get executed
1545 1545 if more:
1546 1546 self.push('\n')
1547 1547
1548 1548 def runsource(self, source, filename='<input>', symbol='single'):
1549 1549 """Compile and run some source in the interpreter.
1550 1550
1551 1551 Arguments are as for compile_command().
1552 1552
1553 1553 One several things can happen:
1554 1554
1555 1555 1) The input is incorrect; compile_command() raised an
1556 1556 exception (SyntaxError or OverflowError). A syntax traceback
1557 1557 will be printed by calling the showsyntaxerror() method.
1558 1558
1559 1559 2) The input is incomplete, and more input is required;
1560 1560 compile_command() returned None. Nothing happens.
1561 1561
1562 1562 3) The input is complete; compile_command() returned a code
1563 1563 object. The code is executed by calling self.runcode() (which
1564 1564 also handles run-time exceptions, except for SystemExit).
1565 1565
1566 1566 The return value is:
1567 1567
1568 1568 - True in case 2
1569 1569
1570 1570 - False in the other cases, unless an exception is raised, where
1571 1571 None is returned instead. This can be used by external callers to
1572 1572 know whether to continue feeding input or not.
1573 1573
1574 1574 The return value can be used to decide whether to use sys.ps1 or
1575 1575 sys.ps2 to prompt the next line."""
1576 1576
1577 1577 try:
1578 1578 code = self.compile(source,filename,symbol)
1579 1579 except (OverflowError, SyntaxError, ValueError):
1580 1580 # Case 1
1581 1581 self.showsyntaxerror(filename)
1582 1582 return None
1583 1583
1584 1584 if code is None:
1585 1585 # Case 2
1586 1586 return True
1587 1587
1588 1588 # Case 3
1589 1589 # We store the code object so that threaded shells and
1590 1590 # custom exception handlers can access all this info if needed.
1591 1591 # The source corresponding to this can be obtained from the
1592 1592 # buffer attribute as '\n'.join(self.buffer).
1593 1593 self.code_to_run = code
1594 1594 # now actually execute the code object
1595 1595 if self.runcode(code) == 0:
1596 1596 return False
1597 1597 else:
1598 1598 return None
1599 1599
1600 1600 def runcode(self,code_obj):
1601 1601 """Execute a code object.
1602 1602
1603 1603 When an exception occurs, self.showtraceback() is called to display a
1604 1604 traceback.
1605 1605
1606 1606 Return value: a flag indicating whether the code to be run completed
1607 1607 successfully:
1608 1608
1609 1609 - 0: successful execution.
1610 1610 - 1: an error occurred.
1611 1611 """
1612 1612
1613 1613 # Set our own excepthook in case the user code tries to call it
1614 1614 # directly, so that the IPython crash handler doesn't get triggered
1615 1615 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1616 1616
1617 1617 # we save the original sys.excepthook in the instance, in case config
1618 1618 # code (such as magics) needs access to it.
1619 1619 self.sys_excepthook = old_excepthook
1620 1620 outflag = 1 # happens in more places, so it's easier as default
1621 1621 try:
1622 1622 try:
1623 1623 # Embedded instances require separate global/local namespaces
1624 1624 # so they can see both the surrounding (local) namespace and
1625 1625 # the module-level globals when called inside another function.
1626 1626 if self.embedded:
1627 1627 exec code_obj in self.user_global_ns, self.user_ns
1628 1628 # Normal (non-embedded) instances should only have a single
1629 1629 # namespace for user code execution, otherwise functions won't
1630 1630 # see interactive top-level globals.
1631 1631 else:
1632 1632 exec code_obj in self.user_ns
1633 1633 finally:
1634 1634 # Reset our crash handler in place
1635 1635 sys.excepthook = old_excepthook
1636 1636 except SystemExit:
1637 1637 self.resetbuffer()
1638 1638 self.showtraceback()
1639 1639 warn("Type exit or quit to exit IPython "
1640 1640 "(%Exit or %Quit do so unconditionally).",level=1)
1641 1641 except self.custom_exceptions:
1642 1642 etype,value,tb = sys.exc_info()
1643 1643 self.CustomTB(etype,value,tb)
1644 1644 except:
1645 1645 self.showtraceback()
1646 1646 else:
1647 1647 outflag = 0
1648 1648 if softspace(sys.stdout, 0):
1649 1649 print
1650 1650 # Flush out code object which has been run (and source)
1651 1651 self.code_to_run = None
1652 1652 return outflag
1653 1653
1654 1654 def push(self, line):
1655 1655 """Push a line to the interpreter.
1656 1656
1657 1657 The line should not have a trailing newline; it may have
1658 1658 internal newlines. The line is appended to a buffer and the
1659 1659 interpreter's runsource() method is called with the
1660 1660 concatenated contents of the buffer as source. If this
1661 1661 indicates that the command was executed or invalid, the buffer
1662 1662 is reset; otherwise, the command is incomplete, and the buffer
1663 1663 is left as it was after the line was appended. The return
1664 1664 value is 1 if more input is required, 0 if the line was dealt
1665 1665 with in some way (this is the same as runsource()).
1666 1666 """
1667 1667
1668 1668 # autoindent management should be done here, and not in the
1669 1669 # interactive loop, since that one is only seen by keyboard input. We
1670 1670 # need this done correctly even for code run via runlines (which uses
1671 1671 # push).
1672 1672
1673 1673 #print 'push line: <%s>' % line # dbg
1674 1674 self.autoindent_update(line)
1675 1675
1676 1676 self.buffer.append(line)
1677 1677 more = self.runsource('\n'.join(self.buffer), self.filename)
1678 1678 if not more:
1679 1679 self.resetbuffer()
1680 1680 return more
1681 1681
1682 1682 def resetbuffer(self):
1683 1683 """Reset the input buffer."""
1684 1684 self.buffer[:] = []
1685 1685
1686 1686 def raw_input(self,prompt='',continue_prompt=False):
1687 1687 """Write a prompt and read a line.
1688 1688
1689 1689 The returned line does not include the trailing newline.
1690 1690 When the user enters the EOF key sequence, EOFError is raised.
1691 1691
1692 1692 Optional inputs:
1693 1693
1694 1694 - prompt(''): a string to be printed to prompt the user.
1695 1695
1696 1696 - continue_prompt(False): whether this line is the first one or a
1697 1697 continuation in a sequence of inputs.
1698 1698 """
1699 1699
1700 1700 line = raw_input_original(prompt)
1701 1701 # Try to be reasonably smart about not re-indenting pasted input more
1702 1702 # than necessary. We do this by trimming out the auto-indent initial
1703 1703 # spaces, if the user's actual input started itself with whitespace.
1704 1704 if self.autoindent:
1705 1705 line2 = line[self.indent_current_nsp:]
1706 1706 if line2[0:1] in (' ','\t'):
1707 1707 line = line2
1708 1708 return self.prefilter(line,continue_prompt)
1709 1709
1710 1710 def split_user_input(self,line):
1711 1711 """Split user input into pre-char, function part and rest."""
1712 1712
1713 1713 lsplit = self.line_split.match(line)
1714 1714 if lsplit is None: # no regexp match returns None
1715 1715 try:
1716 1716 iFun,theRest = line.split(None,1)
1717 1717 except ValueError:
1718 1718 iFun,theRest = line,''
1719 1719 pre = re.match('^(\s*)(.*)',line).groups()[0]
1720 1720 else:
1721 1721 pre,iFun,theRest = lsplit.groups()
1722 1722
1723 1723 #print 'line:<%s>' % line # dbg
1724 1724 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1725 1725 return pre,iFun.strip(),theRest
1726 1726
1727 1727 def _prefilter(self, line, continue_prompt):
1728 1728 """Calls different preprocessors, depending on the form of line."""
1729 1729
1730 1730 # All handlers *must* return a value, even if it's blank ('').
1731 1731
1732 1732 # Lines are NOT logged here. Handlers should process the line as
1733 1733 # needed, update the cache AND log it (so that the input cache array
1734 1734 # stays synced).
1735 1735
1736 1736 # This function is _very_ delicate, and since it's also the one which
1737 1737 # determines IPython's response to user input, it must be as efficient
1738 1738 # as possible. For this reason it has _many_ returns in it, trying
1739 1739 # always to exit as quickly as it can figure out what it needs to do.
1740 1740
1741 1741 # This function is the main responsible for maintaining IPython's
1742 1742 # behavior respectful of Python's semantics. So be _very_ careful if
1743 1743 # making changes to anything here.
1744 1744
1745 1745 #.....................................................................
1746 1746 # Code begins
1747 1747
1748 1748 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1749 1749
1750 1750 # save the line away in case we crash, so the post-mortem handler can
1751 1751 # record it
1752 1752 self._last_input_line = line
1753 1753
1754 1754 #print '***line: <%s>' % line # dbg
1755 1755
1756 1756 # the input history needs to track even empty lines
1757 1757 if not line.strip():
1758 1758 if not continue_prompt:
1759 1759 self.outputcache.prompt_count -= 1
1760 1760 return self.handle_normal(line,continue_prompt)
1761 1761 #return self.handle_normal('',continue_prompt)
1762 1762
1763 1763 # print '***cont',continue_prompt # dbg
1764 1764 # special handlers are only allowed for single line statements
1765 1765 if continue_prompt and not self.rc.multi_line_specials:
1766 1766 return self.handle_normal(line,continue_prompt)
1767 1767
1768 1768 # For the rest, we need the structure of the input
1769 1769 pre,iFun,theRest = self.split_user_input(line)
1770 1770 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1771 1771
1772 1772 # First check for explicit escapes in the last/first character
1773 1773 handler = None
1774 1774 if line[-1] == self.ESC_HELP:
1775 1775 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1776 1776 if handler is None:
1777 1777 # look at the first character of iFun, NOT of line, so we skip
1778 1778 # leading whitespace in multiline input
1779 1779 handler = self.esc_handlers.get(iFun[0:1])
1780 1780 if handler is not None:
1781 1781 return handler(line,continue_prompt,pre,iFun,theRest)
1782 1782 # Emacs ipython-mode tags certain input lines
1783 1783 if line.endswith('# PYTHON-MODE'):
1784 1784 return self.handle_emacs(line,continue_prompt)
1785 1785
1786 1786 # Next, check if we can automatically execute this thing
1787 1787
1788 1788 # Allow ! in multi-line statements if multi_line_specials is on:
1789 1789 if continue_prompt and self.rc.multi_line_specials and \
1790 1790 iFun.startswith(self.ESC_SHELL):
1791 1791 return self.handle_shell_escape(line,continue_prompt,
1792 1792 pre=pre,iFun=iFun,
1793 1793 theRest=theRest)
1794 1794
1795 1795 # Let's try to find if the input line is a magic fn
1796 1796 oinfo = None
1797 1797 if hasattr(self,'magic_'+iFun):
1798 1798 # WARNING: _ofind uses getattr(), so it can consume generators and
1799 1799 # cause other side effects.
1800 1800 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1801 1801 if oinfo['ismagic']:
1802 1802 # Be careful not to call magics when a variable assignment is
1803 1803 # being made (ls='hi', for example)
1804 1804 if self.rc.automagic and \
1805 1805 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1806 1806 (self.rc.multi_line_specials or not continue_prompt):
1807 1807 return self.handle_magic(line,continue_prompt,
1808 1808 pre,iFun,theRest)
1809 1809 else:
1810 1810 return self.handle_normal(line,continue_prompt)
1811 1811
1812 1812 # If the rest of the line begins with an (in)equality, assginment or
1813 1813 # function call, we should not call _ofind but simply execute it.
1814 1814 # This avoids spurious geattr() accesses on objects upon assignment.
1815 1815 #
1816 1816 # It also allows users to assign to either alias or magic names true
1817 1817 # python variables (the magic/alias systems always take second seat to
1818 1818 # true python code).
1819 1819 if theRest and theRest[0] in '!=()':
1820 1820 return self.handle_normal(line,continue_prompt)
1821 1821
1822 1822 if oinfo is None:
1823 1823 # let's try to ensure that _oinfo is ONLY called when autocall is
1824 1824 # on. Since it has inevitable potential side effects, at least
1825 1825 # having autocall off should be a guarantee to the user that no
1826 1826 # weird things will happen.
1827 1827
1828 1828 if self.rc.autocall:
1829 1829 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1830 1830 else:
1831 1831 # in this case, all that's left is either an alias or
1832 1832 # processing the line normally.
1833 1833 if iFun in self.alias_table:
1834 1834 return self.handle_alias(line,continue_prompt,
1835 1835 pre,iFun,theRest)
1836 1836 else:
1837 1837 return self.handle_normal(line,continue_prompt)
1838 1838
1839 1839 if not oinfo['found']:
1840 1840 return self.handle_normal(line,continue_prompt)
1841 1841 else:
1842 1842 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1843 1843 if oinfo['isalias']:
1844 1844 return self.handle_alias(line,continue_prompt,
1845 1845 pre,iFun,theRest)
1846 1846
1847 1847 if self.rc.autocall and \
1848 1848 not self.re_exclude_auto.match(theRest) and \
1849 1849 self.re_fun_name.match(iFun) and \
1850 1850 callable(oinfo['obj']) :
1851 1851 #print 'going auto' # dbg
1852 1852 return self.handle_auto(line,continue_prompt,
1853 1853 pre,iFun,theRest,oinfo['obj'])
1854 1854 else:
1855 1855 #print 'was callable?', callable(oinfo['obj']) # dbg
1856 1856 return self.handle_normal(line,continue_prompt)
1857 1857
1858 1858 # If we get here, we have a normal Python line. Log and return.
1859 1859 return self.handle_normal(line,continue_prompt)
1860 1860
1861 1861 def _prefilter_dumb(self, line, continue_prompt):
1862 1862 """simple prefilter function, for debugging"""
1863 1863 return self.handle_normal(line,continue_prompt)
1864 1864
1865 1865 # Set the default prefilter() function (this can be user-overridden)
1866 1866 prefilter = _prefilter
1867 1867
1868 1868 def handle_normal(self,line,continue_prompt=None,
1869 1869 pre=None,iFun=None,theRest=None):
1870 1870 """Handle normal input lines. Use as a template for handlers."""
1871 1871
1872 1872 # With autoindent on, we need some way to exit the input loop, and I
1873 1873 # don't want to force the user to have to backspace all the way to
1874 1874 # clear the line. The rule will be in this case, that either two
1875 1875 # lines of pure whitespace in a row, or a line of pure whitespace but
1876 1876 # of a size different to the indent level, will exit the input loop.
1877 1877
1878 1878 if (continue_prompt and self.autoindent and isspace(line) and
1879 1879 (line != self.indent_current or isspace(self.buffer[-1]))):
1880 1880 line = ''
1881 1881
1882 1882 self.log(line,continue_prompt)
1883 1883 return line
1884 1884
1885 1885 def handle_alias(self,line,continue_prompt=None,
1886 1886 pre=None,iFun=None,theRest=None):
1887 1887 """Handle alias input lines. """
1888 1888
1889 1889 # pre is needed, because it carries the leading whitespace. Otherwise
1890 1890 # aliases won't work in indented sections.
1891 1891 line_out = '%sipalias(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1892 1892 self.log(line_out,continue_prompt)
1893 1893 return line_out
1894 1894
1895 1895 def handle_shell_escape(self, line, continue_prompt=None,
1896 1896 pre=None,iFun=None,theRest=None):
1897 1897 """Execute the line in a shell, empty return value"""
1898 1898
1899 1899 #print 'line in :', `line` # dbg
1900 1900 # Example of a special handler. Others follow a similar pattern.
1901 if line.startswith('!!'):
1901 if line.lstrip().startswith('!!'):
1902 1902 # rewrite iFun/theRest to properly hold the call to %sx and
1903 1903 # the actual command to be executed, so handle_magic can work
1904 1904 # correctly
1905 1905 theRest = '%s %s' % (iFun[2:],theRest)
1906 1906 iFun = 'sx'
1907 1907 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1908 1908 continue_prompt,pre,iFun,theRest)
1909 1909 else:
1910 cmd=line[1:]
1910 cmd=line.lstrip()[1:]
1911 1911 line_out = '%sipsystem(%s)' % (pre,make_quoted_expr(cmd))
1912 1912 # update cache/log and return
1913 1913 self.log(line_out,continue_prompt)
1914 1914 return line_out
1915 1915
1916 1916 def handle_magic(self, line, continue_prompt=None,
1917 1917 pre=None,iFun=None,theRest=None):
1918 1918 """Execute magic functions."""
1919 1919
1920 1920
1921 1921 cmd = '%sipmagic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1922 1922 self.log(cmd,continue_prompt)
1923 1923 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1924 1924 return cmd
1925 1925
1926 1926 def handle_auto(self, line, continue_prompt=None,
1927 1927 pre=None,iFun=None,theRest=None,obj=None):
1928 1928 """Hande lines which can be auto-executed, quoting if requested."""
1929 1929
1930 1930 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1931 1931
1932 1932 # This should only be active for single-line input!
1933 1933 if continue_prompt:
1934 1934 self.log(line,continue_prompt)
1935 1935 return line
1936 1936
1937 1937 auto_rewrite = True
1938 1938 if pre == self.ESC_QUOTE:
1939 1939 # Auto-quote splitting on whitespace
1940 1940 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1941 1941 elif pre == self.ESC_QUOTE2:
1942 1942 # Auto-quote whole string
1943 1943 newcmd = '%s("%s")' % (iFun,theRest)
1944 1944 else:
1945 1945 # Auto-paren.
1946 1946 # We only apply it to argument-less calls if the autocall
1947 1947 # parameter is set to 2. We only need to check that autocall is <
1948 1948 # 2, since this function isn't called unless it's at least 1.
1949 1949 if not theRest and (self.rc.autocall < 2):
1950 1950 newcmd = '%s %s' % (iFun,theRest)
1951 1951 auto_rewrite = False
1952 1952 else:
1953 1953 if theRest.startswith('['):
1954 1954 if hasattr(obj,'__getitem__'):
1955 1955 # Don't autocall in this case: item access for an object
1956 1956 # which is BOTH callable and implements __getitem__.
1957 1957 newcmd = '%s %s' % (iFun,theRest)
1958 1958 auto_rewrite = False
1959 1959 else:
1960 1960 # if the object doesn't support [] access, go ahead and
1961 1961 # autocall
1962 1962 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1963 1963 elif theRest.endswith(';'):
1964 1964 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1965 1965 else:
1966 1966 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1967 1967
1968 1968 if auto_rewrite:
1969 1969 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1970 1970 # log what is now valid Python, not the actual user input (without the
1971 1971 # final newline)
1972 1972 self.log(newcmd,continue_prompt)
1973 1973 return newcmd
1974 1974
1975 1975 def handle_help(self, line, continue_prompt=None,
1976 1976 pre=None,iFun=None,theRest=None):
1977 1977 """Try to get some help for the object.
1978 1978
1979 1979 obj? or ?obj -> basic information.
1980 1980 obj?? or ??obj -> more details.
1981 1981 """
1982 1982
1983 1983 # We need to make sure that we don't process lines which would be
1984 1984 # otherwise valid python, such as "x=1 # what?"
1985 1985 try:
1986 1986 codeop.compile_command(line)
1987 1987 except SyntaxError:
1988 1988 # We should only handle as help stuff which is NOT valid syntax
1989 1989 if line[0]==self.ESC_HELP:
1990 1990 line = line[1:]
1991 1991 elif line[-1]==self.ESC_HELP:
1992 1992 line = line[:-1]
1993 1993 self.log('#?'+line)
1994 1994 if line:
1995 1995 self.magic_pinfo(line)
1996 1996 else:
1997 1997 page(self.usage,screen_lines=self.rc.screen_length)
1998 1998 return '' # Empty string is needed here!
1999 1999 except:
2000 2000 # Pass any other exceptions through to the normal handler
2001 2001 return self.handle_normal(line,continue_prompt)
2002 2002 else:
2003 2003 # If the code compiles ok, we should handle it normally
2004 2004 return self.handle_normal(line,continue_prompt)
2005 2005
2006 2006 def handle_emacs(self,line,continue_prompt=None,
2007 2007 pre=None,iFun=None,theRest=None):
2008 2008 """Handle input lines marked by python-mode."""
2009 2009
2010 2010 # Currently, nothing is done. Later more functionality can be added
2011 2011 # here if needed.
2012 2012
2013 2013 # The input cache shouldn't be updated
2014 2014
2015 2015 return line
2016 2016
2017 2017 def mktempfile(self,data=None):
2018 2018 """Make a new tempfile and return its filename.
2019 2019
2020 2020 This makes a call to tempfile.mktemp, but it registers the created
2021 2021 filename internally so ipython cleans it up at exit time.
2022 2022
2023 2023 Optional inputs:
2024 2024
2025 2025 - data(None): if data is given, it gets written out to the temp file
2026 2026 immediately, and the file is closed again."""
2027 2027
2028 2028 filename = tempfile.mktemp('.py','ipython_edit_')
2029 2029 self.tempfiles.append(filename)
2030 2030
2031 2031 if data:
2032 2032 tmp_file = open(filename,'w')
2033 2033 tmp_file.write(data)
2034 2034 tmp_file.close()
2035 2035 return filename
2036 2036
2037 2037 def write(self,data):
2038 2038 """Write a string to the default output"""
2039 2039 Term.cout.write(data)
2040 2040
2041 2041 def write_err(self,data):
2042 2042 """Write a string to the default error output"""
2043 2043 Term.cerr.write(data)
2044 2044
2045 2045 def exit(self):
2046 2046 """Handle interactive exit.
2047 2047
2048 2048 This method sets the exit_now attribute."""
2049 2049
2050 2050 if self.rc.confirm_exit:
2051 2051 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2052 2052 self.exit_now = True
2053 2053 else:
2054 2054 self.exit_now = True
2055 2055 return self.exit_now
2056 2056
2057 2057 def safe_execfile(self,fname,*where,**kw):
2058 2058 fname = os.path.expanduser(fname)
2059 2059
2060 2060 # find things also in current directory
2061 2061 dname = os.path.dirname(fname)
2062 2062 if not sys.path.count(dname):
2063 2063 sys.path.append(dname)
2064 2064
2065 2065 try:
2066 2066 xfile = open(fname)
2067 2067 except:
2068 2068 print >> Term.cerr, \
2069 2069 'Could not open file <%s> for safe execution.' % fname
2070 2070 return None
2071 2071
2072 2072 kw.setdefault('islog',0)
2073 2073 kw.setdefault('quiet',1)
2074 2074 kw.setdefault('exit_ignore',0)
2075 2075 first = xfile.readline()
2076 2076 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2077 2077 xfile.close()
2078 2078 # line by line execution
2079 2079 if first.startswith(loghead) or kw['islog']:
2080 2080 print 'Loading log file <%s> one line at a time...' % fname
2081 2081 if kw['quiet']:
2082 2082 stdout_save = sys.stdout
2083 2083 sys.stdout = StringIO.StringIO()
2084 2084 try:
2085 2085 globs,locs = where[0:2]
2086 2086 except:
2087 2087 try:
2088 2088 globs = locs = where[0]
2089 2089 except:
2090 2090 globs = locs = globals()
2091 2091 badblocks = []
2092 2092
2093 2093 # we also need to identify indented blocks of code when replaying
2094 2094 # logs and put them together before passing them to an exec
2095 2095 # statement. This takes a bit of regexp and look-ahead work in the
2096 2096 # file. It's easiest if we swallow the whole thing in memory
2097 2097 # first, and manually walk through the lines list moving the
2098 2098 # counter ourselves.
2099 2099 indent_re = re.compile('\s+\S')
2100 2100 xfile = open(fname)
2101 2101 filelines = xfile.readlines()
2102 2102 xfile.close()
2103 2103 nlines = len(filelines)
2104 2104 lnum = 0
2105 2105 while lnum < nlines:
2106 2106 line = filelines[lnum]
2107 2107 lnum += 1
2108 2108 # don't re-insert logger status info into cache
2109 2109 if line.startswith('#log#'):
2110 2110 continue
2111 2111 else:
2112 2112 # build a block of code (maybe a single line) for execution
2113 2113 block = line
2114 2114 try:
2115 2115 next = filelines[lnum] # lnum has already incremented
2116 2116 except:
2117 2117 next = None
2118 2118 while next and indent_re.match(next):
2119 2119 block += next
2120 2120 lnum += 1
2121 2121 try:
2122 2122 next = filelines[lnum]
2123 2123 except:
2124 2124 next = None
2125 2125 # now execute the block of one or more lines
2126 2126 try:
2127 2127 exec block in globs,locs
2128 2128 except SystemExit:
2129 2129 pass
2130 2130 except:
2131 2131 badblocks.append(block.rstrip())
2132 2132 if kw['quiet']: # restore stdout
2133 2133 sys.stdout.close()
2134 2134 sys.stdout = stdout_save
2135 2135 print 'Finished replaying log file <%s>' % fname
2136 2136 if badblocks:
2137 2137 print >> sys.stderr, ('\nThe following lines/blocks in file '
2138 2138 '<%s> reported errors:' % fname)
2139 2139
2140 2140 for badline in badblocks:
2141 2141 print >> sys.stderr, badline
2142 2142 else: # regular file execution
2143 2143 try:
2144 2144 execfile(fname,*where)
2145 2145 except SyntaxError:
2146 2146 etype,evalue = sys.exc_info()[:2]
2147 2147 self.SyntaxTB(etype,evalue,[])
2148 2148 warn('Failure executing file: <%s>' % fname)
2149 2149 except SystemExit,status:
2150 2150 if not kw['exit_ignore']:
2151 2151 self.InteractiveTB()
2152 2152 warn('Failure executing file: <%s>' % fname)
2153 2153 except:
2154 2154 self.InteractiveTB()
2155 2155 warn('Failure executing file: <%s>' % fname)
2156 2156
2157 2157 #************************* end of file <iplib.py> *****************************
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now