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