1
# -*- coding: utf-8 -*-
3
IPython -- An enhanced Interactive Python
5
Requires Python 2.3 or newer.
7
This file contains all the classes and helper functions specific to IPython.
11
#*****************************************************************************
12
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13
# Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
15
# Distributed under the terms of the BSD License. The full license is in
16
# the file COPYING, distributed as part of this software.
18
# Note: this code originally subclassed code.InteractiveConsole from the
19
# Python standard library. Over time, all of that class has been copied
20
# verbatim here for modifications which could not be accomplished by
21
# subclassing. At this point, there are no dependencies at all on the code
22
# module anymore (it is not even imported). The Python License (sec. 2)
23
# allows for this, but it's always nice to acknowledge credit where credit is
25
#*****************************************************************************
27
#****************************************************************************
30
from IPython import Release
31
__author__ = '%s <%s>\n%s <%s>' % \
32
( Release.authors['Janko'] + Release.authors['Fernando'] )
33
__license__ = Release.license
34
__version__ = Release.version
36
# Python standard modules
41
import cPickle as pickle
58
warnings.filterwarnings('ignore', r'.*sets module*')
60
from pprint import pprint, pformat
62
# IPython's own modules
64
from IPython import Debugger,OInspect,PyColorize,ultraTB
65
from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
66
from IPython.Extensions import pickleshare
67
from IPython.FakeModule import FakeModule
68
from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
69
from IPython.Logger import Logger
70
from IPython.Magic import Magic
71
from IPython.Prompts import CachedOutput
72
from IPython.ipstruct import Struct
73
from IPython.background_jobs import BackgroundJobManager
74
from IPython.usage import cmd_line_usage,interactive_usage
75
from IPython.genutils import *
76
from IPython.strdispatch import StrDispatch
78
import IPython.history
79
import IPython.prefilter as prefilter
80
import IPython.shadowns
83
# store the builtin raw_input globally, and use this always, in case user code
84
# overwrites it (like wx.py.PyShell does)
85
raw_input_original = raw_input
87
# compiled regexps for autoindent management
88
dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
91
#****************************************************************************
92
# Some utility function definitions
94
ini_spaces_re = re.compile(r'^(\s+)')
96
def num_ini_spaces(strng):
97
"""Return the number of initial spaces in a string"""
99
ini_spaces = ini_spaces_re.match(strng)
101
return ini_spaces.end()
105
def softspace(file, newvalue):
106
"""Copied from code.py, to remove the dependency"""
110
oldvalue = file.softspace
111
except AttributeError:
114
file.softspace = newvalue
115
except (AttributeError, TypeError):
116
# "attribute-less object" or "read-only attributes"
121
#****************************************************************************
122
# Local use exceptions
123
class SpaceInInput(exceptions.Exception): pass
126
#****************************************************************************
130
class Undefined: pass
132
class Quitter(object):
133
"""Simple class to handle exit, similar to Python 2.5's.
135
It handles exiting in an ipython-safe manner, which the one in Python 2.5
136
doesn't do (obviously, since it doesn't know about ipython)."""
138
def __init__(self,shell,name):
143
return 'Type %s() to exit.' % self.name
149
class InputList(list):
150
"""Class to store user input.
152
It's basically a list, but slices return a string instead of a list, thus
153
allowing things like (assuming 'In' is an instance):
159
exec In[5:9] + In[14] + In[21:25]"""
161
def __getslice__(self,i,j):
162
return ''.join(list.__getslice__(self,i,j))
164
class SyntaxTB(ultraTB.ListTB):
165
"""Extension which holds some state: the last exception value"""
167
def __init__(self,color_scheme = 'NoColor'):
168
ultraTB.ListTB.__init__(self,color_scheme)
169
self.last_syntax_error = None
171
def __call__(self, etype, value, elist):
172
self.last_syntax_error = value
173
ultraTB.ListTB.__call__(self,etype,value,elist)
175
def clear_err_state(self):
176
"""Return the current error state and clear it"""
177
e = self.last_syntax_error
178
self.last_syntax_error = None
181
#****************************************************************************
184
# FIXME: the Magic class is a mixin for now, and will unfortunately remain so
185
# until a full rewrite is made. I've cleaned all cross-class uses of
186
# attributes and methods, but too much user code out there relies on the
187
# equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
189
# But at least now, all the pieces have been separated and we could, in
190
# principle, stop using the mixin. This will ease the transition to the
193
# For reference, the following is the list of 'self.foo' uses in the Magic
194
# class as of 2005-12-28. These are names we CAN'T use in the main ipython
195
# class, to prevent clashes.
197
# ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
198
# 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
199
# 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
202
class InteractiveShell(object,Magic):
203
"""An enhanced console for Python."""
205
# class attribute to indicate whether the class supports threads or not.
206
# Subclasses with thread support should override this as needed.
209
def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
210
user_ns = None,user_global_ns=None,banner2='',
211
custom_exceptions=((),None),embedded=False):
214
self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
216
# some minimal strict typechecks. For some core data structures, I
217
# want actual basic python types, not just anything that looks like
218
# one. This is especially true for namespaces.
219
for ns in (user_ns,user_global_ns):
220
if ns is not None and type(ns) != types.DictType:
221
raise TypeError,'namespace must be a dictionary'
222
# Job manager (for jobs run as background threads)
223
self.jobs = BackgroundJobManager()
225
# Store the actual shell's name
229
# We need to know whether the instance is meant for embedding, since
230
# global/local namespaces need to be handled differently in that case
231
self.embedded = embedded
233
# Control variable so users can, from within the embedded instance,
234
# permanently deactivate it.
235
self.embedded_active = True
238
self.compile = codeop.CommandCompiler()
243
# Default name given in compilation of code
244
self.filename = '<ipython console>'
246
# Install our own quitter instead of the builtins. For python2.3-2.4,
247
# this brings in behavior like 2.5, and for 2.5 it's identical.
248
__builtin__.exit = Quitter(self,'exit')
249
__builtin__.quit = Quitter(self,'quit')
251
# Make an empty namespace, which extension writers can rely on both
252
# existing and NEVER being used by ipython itself. This gives them a
253
# convenient location for storing additional information and state
254
# their extensions may require, without fear of collisions with other
255
# ipython names that may develop later.
258
# Create the namespace where the user will operate. user_ns is
259
# normally the only one used, and it is passed to the exec calls as
260
# the locals argument. But we do carry a user_global_ns namespace
261
# given as the exec 'globals' argument, This is useful in embedding
262
# situations where the ipython shell opens in a context where the
263
# distinction between locals and globals is meaningful.
265
# FIXME. For some strange reason, __builtins__ is showing up at user
266
# level as a dict instead of a module. This is a manual fix, but I
267
# should really track down where the problem is coming from. Alex
268
# Schmolck reported this problem first.
270
# A useful post by Alex Martelli on this topic:
271
# Re: inconsistent value from __builtins__
272
# Von: Alex Martelli <aleaxit@yahoo.com>
273
# Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
274
# Gruppen: comp.lang.python
276
# Michael Hohn <hohn@hooknose.lbl.gov> wrote:
277
# > >>> print type(builtin_check.get_global_binding('__builtins__'))
279
# > >>> print type(__builtins__)
281
# > Is this difference in return value intentional?
283
# Well, it's documented that '__builtins__' can be either a dictionary
284
# or a module, and it's been that way for a long time. Whether it's
285
# intentional (or sensible), I don't know. In any case, the idea is
286
# that if you need to access the built-in namespace directly, you
287
# should start with "import __builtin__" (note, no 's') which will
288
# definitely give you a module. Yeah, it's somewhat confusing:-(.
290
# These routines return properly built dicts as needed by the rest of
291
# the code, and can also be used by extension writers to generate
292
# properly initialized namespaces.
293
user_ns = IPython.ipapi.make_user_ns(user_ns)
294
user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns)
297
# This is the namespace where all normal user variables live
298
self.user_ns = user_ns
299
# Embedded instances require a separate namespace for globals.
300
# Normally this one is unused by non-embedded instances.
301
self.user_global_ns = user_global_ns
302
# A namespace to keep track of internal data structures to prevent
303
# them from cluttering user-visible stuff. Will be updated later
304
self.internal_ns = {}
306
# Namespace of system aliases. Each entry in the alias
307
# table must be a 2-tuple of the form (N,name), where N is the number
308
# of positional arguments of the alias.
309
self.alias_table = {}
311
# A table holding all the namespaces IPython deals with, so that
312
# introspection facilities can search easily.
313
self.ns_table = {'user':user_ns,
314
'user_global':user_global_ns,
315
'alias':self.alias_table,
316
'internal':self.internal_ns,
317
'builtin':__builtin__.__dict__
319
# The user namespace MUST have a pointer to the shell itself.
320
self.user_ns[name] = self
322
# We need to insert into sys.modules something that looks like a
323
# module but which accesses the IPython namespace, for shelve and
324
# pickle to work interactively. Normally they rely on getting
325
# everything out of __main__, but for embedding purposes each IPython
326
# instance has its own private namespace, so we can't go shoving
327
# everything into __main__.
329
# note, however, that we should only do this for non-embedded
330
# ipythons, which really mimic the __main__.__dict__ with their own
331
# namespace. Embedded instances, on the other hand, should not do
332
# this because they need to manage the user local/global namespaces
333
# only, but they live within a 'normal' __main__ (meaning, they
334
# shouldn't overtake the execution environment of the script they're
339
main_name = self.user_ns['__name__']
341
raise KeyError,'user_ns dictionary MUST have a "__name__" key'
343
#print "pickle hack in place" # dbg
344
#print 'main_name:',main_name # dbg
345
sys.modules[main_name] = FakeModule(self.user_ns)
347
# Now that FakeModule produces a real module, we've run into a nasty
348
# problem: after script execution (via %run), the module where the user
349
# code ran is deleted. Now that this object is a true module (needed
350
# so docetst and other tools work correctly), the Python module
351
# teardown mechanism runs over it, and sets to None every variable
352
# present in that module. This means that later calls to functions
353
# defined in the script (which have become interactively visible after
354
# script exit) fail, because they hold references to objects that have
355
# become overwritten into None. The only solution I see right now is
356
# to protect every FakeModule used by %run by holding an internal
357
# reference to it. This private list will be used for that. The
358
# %reset command will flush it as well.
359
self._user_main_modules = []
361
# List of input with multi-line handling.
362
# Fill its zero entry, user counter starts at 1
363
self.input_hist = InputList(['\n'])
364
# This one will hold the 'raw' input history, without any
365
# pre-processing. This will allow users to retrieve the input just as
366
# it was exactly typed in by the user, with %hist -r.
367
self.input_hist_raw = InputList(['\n'])
369
# list of visited directories
371
self.dir_hist = [os.getcwd()]
375
# dict of output history
376
self.output_hist = {}
378
# Get system encoding at startup time. Certain terminals (like Emacs
379
# under Win32 have it set to None, and we need to have a known valid
380
# encoding to use in the raw_input() method
382
self.stdin_encoding = sys.stdin.encoding or 'ascii'
383
except AttributeError:
384
self.stdin_encoding = 'ascii'
386
# dict of things NOT to alias (keywords, builtins and some magics)
388
no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
389
for key in keyword.kwlist + no_alias_magics:
391
no_alias.update(__builtin__.__dict__)
392
self.no_alias = no_alias
394
# make global variables for user access to these
395
self.user_ns['_ih'] = self.input_hist
396
self.user_ns['_oh'] = self.output_hist
397
self.user_ns['_dh'] = self.dir_hist
399
# user aliases to input and output histories
400
self.user_ns['In'] = self.input_hist
401
self.user_ns['Out'] = self.output_hist
403
self.user_ns['_sh'] = IPython.shadowns
404
# Object variable to store code object waiting execution. This is
405
# used mainly by the multithreaded shells, but it can come in handy in
406
# other situations. No need to use a Queue here, since it's a single
407
# item which gets cleared once run.
408
self.code_to_run = None
410
# escapes for automatic behavior on the command line
412
self.ESC_SH_CAP = '!!'
416
self.ESC_QUOTE2 = ';'
419
# And their associated handlers
420
self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
421
self.ESC_QUOTE : self.handle_auto,
422
self.ESC_QUOTE2 : self.handle_auto,
423
self.ESC_MAGIC : self.handle_magic,
424
self.ESC_HELP : self.handle_help,
425
self.ESC_SHELL : self.handle_shell_escape,
426
self.ESC_SH_CAP : self.handle_shell_escape,
429
# class initializations
430
Magic.__init__(self,self)
432
# Python source parser/formatter for syntax highlighting
433
pyformat = PyColorize.Parser().format
434
self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
436
# hooks holds pointers used for user-side customizations
437
self.hooks = Struct()
439
self.strdispatchers = {}
441
# Set all default hooks, defined in the IPython.hooks module.
442
hooks = IPython.hooks
443
for hook_name in hooks.__all__:
444
# default hooks have priority 100, i.e. low; user hooks should have
446
self.set_hook(hook_name,getattr(hooks,hook_name), 100)
447
#print "bound hook",hook_name
449
# Flag to mark unconditional exit
450
self.exit_now = False
452
self.usage_min = """\
453
An enhanced console for Python.
454
Some of its features are:
455
- Readline support if the readline library is present.
456
- Tab completion in the local namespace.
457
- Logging of input, see command-line options.
458
- System shell escape via ! , eg !ls.
459
- Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
460
- Keeps track of locally defined variables via %who, %whos.
461
- Show object information with a ? eg ?x or x? (use ?? for more info).
463
if usage: self.usage = usage
464
else: self.usage = self.usage_min
467
self.rc = rc # This will hold all configuration information
469
# temporary files used for various purposes. Deleted at exit.
472
# Keep track of readline usage (later set by init_readline)
473
self.has_readline = False
475
# template for logfile headers. It gets resolved at runtime by the
478
"""#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
479
#log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
482
#log# It is safe to make manual edits below here.
483
#log#-----------------------------------------------------------------------
485
# for pushd/popd management
487
self.home_dir = get_home_dir()
488
except HomeDirError,msg:
493
# Functions to call the underlying shell.
495
# The first is similar to os.system, but it doesn't return a value,
496
# and it allows interpolation of variables in the user's namespace.
497
self.system = lambda cmd: \
498
self.hooks.shell_hook(self.var_expand(cmd,depth=2))
500
# These are for getoutput and getoutputerror:
501
self.getoutput = lambda cmd: \
502
getoutput(self.var_expand(cmd,depth=2),
503
header=self.rc.system_header,
504
verbose=self.rc.system_verbose)
506
self.getoutputerror = lambda cmd: \
507
getoutputerror(self.var_expand(cmd,depth=2),
508
header=self.rc.system_header,
509
verbose=self.rc.system_verbose)
512
# keep track of where we started running (mainly for crash post-mortem)
513
self.starting_dir = os.getcwd()
515
# Various switches which can be set
516
self.CACHELENGTH = 5000 # this is cheap, it's just text
517
self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
518
self.banner2 = banner2
520
# TraceBack handlers:
522
# Syntax error handler.
523
self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
525
# The interactive one is initialized with an offset, meaning we always
526
# want to remove the topmost item in the traceback, which is our own
527
# internal code. Valid modes: ['Plain','Context','Verbose']
528
self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
529
color_scheme='NoColor',
532
# IPython itself shouldn't crash. This will produce a detailed
533
# post-mortem if it does. But we only install the crash handler for
534
# non-threaded shells, the threaded ones use a normal verbose reporter
535
# and lose the crash handler. This is because exceptions in the main
536
# thread (such as in GUI code) propagate directly to sys.excepthook,
537
# and there's no point in printing crash dumps for every user exception.
539
ipCrashHandler = ultraTB.FormattedTB()
541
from IPython import CrashHandler
542
ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
543
self.set_crash_handler(ipCrashHandler)
545
# and add any custom exception handlers the user may have specified
546
self.set_custom_exc(*custom_exceptions)
548
# indentation management
549
self.autoindent = False
550
self.indent_current_nsp = 0
552
# Make some aliases automatically
553
# Prepare list of shell aliases to auto-define
554
if os.name == 'posix':
555
auto_alias = ('mkdir mkdir', 'rmdir rmdir',
556
'mv mv -i','rm rm -i','cp cp -i',
557
'cat cat','less less','clear clear',
562
# Extra ls aliases with color, which need special treatment on BSD
564
ls_extra = ( # color ls
565
'lc ls -F -o --color',
566
# ls normal files only
567
'lf ls -F -o --color %l | grep ^-',
569
'lk ls -F -o --color %l | grep ^l',
570
# directories or links to directories,
571
'ldir ls -F -o --color %l | grep /$',
572
# things which are executable
573
'lx ls -F -o --color %l | grep ^-..x',
575
# The BSDs don't ship GNU ls, so they don't understand the
576
# --color switch out of the box
577
if 'bsd' in sys.platform:
578
ls_extra = ( # ls normal files only
579
'lf ls -lF | grep ^-',
581
'lk ls -lF | grep ^l',
582
# directories or links to directories,
583
'ldir ls -lF | grep /$',
584
# things which are executable
585
'lx ls -lF | grep ^-..x',
587
auto_alias = auto_alias + ls_extra
588
elif os.name in ['nt','dos']:
589
auto_alias = ('ls dir /on',
590
'ddir dir /ad /on', 'ldir dir /ad /on',
591
'mkdir mkdir','rmdir rmdir','echo echo',
592
'ren ren','cls cls','copy copy')
595
self.auto_alias = [s.split(None,1) for s in auto_alias]
598
# Produce a public API instance
599
self.api = IPython.ipapi.IPApi(self)
601
# Call the actual (public) initializer
602
self.init_auto_alias()
604
# track which builtins we add, so we can clean up later
605
self.builtins_added = {}
606
# This method will add the necessary builtins for operation, but
607
# tracking what it did via the builtins_added dict.
609
#TODO: remove this, redundant
617
def var_expand(self,cmd,depth=0):
618
"""Expand python variables in a string.
620
The depth argument indicates how many frames above the caller should
621
be walked to look for the local namespace where to expand variables.
623
The global namespace for expansion is always the user's interactive
627
return str(ItplNS(cmd,
628
self.user_ns, # globals
629
# Skip our own frame in searching for locals:
630
sys._getframe(depth+1).f_locals # locals
633
def pre_config_initialization(self):
634
"""Pre-configuration init method
636
This is called before the configuration files are processed to
637
prepare the services the config files might need.
639
self.rc already has reasonable default values at this point.
643
self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
644
except exceptions.UnicodeDecodeError:
645
print "Your ipythondir can't be decoded to unicode!"
646
print "Please set HOME environment variable to something that"
647
print r"only has ASCII characters, e.g. c:\home"
648
print "Now it is",rc.ipythondir
650
self.shadowhist = IPython.history.ShadowHist(self.db)
653
def post_config_initialization(self):
654
"""Post configuration init method
656
This is called after the configuration files have been processed to
657
'finalize' the initialization."""
662
self.inspector = OInspect.Inspector(OInspect.InspectColors,
663
PyColorize.ANSICodeColors,
665
rc.object_info_string_level)
667
self.rl_next_input = None
668
self.rl_do_indent = False
669
# Load readline proper
674
# local shortcut, this is used a LOT
675
self.log = self.logger.log
677
# Initialize cache, set in/out prompts and printing system
678
self.outputcache = CachedOutput(self,
681
input_sep = rc.separate_in,
682
output_sep = rc.separate_out,
683
output_sep2 = rc.separate_out2,
686
ps_out = rc.prompt_out,
687
pad_left = rc.prompts_pad_left)
689
# user may have over-ridden the default print hook:
691
self.outputcache.__class__.display = self.hooks.display
692
except AttributeError:
695
# I don't like assigning globally to sys, because it means when
696
# embedding instances, each embedded instance overrides the previous
697
# choice. But sys.displayhook seems to be called internally by exec,
698
# so I don't see a way around it. We first save the original and then
700
self.sys_displayhook = sys.displayhook
701
sys.displayhook = self.outputcache
703
# Do a proper resetting of doctest, including the necessary displayhook
708
warn("doctest module does not exist.")
710
# Set user colors (don't do it in the constructor above so that it
711
# doesn't crash if colors option is invalid)
712
self.magic_colors(rc.colors)
714
# Set calling of pdb on exceptions
715
self.call_pdb = rc.pdb
718
for alias in rc.alias:
719
self.magic_alias(alias)
721
self.hooks.late_startup_hook()
723
for cmd in self.rc.autoexec:
724
#print "autoexec>",cmd #dbg
725
self.api.runlines(cmd)
728
for batchfile in [path(arg) for arg in self.rc.args
729
if arg.lower().endswith('.ipy')]:
730
if not batchfile.isfile():
731
print "No such batch file:", batchfile
733
self.api.runlines(batchfile.text())
735
# without -i option, exit after running the batch file
736
if batchrun and not self.rc.interact:
739
def add_builtins(self):
740
"""Store ipython references into the builtin namespace.
742
Some parts of ipython operate via builtins injected here, which hold a
743
reference to IPython itself."""
745
# TODO: deprecate all of these, they are unsafe
746
builtins_new = dict(__IPYTHON__ = self,
747
ip_set_hook = self.set_hook,
749
ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
750
ipalias = wrap_deprecated(self.ipalias),
751
ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
754
for biname,bival in builtins_new.items():
756
# store the orignal value so we can restore it
757
self.builtins_added[biname] = __builtin__.__dict__[biname]
759
# or mark that it wasn't defined, and we'll just delete it at
761
self.builtins_added[biname] = Undefined
762
__builtin__.__dict__[biname] = bival
764
# Keep in the builtins a flag for when IPython is active. We set it
765
# with setdefault so that multiple nested IPythons don't clobber one
766
# another. Each will increase its value by one upon being activated,
767
# which also gives us a way to determine the nesting level.
768
__builtin__.__dict__.setdefault('__IPYTHON__active',0)
770
def clean_builtins(self):
771
"""Remove any builtins which might have been added by add_builtins, or
772
restore overwritten ones to their previous values."""
773
for biname,bival in self.builtins_added.items():
774
if bival is Undefined:
775
del __builtin__.__dict__[biname]
777
__builtin__.__dict__[biname] = bival
778
self.builtins_added.clear()
780
def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
781
"""set_hook(name,hook) -> sets an internal IPython hook.
783
IPython exposes some of its internal API as user-modifiable hooks. By
784
adding your function to one of these hooks, you can modify IPython's
785
behavior to call at runtime your own routines."""
787
# At some point in the future, this should validate the hook before it
788
# accepts it. Probably at least check that the hook takes the number
789
# of args it's supposed to.
791
f = new.instancemethod(hook,self,self.__class__)
793
# check if the hook is for strdispatcher first
794
if str_key is not None:
795
sdp = self.strdispatchers.get(name, StrDispatch())
796
sdp.add_s(str_key, f, priority )
797
self.strdispatchers[name] = sdp
799
if re_key is not None:
800
sdp = self.strdispatchers.get(name, StrDispatch())
801
sdp.add_re(re.compile(re_key), f, priority )
802
self.strdispatchers[name] = sdp
805
dp = getattr(self.hooks, name, None)
806
if name not in IPython.hooks.__all__:
807
print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
809
dp = IPython.hooks.CommandChainDispatcher()
813
except AttributeError:
814
# it was not commandchain, plain old func - replace
817
setattr(self.hooks,name, dp)
820
#setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
822
def set_crash_handler(self,crashHandler):
823
"""Set the IPython crash handler.
825
This must be a callable with a signature suitable for use as
828
# Install the given crash handler as the Python exception hook
829
sys.excepthook = crashHandler
831
# The instance will store a pointer to this, so that runtime code
832
# (such as magics) can access it. This is because during the
833
# read-eval loop, it gets temporarily overwritten (to deal with GUI
835
self.sys_excepthook = sys.excepthook
838
def set_custom_exc(self,exc_tuple,handler):
839
"""set_custom_exc(exc_tuple,handler)
841
Set a custom exception handler, which will be called if any of the
842
exceptions in exc_tuple occur in the mainloop (specifically, in the
847
- exc_tuple: a *tuple* of valid exceptions to call the defined
848
handler for. It is very important that you use a tuple, and NOT A
849
LIST here, because of the way Python's except statement works. If
850
you only want to trap a single exception, use a singleton tuple:
852
exc_tuple == (MyCustomException,)
854
- handler: this must be defined as a function with the following
855
basic interface: def my_handler(self,etype,value,tb).
857
This will be made into an instance method (via new.instancemethod)
858
of IPython itself, and it will be called if any of the exceptions
859
listed in the exc_tuple are caught. If the handler is None, an
860
internal basic one is used, which just prints basic info.
862
WARNING: by putting in your own exception handler into IPython's main
863
execution loop, you run a very good chance of nasty crashes. This
864
facility should only be used if you really know what you are doing."""
866
assert type(exc_tuple)==type(()) , \
867
"The custom exceptions must be given AS A TUPLE."
869
def dummy_handler(self,etype,value,tb):
870
print '*** Simple custom exception handler ***'
871
print 'Exception type :',etype
872
print 'Exception value:',value
873
print 'Traceback :',tb
874
print 'Source code :','\n'.join(self.buffer)
876
if handler is None: handler = dummy_handler
878
self.CustomTB = new.instancemethod(handler,self,self.__class__)
879
self.custom_exceptions = exc_tuple
881
def set_custom_completer(self,completer,pos=0):
882
"""set_custom_completer(completer,pos=0)
884
Adds a new custom completer function.
886
The position argument (defaults to 0) is the index in the completers
887
list where you want the completer to be inserted."""
889
newcomp = new.instancemethod(completer,self.Completer,
890
self.Completer.__class__)
891
self.Completer.matchers.insert(pos,newcomp)
893
def set_completer(self):
894
"""reset readline's completer to be our own."""
895
self.readline.set_completer(self.Completer.complete)
897
def _get_call_pdb(self):
898
return self._call_pdb
900
def _set_call_pdb(self,val):
902
if val not in (0,1,False,True):
903
raise ValueError,'new call_pdb value must be boolean'
905
# store value in instance
908
# notify the actual exception handlers
909
self.InteractiveTB.call_pdb = val
912
self.sys_excepthook.call_pdb = val
914
warn('Failed to activate pdb for threaded exception handler')
916
call_pdb = property(_get_call_pdb,_set_call_pdb,None,
917
'Control auto-activation of pdb at exceptions')
920
# These special functions get installed in the builtin namespace, to
921
# provide programmatic (pure python) access to magics, aliases and system
922
# calls. This is important for logging, user scripting, and more.
924
# We are basically exposing, via normal python functions, the three
925
# mechanisms in which ipython offers special call modes (magics for
926
# internal control, aliases for direct system access via pre-selected
927
# names, and !cmd for calling arbitrary system commands).
929
def ipmagic(self,arg_s):
930
"""Call a magic function by name.
932
Input: a string containing the name of the magic function to call and any
933
additional arguments to be passed to the magic.
935
ipmagic('name -opt foo bar') is equivalent to typing at the ipython
938
In[1]: %name -opt foo bar
940
To call a magic without arguments, simply use ipmagic('name').
942
This provides a proper Python function to call IPython's magics in any
943
valid Python code you can type at the interpreter, including loops and
944
compound statements. It is added by IPython to the Python builtin
945
namespace upon initialization."""
947
args = arg_s.split(' ',1)
949
magic_name = magic_name.lstrip(self.ESC_MAGIC)
955
fn = getattr(self,'magic_'+magic_name,None)
957
error("Magic function `%s` not found." % magic_name)
959
magic_args = self.var_expand(magic_args,1)
960
return fn(magic_args)
962
def ipalias(self,arg_s):
963
"""Call an alias by name.
965
Input: a string containing the name of the alias to call and any
966
additional arguments to be passed to the magic.
968
ipalias('name -opt foo bar') is equivalent to typing at the ipython
971
In[1]: name -opt foo bar
973
To call an alias without arguments, simply use ipalias('name').
975
This provides a proper Python function to call IPython's aliases in any
976
valid Python code you can type at the interpreter, including loops and
977
compound statements. It is added by IPython to the Python builtin
978
namespace upon initialization."""
980
args = arg_s.split(' ',1)
986
if alias_name in self.alias_table:
987
self.call_alias(alias_name,alias_args)
989
error("Alias `%s` not found." % alias_name)
991
def ipsystem(self,arg_s):
992
"""Make a system call, using IPython."""
996
def complete(self,text):
997
"""Return a sorted list of all possible completions on text.
1001
- text: a string of text to be completed on.
1003
This is a wrapper around the completion mechanism, similar to what
1004
readline does at the command line when the TAB key is hit. By
1005
exposing it as a method, it can be used by other non-readline
1006
environments (such as GUIs) for text completion.
1008
Simple usage example:
1012
In [2]: __IP.complete('x.l')
1013
Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
1015
complete = self.Completer.complete
1017
# use a dict so we get unique keys, since ipyhton's multiple
1018
# completers can return duplicates. When we make 2.4 a requirement,
1019
# start using sets instead, which are faster.
1022
newcomp = complete(text,state,line_buffer=text)
1027
outcomps = comps.keys()
1031
def set_completer_frame(self, frame=None):
1033
self.Completer.namespace = frame.f_locals
1034
self.Completer.global_namespace = frame.f_globals
1036
self.Completer.namespace = self.user_ns
1037
self.Completer.global_namespace = self.user_global_ns
1039
def init_auto_alias(self):
1040
"""Define some aliases automatically.
1042
These are ALL parameter-less aliases"""
1044
for alias,cmd in self.auto_alias:
1045
self.getapi().defalias(alias,cmd)
1048
def alias_table_validate(self,verbose=0):
1049
"""Update information about the alias table.
1051
In particular, make sure no Python keywords/builtins are in it."""
1053
no_alias = self.no_alias
1054
for k in self.alias_table.keys():
1056
del self.alias_table[k]
1058
print ("Deleting alias <%s>, it's a Python "
1059
"keyword or builtin." % k)
1061
def set_autoindent(self,value=None):
1062
"""Set the autoindent flag, checking for readline support.
1064
If called with no arguments, it acts as a toggle."""
1066
if not self.has_readline:
1067
if os.name == 'posix':
1068
warn("The auto-indent feature requires the readline library")
1072
self.autoindent = not self.autoindent
1074
self.autoindent = value
1076
def rc_set_toggle(self,rc_field,value=None):
1077
"""Set or toggle a field in IPython's rc config. structure.
1079
If called with no arguments, it acts as a toggle.
1081
If called with a non-existent field, the resulting AttributeError
1082
exception will propagate out."""
1084
rc_val = getattr(self.rc,rc_field)
1087
setattr(self.rc,rc_field,value)
1089
def user_setup(self,ipythondir,rc_suffix,mode='install'):
1090
"""Install the user configuration directory.
1092
Can be called when running for the first time or to upgrade the user's
1093
.ipython/ directory with the mode parameter. Valid modes are 'install'
1098
raw_input("Please press <RETURN> to start IPython.")
1103
cwd = os.getcwd() # remember where we started
1106
if mode == 'install':
1108
"""Welcome to IPython. I will try to create a personal configuration directory
1109
where you can customize many aspects of IPython's functionality in:\n"""
1111
print 'I am going to upgrade your configuration in:'
1115
rcdirend = os.path.join('IPython','UserConfig')
1116
cfg = lambda d: os.path.join(d,rcdirend)
1118
rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1119
print "Initializing from configuration",rcdir
1122
Installation error. IPython's directory was not found.
1124
Check the following:
1126
The ipython/IPython directory should be in a directory belonging to your
1127
PYTHONPATH environment variable (that is, it should be in a directory
1128
belonging to sys.path). You can copy it explicitly there or just link to it.
1130
IPython will create a minimal default configuration for you.
1136
if sys.platform =='win32':
1137
inif = 'ipythonrc.ini'
1140
minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' }
1141
os.makedirs(ipythondir, mode = 0777)
1142
for f, cont in minimal_setup.items():
1143
open(ipythondir + '/' + f,'w').write(cont)
1147
if mode == 'install':
1149
shutil.copytree(rcdir,ipythondir)
1150
os.chdir(ipythondir)
1151
rc_files = glb("ipythonrc*")
1152
for rc_file in rc_files:
1153
os.rename(rc_file,rc_file+rc_suffix)
1157
There was a problem with the installation:
1159
Try to correct it or contact the developers if you think it's a bug.
1160
IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1165
elif mode == 'upgrade':
1167
os.chdir(ipythondir)
1170
Can not upgrade: changing to directory %s failed. Details:
1172
""" % (ipythondir,sys.exc_info()[1])
1176
sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1177
for new_full_path in sources:
1178
new_filename = os.path.basename(new_full_path)
1179
if new_filename.startswith('ipythonrc'):
1180
new_filename = new_filename + rc_suffix
1181
# The config directory should only contain files, skip any
1182
# directories which may be there (like CVS)
1183
if os.path.isdir(new_full_path):
1185
if os.path.exists(new_filename):
1186
old_file = new_filename+'.old'
1187
if os.path.exists(old_file):
1189
os.rename(new_filename,old_file)
1190
shutil.copy(new_full_path,new_filename)
1192
raise ValueError,'unrecognized mode for install:',`mode`
1194
# Fix line-endings to those native to each platform in the config
1197
os.chdir(ipythondir)
1200
Problem: changing to directory %s failed.
1204
Some configuration files may have incorrect line endings. This should not
1205
cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1208
for fname in glb('ipythonrc*'):
1210
native_line_ends(fname,backup=0)
1214
if mode == 'install':
1216
Successful installation!
1218
Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1219
IPython manual (there are both HTML and PDF versions supplied with the
1220
distribution) to make sure that your system environment is properly configured
1221
to take advantage of IPython's features.
1223
Important note: the configuration system has changed! The old system is
1224
still in place, but its setting may be partly overridden by the settings in
1225
"~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1226
if some of the new settings bother you.
1233
All files in your directory:
1235
which would have been overwritten by the upgrade were backed up with a .old
1236
extension. If you had made particular customizations in those files you may
1237
want to merge them back into the new files.""" % locals()
1242
def atexit_operations(self):
1243
"""This will be executed at the time of exit.
1245
Saving of persistent data should be performed here. """
1247
#print '*** IPython exit cleanup ***' # dbg
1251
# Cleanup all tempfiles left around
1252
for tfile in self.tempfiles:
1258
self.hooks.shutdown_hook()
1261
"""Save input history to a file (via readline library)."""
1263
if not self.has_readline:
1267
self.readline.write_history_file(self.histfile)
1269
print 'Unable to save IPython command history to file: ' + \
1272
def reloadhist(self):
1273
"""Reload the input history from disk file."""
1275
if self.has_readline:
1277
self.readline.clear_history()
1278
self.readline.read_history_file(self.shell.histfile)
1279
except AttributeError:
1283
def history_saving_wrapper(self, func):
1284
""" Wrap func for readline history saving
1286
Convert func into callable that saves & restores
1287
history around the call """
1289
if not self.has_readline:
1297
readline.read_history_file(self.histfile)
1301
def pre_readline(self):
1302
"""readline hook to be used at the start of each line.
1304
Currently it handles auto-indent only."""
1306
#debugx('self.indent_current_nsp','pre_readline:')
1308
if self.rl_do_indent:
1309
self.readline.insert_text(self.indent_current_str())
1310
if self.rl_next_input is not None:
1311
self.readline.insert_text(self.rl_next_input)
1312
self.rl_next_input = None
1314
def init_readline(self):
1315
"""Command history completion/saving/reloading."""
1318
import IPython.rlineimpl as readline
1320
if not readline.have_readline:
1321
self.has_readline = 0
1322
self.readline = None
1323
# no point in bugging windows users with this every time:
1324
warn('Readline services not available on this platform.')
1326
sys.modules['readline'] = readline
1328
from IPython.completer import IPCompleter
1329
self.Completer = IPCompleter(self,
1331
self.user_global_ns,
1332
self.rc.readline_omit__names,
1334
sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1335
self.strdispatchers['complete_command'] = sdisp
1336
self.Completer.custom_completers = sdisp
1337
# Platform-specific configuration
1339
self.readline_startup_hook = readline.set_pre_input_hook
1341
self.readline_startup_hook = readline.set_startup_hook
1343
# Load user's initrc file (readline config)
1344
# Or if libedit is used, load editrc.
1345
inputrc_name = os.environ.get('INPUTRC')
1346
if inputrc_name is None:
1347
home_dir = get_home_dir()
1348
if home_dir is not None:
1349
inputrc_name = '.inputrc'
1350
if readline.uses_libedit:
1351
inputrc_name = '.editrc'
1352
inputrc_name = os.path.join(home_dir, inputrc_name)
1353
if os.path.isfile(inputrc_name):
1355
readline.read_init_file(inputrc_name)
1357
warn('Problems reading readline initialization file <%s>'
1360
self.has_readline = 1
1361
self.readline = readline
1362
# save this in sys so embedded copies can restore it properly
1363
sys.ipcompleter = self.Completer.complete
1364
self.set_completer()
1366
# Configure readline according to user's prefs
1367
# This is only done if GNU readline is being used. If libedit
1368
# is being used (as on Leopard) the readline config is
1369
# not run as the syntax for libedit is different.
1370
if not readline.uses_libedit:
1371
for rlcommand in self.rc.readline_parse_and_bind:
1372
readline.parse_and_bind(rlcommand)
1374
# remove some chars from the delimiters list
1375
delims = readline.get_completer_delims()
1376
delims = delims.translate(string._idmap,
1377
self.rc.readline_remove_delims)
1378
readline.set_completer_delims(delims)
1379
# otherwise we end up with a monster history after a while:
1380
readline.set_history_length(1000)
1382
#print '*** Reading readline history' # dbg
1383
readline.read_history_file(self.histfile)
1385
pass # It doesn't exist yet.
1387
atexit.register(self.atexit_operations)
1390
# Configure auto-indent for all platforms
1391
self.set_autoindent(self.rc.autoindent)
1393
def ask_yes_no(self,prompt,default=True):
1396
return ask_yes_no(prompt,default)
1398
def _should_recompile(self,e):
1399
"""Utility routine for edit_syntax_error"""
1401
if e.filename in ('<ipython console>','<input>','<string>',
1402
'<console>','<BackgroundJob compilation>',
1407
if (self.rc.autoedit_syntax and
1408
not self.ask_yes_no('Return to editor to correct syntax error? '
1419
# always pass integer line and offset values to editor hook
1420
self.hooks.fix_error_editor(e.filename,
1421
int0(e.lineno),int0(e.offset),e.msg)
1424
def edit_syntax_error(self):
1425
"""The bottom half of the syntax error handler called in the main loop.
1427
Loop until syntax error is fixed or user cancels.
1430
while self.SyntaxTB.last_syntax_error:
1431
# copy and clear last_syntax_error
1432
err = self.SyntaxTB.clear_err_state()
1433
if not self._should_recompile(err):
1436
# may set last_syntax_error again if a SyntaxError is raised
1437
self.safe_execfile(err.filename,self.user_ns)
1439
self.showtraceback()
1442
f = file(err.filename)
1444
sys.displayhook(f.read())
1448
self.showtraceback()
1450
def showsyntaxerror(self, filename=None):
1451
"""Display the syntax error that just occurred.
1453
This doesn't display a stack trace because there isn't one.
1455
If a filename is given, it is stuffed in the exception instead
1456
of what was there before (because Python's parser always uses
1457
"<string>" when reading from a string).
1459
etype, value, last_traceback = sys.exc_info()
1461
# See note about these variables in showtraceback() below
1462
sys.last_type = etype
1463
sys.last_value = value
1464
sys.last_traceback = last_traceback
1466
if filename and etype is SyntaxError:
1467
# Work hard to stuff the correct filename in the exception
1469
msg, (dummy_filename, lineno, offset, line) = value
1471
# Not the format we expect; leave it alone
1474
# Stuff in the right filename
1476
# Assume SyntaxError is a class exception
1477
value = SyntaxError(msg, (filename, lineno, offset, line))
1479
# If that failed, assume SyntaxError is a string
1480
value = msg, (filename, lineno, offset, line)
1481
self.SyntaxTB(etype,value,[])
1483
def debugger(self,force=False):
1484
"""Call the pydb/pdb debugger.
1488
- force(False): by default, this routine checks the instance call_pdb
1489
flag and does not actually invoke the debugger if the flag is false.
1490
The 'force' option forces the debugger to activate even if the flag
1494
if not (force or self.call_pdb):
1497
if not hasattr(sys,'last_traceback'):
1498
error('No traceback has been produced, nothing to debug.')
1501
# use pydb if available
1502
if Debugger.has_pydb:
1505
# fallback to our internal debugger
1506
pm = lambda : self.InteractiveTB.debugger(force=True)
1507
self.history_saving_wrapper(pm)()
1509
def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1510
"""Display the exception that just occurred.
1512
If nothing is known about the exception, this is the method which
1513
should be used throughout the code for presenting user tracebacks,
1514
rather than directly invoking the InteractiveTB object.
1516
A specific showsyntaxerror() also exists, but this method can take
1517
care of calling it if needed, so unless you are explicitly catching a
1518
SyntaxError exception, don't try to analyze the stack manually and
1519
simply call this method."""
1522
# Though this won't be called by syntax errors in the input line,
1523
# there may be SyntaxError cases whith imported code.
1526
if exc_tuple is None:
1527
etype, value, tb = sys.exc_info()
1529
etype, value, tb = exc_tuple
1531
if etype is SyntaxError:
1532
self.showsyntaxerror(filename)
1533
elif etype is IPython.ipapi.UsageError:
1534
print "UsageError:", value
1536
# WARNING: these variables are somewhat deprecated and not
1537
# necessarily safe to use in a threaded environment, but tools
1538
# like pdb depend on their existence, so let's set them. If we
1539
# find problems in the field, we'll need to revisit their use.
1540
sys.last_type = etype
1541
sys.last_value = value
1542
sys.last_traceback = tb
1544
if etype in self.custom_exceptions:
1545
self.CustomTB(etype,value,tb)
1547
self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1548
if self.InteractiveTB.call_pdb and self.has_readline:
1549
# pdb mucks up readline, fix it back
1550
self.set_completer()
1551
except KeyboardInterrupt:
1552
self.write("\nKeyboardInterrupt\n")
1556
def mainloop(self,banner=None):
1557
"""Creates the local namespace and starts the mainloop.
1559
If an optional banner argument is given, it will override the
1560
internally created default banner."""
1562
if self.rc.c: # Emulate Python's -c option
1563
self.exec_init_cmd()
1565
if not self.rc.banner:
1567
# banner is string? Use it directly!
1568
elif isinstance(self.rc.banner,basestring):
1569
banner = self.rc.banner
1571
banner = self.BANNER+self.banner2
1575
self.interact(banner)
1576
#self.interact_with_readline()
1577
# XXX for testing of a readline-decoupled repl loop, call interact_with_readline above
1580
except KeyboardInterrupt:
1581
# this should not be necessary, but KeyboardInterrupt
1582
# handling seems rather unpredictable...
1583
self.write("\nKeyboardInterrupt in interact()\n")
1585
def exec_init_cmd(self):
1586
"""Execute a command given at the command line.
1588
This emulates Python's -c option."""
1591
self.push(self.prefilter(self.rc.c, False))
1592
if not self.rc.interact:
1593
self.exit_now = True
1595
def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1596
"""Embeds IPython into a running python program.
1600
- header: An optional header message can be specified.
1602
- local_ns, global_ns: working namespaces. If given as None, the
1603
IPython-initialized one is updated with __main__.__dict__, so that
1604
program variables become visible but user-specific configuration
1607
- stack_depth: specifies how many levels in the stack to go to
1608
looking for namespaces (when local_ns and global_ns are None). This
1609
allows an intermediate caller to make sure that this function gets
1610
the namespace from the intended level in the stack. By default (0)
1611
it will get its locals and globals from the immediate caller.
1613
Warning: it's possible to use this in a program which is being run by
1614
IPython itself (via %run), but some funny things will happen (a few
1615
globals get overwritten). In the future this will be cleaned up, as
1616
there is no fundamental reason why it can't work perfectly."""
1618
# Get locals and globals from caller
1619
if local_ns is None or global_ns is None:
1620
call_frame = sys._getframe(stack_depth).f_back
1622
if local_ns is None:
1623
local_ns = call_frame.f_locals
1624
if global_ns is None:
1625
global_ns = call_frame.f_globals
1627
# Update namespaces and fire up interpreter
1629
# The global one is easy, we can just throw it in
1630
self.user_global_ns = global_ns
1632
# but the user/local one is tricky: ipython needs it to store internal
1633
# data, but we also need the locals. We'll copy locals in the user
1634
# one, but will track what got copied so we can delete them at exit.
1635
# This is so that a later embedded call doesn't see locals from a
1636
# previous call (which most likely existed in a separate scope).
1637
local_varnames = local_ns.keys()
1638
self.user_ns.update(local_ns)
1640
# Patch for global embedding to make sure that things don't overwrite
1641
# user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1642
# FIXME. Test this a bit more carefully (the if.. is new)
1643
if local_ns is None and global_ns is None:
1644
self.user_global_ns.update(__main__.__dict__)
1646
# make sure the tab-completer has the correct frame information, so it
1647
# actually completes using the frame's locals/globals
1648
self.set_completer_frame()
1650
# before activating the interactive mode, we need to make sure that
1651
# all names in the builtin namespace needed by ipython point to
1652
# ourselves, and not to other instances.
1655
self.interact(header)
1657
# now, purge out the user namespace from anything we might have added
1658
# from the caller's local namespace
1659
delvar = self.user_ns.pop
1660
for var in local_varnames:
1662
# and clean builtins we may have overridden
1663
self.clean_builtins()
1665
def interact_prompt(self):
1666
""" Print the prompt (in read-eval-print loop)
1668
Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1669
used in standard IPython flow.
1673
prompt = self.hooks.generate_prompt(True)
1675
self.showtraceback()
1677
self.rl_do_indent = True
1681
prompt = self.hooks.generate_prompt(False)
1683
self.showtraceback()
1686
def interact_handle_input(self,line):
1687
""" Handle the input line (in read-eval-print loop)
1689
Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1690
used in standard IPython flow.
1692
if line.lstrip() == line:
1693
self.shadowhist.add(line.strip())
1694
lineout = self.prefilter(line,self.more)
1698
self.input_hist_raw[-1] += '%s\n' % line
1700
self.input_hist_raw.append('%s\n' % line)
1703
self.more = self.push(lineout)
1704
if (self.SyntaxTB.last_syntax_error and
1705
self.rc.autoedit_syntax):
1706
self.edit_syntax_error()
1708
def interact_with_readline(self):
1709
""" Demo of using interact_handle_input, interact_prompt
1711
This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1712
it should work like this.
1714
self.readline_startup_hook(self.pre_readline)
1715
while not self.exit_now:
1716
self.interact_prompt()
1718
self.rl_do_indent = True
1720
self.rl_do_indent = False
1721
line = raw_input_original().decode(self.stdin_encoding)
1722
self.interact_handle_input(line)
1725
def interact(self, banner=None):
1726
"""Closely emulate the interactive Python console.
1728
The optional banner argument specify the banner to print
1729
before the first interaction; by default it prints a banner
1730
similar to the one printed by the real Python interpreter,
1731
followed by the current class name in parentheses (so as not
1732
to confuse this with the real interpreter -- since it's so
1738
# batch run -> do not interact
1740
cprt = 'Type "copyright", "credits" or "license" for more information.'
1742
self.write("Python %s on %s\n%s\n(%s)\n" %
1743
(sys.version, sys.platform, cprt,
1744
self.__class__.__name__))
1750
# Mark activity in the builtins
1751
__builtin__.__dict__['__IPYTHON__active'] += 1
1753
if self.has_readline:
1754
self.readline_startup_hook(self.pre_readline)
1755
# exit_now is set by a call to %Exit or %Quit
1757
while not self.exit_now:
1758
self.hooks.pre_prompt_hook()
1761
prompt = self.hooks.generate_prompt(True)
1763
self.showtraceback()
1765
self.rl_do_indent = True
1769
prompt = self.hooks.generate_prompt(False)
1771
self.showtraceback()
1773
line = self.raw_input(prompt,more)
1775
# quick exit on sys.std[in|out] close
1778
self.rl_do_indent = False
1780
except KeyboardInterrupt:
1781
#double-guard against keyboardinterrupts during kbdint handling
1783
self.write('\nKeyboardInterrupt\n')
1785
# keep cache in sync with the prompt counter:
1786
self.outputcache.prompt_count -= 1
1789
self.indent_current_nsp = 0
1791
except KeyboardInterrupt:
1795
self.rl_do_indent = False
1796
self.readline_startup_hook(None)
1800
warn('The Python debugger has exited with a BdbQuit exception.\n'
1801
'Because of how pdb handles the stack, it is impossible\n'
1802
'for IPython to properly format this particular exception.\n'
1803
'IPython will resume normal operation.')
1805
# exceptions here are VERY RARE, but they can be triggered
1806
# asynchronously by signal handlers, for example.
1807
self.showtraceback()
1809
more = self.push(line)
1810
if (self.SyntaxTB.last_syntax_error and
1811
self.rc.autoedit_syntax):
1812
self.edit_syntax_error()
1814
# We are off again...
1815
__builtin__.__dict__['__IPYTHON__active'] -= 1
1817
def excepthook(self, etype, value, tb):
1818
"""One more defense for GUI apps that call sys.excepthook.
1820
GUI frameworks like wxPython trap exceptions and call
1821
sys.excepthook themselves. I guess this is a feature that
1822
enables them to keep running after exceptions that would
1823
otherwise kill their mainloop. This is a bother for IPython
1824
which excepts to catch all of the program exceptions with a try:
1827
Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1828
any app directly invokes sys.excepthook, it will look to the user like
1829
IPython crashed. In order to work around this, we can disable the
1830
CrashHandler and replace it with this excepthook instead, which prints a
1831
regular traceback using our InteractiveTB. In this fashion, apps which
1832
call sys.excepthook will generate a regular-looking exception from
1833
IPython, and the CrashHandler will only be triggered by real IPython
1836
This hook should be used sparingly, only in places which are not likely
1837
to be true IPython errors.
1839
self.showtraceback((etype,value,tb),tb_offset=0)
1841
def expand_aliases(self,fn,rest):
1842
""" Expand multiple levels of aliases:
1851
baz huhhahhei -> bar /tmp huhhahhei
1854
line = fn + " " + rest
1858
pre,fn,rest = prefilter.splitUserInput(line,
1859
prefilter.shell_line_split)
1860
if fn in self.alias_table:
1862
warn("Cyclic alias definition, repeated '%s'" % fn)
1866
l2 = self.transform_alias(fn,rest)
1868
# print "alias",line, "->",l2 #dbg
1871
# ls -> ls -F should not recurse forever
1872
if l2.split(None,1)[0] == line.split(None,1)[0]:
1879
# print "al expand to",line #dbg
1885
def transform_alias(self, alias,rest=''):
1886
""" Transform alias to system command string.
1888
trg = self.alias_table[alias]
1892
if ' ' in cmd and os.path.isfile(cmd):
1895
# Expand the %l special to be the user's input line
1896
if cmd.find('%l') >= 0:
1897
cmd = cmd.replace('%l',rest)
1900
# Simple, argument-less aliases
1901
cmd = '%s %s' % (cmd,rest)
1903
# Handle aliases with positional arguments
1904
args = rest.split(None,nargs)
1905
if len(args)< nargs:
1906
error('Alias <%s> requires %s arguments, %s given.' %
1907
(alias,nargs,len(args)))
1909
cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1910
# Now call the macro, evaluating in the user's namespace
1911
#print 'new command: <%r>' % cmd # dbg
1914
def call_alias(self,alias,rest=''):
1915
"""Call an alias given its name and the rest of the line.
1917
This is only used to provide backwards compatibility for users of
1918
ipalias(), use of which is not recommended for anymore."""
1920
# Now call the macro, evaluating in the user's namespace
1921
cmd = self.transform_alias(alias, rest)
1925
self.showtraceback()
1927
def indent_current_str(self):
1928
"""return the current level of indentation as a string"""
1929
return self.indent_current_nsp * ' '
1931
def autoindent_update(self,line):
1932
"""Keep track of the indent level."""
1935
#debugx('self.indent_current_nsp')
1938
inisp = num_ini_spaces(line)
1939
if inisp < self.indent_current_nsp:
1940
self.indent_current_nsp = inisp
1943
self.indent_current_nsp += 4
1944
elif dedent_re.match(line):
1945
self.indent_current_nsp -= 4
1947
self.indent_current_nsp = 0
1949
def runlines(self,lines):
1950
"""Run a string of one or more lines of source.
1952
This method is capable of running a string containing multiple source
1953
lines, as if they had been entered at the IPython prompt. Since it
1954
exposes IPython's processing machinery, the given strings can contain
1955
magic calls (%magic), special shell access (!cmd), etc."""
1957
# We must start with a clean buffer, in case this is run from an
1958
# interactive IPython session (via a magic, for example).
1960
lines = lines.split('\n')
1964
# skip blank lines so we don't mess up the prompt counter, but do
1965
# NOT skip even a blank line if we are in a code block (more is
1970
# push to raw history, so hist line numbers stay in sync
1971
self.input_hist_raw.append("# " + line + "\n")
1972
more = self.push(self.prefilter(line,more))
1973
# IPython's runsource returns None if there was an error
1974
# compiling the code. This allows us to stop processing right
1975
# away, so the user gets the error message at the right place.
1979
self.input_hist_raw.append("\n")
1980
# final newline in case the input didn't have it, so that the code
1981
# actually does get executed
1985
def runsource(self, source, filename='<input>', symbol='single'):
1986
"""Compile and run some source in the interpreter.
1988
Arguments are as for compile_command().
1990
One several things can happen:
1992
1) The input is incorrect; compile_command() raised an
1993
exception (SyntaxError or OverflowError). A syntax traceback
1994
will be printed by calling the showsyntaxerror() method.
1996
2) The input is incomplete, and more input is required;
1997
compile_command() returned None. Nothing happens.
1999
3) The input is complete; compile_command() returned a code
2000
object. The code is executed by calling self.runcode() (which
2001
also handles run-time exceptions, except for SystemExit).
2003
The return value is:
2007
- False in the other cases, unless an exception is raised, where
2008
None is returned instead. This can be used by external callers to
2009
know whether to continue feeding input or not.
2011
The return value can be used to decide whether to use sys.ps1 or
2012
sys.ps2 to prompt the next line."""
2014
# if the source code has leading blanks, add 'if 1:\n' to it
2015
# this allows execution of indented pasted code. It is tempting
2016
# to add '\n' at the end of source to run commands like ' a=1'
2017
# directly, but this fails for more complicated scenarios
2018
source=source.encode(self.stdin_encoding)
2019
if source[:1] in [' ', '\t']:
2020
source = 'if 1:\n%s' % source
2023
code = self.compile(source,filename,symbol)
2024
except (OverflowError, SyntaxError, ValueError, TypeError):
2026
self.showsyntaxerror(filename)
2034
# We store the code object so that threaded shells and
2035
# custom exception handlers can access all this info if needed.
2036
# The source corresponding to this can be obtained from the
2037
# buffer attribute as '\n'.join(self.buffer).
2038
self.code_to_run = code
2039
# now actually execute the code object
2040
if self.runcode(code) == 0:
2045
def runcode(self,code_obj):
2046
"""Execute a code object.
2048
When an exception occurs, self.showtraceback() is called to display a
2051
Return value: a flag indicating whether the code to be run completed
2054
- 0: successful execution.
2055
- 1: an error occurred.
2058
# Set our own excepthook in case the user code tries to call it
2059
# directly, so that the IPython crash handler doesn't get triggered
2060
old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2062
# we save the original sys.excepthook in the instance, in case config
2063
# code (such as magics) needs access to it.
2064
self.sys_excepthook = old_excepthook
2065
outflag = 1 # happens in more places, so it's easier as default
2068
self.hooks.pre_runcode_hook()
2069
# Embedded instances require separate global/local namespaces
2070
# so they can see both the surrounding (local) namespace and
2071
# the module-level globals when called inside another function.
2073
exec code_obj in self.user_global_ns, self.user_ns
2074
# Normal (non-embedded) instances should only have a single
2075
# namespace for user code execution, otherwise functions won't
2076
# see interactive top-level globals.
2078
exec code_obj in self.user_ns
2080
# Reset our crash handler in place
2081
sys.excepthook = old_excepthook
2084
self.showtraceback()
2085
warn("Type %exit or %quit to exit IPython "
2086
"(%Exit or %Quit do so unconditionally).",level=1)
2087
except self.custom_exceptions:
2088
etype,value,tb = sys.exc_info()
2089
self.CustomTB(etype,value,tb)
2091
self.showtraceback()
2094
if softspace(sys.stdout, 0):
2096
# Flush out code object which has been run (and source)
2097
self.code_to_run = None
2100
def push(self, line):
2101
"""Push a line to the interpreter.
2103
The line should not have a trailing newline; it may have
2104
internal newlines. The line is appended to a buffer and the
2105
interpreter's runsource() method is called with the
2106
concatenated contents of the buffer as source. If this
2107
indicates that the command was executed or invalid, the buffer
2108
is reset; otherwise, the command is incomplete, and the buffer
2109
is left as it was after the line was appended. The return
2110
value is 1 if more input is required, 0 if the line was dealt
2111
with in some way (this is the same as runsource()).
2114
# autoindent management should be done here, and not in the
2115
# interactive loop, since that one is only seen by keyboard input. We
2116
# need this done correctly even for code run via runlines (which uses
2119
#print 'push line: <%s>' % line # dbg
2120
for subline in line.splitlines():
2121
self.autoindent_update(subline)
2122
self.buffer.append(line)
2123
more = self.runsource('\n'.join(self.buffer), self.filename)
2128
def split_user_input(self, line):
2129
# This is really a hold-over to support ipapi and some extensions
2130
return prefilter.splitUserInput(line)
2132
def resetbuffer(self):
2133
"""Reset the input buffer."""
2136
def raw_input(self,prompt='',continue_prompt=False):
2137
"""Write a prompt and read a line.
2139
The returned line does not include the trailing newline.
2140
When the user enters the EOF key sequence, EOFError is raised.
2144
- prompt(''): a string to be printed to prompt the user.
2146
- continue_prompt(False): whether this line is the first one or a
2147
continuation in a sequence of inputs.
2150
# Code run by the user may have modified the readline completer state.
2151
# We must ensure that our completer is back in place.
2152
if self.has_readline:
2153
self.set_completer()
2156
line = raw_input_original(prompt).decode(self.stdin_encoding)
2158
warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2159
" or sys.stdout.close()!\nExiting IPython!")
2160
self.exit_now = True
2163
# Try to be reasonably smart about not re-indenting pasted input more
2164
# than necessary. We do this by trimming out the auto-indent initial
2165
# spaces, if the user's actual input started itself with whitespace.
2166
#debugx('self.buffer[-1]')
2169
if num_ini_spaces(line) > self.indent_current_nsp:
2170
line = line[self.indent_current_nsp:]
2171
self.indent_current_nsp = 0
2173
# store the unfiltered input before the user has any chance to modify
2177
self.input_hist_raw[-1] += '%s\n' % line
2178
if self.has_readline: # and some config option is set?
2180
histlen = self.readline.get_current_history_length()
2182
newhist = self.input_hist_raw[-1].rstrip()
2183
self.readline.remove_history_item(histlen-1)
2184
self.readline.replace_history_item(histlen-2,
2185
newhist.encode(self.stdin_encoding))
2186
except AttributeError:
2187
pass # re{move,place}_history_item are new in 2.4.
2189
self.input_hist_raw.append('%s\n' % line)
2190
# only entries starting at first column go to shadow history
2191
if line.lstrip() == line:
2192
self.shadowhist.add(line.strip())
2193
elif not continue_prompt:
2194
self.input_hist_raw.append('\n')
2196
lineout = self.prefilter(line,continue_prompt)
2198
# blanket except, in case a user-defined prefilter crashes, so it
2199
# can't take all of ipython with it.
2200
self.showtraceback()
2205
def _prefilter(self, line, continue_prompt):
2206
"""Calls different preprocessors, depending on the form of line."""
2208
# All handlers *must* return a value, even if it's blank ('').
2210
# Lines are NOT logged here. Handlers should process the line as
2211
# needed, update the cache AND log it (so that the input cache array
2214
#.....................................................................
2217
#if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2219
# save the line away in case we crash, so the post-mortem handler can
2221
self._last_input_line = line
2223
#print '***line: <%s>' % line # dbg
2226
# Return immediately on purely empty lines, so that if the user
2227
# previously typed some whitespace that started a continuation
2228
# prompt, he can break out of that loop with just an empty line.
2229
# This is how the default python prompt works.
2231
# Only return if the accumulated input buffer was just whitespace!
2232
if ''.join(self.buffer).isspace():
2236
line_info = prefilter.LineInfo(line, continue_prompt)
2238
# the input history needs to track even empty lines
2239
stripped = line.strip()
2242
if not continue_prompt:
2243
self.outputcache.prompt_count -= 1
2244
return self.handle_normal(line_info)
2246
# print '***cont',continue_prompt # dbg
2247
# special handlers are only allowed for single line statements
2248
if continue_prompt and not self.rc.multi_line_specials:
2249
return self.handle_normal(line_info)
2252
# See whether any pre-existing handler can take care of it
2253
rewritten = self.hooks.input_prefilter(stripped)
2254
if rewritten != stripped: # ok, some prefilter did something
2255
rewritten = line_info.pre + rewritten # add indentation
2256
return self.handle_normal(prefilter.LineInfo(rewritten,
2259
#print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2261
return prefilter.prefilter(line_info, self)
2264
def _prefilter_dumb(self, line, continue_prompt):
2265
"""simple prefilter function, for debugging"""
2266
return self.handle_normal(line,continue_prompt)
2269
def multiline_prefilter(self, line, continue_prompt):
2270
""" Run _prefilter for each line of input
2272
Covers cases where there are multiple lines in the user entry,
2273
which is the case when the user goes back to a multiline history
2274
entry and presses enter.
2278
for l in line.rstrip('\n').split('\n'):
2279
out.append(self._prefilter(l, continue_prompt))
2280
return '\n'.join(out)
2282
# Set the default prefilter() function (this can be user-overridden)
2283
prefilter = multiline_prefilter
2285
def handle_normal(self,line_info):
2286
"""Handle normal input lines. Use as a template for handlers."""
2288
# With autoindent on, we need some way to exit the input loop, and I
2289
# don't want to force the user to have to backspace all the way to
2290
# clear the line. The rule will be in this case, that either two
2291
# lines of pure whitespace in a row, or a line of pure whitespace but
2292
# of a size different to the indent level, will exit the input loop.
2293
line = line_info.line
2294
continue_prompt = line_info.continue_prompt
2296
if (continue_prompt and self.autoindent and line.isspace() and
2297
(0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2298
(self.buffer[-1]).isspace() )):
2301
self.log(line,line,continue_prompt)
2304
def handle_alias(self,line_info):
2305
"""Handle alias input lines. """
2306
tgt = self.alias_table[line_info.iFun]
2307
# print "=>",tgt #dbg
2309
if '$' in line_info.line:
2310
call_meth = '(_ip, _ip.itpl(%s))'
2312
call_meth = '(_ip,%s)'
2313
line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2315
make_quoted_expr(line_info.line))
2317
transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2319
# pre is needed, because it carries the leading whitespace. Otherwise
2320
# aliases won't work in indented sections.
2321
line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2322
make_quoted_expr( transformed ))
2324
self.log(line_info.line,line_out,line_info.continue_prompt)
2325
#print 'line out:',line_out # dbg
2328
def handle_shell_escape(self, line_info):
2329
"""Execute the line in a shell, empty return value"""
2330
#print 'line in :', `line` # dbg
2331
line = line_info.line
2332
if line.lstrip().startswith('!!'):
2333
# rewrite LineInfo's line, iFun and theRest to properly hold the
2334
# call to %sx and the actual command to be executed, so
2335
# handle_magic can work correctly. Note that this works even if
2336
# the line is indented, so it handles multi_line_specials
2338
new_rest = line.lstrip()[2:]
2339
line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2340
line_info.iFun = 'sx'
2341
line_info.theRest = new_rest
2342
return self.handle_magic(line_info)
2344
cmd = line.lstrip().lstrip('!')
2345
line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2346
make_quoted_expr(cmd))
2347
# update cache/log and return
2348
self.log(line,line_out,line_info.continue_prompt)
2351
def handle_magic(self, line_info):
2352
"""Execute magic functions."""
2353
iFun = line_info.iFun
2354
theRest = line_info.theRest
2355
cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2356
make_quoted_expr(iFun + " " + theRest))
2357
self.log(line_info.line,cmd,line_info.continue_prompt)
2358
#print 'in handle_magic, cmd=<%s>' % cmd # dbg
2361
def handle_auto(self, line_info):
2362
"""Hande lines which can be auto-executed, quoting if requested."""
2364
#print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2365
line = line_info.line
2366
iFun = line_info.iFun
2367
theRest = line_info.theRest
2369
continue_prompt = line_info.continue_prompt
2370
obj = line_info.ofind(self)['obj']
2372
# This should only be active for single-line input!
2374
self.log(line,line,continue_prompt)
2377
force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2380
if pre == self.ESC_QUOTE:
2381
# Auto-quote splitting on whitespace
2382
newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2383
elif pre == self.ESC_QUOTE2:
2384
# Auto-quote whole string
2385
newcmd = '%s("%s")' % (iFun,theRest)
2386
elif pre == self.ESC_PAREN:
2387
newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2390
# We only apply it to argument-less calls if the autocall
2391
# parameter is set to 2. We only need to check that autocall is <
2392
# 2, since this function isn't called unless it's at least 1.
2393
if not theRest and (self.rc.autocall < 2) and not force_auto:
2394
newcmd = '%s %s' % (iFun,theRest)
2395
auto_rewrite = False
2397
if not force_auto and theRest.startswith('['):
2398
if hasattr(obj,'__getitem__'):
2399
# Don't autocall in this case: item access for an object
2400
# which is BOTH callable and implements __getitem__.
2401
newcmd = '%s %s' % (iFun,theRest)
2402
auto_rewrite = False
2404
# if the object doesn't support [] access, go ahead and
2406
newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2407
elif theRest.endswith(';'):
2408
newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2410
newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2413
rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2416
# plain ascii works better w/ pyreadline, on some machines, so
2417
# we use it and only print uncolored rewrite if we have unicode
2419
print >>Term.cout, rw
2420
except UnicodeEncodeError:
2421
print "-------------->" + newcmd
2423
# log what is now valid Python, not the actual user input (without the
2425
self.log(line,newcmd,continue_prompt)
2428
def handle_help(self, line_info):
2429
"""Try to get some help for the object.
2431
obj? or ?obj -> basic information.
2432
obj?? or ??obj -> more details.
2435
line = line_info.line
2436
# We need to make sure that we don't process lines which would be
2437
# otherwise valid python, such as "x=1 # what?"
2439
codeop.compile_command(line)
2441
# We should only handle as help stuff which is NOT valid syntax
2442
if line[0]==self.ESC_HELP:
2444
elif line[-1]==self.ESC_HELP:
2446
self.log(line,'#?'+line,line_info.continue_prompt)
2448
#print 'line:<%r>' % line # dbg
2449
self.magic_pinfo(line)
2451
page(self.usage,screen_lines=self.rc.screen_length)
2452
return '' # Empty string is needed here!
2454
# Pass any other exceptions through to the normal handler
2455
return self.handle_normal(line_info)
2457
# If the code compiles ok, we should handle it normally
2458
return self.handle_normal(line_info)
2461
""" Get an IPApi object for this shell instance
2463
Getting an IPApi object is always preferable to accessing the shell
2464
directly, but this holds true especially for extensions.
2466
It should always be possible to implement an extension with IPApi
2467
alone. If not, contact maintainer to request an addition.
2472
def handle_emacs(self, line_info):
2473
"""Handle input lines marked by python-mode."""
2475
# Currently, nothing is done. Later more functionality can be added
2478
# The input cache shouldn't be updated
2479
return line_info.line
2482
def mktempfile(self,data=None):
2483
"""Make a new tempfile and return its filename.
2485
This makes a call to tempfile.mktemp, but it registers the created
2486
filename internally so ipython cleans it up at exit time.
2490
- data(None): if data is given, it gets written out to the temp file
2491
immediately, and the file is closed again."""
2493
filename = tempfile.mktemp('.py','ipython_edit_')
2494
self.tempfiles.append(filename)
2497
tmp_file = open(filename,'w')
2498
tmp_file.write(data)
2502
def write(self,data):
2503
"""Write a string to the default output"""
2504
Term.cout.write(data)
2506
def write_err(self,data):
2507
"""Write a string to the default error output"""
2508
Term.cerr.write(data)
2511
"""Handle interactive exit.
2513
This method sets the exit_now attribute."""
2515
if self.rc.confirm_exit:
2516
if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2517
self.exit_now = True
2519
self.exit_now = True
2521
def safe_execfile(self,fname,*where,**kw):
2522
"""A safe version of the builtin execfile().
2524
This version will never throw an exception, and knows how to handle
2525
ipython logs as well.
2529
Name of the file to be executed.
2532
One or two namespaces, passed to execfile() as (globals,locals).
2533
If only one is given, it is passed as both.
2536
islog : boolean (False)
2538
quiet : boolean (True)
2540
exit_ignore : boolean (False)
2543
def syspath_cleanup():
2544
"""Internal cleanup routine for sys.path."""
2547
sys.path.remove(dname)
2549
# For some reason the user has already removed it, ignore.
2552
fname = os.path.expanduser(fname)
2554
# Find things also in current directory. This is needed to mimic the
2555
# behavior of running a script from the system command line, where
2556
# Python inserts the script's directory into sys.path
2557
dname = os.path.dirname(os.path.abspath(fname))
2559
if dname not in sys.path:
2560
sys.path.insert(0,dname)
2566
print >> Term.cerr, \
2567
'Could not open file <%s> for safe execution.' % fname
2571
kw.setdefault('islog',0)
2572
kw.setdefault('quiet',1)
2573
kw.setdefault('exit_ignore',0)
2575
first = xfile.readline()
2576
loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2578
# line by line execution
2579
if first.startswith(loghead) or kw['islog']:
2580
print 'Loading log file <%s> one line at a time...' % fname
2582
stdout_save = sys.stdout
2583
sys.stdout = StringIO.StringIO()
2585
globs,locs = where[0:2]
2588
globs = locs = where[0]
2590
globs = locs = globals()
2593
# we also need to identify indented blocks of code when replaying
2594
# logs and put them together before passing them to an exec
2595
# statement. This takes a bit of regexp and look-ahead work in the
2596
# file. It's easiest if we swallow the whole thing in memory
2597
# first, and manually walk through the lines list moving the
2598
# counter ourselves.
2599
indent_re = re.compile('\s+\S')
2601
filelines = xfile.readlines()
2603
nlines = len(filelines)
2605
while lnum < nlines:
2606
line = filelines[lnum]
2608
# don't re-insert logger status info into cache
2609
if line.startswith('#log#'):
2612
# build a block of code (maybe a single line) for execution
2615
next = filelines[lnum] # lnum has already incremented
2618
while next and indent_re.match(next):
2622
next = filelines[lnum]
2625
# now execute the block of one or more lines
2627
exec block in globs,locs
2631
badblocks.append(block.rstrip())
2632
if kw['quiet']: # restore stdout
2634
sys.stdout = stdout_save
2635
print 'Finished replaying log file <%s>' % fname
2637
print >> sys.stderr, ('\nThe following lines/blocks in file '
2638
'<%s> reported errors:' % fname)
2640
for badline in badblocks:
2641
print >> sys.stderr, badline
2642
else: # regular file execution
2644
if sys.platform == 'win32' and sys.version_info < (2,5,1):
2645
# Work around a bug in Python for Windows. The bug was
2646
# fixed in in Python 2.5 r54159 and 54158, but that's still
2647
# SVN Python as of March/07. For details, see:
2648
# http://projects.scipy.org/ipython/ipython/ticket/123
2650
globs,locs = where[0:2]
2653
globs = locs = where[0]
2655
globs = locs = globals()
2656
exec file(fname) in globs,locs
2658
execfile(fname,*where)
2660
self.showsyntaxerror()
2661
warn('Failure executing file: <%s>' % fname)
2662
except SystemExit,status:
2663
# Code that correctly sets the exit status flag to success (0)
2664
# shouldn't be bothered with a traceback. Note that a plain
2665
# sys.exit() does NOT set the message to 0 (it's empty) so that
2666
# will still get a traceback. Note that the structure of the
2667
# SystemExit exception changed between Python 2.4 and 2.5, so
2668
# the checks must be done in a version-dependent way.
2671
if sys.version_info[:2] > (2,5):
2672
if status.message!=0 and not kw['exit_ignore']:
2675
if status.code and not kw['exit_ignore']:
2678
self.showtraceback()
2679
warn('Failure executing file: <%s>' % fname)
2681
self.showtraceback()
2682
warn('Failure executing file: <%s>' % fname)
2686
#************************* end of file <iplib.py> *****************************