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