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

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

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