##// END OF EJS Templates
pdb support in threaded mode, replaced the crash handler with a verbose...
fperez -
Show More

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

@@ -1,2592 +1,2620 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 962 2005-12-28 18:04:59Z fperez $"""
4 $Id: Magic.py 965 2005-12-28 23:23:09Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Modules and globals
15 # Modules and globals
16
16
17 from IPython import Release
17 from IPython import Release
18 __author__ = '%s <%s>\n%s <%s>' % \
18 __author__ = '%s <%s>\n%s <%s>' % \
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 __license__ = Release.license
20 __license__ = Release.license
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
23 import __builtin__
24 import bdb
24 import bdb
25 import inspect
25 import inspect
26 import os
26 import os
27 import pdb
27 import pdb
28 import pydoc
28 import pydoc
29 import sys
29 import sys
30 import re
30 import re
31 import tempfile
31 import tempfile
32 import time
32 import time
33 from cStringIO import StringIO
33 from cStringIO import StringIO
34 from getopt import getopt
34 from getopt import getopt
35 from pprint import pprint, pformat
35 from pprint import pprint, pformat
36
36
37 # profile isn't bundled by default in Debian for license reasons
37 # profile isn't bundled by default in Debian for license reasons
38 try:
38 try:
39 import profile,pstats
39 import profile,pstats
40 except ImportError:
40 except ImportError:
41 profile = pstats = None
41 profile = pstats = None
42
42
43 # Homebrewed
43 # Homebrewed
44 from IPython import Debugger, OInspect, wildcard
44 from IPython import Debugger, OInspect, wildcard
45 from IPython.FakeModule import FakeModule
45 from IPython.FakeModule import FakeModule
46 from IPython.Itpl import Itpl, itpl, printpl,itplns
46 from IPython.Itpl import Itpl, itpl, printpl,itplns
47 from IPython.PyColorize import Parser
47 from IPython.PyColorize import Parser
48 from IPython.Struct import Struct
48 from IPython.Struct import Struct
49 from IPython.genutils import *
49 from IPython.genutils import *
50
50
51 # Globals to be set later by Magic constructor
51 # Globals to be set later by Magic constructor
52 MAGIC_PREFIX = ''
52 MAGIC_PREFIX = ''
53 MAGIC_ESCAPE = ''
53 MAGIC_ESCAPE = ''
54
54
55 #***************************************************************************
55 #***************************************************************************
56 # Utility functions
56 # Utility functions
57 def magic2python(cmd):
57 def magic2python(cmd):
58 """Convert a command string of magic syntax to valid Python code."""
58 """Convert a command string of magic syntax to valid Python code."""
59
59
60 if cmd.startswith('#'+MAGIC_ESCAPE) or \
60 if cmd.startswith('#'+MAGIC_ESCAPE) or \
61 cmd.startswith(MAGIC_ESCAPE):
61 cmd.startswith(MAGIC_ESCAPE):
62 if cmd[0]=='#':
62 if cmd[0]=='#':
63 cmd = cmd[1:]
63 cmd = cmd[1:]
64 # we need to return the proper line end later
64 # we need to return the proper line end later
65 if cmd[-1] == '\n':
65 if cmd[-1] == '\n':
66 endl = '\n'
66 endl = '\n'
67 else:
67 else:
68 endl = ''
68 endl = ''
69 try:
69 try:
70 func,args = cmd[1:].split(' ',1)
70 func,args = cmd[1:].split(' ',1)
71 except:
71 except:
72 func,args = cmd[1:].rstrip(),''
72 func,args = cmd[1:].rstrip(),''
73 args = args.replace('"','\\"').replace("'","\\'").rstrip()
73 args = args.replace('"','\\"').replace("'","\\'").rstrip()
74 return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl)
74 return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl)
75 else:
75 else:
76 return cmd
76 return cmd
77
77
78 def on_off(tag):
78 def on_off(tag):
79 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
79 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
80 return ['OFF','ON'][tag]
80 return ['OFF','ON'][tag]
81
81
82
82
83 #****************************************************************************
83 #****************************************************************************
84 # Utility classes
84 # Utility classes
85 class Macro:
85 class Macro:
86 """Simple class to store the value of macros as strings.
86 """Simple class to store the value of macros as strings.
87
87
88 This allows us to later exec them by checking when something is an
88 This allows us to later exec them by checking when something is an
89 instance of this class."""
89 instance of this class."""
90
90
91 def __init__(self,cmds):
91 def __init__(self,cmds):
92 """Build a macro from a list of commands."""
92 """Build a macro from a list of commands."""
93
93
94 # Since the list may include multi-line entries, first make sure that
94 # Since the list may include multi-line entries, first make sure that
95 # they've been all broken up before passing it to magic2python
95 # they've been all broken up before passing it to magic2python
96 cmdlist = map(magic2python,''.join(cmds).split('\n'))
96 cmdlist = map(magic2python,''.join(cmds).split('\n'))
97 self.value = '\n'.join(cmdlist)
97 self.value = '\n'.join(cmdlist)
98
98
99 def __str__(self):
99 def __str__(self):
100 return self.value
100 return self.value
101
101
102 #***************************************************************************
102 #***************************************************************************
103 # Main class implementing Magic functionality
103 # Main class implementing Magic functionality
104 class Magic:
104 class Magic:
105 """Magic functions for InteractiveShell.
105 """Magic functions for InteractiveShell.
106
106
107 Shell functions which can be reached as %function_name. All magic
107 Shell functions which can be reached as %function_name. All magic
108 functions should accept a string, which they can parse for their own
108 functions should accept a string, which they can parse for their own
109 needs. This can make some functions easier to type, eg `%cd ../`
109 needs. This can make some functions easier to type, eg `%cd ../`
110 vs. `%cd("../")`
110 vs. `%cd("../")`
111
111
112 ALL definitions MUST begin with the prefix magic_. The user won't need it
112 ALL definitions MUST begin with the prefix magic_. The user won't need it
113 at the command line, but it is is needed in the definition. """
113 at the command line, but it is is needed in the definition. """
114
114
115 # class globals
115 # class globals
116 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
116 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
117 'Automagic is ON, % prefix NOT needed for magic functions.']
117 'Automagic is ON, % prefix NOT needed for magic functions.']
118
118
119 #......................................................................
119 #......................................................................
120 # some utility functions
120 # some utility functions
121
121
122 def __init__(self,shell):
122 def __init__(self,shell):
123 # XXX This is hackish, clean up later to avoid these messy globals
123 # XXX This is hackish, clean up later to avoid these messy globals
124 global MAGIC_PREFIX, MAGIC_ESCAPE
124 global MAGIC_PREFIX, MAGIC_ESCAPE
125
125
126 self.options_table = {}
126 self.options_table = {}
127 MAGIC_PREFIX = shell.name+'.magic_'
127 MAGIC_PREFIX = shell.name+'.magic_'
128 MAGIC_ESCAPE = shell.ESC_MAGIC
128 MAGIC_ESCAPE = shell.ESC_MAGIC
129 if profile is None:
129 if profile is None:
130 self.magic_prun = self.profile_missing_notice
130 self.magic_prun = self.profile_missing_notice
131
131
132 def profile_missing_notice(self, *args, **kwargs):
132 def profile_missing_notice(self, *args, **kwargs):
133 error("""\
133 error("""\
134 The profile module could not be found. If you are a Debian user,
134 The profile module could not be found. If you are a Debian user,
135 it has been removed from the standard Debian package because of its non-free
135 it has been removed from the standard Debian package because of its non-free
136 license. To use profiling, please install"python2.3-profiler" from non-free.""")
136 license. To use profiling, please install"python2.3-profiler" from non-free.""")
137
137
138 def default_option(self,fn,optstr):
138 def default_option(self,fn,optstr):
139 """Make an entry in the options_table for fn, with value optstr"""
139 """Make an entry in the options_table for fn, with value optstr"""
140
140
141 if fn not in self.lsmagic():
141 if fn not in self.lsmagic():
142 error("%s is not a magic function" % fn)
142 error("%s is not a magic function" % fn)
143 self.options_table[fn] = optstr
143 self.options_table[fn] = optstr
144
144
145 def lsmagic(self):
145 def lsmagic(self):
146 """Return a list of currently available magic functions.
146 """Return a list of currently available magic functions.
147
147
148 Gives a list of the bare names after mangling (['ls','cd', ...], not
148 Gives a list of the bare names after mangling (['ls','cd', ...], not
149 ['magic_ls','magic_cd',...]"""
149 ['magic_ls','magic_cd',...]"""
150
150
151 # FIXME. This needs a cleanup, in the way the magics list is built.
151 # FIXME. This needs a cleanup, in the way the magics list is built.
152
152
153 # magics in class definition
153 # magics in class definition
154 class_magic = lambda fn: fn.startswith('magic_') and \
154 class_magic = lambda fn: fn.startswith('magic_') and \
155 callable(Magic.__dict__[fn])
155 callable(Magic.__dict__[fn])
156 # in instance namespace (run-time user additions)
156 # in instance namespace (run-time user additions)
157 inst_magic = lambda fn: fn.startswith('magic_') and \
157 inst_magic = lambda fn: fn.startswith('magic_') and \
158 callable(self.__dict__[fn])
158 callable(self.__dict__[fn])
159 # and bound magics by user (so they can access self):
159 # and bound magics by user (so they can access self):
160 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
160 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
161 callable(self.__class__.__dict__[fn])
161 callable(self.__class__.__dict__[fn])
162 magics = filter(class_magic,Magic.__dict__.keys()) + \
162 magics = filter(class_magic,Magic.__dict__.keys()) + \
163 filter(inst_magic,self.__dict__.keys()) + \
163 filter(inst_magic,self.__dict__.keys()) + \
164 filter(inst_bound_magic,self.__class__.__dict__.keys())
164 filter(inst_bound_magic,self.__class__.__dict__.keys())
165 out = []
165 out = []
166 for fn in magics:
166 for fn in magics:
167 out.append(fn.replace('magic_','',1))
167 out.append(fn.replace('magic_','',1))
168 out.sort()
168 out.sort()
169 return out
169 return out
170
170
171 def set_shell(self,shell):
171 def set_shell(self,shell):
172 self.shell = shell
172 self.shell = shell
173 self.alias_table = shell.alias_table
173 self.alias_table = shell.alias_table
174
174
175 def extract_input_slices(self,slices):
175 def extract_input_slices(self,slices):
176 """Return as a string a set of input history slices.
176 """Return as a string a set of input history slices.
177
177
178 The set of slices is given as a list of strings (like ['1','4:8','9'],
178 The set of slices is given as a list of strings (like ['1','4:8','9'],
179 since this function is for use by magic functions which get their
179 since this function is for use by magic functions which get their
180 arguments as strings."""
180 arguments as strings."""
181
181
182 cmds = []
182 cmds = []
183 for chunk in slices:
183 for chunk in slices:
184 if ':' in chunk:
184 if ':' in chunk:
185 ini,fin = map(int,chunk.split(':'))
185 ini,fin = map(int,chunk.split(':'))
186 else:
186 else:
187 ini = int(chunk)
187 ini = int(chunk)
188 fin = ini+1
188 fin = ini+1
189 cmds.append(self.shell.input_hist[ini:fin])
189 cmds.append(self.shell.input_hist[ini:fin])
190 return cmds
190 return cmds
191
191
192 def _ofind(self,oname):
192 def _ofind(self,oname):
193 """Find an object in the available namespaces.
193 """Find an object in the available namespaces.
194
194
195 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
195 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
196
196
197 Has special code to detect magic functions.
197 Has special code to detect magic functions.
198 """
198 """
199
199
200 oname = oname.strip()
200 oname = oname.strip()
201
201
202 # Namespaces to search in:
202 # Namespaces to search in:
203 user_ns = self.shell.user_ns
203 user_ns = self.shell.user_ns
204 internal_ns = self.shell.internal_ns
204 internal_ns = self.shell.internal_ns
205 builtin_ns = __builtin__.__dict__
205 builtin_ns = __builtin__.__dict__
206 alias_ns = self.shell.alias_table
206 alias_ns = self.shell.alias_table
207
207
208 # Put them in a list. The order is important so that we find things in
208 # Put them in a list. The order is important so that we find things in
209 # the same order that Python finds them.
209 # the same order that Python finds them.
210 namespaces = [ ('Interactive',user_ns),
210 namespaces = [ ('Interactive',user_ns),
211 ('IPython internal',internal_ns),
211 ('IPython internal',internal_ns),
212 ('Python builtin',builtin_ns),
212 ('Python builtin',builtin_ns),
213 ('Alias',alias_ns),
213 ('Alias',alias_ns),
214 ]
214 ]
215
215
216 # initialize results to 'null'
216 # initialize results to 'null'
217 found = 0; obj = None; ospace = None; ds = None;
217 found = 0; obj = None; ospace = None; ds = None;
218 ismagic = 0; isalias = 0
218 ismagic = 0; isalias = 0
219
219
220 # Look for the given name by splitting it in parts. If the head is
220 # Look for the given name by splitting it in parts. If the head is
221 # found, then we look for all the remaining parts as members, and only
221 # found, then we look for all the remaining parts as members, and only
222 # declare success if we can find them all.
222 # declare success if we can find them all.
223 oname_parts = oname.split('.')
223 oname_parts = oname.split('.')
224 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
224 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
225 for nsname,ns in namespaces:
225 for nsname,ns in namespaces:
226 try:
226 try:
227 obj = ns[oname_head]
227 obj = ns[oname_head]
228 except KeyError:
228 except KeyError:
229 continue
229 continue
230 else:
230 else:
231 for part in oname_rest:
231 for part in oname_rest:
232 try:
232 try:
233 obj = getattr(obj,part)
233 obj = getattr(obj,part)
234 except:
234 except:
235 # Blanket except b/c some badly implemented objects
235 # Blanket except b/c some badly implemented objects
236 # allow __getattr__ to raise exceptions other than
236 # allow __getattr__ to raise exceptions other than
237 # AttributeError, which then crashes IPython.
237 # AttributeError, which then crashes IPython.
238 break
238 break
239 else:
239 else:
240 # If we finish the for loop (no break), we got all members
240 # If we finish the for loop (no break), we got all members
241 found = 1
241 found = 1
242 ospace = nsname
242 ospace = nsname
243 if ns == alias_ns:
243 if ns == alias_ns:
244 isalias = 1
244 isalias = 1
245 break # namespace loop
245 break # namespace loop
246
246
247 # Try to see if it's magic
247 # Try to see if it's magic
248 if not found:
248 if not found:
249 if oname.startswith(self.shell.ESC_MAGIC):
249 if oname.startswith(self.shell.ESC_MAGIC):
250 oname = oname[1:]
250 oname = oname[1:]
251 obj = getattr(self,'magic_'+oname,None)
251 obj = getattr(self,'magic_'+oname,None)
252 if obj is not None:
252 if obj is not None:
253 found = 1
253 found = 1
254 ospace = 'IPython internal'
254 ospace = 'IPython internal'
255 ismagic = 1
255 ismagic = 1
256
256
257 # Last try: special-case some literals like '', [], {}, etc:
257 # Last try: special-case some literals like '', [], {}, etc:
258 if not found and oname_head in ["''",'""','[]','{}','()']:
258 if not found and oname_head in ["''",'""','[]','{}','()']:
259 obj = eval(oname_head)
259 obj = eval(oname_head)
260 found = 1
260 found = 1
261 ospace = 'Interactive'
261 ospace = 'Interactive'
262
262
263 return {'found':found, 'obj':obj, 'namespace':ospace,
263 return {'found':found, 'obj':obj, 'namespace':ospace,
264 'ismagic':ismagic, 'isalias':isalias}
264 'ismagic':ismagic, 'isalias':isalias}
265
265
266 def arg_err(self,func):
266 def arg_err(self,func):
267 """Print docstring if incorrect arguments were passed"""
267 """Print docstring if incorrect arguments were passed"""
268 print 'Error in arguments:'
268 print 'Error in arguments:'
269 print OInspect.getdoc(func)
269 print OInspect.getdoc(func)
270
270
271
271
272 def format_latex(self,str):
272 def format_latex(self,str):
273 """Format a string for latex inclusion."""
273 """Format a string for latex inclusion."""
274
274
275 # Characters that need to be escaped for latex:
275 # Characters that need to be escaped for latex:
276 escape_re = re.compile(r'(%|_|\$)',re.MULTILINE)
276 escape_re = re.compile(r'(%|_|\$)',re.MULTILINE)
277 # Magic command names as headers:
277 # Magic command names as headers:
278 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
278 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
279 re.MULTILINE)
279 re.MULTILINE)
280 # Magic commands
280 # Magic commands
281 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
281 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
282 re.MULTILINE)
282 re.MULTILINE)
283 # Paragraph continue
283 # Paragraph continue
284 par_re = re.compile(r'\\$',re.MULTILINE)
284 par_re = re.compile(r'\\$',re.MULTILINE)
285
285
286 str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str)
286 str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str)
287 str = cmd_re.sub(r'\\texttt{\g<cmd>}',str)
287 str = cmd_re.sub(r'\\texttt{\g<cmd>}',str)
288 str = par_re.sub(r'\\\\',str)
288 str = par_re.sub(r'\\\\',str)
289 str = escape_re.sub(r'\\\1',str)
289 str = escape_re.sub(r'\\\1',str)
290 return str
290 return str
291
291
292 def format_screen(self,str):
292 def format_screen(self,str):
293 """Format a string for screen printing.
293 """Format a string for screen printing.
294
294
295 This removes some latex-type format codes."""
295 This removes some latex-type format codes."""
296 # Paragraph continue
296 # Paragraph continue
297 par_re = re.compile(r'\\$',re.MULTILINE)
297 par_re = re.compile(r'\\$',re.MULTILINE)
298 str = par_re.sub('',str)
298 str = par_re.sub('',str)
299 return str
299 return str
300
300
301 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
301 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
302 """Parse options passed to an argument string.
302 """Parse options passed to an argument string.
303
303
304 The interface is similar to that of getopt(), but it returns back a
304 The interface is similar to that of getopt(), but it returns back a
305 Struct with the options as keys and the stripped argument string still
305 Struct with the options as keys and the stripped argument string still
306 as a string.
306 as a string.
307
307
308 arg_str is quoted as a true sys.argv vector by using shlex.split.
308 arg_str is quoted as a true sys.argv vector by using shlex.split.
309 This allows us to easily expand variables, glob files, quote
309 This allows us to easily expand variables, glob files, quote
310 arguments, etc.
310 arguments, etc.
311
311
312 Options:
312 Options:
313 -mode: default 'string'. If given as 'list', the argument string is
313 -mode: default 'string'. If given as 'list', the argument string is
314 returned as a list (split on whitespace) instead of a string.
314 returned as a list (split on whitespace) instead of a string.
315
315
316 -list_all: put all option values in lists. Normally only options
316 -list_all: put all option values in lists. Normally only options
317 appearing more than once are put in a list."""
317 appearing more than once are put in a list."""
318
318
319 # inject default options at the beginning of the input line
319 # inject default options at the beginning of the input line
320 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
320 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
321 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
321 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
322
322
323 mode = kw.get('mode','string')
323 mode = kw.get('mode','string')
324 if mode not in ['string','list']:
324 if mode not in ['string','list']:
325 raise ValueError,'incorrect mode given: %s' % mode
325 raise ValueError,'incorrect mode given: %s' % mode
326 # Get options
326 # Get options
327 list_all = kw.get('list_all',0)
327 list_all = kw.get('list_all',0)
328
328
329 # Check if we have more than one argument to warrant extra processing:
329 # Check if we have more than one argument to warrant extra processing:
330 odict = {} # Dictionary with options
330 odict = {} # Dictionary with options
331 args = arg_str.split()
331 args = arg_str.split()
332 if len(args) >= 1:
332 if len(args) >= 1:
333 # If the list of inputs only has 0 or 1 thing in it, there's no
333 # If the list of inputs only has 0 or 1 thing in it, there's no
334 # need to look for options
334 # need to look for options
335 argv = shlex_split(arg_str)
335 argv = shlex_split(arg_str)
336 # Do regular option processing
336 # Do regular option processing
337 opts,args = getopt(argv,opt_str,*long_opts)
337 opts,args = getopt(argv,opt_str,*long_opts)
338 for o,a in opts:
338 for o,a in opts:
339 if o.startswith('--'):
339 if o.startswith('--'):
340 o = o[2:]
340 o = o[2:]
341 else:
341 else:
342 o = o[1:]
342 o = o[1:]
343 try:
343 try:
344 odict[o].append(a)
344 odict[o].append(a)
345 except AttributeError:
345 except AttributeError:
346 odict[o] = [odict[o],a]
346 odict[o] = [odict[o],a]
347 except KeyError:
347 except KeyError:
348 if list_all:
348 if list_all:
349 odict[o] = [a]
349 odict[o] = [a]
350 else:
350 else:
351 odict[o] = a
351 odict[o] = a
352
352
353 # Prepare opts,args for return
353 # Prepare opts,args for return
354 opts = Struct(odict)
354 opts = Struct(odict)
355 if mode == 'string':
355 if mode == 'string':
356 args = ' '.join(args)
356 args = ' '.join(args)
357
357
358 return opts,args
358 return opts,args
359
359
360 #......................................................................
360 #......................................................................
361 # And now the actual magic functions
361 # And now the actual magic functions
362
362
363 # Functions for IPython shell work (vars,funcs, config, etc)
363 # Functions for IPython shell work (vars,funcs, config, etc)
364 def magic_lsmagic(self, parameter_s = ''):
364 def magic_lsmagic(self, parameter_s = ''):
365 """List currently available magic functions."""
365 """List currently available magic functions."""
366 mesc = self.shell.ESC_MAGIC
366 mesc = self.shell.ESC_MAGIC
367 print 'Available magic functions:\n'+mesc+\
367 print 'Available magic functions:\n'+mesc+\
368 (' '+mesc).join(self.lsmagic())
368 (' '+mesc).join(self.lsmagic())
369 print '\n' + Magic.auto_status[self.shell.rc.automagic]
369 print '\n' + Magic.auto_status[self.shell.rc.automagic]
370 return None
370 return None
371
371
372 def magic_magic(self, parameter_s = ''):
372 def magic_magic(self, parameter_s = ''):
373 """Print information about the magic function system."""
373 """Print information about the magic function system."""
374
374
375 mode = ''
375 mode = ''
376 try:
376 try:
377 if parameter_s.split()[0] == '-latex':
377 if parameter_s.split()[0] == '-latex':
378 mode = 'latex'
378 mode = 'latex'
379 except:
379 except:
380 pass
380 pass
381
381
382 magic_docs = []
382 magic_docs = []
383 for fname in self.lsmagic():
383 for fname in self.lsmagic():
384 mname = 'magic_' + fname
384 mname = 'magic_' + fname
385 for space in (Magic,self,self.__class__):
385 for space in (Magic,self,self.__class__):
386 try:
386 try:
387 fn = space.__dict__[mname]
387 fn = space.__dict__[mname]
388 except KeyError:
388 except KeyError:
389 pass
389 pass
390 else:
390 else:
391 break
391 break
392 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
392 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
393 fname,fn.__doc__))
393 fname,fn.__doc__))
394 magic_docs = ''.join(magic_docs)
394 magic_docs = ''.join(magic_docs)
395
395
396 if mode == 'latex':
396 if mode == 'latex':
397 print self.format_latex(magic_docs)
397 print self.format_latex(magic_docs)
398 return
398 return
399 else:
399 else:
400 magic_docs = self.format_screen(magic_docs)
400 magic_docs = self.format_screen(magic_docs)
401
401
402 outmsg = """
402 outmsg = """
403 IPython's 'magic' functions
403 IPython's 'magic' functions
404 ===========================
404 ===========================
405
405
406 The magic function system provides a series of functions which allow you to
406 The magic function system provides a series of functions which allow you to
407 control the behavior of IPython itself, plus a lot of system-type
407 control the behavior of IPython itself, plus a lot of system-type
408 features. All these functions are prefixed with a % character, but parameters
408 features. All these functions are prefixed with a % character, but parameters
409 are given without parentheses or quotes.
409 are given without parentheses or quotes.
410
410
411 NOTE: If you have 'automagic' enabled (via the command line option or with the
411 NOTE: If you have 'automagic' enabled (via the command line option or with the
412 %automagic function), you don't need to type in the % explicitly. By default,
412 %automagic function), you don't need to type in the % explicitly. By default,
413 IPython ships with automagic on, so you should only rarely need the % escape.
413 IPython ships with automagic on, so you should only rarely need the % escape.
414
414
415 Example: typing '%cd mydir' (without the quotes) changes you working directory
415 Example: typing '%cd mydir' (without the quotes) changes you working directory
416 to 'mydir', if it exists.
416 to 'mydir', if it exists.
417
417
418 You can define your own magic functions to extend the system. See the supplied
418 You can define your own magic functions to extend the system. See the supplied
419 ipythonrc and example-magic.py files for details (in your ipython
419 ipythonrc and example-magic.py files for details (in your ipython
420 configuration directory, typically $HOME/.ipython/).
420 configuration directory, typically $HOME/.ipython/).
421
421
422 You can also define your own aliased names for magic functions. In your
422 You can also define your own aliased names for magic functions. In your
423 ipythonrc file, placing a line like:
423 ipythonrc file, placing a line like:
424
424
425 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
425 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
426
426
427 will define %pf as a new name for %profile.
427 will define %pf as a new name for %profile.
428
428
429 You can also call magics in code using the ipmagic() function, which IPython
429 You can also call magics in code using the ipmagic() function, which IPython
430 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
430 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
431
431
432 For a list of the available magic functions, use %lsmagic. For a description
432 For a list of the available magic functions, use %lsmagic. For a description
433 of any of them, type %magic_name?, e.g. '%cd?'.
433 of any of them, type %magic_name?, e.g. '%cd?'.
434
434
435 Currently the magic system has the following functions:\n"""
435 Currently the magic system has the following functions:\n"""
436
436
437 mesc = self.shell.ESC_MAGIC
437 mesc = self.shell.ESC_MAGIC
438 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
438 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
439 "\n\n%s%s\n\n%s" % (outmsg,
439 "\n\n%s%s\n\n%s" % (outmsg,
440 magic_docs,mesc,mesc,
440 magic_docs,mesc,mesc,
441 (' '+mesc).join(self.lsmagic()),
441 (' '+mesc).join(self.lsmagic()),
442 Magic.auto_status[self.shell.rc.automagic] ) )
442 Magic.auto_status[self.shell.rc.automagic] ) )
443
443
444 page(outmsg,screen_lines=self.shell.rc.screen_length)
444 page(outmsg,screen_lines=self.shell.rc.screen_length)
445
445
446 def magic_automagic(self, parameter_s = ''):
446 def magic_automagic(self, parameter_s = ''):
447 """Make magic functions callable without having to type the initial %.
447 """Make magic functions callable without having to type the initial %.
448
448
449 Toggles on/off (when off, you must call it as %automagic, of
449 Toggles on/off (when off, you must call it as %automagic, of
450 course). Note that magic functions have lowest priority, so if there's
450 course). Note that magic functions have lowest priority, so if there's
451 a variable whose name collides with that of a magic fn, automagic
451 a variable whose name collides with that of a magic fn, automagic
452 won't work for that function (you get the variable instead). However,
452 won't work for that function (you get the variable instead). However,
453 if you delete the variable (del var), the previously shadowed magic
453 if you delete the variable (del var), the previously shadowed magic
454 function becomes visible to automagic again."""
454 function becomes visible to automagic again."""
455
455
456 rc = self.shell.rc
456 rc = self.shell.rc
457 rc.automagic = not rc.automagic
457 rc.automagic = not rc.automagic
458 print '\n' + Magic.auto_status[rc.automagic]
458 print '\n' + Magic.auto_status[rc.automagic]
459
459
460 def magic_autocall(self, parameter_s = ''):
460 def magic_autocall(self, parameter_s = ''):
461 """Make functions callable without having to type parentheses.
461 """Make functions callable without having to type parentheses.
462
462
463 This toggles the autocall command line option on and off."""
463 This toggles the autocall command line option on and off."""
464
464
465 rc = self.shell.rc
465 rc = self.shell.rc
466 rc.autocall = not rc.autocall
466 rc.autocall = not rc.autocall
467 print "Automatic calling is:",['OFF','ON'][rc.autocall]
467 print "Automatic calling is:",['OFF','ON'][rc.autocall]
468
468
469 def magic_autoindent(self, parameter_s = ''):
469 def magic_autoindent(self, parameter_s = ''):
470 """Toggle autoindent on/off (if available)."""
470 """Toggle autoindent on/off (if available)."""
471
471
472 self.shell.set_autoindent()
472 self.shell.set_autoindent()
473 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
473 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
474
474
475 def magic_system_verbose(self, parameter_s = ''):
475 def magic_system_verbose(self, parameter_s = ''):
476 """Toggle verbose printing of system calls on/off."""
476 """Toggle verbose printing of system calls on/off."""
477
477
478 self.shell.rc_set_toggle('system_verbose')
478 self.shell.rc_set_toggle('system_verbose')
479 print "System verbose printing is:",\
479 print "System verbose printing is:",\
480 ['OFF','ON'][self.shell.rc.system_verbose]
480 ['OFF','ON'][self.shell.rc.system_verbose]
481
481
482 def magic_history(self, parameter_s = ''):
482 def magic_history(self, parameter_s = ''):
483 """Print input history (_i<n> variables), with most recent last.
483 """Print input history (_i<n> variables), with most recent last.
484
484
485 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
485 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
486 %history [-n] n -> print at most n inputs\\
486 %history [-n] n -> print at most n inputs\\
487 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
487 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
488
488
489 Each input's number <n> is shown, and is accessible as the
489 Each input's number <n> is shown, and is accessible as the
490 automatically generated variable _i<n>. Multi-line statements are
490 automatically generated variable _i<n>. Multi-line statements are
491 printed starting at a new line for easy copy/paste.
491 printed starting at a new line for easy copy/paste.
492
492
493 If option -n is used, input numbers are not printed. This is useful if
493 If option -n is used, input numbers are not printed. This is useful if
494 you want to get a printout of many lines which can be directly pasted
494 you want to get a printout of many lines which can be directly pasted
495 into a text editor.
495 into a text editor.
496
496
497 This feature is only available if numbered prompts are in use."""
497 This feature is only available if numbered prompts are in use."""
498
498
499 if not self.do_full_cache:
499 if not self.do_full_cache:
500 print 'This feature is only available if numbered prompts are in use.'
500 print 'This feature is only available if numbered prompts are in use.'
501 return
501 return
502 opts,args = self.parse_options(parameter_s,'n',mode='list')
502 opts,args = self.parse_options(parameter_s,'n',mode='list')
503
503
504 default_length = 40
504 default_length = 40
505 if len(args) == 0:
505 if len(args) == 0:
506 final = self.outputcache.prompt_count
506 final = self.outputcache.prompt_count
507 init = max(1,final-default_length)
507 init = max(1,final-default_length)
508 elif len(args) == 1:
508 elif len(args) == 1:
509 final = self.outputcache.prompt_count
509 final = self.outputcache.prompt_count
510 init = max(1,final-int(args[0]))
510 init = max(1,final-int(args[0]))
511 elif len(args) == 2:
511 elif len(args) == 2:
512 init,final = map(int,args)
512 init,final = map(int,args)
513 else:
513 else:
514 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
514 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
515 print self.magic_hist.__doc__
515 print self.magic_hist.__doc__
516 return
516 return
517 width = len(str(final))
517 width = len(str(final))
518 line_sep = ['','\n']
518 line_sep = ['','\n']
519 input_hist = self.shell.input_hist
519 input_hist = self.shell.input_hist
520 print_nums = not opts.has_key('n')
520 print_nums = not opts.has_key('n')
521 for in_num in range(init,final):
521 for in_num in range(init,final):
522 inline = input_hist[in_num]
522 inline = input_hist[in_num]
523 multiline = inline.count('\n') > 1
523 multiline = inline.count('\n') > 1
524 if print_nums:
524 if print_nums:
525 print str(in_num).ljust(width)+':'+ line_sep[multiline],
525 print str(in_num).ljust(width)+':'+ line_sep[multiline],
526 if inline.startswith('#'+self.shell.ESC_MAGIC) or \
526 if inline.startswith('#'+self.shell.ESC_MAGIC) or \
527 inline.startswith('#!'):
527 inline.startswith('#!'):
528 print inline[1:],
528 print inline[1:],
529 else:
529 else:
530 print inline,
530 print inline,
531
531
532 def magic_hist(self, parameter_s=''):
532 def magic_hist(self, parameter_s=''):
533 """Alternate name for %history."""
533 """Alternate name for %history."""
534 return self.magic_history(parameter_s)
534 return self.magic_history(parameter_s)
535
535
536 def magic_p(self, parameter_s=''):
536 def magic_p(self, parameter_s=''):
537 """Just a short alias for Python's 'print'."""
537 """Just a short alias for Python's 'print'."""
538 exec 'print ' + parameter_s in self.shell.user_ns
538 exec 'print ' + parameter_s in self.shell.user_ns
539
539
540 def magic_r(self, parameter_s=''):
540 def magic_r(self, parameter_s=''):
541 """Repeat previous input.
541 """Repeat previous input.
542
542
543 If given an argument, repeats the previous command which starts with
543 If given an argument, repeats the previous command which starts with
544 the same string, otherwise it just repeats the previous input.
544 the same string, otherwise it just repeats the previous input.
545
545
546 Shell escaped commands (with ! as first character) are not recognized
546 Shell escaped commands (with ! as first character) are not recognized
547 by this system, only pure python code and magic commands.
547 by this system, only pure python code and magic commands.
548 """
548 """
549
549
550 start = parameter_s.strip()
550 start = parameter_s.strip()
551 esc_magic = self.shell.ESC_MAGIC
551 esc_magic = self.shell.ESC_MAGIC
552 # Identify magic commands even if automagic is on (which means
552 # Identify magic commands even if automagic is on (which means
553 # the in-memory version is different from that typed by the user).
553 # the in-memory version is different from that typed by the user).
554 if self.shell.rc.automagic:
554 if self.shell.rc.automagic:
555 start_magic = esc_magic+start
555 start_magic = esc_magic+start
556 else:
556 else:
557 start_magic = start
557 start_magic = start
558 # Look through the input history in reverse
558 # Look through the input history in reverse
559 for n in range(len(self.shell.input_hist)-2,0,-1):
559 for n in range(len(self.shell.input_hist)-2,0,-1):
560 input = self.shell.input_hist[n]
560 input = self.shell.input_hist[n]
561 # skip plain 'r' lines so we don't recurse to infinity
561 # skip plain 'r' lines so we don't recurse to infinity
562 if input != 'ipmagic("r")\n' and \
562 if input != 'ipmagic("r")\n' and \
563 (input.startswith(start) or input.startswith(start_magic)):
563 (input.startswith(start) or input.startswith(start_magic)):
564 #print 'match',`input` # dbg
564 #print 'match',`input` # dbg
565 if input.startswith(esc_magic):
565 if input.startswith(esc_magic):
566 input = magic2python(input)
566 input = magic2python(input)
567 #print 'modified',`input` # dbg
567 #print 'modified',`input` # dbg
568 print 'Executing:',input,
568 print 'Executing:',input,
569 exec input in self.shell.user_ns
569 exec input in self.shell.user_ns
570 return
570 return
571 print 'No previous input matching `%s` found.' % start
571 print 'No previous input matching `%s` found.' % start
572
572
573 def magic_page(self, parameter_s=''):
573 def magic_page(self, parameter_s=''):
574 """Pretty print the object and display it through a pager.
574 """Pretty print the object and display it through a pager.
575
575
576 If no parameter is given, use _ (last output)."""
576 If no parameter is given, use _ (last output)."""
577 # After a function contributed by Olivier Aubert, slightly modified.
577 # After a function contributed by Olivier Aubert, slightly modified.
578
578
579 oname = parameter_s and parameter_s or '_'
579 oname = parameter_s and parameter_s or '_'
580 info = self._ofind(oname)
580 info = self._ofind(oname)
581 if info['found']:
581 if info['found']:
582 page(pformat(info['obj']))
582 page(pformat(info['obj']))
583 else:
583 else:
584 print 'Object `%s` not found' % oname
584 print 'Object `%s` not found' % oname
585
585
586 def magic_profile(self, parameter_s=''):
586 def magic_profile(self, parameter_s=''):
587 """Print your currently active IPyhton profile."""
587 """Print your currently active IPyhton profile."""
588 if self.shell.rc.profile:
588 if self.shell.rc.profile:
589 printpl('Current IPython profile: $self.shell.rc.profile.')
589 printpl('Current IPython profile: $self.shell.rc.profile.')
590 else:
590 else:
591 print 'No profile active.'
591 print 'No profile active.'
592
592
593 def _inspect(self,meth,oname,**kw):
593 def _inspect(self,meth,oname,**kw):
594 """Generic interface to the inspector system.
594 """Generic interface to the inspector system.
595
595
596 This function is meant to be called by pdef, pdoc & friends."""
596 This function is meant to be called by pdef, pdoc & friends."""
597
597
598 oname = oname.strip()
598 oname = oname.strip()
599 info = Struct(self._ofind(oname))
599 info = Struct(self._ofind(oname))
600 if info.found:
600 if info.found:
601 pmethod = getattr(self.shell.inspector,meth)
601 pmethod = getattr(self.shell.inspector,meth)
602 formatter = info.ismagic and self.format_screen or None
602 formatter = info.ismagic and self.format_screen or None
603 if meth == 'pdoc':
603 if meth == 'pdoc':
604 pmethod(info.obj,oname,formatter)
604 pmethod(info.obj,oname,formatter)
605 elif meth == 'pinfo':
605 elif meth == 'pinfo':
606 pmethod(info.obj,oname,formatter,info,**kw)
606 pmethod(info.obj,oname,formatter,info,**kw)
607 else:
607 else:
608 pmethod(info.obj,oname)
608 pmethod(info.obj,oname)
609 else:
609 else:
610 print 'Object `%s` not found.' % oname
610 print 'Object `%s` not found.' % oname
611 return 'not found' # so callers can take other action
611 return 'not found' # so callers can take other action
612
612
613 def magic_pdef(self, parameter_s=''):
613 def magic_pdef(self, parameter_s=''):
614 """Print the definition header for any callable object.
614 """Print the definition header for any callable object.
615
615
616 If the object is a class, print the constructor information."""
616 If the object is a class, print the constructor information."""
617 self._inspect('pdef',parameter_s)
617 self._inspect('pdef',parameter_s)
618
618
619 def magic_pdoc(self, parameter_s=''):
619 def magic_pdoc(self, parameter_s=''):
620 """Print the docstring for an object.
620 """Print the docstring for an object.
621
621
622 If the given object is a class, it will print both the class and the
622 If the given object is a class, it will print both the class and the
623 constructor docstrings."""
623 constructor docstrings."""
624 self._inspect('pdoc',parameter_s)
624 self._inspect('pdoc',parameter_s)
625
625
626 def magic_psource(self, parameter_s=''):
626 def magic_psource(self, parameter_s=''):
627 """Print (or run through pager) the source code for an object."""
627 """Print (or run through pager) the source code for an object."""
628 self._inspect('psource',parameter_s)
628 self._inspect('psource',parameter_s)
629
629
630 def magic_pfile(self, parameter_s=''):
630 def magic_pfile(self, parameter_s=''):
631 """Print (or run through pager) the file where an object is defined.
631 """Print (or run through pager) the file where an object is defined.
632
632
633 The file opens at the line where the object definition begins. IPython
633 The file opens at the line where the object definition begins. IPython
634 will honor the environment variable PAGER if set, and otherwise will
634 will honor the environment variable PAGER if set, and otherwise will
635 do its best to print the file in a convenient form.
635 do its best to print the file in a convenient form.
636
636
637 If the given argument is not an object currently defined, IPython will
637 If the given argument is not an object currently defined, IPython will
638 try to interpret it as a filename (automatically adding a .py extension
638 try to interpret it as a filename (automatically adding a .py extension
639 if needed). You can thus use %pfile as a syntax highlighting code
639 if needed). You can thus use %pfile as a syntax highlighting code
640 viewer."""
640 viewer."""
641
641
642 # first interpret argument as an object name
642 # first interpret argument as an object name
643 out = self._inspect('pfile',parameter_s)
643 out = self._inspect('pfile',parameter_s)
644 # if not, try the input as a filename
644 # if not, try the input as a filename
645 if out == 'not found':
645 if out == 'not found':
646 try:
646 try:
647 filename = get_py_filename(parameter_s)
647 filename = get_py_filename(parameter_s)
648 except IOError,msg:
648 except IOError,msg:
649 print msg
649 print msg
650 return
650 return
651 page(self.shell.inspector.format(file(filename).read()))
651 page(self.shell.inspector.format(file(filename).read()))
652
652
653 def magic_pinfo(self, parameter_s=''):
653 def magic_pinfo(self, parameter_s=''):
654 """Provide detailed information about an object.
654 """Provide detailed information about an object.
655
655
656 '%pinfo object' is just a synonym for object? or ?object."""
656 '%pinfo object' is just a synonym for object? or ?object."""
657
657
658 #print 'pinfo par: <%s>' % parameter_s # dbg
658 #print 'pinfo par: <%s>' % parameter_s # dbg
659
659
660 # detail_level: 0 -> obj? , 1 -> obj??
660 # detail_level: 0 -> obj? , 1 -> obj??
661 detail_level = 0
661 detail_level = 0
662 # We need to detect if we got called as 'pinfo pinfo foo', which can
662 # We need to detect if we got called as 'pinfo pinfo foo', which can
663 # happen if the user types 'pinfo foo?' at the cmd line.
663 # happen if the user types 'pinfo foo?' at the cmd line.
664 pinfo,qmark1,oname,qmark2 = \
664 pinfo,qmark1,oname,qmark2 = \
665 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
665 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
666 if pinfo or qmark1 or qmark2:
666 if pinfo or qmark1 or qmark2:
667 detail_level = 1
667 detail_level = 1
668 if "*" in oname:
668 if "*" in oname:
669 self.magic_psearch(oname)
669 self.magic_psearch(oname)
670 else:
670 else:
671 self._inspect('pinfo',oname,detail_level=detail_level)
671 self._inspect('pinfo',oname,detail_level=detail_level)
672
672
673 def magic_psearch(self, parameter_s=''):
673 def magic_psearch(self, parameter_s=''):
674 """Search for object in namespaces by wildcard.
674 """Search for object in namespaces by wildcard.
675
675
676 %psearch [options] PATTERN [OBJECT TYPE]
676 %psearch [options] PATTERN [OBJECT TYPE]
677
677
678 Note: ? can be used as a synonym for %psearch, at the beginning or at
678 Note: ? can be used as a synonym for %psearch, at the beginning or at
679 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
679 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
680 rest of the command line must be unchanged (options come first), so
680 rest of the command line must be unchanged (options come first), so
681 for example the following forms are equivalent
681 for example the following forms are equivalent
682
682
683 %psearch -i a* function
683 %psearch -i a* function
684 -i a* function?
684 -i a* function?
685 ?-i a* function
685 ?-i a* function
686
686
687 Arguments:
687 Arguments:
688
688
689 PATTERN
689 PATTERN
690
690
691 where PATTERN is a string containing * as a wildcard similar to its
691 where PATTERN is a string containing * as a wildcard similar to its
692 use in a shell. The pattern is matched in all namespaces on the
692 use in a shell. The pattern is matched in all namespaces on the
693 search path. By default objects starting with a single _ are not
693 search path. By default objects starting with a single _ are not
694 matched, many IPython generated objects have a single
694 matched, many IPython generated objects have a single
695 underscore. The default is case insensitive matching. Matching is
695 underscore. The default is case insensitive matching. Matching is
696 also done on the attributes of objects and not only on the objects
696 also done on the attributes of objects and not only on the objects
697 in a module.
697 in a module.
698
698
699 [OBJECT TYPE]
699 [OBJECT TYPE]
700
700
701 Is the name of a python type from the types module. The name is
701 Is the name of a python type from the types module. The name is
702 given in lowercase without the ending type, ex. StringType is
702 given in lowercase without the ending type, ex. StringType is
703 written string. By adding a type here only objects matching the
703 written string. By adding a type here only objects matching the
704 given type are matched. Using all here makes the pattern match all
704 given type are matched. Using all here makes the pattern match all
705 types (this is the default).
705 types (this is the default).
706
706
707 Options:
707 Options:
708
708
709 -a: makes the pattern match even objects whose names start with a
709 -a: makes the pattern match even objects whose names start with a
710 single underscore. These names are normally ommitted from the
710 single underscore. These names are normally ommitted from the
711 search.
711 search.
712
712
713 -i/-c: make the pattern case insensitive/sensitive. If neither of
713 -i/-c: make the pattern case insensitive/sensitive. If neither of
714 these options is given, the default is read from your ipythonrc
714 these options is given, the default is read from your ipythonrc
715 file. The option name which sets this value is
715 file. The option name which sets this value is
716 'wildcards_case_sensitive'. If this option is not specified in your
716 'wildcards_case_sensitive'. If this option is not specified in your
717 ipythonrc file, IPython's internal default is to do a case sensitive
717 ipythonrc file, IPython's internal default is to do a case sensitive
718 search.
718 search.
719
719
720 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
720 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
721 specifiy can be searched in any of the following namespaces:
721 specifiy can be searched in any of the following namespaces:
722 'builtin', 'user', 'user_global','internal', 'alias', where
722 'builtin', 'user', 'user_global','internal', 'alias', where
723 'builtin' and 'user' are the search defaults. Note that you should
723 'builtin' and 'user' are the search defaults. Note that you should
724 not use quotes when specifying namespaces.
724 not use quotes when specifying namespaces.
725
725
726 'Builtin' contains the python module builtin, 'user' contains all
726 'Builtin' contains the python module builtin, 'user' contains all
727 user data, 'alias' only contain the shell aliases and no python
727 user data, 'alias' only contain the shell aliases and no python
728 objects, 'internal' contains objects used by IPython. The
728 objects, 'internal' contains objects used by IPython. The
729 'user_global' namespace is only used by embedded IPython instances,
729 'user_global' namespace is only used by embedded IPython instances,
730 and it contains module-level globals. You can add namespaces to the
730 and it contains module-level globals. You can add namespaces to the
731 search with -s or exclude them with -e (these options can be given
731 search with -s or exclude them with -e (these options can be given
732 more than once).
732 more than once).
733
733
734 Examples:
734 Examples:
735
735
736 %psearch a* -> objects beginning with an a
736 %psearch a* -> objects beginning with an a
737 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
737 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
738 %psearch a* function -> all functions beginning with an a
738 %psearch a* function -> all functions beginning with an a
739 %psearch re.e* -> objects beginning with an e in module re
739 %psearch re.e* -> objects beginning with an e in module re
740 %psearch r*.e* -> objects that start with e in modules starting in r
740 %psearch r*.e* -> objects that start with e in modules starting in r
741 %psearch r*.* string -> all strings in modules beginning with r
741 %psearch r*.* string -> all strings in modules beginning with r
742
742
743 Case sensitve search:
743 Case sensitve search:
744
744
745 %psearch -c a* list all object beginning with lower case a
745 %psearch -c a* list all object beginning with lower case a
746
746
747 Show objects beginning with a single _:
747 Show objects beginning with a single _:
748
748
749 %psearch -a _* list objects beginning with a single underscore"""
749 %psearch -a _* list objects beginning with a single underscore"""
750
750
751 # default namespaces to be searched
751 # default namespaces to be searched
752 def_search = ['user','builtin']
752 def_search = ['user','builtin']
753
753
754 # Process options/args
754 # Process options/args
755 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
755 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
756 opt = opts.get
756 opt = opts.get
757 shell = self.shell
757 shell = self.shell
758 psearch = shell.inspector.psearch
758 psearch = shell.inspector.psearch
759
759
760 # select case options
760 # select case options
761 if opts.has_key('i'):
761 if opts.has_key('i'):
762 ignore_case = True
762 ignore_case = True
763 elif opts.has_key('c'):
763 elif opts.has_key('c'):
764 ignore_case = False
764 ignore_case = False
765 else:
765 else:
766 ignore_case = not shell.rc.wildcards_case_sensitive
766 ignore_case = not shell.rc.wildcards_case_sensitive
767
767
768 # Build list of namespaces to search from user options
768 # Build list of namespaces to search from user options
769 def_search.extend(opt('s',[]))
769 def_search.extend(opt('s',[]))
770 ns_exclude = ns_exclude=opt('e',[])
770 ns_exclude = ns_exclude=opt('e',[])
771 ns_search = [nm for nm in def_search if nm not in ns_exclude]
771 ns_search = [nm for nm in def_search if nm not in ns_exclude]
772
772
773 # Call the actual search
773 # Call the actual search
774 try:
774 try:
775 psearch(args,shell.ns_table,ns_search,
775 psearch(args,shell.ns_table,ns_search,
776 show_all=opt('a'),ignore_case=ignore_case)
776 show_all=opt('a'),ignore_case=ignore_case)
777 except:
777 except:
778 shell.showtraceback()
778 shell.showtraceback()
779
779
780 def magic_who_ls(self, parameter_s=''):
780 def magic_who_ls(self, parameter_s=''):
781 """Return a sorted list of all interactive variables.
781 """Return a sorted list of all interactive variables.
782
782
783 If arguments are given, only variables of types matching these
783 If arguments are given, only variables of types matching these
784 arguments are returned."""
784 arguments are returned."""
785
785
786 user_ns = self.shell.user_ns
786 user_ns = self.shell.user_ns
787 out = []
787 out = []
788 typelist = parameter_s.split()
788 typelist = parameter_s.split()
789 for i in self.shell.user_ns.keys():
789 for i in self.shell.user_ns.keys():
790 if not (i.startswith('_') or i.startswith('_i')) \
790 if not (i.startswith('_') or i.startswith('_i')) \
791 and not (self.internal_ns.has_key(i) or
791 and not (self.internal_ns.has_key(i) or
792 self.user_config_ns.has_key(i)):
792 self.user_config_ns.has_key(i)):
793 if typelist:
793 if typelist:
794 if type(user_ns[i]).__name__ in typelist:
794 if type(user_ns[i]).__name__ in typelist:
795 out.append(i)
795 out.append(i)
796 else:
796 else:
797 out.append(i)
797 out.append(i)
798 out.sort()
798 out.sort()
799 return out
799 return out
800
800
801 def magic_who(self, parameter_s=''):
801 def magic_who(self, parameter_s=''):
802 """Print all interactive variables, with some minimal formatting.
802 """Print all interactive variables, with some minimal formatting.
803
803
804 If any arguments are given, only variables whose type matches one of
804 If any arguments are given, only variables whose type matches one of
805 these are printed. For example:
805 these are printed. For example:
806
806
807 %who function str
807 %who function str
808
808
809 will only list functions and strings, excluding all other types of
809 will only list functions and strings, excluding all other types of
810 variables. To find the proper type names, simply use type(var) at a
810 variables. To find the proper type names, simply use type(var) at a
811 command line to see how python prints type names. For example:
811 command line to see how python prints type names. For example:
812
812
813 In [1]: type('hello')\\
813 In [1]: type('hello')\\
814 Out[1]: <type 'str'>
814 Out[1]: <type 'str'>
815
815
816 indicates that the type name for strings is 'str'.
816 indicates that the type name for strings is 'str'.
817
817
818 %who always excludes executed names loaded through your configuration
818 %who always excludes executed names loaded through your configuration
819 file and things which are internal to IPython.
819 file and things which are internal to IPython.
820
820
821 This is deliberate, as typically you may load many modules and the
821 This is deliberate, as typically you may load many modules and the
822 purpose of %who is to show you only what you've manually defined."""
822 purpose of %who is to show you only what you've manually defined."""
823
823
824 varlist = self.magic_who_ls(parameter_s)
824 varlist = self.magic_who_ls(parameter_s)
825 if not varlist:
825 if not varlist:
826 print 'Interactive namespace is empty.'
826 print 'Interactive namespace is empty.'
827 return
827 return
828
828
829 # if we have variables, move on...
829 # if we have variables, move on...
830
830
831 # stupid flushing problem: when prompts have no separators, stdout is
831 # stupid flushing problem: when prompts have no separators, stdout is
832 # getting lost. I'm starting to think this is a python bug. I'm having
832 # getting lost. I'm starting to think this is a python bug. I'm having
833 # to force a flush with a print because even a sys.stdout.flush
833 # to force a flush with a print because even a sys.stdout.flush
834 # doesn't seem to do anything!
834 # doesn't seem to do anything!
835
835
836 count = 0
836 count = 0
837 for i in varlist:
837 for i in varlist:
838 print i+'\t',
838 print i+'\t',
839 count += 1
839 count += 1
840 if count > 8:
840 if count > 8:
841 count = 0
841 count = 0
842 print
842 print
843 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
843 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
844
844
845 print # well, this does force a flush at the expense of an extra \n
845 print # well, this does force a flush at the expense of an extra \n
846
846
847 def magic_whos(self, parameter_s=''):
847 def magic_whos(self, parameter_s=''):
848 """Like %who, but gives some extra information about each variable.
848 """Like %who, but gives some extra information about each variable.
849
849
850 The same type filtering of %who can be applied here.
850 The same type filtering of %who can be applied here.
851
851
852 For all variables, the type is printed. Additionally it prints:
852 For all variables, the type is printed. Additionally it prints:
853
853
854 - For {},[],(): their length.
854 - For {},[],(): their length.
855
855
856 - For Numeric arrays, a summary with shape, number of elements,
856 - For Numeric arrays, a summary with shape, number of elements,
857 typecode and size in memory.
857 typecode and size in memory.
858
858
859 - Everything else: a string representation, snipping their middle if
859 - Everything else: a string representation, snipping their middle if
860 too long."""
860 too long."""
861
861
862 varnames = self.magic_who_ls(parameter_s)
862 varnames = self.magic_who_ls(parameter_s)
863 if not varnames:
863 if not varnames:
864 print 'Interactive namespace is empty.'
864 print 'Interactive namespace is empty.'
865 return
865 return
866
866
867 # if we have variables, move on...
867 # if we have variables, move on...
868
868
869 # for these types, show len() instead of data:
869 # for these types, show len() instead of data:
870 seq_types = [types.DictType,types.ListType,types.TupleType]
870 seq_types = [types.DictType,types.ListType,types.TupleType]
871
871
872 # for Numeric arrays, display summary info
872 # for Numeric arrays, display summary info
873 try:
873 try:
874 import Numeric
874 import Numeric
875 except ImportError:
875 except ImportError:
876 array_type = None
876 array_type = None
877 else:
877 else:
878 array_type = Numeric.ArrayType.__name__
878 array_type = Numeric.ArrayType.__name__
879
879
880 # Find all variable names and types so we can figure out column sizes
880 # Find all variable names and types so we can figure out column sizes
881 get_vars = lambda i: self.shell.user_ns[i]
881 get_vars = lambda i: self.shell.user_ns[i]
882 type_name = lambda v: type(v).__name__
882 type_name = lambda v: type(v).__name__
883 varlist = map(get_vars,varnames)
883 varlist = map(get_vars,varnames)
884 typelist = map(type_name,varlist)
884 typelist = map(type_name,varlist)
885 # column labels and # of spaces as separator
885 # column labels and # of spaces as separator
886 varlabel = 'Variable'
886 varlabel = 'Variable'
887 typelabel = 'Type'
887 typelabel = 'Type'
888 datalabel = 'Data/Info'
888 datalabel = 'Data/Info'
889 colsep = 3
889 colsep = 3
890 # variable format strings
890 # variable format strings
891 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
891 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
892 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
892 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
893 aformat = "%s: %s elems, type `%s`, %s bytes"
893 aformat = "%s: %s elems, type `%s`, %s bytes"
894 # find the size of the columns to format the output nicely
894 # find the size of the columns to format the output nicely
895 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
895 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
896 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
896 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
897 # table header
897 # table header
898 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
898 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
899 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
899 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
900 # and the table itself
900 # and the table itself
901 kb = 1024
901 kb = 1024
902 Mb = 1048576 # kb**2
902 Mb = 1048576 # kb**2
903 for vname,var,vtype in zip(varnames,varlist,typelist):
903 for vname,var,vtype in zip(varnames,varlist,typelist):
904 print itpl(vformat),
904 print itpl(vformat),
905 if vtype in seq_types:
905 if vtype in seq_types:
906 print len(var)
906 print len(var)
907 elif vtype==array_type:
907 elif vtype==array_type:
908 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
908 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
909 vsize = Numeric.size(var)
909 vsize = Numeric.size(var)
910 vbytes = vsize*var.itemsize()
910 vbytes = vsize*var.itemsize()
911 if vbytes < 100000:
911 if vbytes < 100000:
912 print aformat % (vshape,vsize,var.typecode(),vbytes)
912 print aformat % (vshape,vsize,var.typecode(),vbytes)
913 else:
913 else:
914 print aformat % (vshape,vsize,var.typecode(),vbytes),
914 print aformat % (vshape,vsize,var.typecode(),vbytes),
915 if vbytes < Mb:
915 if vbytes < Mb:
916 print '(%s kb)' % (vbytes/kb,)
916 print '(%s kb)' % (vbytes/kb,)
917 else:
917 else:
918 print '(%s Mb)' % (vbytes/Mb,)
918 print '(%s Mb)' % (vbytes/Mb,)
919 else:
919 else:
920 vstr = str(var)
920 vstr = str(var)
921 if len(vstr) < 50:
921 if len(vstr) < 50:
922 print vstr
922 print vstr
923 else:
923 else:
924 printpl(vfmt_short)
924 printpl(vfmt_short)
925
925
926 def magic_reset(self, parameter_s=''):
926 def magic_reset(self, parameter_s=''):
927 """Resets the namespace by removing all names defined by the user.
927 """Resets the namespace by removing all names defined by the user.
928
928
929 Input/Output history are left around in case you need them."""
929 Input/Output history are left around in case you need them."""
930
930
931 ans = raw_input(
931 ans = raw_input(
932 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
932 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
933 if not ans.lower() == 'y':
933 if not ans.lower() == 'y':
934 print 'Nothing done.'
934 print 'Nothing done.'
935 return
935 return
936 user_ns = self.shell.user_ns
936 user_ns = self.shell.user_ns
937 for i in self.magic_who_ls():
937 for i in self.magic_who_ls():
938 del(user_ns[i])
938 del(user_ns[i])
939
939
940 def magic_config(self,parameter_s=''):
940 def magic_config(self,parameter_s=''):
941 """Show IPython's internal configuration."""
941 """Show IPython's internal configuration."""
942
942
943 page('Current configuration structure:\n'+
943 page('Current configuration structure:\n'+
944 pformat(self.shell.rc.dict()))
944 pformat(self.shell.rc.dict()))
945
945
946 def magic_logstart(self,parameter_s=''):
946 def magic_logstart(self,parameter_s=''):
947 """Start logging anywhere in a session.
947 """Start logging anywhere in a session.
948
948
949 %logstart [log_name [log_mode]]
949 %logstart [log_name [log_mode]]
950
950
951 If no name is given, it defaults to a file named 'ipython.log' in your
951 If no name is given, it defaults to a file named 'ipython.log' in your
952 current directory, in 'rotate' mode (see below).
952 current directory, in 'rotate' mode (see below).
953
953
954 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
954 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
955 history up to that point and then continues logging.
955 history up to that point and then continues logging.
956
956
957 %logstart takes a second optional parameter: logging mode. This can be one
957 %logstart takes a second optional parameter: logging mode. This can be one
958 of (note that the modes are given unquoted):\\
958 of (note that the modes are given unquoted):\\
959 over: overwrite existing log.\\
959 over: overwrite existing log.\\
960 backup: rename (if exists) to name~ and start name.\\
960 backup: rename (if exists) to name~ and start name.\\
961 append: well, that says it.\\
961 append: well, that says it.\\
962 rotate: create rotating logs name.1~, name.2~, etc.
962 rotate: create rotating logs name.1~, name.2~, etc.
963 """
963 """
964
964
965 #FIXME. This function should all be moved to the Logger class.
965 #FIXME. This function should all be moved to the Logger class.
966
966
967 valid_modes = qw('over backup append rotate')
967 valid_modes = qw('over backup append rotate')
968 if self.LOG:
968 if self.LOG:
969 print 'Logging is already in place. Logfile:',self.LOG
969 print 'Logging is already in place. Logfile:',self.LOG
970 return
970 return
971
971
972 par = parameter_s.strip()
972 par = parameter_s.strip()
973 if not par:
973 if not par:
974 logname = self.LOGDEF
974 logname = self.LOGDEF
975 logmode = 'rotate' # use rotate for the auto-generated logs
975 logmode = 'rotate' # use rotate for the auto-generated logs
976 else:
976 else:
977 try:
977 try:
978 logname,logmode = par.split()
978 logname,logmode = par.split()
979 except:
979 except:
980 try:
980 try:
981 logname = par
981 logname = par
982 logmode = 'backup'
982 logmode = 'backup'
983 except:
983 except:
984 warn('Usage: %log [log_name [log_mode]]')
984 warn('Usage: %log [log_name [log_mode]]')
985 return
985 return
986 if not logmode in valid_modes:
986 if not logmode in valid_modes:
987 warn('Logging NOT activated.\n'
987 warn('Logging NOT activated.\n'
988 'Usage: %log [log_name [log_mode]]\n'
988 'Usage: %log [log_name [log_mode]]\n'
989 'Valid modes: '+str(valid_modes))
989 'Valid modes: '+str(valid_modes))
990 return
990 return
991
991
992 # If we made it this far, I think we're ok:
992 # If we made it this far, I think we're ok:
993 print 'Activating auto-logging.'
993 print 'Activating auto-logging.'
994 print 'Current session state plus future input saved to:',logname
994 print 'Current session state plus future input saved to:',logname
995 print 'Logging mode: ',logmode
995 print 'Logging mode: ',logmode
996 # put logname into rc struct as if it had been called on the command line,
996 # put logname into rc struct as if it had been called on the command line,
997 # so it ends up saved in the log header
997 # so it ends up saved in the log header
998 # Save it in case we need to restore it...
998 # Save it in case we need to restore it...
999 old_logfile = self.shell.rc.opts.get('logfile','')
999 old_logfile = self.shell.rc.opts.get('logfile','')
1000 logname = os.path.expanduser(logname)
1000 logname = os.path.expanduser(logname)
1001 self.shell.rc.opts.logfile = logname
1001 self.shell.rc.opts.logfile = logname
1002 self.LOGMODE = logmode # FIXME: this should be set through a function.
1002 self.LOGMODE = logmode # FIXME: this should be set through a function.
1003 try:
1003 try:
1004 header = str(self.LOGHEAD)
1004 header = str(self.LOGHEAD)
1005 self.create_log(header,logname)
1005 self.create_log(header,logname)
1006 self.logstart(header,logname)
1006 self.logstart(header,logname)
1007 except:
1007 except:
1008 self.LOG = '' # we are NOT logging, something went wrong
1008 self.LOG = '' # we are NOT logging, something went wrong
1009 self.shell.rc.opts.logfile = old_logfile
1009 self.shell.rc.opts.logfile = old_logfile
1010 warn("Couldn't start log: "+str(sys.exc_info()[1]))
1010 warn("Couldn't start log: "+str(sys.exc_info()[1]))
1011 else: # log input history up to this point
1011 else: # log input history up to this point
1012 self.logfile.write(self.shell.user_ns['_ih'][1:])
1012 self.logfile.write(self.shell.user_ns['_ih'][1:])
1013 self.logfile.flush()
1013 self.logfile.flush()
1014
1014
1015 def magic_logoff(self,parameter_s=''):
1015 def magic_logoff(self,parameter_s=''):
1016 """Temporarily stop logging.
1016 """Temporarily stop logging.
1017
1017
1018 You must have previously started logging."""
1018 You must have previously started logging."""
1019 self.switch_log(0)
1019 self.switch_log(0)
1020
1020
1021 def magic_logon(self,parameter_s=''):
1021 def magic_logon(self,parameter_s=''):
1022 """Restart logging.
1022 """Restart logging.
1023
1023
1024 This function is for restarting logging which you've temporarily
1024 This function is for restarting logging which you've temporarily
1025 stopped with %logoff. For starting logging for the first time, you
1025 stopped with %logoff. For starting logging for the first time, you
1026 must use the %logstart function, which allows you to specify an
1026 must use the %logstart function, which allows you to specify an
1027 optional log filename."""
1027 optional log filename."""
1028
1028
1029 self.switch_log(1)
1029 self.switch_log(1)
1030
1030
1031 def magic_logstate(self,parameter_s=''):
1031 def magic_logstate(self,parameter_s=''):
1032 """Print the status of the logging system."""
1032 """Print the status of the logging system."""
1033
1033
1034 self.logstate()
1034 self.logstate()
1035
1035
1036 def magic_pdb(self, parameter_s=''):
1036 def magic_pdb(self, parameter_s=''):
1037 """Control the calling of the pdb interactive debugger.
1037 """Control the calling of the pdb interactive debugger.
1038
1038
1039 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1039 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1040 argument it works as a toggle.
1040 argument it works as a toggle.
1041
1041
1042 When an exception is triggered, IPython can optionally call the
1042 When an exception is triggered, IPython can optionally call the
1043 interactive pdb debugger after the traceback printout. %pdb toggles
1043 interactive pdb debugger after the traceback printout. %pdb toggles
1044 this feature on and off."""
1044 this feature on and off."""
1045
1045
1046 par = parameter_s.strip().lower()
1046 par = parameter_s.strip().lower()
1047
1047
1048 if par:
1048 if par:
1049 try:
1049 try:
1050 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1050 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1051 except KeyError:
1051 except KeyError:
1052 print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.'
1052 print 'Incorrect argument. Use on/1, off/0, or nothing for a toggle.'
1053 return
1053 return
1054 else:
1054 else:
1055 self.shell.InteractiveTB.call_pdb = pdb
1055 self.shell.InteractiveTB.call_pdb = pdb
1056 else:
1056 else:
1057 self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb
1057 new_pdb = not self.shell.InteractiveTB.call_pdb
1058 print 'Automatic pdb calling has been turned',\
1058 self.shell.InteractiveTB.call_pdb = new_pdb
1059 on_off(self.shell.InteractiveTB.call_pdb)
1059 if self.shell.isthreaded:
1060 try:
1061 self.sys_excepthook.call_pdb = new_pdb
1062 except:
1063 warn('Failed to activate pdb for threaded exception handler')
1064
1065 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1066
1060
1067
1061
1068
1062 def magic_prun(self, parameter_s ='',user_mode=1,
1069 def magic_prun(self, parameter_s ='',user_mode=1,
1063 opts=None,arg_lst=None,prog_ns=None):
1070 opts=None,arg_lst=None,prog_ns=None):
1064
1071
1065 """Run a statement through the python code profiler.
1072 """Run a statement through the python code profiler.
1066
1073
1067 Usage:\\
1074 Usage:\\
1068 %prun [options] statement
1075 %prun [options] statement
1069
1076
1070 The given statement (which doesn't require quote marks) is run via the
1077 The given statement (which doesn't require quote marks) is run via the
1071 python profiler in a manner similar to the profile.run() function.
1078 python profiler in a manner similar to the profile.run() function.
1072 Namespaces are internally managed to work correctly; profile.run
1079 Namespaces are internally managed to work correctly; profile.run
1073 cannot be used in IPython because it makes certain assumptions about
1080 cannot be used in IPython because it makes certain assumptions about
1074 namespaces which do not hold under IPython.
1081 namespaces which do not hold under IPython.
1075
1082
1076 Options:
1083 Options:
1077
1084
1078 -l <limit>: you can place restrictions on what or how much of the
1085 -l <limit>: you can place restrictions on what or how much of the
1079 profile gets printed. The limit value can be:
1086 profile gets printed. The limit value can be:
1080
1087
1081 * A string: only information for function names containing this string
1088 * A string: only information for function names containing this string
1082 is printed.
1089 is printed.
1083
1090
1084 * An integer: only these many lines are printed.
1091 * An integer: only these many lines are printed.
1085
1092
1086 * A float (between 0 and 1): this fraction of the report is printed
1093 * A float (between 0 and 1): this fraction of the report is printed
1087 (for example, use a limit of 0.4 to see the topmost 40% only).
1094 (for example, use a limit of 0.4 to see the topmost 40% only).
1088
1095
1089 You can combine several limits with repeated use of the option. For
1096 You can combine several limits with repeated use of the option. For
1090 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1097 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1091 information about class constructors.
1098 information about class constructors.
1092
1099
1093 -r: return the pstats.Stats object generated by the profiling. This
1100 -r: return the pstats.Stats object generated by the profiling. This
1094 object has all the information about the profile in it, and you can
1101 object has all the information about the profile in it, and you can
1095 later use it for further analysis or in other functions.
1102 later use it for further analysis or in other functions.
1096
1103
1097 Since magic functions have a particular form of calling which prevents
1104 Since magic functions have a particular form of calling which prevents
1098 you from writing something like:\\
1105 you from writing something like:\\
1099 In [1]: p = %prun -r print 4 # invalid!\\
1106 In [1]: p = %prun -r print 4 # invalid!\\
1100 you must instead use IPython's automatic variables to assign this:\\
1107 you must instead use IPython's automatic variables to assign this:\\
1101 In [1]: %prun -r print 4 \\
1108 In [1]: %prun -r print 4 \\
1102 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1109 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1103 In [2]: stats = _
1110 In [2]: stats = _
1104
1111
1105 If you really need to assign this value via an explicit function call,
1112 If you really need to assign this value via an explicit function call,
1106 you can always tap directly into the true name of the magic function
1113 you can always tap directly into the true name of the magic function
1107 by using the ipmagic function (which IPython automatically adds to the
1114 by using the ipmagic function (which IPython automatically adds to the
1108 builtins):\\
1115 builtins):\\
1109 In [3]: stats = ipmagic('prun','-r print 4')
1116 In [3]: stats = ipmagic('prun','-r print 4')
1110
1117
1111 You can type ipmagic? for more details on ipmagic.
1118 You can type ipmagic? for more details on ipmagic.
1112
1119
1113 -s <key>: sort profile by given key. You can provide more than one key
1120 -s <key>: sort profile by given key. You can provide more than one key
1114 by using the option several times: '-s key1 -s key2 -s key3...'. The
1121 by using the option several times: '-s key1 -s key2 -s key3...'. The
1115 default sorting key is 'time'.
1122 default sorting key is 'time'.
1116
1123
1117 The following is copied verbatim from the profile documentation
1124 The following is copied verbatim from the profile documentation
1118 referenced below:
1125 referenced below:
1119
1126
1120 When more than one key is provided, additional keys are used as
1127 When more than one key is provided, additional keys are used as
1121 secondary criteria when the there is equality in all keys selected
1128 secondary criteria when the there is equality in all keys selected
1122 before them.
1129 before them.
1123
1130
1124 Abbreviations can be used for any key names, as long as the
1131 Abbreviations can be used for any key names, as long as the
1125 abbreviation is unambiguous. The following are the keys currently
1132 abbreviation is unambiguous. The following are the keys currently
1126 defined:
1133 defined:
1127
1134
1128 Valid Arg Meaning\\
1135 Valid Arg Meaning\\
1129 "calls" call count\\
1136 "calls" call count\\
1130 "cumulative" cumulative time\\
1137 "cumulative" cumulative time\\
1131 "file" file name\\
1138 "file" file name\\
1132 "module" file name\\
1139 "module" file name\\
1133 "pcalls" primitive call count\\
1140 "pcalls" primitive call count\\
1134 "line" line number\\
1141 "line" line number\\
1135 "name" function name\\
1142 "name" function name\\
1136 "nfl" name/file/line\\
1143 "nfl" name/file/line\\
1137 "stdname" standard name\\
1144 "stdname" standard name\\
1138 "time" internal time
1145 "time" internal time
1139
1146
1140 Note that all sorts on statistics are in descending order (placing
1147 Note that all sorts on statistics are in descending order (placing
1141 most time consuming items first), where as name, file, and line number
1148 most time consuming items first), where as name, file, and line number
1142 searches are in ascending order (i.e., alphabetical). The subtle
1149 searches are in ascending order (i.e., alphabetical). The subtle
1143 distinction between "nfl" and "stdname" is that the standard name is a
1150 distinction between "nfl" and "stdname" is that the standard name is a
1144 sort of the name as printed, which means that the embedded line
1151 sort of the name as printed, which means that the embedded line
1145 numbers get compared in an odd way. For example, lines 3, 20, and 40
1152 numbers get compared in an odd way. For example, lines 3, 20, and 40
1146 would (if the file names were the same) appear in the string order
1153 would (if the file names were the same) appear in the string order
1147 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1154 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1148 line numbers. In fact, sort_stats("nfl") is the same as
1155 line numbers. In fact, sort_stats("nfl") is the same as
1149 sort_stats("name", "file", "line").
1156 sort_stats("name", "file", "line").
1150
1157
1151 -T <filename>: save profile results as shown on screen to a text
1158 -T <filename>: save profile results as shown on screen to a text
1152 file. The profile is still shown on screen.
1159 file. The profile is still shown on screen.
1153
1160
1154 -D <filename>: save (via dump_stats) profile statistics to given
1161 -D <filename>: save (via dump_stats) profile statistics to given
1155 filename. This data is in a format understod by the pstats module, and
1162 filename. This data is in a format understod by the pstats module, and
1156 is generated by a call to the dump_stats() method of profile
1163 is generated by a call to the dump_stats() method of profile
1157 objects. The profile is still shown on screen.
1164 objects. The profile is still shown on screen.
1158
1165
1159 If you want to run complete programs under the profiler's control, use
1166 If you want to run complete programs under the profiler's control, use
1160 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1167 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1161 contains profiler specific options as described here.
1168 contains profiler specific options as described here.
1162
1169
1163 You can read the complete documentation for the profile module with:\\
1170 You can read the complete documentation for the profile module with:\\
1164 In [1]: import profile; profile.help() """
1171 In [1]: import profile; profile.help() """
1165
1172
1166 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1173 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1167 # protect user quote marks
1174 # protect user quote marks
1168 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1175 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1169
1176
1170 if user_mode: # regular user call
1177 if user_mode: # regular user call
1171 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1178 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1172 list_all=1)
1179 list_all=1)
1173 namespace = self.shell.user_ns
1180 namespace = self.shell.user_ns
1174 else: # called to run a program by %run -p
1181 else: # called to run a program by %run -p
1175 try:
1182 try:
1176 filename = get_py_filename(arg_lst[0])
1183 filename = get_py_filename(arg_lst[0])
1177 except IOError,msg:
1184 except IOError,msg:
1178 error(msg)
1185 error(msg)
1179 return
1186 return
1180
1187
1181 arg_str = 'execfile(filename,prog_ns)'
1188 arg_str = 'execfile(filename,prog_ns)'
1182 namespace = locals()
1189 namespace = locals()
1183
1190
1184 opts.merge(opts_def)
1191 opts.merge(opts_def)
1185
1192
1186 prof = profile.Profile()
1193 prof = profile.Profile()
1187 try:
1194 try:
1188 prof = prof.runctx(arg_str,namespace,namespace)
1195 prof = prof.runctx(arg_str,namespace,namespace)
1189 sys_exit = ''
1196 sys_exit = ''
1190 except SystemExit:
1197 except SystemExit:
1191 sys_exit = """*** SystemExit exception caught in code being profiled."""
1198 sys_exit = """*** SystemExit exception caught in code being profiled."""
1192
1199
1193 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1200 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1194
1201
1195 lims = opts.l
1202 lims = opts.l
1196 if lims:
1203 if lims:
1197 lims = [] # rebuild lims with ints/floats/strings
1204 lims = [] # rebuild lims with ints/floats/strings
1198 for lim in opts.l:
1205 for lim in opts.l:
1199 try:
1206 try:
1200 lims.append(int(lim))
1207 lims.append(int(lim))
1201 except ValueError:
1208 except ValueError:
1202 try:
1209 try:
1203 lims.append(float(lim))
1210 lims.append(float(lim))
1204 except ValueError:
1211 except ValueError:
1205 lims.append(lim)
1212 lims.append(lim)
1206
1213
1207 # trap output
1214 # trap output
1208 sys_stdout = sys.stdout
1215 sys_stdout = sys.stdout
1209 stdout_trap = StringIO()
1216 stdout_trap = StringIO()
1210 try:
1217 try:
1211 sys.stdout = stdout_trap
1218 sys.stdout = stdout_trap
1212 stats.print_stats(*lims)
1219 stats.print_stats(*lims)
1213 finally:
1220 finally:
1214 sys.stdout = sys_stdout
1221 sys.stdout = sys_stdout
1215 output = stdout_trap.getvalue()
1222 output = stdout_trap.getvalue()
1216 output = output.rstrip()
1223 output = output.rstrip()
1217
1224
1218 page(output,screen_lines=self.shell.rc.screen_length)
1225 page(output,screen_lines=self.shell.rc.screen_length)
1219 print sys_exit,
1226 print sys_exit,
1220
1227
1221 dump_file = opts.D[0]
1228 dump_file = opts.D[0]
1222 text_file = opts.T[0]
1229 text_file = opts.T[0]
1223 if dump_file:
1230 if dump_file:
1224 prof.dump_stats(dump_file)
1231 prof.dump_stats(dump_file)
1225 print '\n*** Profile stats marshalled to file',\
1232 print '\n*** Profile stats marshalled to file',\
1226 `dump_file`+'.',sys_exit
1233 `dump_file`+'.',sys_exit
1227 if text_file:
1234 if text_file:
1228 file(text_file,'w').write(output)
1235 file(text_file,'w').write(output)
1229 print '\n*** Profile printout saved to text file',\
1236 print '\n*** Profile printout saved to text file',\
1230 `text_file`+'.',sys_exit
1237 `text_file`+'.',sys_exit
1231
1238
1232 if opts.has_key('r'):
1239 if opts.has_key('r'):
1233 return stats
1240 return stats
1234 else:
1241 else:
1235 return None
1242 return None
1236
1243
1237 def magic_run(self, parameter_s ='',runner=None):
1244 def magic_run(self, parameter_s ='',runner=None):
1238 """Run the named file inside IPython as a program.
1245 """Run the named file inside IPython as a program.
1239
1246
1240 Usage:\\
1247 Usage:\\
1241 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1248 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1242
1249
1243 Parameters after the filename are passed as command-line arguments to
1250 Parameters after the filename are passed as command-line arguments to
1244 the program (put in sys.argv). Then, control returns to IPython's
1251 the program (put in sys.argv). Then, control returns to IPython's
1245 prompt.
1252 prompt.
1246
1253
1247 This is similar to running at a system prompt:\\
1254 This is similar to running at a system prompt:\\
1248 $ python file args\\
1255 $ python file args\\
1249 but with the advantage of giving you IPython's tracebacks, and of
1256 but with the advantage of giving you IPython's tracebacks, and of
1250 loading all variables into your interactive namespace for further use
1257 loading all variables into your interactive namespace for further use
1251 (unless -p is used, see below).
1258 (unless -p is used, see below).
1252
1259
1253 The file is executed in a namespace initially consisting only of
1260 The file is executed in a namespace initially consisting only of
1254 __name__=='__main__' and sys.argv constructed as indicated. It thus
1261 __name__=='__main__' and sys.argv constructed as indicated. It thus
1255 sees its environment as if it were being run as a stand-alone
1262 sees its environment as if it were being run as a stand-alone
1256 program. But after execution, the IPython interactive namespace gets
1263 program. But after execution, the IPython interactive namespace gets
1257 updated with all variables defined in the program (except for __name__
1264 updated with all variables defined in the program (except for __name__
1258 and sys.argv). This allows for very convenient loading of code for
1265 and sys.argv). This allows for very convenient loading of code for
1259 interactive work, while giving each program a 'clean sheet' to run in.
1266 interactive work, while giving each program a 'clean sheet' to run in.
1260
1267
1261 Options:
1268 Options:
1262
1269
1263 -n: __name__ is NOT set to '__main__', but to the running file's name
1270 -n: __name__ is NOT set to '__main__', but to the running file's name
1264 without extension (as python does under import). This allows running
1271 without extension (as python does under import). This allows running
1265 scripts and reloading the definitions in them without calling code
1272 scripts and reloading the definitions in them without calling code
1266 protected by an ' if __name__ == "__main__" ' clause.
1273 protected by an ' if __name__ == "__main__" ' clause.
1267
1274
1268 -i: run the file in IPython's namespace instead of an empty one. This
1275 -i: run the file in IPython's namespace instead of an empty one. This
1269 is useful if you are experimenting with code written in a text editor
1276 is useful if you are experimenting with code written in a text editor
1270 which depends on variables defined interactively.
1277 which depends on variables defined interactively.
1271
1278
1272 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1279 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1273 being run. This is particularly useful if IPython is being used to
1280 being run. This is particularly useful if IPython is being used to
1274 run unittests, which always exit with a sys.exit() call. In such
1281 run unittests, which always exit with a sys.exit() call. In such
1275 cases you are interested in the output of the test results, not in
1282 cases you are interested in the output of the test results, not in
1276 seeing a traceback of the unittest module.
1283 seeing a traceback of the unittest module.
1277
1284
1278 -t: print timing information at the end of the run. IPython will give
1285 -t: print timing information at the end of the run. IPython will give
1279 you an estimated CPU time consumption for your script, which under
1286 you an estimated CPU time consumption for your script, which under
1280 Unix uses the resource module to avoid the wraparound problems of
1287 Unix uses the resource module to avoid the wraparound problems of
1281 time.clock(). Under Unix, an estimate of time spent on system tasks
1288 time.clock(). Under Unix, an estimate of time spent on system tasks
1282 is also given (for Windows platforms this is reported as 0.0).
1289 is also given (for Windows platforms this is reported as 0.0).
1283
1290
1284 If -t is given, an additional -N<N> option can be given, where <N>
1291 If -t is given, an additional -N<N> option can be given, where <N>
1285 must be an integer indicating how many times you want the script to
1292 must be an integer indicating how many times you want the script to
1286 run. The final timing report will include total and per run results.
1293 run. The final timing report will include total and per run results.
1287
1294
1288 For example (testing the script uniq_stable.py):
1295 For example (testing the script uniq_stable.py):
1289
1296
1290 In [1]: run -t uniq_stable
1297 In [1]: run -t uniq_stable
1291
1298
1292 IPython CPU timings (estimated):\\
1299 IPython CPU timings (estimated):\\
1293 User : 0.19597 s.\\
1300 User : 0.19597 s.\\
1294 System: 0.0 s.\\
1301 System: 0.0 s.\\
1295
1302
1296 In [2]: run -t -N5 uniq_stable
1303 In [2]: run -t -N5 uniq_stable
1297
1304
1298 IPython CPU timings (estimated):\\
1305 IPython CPU timings (estimated):\\
1299 Total runs performed: 5\\
1306 Total runs performed: 5\\
1300 Times : Total Per run\\
1307 Times : Total Per run\\
1301 User : 0.910862 s, 0.1821724 s.\\
1308 User : 0.910862 s, 0.1821724 s.\\
1302 System: 0.0 s, 0.0 s.
1309 System: 0.0 s, 0.0 s.
1303
1310
1304 -d: run your program under the control of pdb, the Python debugger.
1311 -d: run your program under the control of pdb, the Python debugger.
1305 This allows you to execute your program step by step, watch variables,
1312 This allows you to execute your program step by step, watch variables,
1306 etc. Internally, what IPython does is similar to calling:
1313 etc. Internally, what IPython does is similar to calling:
1307
1314
1308 pdb.run('execfile("YOURFILENAME")')
1315 pdb.run('execfile("YOURFILENAME")')
1309
1316
1310 with a breakpoint set on line 1 of your file. You can change the line
1317 with a breakpoint set on line 1 of your file. You can change the line
1311 number for this automatic breakpoint to be <N> by using the -bN option
1318 number for this automatic breakpoint to be <N> by using the -bN option
1312 (where N must be an integer). For example:
1319 (where N must be an integer). For example:
1313
1320
1314 %run -d -b40 myscript
1321 %run -d -b40 myscript
1315
1322
1316 will set the first breakpoint at line 40 in myscript.py. Note that
1323 will set the first breakpoint at line 40 in myscript.py. Note that
1317 the first breakpoint must be set on a line which actually does
1324 the first breakpoint must be set on a line which actually does
1318 something (not a comment or docstring) for it to stop execution.
1325 something (not a comment or docstring) for it to stop execution.
1319
1326
1320 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1327 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1321 first enter 'c' (without qoutes) to start execution up to the first
1328 first enter 'c' (without qoutes) to start execution up to the first
1322 breakpoint.
1329 breakpoint.
1323
1330
1324 Entering 'help' gives information about the use of the debugger. You
1331 Entering 'help' gives information about the use of the debugger. You
1325 can easily see pdb's full documentation with "import pdb;pdb.help()"
1332 can easily see pdb's full documentation with "import pdb;pdb.help()"
1326 at a prompt.
1333 at a prompt.
1327
1334
1328 -p: run program under the control of the Python profiler module (which
1335 -p: run program under the control of the Python profiler module (which
1329 prints a detailed report of execution times, function calls, etc).
1336 prints a detailed report of execution times, function calls, etc).
1330
1337
1331 You can pass other options after -p which affect the behavior of the
1338 You can pass other options after -p which affect the behavior of the
1332 profiler itself. See the docs for %prun for details.
1339 profiler itself. See the docs for %prun for details.
1333
1340
1334 In this mode, the program's variables do NOT propagate back to the
1341 In this mode, the program's variables do NOT propagate back to the
1335 IPython interactive namespace (because they remain in the namespace
1342 IPython interactive namespace (because they remain in the namespace
1336 where the profiler executes them).
1343 where the profiler executes them).
1337
1344
1338 Internally this triggers a call to %prun, see its documentation for
1345 Internally this triggers a call to %prun, see its documentation for
1339 details on the options available specifically for profiling."""
1346 details on the options available specifically for profiling."""
1340
1347
1341 # get arguments and set sys.argv for program to be run.
1348 # get arguments and set sys.argv for program to be run.
1342 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1349 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1343 mode='list',list_all=1)
1350 mode='list',list_all=1)
1344
1351
1345 try:
1352 try:
1346 filename = get_py_filename(arg_lst[0])
1353 filename = get_py_filename(arg_lst[0])
1347 except IndexError:
1354 except IndexError:
1348 warn('you must provide at least a filename.')
1355 warn('you must provide at least a filename.')
1349 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1356 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1350 return
1357 return
1351 except IOError,msg:
1358 except IOError,msg:
1352 error(msg)
1359 error(msg)
1353 return
1360 return
1354
1361
1355 # Control the response to exit() calls made by the script being run
1362 # Control the response to exit() calls made by the script being run
1356 exit_ignore = opts.has_key('e')
1363 exit_ignore = opts.has_key('e')
1357
1364
1358 # Make sure that the running script gets a proper sys.argv as if it
1365 # Make sure that the running script gets a proper sys.argv as if it
1359 # were run from a system shell.
1366 # were run from a system shell.
1360 save_argv = sys.argv # save it for later restoring
1367 save_argv = sys.argv # save it for later restoring
1361 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1368 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1362
1369
1363 if opts.has_key('i'):
1370 if opts.has_key('i'):
1364 prog_ns = self.shell.user_ns
1371 prog_ns = self.shell.user_ns
1365 __name__save = self.shell.user_ns['__name__']
1372 __name__save = self.shell.user_ns['__name__']
1366 prog_ns['__name__'] = '__main__'
1373 prog_ns['__name__'] = '__main__'
1367 else:
1374 else:
1368 if opts.has_key('n'):
1375 if opts.has_key('n'):
1369 name = os.path.splitext(os.path.basename(filename))[0]
1376 name = os.path.splitext(os.path.basename(filename))[0]
1370 else:
1377 else:
1371 name = '__main__'
1378 name = '__main__'
1372 prog_ns = {'__name__':name}
1379 prog_ns = {'__name__':name}
1373
1380
1374 # pickle fix. See iplib for an explanation
1381 # pickle fix. See iplib for an explanation
1375 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1382 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1376
1383
1377 stats = None
1384 stats = None
1378 try:
1385 try:
1379 if opts.has_key('p'):
1386 if opts.has_key('p'):
1380 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1387 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1381 else:
1388 else:
1382 if opts.has_key('d'):
1389 if opts.has_key('d'):
1383 deb = Debugger.Pdb(self.shell.rc.colors)
1390 deb = Debugger.Pdb(self.shell.rc.colors)
1384 # reset Breakpoint state, which is moronically kept
1391 # reset Breakpoint state, which is moronically kept
1385 # in a class
1392 # in a class
1386 bdb.Breakpoint.next = 1
1393 bdb.Breakpoint.next = 1
1387 bdb.Breakpoint.bplist = {}
1394 bdb.Breakpoint.bplist = {}
1388 bdb.Breakpoint.bpbynumber = [None]
1395 bdb.Breakpoint.bpbynumber = [None]
1389 # Set an initial breakpoint to stop execution
1396 # Set an initial breakpoint to stop execution
1390 maxtries = 10
1397 maxtries = 10
1391 bp = int(opts.get('b',[1])[0])
1398 bp = int(opts.get('b',[1])[0])
1392 checkline = deb.checkline(filename,bp)
1399 checkline = deb.checkline(filename,bp)
1393 if not checkline:
1400 if not checkline:
1394 for bp in range(bp+1,bp+maxtries+1):
1401 for bp in range(bp+1,bp+maxtries+1):
1395 if deb.checkline(filename,bp):
1402 if deb.checkline(filename,bp):
1396 break
1403 break
1397 else:
1404 else:
1398 msg = ("\nI failed to find a valid line to set "
1405 msg = ("\nI failed to find a valid line to set "
1399 "a breakpoint\n"
1406 "a breakpoint\n"
1400 "after trying up to line: %s.\n"
1407 "after trying up to line: %s.\n"
1401 "Please set a valid breakpoint manually "
1408 "Please set a valid breakpoint manually "
1402 "with the -b option." % bp)
1409 "with the -b option." % bp)
1403 error(msg)
1410 error(msg)
1404 return
1411 return
1405 # if we find a good linenumber, set the breakpoint
1412 # if we find a good linenumber, set the breakpoint
1406 deb.do_break('%s:%s' % (filename,bp))
1413 deb.do_break('%s:%s' % (filename,bp))
1407 # Start file run
1414 # Start file run
1408 print "NOTE: Enter 'c' at the",
1415 print "NOTE: Enter 'c' at the",
1409 print "ipdb> prompt to start your script."
1416 print "ipdb> prompt to start your script."
1410 try:
1417 try:
1411 deb.run('execfile("%s")' % filename,prog_ns)
1418 deb.run('execfile("%s")' % filename,prog_ns)
1412 except:
1419 except:
1413 etype, value, tb = sys.exc_info()
1420 etype, value, tb = sys.exc_info()
1414 # Skip three frames in the traceback: the %run one,
1421 # Skip three frames in the traceback: the %run one,
1415 # one inside bdb.py, and the command-line typed by the
1422 # one inside bdb.py, and the command-line typed by the
1416 # user (run by exec in pdb itself).
1423 # user (run by exec in pdb itself).
1417 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1424 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1418 else:
1425 else:
1419 if runner is None:
1426 if runner is None:
1420 runner = self.shell.safe_execfile
1427 runner = self.shell.safe_execfile
1421 if opts.has_key('t'):
1428 if opts.has_key('t'):
1422 try:
1429 try:
1423 nruns = int(opts['N'][0])
1430 nruns = int(opts['N'][0])
1424 if nruns < 1:
1431 if nruns < 1:
1425 error('Number of runs must be >=1')
1432 error('Number of runs must be >=1')
1426 return
1433 return
1427 except (KeyError):
1434 except (KeyError):
1428 nruns = 1
1435 nruns = 1
1429 if nruns == 1:
1436 if nruns == 1:
1430 t0 = clock2()
1437 t0 = clock2()
1431 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1438 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1432 t1 = clock2()
1439 t1 = clock2()
1433 t_usr = t1[0]-t0[0]
1440 t_usr = t1[0]-t0[0]
1434 t_sys = t1[1]-t1[1]
1441 t_sys = t1[1]-t1[1]
1435 print "\nIPython CPU timings (estimated):"
1442 print "\nIPython CPU timings (estimated):"
1436 print " User : %10s s." % t_usr
1443 print " User : %10s s." % t_usr
1437 print " System: %10s s." % t_sys
1444 print " System: %10s s." % t_sys
1438 else:
1445 else:
1439 runs = range(nruns)
1446 runs = range(nruns)
1440 t0 = clock2()
1447 t0 = clock2()
1441 for nr in runs:
1448 for nr in runs:
1442 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1449 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1443 t1 = clock2()
1450 t1 = clock2()
1444 t_usr = t1[0]-t0[0]
1451 t_usr = t1[0]-t0[0]
1445 t_sys = t1[1]-t1[1]
1452 t_sys = t1[1]-t1[1]
1446 print "\nIPython CPU timings (estimated):"
1453 print "\nIPython CPU timings (estimated):"
1447 print "Total runs performed:",nruns
1454 print "Total runs performed:",nruns
1448 print " Times : %10s %10s" % ('Total','Per run')
1455 print " Times : %10s %10s" % ('Total','Per run')
1449 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1456 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1450 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1457 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1451
1458
1452 else:
1459 else:
1453 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1460 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1454 if opts.has_key('i'):
1461 if opts.has_key('i'):
1455 self.shell.user_ns['__name__'] = __name__save
1462 self.shell.user_ns['__name__'] = __name__save
1456 else:
1463 else:
1457 # update IPython interactive namespace
1464 # update IPython interactive namespace
1458 del prog_ns['__name__']
1465 del prog_ns['__name__']
1459 self.shell.user_ns.update(prog_ns)
1466 self.shell.user_ns.update(prog_ns)
1460 finally:
1467 finally:
1461 sys.argv = save_argv
1468 sys.argv = save_argv
1462 return stats
1469 return stats
1463
1470
1464 def magic_runlog(self, parameter_s =''):
1471 def magic_runlog(self, parameter_s =''):
1465 """Run files as logs.
1472 """Run files as logs.
1466
1473
1467 Usage:\\
1474 Usage:\\
1468 %runlog file1 file2 ...
1475 %runlog file1 file2 ...
1469
1476
1470 Run the named files (treating them as log files) in sequence inside
1477 Run the named files (treating them as log files) in sequence inside
1471 the interpreter, and return to the prompt. This is much slower than
1478 the interpreter, and return to the prompt. This is much slower than
1472 %run because each line is executed in a try/except block, but it
1479 %run because each line is executed in a try/except block, but it
1473 allows running files with syntax errors in them.
1480 allows running files with syntax errors in them.
1474
1481
1475 Normally IPython will guess when a file is one of its own logfiles, so
1482 Normally IPython will guess when a file is one of its own logfiles, so
1476 you can typically use %run even for logs. This shorthand allows you to
1483 you can typically use %run even for logs. This shorthand allows you to
1477 force any file to be treated as a log file."""
1484 force any file to be treated as a log file."""
1478
1485
1479 for f in parameter_s.split():
1486 for f in parameter_s.split():
1480 self.shell.safe_execfile(f,self.shell.user_ns,
1487 self.shell.safe_execfile(f,self.shell.user_ns,
1481 self.shell.user_ns,islog=1)
1488 self.shell.user_ns,islog=1)
1482
1489
1483 def magic_time(self,parameter_s = ''):
1490 def magic_time(self,parameter_s = ''):
1484 """Time execution of a Python statement or expression.
1491 """Time execution of a Python statement or expression.
1485
1492
1486 The CPU and wall clock times are printed, and the value of the
1493 The CPU and wall clock times are printed, and the value of the
1487 expression (if any) is returned. Note that under Win32, system time
1494 expression (if any) is returned. Note that under Win32, system time
1488 is always reported as 0, since it can not be measured.
1495 is always reported as 0, since it can not be measured.
1489
1496
1490 This function provides very basic timing functionality. In Python
1497 This function provides very basic timing functionality. In Python
1491 2.3, the timeit module offers more control and sophistication, but for
1498 2.3, the timeit module offers more control and sophistication, but for
1492 now IPython supports Python 2.2, so we can not rely on timeit being
1499 now IPython supports Python 2.2, so we can not rely on timeit being
1493 present.
1500 present.
1494
1501
1495 Some examples:
1502 Some examples:
1496
1503
1497 In [1]: time 2**128
1504 In [1]: time 2**128
1498 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1505 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1499 Wall time: 0.00
1506 Wall time: 0.00
1500 Out[1]: 340282366920938463463374607431768211456L
1507 Out[1]: 340282366920938463463374607431768211456L
1501
1508
1502 In [2]: n = 1000000
1509 In [2]: n = 1000000
1503
1510
1504 In [3]: time sum(range(n))
1511 In [3]: time sum(range(n))
1505 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1512 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1506 Wall time: 1.37
1513 Wall time: 1.37
1507 Out[3]: 499999500000L
1514 Out[3]: 499999500000L
1508
1515
1509 In [4]: time print 'hello world'
1516 In [4]: time print 'hello world'
1510 hello world
1517 hello world
1511 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1518 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1512 Wall time: 0.00
1519 Wall time: 0.00
1513 """
1520 """
1514
1521
1515 # fail immediately if the given expression can't be compiled
1522 # fail immediately if the given expression can't be compiled
1516 try:
1523 try:
1517 mode = 'eval'
1524 mode = 'eval'
1518 code = compile(parameter_s,'<timed eval>',mode)
1525 code = compile(parameter_s,'<timed eval>',mode)
1519 except SyntaxError:
1526 except SyntaxError:
1520 mode = 'exec'
1527 mode = 'exec'
1521 code = compile(parameter_s,'<timed exec>',mode)
1528 code = compile(parameter_s,'<timed exec>',mode)
1522 # skew measurement as little as possible
1529 # skew measurement as little as possible
1523 glob = self.shell.user_ns
1530 glob = self.shell.user_ns
1524 clk = clock2
1531 clk = clock2
1525 wtime = time.time
1532 wtime = time.time
1526 # time execution
1533 # time execution
1527 wall_st = wtime()
1534 wall_st = wtime()
1528 if mode=='eval':
1535 if mode=='eval':
1529 st = clk()
1536 st = clk()
1530 out = eval(code,glob)
1537 out = eval(code,glob)
1531 end = clk()
1538 end = clk()
1532 else:
1539 else:
1533 st = clk()
1540 st = clk()
1534 exec code in glob
1541 exec code in glob
1535 end = clk()
1542 end = clk()
1536 out = None
1543 out = None
1537 wall_end = wtime()
1544 wall_end = wtime()
1538 # Compute actual times and report
1545 # Compute actual times and report
1539 wall_time = wall_end-wall_st
1546 wall_time = wall_end-wall_st
1540 cpu_user = end[0]-st[0]
1547 cpu_user = end[0]-st[0]
1541 cpu_sys = end[1]-st[1]
1548 cpu_sys = end[1]-st[1]
1542 cpu_tot = cpu_user+cpu_sys
1549 cpu_tot = cpu_user+cpu_sys
1543 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1550 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1544 (cpu_user,cpu_sys,cpu_tot)
1551 (cpu_user,cpu_sys,cpu_tot)
1545 print "Wall time: %.2f" % wall_time
1552 print "Wall time: %.2f" % wall_time
1546 return out
1553 return out
1547
1554
1548 def magic_macro(self,parameter_s = ''):
1555 def magic_macro(self,parameter_s = ''):
1549 """Define a set of input lines as a macro for future re-execution.
1556 """Define a set of input lines as a macro for future re-execution.
1550
1557
1551 Usage:\\
1558 Usage:\\
1552 %macro name n1:n2 n3:n4 ... n5 .. n6 ...
1559 %macro name n1:n2 n3:n4 ... n5 .. n6 ...
1553
1560
1554 This will define a global variable called `name` which is a string
1561 This will define a global variable called `name` which is a string
1555 made of joining the slices and lines you specify (n1,n2,... numbers
1562 made of joining the slices and lines you specify (n1,n2,... numbers
1556 above) from your input history into a single string. This variable
1563 above) from your input history into a single string. This variable
1557 acts like an automatic function which re-executes those lines as if
1564 acts like an automatic function which re-executes those lines as if
1558 you had typed them. You just type 'name' at the prompt and the code
1565 you had typed them. You just type 'name' at the prompt and the code
1559 executes.
1566 executes.
1560
1567
1561 Note that the slices use the standard Python slicing notation (5:8
1568 Note that the slices use the standard Python slicing notation (5:8
1562 means include lines numbered 5,6,7).
1569 means include lines numbered 5,6,7).
1563
1570
1564 For example, if your history contains (%hist prints it):
1571 For example, if your history contains (%hist prints it):
1565
1572
1566 44: x=1\\
1573 44: x=1\\
1567 45: y=3\\
1574 45: y=3\\
1568 46: z=x+y\\
1575 46: z=x+y\\
1569 47: print x\\
1576 47: print x\\
1570 48: a=5\\
1577 48: a=5\\
1571 49: print 'x',x,'y',y\\
1578 49: print 'x',x,'y',y\\
1572
1579
1573 you can create a macro with lines 44 through 47 (included) and line 49
1580 you can create a macro with lines 44 through 47 (included) and line 49
1574 called my_macro with:
1581 called my_macro with:
1575
1582
1576 In [51]: %macro my_macro 44:48 49
1583 In [51]: %macro my_macro 44:48 49
1577
1584
1578 Now, typing `my_macro` (without quotes) will re-execute all this code
1585 Now, typing `my_macro` (without quotes) will re-execute all this code
1579 in one pass.
1586 in one pass.
1580
1587
1581 You don't need to give the line-numbers in order, and any given line
1588 You don't need to give the line-numbers in order, and any given line
1582 number can appear multiple times. You can assemble macros with any
1589 number can appear multiple times. You can assemble macros with any
1583 lines from your input history in any order.
1590 lines from your input history in any order.
1584
1591
1585 The macro is a simple object which holds its value in an attribute,
1592 The macro is a simple object which holds its value in an attribute,
1586 but IPython's display system checks for macros and executes them as
1593 but IPython's display system checks for macros and executes them as
1587 code instead of printing them when you type their name.
1594 code instead of printing them when you type their name.
1588
1595
1589 You can view a macro's contents by explicitly printing it with:
1596 You can view a macro's contents by explicitly printing it with:
1590
1597
1591 'print macro_name'.
1598 'print macro_name'.
1592
1599
1593 For one-off cases which DON'T contain magic function calls in them you
1600 For one-off cases which DON'T contain magic function calls in them you
1594 can obtain similar results by explicitly executing slices from your
1601 can obtain similar results by explicitly executing slices from your
1595 input history with:
1602 input history with:
1596
1603
1597 In [60]: exec In[44:48]+In[49]"""
1604 In [60]: exec In[44:48]+In[49]"""
1598
1605
1599 args = parameter_s.split()
1606 args = parameter_s.split()
1600 name,ranges = args[0], args[1:]
1607 name,ranges = args[0], args[1:]
1601 #print 'rng',ranges # dbg
1608 #print 'rng',ranges # dbg
1602 cmds = self.extract_input_slices(ranges)
1609 cmds = self.extract_input_slices(ranges)
1603 macro = Macro(cmds)
1610 macro = Macro(cmds)
1604 self.shell.user_ns.update({name:macro})
1611 self.shell.user_ns.update({name:macro})
1605 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1612 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1606 print 'Macro contents:'
1613 print 'Macro contents:'
1607 print str(macro).rstrip(),
1614 print str(macro).rstrip(),
1608
1615
1609 def magic_save(self,parameter_s = ''):
1616 def magic_save(self,parameter_s = ''):
1610 """Save a set of lines to a given filename.
1617 """Save a set of lines to a given filename.
1611
1618
1612 Usage:\\
1619 Usage:\\
1613 %save filename n1:n2 n3:n4 ... n5 .. n6 ...
1620 %save filename n1:n2 n3:n4 ... n5 .. n6 ...
1614
1621
1615 This function uses the same syntax as %macro for line extraction, but
1622 This function uses the same syntax as %macro for line extraction, but
1616 instead of creating a macro it saves the resulting string to the
1623 instead of creating a macro it saves the resulting string to the
1617 filename you specify.
1624 filename you specify.
1618
1625
1619 It adds a '.py' extension to the file if you don't do so yourself, and
1626 It adds a '.py' extension to the file if you don't do so yourself, and
1620 it asks for confirmation before overwriting existing files."""
1627 it asks for confirmation before overwriting existing files."""
1621
1628
1622 args = parameter_s.split()
1629 args = parameter_s.split()
1623 fname,ranges = args[0], args[1:]
1630 fname,ranges = args[0], args[1:]
1624 if not fname.endswith('.py'):
1631 if not fname.endswith('.py'):
1625 fname += '.py'
1632 fname += '.py'
1626 if os.path.isfile(fname):
1633 if os.path.isfile(fname):
1627 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1634 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1628 if ans.lower() not in ['y','yes']:
1635 if ans.lower() not in ['y','yes']:
1629 print 'Operation cancelled.'
1636 print 'Operation cancelled.'
1630 return
1637 return
1631 cmds = ''.join(self.extract_input_slices(ranges))
1638 cmds = ''.join(self.extract_input_slices(ranges))
1632 f = file(fname,'w')
1639 f = file(fname,'w')
1633 f.write(cmds)
1640 f.write(cmds)
1634 f.close()
1641 f.close()
1635 print 'The following commands were written to file `%s`:' % fname
1642 print 'The following commands were written to file `%s`:' % fname
1636 print cmds
1643 print cmds
1637
1644
1638 def magic_ed(self,parameter_s = ''):
1645 def magic_ed(self,parameter_s = ''):
1639 """Alias to %edit."""
1646 """Alias to %edit."""
1640 return self.magic_edit(parameter_s)
1647 return self.magic_edit(parameter_s)
1641
1648
1642 def magic_edit(self,parameter_s = '',last_call=['','']):
1649 def magic_edit(self,parameter_s = '',last_call=['','']):
1643 """Bring up an editor and execute the resulting code.
1650 """Bring up an editor and execute the resulting code.
1644
1651
1645 Usage:
1652 Usage:
1646 %edit [options] [args]
1653 %edit [options] [args]
1647
1654
1648 %edit runs IPython's editor hook. The default version of this hook is
1655 %edit runs IPython's editor hook. The default version of this hook is
1649 set to call the __IPYTHON__.rc.editor command. This is read from your
1656 set to call the __IPYTHON__.rc.editor command. This is read from your
1650 environment variable $EDITOR. If this isn't found, it will default to
1657 environment variable $EDITOR. If this isn't found, it will default to
1651 vi under Linux/Unix and to notepad under Windows. See the end of this
1658 vi under Linux/Unix and to notepad under Windows. See the end of this
1652 docstring for how to change the editor hook.
1659 docstring for how to change the editor hook.
1653
1660
1654 You can also set the value of this editor via the command line option
1661 You can also set the value of this editor via the command line option
1655 '-editor' or in your ipythonrc file. This is useful if you wish to use
1662 '-editor' or in your ipythonrc file. This is useful if you wish to use
1656 specifically for IPython an editor different from your typical default
1663 specifically for IPython an editor different from your typical default
1657 (and for Windows users who typically don't set environment variables).
1664 (and for Windows users who typically don't set environment variables).
1658
1665
1659 This command allows you to conveniently edit multi-line code right in
1666 This command allows you to conveniently edit multi-line code right in
1660 your IPython session.
1667 your IPython session.
1661
1668
1662 If called without arguments, %edit opens up an empty editor with a
1669 If called without arguments, %edit opens up an empty editor with a
1663 temporary file and will execute the contents of this file when you
1670 temporary file and will execute the contents of this file when you
1664 close it (don't forget to save it!).
1671 close it (don't forget to save it!).
1665
1672
1666 Options:
1673 Options:
1667
1674
1668 -p: this will call the editor with the same data as the previous time
1675 -p: this will call the editor with the same data as the previous time
1669 it was used, regardless of how long ago (in your current session) it
1676 it was used, regardless of how long ago (in your current session) it
1670 was.
1677 was.
1671
1678
1672 -x: do not execute the edited code immediately upon exit. This is
1679 -x: do not execute the edited code immediately upon exit. This is
1673 mainly useful if you are editing programs which need to be called with
1680 mainly useful if you are editing programs which need to be called with
1674 command line arguments, which you can then do using %run.
1681 command line arguments, which you can then do using %run.
1675
1682
1676 Arguments:
1683 Arguments:
1677
1684
1678 If arguments are given, the following possibilites exist:
1685 If arguments are given, the following possibilites exist:
1679
1686
1680 - The arguments are numbers or pairs of colon-separated numbers (like
1687 - The arguments are numbers or pairs of colon-separated numbers (like
1681 1 4:8 9). These are interpreted as lines of previous input to be
1688 1 4:8 9). These are interpreted as lines of previous input to be
1682 loaded into the editor. The syntax is the same of the %macro command.
1689 loaded into the editor. The syntax is the same of the %macro command.
1683
1690
1684 - If the argument doesn't start with a number, it is evaluated as a
1691 - If the argument doesn't start with a number, it is evaluated as a
1685 variable and its contents loaded into the editor. You can thus edit
1692 variable and its contents loaded into the editor. You can thus edit
1686 any string which contains python code (including the result of
1693 any string which contains python code (including the result of
1687 previous edits).
1694 previous edits).
1688
1695
1689 - If the argument is the name of an object (other than a string),
1696 - If the argument is the name of an object (other than a string),
1690 IPython will try to locate the file where it was defined and open the
1697 IPython will try to locate the file where it was defined and open the
1691 editor at the point where it is defined. You can use `%edit function`
1698 editor at the point where it is defined. You can use `%edit function`
1692 to load an editor exactly at the point where 'function' is defined,
1699 to load an editor exactly at the point where 'function' is defined,
1693 edit it and have the file be executed automatically.
1700 edit it and have the file be executed automatically.
1694
1701
1695 Note: opening at an exact line is only supported under Unix, and some
1702 Note: opening at an exact line is only supported under Unix, and some
1696 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1703 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1697 '+NUMBER' parameter necessary for this feature. Good editors like
1704 '+NUMBER' parameter necessary for this feature. Good editors like
1698 (X)Emacs, vi, jed, pico and joe all do.
1705 (X)Emacs, vi, jed, pico and joe all do.
1699
1706
1700 - If the argument is not found as a variable, IPython will look for a
1707 - If the argument is not found as a variable, IPython will look for a
1701 file with that name (adding .py if necessary) and load it into the
1708 file with that name (adding .py if necessary) and load it into the
1702 editor. It will execute its contents with execfile() when you exit,
1709 editor. It will execute its contents with execfile() when you exit,
1703 loading any code in the file into your interactive namespace.
1710 loading any code in the file into your interactive namespace.
1704
1711
1705 After executing your code, %edit will return as output the code you
1712 After executing your code, %edit will return as output the code you
1706 typed in the editor (except when it was an existing file). This way
1713 typed in the editor (except when it was an existing file). This way
1707 you can reload the code in further invocations of %edit as a variable,
1714 you can reload the code in further invocations of %edit as a variable,
1708 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1715 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1709 the output.
1716 the output.
1710
1717
1711 Note that %edit is also available through the alias %ed.
1718 Note that %edit is also available through the alias %ed.
1712
1719
1713 This is an example of creating a simple function inside the editor and
1720 This is an example of creating a simple function inside the editor and
1714 then modifying it. First, start up the editor:
1721 then modifying it. First, start up the editor:
1715
1722
1716 In [1]: ed\\
1723 In [1]: ed\\
1717 Editing... done. Executing edited code...\\
1724 Editing... done. Executing edited code...\\
1718 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1725 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1719
1726
1720 We can then call the function foo():
1727 We can then call the function foo():
1721
1728
1722 In [2]: foo()\\
1729 In [2]: foo()\\
1723 foo() was defined in an editing session
1730 foo() was defined in an editing session
1724
1731
1725 Now we edit foo. IPython automatically loads the editor with the
1732 Now we edit foo. IPython automatically loads the editor with the
1726 (temporary) file where foo() was previously defined:
1733 (temporary) file where foo() was previously defined:
1727
1734
1728 In [3]: ed foo\\
1735 In [3]: ed foo\\
1729 Editing... done. Executing edited code...
1736 Editing... done. Executing edited code...
1730
1737
1731 And if we call foo() again we get the modified version:
1738 And if we call foo() again we get the modified version:
1732
1739
1733 In [4]: foo()\\
1740 In [4]: foo()\\
1734 foo() has now been changed!
1741 foo() has now been changed!
1735
1742
1736 Here is an example of how to edit a code snippet successive
1743 Here is an example of how to edit a code snippet successive
1737 times. First we call the editor:
1744 times. First we call the editor:
1738
1745
1739 In [8]: ed\\
1746 In [8]: ed\\
1740 Editing... done. Executing edited code...\\
1747 Editing... done. Executing edited code...\\
1741 hello\\
1748 hello\\
1742 Out[8]: "print 'hello'\\n"
1749 Out[8]: "print 'hello'\\n"
1743
1750
1744 Now we call it again with the previous output (stored in _):
1751 Now we call it again with the previous output (stored in _):
1745
1752
1746 In [9]: ed _\\
1753 In [9]: ed _\\
1747 Editing... done. Executing edited code...\\
1754 Editing... done. Executing edited code...\\
1748 hello world\\
1755 hello world\\
1749 Out[9]: "print 'hello world'\\n"
1756 Out[9]: "print 'hello world'\\n"
1750
1757
1751 Now we call it with the output #8 (stored in _8, also as Out[8]):
1758 Now we call it with the output #8 (stored in _8, also as Out[8]):
1752
1759
1753 In [10]: ed _8\\
1760 In [10]: ed _8\\
1754 Editing... done. Executing edited code...\\
1761 Editing... done. Executing edited code...\\
1755 hello again\\
1762 hello again\\
1756 Out[10]: "print 'hello again'\\n"
1763 Out[10]: "print 'hello again'\\n"
1757
1764
1758
1765
1759 Changing the default editor hook:
1766 Changing the default editor hook:
1760
1767
1761 If you wish to write your own editor hook, you can put it in a
1768 If you wish to write your own editor hook, you can put it in a
1762 configuration file which you load at startup time. The default hook
1769 configuration file which you load at startup time. The default hook
1763 is defined in the IPython.hooks module, and you can use that as a
1770 is defined in the IPython.hooks module, and you can use that as a
1764 starting example for further modifications. That file also has
1771 starting example for further modifications. That file also has
1765 general instructions on how to set a new hook for use once you've
1772 general instructions on how to set a new hook for use once you've
1766 defined it."""
1773 defined it."""
1767
1774
1768 # FIXME: This function has become a convoluted mess. It needs a
1775 # FIXME: This function has become a convoluted mess. It needs a
1769 # ground-up rewrite with clean, simple logic.
1776 # ground-up rewrite with clean, simple logic.
1770
1777
1771 def make_filename(arg):
1778 def make_filename(arg):
1772 "Make a filename from the given args"
1779 "Make a filename from the given args"
1773 try:
1780 try:
1774 filename = get_py_filename(arg)
1781 filename = get_py_filename(arg)
1775 except IOError:
1782 except IOError:
1776 if args.endswith('.py'):
1783 if args.endswith('.py'):
1777 filename = arg
1784 filename = arg
1778 else:
1785 else:
1779 filename = None
1786 filename = None
1780 return filename
1787 return filename
1781
1788
1782 # custom exceptions
1789 # custom exceptions
1783 class DataIsObject(Exception): pass
1790 class DataIsObject(Exception): pass
1784
1791
1785 opts,args = self.parse_options(parameter_s,'px')
1792 opts,args = self.parse_options(parameter_s,'px')
1786
1793
1787 # Default line number value
1794 # Default line number value
1788 lineno = None
1795 lineno = None
1789 if opts.has_key('p'):
1796 if opts.has_key('p'):
1790 args = '_%s' % last_call[0]
1797 args = '_%s' % last_call[0]
1791 if not self.shell.user_ns.has_key(args):
1798 if not self.shell.user_ns.has_key(args):
1792 args = last_call[1]
1799 args = last_call[1]
1793
1800
1794 # use last_call to remember the state of the previous call, but don't
1801 # use last_call to remember the state of the previous call, but don't
1795 # let it be clobbered by successive '-p' calls.
1802 # let it be clobbered by successive '-p' calls.
1796 try:
1803 try:
1797 last_call[0] = self.shell.outputcache.prompt_count
1804 last_call[0] = self.shell.outputcache.prompt_count
1798 if not opts.has_key('p'):
1805 if not opts.has_key('p'):
1799 last_call[1] = parameter_s
1806 last_call[1] = parameter_s
1800 except:
1807 except:
1801 pass
1808 pass
1802
1809
1803 # by default this is done with temp files, except when the given
1810 # by default this is done with temp files, except when the given
1804 # arg is a filename
1811 # arg is a filename
1805 use_temp = 1
1812 use_temp = 1
1806
1813
1807 if re.match(r'\d',args):
1814 if re.match(r'\d',args):
1808 # Mode where user specifies ranges of lines, like in %macro.
1815 # Mode where user specifies ranges of lines, like in %macro.
1809 # This means that you can't edit files whose names begin with
1816 # This means that you can't edit files whose names begin with
1810 # numbers this way. Tough.
1817 # numbers this way. Tough.
1811 ranges = args.split()
1818 ranges = args.split()
1812 data = ''.join(self.extract_input_slices(ranges))
1819 data = ''.join(self.extract_input_slices(ranges))
1813 elif args.endswith('.py'):
1820 elif args.endswith('.py'):
1814 filename = make_filename(args)
1821 filename = make_filename(args)
1815 data = ''
1822 data = ''
1816 use_temp = 0
1823 use_temp = 0
1817 elif args:
1824 elif args:
1818 try:
1825 try:
1819 # Load the parameter given as a variable. If not a string,
1826 # Load the parameter given as a variable. If not a string,
1820 # process it as an object instead (below)
1827 # process it as an object instead (below)
1821
1828
1822 #print '*** args',args,'type',type(args) # dbg
1829 #print '*** args',args,'type',type(args) # dbg
1823 data = eval(args,self.shell.user_ns)
1830 data = eval(args,self.shell.user_ns)
1824 if not type(data) in StringTypes:
1831 if not type(data) in StringTypes:
1825 raise DataIsObject
1832 raise DataIsObject
1826 except (NameError,SyntaxError):
1833 except (NameError,SyntaxError):
1827 # given argument is not a variable, try as a filename
1834 # given argument is not a variable, try as a filename
1828 filename = make_filename(args)
1835 filename = make_filename(args)
1829 if filename is None:
1836 if filename is None:
1830 warn("Argument given (%s) can't be found as a variable "
1837 warn("Argument given (%s) can't be found as a variable "
1831 "or as a filename." % args)
1838 "or as a filename." % args)
1832 return
1839 return
1833 data = ''
1840 data = ''
1834 use_temp = 0
1841 use_temp = 0
1835 except DataIsObject:
1842 except DataIsObject:
1836 # For objects, try to edit the file where they are defined
1843 # For objects, try to edit the file where they are defined
1837 try:
1844 try:
1838 filename = inspect.getabsfile(data)
1845 filename = inspect.getabsfile(data)
1839 datafile = 1
1846 datafile = 1
1840 except TypeError:
1847 except TypeError:
1841 filename = make_filename(args)
1848 filename = make_filename(args)
1842 datafile = 1
1849 datafile = 1
1843 warn('Could not find file where `%s` is defined.\n'
1850 warn('Could not find file where `%s` is defined.\n'
1844 'Opening a file named `%s`' % (args,filename))
1851 'Opening a file named `%s`' % (args,filename))
1845 # Now, make sure we can actually read the source (if it was in
1852 # Now, make sure we can actually read the source (if it was in
1846 # a temp file it's gone by now).
1853 # a temp file it's gone by now).
1847 if datafile:
1854 if datafile:
1848 try:
1855 try:
1849 lineno = inspect.getsourcelines(data)[1]
1856 lineno = inspect.getsourcelines(data)[1]
1850 except IOError:
1857 except IOError:
1851 filename = make_filename(args)
1858 filename = make_filename(args)
1852 if filename is None:
1859 if filename is None:
1853 warn('The file `%s` where `%s` was defined cannot '
1860 warn('The file `%s` where `%s` was defined cannot '
1854 'be read.' % (filename,data))
1861 'be read.' % (filename,data))
1855 return
1862 return
1856 use_temp = 0
1863 use_temp = 0
1857 else:
1864 else:
1858 data = ''
1865 data = ''
1859
1866
1860 if use_temp:
1867 if use_temp:
1861 filename = tempfile.mktemp('.py')
1868 filename = tempfile.mktemp('.py')
1862 self.shell.tempfiles.append(filename)
1869 self.shell.tempfiles.append(filename)
1863
1870
1864 if data and use_temp:
1871 if data and use_temp:
1865 tmp_file = open(filename,'w')
1872 tmp_file = open(filename,'w')
1866 tmp_file.write(data)
1873 tmp_file.write(data)
1867 tmp_file.close()
1874 tmp_file.close()
1868
1875
1869 # do actual editing here
1876 # do actual editing here
1870 print 'Editing...',
1877 print 'Editing...',
1871 sys.stdout.flush()
1878 sys.stdout.flush()
1872 self.shell.hooks.editor(filename,lineno)
1879 self.shell.hooks.editor(filename,lineno)
1873 if opts.has_key('x'): # -x prevents actual execution
1880 if opts.has_key('x'): # -x prevents actual execution
1874 print
1881 print
1875 else:
1882 else:
1876 print 'done. Executing edited code...'
1883 print 'done. Executing edited code...'
1877 try:
1884 try:
1878 self.shell.safe_execfile(filename,self.shell.user_ns)
1885 self.shell.safe_execfile(filename,self.shell.user_ns)
1879 except IOError,msg:
1886 except IOError,msg:
1880 if msg.filename == filename:
1887 if msg.filename == filename:
1881 warn('File not found. Did you forget to save?')
1888 warn('File not found. Did you forget to save?')
1882 return
1889 return
1883 else:
1890 else:
1884 self.shell.showtraceback()
1891 self.shell.showtraceback()
1885 except:
1892 except:
1886 self.shell.showtraceback()
1893 self.shell.showtraceback()
1887 if use_temp:
1894 if use_temp:
1888 contents = open(filename).read()
1895 contents = open(filename).read()
1889 return contents
1896 return contents
1890
1897
1891 def magic_xmode(self,parameter_s = ''):
1898 def magic_xmode(self,parameter_s = ''):
1892 """Switch modes for the exception handlers.
1899 """Switch modes for the exception handlers.
1893
1900
1894 Valid modes: Plain, Context and Verbose.
1901 Valid modes: Plain, Context and Verbose.
1895
1902
1896 If called without arguments, acts as a toggle."""
1903 If called without arguments, acts as a toggle."""
1897
1904
1905 def xmode_switch_err(name):
1906 warn('Error changing %s exception modes.\n%s' %
1907 (name,sys.exc_info()[1]))
1908
1898 new_mode = parameter_s.strip().capitalize()
1909 new_mode = parameter_s.strip().capitalize()
1899 try:
1910 try:
1900 self.InteractiveTB.set_mode(mode = new_mode)
1911 self.InteractiveTB.set_mode(mode=new_mode)
1901 print 'Exception reporting mode:',self.InteractiveTB.mode
1912 print 'Exception reporting mode:',self.InteractiveTB.mode
1902 except:
1913 except:
1903 warn('Error changing exception modes.\n' + str(sys.exc_info()[1]))
1914 xmode_switch_err('user')
1915
1916 # threaded shells use a special handler in sys.excepthook
1917 if self.isthreaded:
1918 try:
1919 self.shell.sys_excepthook.set_mode(mode=new_mode)
1920 except:
1921 xmode_switch_err('threaded')
1904
1922
1905 def magic_colors(self,parameter_s = ''):
1923 def magic_colors(self,parameter_s = ''):
1906 """Switch color scheme for prompts, info system and exception handlers.
1924 """Switch color scheme for prompts, info system and exception handlers.
1907
1925
1908 Currently implemented schemes: NoColor, Linux, LightBG.
1926 Currently implemented schemes: NoColor, Linux, LightBG.
1909
1927
1910 Color scheme names are not case-sensitive."""
1928 Color scheme names are not case-sensitive."""
1911
1929
1930 def color_switch_err(name):
1931 warn('Error changing %s color schemes.\n%s' %
1932 (name,sys.exc_info()[1]))
1933
1934
1912 new_scheme = parameter_s.strip()
1935 new_scheme = parameter_s.strip()
1913 if not new_scheme:
1936 if not new_scheme:
1914 print 'You must specify a color scheme.'
1937 print 'You must specify a color scheme.'
1915 return
1938 return
1916 # Under Windows, check for Gary Bishop's readline, which is necessary
1939 # Under Windows, check for Gary Bishop's readline, which is necessary
1917 # for ANSI coloring
1940 # for ANSI coloring
1918 if os.name in ['nt','dos']:
1941 if os.name in ['nt','dos']:
1919 try:
1942 try:
1920 import readline
1943 import readline
1921 except ImportError:
1944 except ImportError:
1922 has_readline = 0
1945 has_readline = 0
1923 else:
1946 else:
1924 try:
1947 try:
1925 readline.GetOutputFile()
1948 readline.GetOutputFile()
1926 except AttributeError:
1949 except AttributeError:
1927 has_readline = 0
1950 has_readline = 0
1928 else:
1951 else:
1929 has_readline = 1
1952 has_readline = 1
1930 if not has_readline:
1953 if not has_readline:
1931 msg = """\
1954 msg = """\
1932 Proper color support under MS Windows requires Gary Bishop's readline library.
1955 Proper color support under MS Windows requires Gary Bishop's readline library.
1933 You can find it at:
1956 You can find it at:
1934 http://sourceforge.net/projects/uncpythontools
1957 http://sourceforge.net/projects/uncpythontools
1935 Gary's readline needs the ctypes module, from:
1958 Gary's readline needs the ctypes module, from:
1936 http://starship.python.net/crew/theller/ctypes
1959 http://starship.python.net/crew/theller/ctypes
1937
1960
1938 Defaulting color scheme to 'NoColor'"""
1961 Defaulting color scheme to 'NoColor'"""
1939 new_scheme = 'NoColor'
1962 new_scheme = 'NoColor'
1940 warn(msg)
1963 warn(msg)
1941
1964
1942 # Set prompt colors
1965 # Set prompt colors
1943 try:
1966 try:
1944 self.shell.outputcache.set_colors(new_scheme)
1967 self.shell.outputcache.set_colors(new_scheme)
1945 except:
1968 except:
1946 warn('Error changing prompt color schemes.\n'
1969 color_switch_err('prompt')
1947 + str(sys.exc_info()[1]))
1948 else:
1970 else:
1949 self.shell.rc.colors = \
1971 self.shell.rc.colors = \
1950 self.shell.outputcache.color_table.active_scheme_name
1972 self.shell.outputcache.color_table.active_scheme_name
1951 # Set exception colors
1973 # Set exception colors
1952 try:
1974 try:
1953 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1975 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1954 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1976 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1955 except:
1977 except:
1956 warn('Error changing exception color schemes.\n'
1978 color_switch_err('exception')
1957 + str(sys.exc_info()[1]))
1979
1980 # threaded shells use a verbose traceback in sys.excepthook
1981 if self.isthreaded:
1982 try:
1983 self.shell.sys_excepthook.set_colors(scheme=new_scheme)
1984 except:
1985 color_switch_err('system exception handler')
1986
1958 # Set info (for 'object?') colors
1987 # Set info (for 'object?') colors
1959 if self.shell.rc.color_info:
1988 if self.shell.rc.color_info:
1960 try:
1989 try:
1961 self.shell.inspector.set_active_scheme(new_scheme)
1990 self.shell.inspector.set_active_scheme(new_scheme)
1962 except:
1991 except:
1963 warn('Error changing object inspector color schemes.\n'
1992 color_switch_err('object inspector')
1964 + str(sys.exc_info()[1]))
1965 else:
1993 else:
1966 self.shell.inspector.set_active_scheme('NoColor')
1994 self.shell.inspector.set_active_scheme('NoColor')
1967
1995
1968 def magic_color_info(self,parameter_s = ''):
1996 def magic_color_info(self,parameter_s = ''):
1969 """Toggle color_info.
1997 """Toggle color_info.
1970
1998
1971 The color_info configuration parameter controls whether colors are
1999 The color_info configuration parameter controls whether colors are
1972 used for displaying object details (by things like %psource, %pfile or
2000 used for displaying object details (by things like %psource, %pfile or
1973 the '?' system). This function toggles this value with each call.
2001 the '?' system). This function toggles this value with each call.
1974
2002
1975 Note that unless you have a fairly recent pager (less works better
2003 Note that unless you have a fairly recent pager (less works better
1976 than more) in your system, using colored object information displays
2004 than more) in your system, using colored object information displays
1977 will not work properly. Test it and see."""
2005 will not work properly. Test it and see."""
1978
2006
1979 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2007 self.shell.rc.color_info = 1 - self.shell.rc.color_info
1980 self.magic_colors(self.shell.rc.colors)
2008 self.magic_colors(self.shell.rc.colors)
1981 print 'Object introspection functions have now coloring:',
2009 print 'Object introspection functions have now coloring:',
1982 print ['OFF','ON'][self.shell.rc.color_info]
2010 print ['OFF','ON'][self.shell.rc.color_info]
1983
2011
1984 def magic_Pprint(self, parameter_s=''):
2012 def magic_Pprint(self, parameter_s=''):
1985 """Toggle pretty printing on/off."""
2013 """Toggle pretty printing on/off."""
1986
2014
1987 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
2015 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
1988 print 'Pretty printing has been turned', \
2016 print 'Pretty printing has been turned', \
1989 ['OFF','ON'][self.shell.outputcache.Pprint]
2017 ['OFF','ON'][self.shell.outputcache.Pprint]
1990
2018
1991 def magic_exit(self, parameter_s=''):
2019 def magic_exit(self, parameter_s=''):
1992 """Exit IPython, confirming if configured to do so.
2020 """Exit IPython, confirming if configured to do so.
1993
2021
1994 You can configure whether IPython asks for confirmation upon exit by
2022 You can configure whether IPython asks for confirmation upon exit by
1995 setting the confirm_exit flag in the ipythonrc file."""
2023 setting the confirm_exit flag in the ipythonrc file."""
1996
2024
1997 self.shell.exit()
2025 self.shell.exit()
1998
2026
1999 def magic_quit(self, parameter_s=''):
2027 def magic_quit(self, parameter_s=''):
2000 """Exit IPython, confirming if configured to do so (like %exit)"""
2028 """Exit IPython, confirming if configured to do so (like %exit)"""
2001
2029
2002 self.shell.exit()
2030 self.shell.exit()
2003
2031
2004 def magic_Exit(self, parameter_s=''):
2032 def magic_Exit(self, parameter_s=''):
2005 """Exit IPython without confirmation."""
2033 """Exit IPython without confirmation."""
2006
2034
2007 self.shell.exit_now = True
2035 self.shell.exit_now = True
2008
2036
2009 def magic_Quit(self, parameter_s=''):
2037 def magic_Quit(self, parameter_s=''):
2010 """Exit IPython without confirmation (like %Exit)."""
2038 """Exit IPython without confirmation (like %Exit)."""
2011
2039
2012 self.shell.exit_now = True
2040 self.shell.exit_now = True
2013
2041
2014 #......................................................................
2042 #......................................................................
2015 # Functions to implement unix shell-type things
2043 # Functions to implement unix shell-type things
2016
2044
2017 def magic_alias(self, parameter_s = ''):
2045 def magic_alias(self, parameter_s = ''):
2018 """Define an alias for a system command.
2046 """Define an alias for a system command.
2019
2047
2020 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2048 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2021
2049
2022 Then, typing 'alias_name params' will execute the system command 'cmd
2050 Then, typing 'alias_name params' will execute the system command 'cmd
2023 params' (from your underlying operating system).
2051 params' (from your underlying operating system).
2024
2052
2025 Aliases have lower precedence than magic functions and Python normal
2053 Aliases have lower precedence than magic functions and Python normal
2026 variables, so if 'foo' is both a Python variable and an alias, the
2054 variables, so if 'foo' is both a Python variable and an alias, the
2027 alias can not be executed until 'del foo' removes the Python variable.
2055 alias can not be executed until 'del foo' removes the Python variable.
2028
2056
2029 You can use the %l specifier in an alias definition to represent the
2057 You can use the %l specifier in an alias definition to represent the
2030 whole line when the alias is called. For example:
2058 whole line when the alias is called. For example:
2031
2059
2032 In [2]: alias all echo "Input in brackets: <%l>"\\
2060 In [2]: alias all echo "Input in brackets: <%l>"\\
2033 In [3]: all hello world\\
2061 In [3]: all hello world\\
2034 Input in brackets: <hello world>
2062 Input in brackets: <hello world>
2035
2063
2036 You can also define aliases with parameters using %s specifiers (one
2064 You can also define aliases with parameters using %s specifiers (one
2037 per parameter):
2065 per parameter):
2038
2066
2039 In [1]: alias parts echo first %s second %s\\
2067 In [1]: alias parts echo first %s second %s\\
2040 In [2]: %parts A B\\
2068 In [2]: %parts A B\\
2041 first A second B\\
2069 first A second B\\
2042 In [3]: %parts A\\
2070 In [3]: %parts A\\
2043 Incorrect number of arguments: 2 expected.\\
2071 Incorrect number of arguments: 2 expected.\\
2044 parts is an alias to: 'echo first %s second %s'
2072 parts is an alias to: 'echo first %s second %s'
2045
2073
2046 Note that %l and %s are mutually exclusive. You can only use one or
2074 Note that %l and %s are mutually exclusive. You can only use one or
2047 the other in your aliases.
2075 the other in your aliases.
2048
2076
2049 Aliases expand Python variables just like system calls using ! or !!
2077 Aliases expand Python variables just like system calls using ! or !!
2050 do: all expressions prefixed with '$' get expanded. For details of
2078 do: all expressions prefixed with '$' get expanded. For details of
2051 the semantic rules, see PEP-215:
2079 the semantic rules, see PEP-215:
2052 http://www.python.org/peps/pep-0215.html. This is the library used by
2080 http://www.python.org/peps/pep-0215.html. This is the library used by
2053 IPython for variable expansion. If you want to access a true shell
2081 IPython for variable expansion. If you want to access a true shell
2054 variable, an extra $ is necessary to prevent its expansion by IPython:
2082 variable, an extra $ is necessary to prevent its expansion by IPython:
2055
2083
2056 In [6]: alias show echo\\
2084 In [6]: alias show echo\\
2057 In [7]: PATH='A Python string'\\
2085 In [7]: PATH='A Python string'\\
2058 In [8]: show $PATH\\
2086 In [8]: show $PATH\\
2059 A Python string\\
2087 A Python string\\
2060 In [9]: show $$PATH\\
2088 In [9]: show $$PATH\\
2061 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2089 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2062
2090
2063 You can use the alias facility to acess all of $PATH. See the %rehash
2091 You can use the alias facility to acess all of $PATH. See the %rehash
2064 and %rehashx functions, which automatically create aliases for the
2092 and %rehashx functions, which automatically create aliases for the
2065 contents of your $PATH.
2093 contents of your $PATH.
2066
2094
2067 If called with no parameters, %alias prints the current alias table."""
2095 If called with no parameters, %alias prints the current alias table."""
2068
2096
2069 par = parameter_s.strip()
2097 par = parameter_s.strip()
2070 if not par:
2098 if not par:
2071 if self.shell.rc.automagic:
2099 if self.shell.rc.automagic:
2072 prechar = ''
2100 prechar = ''
2073 else:
2101 else:
2074 prechar = self.shell.ESC_MAGIC
2102 prechar = self.shell.ESC_MAGIC
2075 print 'Alias\t\tSystem Command\n'+'-'*30
2103 print 'Alias\t\tSystem Command\n'+'-'*30
2076 atab = self.shell.alias_table
2104 atab = self.shell.alias_table
2077 aliases = atab.keys()
2105 aliases = atab.keys()
2078 aliases.sort()
2106 aliases.sort()
2079 for alias in aliases:
2107 for alias in aliases:
2080 print prechar+alias+'\t\t'+atab[alias][1]
2108 print prechar+alias+'\t\t'+atab[alias][1]
2081 print '-'*30+'\nTotal number of aliases:',len(aliases)
2109 print '-'*30+'\nTotal number of aliases:',len(aliases)
2082 return
2110 return
2083 try:
2111 try:
2084 alias,cmd = par.split(None,1)
2112 alias,cmd = par.split(None,1)
2085 except:
2113 except:
2086 print OInspect.getdoc(self.magic_alias)
2114 print OInspect.getdoc(self.magic_alias)
2087 else:
2115 else:
2088 nargs = cmd.count('%s')
2116 nargs = cmd.count('%s')
2089 if nargs>0 and cmd.find('%l')>=0:
2117 if nargs>0 and cmd.find('%l')>=0:
2090 error('The %s and %l specifiers are mutually exclusive '
2118 error('The %s and %l specifiers are mutually exclusive '
2091 'in alias definitions.')
2119 'in alias definitions.')
2092 else: # all looks OK
2120 else: # all looks OK
2093 self.shell.alias_table[alias] = (nargs,cmd)
2121 self.shell.alias_table[alias] = (nargs,cmd)
2094 self.shell.alias_table_validate(verbose=1)
2122 self.shell.alias_table_validate(verbose=1)
2095 # end magic_alias
2123 # end magic_alias
2096
2124
2097 def magic_unalias(self, parameter_s = ''):
2125 def magic_unalias(self, parameter_s = ''):
2098 """Remove an alias"""
2126 """Remove an alias"""
2099
2127
2100 aname = parameter_s.strip()
2128 aname = parameter_s.strip()
2101 if aname in self.shell.alias_table:
2129 if aname in self.shell.alias_table:
2102 del self.shell.alias_table[aname]
2130 del self.shell.alias_table[aname]
2103
2131
2104 def magic_rehash(self, parameter_s = ''):
2132 def magic_rehash(self, parameter_s = ''):
2105 """Update the alias table with all entries in $PATH.
2133 """Update the alias table with all entries in $PATH.
2106
2134
2107 This version does no checks on execute permissions or whether the
2135 This version does no checks on execute permissions or whether the
2108 contents of $PATH are truly files (instead of directories or something
2136 contents of $PATH are truly files (instead of directories or something
2109 else). For such a safer (but slower) version, use %rehashx."""
2137 else). For such a safer (but slower) version, use %rehashx."""
2110
2138
2111 # This function (and rehashx) manipulate the alias_table directly
2139 # This function (and rehashx) manipulate the alias_table directly
2112 # rather than calling magic_alias, for speed reasons. A rehash on a
2140 # rather than calling magic_alias, for speed reasons. A rehash on a
2113 # typical Linux box involves several thousand entries, so efficiency
2141 # typical Linux box involves several thousand entries, so efficiency
2114 # here is a top concern.
2142 # here is a top concern.
2115
2143
2116 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2144 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2117 alias_table = self.shell.alias_table
2145 alias_table = self.shell.alias_table
2118 for pdir in path:
2146 for pdir in path:
2119 for ff in os.listdir(pdir):
2147 for ff in os.listdir(pdir):
2120 # each entry in the alias table must be (N,name), where
2148 # each entry in the alias table must be (N,name), where
2121 # N is the number of positional arguments of the alias.
2149 # N is the number of positional arguments of the alias.
2122 alias_table[ff] = (0,ff)
2150 alias_table[ff] = (0,ff)
2123 # Make sure the alias table doesn't contain keywords or builtins
2151 # Make sure the alias table doesn't contain keywords or builtins
2124 self.shell.alias_table_validate()
2152 self.shell.alias_table_validate()
2125 # Call again init_auto_alias() so we get 'rm -i' and other modified
2153 # Call again init_auto_alias() so we get 'rm -i' and other modified
2126 # aliases since %rehash will probably clobber them
2154 # aliases since %rehash will probably clobber them
2127 self.shell.init_auto_alias()
2155 self.shell.init_auto_alias()
2128
2156
2129 def magic_rehashx(self, parameter_s = ''):
2157 def magic_rehashx(self, parameter_s = ''):
2130 """Update the alias table with all executable files in $PATH.
2158 """Update the alias table with all executable files in $PATH.
2131
2159
2132 This version explicitly checks that every entry in $PATH is a file
2160 This version explicitly checks that every entry in $PATH is a file
2133 with execute access (os.X_OK), so it is much slower than %rehash.
2161 with execute access (os.X_OK), so it is much slower than %rehash.
2134
2162
2135 Under Windows, it checks executability as a match agains a
2163 Under Windows, it checks executability as a match agains a
2136 '|'-separated string of extensions, stored in the IPython config
2164 '|'-separated string of extensions, stored in the IPython config
2137 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2165 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2138
2166
2139 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2167 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2140 alias_table = self.shell.alias_table
2168 alias_table = self.shell.alias_table
2141
2169
2142 if os.name == 'posix':
2170 if os.name == 'posix':
2143 isexec = lambda fname:os.path.isfile(fname) and \
2171 isexec = lambda fname:os.path.isfile(fname) and \
2144 os.access(fname,os.X_OK)
2172 os.access(fname,os.X_OK)
2145 else:
2173 else:
2146
2174
2147 try:
2175 try:
2148 winext = os.environ['pathext'].replace(';','|').replace('.','')
2176 winext = os.environ['pathext'].replace(';','|').replace('.','')
2149 except KeyError:
2177 except KeyError:
2150 winext = 'exe|com|bat'
2178 winext = 'exe|com|bat'
2151
2179
2152 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2180 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2153 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2181 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2154 savedir = os.getcwd()
2182 savedir = os.getcwd()
2155 try:
2183 try:
2156 # write the whole loop for posix/Windows so we don't have an if in
2184 # write the whole loop for posix/Windows so we don't have an if in
2157 # the innermost part
2185 # the innermost part
2158 if os.name == 'posix':
2186 if os.name == 'posix':
2159 for pdir in path:
2187 for pdir in path:
2160 os.chdir(pdir)
2188 os.chdir(pdir)
2161 for ff in os.listdir(pdir):
2189 for ff in os.listdir(pdir):
2162 if isexec(ff):
2190 if isexec(ff):
2163 # each entry in the alias table must be (N,name),
2191 # each entry in the alias table must be (N,name),
2164 # where N is the number of positional arguments of the
2192 # where N is the number of positional arguments of the
2165 # alias.
2193 # alias.
2166 alias_table[ff] = (0,ff)
2194 alias_table[ff] = (0,ff)
2167 else:
2195 else:
2168 for pdir in path:
2196 for pdir in path:
2169 os.chdir(pdir)
2197 os.chdir(pdir)
2170 for ff in os.listdir(pdir):
2198 for ff in os.listdir(pdir):
2171 if isexec(ff):
2199 if isexec(ff):
2172 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2200 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2173 # Make sure the alias table doesn't contain keywords or builtins
2201 # Make sure the alias table doesn't contain keywords or builtins
2174 self.shell.alias_table_validate()
2202 self.shell.alias_table_validate()
2175 # Call again init_auto_alias() so we get 'rm -i' and other
2203 # Call again init_auto_alias() so we get 'rm -i' and other
2176 # modified aliases since %rehashx will probably clobber them
2204 # modified aliases since %rehashx will probably clobber them
2177 self.shell.init_auto_alias()
2205 self.shell.init_auto_alias()
2178 finally:
2206 finally:
2179 os.chdir(savedir)
2207 os.chdir(savedir)
2180
2208
2181 def magic_pwd(self, parameter_s = ''):
2209 def magic_pwd(self, parameter_s = ''):
2182 """Return the current working directory path."""
2210 """Return the current working directory path."""
2183 return os.getcwd()
2211 return os.getcwd()
2184
2212
2185 def magic_cd(self, parameter_s=''):
2213 def magic_cd(self, parameter_s=''):
2186 """Change the current working directory.
2214 """Change the current working directory.
2187
2215
2188 This command automatically maintains an internal list of directories
2216 This command automatically maintains an internal list of directories
2189 you visit during your IPython session, in the variable _dh. The
2217 you visit during your IPython session, in the variable _dh. The
2190 command %dhist shows this history nicely formatted.
2218 command %dhist shows this history nicely formatted.
2191
2219
2192 Usage:
2220 Usage:
2193
2221
2194 cd 'dir': changes to directory 'dir'.
2222 cd 'dir': changes to directory 'dir'.
2195
2223
2196 cd -: changes to the last visited directory.
2224 cd -: changes to the last visited directory.
2197
2225
2198 cd -<n>: changes to the n-th directory in the directory history.
2226 cd -<n>: changes to the n-th directory in the directory history.
2199
2227
2200 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2228 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2201 (note: cd <bookmark_name> is enough if there is no
2229 (note: cd <bookmark_name> is enough if there is no
2202 directory <bookmark_name>, but a bookmark with the name exists.)
2230 directory <bookmark_name>, but a bookmark with the name exists.)
2203
2231
2204 Options:
2232 Options:
2205
2233
2206 -q: quiet. Do not print the working directory after the cd command is
2234 -q: quiet. Do not print the working directory after the cd command is
2207 executed. By default IPython's cd command does print this directory,
2235 executed. By default IPython's cd command does print this directory,
2208 since the default prompts do not display path information.
2236 since the default prompts do not display path information.
2209
2237
2210 Note that !cd doesn't work for this purpose because the shell where
2238 Note that !cd doesn't work for this purpose because the shell where
2211 !command runs is immediately discarded after executing 'command'."""
2239 !command runs is immediately discarded after executing 'command'."""
2212
2240
2213 parameter_s = parameter_s.strip()
2241 parameter_s = parameter_s.strip()
2214 bkms = self.shell.persist.get("bookmarks",{})
2242 bkms = self.shell.persist.get("bookmarks",{})
2215
2243
2216 numcd = re.match(r'(-)(\d+)$',parameter_s)
2244 numcd = re.match(r'(-)(\d+)$',parameter_s)
2217 # jump in directory history by number
2245 # jump in directory history by number
2218 if numcd:
2246 if numcd:
2219 nn = int(numcd.group(2))
2247 nn = int(numcd.group(2))
2220 try:
2248 try:
2221 ps = self.shell.user_ns['_dh'][nn]
2249 ps = self.shell.user_ns['_dh'][nn]
2222 except IndexError:
2250 except IndexError:
2223 print 'The requested directory does not exist in history.'
2251 print 'The requested directory does not exist in history.'
2224 return
2252 return
2225 else:
2253 else:
2226 opts = {}
2254 opts = {}
2227 else:
2255 else:
2228 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2256 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2229 # jump to previous
2257 # jump to previous
2230 if ps == '-':
2258 if ps == '-':
2231 try:
2259 try:
2232 ps = self.shell.user_ns['_dh'][-2]
2260 ps = self.shell.user_ns['_dh'][-2]
2233 except IndexError:
2261 except IndexError:
2234 print 'No previous directory to change to.'
2262 print 'No previous directory to change to.'
2235 return
2263 return
2236 # jump to bookmark
2264 # jump to bookmark
2237 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2265 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2238 if bkms.has_key(ps):
2266 if bkms.has_key(ps):
2239 target = bkms[ps]
2267 target = bkms[ps]
2240 print '(bookmark:%s) -> %s' % (ps,target)
2268 print '(bookmark:%s) -> %s' % (ps,target)
2241 ps = target
2269 ps = target
2242 else:
2270 else:
2243 if bkms:
2271 if bkms:
2244 error("Bookmark '%s' not found. "
2272 error("Bookmark '%s' not found. "
2245 "Use '%bookmark -l' to see your bookmarks." % ps)
2273 "Use '%bookmark -l' to see your bookmarks." % ps)
2246 else:
2274 else:
2247 print "Bookmarks not set - use %bookmark <bookmarkname>"
2275 print "Bookmarks not set - use %bookmark <bookmarkname>"
2248 return
2276 return
2249
2277
2250 # at this point ps should point to the target dir
2278 # at this point ps should point to the target dir
2251 if ps:
2279 if ps:
2252 try:
2280 try:
2253 os.chdir(os.path.expanduser(ps))
2281 os.chdir(os.path.expanduser(ps))
2254 except OSError:
2282 except OSError:
2255 print sys.exc_info()[1]
2283 print sys.exc_info()[1]
2256 else:
2284 else:
2257 self.shell.user_ns['_dh'].append(os.getcwd())
2285 self.shell.user_ns['_dh'].append(os.getcwd())
2258 else:
2286 else:
2259 os.chdir(self.home_dir)
2287 os.chdir(self.home_dir)
2260 self.shell.user_ns['_dh'].append(os.getcwd())
2288 self.shell.user_ns['_dh'].append(os.getcwd())
2261 if not 'q' in opts:
2289 if not 'q' in opts:
2262 print self.shell.user_ns['_dh'][-1]
2290 print self.shell.user_ns['_dh'][-1]
2263
2291
2264 def magic_dhist(self, parameter_s=''):
2292 def magic_dhist(self, parameter_s=''):
2265 """Print your history of visited directories.
2293 """Print your history of visited directories.
2266
2294
2267 %dhist -> print full history\\
2295 %dhist -> print full history\\
2268 %dhist n -> print last n entries only\\
2296 %dhist n -> print last n entries only\\
2269 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2297 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2270
2298
2271 This history is automatically maintained by the %cd command, and
2299 This history is automatically maintained by the %cd command, and
2272 always available as the global list variable _dh. You can use %cd -<n>
2300 always available as the global list variable _dh. You can use %cd -<n>
2273 to go to directory number <n>."""
2301 to go to directory number <n>."""
2274
2302
2275 dh = self.shell.user_ns['_dh']
2303 dh = self.shell.user_ns['_dh']
2276 if parameter_s:
2304 if parameter_s:
2277 try:
2305 try:
2278 args = map(int,parameter_s.split())
2306 args = map(int,parameter_s.split())
2279 except:
2307 except:
2280 self.arg_err(Magic.magic_dhist)
2308 self.arg_err(Magic.magic_dhist)
2281 return
2309 return
2282 if len(args) == 1:
2310 if len(args) == 1:
2283 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2311 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2284 elif len(args) == 2:
2312 elif len(args) == 2:
2285 ini,fin = args
2313 ini,fin = args
2286 else:
2314 else:
2287 self.arg_err(Magic.magic_dhist)
2315 self.arg_err(Magic.magic_dhist)
2288 return
2316 return
2289 else:
2317 else:
2290 ini,fin = 0,len(dh)
2318 ini,fin = 0,len(dh)
2291 nlprint(dh,
2319 nlprint(dh,
2292 header = 'Directory history (kept in _dh)',
2320 header = 'Directory history (kept in _dh)',
2293 start=ini,stop=fin)
2321 start=ini,stop=fin)
2294
2322
2295 def magic_env(self, parameter_s=''):
2323 def magic_env(self, parameter_s=''):
2296 """List environment variables."""
2324 """List environment variables."""
2297
2325
2298 # environ is an instance of UserDict
2326 # environ is an instance of UserDict
2299 return os.environ.data
2327 return os.environ.data
2300
2328
2301 def magic_pushd(self, parameter_s=''):
2329 def magic_pushd(self, parameter_s=''):
2302 """Place the current dir on stack and change directory.
2330 """Place the current dir on stack and change directory.
2303
2331
2304 Usage:\\
2332 Usage:\\
2305 %pushd ['dirname']
2333 %pushd ['dirname']
2306
2334
2307 %pushd with no arguments does a %pushd to your home directory.
2335 %pushd with no arguments does a %pushd to your home directory.
2308 """
2336 """
2309 if parameter_s == '': parameter_s = '~'
2337 if parameter_s == '': parameter_s = '~'
2310 if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \
2338 if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \
2311 os.path.expanduser(self.dir_stack[0]):
2339 os.path.expanduser(self.dir_stack[0]):
2312 try:
2340 try:
2313 self.magic_cd(parameter_s)
2341 self.magic_cd(parameter_s)
2314 self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~'))
2342 self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~'))
2315 self.magic_dirs()
2343 self.magic_dirs()
2316 except:
2344 except:
2317 print 'Invalid directory'
2345 print 'Invalid directory'
2318 else:
2346 else:
2319 print 'You are already there!'
2347 print 'You are already there!'
2320
2348
2321 def magic_popd(self, parameter_s=''):
2349 def magic_popd(self, parameter_s=''):
2322 """Change to directory popped off the top of the stack.
2350 """Change to directory popped off the top of the stack.
2323 """
2351 """
2324 if len (self.dir_stack) > 1:
2352 if len (self.dir_stack) > 1:
2325 self.dir_stack.pop(0)
2353 self.dir_stack.pop(0)
2326 self.magic_cd(self.dir_stack[0])
2354 self.magic_cd(self.dir_stack[0])
2327 print self.dir_stack[0]
2355 print self.dir_stack[0]
2328 else:
2356 else:
2329 print "You can't remove the starting directory from the stack:",\
2357 print "You can't remove the starting directory from the stack:",\
2330 self.dir_stack
2358 self.dir_stack
2331
2359
2332 def magic_dirs(self, parameter_s=''):
2360 def magic_dirs(self, parameter_s=''):
2333 """Return the current directory stack."""
2361 """Return the current directory stack."""
2334
2362
2335 return self.dir_stack[:]
2363 return self.dir_stack[:]
2336
2364
2337 def magic_sc(self, parameter_s=''):
2365 def magic_sc(self, parameter_s=''):
2338 """Shell capture - execute a shell command and capture its output.
2366 """Shell capture - execute a shell command and capture its output.
2339
2367
2340 %sc [options] varname=command
2368 %sc [options] varname=command
2341
2369
2342 IPython will run the given command using commands.getoutput(), and
2370 IPython will run the given command using commands.getoutput(), and
2343 will then update the user's interactive namespace with a variable
2371 will then update the user's interactive namespace with a variable
2344 called varname, containing the value of the call. Your command can
2372 called varname, containing the value of the call. Your command can
2345 contain shell wildcards, pipes, etc.
2373 contain shell wildcards, pipes, etc.
2346
2374
2347 The '=' sign in the syntax is mandatory, and the variable name you
2375 The '=' sign in the syntax is mandatory, and the variable name you
2348 supply must follow Python's standard conventions for valid names.
2376 supply must follow Python's standard conventions for valid names.
2349
2377
2350 Options:
2378 Options:
2351
2379
2352 -l: list output. Split the output on newlines into a list before
2380 -l: list output. Split the output on newlines into a list before
2353 assigning it to the given variable. By default the output is stored
2381 assigning it to the given variable. By default the output is stored
2354 as a single string.
2382 as a single string.
2355
2383
2356 -v: verbose. Print the contents of the variable.
2384 -v: verbose. Print the contents of the variable.
2357
2385
2358 In most cases you should not need to split as a list, because the
2386 In most cases you should not need to split as a list, because the
2359 returned value is a special type of string which can automatically
2387 returned value is a special type of string which can automatically
2360 provide its contents either as a list (split on newlines) or as a
2388 provide its contents either as a list (split on newlines) or as a
2361 space-separated string. These are convenient, respectively, either
2389 space-separated string. These are convenient, respectively, either
2362 for sequential processing or to be passed to a shell command.
2390 for sequential processing or to be passed to a shell command.
2363
2391
2364 For example:
2392 For example:
2365
2393
2366 # Capture into variable a
2394 # Capture into variable a
2367 In [9]: sc a=ls *py
2395 In [9]: sc a=ls *py
2368
2396
2369 # a is a string with embedded newlines
2397 # a is a string with embedded newlines
2370 In [10]: a
2398 In [10]: a
2371 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2399 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2372
2400
2373 # which can be seen as a list:
2401 # which can be seen as a list:
2374 In [11]: a.l
2402 In [11]: a.l
2375 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2403 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2376
2404
2377 # or as a whitespace-separated string:
2405 # or as a whitespace-separated string:
2378 In [12]: a.s
2406 In [12]: a.s
2379 Out[12]: 'setup.py win32_manual_post_install.py'
2407 Out[12]: 'setup.py win32_manual_post_install.py'
2380
2408
2381 # a.s is useful to pass as a single command line:
2409 # a.s is useful to pass as a single command line:
2382 In [13]: !wc -l $a.s
2410 In [13]: !wc -l $a.s
2383 146 setup.py
2411 146 setup.py
2384 130 win32_manual_post_install.py
2412 130 win32_manual_post_install.py
2385 276 total
2413 276 total
2386
2414
2387 # while the list form is useful to loop over:
2415 # while the list form is useful to loop over:
2388 In [14]: for f in a.l:
2416 In [14]: for f in a.l:
2389 ....: !wc -l $f
2417 ....: !wc -l $f
2390 ....:
2418 ....:
2391 146 setup.py
2419 146 setup.py
2392 130 win32_manual_post_install.py
2420 130 win32_manual_post_install.py
2393
2421
2394 Similiarly, the lists returned by the -l option are also special, in
2422 Similiarly, the lists returned by the -l option are also special, in
2395 the sense that you can equally invoke the .s attribute on them to
2423 the sense that you can equally invoke the .s attribute on them to
2396 automatically get a whitespace-separated string from their contents:
2424 automatically get a whitespace-separated string from their contents:
2397
2425
2398 In [1]: sc -l b=ls *py
2426 In [1]: sc -l b=ls *py
2399
2427
2400 In [2]: b
2428 In [2]: b
2401 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2429 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2402
2430
2403 In [3]: b.s
2431 In [3]: b.s
2404 Out[3]: 'setup.py win32_manual_post_install.py'
2432 Out[3]: 'setup.py win32_manual_post_install.py'
2405
2433
2406 In summary, both the lists and strings used for ouptut capture have
2434 In summary, both the lists and strings used for ouptut capture have
2407 the following special attributes:
2435 the following special attributes:
2408
2436
2409 .l (or .list) : value as list.
2437 .l (or .list) : value as list.
2410 .n (or .nlstr): value as newline-separated string.
2438 .n (or .nlstr): value as newline-separated string.
2411 .s (or .spstr): value as space-separated string.
2439 .s (or .spstr): value as space-separated string.
2412 """
2440 """
2413
2441
2414 opts,args = self.parse_options(parameter_s,'lv')
2442 opts,args = self.parse_options(parameter_s,'lv')
2415 # Try to get a variable name and command to run
2443 # Try to get a variable name and command to run
2416 try:
2444 try:
2417 # the variable name must be obtained from the parse_options
2445 # the variable name must be obtained from the parse_options
2418 # output, which uses shlex.split to strip options out.
2446 # output, which uses shlex.split to strip options out.
2419 var,_ = args.split('=',1)
2447 var,_ = args.split('=',1)
2420 var = var.strip()
2448 var = var.strip()
2421 # But the the command has to be extracted from the original input
2449 # But the the command has to be extracted from the original input
2422 # parameter_s, not on what parse_options returns, to avoid the
2450 # parameter_s, not on what parse_options returns, to avoid the
2423 # quote stripping which shlex.split performs on it.
2451 # quote stripping which shlex.split performs on it.
2424 _,cmd = parameter_s.split('=',1)
2452 _,cmd = parameter_s.split('=',1)
2425 except ValueError:
2453 except ValueError:
2426 var,cmd = '',''
2454 var,cmd = '',''
2427 if not var:
2455 if not var:
2428 error('you must specify a variable to assign the command to.')
2456 error('you must specify a variable to assign the command to.')
2429 return
2457 return
2430 # If all looks ok, proceed
2458 # If all looks ok, proceed
2431 out,err = self.shell.getoutputerror(cmd)
2459 out,err = self.shell.getoutputerror(cmd)
2432 if err:
2460 if err:
2433 print >> Term.cerr,err
2461 print >> Term.cerr,err
2434 if opts.has_key('l'):
2462 if opts.has_key('l'):
2435 out = SList(out.split('\n'))
2463 out = SList(out.split('\n'))
2436 else:
2464 else:
2437 out = LSString(out)
2465 out = LSString(out)
2438 if opts.has_key('v'):
2466 if opts.has_key('v'):
2439 print '%s ==\n%s' % (var,pformat(out))
2467 print '%s ==\n%s' % (var,pformat(out))
2440 self.shell.user_ns.update({var:out})
2468 self.shell.user_ns.update({var:out})
2441
2469
2442 def magic_sx(self, parameter_s=''):
2470 def magic_sx(self, parameter_s=''):
2443 """Shell execute - run a shell command and capture its output.
2471 """Shell execute - run a shell command and capture its output.
2444
2472
2445 %sx command
2473 %sx command
2446
2474
2447 IPython will run the given command using commands.getoutput(), and
2475 IPython will run the given command using commands.getoutput(), and
2448 return the result formatted as a list (split on '\\n'). Since the
2476 return the result formatted as a list (split on '\\n'). Since the
2449 output is _returned_, it will be stored in ipython's regular output
2477 output is _returned_, it will be stored in ipython's regular output
2450 cache Out[N] and in the '_N' automatic variables.
2478 cache Out[N] and in the '_N' automatic variables.
2451
2479
2452 Notes:
2480 Notes:
2453
2481
2454 1) If an input line begins with '!!', then %sx is automatically
2482 1) If an input line begins with '!!', then %sx is automatically
2455 invoked. That is, while:
2483 invoked. That is, while:
2456 !ls
2484 !ls
2457 causes ipython to simply issue system('ls'), typing
2485 causes ipython to simply issue system('ls'), typing
2458 !!ls
2486 !!ls
2459 is a shorthand equivalent to:
2487 is a shorthand equivalent to:
2460 %sx ls
2488 %sx ls
2461
2489
2462 2) %sx differs from %sc in that %sx automatically splits into a list,
2490 2) %sx differs from %sc in that %sx automatically splits into a list,
2463 like '%sc -l'. The reason for this is to make it as easy as possible
2491 like '%sc -l'. The reason for this is to make it as easy as possible
2464 to process line-oriented shell output via further python commands.
2492 to process line-oriented shell output via further python commands.
2465 %sc is meant to provide much finer control, but requires more
2493 %sc is meant to provide much finer control, but requires more
2466 typing.
2494 typing.
2467
2495
2468 3) Just like %sc -l, this is a list with special attributes:
2496 3) Just like %sc -l, this is a list with special attributes:
2469
2497
2470 .l (or .list) : value as list.
2498 .l (or .list) : value as list.
2471 .n (or .nlstr): value as newline-separated string.
2499 .n (or .nlstr): value as newline-separated string.
2472 .s (or .spstr): value as whitespace-separated string.
2500 .s (or .spstr): value as whitespace-separated string.
2473
2501
2474 This is very useful when trying to use such lists as arguments to
2502 This is very useful when trying to use such lists as arguments to
2475 system commands."""
2503 system commands."""
2476
2504
2477 if parameter_s:
2505 if parameter_s:
2478 out,err = self.shell.getoutputerror(parameter_s)
2506 out,err = self.shell.getoutputerror(parameter_s)
2479 if err:
2507 if err:
2480 print >> Term.cerr,err
2508 print >> Term.cerr,err
2481 return SList(out.split('\n'))
2509 return SList(out.split('\n'))
2482
2510
2483 def magic_bg(self, parameter_s=''):
2511 def magic_bg(self, parameter_s=''):
2484 """Run a job in the background, in a separate thread.
2512 """Run a job in the background, in a separate thread.
2485
2513
2486 For example,
2514 For example,
2487
2515
2488 %bg myfunc(x,y,z=1)
2516 %bg myfunc(x,y,z=1)
2489
2517
2490 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2518 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2491 execution starts, a message will be printed indicating the job
2519 execution starts, a message will be printed indicating the job
2492 number. If your job number is 5, you can use
2520 number. If your job number is 5, you can use
2493
2521
2494 myvar = jobs.result(5) or myvar = jobs[5].result
2522 myvar = jobs.result(5) or myvar = jobs[5].result
2495
2523
2496 to assign this result to variable 'myvar'.
2524 to assign this result to variable 'myvar'.
2497
2525
2498 IPython has a job manager, accessible via the 'jobs' object. You can
2526 IPython has a job manager, accessible via the 'jobs' object. You can
2499 type jobs? to get more information about it, and use jobs.<TAB> to see
2527 type jobs? to get more information about it, and use jobs.<TAB> to see
2500 its attributes. All attributes not starting with an underscore are
2528 its attributes. All attributes not starting with an underscore are
2501 meant for public use.
2529 meant for public use.
2502
2530
2503 In particular, look at the jobs.new() method, which is used to create
2531 In particular, look at the jobs.new() method, which is used to create
2504 new jobs. This magic %bg function is just a convenience wrapper
2532 new jobs. This magic %bg function is just a convenience wrapper
2505 around jobs.new(), for expression-based jobs. If you want to create a
2533 around jobs.new(), for expression-based jobs. If you want to create a
2506 new job with an explicit function object and arguments, you must call
2534 new job with an explicit function object and arguments, you must call
2507 jobs.new() directly.
2535 jobs.new() directly.
2508
2536
2509 The jobs.new docstring also describes in detail several important
2537 The jobs.new docstring also describes in detail several important
2510 caveats associated with a thread-based model for background job
2538 caveats associated with a thread-based model for background job
2511 execution. Type jobs.new? for details.
2539 execution. Type jobs.new? for details.
2512
2540
2513 You can check the status of all jobs with jobs.status().
2541 You can check the status of all jobs with jobs.status().
2514
2542
2515 The jobs variable is set by IPython into the Python builtin namespace.
2543 The jobs variable is set by IPython into the Python builtin namespace.
2516 If you ever declare a variable named 'jobs', you will shadow this
2544 If you ever declare a variable named 'jobs', you will shadow this
2517 name. You can either delete your global jobs variable to regain
2545 name. You can either delete your global jobs variable to regain
2518 access to the job manager, or make a new name and assign it manually
2546 access to the job manager, or make a new name and assign it manually
2519 to the manager (stored in IPython's namespace). For example, to
2547 to the manager (stored in IPython's namespace). For example, to
2520 assign the job manager to the Jobs name, use:
2548 assign the job manager to the Jobs name, use:
2521
2549
2522 Jobs = __builtins__.jobs"""
2550 Jobs = __builtins__.jobs"""
2523
2551
2524 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2552 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2525
2553
2526 def magic_bookmark(self, parameter_s=''):
2554 def magic_bookmark(self, parameter_s=''):
2527 """Manage IPython's bookmark system.
2555 """Manage IPython's bookmark system.
2528
2556
2529 %bookmark <name> - set bookmark to current dir
2557 %bookmark <name> - set bookmark to current dir
2530 %bookmark <name> <dir> - set bookmark to <dir>
2558 %bookmark <name> <dir> - set bookmark to <dir>
2531 %bookmark -l - list all bookmarks
2559 %bookmark -l - list all bookmarks
2532 %bookmark -d <name> - remove bookmark
2560 %bookmark -d <name> - remove bookmark
2533 %bookmark -r - remove all bookmarks
2561 %bookmark -r - remove all bookmarks
2534
2562
2535 You can later on access a bookmarked folder with:
2563 You can later on access a bookmarked folder with:
2536 %cd -b <name>
2564 %cd -b <name>
2537 or simply '%cd <name>' if there is no directory called <name> AND
2565 or simply '%cd <name>' if there is no directory called <name> AND
2538 there is such a bookmark defined.
2566 there is such a bookmark defined.
2539
2567
2540 Your bookmarks persist through IPython sessions, but they are
2568 Your bookmarks persist through IPython sessions, but they are
2541 associated with each profile."""
2569 associated with each profile."""
2542
2570
2543 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2571 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2544 if len(args) > 2:
2572 if len(args) > 2:
2545 error('You can only give at most two arguments')
2573 error('You can only give at most two arguments')
2546 return
2574 return
2547
2575
2548 bkms = self.shell.persist.get('bookmarks',{})
2576 bkms = self.shell.persist.get('bookmarks',{})
2549
2577
2550 if opts.has_key('d'):
2578 if opts.has_key('d'):
2551 try:
2579 try:
2552 todel = args[0]
2580 todel = args[0]
2553 except IndexError:
2581 except IndexError:
2554 error('You must provide a bookmark to delete')
2582 error('You must provide a bookmark to delete')
2555 else:
2583 else:
2556 try:
2584 try:
2557 del bkms[todel]
2585 del bkms[todel]
2558 except:
2586 except:
2559 error("Can't delete bookmark '%s'" % todel)
2587 error("Can't delete bookmark '%s'" % todel)
2560 elif opts.has_key('r'):
2588 elif opts.has_key('r'):
2561 bkms = {}
2589 bkms = {}
2562 elif opts.has_key('l'):
2590 elif opts.has_key('l'):
2563 bks = bkms.keys()
2591 bks = bkms.keys()
2564 bks.sort()
2592 bks.sort()
2565 if bks:
2593 if bks:
2566 size = max(map(len,bks))
2594 size = max(map(len,bks))
2567 else:
2595 else:
2568 size = 0
2596 size = 0
2569 fmt = '%-'+str(size)+'s -> %s'
2597 fmt = '%-'+str(size)+'s -> %s'
2570 print 'Current bookmarks:'
2598 print 'Current bookmarks:'
2571 for bk in bks:
2599 for bk in bks:
2572 print fmt % (bk,bkms[bk])
2600 print fmt % (bk,bkms[bk])
2573 else:
2601 else:
2574 if not args:
2602 if not args:
2575 error("You must specify the bookmark name")
2603 error("You must specify the bookmark name")
2576 elif len(args)==1:
2604 elif len(args)==1:
2577 bkms[args[0]] = os.getcwd()
2605 bkms[args[0]] = os.getcwd()
2578 elif len(args)==2:
2606 elif len(args)==2:
2579 bkms[args[0]] = args[1]
2607 bkms[args[0]] = args[1]
2580 self.persist['bookmarks'] = bkms
2608 self.persist['bookmarks'] = bkms
2581
2609
2582 def magic_pycat(self, parameter_s=''):
2610 def magic_pycat(self, parameter_s=''):
2583 """Show a syntax-highlighted file through a pager.
2611 """Show a syntax-highlighted file through a pager.
2584
2612
2585 This magic is similar to the cat utility, but it will assume the file
2613 This magic is similar to the cat utility, but it will assume the file
2586 to be Python source and will show it with syntax highlighting. """
2614 to be Python source and will show it with syntax highlighting. """
2587
2615
2588 filename = get_py_filename(parameter_s)
2616 filename = get_py_filename(parameter_s)
2589 page(self.shell.colorize(file_read(filename)),
2617 page(self.shell.colorize(file_read(filename)),
2590 screen_lines=self.shell.rc.screen_length)
2618 screen_lines=self.shell.rc.screen_length)
2591
2619
2592 # end Magic
2620 # end Magic
@@ -1,913 +1,917 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """IPython Shell classes.
2 """IPython Shell classes.
3
3
4 All the matplotlib support code was co-developed with John Hunter,
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
5 matplotlib's author.
6
6
7 $Id: Shell.py 964 2005-12-28 21:03:01Z fperez $"""
7 $Id: Shell.py 965 2005-12-28 23:23:09Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 from IPython import Release
16 from IPython import Release
17 __author__ = '%s <%s>' % Release.authors['Fernando']
17 __author__ = '%s <%s>' % Release.authors['Fernando']
18 __license__ = Release.license
18 __license__ = Release.license
19
19
20 # Code begins
20 # Code begins
21 import __main__
21 import __main__
22 import __builtin__
22 import __builtin__
23 import os
23 import os
24 import sys
24 import sys
25 import signal
25 import signal
26 import threading
26 import threading
27
27
28 import IPython
28 import IPython
29 from IPython import ultraTB
29 from IPython import ultraTB
30 from IPython.genutils import Term,warn,error,flag_calls
30 from IPython.genutils import Term,warn,error,flag_calls
31 from IPython.iplib import InteractiveShell
31 from IPython.iplib import InteractiveShell
32 from IPython.ipmaker import make_IPython
32 from IPython.ipmaker import make_IPython
33 from IPython.Magic import Magic
33 from IPython.Magic import Magic
34 from IPython.Struct import Struct
34 from IPython.Struct import Struct
35
35
36 # global flag to pass around information about Ctrl-C without exceptions
36 # global flag to pass around information about Ctrl-C without exceptions
37 KBINT = False
37 KBINT = False
38
38
39 # global flag to turn on/off Tk support.
39 # global flag to turn on/off Tk support.
40 USE_TK = False
40 USE_TK = False
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # This class is trivial now, but I want to have it in to publish a clean
43 # This class is trivial now, but I want to have it in to publish a clean
44 # interface. Later when the internals are reorganized, code that uses this
44 # interface. Later when the internals are reorganized, code that uses this
45 # shouldn't have to change.
45 # shouldn't have to change.
46
46
47 class IPShell:
47 class IPShell:
48 """Create an IPython instance."""
48 """Create an IPython instance."""
49
49
50 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
50 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
51 debug=1,shell_class=InteractiveShell):
51 debug=1,shell_class=InteractiveShell):
52 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
52 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
53 debug=debug,shell_class=shell_class)
53 debug=debug,shell_class=shell_class)
54
54
55 def mainloop(self,sys_exit=0,banner=None):
55 def mainloop(self,sys_exit=0,banner=None):
56 self.IP.mainloop(banner)
56 self.IP.mainloop(banner)
57 if sys_exit:
57 if sys_exit:
58 sys.exit()
58 sys.exit()
59
59
60 #-----------------------------------------------------------------------------
60 #-----------------------------------------------------------------------------
61 class IPShellEmbed:
61 class IPShellEmbed:
62 """Allow embedding an IPython shell into a running program.
62 """Allow embedding an IPython shell into a running program.
63
63
64 Instances of this class are callable, with the __call__ method being an
64 Instances of this class are callable, with the __call__ method being an
65 alias to the embed() method of an InteractiveShell instance.
65 alias to the embed() method of an InteractiveShell instance.
66
66
67 Usage (see also the example-embed.py file for a running example):
67 Usage (see also the example-embed.py file for a running example):
68
68
69 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
69 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
70
70
71 - argv: list containing valid command-line options for IPython, as they
71 - argv: list containing valid command-line options for IPython, as they
72 would appear in sys.argv[1:].
72 would appear in sys.argv[1:].
73
73
74 For example, the following command-line options:
74 For example, the following command-line options:
75
75
76 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
76 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
77
77
78 would be passed in the argv list as:
78 would be passed in the argv list as:
79
79
80 ['-prompt_in1','Input <\\#>','-colors','LightBG']
80 ['-prompt_in1','Input <\\#>','-colors','LightBG']
81
81
82 - banner: string which gets printed every time the interpreter starts.
82 - banner: string which gets printed every time the interpreter starts.
83
83
84 - exit_msg: string which gets printed every time the interpreter exits.
84 - exit_msg: string which gets printed every time the interpreter exits.
85
85
86 - rc_override: a dict or Struct of configuration options such as those
86 - rc_override: a dict or Struct of configuration options such as those
87 used by IPython. These options are read from your ~/.ipython/ipythonrc
87 used by IPython. These options are read from your ~/.ipython/ipythonrc
88 file when the Shell object is created. Passing an explicit rc_override
88 file when the Shell object is created. Passing an explicit rc_override
89 dict with any options you want allows you to override those values at
89 dict with any options you want allows you to override those values at
90 creation time without having to modify the file. This way you can create
90 creation time without having to modify the file. This way you can create
91 embeddable instances configured in any way you want without editing any
91 embeddable instances configured in any way you want without editing any
92 global files (thus keeping your interactive IPython configuration
92 global files (thus keeping your interactive IPython configuration
93 unchanged).
93 unchanged).
94
94
95 Then the ipshell instance can be called anywhere inside your code:
95 Then the ipshell instance can be called anywhere inside your code:
96
96
97 ipshell(header='') -> Opens up an IPython shell.
97 ipshell(header='') -> Opens up an IPython shell.
98
98
99 - header: string printed by the IPython shell upon startup. This can let
99 - header: string printed by the IPython shell upon startup. This can let
100 you know where in your code you are when dropping into the shell. Note
100 you know where in your code you are when dropping into the shell. Note
101 that 'banner' gets prepended to all calls, so header is used for
101 that 'banner' gets prepended to all calls, so header is used for
102 location-specific information.
102 location-specific information.
103
103
104 For more details, see the __call__ method below.
104 For more details, see the __call__ method below.
105
105
106 When the IPython shell is exited with Ctrl-D, normal program execution
106 When the IPython shell is exited with Ctrl-D, normal program execution
107 resumes.
107 resumes.
108
108
109 This functionality was inspired by a posting on comp.lang.python by cmkl
109 This functionality was inspired by a posting on comp.lang.python by cmkl
110 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
110 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
111 by the IDL stop/continue commands."""
111 by the IDL stop/continue commands."""
112
112
113 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
113 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
114 """Note that argv here is a string, NOT a list."""
114 """Note that argv here is a string, NOT a list."""
115 self.set_banner(banner)
115 self.set_banner(banner)
116 self.set_exit_msg(exit_msg)
116 self.set_exit_msg(exit_msg)
117 self.set_dummy_mode(0)
117 self.set_dummy_mode(0)
118
118
119 # sys.displayhook is a global, we need to save the user's original
119 # sys.displayhook is a global, we need to save the user's original
120 # Don't rely on __displayhook__, as the user may have changed that.
120 # Don't rely on __displayhook__, as the user may have changed that.
121 self.sys_displayhook_ori = sys.displayhook
121 self.sys_displayhook_ori = sys.displayhook
122
122
123 # save readline completer status
123 # save readline completer status
124 try:
124 try:
125 #print 'Save completer',sys.ipcompleter # dbg
125 #print 'Save completer',sys.ipcompleter # dbg
126 self.sys_ipcompleter_ori = sys.ipcompleter
126 self.sys_ipcompleter_ori = sys.ipcompleter
127 except:
127 except:
128 pass # not nested with IPython
128 pass # not nested with IPython
129
129
130 # FIXME. Passing user_ns breaks namespace handling.
130 # FIXME. Passing user_ns breaks namespace handling.
131 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
131 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
132 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
132 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
133
133
134 # copy our own displayhook also
134 # copy our own displayhook also
135 self.sys_displayhook_embed = sys.displayhook
135 self.sys_displayhook_embed = sys.displayhook
136 # and leave the system's display hook clean
136 # and leave the system's display hook clean
137 sys.displayhook = self.sys_displayhook_ori
137 sys.displayhook = self.sys_displayhook_ori
138 # don't use the ipython crash handler so that user exceptions aren't
138 # don't use the ipython crash handler so that user exceptions aren't
139 # trapped
139 # trapped
140 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
140 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
141 mode = self.IP.rc.xmode,
141 mode = self.IP.rc.xmode,
142 call_pdb = self.IP.rc.pdb)
142 call_pdb = self.IP.rc.pdb)
143 self.restore_system_completer()
143 self.restore_system_completer()
144
144
145 def restore_system_completer(self):
145 def restore_system_completer(self):
146 """Restores the readline completer which was in place.
146 """Restores the readline completer which was in place.
147
147
148 This allows embedded IPython within IPython not to disrupt the
148 This allows embedded IPython within IPython not to disrupt the
149 parent's completion.
149 parent's completion.
150 """
150 """
151
151
152 try:
152 try:
153 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
153 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
154 sys.ipcompleter = self.sys_ipcompleter_ori
154 sys.ipcompleter = self.sys_ipcompleter_ori
155 except:
155 except:
156 pass
156 pass
157
157
158 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
158 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
159 """Activate the interactive interpreter.
159 """Activate the interactive interpreter.
160
160
161 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
161 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
162 the interpreter shell with the given local and global namespaces, and
162 the interpreter shell with the given local and global namespaces, and
163 optionally print a header string at startup.
163 optionally print a header string at startup.
164
164
165 The shell can be globally activated/deactivated using the
165 The shell can be globally activated/deactivated using the
166 set/get_dummy_mode methods. This allows you to turn off a shell used
166 set/get_dummy_mode methods. This allows you to turn off a shell used
167 for debugging globally.
167 for debugging globally.
168
168
169 However, *each* time you call the shell you can override the current
169 However, *each* time you call the shell you can override the current
170 state of dummy_mode with the optional keyword parameter 'dummy'. For
170 state of dummy_mode with the optional keyword parameter 'dummy'. For
171 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
171 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
172 can still have a specific call work by making it as IPShell(dummy=0).
172 can still have a specific call work by making it as IPShell(dummy=0).
173
173
174 The optional keyword parameter dummy controls whether the call
174 The optional keyword parameter dummy controls whether the call
175 actually does anything. """
175 actually does anything. """
176
176
177 # Allow the dummy parameter to override the global __dummy_mode
177 # Allow the dummy parameter to override the global __dummy_mode
178 if dummy or (dummy != 0 and self.__dummy_mode):
178 if dummy or (dummy != 0 and self.__dummy_mode):
179 return
179 return
180
180
181 # Set global subsystems (display,completions) to our values
181 # Set global subsystems (display,completions) to our values
182 sys.displayhook = self.sys_displayhook_embed
182 sys.displayhook = self.sys_displayhook_embed
183 if self.IP.has_readline:
183 if self.IP.has_readline:
184 self.IP.readline.set_completer(self.IP.Completer.complete)
184 self.IP.readline.set_completer(self.IP.Completer.complete)
185
185
186 if self.banner and header:
186 if self.banner and header:
187 format = '%s\n%s\n'
187 format = '%s\n%s\n'
188 else:
188 else:
189 format = '%s%s\n'
189 format = '%s%s\n'
190 banner = format % (self.banner,header)
190 banner = format % (self.banner,header)
191
191
192 # Call the embedding code with a stack depth of 1 so it can skip over
192 # Call the embedding code with a stack depth of 1 so it can skip over
193 # our call and get the original caller's namespaces.
193 # our call and get the original caller's namespaces.
194 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
194 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
195
195
196 if self.exit_msg:
196 if self.exit_msg:
197 print self.exit_msg
197 print self.exit_msg
198
198
199 # Restore global systems (display, completion)
199 # Restore global systems (display, completion)
200 sys.displayhook = self.sys_displayhook_ori
200 sys.displayhook = self.sys_displayhook_ori
201 self.restore_system_completer()
201 self.restore_system_completer()
202
202
203 def set_dummy_mode(self,dummy):
203 def set_dummy_mode(self,dummy):
204 """Sets the embeddable shell's dummy mode parameter.
204 """Sets the embeddable shell's dummy mode parameter.
205
205
206 set_dummy_mode(dummy): dummy = 0 or 1.
206 set_dummy_mode(dummy): dummy = 0 or 1.
207
207
208 This parameter is persistent and makes calls to the embeddable shell
208 This parameter is persistent and makes calls to the embeddable shell
209 silently return without performing any action. This allows you to
209 silently return without performing any action. This allows you to
210 globally activate or deactivate a shell you're using with a single call.
210 globally activate or deactivate a shell you're using with a single call.
211
211
212 If you need to manually"""
212 If you need to manually"""
213
213
214 if dummy not in [0,1,False,True]:
214 if dummy not in [0,1,False,True]:
215 raise ValueError,'dummy parameter must be boolean'
215 raise ValueError,'dummy parameter must be boolean'
216 self.__dummy_mode = dummy
216 self.__dummy_mode = dummy
217
217
218 def get_dummy_mode(self):
218 def get_dummy_mode(self):
219 """Return the current value of the dummy mode parameter.
219 """Return the current value of the dummy mode parameter.
220 """
220 """
221 return self.__dummy_mode
221 return self.__dummy_mode
222
222
223 def set_banner(self,banner):
223 def set_banner(self,banner):
224 """Sets the global banner.
224 """Sets the global banner.
225
225
226 This banner gets prepended to every header printed when the shell
226 This banner gets prepended to every header printed when the shell
227 instance is called."""
227 instance is called."""
228
228
229 self.banner = banner
229 self.banner = banner
230
230
231 def set_exit_msg(self,exit_msg):
231 def set_exit_msg(self,exit_msg):
232 """Sets the global exit_msg.
232 """Sets the global exit_msg.
233
233
234 This exit message gets printed upon exiting every time the embedded
234 This exit message gets printed upon exiting every time the embedded
235 shell is called. It is None by default. """
235 shell is called. It is None by default. """
236
236
237 self.exit_msg = exit_msg
237 self.exit_msg = exit_msg
238
238
239 #-----------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------
240 def sigint_handler (signum,stack_frame):
240 def sigint_handler (signum,stack_frame):
241 """Sigint handler for threaded apps.
241 """Sigint handler for threaded apps.
242
242
243 This is a horrible hack to pass information about SIGINT _without_ using
243 This is a horrible hack to pass information about SIGINT _without_ using
244 exceptions, since I haven't been able to properly manage cross-thread
244 exceptions, since I haven't been able to properly manage cross-thread
245 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
245 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
246 that's my understanding from a c.l.py thread where this was discussed)."""
246 that's my understanding from a c.l.py thread where this was discussed)."""
247
247
248 global KBINT
248 global KBINT
249
249
250 print '\nKeyboardInterrupt - Press <Enter> to continue.',
250 print '\nKeyboardInterrupt - Press <Enter> to continue.',
251 Term.cout.flush()
251 Term.cout.flush()
252 # Set global flag so that runsource can know that Ctrl-C was hit
252 # Set global flag so that runsource can know that Ctrl-C was hit
253 KBINT = True
253 KBINT = True
254
254
255 class MTInteractiveShell(InteractiveShell):
255 class MTInteractiveShell(InteractiveShell):
256 """Simple multi-threaded shell."""
256 """Simple multi-threaded shell."""
257
257
258 # Threading strategy taken from:
258 # Threading strategy taken from:
259 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
259 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
261 # from the pygtk mailing list, to avoid lockups with system calls.
261 # from the pygtk mailing list, to avoid lockups with system calls.
262
262
263 # class attribute to indicate whether the class supports threads or not.
264 # Subclasses with thread support should override this as needed.
265 isthreaded = True
266
263 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
264 user_ns=None,user_global_ns=None,banner2='',**kw):
268 user_ns=None,user_global_ns=None,banner2='',**kw):
265 """Similar to the normal InteractiveShell, but with threading control"""
269 """Similar to the normal InteractiveShell, but with threading control"""
266
270
267 InteractiveShell.__init__(self,name,usage,rc,user_ns,
271 InteractiveShell.__init__(self,name,usage,rc,user_ns,
268 user_global_ns,banner2)
272 user_global_ns,banner2)
269
273
270 # Locking control variable
274 # Locking control variable
271 self.thread_ready = threading.Condition()
275 self.thread_ready = threading.Condition()
272
276
273 # Stuff to do at closing time
277 # Stuff to do at closing time
274 self._kill = False
278 self._kill = False
275 on_kill = kw.get('on_kill')
279 on_kill = kw.get('on_kill')
276 if on_kill is None:
280 if on_kill is None:
277 on_kill = []
281 on_kill = []
278 # Check that all things to kill are callable:
282 # Check that all things to kill are callable:
279 for t in on_kill:
283 for t in on_kill:
280 if not callable(t):
284 if not callable(t):
281 raise TypeError,'on_kill must be a list of callables'
285 raise TypeError,'on_kill must be a list of callables'
282 self.on_kill = on_kill
286 self.on_kill = on_kill
283
287
284 def runsource(self, source, filename="<input>", symbol="single"):
288 def runsource(self, source, filename="<input>", symbol="single"):
285 """Compile and run some source in the interpreter.
289 """Compile and run some source in the interpreter.
286
290
287 Modified version of code.py's runsource(), to handle threading issues.
291 Modified version of code.py's runsource(), to handle threading issues.
288 See the original for full docstring details."""
292 See the original for full docstring details."""
289
293
290 global KBINT
294 global KBINT
291
295
292 # If Ctrl-C was typed, we reset the flag and return right away
296 # If Ctrl-C was typed, we reset the flag and return right away
293 if KBINT:
297 if KBINT:
294 KBINT = False
298 KBINT = False
295 return False
299 return False
296
300
297 try:
301 try:
298 code = self.compile(source, filename, symbol)
302 code = self.compile(source, filename, symbol)
299 except (OverflowError, SyntaxError, ValueError):
303 except (OverflowError, SyntaxError, ValueError):
300 # Case 1
304 # Case 1
301 self.showsyntaxerror(filename)
305 self.showsyntaxerror(filename)
302 return False
306 return False
303
307
304 if code is None:
308 if code is None:
305 # Case 2
309 # Case 2
306 return True
310 return True
307
311
308 # Case 3
312 # Case 3
309 # Store code in self, so the execution thread can handle it
313 # Store code in self, so the execution thread can handle it
310 self.thread_ready.acquire()
314 self.thread_ready.acquire()
311 self.code_to_run = code
315 self.code_to_run = code
312 self.thread_ready.wait() # Wait until processed in timeout interval
316 self.thread_ready.wait() # Wait until processed in timeout interval
313 self.thread_ready.release()
317 self.thread_ready.release()
314
318
315 return False
319 return False
316
320
317 def runcode(self):
321 def runcode(self):
318 """Execute a code object.
322 """Execute a code object.
319
323
320 Multithreaded wrapper around IPython's runcode()."""
324 Multithreaded wrapper around IPython's runcode()."""
321
325
322 # lock thread-protected stuff
326 # lock thread-protected stuff
323 self.thread_ready.acquire()
327 self.thread_ready.acquire()
324
328
325 # Install sigint handler
329 # Install sigint handler
326 try:
330 try:
327 signal.signal(signal.SIGINT, sigint_handler)
331 signal.signal(signal.SIGINT, sigint_handler)
328 except SystemError:
332 except SystemError:
329 # This happens under Windows, which seems to have all sorts
333 # This happens under Windows, which seems to have all sorts
330 # of problems with signal handling. Oh well...
334 # of problems with signal handling. Oh well...
331 pass
335 pass
332
336
333 if self._kill:
337 if self._kill:
334 print >>Term.cout, 'Closing threads...',
338 print >>Term.cout, 'Closing threads...',
335 Term.cout.flush()
339 Term.cout.flush()
336 for tokill in self.on_kill:
340 for tokill in self.on_kill:
337 tokill()
341 tokill()
338 print >>Term.cout, 'Done.'
342 print >>Term.cout, 'Done.'
339
343
340 # Run pending code by calling parent class
344 # Run pending code by calling parent class
341 if self.code_to_run is not None:
345 if self.code_to_run is not None:
342 self.thread_ready.notify()
346 self.thread_ready.notify()
343 InteractiveShell.runcode(self,self.code_to_run)
347 InteractiveShell.runcode(self,self.code_to_run)
344
348
345 # We're done with thread-protected variables
349 # We're done with thread-protected variables
346 self.thread_ready.release()
350 self.thread_ready.release()
347 # This MUST return true for gtk threading to work
351 # This MUST return true for gtk threading to work
348 return True
352 return True
349
353
350 def kill (self):
354 def kill (self):
351 """Kill the thread, returning when it has been shut down."""
355 """Kill the thread, returning when it has been shut down."""
352 self.thread_ready.acquire()
356 self.thread_ready.acquire()
353 self._kill = True
357 self._kill = True
354 self.thread_ready.release()
358 self.thread_ready.release()
355
359
356 class MatplotlibShellBase:
360 class MatplotlibShellBase:
357 """Mixin class to provide the necessary modifications to regular IPython
361 """Mixin class to provide the necessary modifications to regular IPython
358 shell classes for matplotlib support.
362 shell classes for matplotlib support.
359
363
360 Given Python's MRO, this should be used as the FIRST class in the
364 Given Python's MRO, this should be used as the FIRST class in the
361 inheritance hierarchy, so that it overrides the relevant methods."""
365 inheritance hierarchy, so that it overrides the relevant methods."""
362
366
363 def _matplotlib_config(self,name):
367 def _matplotlib_config(self,name):
364 """Return various items needed to setup the user's shell with matplotlib"""
368 """Return various items needed to setup the user's shell with matplotlib"""
365
369
366 # Initialize matplotlib to interactive mode always
370 # Initialize matplotlib to interactive mode always
367 import matplotlib
371 import matplotlib
368 from matplotlib import backends
372 from matplotlib import backends
369 matplotlib.interactive(True)
373 matplotlib.interactive(True)
370
374
371 def use(arg):
375 def use(arg):
372 """IPython wrapper for matplotlib's backend switcher.
376 """IPython wrapper for matplotlib's backend switcher.
373
377
374 In interactive use, we can not allow switching to a different
378 In interactive use, we can not allow switching to a different
375 interactive backend, since thread conflicts will most likely crash
379 interactive backend, since thread conflicts will most likely crash
376 the python interpreter. This routine does a safety check first,
380 the python interpreter. This routine does a safety check first,
377 and refuses to perform a dangerous switch. It still allows
381 and refuses to perform a dangerous switch. It still allows
378 switching to non-interactive backends."""
382 switching to non-interactive backends."""
379
383
380 if arg in backends.interactive_bk and arg != self.mpl_backend:
384 if arg in backends.interactive_bk and arg != self.mpl_backend:
381 m=('invalid matplotlib backend switch.\n'
385 m=('invalid matplotlib backend switch.\n'
382 'This script attempted to switch to the interactive '
386 'This script attempted to switch to the interactive '
383 'backend: `%s`\n'
387 'backend: `%s`\n'
384 'Your current choice of interactive backend is: `%s`\n\n'
388 'Your current choice of interactive backend is: `%s`\n\n'
385 'Switching interactive matplotlib backends at runtime\n'
389 'Switching interactive matplotlib backends at runtime\n'
386 'would crash the python interpreter, '
390 'would crash the python interpreter, '
387 'and IPython has blocked it.\n\n'
391 'and IPython has blocked it.\n\n'
388 'You need to either change your choice of matplotlib backend\n'
392 'You need to either change your choice of matplotlib backend\n'
389 'by editing your .matplotlibrc file, or run this script as a \n'
393 'by editing your .matplotlibrc file, or run this script as a \n'
390 'standalone file from the command line, not using IPython.\n' %
394 'standalone file from the command line, not using IPython.\n' %
391 (arg,self.mpl_backend) )
395 (arg,self.mpl_backend) )
392 raise RuntimeError, m
396 raise RuntimeError, m
393 else:
397 else:
394 self.mpl_use(arg)
398 self.mpl_use(arg)
395 self.mpl_use._called = True
399 self.mpl_use._called = True
396
400
397 self.matplotlib = matplotlib
401 self.matplotlib = matplotlib
398 self.mpl_backend = matplotlib.rcParams['backend']
402 self.mpl_backend = matplotlib.rcParams['backend']
399
403
400 # we also need to block switching of interactive backends by use()
404 # we also need to block switching of interactive backends by use()
401 self.mpl_use = matplotlib.use
405 self.mpl_use = matplotlib.use
402 self.mpl_use._called = False
406 self.mpl_use._called = False
403 # overwrite the original matplotlib.use with our wrapper
407 # overwrite the original matplotlib.use with our wrapper
404 matplotlib.use = use
408 matplotlib.use = use
405
409
406
410
407 # This must be imported last in the matplotlib series, after
411 # This must be imported last in the matplotlib series, after
408 # backend/interactivity choices have been made
412 # backend/interactivity choices have been made
409 try:
413 try:
410 import matplotlib.pylab as pylab
414 import matplotlib.pylab as pylab
411 self.pylab = pylab
415 self.pylab = pylab
412 self.pylab_name = 'pylab'
416 self.pylab_name = 'pylab'
413 except ImportError:
417 except ImportError:
414 import matplotlib.matlab as matlab
418 import matplotlib.matlab as matlab
415 self.pylab = matlab
419 self.pylab = matlab
416 self.pylab_name = 'matlab'
420 self.pylab_name = 'matlab'
417
421
418 self.pylab.show._needmain = False
422 self.pylab.show._needmain = False
419 # We need to detect at runtime whether show() is called by the user.
423 # We need to detect at runtime whether show() is called by the user.
420 # For this, we wrap it into a decorator which adds a 'called' flag.
424 # For this, we wrap it into a decorator which adds a 'called' flag.
421 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
425 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
422
426
423 # Build a user namespace initialized with matplotlib/matlab features.
427 # Build a user namespace initialized with matplotlib/matlab features.
424 user_ns = {'__name__':'__main__',
428 user_ns = {'__name__':'__main__',
425 '__builtins__' : __builtin__ }
429 '__builtins__' : __builtin__ }
426
430
427 # Be careful not to remove the final \n in the code string below, or
431 # Be careful not to remove the final \n in the code string below, or
428 # things will break badly with py22 (I think it's a python bug, 2.3 is
432 # things will break badly with py22 (I think it's a python bug, 2.3 is
429 # OK).
433 # OK).
430 pname = self.pylab_name # Python can't interpolate dotted var names
434 pname = self.pylab_name # Python can't interpolate dotted var names
431 exec ("import matplotlib\n"
435 exec ("import matplotlib\n"
432 "import matplotlib.%(pname)s as %(pname)s\n"
436 "import matplotlib.%(pname)s as %(pname)s\n"
433 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
437 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
434
438
435 # Build matplotlib info banner
439 # Build matplotlib info banner
436 b="""
440 b="""
437 Welcome to pylab, a matplotlib-based Python environment.
441 Welcome to pylab, a matplotlib-based Python environment.
438 For more information, type 'help(pylab)'.
442 For more information, type 'help(pylab)'.
439 """
443 """
440 return user_ns,b
444 return user_ns,b
441
445
442 def mplot_exec(self,fname,*where,**kw):
446 def mplot_exec(self,fname,*where,**kw):
443 """Execute a matplotlib script.
447 """Execute a matplotlib script.
444
448
445 This is a call to execfile(), but wrapped in safeties to properly
449 This is a call to execfile(), but wrapped in safeties to properly
446 handle interactive rendering and backend switching."""
450 handle interactive rendering and backend switching."""
447
451
448 #print '*** Matplotlib runner ***' # dbg
452 #print '*** Matplotlib runner ***' # dbg
449 # turn off rendering until end of script
453 # turn off rendering until end of script
450 isInteractive = self.matplotlib.rcParams['interactive']
454 isInteractive = self.matplotlib.rcParams['interactive']
451 self.matplotlib.interactive(False)
455 self.matplotlib.interactive(False)
452 self.safe_execfile(fname,*where,**kw)
456 self.safe_execfile(fname,*where,**kw)
453 self.matplotlib.interactive(isInteractive)
457 self.matplotlib.interactive(isInteractive)
454 # make rendering call now, if the user tried to do it
458 # make rendering call now, if the user tried to do it
455 if self.pylab.draw_if_interactive.called:
459 if self.pylab.draw_if_interactive.called:
456 self.pylab.draw()
460 self.pylab.draw()
457 self.pylab.draw_if_interactive.called = False
461 self.pylab.draw_if_interactive.called = False
458
462
459 # if a backend switch was performed, reverse it now
463 # if a backend switch was performed, reverse it now
460 if self.mpl_use._called:
464 if self.mpl_use._called:
461 self.matplotlib.rcParams['backend'] = self.mpl_backend
465 self.matplotlib.rcParams['backend'] = self.mpl_backend
462
466
463 def magic_run(self,parameter_s=''):
467 def magic_run(self,parameter_s=''):
464 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
468 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
465
469
466 # Fix the docstring so users see the original as well
470 # Fix the docstring so users see the original as well
467 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
471 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
468 "\n *** Modified %run for Matplotlib,"
472 "\n *** Modified %run for Matplotlib,"
469 " with proper interactive handling ***")
473 " with proper interactive handling ***")
470
474
471 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
475 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
472 # and multithreaded. Note that these are meant for internal use, the IPShell*
476 # and multithreaded. Note that these are meant for internal use, the IPShell*
473 # classes below are the ones meant for public consumption.
477 # classes below are the ones meant for public consumption.
474
478
475 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
479 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
476 """Single-threaded shell with matplotlib support."""
480 """Single-threaded shell with matplotlib support."""
477
481
478 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
482 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
479 user_ns=None,user_global_ns=None,**kw):
483 user_ns=None,user_global_ns=None,**kw):
480 user_ns,b2 = self._matplotlib_config(name)
484 user_ns,b2 = self._matplotlib_config(name)
481 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
485 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
482 banner2=b2,**kw)
486 banner2=b2,**kw)
483
487
484 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
488 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
485 """Multi-threaded shell with matplotlib support."""
489 """Multi-threaded shell with matplotlib support."""
486
490
487 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
491 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
488 user_ns=None,user_global_ns=None, **kw):
492 user_ns=None,user_global_ns=None, **kw):
489 user_ns,b2 = self._matplotlib_config(name)
493 user_ns,b2 = self._matplotlib_config(name)
490 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
494 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
491 banner2=b2,**kw)
495 banner2=b2,**kw)
492
496
493 #-----------------------------------------------------------------------------
497 #-----------------------------------------------------------------------------
494 # Utility functions for the different GUI enabled IPShell* classes.
498 # Utility functions for the different GUI enabled IPShell* classes.
495
499
496 def get_tk():
500 def get_tk():
497 """Tries to import Tkinter and returns a withdrawn Tkinter root
501 """Tries to import Tkinter and returns a withdrawn Tkinter root
498 window. If Tkinter is already imported or not available, this
502 window. If Tkinter is already imported or not available, this
499 returns None. This function calls `hijack_tk` underneath.
503 returns None. This function calls `hijack_tk` underneath.
500 """
504 """
501 if not USE_TK or sys.modules.has_key('Tkinter'):
505 if not USE_TK or sys.modules.has_key('Tkinter'):
502 return None
506 return None
503 else:
507 else:
504 try:
508 try:
505 import Tkinter
509 import Tkinter
506 except ImportError:
510 except ImportError:
507 return None
511 return None
508 else:
512 else:
509 hijack_tk()
513 hijack_tk()
510 r = Tkinter.Tk()
514 r = Tkinter.Tk()
511 r.withdraw()
515 r.withdraw()
512 return r
516 return r
513
517
514 def hijack_tk():
518 def hijack_tk():
515 """Modifies Tkinter's mainloop with a dummy so when a module calls
519 """Modifies Tkinter's mainloop with a dummy so when a module calls
516 mainloop, it does not block.
520 mainloop, it does not block.
517
521
518 """
522 """
519 def misc_mainloop(self, n=0):
523 def misc_mainloop(self, n=0):
520 pass
524 pass
521 def tkinter_mainloop(n=0):
525 def tkinter_mainloop(n=0):
522 pass
526 pass
523
527
524 import Tkinter
528 import Tkinter
525 Tkinter.Misc.mainloop = misc_mainloop
529 Tkinter.Misc.mainloop = misc_mainloop
526 Tkinter.mainloop = tkinter_mainloop
530 Tkinter.mainloop = tkinter_mainloop
527
531
528 def update_tk(tk):
532 def update_tk(tk):
529 """Updates the Tkinter event loop. This is typically called from
533 """Updates the Tkinter event loop. This is typically called from
530 the respective WX or GTK mainloops.
534 the respective WX or GTK mainloops.
531 """
535 """
532 if tk:
536 if tk:
533 tk.update()
537 tk.update()
534
538
535 def hijack_wx():
539 def hijack_wx():
536 """Modifies wxPython's MainLoop with a dummy so user code does not
540 """Modifies wxPython's MainLoop with a dummy so user code does not
537 block IPython. The hijacked mainloop function is returned.
541 block IPython. The hijacked mainloop function is returned.
538 """
542 """
539 def dummy_mainloop(*args, **kw):
543 def dummy_mainloop(*args, **kw):
540 pass
544 pass
541 import wxPython
545 import wxPython
542 ver = wxPython.__version__
546 ver = wxPython.__version__
543 orig_mainloop = None
547 orig_mainloop = None
544 if ver[:3] >= '2.5':
548 if ver[:3] >= '2.5':
545 import wx
549 import wx
546 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
550 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
547 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
551 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
548 else: raise AttributeError('Could not find wx core module')
552 else: raise AttributeError('Could not find wx core module')
549 orig_mainloop = core.PyApp_MainLoop
553 orig_mainloop = core.PyApp_MainLoop
550 core.PyApp_MainLoop = dummy_mainloop
554 core.PyApp_MainLoop = dummy_mainloop
551 elif ver[:3] == '2.4':
555 elif ver[:3] == '2.4':
552 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
556 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
553 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
557 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
554 else:
558 else:
555 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
559 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
556 return orig_mainloop
560 return orig_mainloop
557
561
558 def hijack_gtk():
562 def hijack_gtk():
559 """Modifies pyGTK's mainloop with a dummy so user code does not
563 """Modifies pyGTK's mainloop with a dummy so user code does not
560 block IPython. This function returns the original `gtk.mainloop`
564 block IPython. This function returns the original `gtk.mainloop`
561 function that has been hijacked.
565 function that has been hijacked.
562 """
566 """
563 def dummy_mainloop(*args, **kw):
567 def dummy_mainloop(*args, **kw):
564 pass
568 pass
565 import gtk
569 import gtk
566 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
570 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
567 else: orig_mainloop = gtk.mainloop
571 else: orig_mainloop = gtk.mainloop
568 gtk.mainloop = dummy_mainloop
572 gtk.mainloop = dummy_mainloop
569 gtk.main = dummy_mainloop
573 gtk.main = dummy_mainloop
570 return orig_mainloop
574 return orig_mainloop
571
575
572 #-----------------------------------------------------------------------------
576 #-----------------------------------------------------------------------------
573 # The IPShell* classes below are the ones meant to be run by external code as
577 # The IPShell* classes below are the ones meant to be run by external code as
574 # IPython instances. Note that unless a specific threading strategy is
578 # IPython instances. Note that unless a specific threading strategy is
575 # desired, the factory function start() below should be used instead (it
579 # desired, the factory function start() below should be used instead (it
576 # selects the proper threaded class).
580 # selects the proper threaded class).
577
581
578 class IPShellGTK(threading.Thread):
582 class IPShellGTK(threading.Thread):
579 """Run a gtk mainloop() in a separate thread.
583 """Run a gtk mainloop() in a separate thread.
580
584
581 Python commands can be passed to the thread where they will be executed.
585 Python commands can be passed to the thread where they will be executed.
582 This is implemented by periodically checking for passed code using a
586 This is implemented by periodically checking for passed code using a
583 GTK timeout callback."""
587 GTK timeout callback."""
584
588
585 TIMEOUT = 100 # Millisecond interval between timeouts.
589 TIMEOUT = 100 # Millisecond interval between timeouts.
586
590
587 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
591 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
588 debug=1,shell_class=MTInteractiveShell):
592 debug=1,shell_class=MTInteractiveShell):
589
593
590 import gtk
594 import gtk
591
595
592 self.gtk = gtk
596 self.gtk = gtk
593 self.gtk_mainloop = hijack_gtk()
597 self.gtk_mainloop = hijack_gtk()
594
598
595 # Allows us to use both Tk and GTK.
599 # Allows us to use both Tk and GTK.
596 self.tk = get_tk()
600 self.tk = get_tk()
597
601
598 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
602 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
599 else: mainquit = self.gtk.mainquit
603 else: mainquit = self.gtk.mainquit
600
604
601 self.IP = make_IPython(argv,user_ns=user_ns,
605 self.IP = make_IPython(argv,user_ns=user_ns,
602 user_global_ns=user_global_ns,
606 user_global_ns=user_global_ns,
603 debug=debug,
607 debug=debug,
604 shell_class=shell_class,
608 shell_class=shell_class,
605 on_kill=[mainquit])
609 on_kill=[mainquit])
606
610
607 # HACK: slot for banner in self; it will be passed to the mainloop
611 # HACK: slot for banner in self; it will be passed to the mainloop
608 # method only and .run() needs it. The actual value will be set by
612 # method only and .run() needs it. The actual value will be set by
609 # .mainloop().
613 # .mainloop().
610 self._banner = None
614 self._banner = None
611
615
612 threading.Thread.__init__(self)
616 threading.Thread.__init__(self)
613
617
614 def run(self):
618 def run(self):
615 self.IP.mainloop(self._banner)
619 self.IP.mainloop(self._banner)
616 self.IP.kill()
620 self.IP.kill()
617
621
618 def mainloop(self,sys_exit=0,banner=None):
622 def mainloop(self,sys_exit=0,banner=None):
619
623
620 self._banner = banner
624 self._banner = banner
621
625
622 if self.gtk.pygtk_version >= (2,4,0):
626 if self.gtk.pygtk_version >= (2,4,0):
623 import gobject
627 import gobject
624 gobject.idle_add(self.on_timer)
628 gobject.idle_add(self.on_timer)
625 else:
629 else:
626 self.gtk.idle_add(self.on_timer)
630 self.gtk.idle_add(self.on_timer)
627
631
628 if sys.platform != 'win32':
632 if sys.platform != 'win32':
629 try:
633 try:
630 if self.gtk.gtk_version[0] >= 2:
634 if self.gtk.gtk_version[0] >= 2:
631 self.gtk.threads_init()
635 self.gtk.threads_init()
632 except AttributeError:
636 except AttributeError:
633 pass
637 pass
634 except RuntimeError:
638 except RuntimeError:
635 error('Your pyGTK likely has not been compiled with '
639 error('Your pyGTK likely has not been compiled with '
636 'threading support.\n'
640 'threading support.\n'
637 'The exception printout is below.\n'
641 'The exception printout is below.\n'
638 'You can either rebuild pyGTK with threads, or '
642 'You can either rebuild pyGTK with threads, or '
639 'try using \n'
643 'try using \n'
640 'matplotlib with a different backend (like Tk or WX).\n'
644 'matplotlib with a different backend (like Tk or WX).\n'
641 'Note that matplotlib will most likely not work in its '
645 'Note that matplotlib will most likely not work in its '
642 'current state!')
646 'current state!')
643 self.IP.InteractiveTB()
647 self.IP.InteractiveTB()
644 self.start()
648 self.start()
645 self.gtk.threads_enter()
649 self.gtk.threads_enter()
646 self.gtk_mainloop()
650 self.gtk_mainloop()
647 self.gtk.threads_leave()
651 self.gtk.threads_leave()
648 self.join()
652 self.join()
649
653
650 def on_timer(self):
654 def on_timer(self):
651 update_tk(self.tk)
655 update_tk(self.tk)
652 return self.IP.runcode()
656 return self.IP.runcode()
653
657
654
658
655 class IPShellWX(threading.Thread):
659 class IPShellWX(threading.Thread):
656 """Run a wx mainloop() in a separate thread.
660 """Run a wx mainloop() in a separate thread.
657
661
658 Python commands can be passed to the thread where they will be executed.
662 Python commands can be passed to the thread where they will be executed.
659 This is implemented by periodically checking for passed code using a
663 This is implemented by periodically checking for passed code using a
660 GTK timeout callback."""
664 GTK timeout callback."""
661
665
662 TIMEOUT = 100 # Millisecond interval between timeouts.
666 TIMEOUT = 100 # Millisecond interval between timeouts.
663
667
664 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
668 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
665 debug=1,shell_class=MTInteractiveShell):
669 debug=1,shell_class=MTInteractiveShell):
666
670
667 import wxPython.wx as wx
671 import wxPython.wx as wx
668
672
669 threading.Thread.__init__(self)
673 threading.Thread.__init__(self)
670 self.wx = wx
674 self.wx = wx
671 self.wx_mainloop = hijack_wx()
675 self.wx_mainloop = hijack_wx()
672
676
673 # Allows us to use both Tk and GTK.
677 # Allows us to use both Tk and GTK.
674 self.tk = get_tk()
678 self.tk = get_tk()
675
679
676 self.IP = make_IPython(argv,user_ns=user_ns,
680 self.IP = make_IPython(argv,user_ns=user_ns,
677 user_global_ns=user_global_ns,
681 user_global_ns=user_global_ns,
678 debug=debug,
682 debug=debug,
679 shell_class=shell_class,
683 shell_class=shell_class,
680 on_kill=[self.wxexit])
684 on_kill=[self.wxexit])
681 # HACK: slot for banner in self; it will be passed to the mainloop
685 # HACK: slot for banner in self; it will be passed to the mainloop
682 # method only and .run() needs it. The actual value will be set by
686 # method only and .run() needs it. The actual value will be set by
683 # .mainloop().
687 # .mainloop().
684 self._banner = None
688 self._banner = None
685
689
686 self.app = None
690 self.app = None
687
691
688 def wxexit(self, *args):
692 def wxexit(self, *args):
689 if self.app is not None:
693 if self.app is not None:
690 self.app.agent.timer.Stop()
694 self.app.agent.timer.Stop()
691 self.app.ExitMainLoop()
695 self.app.ExitMainLoop()
692
696
693 def run(self):
697 def run(self):
694 self.IP.mainloop(self._banner)
698 self.IP.mainloop(self._banner)
695 self.IP.kill()
699 self.IP.kill()
696
700
697 def mainloop(self,sys_exit=0,banner=None):
701 def mainloop(self,sys_exit=0,banner=None):
698
702
699 self._banner = banner
703 self._banner = banner
700
704
701 self.start()
705 self.start()
702
706
703 class TimerAgent(self.wx.wxMiniFrame):
707 class TimerAgent(self.wx.wxMiniFrame):
704 wx = self.wx
708 wx = self.wx
705 IP = self.IP
709 IP = self.IP
706 tk = self.tk
710 tk = self.tk
707 def __init__(self, parent, interval):
711 def __init__(self, parent, interval):
708 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
712 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
709 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
713 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
710 size=(100, 100),style=style)
714 size=(100, 100),style=style)
711 self.Show(False)
715 self.Show(False)
712 self.interval = interval
716 self.interval = interval
713 self.timerId = self.wx.wxNewId()
717 self.timerId = self.wx.wxNewId()
714
718
715 def StartWork(self):
719 def StartWork(self):
716 self.timer = self.wx.wxTimer(self, self.timerId)
720 self.timer = self.wx.wxTimer(self, self.timerId)
717 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
721 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
718 self.timer.Start(self.interval)
722 self.timer.Start(self.interval)
719
723
720 def OnTimer(self, event):
724 def OnTimer(self, event):
721 update_tk(self.tk)
725 update_tk(self.tk)
722 self.IP.runcode()
726 self.IP.runcode()
723
727
724 class App(self.wx.wxApp):
728 class App(self.wx.wxApp):
725 wx = self.wx
729 wx = self.wx
726 TIMEOUT = self.TIMEOUT
730 TIMEOUT = self.TIMEOUT
727 def OnInit(self):
731 def OnInit(self):
728 'Create the main window and insert the custom frame'
732 'Create the main window and insert the custom frame'
729 self.agent = TimerAgent(None, self.TIMEOUT)
733 self.agent = TimerAgent(None, self.TIMEOUT)
730 self.agent.Show(self.wx.false)
734 self.agent.Show(self.wx.false)
731 self.agent.StartWork()
735 self.agent.StartWork()
732 return self.wx.true
736 return self.wx.true
733
737
734 self.app = App(redirect=False)
738 self.app = App(redirect=False)
735 self.wx_mainloop(self.app)
739 self.wx_mainloop(self.app)
736 self.join()
740 self.join()
737
741
738
742
739 class IPShellQt(threading.Thread):
743 class IPShellQt(threading.Thread):
740 """Run a Qt event loop in a separate thread.
744 """Run a Qt event loop in a separate thread.
741
745
742 Python commands can be passed to the thread where they will be executed.
746 Python commands can be passed to the thread where they will be executed.
743 This is implemented by periodically checking for passed code using a
747 This is implemented by periodically checking for passed code using a
744 Qt timer / slot."""
748 Qt timer / slot."""
745
749
746 TIMEOUT = 100 # Millisecond interval between timeouts.
750 TIMEOUT = 100 # Millisecond interval between timeouts.
747
751
748 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
752 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
749 debug=0,shell_class=MTInteractiveShell):
753 debug=0,shell_class=MTInteractiveShell):
750
754
751 import qt
755 import qt
752
756
753 class newQApplication:
757 class newQApplication:
754 def __init__( self ):
758 def __init__( self ):
755 self.QApplication = qt.QApplication
759 self.QApplication = qt.QApplication
756
760
757 def __call__( *args, **kwargs ):
761 def __call__( *args, **kwargs ):
758 return qt.qApp
762 return qt.qApp
759
763
760 def exec_loop( *args, **kwargs ):
764 def exec_loop( *args, **kwargs ):
761 pass
765 pass
762
766
763 def __getattr__( self, name ):
767 def __getattr__( self, name ):
764 return getattr( self.QApplication, name )
768 return getattr( self.QApplication, name )
765
769
766 qt.QApplication = newQApplication()
770 qt.QApplication = newQApplication()
767
771
768 # Allows us to use both Tk and QT.
772 # Allows us to use both Tk and QT.
769 self.tk = get_tk()
773 self.tk = get_tk()
770
774
771 self.IP = make_IPython(argv,user_ns=user_ns,
775 self.IP = make_IPython(argv,user_ns=user_ns,
772 user_global_ns=user_global_ns,
776 user_global_ns=user_global_ns,
773 debug=debug,
777 debug=debug,
774 shell_class=shell_class,
778 shell_class=shell_class,
775 on_kill=[qt.qApp.exit])
779 on_kill=[qt.qApp.exit])
776
780
777 # HACK: slot for banner in self; it will be passed to the mainloop
781 # HACK: slot for banner in self; it will be passed to the mainloop
778 # method only and .run() needs it. The actual value will be set by
782 # method only and .run() needs it. The actual value will be set by
779 # .mainloop().
783 # .mainloop().
780 self._banner = None
784 self._banner = None
781
785
782 threading.Thread.__init__(self)
786 threading.Thread.__init__(self)
783
787
784 def run(self):
788 def run(self):
785 self.IP.mainloop(self._banner)
789 self.IP.mainloop(self._banner)
786 self.IP.kill()
790 self.IP.kill()
787
791
788 def mainloop(self,sys_exit=0,banner=None):
792 def mainloop(self,sys_exit=0,banner=None):
789
793
790 import qt
794 import qt
791
795
792 self._banner = banner
796 self._banner = banner
793
797
794 if qt.QApplication.startingUp():
798 if qt.QApplication.startingUp():
795 a = qt.QApplication.QApplication(sys.argv)
799 a = qt.QApplication.QApplication(sys.argv)
796 self.timer = qt.QTimer()
800 self.timer = qt.QTimer()
797 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
801 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
798
802
799 self.start()
803 self.start()
800 self.timer.start( self.TIMEOUT, True )
804 self.timer.start( self.TIMEOUT, True )
801 while True:
805 while True:
802 if self.IP._kill: break
806 if self.IP._kill: break
803 qt.qApp.exec_loop()
807 qt.qApp.exec_loop()
804 self.join()
808 self.join()
805
809
806 def on_timer(self):
810 def on_timer(self):
807 update_tk(self.tk)
811 update_tk(self.tk)
808 result = self.IP.runcode()
812 result = self.IP.runcode()
809 self.timer.start( self.TIMEOUT, True )
813 self.timer.start( self.TIMEOUT, True )
810 return result
814 return result
811
815
812 # A set of matplotlib public IPython shell classes, for single-threaded
816 # A set of matplotlib public IPython shell classes, for single-threaded
813 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
817 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
814 class IPShellMatplotlib(IPShell):
818 class IPShellMatplotlib(IPShell):
815 """Subclass IPShell with MatplotlibShell as the internal shell.
819 """Subclass IPShell with MatplotlibShell as the internal shell.
816
820
817 Single-threaded class, meant for the Tk* and FLTK* backends.
821 Single-threaded class, meant for the Tk* and FLTK* backends.
818
822
819 Having this on a separate class simplifies the external driver code."""
823 Having this on a separate class simplifies the external driver code."""
820
824
821 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
825 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
822 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
826 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
823 shell_class=MatplotlibShell)
827 shell_class=MatplotlibShell)
824
828
825 class IPShellMatplotlibGTK(IPShellGTK):
829 class IPShellMatplotlibGTK(IPShellGTK):
826 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
830 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
827
831
828 Multi-threaded class, meant for the GTK* backends."""
832 Multi-threaded class, meant for the GTK* backends."""
829
833
830 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
834 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
831 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
835 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
832 shell_class=MatplotlibMTShell)
836 shell_class=MatplotlibMTShell)
833
837
834 class IPShellMatplotlibWX(IPShellWX):
838 class IPShellMatplotlibWX(IPShellWX):
835 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
839 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
836
840
837 Multi-threaded class, meant for the WX* backends."""
841 Multi-threaded class, meant for the WX* backends."""
838
842
839 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
843 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
840 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
844 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
841 shell_class=MatplotlibMTShell)
845 shell_class=MatplotlibMTShell)
842
846
843 class IPShellMatplotlibQt(IPShellQt):
847 class IPShellMatplotlibQt(IPShellQt):
844 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
848 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
845
849
846 Multi-threaded class, meant for the Qt* backends."""
850 Multi-threaded class, meant for the Qt* backends."""
847
851
848 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
852 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
849 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
853 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
850 shell_class=MatplotlibMTShell)
854 shell_class=MatplotlibMTShell)
851
855
852 #-----------------------------------------------------------------------------
856 #-----------------------------------------------------------------------------
853 # Factory functions to actually start the proper thread-aware shell
857 # Factory functions to actually start the proper thread-aware shell
854
858
855 def _matplotlib_shell_class():
859 def _matplotlib_shell_class():
856 """Factory function to handle shell class selection for matplotlib.
860 """Factory function to handle shell class selection for matplotlib.
857
861
858 The proper shell class to use depends on the matplotlib backend, since
862 The proper shell class to use depends on the matplotlib backend, since
859 each backend requires a different threading strategy."""
863 each backend requires a different threading strategy."""
860
864
861 try:
865 try:
862 import matplotlib
866 import matplotlib
863 except ImportError:
867 except ImportError:
864 error('matplotlib could NOT be imported! Starting normal IPython.')
868 error('matplotlib could NOT be imported! Starting normal IPython.')
865 sh_class = IPShell
869 sh_class = IPShell
866 else:
870 else:
867 backend = matplotlib.rcParams['backend']
871 backend = matplotlib.rcParams['backend']
868 if backend.startswith('GTK'):
872 if backend.startswith('GTK'):
869 sh_class = IPShellMatplotlibGTK
873 sh_class = IPShellMatplotlibGTK
870 elif backend.startswith('WX'):
874 elif backend.startswith('WX'):
871 sh_class = IPShellMatplotlibWX
875 sh_class = IPShellMatplotlibWX
872 elif backend.startswith('Qt'):
876 elif backend.startswith('Qt'):
873 sh_class = IPShellMatplotlibQt
877 sh_class = IPShellMatplotlibQt
874 else:
878 else:
875 sh_class = IPShellMatplotlib
879 sh_class = IPShellMatplotlib
876 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
880 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
877 return sh_class
881 return sh_class
878
882
879 # This is the one which should be called by external code.
883 # This is the one which should be called by external code.
880 def start():
884 def start():
881 """Return a running shell instance, dealing with threading options.
885 """Return a running shell instance, dealing with threading options.
882
886
883 This is a factory function which will instantiate the proper IPython shell
887 This is a factory function which will instantiate the proper IPython shell
884 based on the user's threading choice. Such a selector is needed because
888 based on the user's threading choice. Such a selector is needed because
885 different GUI toolkits require different thread handling details."""
889 different GUI toolkits require different thread handling details."""
886
890
887 global USE_TK
891 global USE_TK
888 # Crude sys.argv hack to extract the threading options.
892 # Crude sys.argv hack to extract the threading options.
889 argv = sys.argv
893 argv = sys.argv
890 if len(argv) > 1:
894 if len(argv) > 1:
891 if len(argv) > 2:
895 if len(argv) > 2:
892 arg2 = argv[2]
896 arg2 = argv[2]
893 if arg2.endswith('-tk'):
897 if arg2.endswith('-tk'):
894 USE_TK = True
898 USE_TK = True
895 arg1 = argv[1]
899 arg1 = argv[1]
896 if arg1.endswith('-gthread'):
900 if arg1.endswith('-gthread'):
897 shell = IPShellGTK
901 shell = IPShellGTK
898 elif arg1.endswith( '-qthread' ):
902 elif arg1.endswith( '-qthread' ):
899 shell = IPShellQt
903 shell = IPShellQt
900 elif arg1.endswith('-wthread'):
904 elif arg1.endswith('-wthread'):
901 shell = IPShellWX
905 shell = IPShellWX
902 elif arg1.endswith('-pylab'):
906 elif arg1.endswith('-pylab'):
903 shell = _matplotlib_shell_class()
907 shell = _matplotlib_shell_class()
904 else:
908 else:
905 shell = IPShell
909 shell = IPShell
906 else:
910 else:
907 shell = IPShell
911 shell = IPShell
908 return shell()
912 return shell()
909
913
910 # Some aliases for backwards compatibility
914 # Some aliases for backwards compatibility
911 IPythonShell = IPShell
915 IPythonShell = IPShell
912 IPythonShellEmbed = IPShellEmbed
916 IPythonShellEmbed = IPShellEmbed
913 #************************ End of file <Shell.py> ***************************
917 #************************ End of file <Shell.py> ***************************
@@ -1,1968 +1,1997 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.1 or newer.
5 Requires Python 2.1 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 964 2005-12-28 21:03:01Z fperez $
9 $Id: iplib.py 965 2005-12-28 23:23:09Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 # Copyright (C) 2001-2005 Fernando Perez. <fperez@colorado.edu>
14 # Copyright (C) 2001-2005 Fernando Perez. <fperez@colorado.edu>
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #
18 #
19 # Note: this code originally subclassed code.InteractiveConsole from the
19 # Note: this code originally subclassed code.InteractiveConsole from the
20 # Python standard library. Over time, all of that class has been copied
20 # Python standard library. Over time, all of that class has been copied
21 # verbatim here for modifications which could not be accomplished by
21 # verbatim here for modifications which could not be accomplished by
22 # subclassing. At this point, there are no dependencies at all on the code
22 # subclassing. At this point, there are no dependencies at all on the code
23 # module anymore (it is not even imported). The Python License (sec. 2)
23 # module anymore (it is not even imported). The Python License (sec. 2)
24 # allows for this, but it's always nice to acknowledge credit where credit is
24 # allows for this, but it's always nice to acknowledge credit where credit is
25 # due.
25 # due.
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 #****************************************************************************
28 #****************************************************************************
29 # Modules and globals
29 # Modules and globals
30
30
31 from __future__ import generators # for 2.2 backwards-compatibility
31 from __future__ import generators # for 2.2 backwards-compatibility
32
32
33 from IPython import Release
33 from IPython import Release
34 __author__ = '%s <%s>\n%s <%s>' % \
34 __author__ = '%s <%s>\n%s <%s>' % \
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 __license__ = Release.license
36 __license__ = Release.license
37 __version__ = Release.version
37 __version__ = Release.version
38
38
39 # Python standard modules
39 # Python standard modules
40 import __main__
40 import __main__
41 import __builtin__
41 import __builtin__
42 import StringIO
42 import StringIO
43 import bdb
43 import bdb
44 import cPickle as pickle
44 import cPickle as pickle
45 import codeop
45 import codeop
46 import exceptions
46 import exceptions
47 import glob
47 import glob
48 import inspect
48 import inspect
49 import keyword
49 import keyword
50 import new
50 import new
51 import os
51 import os
52 import pdb
52 import pdb
53 import pydoc
53 import pydoc
54 import re
54 import re
55 import shutil
55 import shutil
56 import string
56 import string
57 import sys
57 import sys
58 import traceback
58 import traceback
59 import types
59 import types
60
60
61 from pprint import pprint, pformat
61 from pprint import pprint, pformat
62
62
63 # IPython's own modules
63 # IPython's own modules
64 import IPython
64 import IPython
65 from IPython import OInspect,PyColorize,ultraTB
65 from IPython import OInspect,PyColorize,ultraTB
66 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
66 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
67 from IPython.FakeModule import FakeModule
67 from IPython.FakeModule import FakeModule
68 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
68 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
69 from IPython.Logger import Logger
69 from IPython.Logger import Logger
70 from IPython.Magic import Magic,magic2python
70 from IPython.Magic import Magic,magic2python
71 from IPython.Struct import Struct
71 from IPython.Struct import Struct
72 from IPython.background_jobs import BackgroundJobManager
72 from IPython.background_jobs import BackgroundJobManager
73 from IPython.usage import cmd_line_usage,interactive_usage
73 from IPython.usage import cmd_line_usage,interactive_usage
74 from IPython.genutils import *
74 from IPython.genutils import *
75
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 #****************************************************************************
80 #****************************************************************************
81 # Some utility function definitions
81 # Some utility function definitions
82
82
83 # This can be replaced with an isspace() call once we drop 2.2 compatibility
83 # This can be replaced with an isspace() call once we drop 2.2 compatibility
84 _isspace_match = re.compile(r'^\s+$').match
84 _isspace_match = re.compile(r'^\s+$').match
85 def isspace(s):
85 def isspace(s):
86 return bool(_isspace_match(s))
86 return bool(_isspace_match(s))
87
87
88 def esc_quotes(strng):
88 def esc_quotes(strng):
89 """Return the input string with single and double quotes escaped out"""
89 """Return the input string with single and double quotes escaped out"""
90
90
91 return strng.replace('"','\\"').replace("'","\\'")
91 return strng.replace('"','\\"').replace("'","\\'")
92
92
93 def import_fail_info(mod_name,fns=None):
93 def import_fail_info(mod_name,fns=None):
94 """Inform load failure for a module."""
94 """Inform load failure for a module."""
95
95
96 if fns == None:
96 if fns == None:
97 warn("Loading of %s failed.\n" % (mod_name,))
97 warn("Loading of %s failed.\n" % (mod_name,))
98 else:
98 else:
99 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
99 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
100
100
101 def qw_lol(indata):
101 def qw_lol(indata):
102 """qw_lol('a b') -> [['a','b']],
102 """qw_lol('a b') -> [['a','b']],
103 otherwise it's just a call to qw().
103 otherwise it's just a call to qw().
104
104
105 We need this to make sure the modules_some keys *always* end up as a
105 We need this to make sure the modules_some keys *always* end up as a
106 list of lists."""
106 list of lists."""
107
107
108 if type(indata) in StringTypes:
108 if type(indata) in StringTypes:
109 return [qw(indata)]
109 return [qw(indata)]
110 else:
110 else:
111 return qw(indata)
111 return qw(indata)
112
112
113 def ipmagic(arg_s):
113 def ipmagic(arg_s):
114 """Call a magic function by name.
114 """Call a magic function by name.
115
115
116 Input: a string containing the name of the magic function to call and any
116 Input: a string containing the name of the magic function to call and any
117 additional arguments to be passed to the magic.
117 additional arguments to be passed to the magic.
118
118
119 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
119 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
120 prompt:
120 prompt:
121
121
122 In[1]: %name -opt foo bar
122 In[1]: %name -opt foo bar
123
123
124 To call a magic without arguments, simply use ipmagic('name').
124 To call a magic without arguments, simply use ipmagic('name').
125
125
126 This provides a proper Python function to call IPython's magics in any
126 This provides a proper Python function to call IPython's magics in any
127 valid Python code you can type at the interpreter, including loops and
127 valid Python code you can type at the interpreter, including loops and
128 compound statements. It is added by IPython to the Python builtin
128 compound statements. It is added by IPython to the Python builtin
129 namespace upon initialization."""
129 namespace upon initialization."""
130
130
131 args = arg_s.split(' ',1)
131 args = arg_s.split(' ',1)
132 magic_name = args[0]
132 magic_name = args[0]
133 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
133 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
134 magic_name = magic_name[1:]
134 magic_name = magic_name[1:]
135 try:
135 try:
136 magic_args = args[1]
136 magic_args = args[1]
137 except IndexError:
137 except IndexError:
138 magic_args = ''
138 magic_args = ''
139 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
139 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
140 if fn is None:
140 if fn is None:
141 error("Magic function `%s` not found." % magic_name)
141 error("Magic function `%s` not found." % magic_name)
142 else:
142 else:
143 magic_args = __IPYTHON__.var_expand(magic_args)
143 magic_args = __IPYTHON__.var_expand(magic_args)
144 return fn(magic_args)
144 return fn(magic_args)
145
145
146 def ipalias(arg_s):
146 def ipalias(arg_s):
147 """Call an alias by name.
147 """Call an alias by name.
148
148
149 Input: a string containing the name of the alias to call and any
149 Input: a string containing the name of the alias to call and any
150 additional arguments to be passed to the magic.
150 additional arguments to be passed to the magic.
151
151
152 ipalias('name -opt foo bar') is equivalent to typing at the ipython
152 ipalias('name -opt foo bar') is equivalent to typing at the ipython
153 prompt:
153 prompt:
154
154
155 In[1]: name -opt foo bar
155 In[1]: name -opt foo bar
156
156
157 To call an alias without arguments, simply use ipalias('name').
157 To call an alias without arguments, simply use ipalias('name').
158
158
159 This provides a proper Python function to call IPython's aliases in any
159 This provides a proper Python function to call IPython's aliases in any
160 valid Python code you can type at the interpreter, including loops and
160 valid Python code you can type at the interpreter, including loops and
161 compound statements. It is added by IPython to the Python builtin
161 compound statements. It is added by IPython to the Python builtin
162 namespace upon initialization."""
162 namespace upon initialization."""
163
163
164 args = arg_s.split(' ',1)
164 args = arg_s.split(' ',1)
165 alias_name = args[0]
165 alias_name = args[0]
166 try:
166 try:
167 alias_args = args[1]
167 alias_args = args[1]
168 except IndexError:
168 except IndexError:
169 alias_args = ''
169 alias_args = ''
170 if alias_name in __IPYTHON__.alias_table:
170 if alias_name in __IPYTHON__.alias_table:
171 __IPYTHON__.call_alias(alias_name,alias_args)
171 __IPYTHON__.call_alias(alias_name,alias_args)
172 else:
172 else:
173 error("Alias `%s` not found." % alias_name)
173 error("Alias `%s` not found." % alias_name)
174
174
175 def softspace(file, newvalue):
175 def softspace(file, newvalue):
176 """Copied from code.py, to remove the dependency"""
176 """Copied from code.py, to remove the dependency"""
177 oldvalue = 0
177 oldvalue = 0
178 try:
178 try:
179 oldvalue = file.softspace
179 oldvalue = file.softspace
180 except AttributeError:
180 except AttributeError:
181 pass
181 pass
182 try:
182 try:
183 file.softspace = newvalue
183 file.softspace = newvalue
184 except (AttributeError, TypeError):
184 except (AttributeError, TypeError):
185 # "attribute-less object" or "read-only attributes"
185 # "attribute-less object" or "read-only attributes"
186 pass
186 pass
187 return oldvalue
187 return oldvalue
188
188
189
189
190 #****************************************************************************
190 #****************************************************************************
191 # Local use exceptions
191 # Local use exceptions
192 class SpaceInInput(exceptions.Exception): pass
192 class SpaceInInput(exceptions.Exception): pass
193
193
194 #****************************************************************************
194 #****************************************************************************
195 # Local use classes
195 # Local use classes
196 class Bunch: pass
196 class Bunch: pass
197
197
198 class InputList(list):
198 class InputList(list):
199 """Class to store user input.
199 """Class to store user input.
200
200
201 It's basically a list, but slices return a string instead of a list, thus
201 It's basically a list, but slices return a string instead of a list, thus
202 allowing things like (assuming 'In' is an instance):
202 allowing things like (assuming 'In' is an instance):
203
203
204 exec In[4:7]
204 exec In[4:7]
205
205
206 or
206 or
207
207
208 exec In[5:9] + In[14] + In[21:25]"""
208 exec In[5:9] + In[14] + In[21:25]"""
209
209
210 def __getslice__(self,i,j):
210 def __getslice__(self,i,j):
211 return ''.join(list.__getslice__(self,i,j))
211 return ''.join(list.__getslice__(self,i,j))
212
212
213 class SyntaxTB(ultraTB.ListTB):
213 class SyntaxTB(ultraTB.ListTB):
214 """Extension which holds some state: the last exception value"""
214 """Extension which holds some state: the last exception value"""
215
215
216 def __init__(self,color_scheme = 'NoColor'):
216 def __init__(self,color_scheme = 'NoColor'):
217 ultraTB.ListTB.__init__(self,color_scheme)
217 ultraTB.ListTB.__init__(self,color_scheme)
218 self.last_syntax_error = None
218 self.last_syntax_error = None
219
219
220 def __call__(self, etype, value, elist):
220 def __call__(self, etype, value, elist):
221 self.last_syntax_error = value
221 self.last_syntax_error = value
222 ultraTB.ListTB.__call__(self,etype,value,elist)
222 ultraTB.ListTB.__call__(self,etype,value,elist)
223
223
224 def clear_err_state(self):
224 def clear_err_state(self):
225 """Return the current error state and clear it"""
225 """Return the current error state and clear it"""
226 e = self.last_syntax_error
226 e = self.last_syntax_error
227 self.last_syntax_error = None
227 self.last_syntax_error = None
228 return e
228 return e
229
229
230 #****************************************************************************
230 #****************************************************************************
231 # Main IPython class
231 # Main IPython class
232 class InteractiveShell(Logger, Magic):
232 class InteractiveShell(Logger, Magic):
233 """An enhanced console for Python."""
233 """An enhanced console for Python."""
234
234
235 # class attribute to indicate whether the class supports threads or not.
236 # Subclasses with thread support should override this as needed.
237 isthreaded = False
238
235 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
239 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
236 user_ns = None,user_global_ns=None,banner2='',
240 user_ns = None,user_global_ns=None,banner2='',
237 custom_exceptions=((),None),embedded=False):
241 custom_exceptions=((),None),embedded=False):
238
242
239 # some minimal strict typechecks. For some core data structures, I
243 # some minimal strict typechecks. For some core data structures, I
240 # want actual basic python types, not just anything that looks like
244 # want actual basic python types, not just anything that looks like
241 # one. This is especially true for namespaces.
245 # one. This is especially true for namespaces.
242 for ns in (user_ns,user_global_ns):
246 for ns in (user_ns,user_global_ns):
243 if ns is not None and type(ns) != types.DictType:
247 if ns is not None and type(ns) != types.DictType:
244 raise TypeError,'namespace must be a dictionary'
248 raise TypeError,'namespace must be a dictionary'
245
249
246 # Put a reference to self in builtins so that any form of embedded or
250 # Put a reference to self in builtins so that any form of embedded or
247 # imported code can test for being inside IPython.
251 # imported code can test for being inside IPython.
248 __builtin__.__IPYTHON__ = self
252 __builtin__.__IPYTHON__ = self
249
253
250 # And load into builtins ipmagic/ipalias as well
254 # And load into builtins ipmagic/ipalias as well
251 __builtin__.ipmagic = ipmagic
255 __builtin__.ipmagic = ipmagic
252 __builtin__.ipalias = ipalias
256 __builtin__.ipalias = ipalias
253
257
254 # Add to __builtin__ other parts of IPython's public API
258 # Add to __builtin__ other parts of IPython's public API
255 __builtin__.ip_set_hook = self.set_hook
259 __builtin__.ip_set_hook = self.set_hook
256
260
257 # Keep in the builtins a flag for when IPython is active. We set it
261 # Keep in the builtins a flag for when IPython is active. We set it
258 # with setdefault so that multiple nested IPythons don't clobber one
262 # with setdefault so that multiple nested IPythons don't clobber one
259 # another. Each will increase its value by one upon being activated,
263 # another. Each will increase its value by one upon being activated,
260 # which also gives us a way to determine the nesting level.
264 # which also gives us a way to determine the nesting level.
261 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
265 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
262
266
263 # Do the intuitively correct thing for quit/exit: we remove the
267 # Do the intuitively correct thing for quit/exit: we remove the
264 # builtins if they exist, and our own prefilter routine will handle
268 # builtins if they exist, and our own prefilter routine will handle
265 # these special cases
269 # these special cases
266 try:
270 try:
267 del __builtin__.exit, __builtin__.quit
271 del __builtin__.exit, __builtin__.quit
268 except AttributeError:
272 except AttributeError:
269 pass
273 pass
270
274
275 # Store the actual shell's name
276 self.name = name
277
271 # We need to know whether the instance is meant for embedding, since
278 # We need to know whether the instance is meant for embedding, since
272 # global/local namespaces need to be handled differently in that case
279 # global/local namespaces need to be handled differently in that case
273 self.embedded = embedded
280 self.embedded = embedded
274
281
275 # command compiler
282 # command compiler
276 self.compile = codeop.CommandCompiler()
283 self.compile = codeop.CommandCompiler()
277
284
278 # User input buffer
285 # User input buffer
279 self.buffer = []
286 self.buffer = []
280
287
281 # Default name given in compilation of code
288 # Default name given in compilation of code
282 self.filename = '<ipython console>'
289 self.filename = '<ipython console>'
283
290
284 # Create the namespace where the user will operate. user_ns is
291 # Create the namespace where the user will operate. user_ns is
285 # normally the only one used, and it is passed to the exec calls as
292 # normally the only one used, and it is passed to the exec calls as
286 # the locals argument. But we do carry a user_global_ns namespace
293 # the locals argument. But we do carry a user_global_ns namespace
287 # given as the exec 'globals' argument, This is useful in embedding
294 # given as the exec 'globals' argument, This is useful in embedding
288 # situations where the ipython shell opens in a context where the
295 # situations where the ipython shell opens in a context where the
289 # distinction between locals and globals is meaningful.
296 # distinction between locals and globals is meaningful.
290
297
291 # FIXME. For some strange reason, __builtins__ is showing up at user
298 # FIXME. For some strange reason, __builtins__ is showing up at user
292 # level as a dict instead of a module. This is a manual fix, but I
299 # level as a dict instead of a module. This is a manual fix, but I
293 # should really track down where the problem is coming from. Alex
300 # should really track down where the problem is coming from. Alex
294 # Schmolck reported this problem first.
301 # Schmolck reported this problem first.
295
302
296 # A useful post by Alex Martelli on this topic:
303 # A useful post by Alex Martelli on this topic:
297 # Re: inconsistent value from __builtins__
304 # Re: inconsistent value from __builtins__
298 # Von: Alex Martelli <aleaxit@yahoo.com>
305 # Von: Alex Martelli <aleaxit@yahoo.com>
299 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
306 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
300 # Gruppen: comp.lang.python
307 # Gruppen: comp.lang.python
301 # Referenzen: 1
308 # Referenzen: 1
302
309
303 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
310 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
304 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
311 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
305 # > <type 'dict'>
312 # > <type 'dict'>
306 # > >>> print type(__builtins__)
313 # > >>> print type(__builtins__)
307 # > <type 'module'>
314 # > <type 'module'>
308 # > Is this difference in return value intentional?
315 # > Is this difference in return value intentional?
309
316
310 # Well, it's documented that '__builtins__' can be either a dictionary
317 # Well, it's documented that '__builtins__' can be either a dictionary
311 # or a module, and it's been that way for a long time. Whether it's
318 # or a module, and it's been that way for a long time. Whether it's
312 # intentional (or sensible), I don't know. In any case, the idea is
319 # intentional (or sensible), I don't know. In any case, the idea is
313 # that if you need to access the built-in namespace directly, you
320 # that if you need to access the built-in namespace directly, you
314 # should start with "import __builtin__" (note, no 's') which will
321 # should start with "import __builtin__" (note, no 's') which will
315 # definitely give you a module. Yeah, it's somewhat confusing:-(.
322 # definitely give you a module. Yeah, it's somewhat confusing:-(.
316
323
317 if user_ns is None:
324 if user_ns is None:
318 # Set __name__ to __main__ to better match the behavior of the
325 # Set __name__ to __main__ to better match the behavior of the
319 # normal interpreter.
326 # normal interpreter.
320 user_ns = {'__name__' :'__main__',
327 user_ns = {'__name__' :'__main__',
321 '__builtins__' : __builtin__,
328 '__builtins__' : __builtin__,
322 }
329 }
323
330
324 if user_global_ns is None:
331 if user_global_ns is None:
325 user_global_ns = {}
332 user_global_ns = {}
326
333
327 # Assign namespaces
334 # Assign namespaces
328 # This is the namespace where all normal user variables live
335 # This is the namespace where all normal user variables live
329 self.user_ns = user_ns
336 self.user_ns = user_ns
330 # Embedded instances require a separate namespace for globals.
337 # Embedded instances require a separate namespace for globals.
331 # Normally this one is unused by non-embedded instances.
338 # Normally this one is unused by non-embedded instances.
332 self.user_global_ns = user_global_ns
339 self.user_global_ns = user_global_ns
333 # A namespace to keep track of internal data structures to prevent
340 # A namespace to keep track of internal data structures to prevent
334 # them from cluttering user-visible stuff. Will be updated later
341 # them from cluttering user-visible stuff. Will be updated later
335 self.internal_ns = {}
342 self.internal_ns = {}
336
343
337 # Namespace of system aliases. Each entry in the alias
344 # Namespace of system aliases. Each entry in the alias
338 # table must be a 2-tuple of the form (N,name), where N is the number
345 # table must be a 2-tuple of the form (N,name), where N is the number
339 # of positional arguments of the alias.
346 # of positional arguments of the alias.
340 self.alias_table = {}
347 self.alias_table = {}
341
348
342 # A table holding all the namespaces IPython deals with, so that
349 # A table holding all the namespaces IPython deals with, so that
343 # introspection facilities can search easily.
350 # introspection facilities can search easily.
344 self.ns_table = {'user':user_ns,
351 self.ns_table = {'user':user_ns,
345 'user_global':user_global_ns,
352 'user_global':user_global_ns,
346 'alias':self.alias_table,
353 'alias':self.alias_table,
347 'internal':self.internal_ns,
354 'internal':self.internal_ns,
348 'builtin':__builtin__.__dict__
355 'builtin':__builtin__.__dict__
349 }
356 }
350
357
351 # The user namespace MUST have a pointer to the shell itself.
358 # The user namespace MUST have a pointer to the shell itself.
352 self.user_ns[name] = self
359 self.user_ns[name] = self
353
360
354 # We need to insert into sys.modules something that looks like a
361 # We need to insert into sys.modules something that looks like a
355 # module but which accesses the IPython namespace, for shelve and
362 # module but which accesses the IPython namespace, for shelve and
356 # pickle to work interactively. Normally they rely on getting
363 # pickle to work interactively. Normally they rely on getting
357 # everything out of __main__, but for embedding purposes each IPython
364 # everything out of __main__, but for embedding purposes each IPython
358 # instance has its own private namespace, so we can't go shoving
365 # instance has its own private namespace, so we can't go shoving
359 # everything into __main__.
366 # everything into __main__.
360
367
361 # note, however, that we should only do this for non-embedded
368 # note, however, that we should only do this for non-embedded
362 # ipythons, which really mimic the __main__.__dict__ with their own
369 # ipythons, which really mimic the __main__.__dict__ with their own
363 # namespace. Embedded instances, on the other hand, should not do
370 # namespace. Embedded instances, on the other hand, should not do
364 # this because they need to manage the user local/global namespaces
371 # this because they need to manage the user local/global namespaces
365 # only, but they live within a 'normal' __main__ (meaning, they
372 # only, but they live within a 'normal' __main__ (meaning, they
366 # shouldn't overtake the execution environment of the script they're
373 # shouldn't overtake the execution environment of the script they're
367 # embedded in).
374 # embedded in).
368
375
369 if not embedded:
376 if not embedded:
370 try:
377 try:
371 main_name = self.user_ns['__name__']
378 main_name = self.user_ns['__name__']
372 except KeyError:
379 except KeyError:
373 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
380 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
374 else:
381 else:
375 #print "pickle hack in place" # dbg
382 #print "pickle hack in place" # dbg
376 sys.modules[main_name] = FakeModule(self.user_ns)
383 sys.modules[main_name] = FakeModule(self.user_ns)
377
384
378 # List of input with multi-line handling.
385 # List of input with multi-line handling.
379 # Fill its zero entry, user counter starts at 1
386 # Fill its zero entry, user counter starts at 1
380 self.input_hist = InputList(['\n'])
387 self.input_hist = InputList(['\n'])
381
388
382 # list of visited directories
389 # list of visited directories
383 try:
390 try:
384 self.dir_hist = [os.getcwd()]
391 self.dir_hist = [os.getcwd()]
385 except IOError, e:
392 except IOError, e:
386 self.dir_hist = []
393 self.dir_hist = []
387
394
388 # dict of output history
395 # dict of output history
389 self.output_hist = {}
396 self.output_hist = {}
390
397
391 # dict of things NOT to alias (keywords, builtins and some magics)
398 # dict of things NOT to alias (keywords, builtins and some magics)
392 no_alias = {}
399 no_alias = {}
393 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
400 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
394 for key in keyword.kwlist + no_alias_magics:
401 for key in keyword.kwlist + no_alias_magics:
395 no_alias[key] = 1
402 no_alias[key] = 1
396 no_alias.update(__builtin__.__dict__)
403 no_alias.update(__builtin__.__dict__)
397 self.no_alias = no_alias
404 self.no_alias = no_alias
398
405
399 # make global variables for user access to these
406 # make global variables for user access to these
400 self.user_ns['_ih'] = self.input_hist
407 self.user_ns['_ih'] = self.input_hist
401 self.user_ns['_oh'] = self.output_hist
408 self.user_ns['_oh'] = self.output_hist
402 self.user_ns['_dh'] = self.dir_hist
409 self.user_ns['_dh'] = self.dir_hist
403
410
404 # user aliases to input and output histories
411 # user aliases to input and output histories
405 self.user_ns['In'] = self.input_hist
412 self.user_ns['In'] = self.input_hist
406 self.user_ns['Out'] = self.output_hist
413 self.user_ns['Out'] = self.output_hist
407
414
408 # Store the actual shell's name
409 self.name = name
410
411 # Object variable to store code object waiting execution. This is
415 # Object variable to store code object waiting execution. This is
412 # used mainly by the multithreaded shells, but it can come in handy in
416 # used mainly by the multithreaded shells, but it can come in handy in
413 # other situations. No need to use a Queue here, since it's a single
417 # other situations. No need to use a Queue here, since it's a single
414 # item which gets cleared once run.
418 # item which gets cleared once run.
415 self.code_to_run = None
419 self.code_to_run = None
416
420
417 # Job manager (for jobs run as background threads)
421 # Job manager (for jobs run as background threads)
418 self.jobs = BackgroundJobManager()
422 self.jobs = BackgroundJobManager()
419 # Put the job manager into builtins so it's always there.
423 # Put the job manager into builtins so it's always there.
420 __builtin__.jobs = self.jobs
424 __builtin__.jobs = self.jobs
421
425
422 # escapes for automatic behavior on the command line
426 # escapes for automatic behavior on the command line
423 self.ESC_SHELL = '!'
427 self.ESC_SHELL = '!'
424 self.ESC_HELP = '?'
428 self.ESC_HELP = '?'
425 self.ESC_MAGIC = '%'
429 self.ESC_MAGIC = '%'
426 self.ESC_QUOTE = ','
430 self.ESC_QUOTE = ','
427 self.ESC_QUOTE2 = ';'
431 self.ESC_QUOTE2 = ';'
428 self.ESC_PAREN = '/'
432 self.ESC_PAREN = '/'
429
433
430 # And their associated handlers
434 # And their associated handlers
431 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
435 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
432 self.ESC_QUOTE:self.handle_auto,
436 self.ESC_QUOTE:self.handle_auto,
433 self.ESC_QUOTE2:self.handle_auto,
437 self.ESC_QUOTE2:self.handle_auto,
434 self.ESC_MAGIC:self.handle_magic,
438 self.ESC_MAGIC:self.handle_magic,
435 self.ESC_HELP:self.handle_help,
439 self.ESC_HELP:self.handle_help,
436 self.ESC_SHELL:self.handle_shell_escape,
440 self.ESC_SHELL:self.handle_shell_escape,
437 }
441 }
438
442
439 # class initializations
443 # class initializations
440 Logger.__init__(self,log_ns = self.user_ns)
444 Logger.__init__(self,log_ns = self.user_ns)
441 Magic.__init__(self,self)
445 Magic.__init__(self,self)
442
446
443 # an ugly hack to get a pointer to the shell, so I can start writing
447 # an ugly hack to get a pointer to the shell, so I can start writing
444 # magic code via this pointer instead of the current mixin salad.
448 # magic code via this pointer instead of the current mixin salad.
445 Magic.set_shell(self,self)
449 Magic.set_shell(self,self)
446
450
447 # Python source parser/formatter for syntax highlighting
451 # Python source parser/formatter for syntax highlighting
448 pyformat = PyColorize.Parser().format
452 pyformat = PyColorize.Parser().format
449 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
453 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
450
454
451 # hooks holds pointers used for user-side customizations
455 # hooks holds pointers used for user-side customizations
452 self.hooks = Struct()
456 self.hooks = Struct()
453
457
454 # Set all default hooks, defined in the IPython.hooks module.
458 # Set all default hooks, defined in the IPython.hooks module.
455 hooks = IPython.hooks
459 hooks = IPython.hooks
456 for hook_name in hooks.__all__:
460 for hook_name in hooks.__all__:
457 self.set_hook(hook_name,getattr(hooks,hook_name))
461 self.set_hook(hook_name,getattr(hooks,hook_name))
458
462
459 # Flag to mark unconditional exit
463 # Flag to mark unconditional exit
460 self.exit_now = False
464 self.exit_now = False
461
465
462 self.usage_min = """\
466 self.usage_min = """\
463 An enhanced console for Python.
467 An enhanced console for Python.
464 Some of its features are:
468 Some of its features are:
465 - Readline support if the readline library is present.
469 - Readline support if the readline library is present.
466 - Tab completion in the local namespace.
470 - Tab completion in the local namespace.
467 - Logging of input, see command-line options.
471 - Logging of input, see command-line options.
468 - System shell escape via ! , eg !ls.
472 - System shell escape via ! , eg !ls.
469 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
473 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
470 - Keeps track of locally defined variables via %who, %whos.
474 - Keeps track of locally defined variables via %who, %whos.
471 - Show object information with a ? eg ?x or x? (use ?? for more info).
475 - Show object information with a ? eg ?x or x? (use ?? for more info).
472 """
476 """
473 if usage: self.usage = usage
477 if usage: self.usage = usage
474 else: self.usage = self.usage_min
478 else: self.usage = self.usage_min
475
479
476 # Storage
480 # Storage
477 self.rc = rc # This will hold all configuration information
481 self.rc = rc # This will hold all configuration information
478 self.inputcache = []
482 self.inputcache = []
479 self._boundcache = []
483 self._boundcache = []
480 self.pager = 'less'
484 self.pager = 'less'
481 # temporary files used for various purposes. Deleted at exit.
485 # temporary files used for various purposes. Deleted at exit.
482 self.tempfiles = []
486 self.tempfiles = []
483
487
484 # Keep track of readline usage (later set by init_readline)
488 # Keep track of readline usage (later set by init_readline)
485 self.has_readline = False
489 self.has_readline = False
486
490
487 # for pushd/popd management
491 # for pushd/popd management
488 try:
492 try:
489 self.home_dir = get_home_dir()
493 self.home_dir = get_home_dir()
490 except HomeDirError,msg:
494 except HomeDirError,msg:
491 fatal(msg)
495 fatal(msg)
492
496
493 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
497 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
494
498
495 # Functions to call the underlying shell.
499 # Functions to call the underlying shell.
496
500
497 # utility to expand user variables via Itpl
501 # utility to expand user variables via Itpl
498 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
502 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
499 self.user_ns))
503 self.user_ns))
500 # The first is similar to os.system, but it doesn't return a value,
504 # The first is similar to os.system, but it doesn't return a value,
501 # and it allows interpolation of variables in the user's namespace.
505 # and it allows interpolation of variables in the user's namespace.
502 self.system = lambda cmd: shell(self.var_expand(cmd),
506 self.system = lambda cmd: shell(self.var_expand(cmd),
503 header='IPython system call: ',
507 header='IPython system call: ',
504 verbose=self.rc.system_verbose)
508 verbose=self.rc.system_verbose)
505 # These are for getoutput and getoutputerror:
509 # These are for getoutput and getoutputerror:
506 self.getoutput = lambda cmd: \
510 self.getoutput = lambda cmd: \
507 getoutput(self.var_expand(cmd),
511 getoutput(self.var_expand(cmd),
508 header='IPython system call: ',
512 header='IPython system call: ',
509 verbose=self.rc.system_verbose)
513 verbose=self.rc.system_verbose)
510 self.getoutputerror = lambda cmd: \
514 self.getoutputerror = lambda cmd: \
511 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
515 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
512 self.user_ns)),
516 self.user_ns)),
513 header='IPython system call: ',
517 header='IPython system call: ',
514 verbose=self.rc.system_verbose)
518 verbose=self.rc.system_verbose)
515
519
516 # RegExp for splitting line contents into pre-char//first
520 # RegExp for splitting line contents into pre-char//first
517 # word-method//rest. For clarity, each group in on one line.
521 # word-method//rest. For clarity, each group in on one line.
518
522
519 # WARNING: update the regexp if the above escapes are changed, as they
523 # WARNING: update the regexp if the above escapes are changed, as they
520 # are hardwired in.
524 # are hardwired in.
521
525
522 # Don't get carried away with trying to make the autocalling catch too
526 # Don't get carried away with trying to make the autocalling catch too
523 # much: it's better to be conservative rather than to trigger hidden
527 # much: it's better to be conservative rather than to trigger hidden
524 # evals() somewhere and end up causing side effects.
528 # evals() somewhere and end up causing side effects.
525
529
526 self.line_split = re.compile(r'^([\s*,;/])'
530 self.line_split = re.compile(r'^([\s*,;/])'
527 r'([\?\w\.]+\w*\s*)'
531 r'([\?\w\.]+\w*\s*)'
528 r'(\(?.*$)')
532 r'(\(?.*$)')
529
533
530 # Original re, keep around for a while in case changes break something
534 # Original re, keep around for a while in case changes break something
531 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
535 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
532 # r'(\s*[\?\w\.]+\w*\s*)'
536 # r'(\s*[\?\w\.]+\w*\s*)'
533 # r'(\(?.*$)')
537 # r'(\(?.*$)')
534
538
535 # RegExp to identify potential function names
539 # RegExp to identify potential function names
536 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
540 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
537 # RegExp to exclude strings with this start from autocalling
541 # RegExp to exclude strings with this start from autocalling
538 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
542 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
539
543
540 # try to catch also methods for stuff in lists/tuples/dicts: off
544 # try to catch also methods for stuff in lists/tuples/dicts: off
541 # (experimental). For this to work, the line_split regexp would need
545 # (experimental). For this to work, the line_split regexp would need
542 # to be modified so it wouldn't break things at '['. That line is
546 # to be modified so it wouldn't break things at '['. That line is
543 # nasty enough that I shouldn't change it until I can test it _well_.
547 # nasty enough that I shouldn't change it until I can test it _well_.
544 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
548 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
545
549
546 # keep track of where we started running (mainly for crash post-mortem)
550 # keep track of where we started running (mainly for crash post-mortem)
547 self.starting_dir = os.getcwd()
551 self.starting_dir = os.getcwd()
548
552
549 # Attributes for Logger mixin class, make defaults here
553 # Attributes for Logger mixin class, make defaults here
550 self._dolog = False
554 self._dolog = False
551 self.LOG = ''
555 self.LOG = ''
552 self.LOGDEF = '.InteractiveShell.log'
556 self.LOGDEF = '.InteractiveShell.log'
553 self.LOGMODE = 'over'
557 self.LOGMODE = 'over'
554 self.LOGHEAD = Itpl(
558 self.LOGHEAD = Itpl(
555 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
559 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
556 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
560 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
557 #log# opts = $self.rc.opts
561 #log# opts = $self.rc.opts
558 #log# args = $self.rc.args
562 #log# args = $self.rc.args
559 #log# It is safe to make manual edits below here.
563 #log# It is safe to make manual edits below here.
560 #log#-----------------------------------------------------------------------
564 #log#-----------------------------------------------------------------------
561 """)
565 """)
562 # Various switches which can be set
566 # Various switches which can be set
563 self.CACHELENGTH = 5000 # this is cheap, it's just text
567 self.CACHELENGTH = 5000 # this is cheap, it's just text
564 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
568 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
565 self.banner2 = banner2
569 self.banner2 = banner2
566
570
567 # TraceBack handlers:
571 # TraceBack handlers:
568 # Need two, one for syntax errors and one for other exceptions.
572
573 # Syntax error handler.
569 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
574 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
575
570 # The interactive one is initialized with an offset, meaning we always
576 # The interactive one is initialized with an offset, meaning we always
571 # want to remove the topmost item in the traceback, which is our own
577 # want to remove the topmost item in the traceback, which is our own
572 # internal code. Valid modes: ['Plain','Context','Verbose']
578 # internal code. Valid modes: ['Plain','Context','Verbose']
573 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
579 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
574 color_scheme='NoColor',
580 color_scheme='NoColor',
575 tb_offset = 1)
581 tb_offset = 1)
582
583 # IPython itself shouldn't crash. This will produce a detailed
584 # post-mortem if it does. But we only install the crash handler for
585 # non-threaded shells, the threaded ones use a normal verbose reporter
586 # and lose the crash handler. This is because exceptions in the main
587 # thread (such as in GUI code) propagate directly to sys.excepthook,
588 # and there's no point in printing crash dumps for every user exception.
589 if self.isthreaded:
590 sys.excepthook = ultraTB.FormattedTB()
591 else:
592 from IPython import CrashHandler
593 sys.excepthook = CrashHandler.CrashHandler(self)
594
595 # The instance will store a pointer to this, so that runtime code
596 # (such as magics) can access it. This is because during the
597 # read-eval loop, it gets temporarily overwritten (to deal with GUI
598 # frameworks).
599 self.sys_excepthook = sys.excepthook
600
576 # and add any custom exception handlers the user may have specified
601 # and add any custom exception handlers the user may have specified
577 self.set_custom_exc(*custom_exceptions)
602 self.set_custom_exc(*custom_exceptions)
578
603
579 # Object inspector
604 # Object inspector
580 self.inspector = OInspect.Inspector(OInspect.InspectColors,
605 self.inspector = OInspect.Inspector(OInspect.InspectColors,
581 PyColorize.ANSICodeColors,
606 PyColorize.ANSICodeColors,
582 'NoColor')
607 'NoColor')
583 # indentation management
608 # indentation management
584 self.autoindent = False
609 self.autoindent = False
585 self.indent_current_nsp = 0
610 self.indent_current_nsp = 0
586 self.indent_current = '' # actual indent string
611 self.indent_current = '' # actual indent string
587
612
588 # Make some aliases automatically
613 # Make some aliases automatically
589 # Prepare list of shell aliases to auto-define
614 # Prepare list of shell aliases to auto-define
590 if os.name == 'posix':
615 if os.name == 'posix':
591 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
616 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
592 'mv mv -i','rm rm -i','cp cp -i',
617 'mv mv -i','rm rm -i','cp cp -i',
593 'cat cat','less less','clear clear',
618 'cat cat','less less','clear clear',
594 # a better ls
619 # a better ls
595 'ls ls -F',
620 'ls ls -F',
596 # long ls
621 # long ls
597 'll ls -lF',
622 'll ls -lF',
598 # color ls
623 # color ls
599 'lc ls -F -o --color',
624 'lc ls -F -o --color',
600 # ls normal files only
625 # ls normal files only
601 'lf ls -F -o --color %l | grep ^-',
626 'lf ls -F -o --color %l | grep ^-',
602 # ls symbolic links
627 # ls symbolic links
603 'lk ls -F -o --color %l | grep ^l',
628 'lk ls -F -o --color %l | grep ^l',
604 # directories or links to directories,
629 # directories or links to directories,
605 'ldir ls -F -o --color %l | grep /$',
630 'ldir ls -F -o --color %l | grep /$',
606 # things which are executable
631 # things which are executable
607 'lx ls -F -o --color %l | grep ^-..x',
632 'lx ls -F -o --color %l | grep ^-..x',
608 )
633 )
609 elif os.name in ['nt','dos']:
634 elif os.name in ['nt','dos']:
610 auto_alias = ('dir dir /on', 'ls dir /on',
635 auto_alias = ('dir dir /on', 'ls dir /on',
611 'ddir dir /ad /on', 'ldir dir /ad /on',
636 'ddir dir /ad /on', 'ldir dir /ad /on',
612 'mkdir mkdir','rmdir rmdir','echo echo',
637 'mkdir mkdir','rmdir rmdir','echo echo',
613 'ren ren','cls cls','copy copy')
638 'ren ren','cls cls','copy copy')
614 else:
639 else:
615 auto_alias = ()
640 auto_alias = ()
616 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
641 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
617 # Call the actual (public) initializer
642 # Call the actual (public) initializer
618 self.init_auto_alias()
643 self.init_auto_alias()
619 # end __init__
644 # end __init__
620
645
646 def post_config_initialization(self):
647 """Post configuration init method
648
649 This is called after the configuration files have been processed to
650 'finalize' the initialization."""
651
652 rc = self.rc
653
654 # Load readline proper
655 if rc.readline:
656 self.init_readline()
657
658 # Set user colors (don't do it in the constructor above so that it
659 # doesn't crash if colors option is invalid)
660 self.magic_colors(rc.colors)
661
662 # Load user aliases
663 for alias in rc.alias:
664 self.magic_alias(alias)
665
666 # dynamic data that survives through sessions
667 # XXX make the filename a config option?
668 persist_base = 'persist'
669 if rc.profile:
670 persist_base += '_%s' % rc.profile
671 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
672
673 try:
674 self.persist = pickle.load(file(self.persist_fname))
675 except:
676 self.persist = {}
677
621 def set_hook(self,name,hook):
678 def set_hook(self,name,hook):
622 """set_hook(name,hook) -> sets an internal IPython hook.
679 """set_hook(name,hook) -> sets an internal IPython hook.
623
680
624 IPython exposes some of its internal API as user-modifiable hooks. By
681 IPython exposes some of its internal API as user-modifiable hooks. By
625 resetting one of these hooks, you can modify IPython's behavior to
682 resetting one of these hooks, you can modify IPython's behavior to
626 call at runtime your own routines."""
683 call at runtime your own routines."""
627
684
628 # At some point in the future, this should validate the hook before it
685 # At some point in the future, this should validate the hook before it
629 # accepts it. Probably at least check that the hook takes the number
686 # accepts it. Probably at least check that the hook takes the number
630 # of args it's supposed to.
687 # of args it's supposed to.
631 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
688 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
632
689
633 def set_custom_exc(self,exc_tuple,handler):
690 def set_custom_exc(self,exc_tuple,handler):
634 """set_custom_exc(exc_tuple,handler)
691 """set_custom_exc(exc_tuple,handler)
635
692
636 Set a custom exception handler, which will be called if any of the
693 Set a custom exception handler, which will be called if any of the
637 exceptions in exc_tuple occur in the mainloop (specifically, in the
694 exceptions in exc_tuple occur in the mainloop (specifically, in the
638 runcode() method.
695 runcode() method.
639
696
640 Inputs:
697 Inputs:
641
698
642 - exc_tuple: a *tuple* of valid exceptions to call the defined
699 - exc_tuple: a *tuple* of valid exceptions to call the defined
643 handler for. It is very important that you use a tuple, and NOT A
700 handler for. It is very important that you use a tuple, and NOT A
644 LIST here, because of the way Python's except statement works. If
701 LIST here, because of the way Python's except statement works. If
645 you only want to trap a single exception, use a singleton tuple:
702 you only want to trap a single exception, use a singleton tuple:
646
703
647 exc_tuple == (MyCustomException,)
704 exc_tuple == (MyCustomException,)
648
705
649 - handler: this must be defined as a function with the following
706 - handler: this must be defined as a function with the following
650 basic interface: def my_handler(self,etype,value,tb).
707 basic interface: def my_handler(self,etype,value,tb).
651
708
652 This will be made into an instance method (via new.instancemethod)
709 This will be made into an instance method (via new.instancemethod)
653 of IPython itself, and it will be called if any of the exceptions
710 of IPython itself, and it will be called if any of the exceptions
654 listed in the exc_tuple are caught. If the handler is None, an
711 listed in the exc_tuple are caught. If the handler is None, an
655 internal basic one is used, which just prints basic info.
712 internal basic one is used, which just prints basic info.
656
713
657 WARNING: by putting in your own exception handler into IPython's main
714 WARNING: by putting in your own exception handler into IPython's main
658 execution loop, you run a very good chance of nasty crashes. This
715 execution loop, you run a very good chance of nasty crashes. This
659 facility should only be used if you really know what you are doing."""
716 facility should only be used if you really know what you are doing."""
660
717
661 assert type(exc_tuple)==type(()) , \
718 assert type(exc_tuple)==type(()) , \
662 "The custom exceptions must be given AS A TUPLE."
719 "The custom exceptions must be given AS A TUPLE."
663
720
664 def dummy_handler(self,etype,value,tb):
721 def dummy_handler(self,etype,value,tb):
665 print '*** Simple custom exception handler ***'
722 print '*** Simple custom exception handler ***'
666 print 'Exception type :',etype
723 print 'Exception type :',etype
667 print 'Exception value:',value
724 print 'Exception value:',value
668 print 'Traceback :',tb
725 print 'Traceback :',tb
669 print 'Source code :','\n'.join(self.buffer)
726 print 'Source code :','\n'.join(self.buffer)
670
727
671 if handler is None: handler = dummy_handler
728 if handler is None: handler = dummy_handler
672
729
673 self.CustomTB = new.instancemethod(handler,self,self.__class__)
730 self.CustomTB = new.instancemethod(handler,self,self.__class__)
674 self.custom_exceptions = exc_tuple
731 self.custom_exceptions = exc_tuple
675
732
676 def set_custom_completer(self,completer,pos=0):
733 def set_custom_completer(self,completer,pos=0):
677 """set_custom_completer(completer,pos=0)
734 """set_custom_completer(completer,pos=0)
678
735
679 Adds a new custom completer function.
736 Adds a new custom completer function.
680
737
681 The position argument (defaults to 0) is the index in the completers
738 The position argument (defaults to 0) is the index in the completers
682 list where you want the completer to be inserted."""
739 list where you want the completer to be inserted."""
683
740
684 newcomp = new.instancemethod(completer,self.Completer,
741 newcomp = new.instancemethod(completer,self.Completer,
685 self.Completer.__class__)
742 self.Completer.__class__)
686 self.Completer.matchers.insert(pos,newcomp)
743 self.Completer.matchers.insert(pos,newcomp)
687
744
688 def complete(self,text):
745 def complete(self,text):
689 """Return a sorted list of all possible completions on text.
746 """Return a sorted list of all possible completions on text.
690
747
691 Inputs:
748 Inputs:
692
749
693 - text: a string of text to be completed on.
750 - text: a string of text to be completed on.
694
751
695 This is a wrapper around the completion mechanism, similar to what
752 This is a wrapper around the completion mechanism, similar to what
696 readline does at the command line when the TAB key is hit. By
753 readline does at the command line when the TAB key is hit. By
697 exposing it as a method, it can be used by other non-readline
754 exposing it as a method, it can be used by other non-readline
698 environments (such as GUIs) for text completion.
755 environments (such as GUIs) for text completion.
699
756
700 Simple usage example:
757 Simple usage example:
701
758
702 In [1]: x = 'hello'
759 In [1]: x = 'hello'
703
760
704 In [2]: __IP.complete('x.l')
761 In [2]: __IP.complete('x.l')
705 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
762 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
706
763
707 complete = self.Completer.complete
764 complete = self.Completer.complete
708 state = 0
765 state = 0
709 # use a dict so we get unique keys, since ipyhton's multiple
766 # use a dict so we get unique keys, since ipyhton's multiple
710 # completers can return duplicates.
767 # completers can return duplicates.
711 comps = {}
768 comps = {}
712 while True:
769 while True:
713 newcomp = complete(text,state)
770 newcomp = complete(text,state)
714 if newcomp is None:
771 if newcomp is None:
715 break
772 break
716 comps[newcomp] = 1
773 comps[newcomp] = 1
717 state += 1
774 state += 1
718 outcomps = comps.keys()
775 outcomps = comps.keys()
719 outcomps.sort()
776 outcomps.sort()
720 return outcomps
777 return outcomps
721
778
722 def set_completer_frame(self, frame):
779 def set_completer_frame(self, frame):
723 if frame:
780 if frame:
724 self.Completer.namespace = frame.f_locals
781 self.Completer.namespace = frame.f_locals
725 self.Completer.global_namespace = frame.f_globals
782 self.Completer.global_namespace = frame.f_globals
726 else:
783 else:
727 self.Completer.namespace = self.user_ns
784 self.Completer.namespace = self.user_ns
728 self.Completer.global_namespace = self.user_global_ns
785 self.Completer.global_namespace = self.user_global_ns
729
786
730 def post_config_initialization(self):
731 """Post configuration init method
732
733 This is called after the configuration files have been processed to
734 'finalize' the initialization."""
735
736 rc = self.rc
737
738 # Load readline proper
739 if rc.readline:
740 self.init_readline()
741
742 # Set user colors (don't do it in the constructor above so that it
743 # doesn't crash if colors option is invalid)
744 self.magic_colors(rc.colors)
745
746 # Load user aliases
747 for alias in rc.alias:
748 self.magic_alias(alias)
749
750 # dynamic data that survives through sessions
751 # XXX make the filename a config option?
752 persist_base = 'persist'
753 if rc.profile:
754 persist_base += '_%s' % rc.profile
755 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
756
757 try:
758 self.persist = pickle.load(file(self.persist_fname))
759 except:
760 self.persist = {}
761
762 def init_auto_alias(self):
787 def init_auto_alias(self):
763 """Define some aliases automatically.
788 """Define some aliases automatically.
764
789
765 These are ALL parameter-less aliases"""
790 These are ALL parameter-less aliases"""
766 for alias,cmd in self.auto_alias:
791 for alias,cmd in self.auto_alias:
767 self.alias_table[alias] = (0,cmd)
792 self.alias_table[alias] = (0,cmd)
768
793
769 def alias_table_validate(self,verbose=0):
794 def alias_table_validate(self,verbose=0):
770 """Update information about the alias table.
795 """Update information about the alias table.
771
796
772 In particular, make sure no Python keywords/builtins are in it."""
797 In particular, make sure no Python keywords/builtins are in it."""
773
798
774 no_alias = self.no_alias
799 no_alias = self.no_alias
775 for k in self.alias_table.keys():
800 for k in self.alias_table.keys():
776 if k in no_alias:
801 if k in no_alias:
777 del self.alias_table[k]
802 del self.alias_table[k]
778 if verbose:
803 if verbose:
779 print ("Deleting alias <%s>, it's a Python "
804 print ("Deleting alias <%s>, it's a Python "
780 "keyword or builtin." % k)
805 "keyword or builtin." % k)
781
806
782 def set_autoindent(self,value=None):
807 def set_autoindent(self,value=None):
783 """Set the autoindent flag, checking for readline support.
808 """Set the autoindent flag, checking for readline support.
784
809
785 If called with no arguments, it acts as a toggle."""
810 If called with no arguments, it acts as a toggle."""
786
811
787 if not self.has_readline:
812 if not self.has_readline:
788 if os.name == 'posix':
813 if os.name == 'posix':
789 warn("The auto-indent feature requires the readline library")
814 warn("The auto-indent feature requires the readline library")
790 self.autoindent = 0
815 self.autoindent = 0
791 return
816 return
792 if value is None:
817 if value is None:
793 self.autoindent = not self.autoindent
818 self.autoindent = not self.autoindent
794 else:
819 else:
795 self.autoindent = value
820 self.autoindent = value
796
821
797 def rc_set_toggle(self,rc_field,value=None):
822 def rc_set_toggle(self,rc_field,value=None):
798 """Set or toggle a field in IPython's rc config. structure.
823 """Set or toggle a field in IPython's rc config. structure.
799
824
800 If called with no arguments, it acts as a toggle.
825 If called with no arguments, it acts as a toggle.
801
826
802 If called with a non-existent field, the resulting AttributeError
827 If called with a non-existent field, the resulting AttributeError
803 exception will propagate out."""
828 exception will propagate out."""
804
829
805 rc_val = getattr(self.rc,rc_field)
830 rc_val = getattr(self.rc,rc_field)
806 if value is None:
831 if value is None:
807 value = not rc_val
832 value = not rc_val
808 setattr(self.rc,rc_field,value)
833 setattr(self.rc,rc_field,value)
809
834
810 def user_setup(self,ipythondir,rc_suffix,mode='install'):
835 def user_setup(self,ipythondir,rc_suffix,mode='install'):
811 """Install the user configuration directory.
836 """Install the user configuration directory.
812
837
813 Can be called when running for the first time or to upgrade the user's
838 Can be called when running for the first time or to upgrade the user's
814 .ipython/ directory with the mode parameter. Valid modes are 'install'
839 .ipython/ directory with the mode parameter. Valid modes are 'install'
815 and 'upgrade'."""
840 and 'upgrade'."""
816
841
817 def wait():
842 def wait():
818 try:
843 try:
819 raw_input("Please press <RETURN> to start IPython.")
844 raw_input("Please press <RETURN> to start IPython.")
820 except EOFError:
845 except EOFError:
821 print >> Term.cout
846 print >> Term.cout
822 print '*'*70
847 print '*'*70
823
848
824 cwd = os.getcwd() # remember where we started
849 cwd = os.getcwd() # remember where we started
825 glb = glob.glob
850 glb = glob.glob
826 print '*'*70
851 print '*'*70
827 if mode == 'install':
852 if mode == 'install':
828 print \
853 print \
829 """Welcome to IPython. I will try to create a personal configuration directory
854 """Welcome to IPython. I will try to create a personal configuration directory
830 where you can customize many aspects of IPython's functionality in:\n"""
855 where you can customize many aspects of IPython's functionality in:\n"""
831 else:
856 else:
832 print 'I am going to upgrade your configuration in:'
857 print 'I am going to upgrade your configuration in:'
833
858
834 print ipythondir
859 print ipythondir
835
860
836 rcdirend = os.path.join('IPython','UserConfig')
861 rcdirend = os.path.join('IPython','UserConfig')
837 cfg = lambda d: os.path.join(d,rcdirend)
862 cfg = lambda d: os.path.join(d,rcdirend)
838 try:
863 try:
839 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
864 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
840 except IOError:
865 except IOError:
841 warning = """
866 warning = """
842 Installation error. IPython's directory was not found.
867 Installation error. IPython's directory was not found.
843
868
844 Check the following:
869 Check the following:
845
870
846 The ipython/IPython directory should be in a directory belonging to your
871 The ipython/IPython directory should be in a directory belonging to your
847 PYTHONPATH environment variable (that is, it should be in a directory
872 PYTHONPATH environment variable (that is, it should be in a directory
848 belonging to sys.path). You can copy it explicitly there or just link to it.
873 belonging to sys.path). You can copy it explicitly there or just link to it.
849
874
850 IPython will proceed with builtin defaults.
875 IPython will proceed with builtin defaults.
851 """
876 """
852 warn(warning)
877 warn(warning)
853 wait()
878 wait()
854 return
879 return
855
880
856 if mode == 'install':
881 if mode == 'install':
857 try:
882 try:
858 shutil.copytree(rcdir,ipythondir)
883 shutil.copytree(rcdir,ipythondir)
859 os.chdir(ipythondir)
884 os.chdir(ipythondir)
860 rc_files = glb("ipythonrc*")
885 rc_files = glb("ipythonrc*")
861 for rc_file in rc_files:
886 for rc_file in rc_files:
862 os.rename(rc_file,rc_file+rc_suffix)
887 os.rename(rc_file,rc_file+rc_suffix)
863 except:
888 except:
864 warning = """
889 warning = """
865
890
866 There was a problem with the installation:
891 There was a problem with the installation:
867 %s
892 %s
868 Try to correct it or contact the developers if you think it's a bug.
893 Try to correct it or contact the developers if you think it's a bug.
869 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
894 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
870 warn(warning)
895 warn(warning)
871 wait()
896 wait()
872 return
897 return
873
898
874 elif mode == 'upgrade':
899 elif mode == 'upgrade':
875 try:
900 try:
876 os.chdir(ipythondir)
901 os.chdir(ipythondir)
877 except:
902 except:
878 print """
903 print """
879 Can not upgrade: changing to directory %s failed. Details:
904 Can not upgrade: changing to directory %s failed. Details:
880 %s
905 %s
881 """ % (ipythondir,sys.exc_info()[1])
906 """ % (ipythondir,sys.exc_info()[1])
882 wait()
907 wait()
883 return
908 return
884 else:
909 else:
885 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
910 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
886 for new_full_path in sources:
911 for new_full_path in sources:
887 new_filename = os.path.basename(new_full_path)
912 new_filename = os.path.basename(new_full_path)
888 if new_filename.startswith('ipythonrc'):
913 if new_filename.startswith('ipythonrc'):
889 new_filename = new_filename + rc_suffix
914 new_filename = new_filename + rc_suffix
890 # The config directory should only contain files, skip any
915 # The config directory should only contain files, skip any
891 # directories which may be there (like CVS)
916 # directories which may be there (like CVS)
892 if os.path.isdir(new_full_path):
917 if os.path.isdir(new_full_path):
893 continue
918 continue
894 if os.path.exists(new_filename):
919 if os.path.exists(new_filename):
895 old_file = new_filename+'.old'
920 old_file = new_filename+'.old'
896 if os.path.exists(old_file):
921 if os.path.exists(old_file):
897 os.remove(old_file)
922 os.remove(old_file)
898 os.rename(new_filename,old_file)
923 os.rename(new_filename,old_file)
899 shutil.copy(new_full_path,new_filename)
924 shutil.copy(new_full_path,new_filename)
900 else:
925 else:
901 raise ValueError,'unrecognized mode for install:',`mode`
926 raise ValueError,'unrecognized mode for install:',`mode`
902
927
903 # Fix line-endings to those native to each platform in the config
928 # Fix line-endings to those native to each platform in the config
904 # directory.
929 # directory.
905 try:
930 try:
906 os.chdir(ipythondir)
931 os.chdir(ipythondir)
907 except:
932 except:
908 print """
933 print """
909 Problem: changing to directory %s failed.
934 Problem: changing to directory %s failed.
910 Details:
935 Details:
911 %s
936 %s
912
937
913 Some configuration files may have incorrect line endings. This should not
938 Some configuration files may have incorrect line endings. This should not
914 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
939 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
915 wait()
940 wait()
916 else:
941 else:
917 for fname in glb('ipythonrc*'):
942 for fname in glb('ipythonrc*'):
918 try:
943 try:
919 native_line_ends(fname,backup=0)
944 native_line_ends(fname,backup=0)
920 except IOError:
945 except IOError:
921 pass
946 pass
922
947
923 if mode == 'install':
948 if mode == 'install':
924 print """
949 print """
925 Successful installation!
950 Successful installation!
926
951
927 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
952 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
928 IPython manual (there are both HTML and PDF versions supplied with the
953 IPython manual (there are both HTML and PDF versions supplied with the
929 distribution) to make sure that your system environment is properly configured
954 distribution) to make sure that your system environment is properly configured
930 to take advantage of IPython's features."""
955 to take advantage of IPython's features."""
931 else:
956 else:
932 print """
957 print """
933 Successful upgrade!
958 Successful upgrade!
934
959
935 All files in your directory:
960 All files in your directory:
936 %(ipythondir)s
961 %(ipythondir)s
937 which would have been overwritten by the upgrade were backed up with a .old
962 which would have been overwritten by the upgrade were backed up with a .old
938 extension. If you had made particular customizations in those files you may
963 extension. If you had made particular customizations in those files you may
939 want to merge them back into the new files.""" % locals()
964 want to merge them back into the new files.""" % locals()
940 wait()
965 wait()
941 os.chdir(cwd)
966 os.chdir(cwd)
942 # end user_setup()
967 # end user_setup()
943
968
944 def atexit_operations(self):
969 def atexit_operations(self):
945 """This will be executed at the time of exit.
970 """This will be executed at the time of exit.
946
971
947 Saving of persistent data should be performed here. """
972 Saving of persistent data should be performed here. """
948
973
949 # input history
974 # input history
950 self.savehist()
975 self.savehist()
951
976
952 # Cleanup all tempfiles left around
977 # Cleanup all tempfiles left around
953 for tfile in self.tempfiles:
978 for tfile in self.tempfiles:
954 try:
979 try:
955 os.unlink(tfile)
980 os.unlink(tfile)
956 except OSError:
981 except OSError:
957 pass
982 pass
958
983
959 # save the "persistent data" catch-all dictionary
984 # save the "persistent data" catch-all dictionary
960 try:
985 try:
961 pickle.dump(self.persist, open(self.persist_fname,"w"))
986 pickle.dump(self.persist, open(self.persist_fname,"w"))
962 except:
987 except:
963 print "*** ERROR *** persistent data saving failed."
988 print "*** ERROR *** persistent data saving failed."
964
989
965 def savehist(self):
990 def savehist(self):
966 """Save input history to a file (via readline library)."""
991 """Save input history to a file (via readline library)."""
967 try:
992 try:
968 self.readline.write_history_file(self.histfile)
993 self.readline.write_history_file(self.histfile)
969 except:
994 except:
970 print 'Unable to save IPython command history to file: ' + \
995 print 'Unable to save IPython command history to file: ' + \
971 `self.histfile`
996 `self.histfile`
972
997
973 def pre_readline(self):
998 def pre_readline(self):
974 """readline hook to be used at the start of each line.
999 """readline hook to be used at the start of each line.
975
1000
976 Currently it handles auto-indent only."""
1001 Currently it handles auto-indent only."""
977
1002
978 self.readline.insert_text(self.indent_current)
1003 self.readline.insert_text(self.indent_current)
979
1004
980 def init_readline(self):
1005 def init_readline(self):
981 """Command history completion/saving/reloading."""
1006 """Command history completion/saving/reloading."""
982 try:
1007 try:
983 import readline
1008 import readline
984 except ImportError:
1009 except ImportError:
985 self.has_readline = 0
1010 self.has_readline = 0
986 self.readline = None
1011 self.readline = None
987 # no point in bugging windows users with this every time:
1012 # no point in bugging windows users with this every time:
988 if os.name == 'posix':
1013 if os.name == 'posix':
989 warn('Readline services not available on this platform.')
1014 warn('Readline services not available on this platform.')
990 else:
1015 else:
991 import atexit
1016 import atexit
992 from IPython.completer import IPCompleter
1017 from IPython.completer import IPCompleter
993 self.Completer = IPCompleter(self,
1018 self.Completer = IPCompleter(self,
994 self.user_ns,
1019 self.user_ns,
995 self.user_global_ns,
1020 self.user_global_ns,
996 self.rc.readline_omit__names,
1021 self.rc.readline_omit__names,
997 self.alias_table)
1022 self.alias_table)
998
1023
999 # Platform-specific configuration
1024 # Platform-specific configuration
1000 if os.name == 'nt':
1025 if os.name == 'nt':
1001 self.readline_startup_hook = readline.set_pre_input_hook
1026 self.readline_startup_hook = readline.set_pre_input_hook
1002 else:
1027 else:
1003 self.readline_startup_hook = readline.set_startup_hook
1028 self.readline_startup_hook = readline.set_startup_hook
1004
1029
1005 # Load user's initrc file (readline config)
1030 # Load user's initrc file (readline config)
1006 inputrc_name = os.environ.get('INPUTRC')
1031 inputrc_name = os.environ.get('INPUTRC')
1007 if inputrc_name is None:
1032 if inputrc_name is None:
1008 home_dir = get_home_dir()
1033 home_dir = get_home_dir()
1009 if home_dir is not None:
1034 if home_dir is not None:
1010 inputrc_name = os.path.join(home_dir,'.inputrc')
1035 inputrc_name = os.path.join(home_dir,'.inputrc')
1011 if os.path.isfile(inputrc_name):
1036 if os.path.isfile(inputrc_name):
1012 try:
1037 try:
1013 readline.read_init_file(inputrc_name)
1038 readline.read_init_file(inputrc_name)
1014 except:
1039 except:
1015 warn('Problems reading readline initialization file <%s>'
1040 warn('Problems reading readline initialization file <%s>'
1016 % inputrc_name)
1041 % inputrc_name)
1017
1042
1018 self.has_readline = 1
1043 self.has_readline = 1
1019 self.readline = readline
1044 self.readline = readline
1020 # save this in sys so embedded copies can restore it properly
1045 # save this in sys so embedded copies can restore it properly
1021 sys.ipcompleter = self.Completer.complete
1046 sys.ipcompleter = self.Completer.complete
1022 readline.set_completer(self.Completer.complete)
1047 readline.set_completer(self.Completer.complete)
1023
1048
1024 # Configure readline according to user's prefs
1049 # Configure readline according to user's prefs
1025 for rlcommand in self.rc.readline_parse_and_bind:
1050 for rlcommand in self.rc.readline_parse_and_bind:
1026 readline.parse_and_bind(rlcommand)
1051 readline.parse_and_bind(rlcommand)
1027
1052
1028 # remove some chars from the delimiters list
1053 # remove some chars from the delimiters list
1029 delims = readline.get_completer_delims()
1054 delims = readline.get_completer_delims()
1030 delims = delims.translate(string._idmap,
1055 delims = delims.translate(string._idmap,
1031 self.rc.readline_remove_delims)
1056 self.rc.readline_remove_delims)
1032 readline.set_completer_delims(delims)
1057 readline.set_completer_delims(delims)
1033 # otherwise we end up with a monster history after a while:
1058 # otherwise we end up with a monster history after a while:
1034 readline.set_history_length(1000)
1059 readline.set_history_length(1000)
1035 try:
1060 try:
1036 #print '*** Reading readline history' # dbg
1061 #print '*** Reading readline history' # dbg
1037 readline.read_history_file(self.histfile)
1062 readline.read_history_file(self.histfile)
1038 except IOError:
1063 except IOError:
1039 pass # It doesn't exist yet.
1064 pass # It doesn't exist yet.
1040
1065
1041 atexit.register(self.atexit_operations)
1066 atexit.register(self.atexit_operations)
1042 del atexit
1067 del atexit
1043
1068
1044 # Configure auto-indent for all platforms
1069 # Configure auto-indent for all platforms
1045 self.set_autoindent(self.rc.autoindent)
1070 self.set_autoindent(self.rc.autoindent)
1046
1071
1047 def _should_recompile(self,e):
1072 def _should_recompile(self,e):
1048 """Utility routine for edit_syntax_error"""
1073 """Utility routine for edit_syntax_error"""
1049
1074
1050 if e.filename in ('<ipython console>','<input>','<string>',
1075 if e.filename in ('<ipython console>','<input>','<string>',
1051 '<console>'):
1076 '<console>'):
1052 return False
1077 return False
1053 try:
1078 try:
1054 if not ask_yes_no('Return to editor to correct syntax error? '
1079 if not ask_yes_no('Return to editor to correct syntax error? '
1055 '[Y/n] ','y'):
1080 '[Y/n] ','y'):
1056 return False
1081 return False
1057 except EOFError:
1082 except EOFError:
1058 return False
1083 return False
1059 self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg)
1084 self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg)
1060 return True
1085 return True
1061
1086
1062 def edit_syntax_error(self):
1087 def edit_syntax_error(self):
1063 """The bottom half of the syntax error handler called in the main loop.
1088 """The bottom half of the syntax error handler called in the main loop.
1064
1089
1065 Loop until syntax error is fixed or user cancels.
1090 Loop until syntax error is fixed or user cancels.
1066 """
1091 """
1067
1092
1068 while self.SyntaxTB.last_syntax_error:
1093 while self.SyntaxTB.last_syntax_error:
1069 # copy and clear last_syntax_error
1094 # copy and clear last_syntax_error
1070 err = self.SyntaxTB.clear_err_state()
1095 err = self.SyntaxTB.clear_err_state()
1071 if not self._should_recompile(err):
1096 if not self._should_recompile(err):
1072 return
1097 return
1073 try:
1098 try:
1074 # may set last_syntax_error again if a SyntaxError is raised
1099 # may set last_syntax_error again if a SyntaxError is raised
1075 self.safe_execfile(err.filename,self.shell.user_ns)
1100 self.safe_execfile(err.filename,self.shell.user_ns)
1076 except:
1101 except:
1077 self.showtraceback()
1102 self.showtraceback()
1078 else:
1103 else:
1079 f = file(err.filename)
1104 f = file(err.filename)
1080 try:
1105 try:
1081 sys.displayhook(f.read())
1106 sys.displayhook(f.read())
1082 finally:
1107 finally:
1083 f.close()
1108 f.close()
1084
1109
1085 def showsyntaxerror(self, filename=None):
1110 def showsyntaxerror(self, filename=None):
1086 """Display the syntax error that just occurred.
1111 """Display the syntax error that just occurred.
1087
1112
1088 This doesn't display a stack trace because there isn't one.
1113 This doesn't display a stack trace because there isn't one.
1089
1114
1090 If a filename is given, it is stuffed in the exception instead
1115 If a filename is given, it is stuffed in the exception instead
1091 of what was there before (because Python's parser always uses
1116 of what was there before (because Python's parser always uses
1092 "<string>" when reading from a string).
1117 "<string>" when reading from a string).
1093 """
1118 """
1094 type, value, sys.last_traceback = sys.exc_info()
1119 type, value, sys.last_traceback = sys.exc_info()
1095 sys.last_type = type
1120 sys.last_type = type
1096 sys.last_value = value
1121 sys.last_value = value
1097 if filename and type is SyntaxError:
1122 if filename and type is SyntaxError:
1098 # Work hard to stuff the correct filename in the exception
1123 # Work hard to stuff the correct filename in the exception
1099 try:
1124 try:
1100 msg, (dummy_filename, lineno, offset, line) = value
1125 msg, (dummy_filename, lineno, offset, line) = value
1101 except:
1126 except:
1102 # Not the format we expect; leave it alone
1127 # Not the format we expect; leave it alone
1103 pass
1128 pass
1104 else:
1129 else:
1105 # Stuff in the right filename
1130 # Stuff in the right filename
1106 try:
1131 try:
1107 # Assume SyntaxError is a class exception
1132 # Assume SyntaxError is a class exception
1108 value = SyntaxError(msg, (filename, lineno, offset, line))
1133 value = SyntaxError(msg, (filename, lineno, offset, line))
1109 except:
1134 except:
1110 # If that failed, assume SyntaxError is a string
1135 # If that failed, assume SyntaxError is a string
1111 value = msg, (filename, lineno, offset, line)
1136 value = msg, (filename, lineno, offset, line)
1112 self.SyntaxTB(type,value,[])
1137 self.SyntaxTB(type,value,[])
1113
1138
1114 def debugger(self):
1139 def debugger(self):
1115 """Call the pdb debugger."""
1140 """Call the pdb debugger."""
1116
1141
1117 if not self.rc.pdb:
1142 if not self.rc.pdb:
1118 return
1143 return
1119 pdb.pm()
1144 pdb.pm()
1120
1145
1121 def showtraceback(self,exc_tuple = None,filename=None):
1146 def showtraceback(self,exc_tuple = None,filename=None):
1122 """Display the exception that just occurred."""
1147 """Display the exception that just occurred."""
1123
1148
1124 # Though this won't be called by syntax errors in the input line,
1149 # Though this won't be called by syntax errors in the input line,
1125 # there may be SyntaxError cases whith imported code.
1150 # there may be SyntaxError cases whith imported code.
1126 if exc_tuple is None:
1151 if exc_tuple is None:
1127 type, value, tb = sys.exc_info()
1152 type, value, tb = sys.exc_info()
1128 else:
1153 else:
1129 type, value, tb = exc_tuple
1154 type, value, tb = exc_tuple
1130 if type is SyntaxError:
1155 if type is SyntaxError:
1131 self.showsyntaxerror(filename)
1156 self.showsyntaxerror(filename)
1132 else:
1157 else:
1133 sys.last_type = type
1158 sys.last_type = type
1134 sys.last_value = value
1159 sys.last_value = value
1135 sys.last_traceback = tb
1160 sys.last_traceback = tb
1136 self.InteractiveTB()
1161 self.InteractiveTB()
1137 if self.InteractiveTB.call_pdb and self.has_readline:
1162 if self.InteractiveTB.call_pdb and self.has_readline:
1138 # pdb mucks up readline, fix it back
1163 # pdb mucks up readline, fix it back
1139 self.readline.set_completer(self.Completer.complete)
1164 self.readline.set_completer(self.Completer.complete)
1140
1165
1141 def update_cache(self, line):
1166 def update_cache(self, line):
1142 """puts line into cache"""
1167 """puts line into cache"""
1143 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1168 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1144 if len(self.inputcache) >= self.CACHELENGTH:
1169 if len(self.inputcache) >= self.CACHELENGTH:
1145 self.inputcache.pop() # This doesn't :-)
1170 self.inputcache.pop() # This doesn't :-)
1146
1171
1147 def mainloop(self,banner=None):
1172 def mainloop(self,banner=None):
1148 """Creates the local namespace and starts the mainloop.
1173 """Creates the local namespace and starts the mainloop.
1149
1174
1150 If an optional banner argument is given, it will override the
1175 If an optional banner argument is given, it will override the
1151 internally created default banner."""
1176 internally created default banner."""
1152
1177
1153 if self.rc.c: # Emulate Python's -c option
1178 if self.rc.c: # Emulate Python's -c option
1154 self.exec_init_cmd()
1179 self.exec_init_cmd()
1155 if banner is None:
1180 if banner is None:
1156 if self.rc.banner:
1181 if self.rc.banner:
1157 banner = self.BANNER+self.banner2
1182 banner = self.BANNER+self.banner2
1158 else:
1183 else:
1159 banner = ''
1184 banner = ''
1160 self.interact(banner)
1185 self.interact(banner)
1161
1186
1162 def exec_init_cmd(self):
1187 def exec_init_cmd(self):
1163 """Execute a command given at the command line.
1188 """Execute a command given at the command line.
1164
1189
1165 This emulates Python's -c option."""
1190 This emulates Python's -c option."""
1166
1191
1167 sys.argv = ['-c']
1192 sys.argv = ['-c']
1168 self.push(self.rc.c)
1193 self.push(self.rc.c)
1169
1194
1170 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1195 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1171 """Embeds IPython into a running python program.
1196 """Embeds IPython into a running python program.
1172
1197
1173 Input:
1198 Input:
1174
1199
1175 - header: An optional header message can be specified.
1200 - header: An optional header message can be specified.
1176
1201
1177 - local_ns, global_ns: working namespaces. If given as None, the
1202 - local_ns, global_ns: working namespaces. If given as None, the
1178 IPython-initialized one is updated with __main__.__dict__, so that
1203 IPython-initialized one is updated with __main__.__dict__, so that
1179 program variables become visible but user-specific configuration
1204 program variables become visible but user-specific configuration
1180 remains possible.
1205 remains possible.
1181
1206
1182 - stack_depth: specifies how many levels in the stack to go to
1207 - stack_depth: specifies how many levels in the stack to go to
1183 looking for namespaces (when local_ns and global_ns are None). This
1208 looking for namespaces (when local_ns and global_ns are None). This
1184 allows an intermediate caller to make sure that this function gets
1209 allows an intermediate caller to make sure that this function gets
1185 the namespace from the intended level in the stack. By default (0)
1210 the namespace from the intended level in the stack. By default (0)
1186 it will get its locals and globals from the immediate caller.
1211 it will get its locals and globals from the immediate caller.
1187
1212
1188 Warning: it's possible to use this in a program which is being run by
1213 Warning: it's possible to use this in a program which is being run by
1189 IPython itself (via %run), but some funny things will happen (a few
1214 IPython itself (via %run), but some funny things will happen (a few
1190 globals get overwritten). In the future this will be cleaned up, as
1215 globals get overwritten). In the future this will be cleaned up, as
1191 there is no fundamental reason why it can't work perfectly."""
1216 there is no fundamental reason why it can't work perfectly."""
1192
1217
1193 # Get locals and globals from caller
1218 # Get locals and globals from caller
1194 if local_ns is None or global_ns is None:
1219 if local_ns is None or global_ns is None:
1195 call_frame = sys._getframe(stack_depth).f_back
1220 call_frame = sys._getframe(stack_depth).f_back
1196
1221
1197 if local_ns is None:
1222 if local_ns is None:
1198 local_ns = call_frame.f_locals
1223 local_ns = call_frame.f_locals
1199 if global_ns is None:
1224 if global_ns is None:
1200 global_ns = call_frame.f_globals
1225 global_ns = call_frame.f_globals
1201
1226
1202 # Update namespaces and fire up interpreter
1227 # Update namespaces and fire up interpreter
1203 self.user_ns = local_ns
1228 self.user_ns = local_ns
1204 self.user_global_ns = global_ns
1229 self.user_global_ns = global_ns
1205
1230
1206 # Patch for global embedding to make sure that things don't overwrite
1231 # Patch for global embedding to make sure that things don't overwrite
1207 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1232 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1208 # FIXME. Test this a bit more carefully (the if.. is new)
1233 # FIXME. Test this a bit more carefully (the if.. is new)
1209 if local_ns is None and global_ns is None:
1234 if local_ns is None and global_ns is None:
1210 self.user_global_ns.update(__main__.__dict__)
1235 self.user_global_ns.update(__main__.__dict__)
1211
1236
1212 # make sure the tab-completer has the correct frame information, so it
1237 # make sure the tab-completer has the correct frame information, so it
1213 # actually completes using the frame's locals/globals
1238 # actually completes using the frame's locals/globals
1214 self.set_completer_frame(call_frame)
1239 self.set_completer_frame(call_frame)
1215
1240
1216 self.interact(header)
1241 self.interact(header)
1217
1242
1218 def interact(self, banner=None):
1243 def interact(self, banner=None):
1219 """Closely emulate the interactive Python console.
1244 """Closely emulate the interactive Python console.
1220
1245
1221 The optional banner argument specify the banner to print
1246 The optional banner argument specify the banner to print
1222 before the first interaction; by default it prints a banner
1247 before the first interaction; by default it prints a banner
1223 similar to the one printed by the real Python interpreter,
1248 similar to the one printed by the real Python interpreter,
1224 followed by the current class name in parentheses (so as not
1249 followed by the current class name in parentheses (so as not
1225 to confuse this with the real interpreter -- since it's so
1250 to confuse this with the real interpreter -- since it's so
1226 close!).
1251 close!).
1227
1252
1228 """
1253 """
1229 cprt = 'Type "copyright", "credits" or "license" for more information.'
1254 cprt = 'Type "copyright", "credits" or "license" for more information.'
1230 if banner is None:
1255 if banner is None:
1231 self.write("Python %s on %s\n%s\n(%s)\n" %
1256 self.write("Python %s on %s\n%s\n(%s)\n" %
1232 (sys.version, sys.platform, cprt,
1257 (sys.version, sys.platform, cprt,
1233 self.__class__.__name__))
1258 self.__class__.__name__))
1234 else:
1259 else:
1235 self.write(banner)
1260 self.write(banner)
1236
1261
1237 more = 0
1262 more = 0
1238
1263
1239 # Mark activity in the builtins
1264 # Mark activity in the builtins
1240 __builtin__.__dict__['__IPYTHON__active'] += 1
1265 __builtin__.__dict__['__IPYTHON__active'] += 1
1241
1266
1242 # compiled regexps for autoindent management
1267 # compiled regexps for autoindent management
1243 ini_spaces_re = re.compile(r'^(\s+)')
1268 ini_spaces_re = re.compile(r'^(\s+)')
1244 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
1269 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
1245
1270
1246 # exit_now is set by a call to %Exit or %Quit
1271 # exit_now is set by a call to %Exit or %Quit
1247 while not self.exit_now:
1272 while not self.exit_now:
1248 try:
1273 try:
1249 if more:
1274 if more:
1250 prompt = self.outputcache.prompt2
1275 prompt = self.outputcache.prompt2
1251 if self.autoindent:
1276 if self.autoindent:
1252 self.readline_startup_hook(self.pre_readline)
1277 self.readline_startup_hook(self.pre_readline)
1253 else:
1278 else:
1254 prompt = self.outputcache.prompt1
1279 prompt = self.outputcache.prompt1
1255 try:
1280 try:
1256 line = self.raw_input(prompt,more)
1281 line = self.raw_input(prompt,more)
1257 if self.autoindent:
1282 if self.autoindent:
1258 self.readline_startup_hook(None)
1283 self.readline_startup_hook(None)
1259 except EOFError:
1284 except EOFError:
1260 if self.autoindent:
1285 if self.autoindent:
1261 self.readline_startup_hook(None)
1286 self.readline_startup_hook(None)
1262 self.write("\n")
1287 self.write("\n")
1263 self.exit()
1288 self.exit()
1264 else:
1289 else:
1265 more = self.push(line)
1290 more = self.push(line)
1266 # Auto-indent management
1291 # Auto-indent management
1267 if self.autoindent:
1292 if self.autoindent:
1268 if line:
1293 if line:
1269 ini_spaces = ini_spaces_re.match(line)
1294 ini_spaces = ini_spaces_re.match(line)
1270 if ini_spaces:
1295 if ini_spaces:
1271 nspaces = ini_spaces.end()
1296 nspaces = ini_spaces.end()
1272 else:
1297 else:
1273 nspaces = 0
1298 nspaces = 0
1274 self.indent_current_nsp = nspaces
1299 self.indent_current_nsp = nspaces
1275
1300
1276 if line[-1] == ':':
1301 if line[-1] == ':':
1277 self.indent_current_nsp += 4
1302 self.indent_current_nsp += 4
1278 elif dedent_re.match(line):
1303 elif dedent_re.match(line):
1279 self.indent_current_nsp -= 4
1304 self.indent_current_nsp -= 4
1280 else:
1305 else:
1281 self.indent_current_nsp = 0
1306 self.indent_current_nsp = 0
1282
1307
1283 # indent_current is the actual string to be inserted
1308 # indent_current is the actual string to be inserted
1284 # by the readline hooks for indentation
1309 # by the readline hooks for indentation
1285 self.indent_current = ' '* self.indent_current_nsp
1310 self.indent_current = ' '* self.indent_current_nsp
1286
1311
1287 if (self.SyntaxTB.last_syntax_error and
1312 if (self.SyntaxTB.last_syntax_error and
1288 self.rc.autoedit_syntax):
1313 self.rc.autoedit_syntax):
1289 self.edit_syntax_error()
1314 self.edit_syntax_error()
1290
1315
1291 except KeyboardInterrupt:
1316 except KeyboardInterrupt:
1292 self.write("\nKeyboardInterrupt\n")
1317 self.write("\nKeyboardInterrupt\n")
1293 self.resetbuffer()
1318 self.resetbuffer()
1294 more = 0
1319 more = 0
1295 # keep cache in sync with the prompt counter:
1320 # keep cache in sync with the prompt counter:
1296 self.outputcache.prompt_count -= 1
1321 self.outputcache.prompt_count -= 1
1297
1322
1298 if self.autoindent:
1323 if self.autoindent:
1299 self.indent_current_nsp = 0
1324 self.indent_current_nsp = 0
1300 self.indent_current = ' '* self.indent_current_nsp
1325 self.indent_current = ' '* self.indent_current_nsp
1301
1326
1302 except bdb.BdbQuit:
1327 except bdb.BdbQuit:
1303 warn("The Python debugger has exited with a BdbQuit exception.\n"
1328 warn("The Python debugger has exited with a BdbQuit exception.\n"
1304 "Because of how pdb handles the stack, it is impossible\n"
1329 "Because of how pdb handles the stack, it is impossible\n"
1305 "for IPython to properly format this particular exception.\n"
1330 "for IPython to properly format this particular exception.\n"
1306 "IPython will resume normal operation.")
1331 "IPython will resume normal operation.")
1307
1332
1308 # We are off again...
1333 # We are off again...
1309 __builtin__.__dict__['__IPYTHON__active'] -= 1
1334 __builtin__.__dict__['__IPYTHON__active'] -= 1
1310
1335
1311 def excepthook(self, type, value, tb):
1336 def excepthook(self, type, value, tb):
1312 """One more defense for GUI apps that call sys.excepthook.
1337 """One more defense for GUI apps that call sys.excepthook.
1313
1338
1314 GUI frameworks like wxPython trap exceptions and call
1339 GUI frameworks like wxPython trap exceptions and call
1315 sys.excepthook themselves. I guess this is a feature that
1340 sys.excepthook themselves. I guess this is a feature that
1316 enables them to keep running after exceptions that would
1341 enables them to keep running after exceptions that would
1317 otherwise kill their mainloop. This is a bother for IPython
1342 otherwise kill their mainloop. This is a bother for IPython
1318 which excepts to catch all of the program exceptions with a try:
1343 which excepts to catch all of the program exceptions with a try:
1319 except: statement.
1344 except: statement.
1320
1345
1321 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1346 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1322 any app directly invokes sys.excepthook, it will look to the user like
1347 any app directly invokes sys.excepthook, it will look to the user like
1323 IPython crashed. In order to work around this, we can disable the
1348 IPython crashed. In order to work around this, we can disable the
1324 CrashHandler and replace it with this excepthook instead, which prints a
1349 CrashHandler and replace it with this excepthook instead, which prints a
1325 regular traceback using our InteractiveTB. In this fashion, apps which
1350 regular traceback using our InteractiveTB. In this fashion, apps which
1326 call sys.excepthook will generate a regular-looking exception from
1351 call sys.excepthook will generate a regular-looking exception from
1327 IPython, and the CrashHandler will only be triggered by real IPython
1352 IPython, and the CrashHandler will only be triggered by real IPython
1328 crashes.
1353 crashes.
1329
1354
1330 This hook should be used sparingly, only in places which are not likely
1355 This hook should be used sparingly, only in places which are not likely
1331 to be true IPython errors.
1356 to be true IPython errors.
1332 """
1357 """
1333
1358
1334 self.InteractiveTB(type, value, tb, tb_offset=0)
1359 self.InteractiveTB(type, value, tb, tb_offset=0)
1335 if self.InteractiveTB.call_pdb and self.has_readline:
1360 if self.InteractiveTB.call_pdb and self.has_readline:
1336 self.readline.set_completer(self.Completer.complete)
1361 self.readline.set_completer(self.Completer.complete)
1337
1362
1338 def call_alias(self,alias,rest=''):
1363 def call_alias(self,alias,rest=''):
1339 """Call an alias given its name and the rest of the line.
1364 """Call an alias given its name and the rest of the line.
1340
1365
1341 This function MUST be given a proper alias, because it doesn't make
1366 This function MUST be given a proper alias, because it doesn't make
1342 any checks when looking up into the alias table. The caller is
1367 any checks when looking up into the alias table. The caller is
1343 responsible for invoking it only with a valid alias."""
1368 responsible for invoking it only with a valid alias."""
1344
1369
1345 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1370 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1346 nargs,cmd = self.alias_table[alias]
1371 nargs,cmd = self.alias_table[alias]
1347 # Expand the %l special to be the user's input line
1372 # Expand the %l special to be the user's input line
1348 if cmd.find('%l') >= 0:
1373 if cmd.find('%l') >= 0:
1349 cmd = cmd.replace('%l',rest)
1374 cmd = cmd.replace('%l',rest)
1350 rest = ''
1375 rest = ''
1351 if nargs==0:
1376 if nargs==0:
1352 # Simple, argument-less aliases
1377 # Simple, argument-less aliases
1353 cmd = '%s %s' % (cmd,rest)
1378 cmd = '%s %s' % (cmd,rest)
1354 else:
1379 else:
1355 # Handle aliases with positional arguments
1380 # Handle aliases with positional arguments
1356 args = rest.split(None,nargs)
1381 args = rest.split(None,nargs)
1357 if len(args)< nargs:
1382 if len(args)< nargs:
1358 error('Alias <%s> requires %s arguments, %s given.' %
1383 error('Alias <%s> requires %s arguments, %s given.' %
1359 (alias,nargs,len(args)))
1384 (alias,nargs,len(args)))
1360 return
1385 return
1361 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1386 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1362 # Now call the macro, evaluating in the user's namespace
1387 # Now call the macro, evaluating in the user's namespace
1363 try:
1388 try:
1364 self.system(cmd)
1389 self.system(cmd)
1365 except:
1390 except:
1366 self.showtraceback()
1391 self.showtraceback()
1367
1392
1368 def runlines(self,lines):
1393 def runlines(self,lines):
1369 """Run a string of one or more lines of source.
1394 """Run a string of one or more lines of source.
1370
1395
1371 This method is capable of running a string containing multiple source
1396 This method is capable of running a string containing multiple source
1372 lines, as if they had been entered at the IPython prompt. Since it
1397 lines, as if they had been entered at the IPython prompt. Since it
1373 exposes IPython's processing machinery, the given strings can contain
1398 exposes IPython's processing machinery, the given strings can contain
1374 magic calls (%magic), special shell access (!cmd), etc."""
1399 magic calls (%magic), special shell access (!cmd), etc."""
1375
1400
1376 # We must start with a clean buffer, in case this is run from an
1401 # We must start with a clean buffer, in case this is run from an
1377 # interactive IPython session (via a magic, for example).
1402 # interactive IPython session (via a magic, for example).
1378 self.resetbuffer()
1403 self.resetbuffer()
1379 lines = lines.split('\n')
1404 lines = lines.split('\n')
1380 more = 0
1405 more = 0
1381 for line in lines:
1406 for line in lines:
1382 # skip blank lines so we don't mess up the prompt counter, but do
1407 # skip blank lines so we don't mess up the prompt counter, but do
1383 # NOT skip even a blank line if we are in a code block (more is
1408 # NOT skip even a blank line if we are in a code block (more is
1384 # true)
1409 # true)
1385 if line or more:
1410 if line or more:
1386 more = self.push((self.prefilter(line,more)))
1411 more = self.push((self.prefilter(line,more)))
1387 # IPython's runsource returns None if there was an error
1412 # IPython's runsource returns None if there was an error
1388 # compiling the code. This allows us to stop processing right
1413 # compiling the code. This allows us to stop processing right
1389 # away, so the user gets the error message at the right place.
1414 # away, so the user gets the error message at the right place.
1390 if more is None:
1415 if more is None:
1391 break
1416 break
1392 # final newline in case the input didn't have it, so that the code
1417 # final newline in case the input didn't have it, so that the code
1393 # actually does get executed
1418 # actually does get executed
1394 if more:
1419 if more:
1395 self.push('\n')
1420 self.push('\n')
1396
1421
1397 def runsource(self, source, filename='<input>', symbol='single'):
1422 def runsource(self, source, filename='<input>', symbol='single'):
1398 """Compile and run some source in the interpreter.
1423 """Compile and run some source in the interpreter.
1399
1424
1400 Arguments are as for compile_command().
1425 Arguments are as for compile_command().
1401
1426
1402 One several things can happen:
1427 One several things can happen:
1403
1428
1404 1) The input is incorrect; compile_command() raised an
1429 1) The input is incorrect; compile_command() raised an
1405 exception (SyntaxError or OverflowError). A syntax traceback
1430 exception (SyntaxError or OverflowError). A syntax traceback
1406 will be printed by calling the showsyntaxerror() method.
1431 will be printed by calling the showsyntaxerror() method.
1407
1432
1408 2) The input is incomplete, and more input is required;
1433 2) The input is incomplete, and more input is required;
1409 compile_command() returned None. Nothing happens.
1434 compile_command() returned None. Nothing happens.
1410
1435
1411 3) The input is complete; compile_command() returned a code
1436 3) The input is complete; compile_command() returned a code
1412 object. The code is executed by calling self.runcode() (which
1437 object. The code is executed by calling self.runcode() (which
1413 also handles run-time exceptions, except for SystemExit).
1438 also handles run-time exceptions, except for SystemExit).
1414
1439
1415 The return value is:
1440 The return value is:
1416
1441
1417 - True in case 2
1442 - True in case 2
1418
1443
1419 - False in the other cases, unless an exception is raised, where
1444 - False in the other cases, unless an exception is raised, where
1420 None is returned instead. This can be used by external callers to
1445 None is returned instead. This can be used by external callers to
1421 know whether to continue feeding input or not.
1446 know whether to continue feeding input or not.
1422
1447
1423 The return value can be used to decide whether to use sys.ps1 or
1448 The return value can be used to decide whether to use sys.ps1 or
1424 sys.ps2 to prompt the next line."""
1449 sys.ps2 to prompt the next line."""
1425
1450
1426 try:
1451 try:
1427 code = self.compile(source,filename,symbol)
1452 code = self.compile(source,filename,symbol)
1428 except (OverflowError, SyntaxError, ValueError):
1453 except (OverflowError, SyntaxError, ValueError):
1429 # Case 1
1454 # Case 1
1430 self.showsyntaxerror(filename)
1455 self.showsyntaxerror(filename)
1431 return None
1456 return None
1432
1457
1433 if code is None:
1458 if code is None:
1434 # Case 2
1459 # Case 2
1435 return True
1460 return True
1436
1461
1437 # Case 3
1462 # Case 3
1438 # We store the code object so that threaded shells and
1463 # We store the code object so that threaded shells and
1439 # custom exception handlers can access all this info if needed.
1464 # custom exception handlers can access all this info if needed.
1440 # The source corresponding to this can be obtained from the
1465 # The source corresponding to this can be obtained from the
1441 # buffer attribute as '\n'.join(self.buffer).
1466 # buffer attribute as '\n'.join(self.buffer).
1442 self.code_to_run = code
1467 self.code_to_run = code
1443 # now actually execute the code object
1468 # now actually execute the code object
1444 if self.runcode(code) == 0:
1469 if self.runcode(code) == 0:
1445 return False
1470 return False
1446 else:
1471 else:
1447 return None
1472 return None
1448
1473
1449 def runcode(self,code_obj):
1474 def runcode(self,code_obj):
1450 """Execute a code object.
1475 """Execute a code object.
1451
1476
1452 When an exception occurs, self.showtraceback() is called to display a
1477 When an exception occurs, self.showtraceback() is called to display a
1453 traceback.
1478 traceback.
1454
1479
1455 Return value: a flag indicating whether the code to be run completed
1480 Return value: a flag indicating whether the code to be run completed
1456 successfully:
1481 successfully:
1457
1482
1458 - 0: successful execution.
1483 - 0: successful execution.
1459 - 1: an error occurred.
1484 - 1: an error occurred.
1460 """
1485 """
1461
1486
1462 # Set our own excepthook in case the user code tries to call it
1487 # Set our own excepthook in case the user code tries to call it
1463 # directly, so that the IPython crash handler doesn't get triggered
1488 # directly, so that the IPython crash handler doesn't get triggered
1464 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1489 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1490
1491 # we save the original sys.excepthook in the instance, in case config
1492 # code (such as magics) needs access to it.
1493 self.sys_excepthook = old_excepthook
1465 outflag = 1 # happens in more places, so it's easier as default
1494 outflag = 1 # happens in more places, so it's easier as default
1466 try:
1495 try:
1467 try:
1496 try:
1468 # Embedded instances require separate global/local namespaces
1497 # Embedded instances require separate global/local namespaces
1469 # so they can see both the surrounding (local) namespace and
1498 # so they can see both the surrounding (local) namespace and
1470 # the module-level globals when called inside another function.
1499 # the module-level globals when called inside another function.
1471 if self.embedded:
1500 if self.embedded:
1472 exec code_obj in self.user_global_ns, self.user_ns
1501 exec code_obj in self.user_global_ns, self.user_ns
1473 # Normal (non-embedded) instances should only have a single
1502 # Normal (non-embedded) instances should only have a single
1474 # namespace for user code execution, otherwise functions won't
1503 # namespace for user code execution, otherwise functions won't
1475 # see interactive top-level globals.
1504 # see interactive top-level globals.
1476 else:
1505 else:
1477 exec code_obj in self.user_ns
1506 exec code_obj in self.user_ns
1478 finally:
1507 finally:
1479 # Reset our crash handler in place
1508 # Reset our crash handler in place
1480 sys.excepthook = old_excepthook
1509 sys.excepthook = old_excepthook
1481 except SystemExit:
1510 except SystemExit:
1482 self.resetbuffer()
1511 self.resetbuffer()
1483 self.showtraceback()
1512 self.showtraceback()
1484 warn("Type exit or quit to exit IPython "
1513 warn("Type exit or quit to exit IPython "
1485 "(%Exit or %Quit do so unconditionally).",level=1)
1514 "(%Exit or %Quit do so unconditionally).",level=1)
1486 except self.custom_exceptions:
1515 except self.custom_exceptions:
1487 etype,value,tb = sys.exc_info()
1516 etype,value,tb = sys.exc_info()
1488 self.CustomTB(etype,value,tb)
1517 self.CustomTB(etype,value,tb)
1489 except:
1518 except:
1490 self.showtraceback()
1519 self.showtraceback()
1491 else:
1520 else:
1492 outflag = 0
1521 outflag = 0
1493 if softspace(sys.stdout, 0):
1522 if softspace(sys.stdout, 0):
1494 print
1523 print
1495 # Flush out code object which has been run (and source)
1524 # Flush out code object which has been run (and source)
1496 self.code_to_run = None
1525 self.code_to_run = None
1497 return outflag
1526 return outflag
1498
1527
1499 def push(self, line):
1528 def push(self, line):
1500 """Push a line to the interpreter.
1529 """Push a line to the interpreter.
1501
1530
1502 The line should not have a trailing newline; it may have
1531 The line should not have a trailing newline; it may have
1503 internal newlines. The line is appended to a buffer and the
1532 internal newlines. The line is appended to a buffer and the
1504 interpreter's runsource() method is called with the
1533 interpreter's runsource() method is called with the
1505 concatenated contents of the buffer as source. If this
1534 concatenated contents of the buffer as source. If this
1506 indicates that the command was executed or invalid, the buffer
1535 indicates that the command was executed or invalid, the buffer
1507 is reset; otherwise, the command is incomplete, and the buffer
1536 is reset; otherwise, the command is incomplete, and the buffer
1508 is left as it was after the line was appended. The return
1537 is left as it was after the line was appended. The return
1509 value is 1 if more input is required, 0 if the line was dealt
1538 value is 1 if more input is required, 0 if the line was dealt
1510 with in some way (this is the same as runsource()).
1539 with in some way (this is the same as runsource()).
1511
1540
1512 """
1541 """
1513 self.buffer.append(line)
1542 self.buffer.append(line)
1514 more = self.runsource('\n'.join(self.buffer), self.filename)
1543 more = self.runsource('\n'.join(self.buffer), self.filename)
1515 if not more:
1544 if not more:
1516 self.resetbuffer()
1545 self.resetbuffer()
1517 return more
1546 return more
1518
1547
1519 def resetbuffer(self):
1548 def resetbuffer(self):
1520 """Reset the input buffer."""
1549 """Reset the input buffer."""
1521 self.buffer[:] = []
1550 self.buffer[:] = []
1522
1551
1523 def raw_input(self,prompt='',continue_prompt=False):
1552 def raw_input(self,prompt='',continue_prompt=False):
1524 """Write a prompt and read a line.
1553 """Write a prompt and read a line.
1525
1554
1526 The returned line does not include the trailing newline.
1555 The returned line does not include the trailing newline.
1527 When the user enters the EOF key sequence, EOFError is raised.
1556 When the user enters the EOF key sequence, EOFError is raised.
1528
1557
1529 Optional inputs:
1558 Optional inputs:
1530
1559
1531 - prompt(''): a string to be printed to prompt the user.
1560 - prompt(''): a string to be printed to prompt the user.
1532
1561
1533 - continue_prompt(False): whether this line is the first one or a
1562 - continue_prompt(False): whether this line is the first one or a
1534 continuation in a sequence of inputs.
1563 continuation in a sequence of inputs.
1535 """
1564 """
1536
1565
1537 line = raw_input_original(prompt)
1566 line = raw_input_original(prompt)
1538 # Try to be reasonably smart about not re-indenting pasted input more
1567 # Try to be reasonably smart about not re-indenting pasted input more
1539 # than necessary. We do this by trimming out the auto-indent initial
1568 # than necessary. We do this by trimming out the auto-indent initial
1540 # spaces, if the user's actual input started itself with whitespace.
1569 # spaces, if the user's actual input started itself with whitespace.
1541 if self.autoindent:
1570 if self.autoindent:
1542 line2 = line[self.indent_current_nsp:]
1571 line2 = line[self.indent_current_nsp:]
1543 if line2[0:1] in (' ','\t'):
1572 if line2[0:1] in (' ','\t'):
1544 line = line2
1573 line = line2
1545 return self.prefilter(line,continue_prompt)
1574 return self.prefilter(line,continue_prompt)
1546
1575
1547 def split_user_input(self,line):
1576 def split_user_input(self,line):
1548 """Split user input into pre-char, function part and rest."""
1577 """Split user input into pre-char, function part and rest."""
1549
1578
1550 lsplit = self.line_split.match(line)
1579 lsplit = self.line_split.match(line)
1551 if lsplit is None: # no regexp match returns None
1580 if lsplit is None: # no regexp match returns None
1552 try:
1581 try:
1553 iFun,theRest = line.split(None,1)
1582 iFun,theRest = line.split(None,1)
1554 except ValueError:
1583 except ValueError:
1555 iFun,theRest = line,''
1584 iFun,theRest = line,''
1556 pre = re.match('^(\s*)(.*)',line).groups()[0]
1585 pre = re.match('^(\s*)(.*)',line).groups()[0]
1557 else:
1586 else:
1558 pre,iFun,theRest = lsplit.groups()
1587 pre,iFun,theRest = lsplit.groups()
1559
1588
1560 #print 'line:<%s>' % line # dbg
1589 #print 'line:<%s>' % line # dbg
1561 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1590 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1562 return pre,iFun.strip(),theRest
1591 return pre,iFun.strip(),theRest
1563
1592
1564 def _prefilter(self, line, continue_prompt):
1593 def _prefilter(self, line, continue_prompt):
1565 """Calls different preprocessors, depending on the form of line."""
1594 """Calls different preprocessors, depending on the form of line."""
1566
1595
1567 # All handlers *must* return a value, even if it's blank ('').
1596 # All handlers *must* return a value, even if it's blank ('').
1568
1597
1569 # Lines are NOT logged here. Handlers should process the line as
1598 # Lines are NOT logged here. Handlers should process the line as
1570 # needed, update the cache AND log it (so that the input cache array
1599 # needed, update the cache AND log it (so that the input cache array
1571 # stays synced).
1600 # stays synced).
1572
1601
1573 # This function is _very_ delicate, and since it's also the one which
1602 # This function is _very_ delicate, and since it's also the one which
1574 # determines IPython's response to user input, it must be as efficient
1603 # determines IPython's response to user input, it must be as efficient
1575 # as possible. For this reason it has _many_ returns in it, trying
1604 # as possible. For this reason it has _many_ returns in it, trying
1576 # always to exit as quickly as it can figure out what it needs to do.
1605 # always to exit as quickly as it can figure out what it needs to do.
1577
1606
1578 # This function is the main responsible for maintaining IPython's
1607 # This function is the main responsible for maintaining IPython's
1579 # behavior respectful of Python's semantics. So be _very_ careful if
1608 # behavior respectful of Python's semantics. So be _very_ careful if
1580 # making changes to anything here.
1609 # making changes to anything here.
1581
1610
1582 #.....................................................................
1611 #.....................................................................
1583 # Code begins
1612 # Code begins
1584
1613
1585 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1614 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1586
1615
1587 # save the line away in case we crash, so the post-mortem handler can
1616 # save the line away in case we crash, so the post-mortem handler can
1588 # record it
1617 # record it
1589 self._last_input_line = line
1618 self._last_input_line = line
1590
1619
1591 #print '***line: <%s>' % line # dbg
1620 #print '***line: <%s>' % line # dbg
1592
1621
1593 # the input history needs to track even empty lines
1622 # the input history needs to track even empty lines
1594 if not line.strip():
1623 if not line.strip():
1595 if not continue_prompt:
1624 if not continue_prompt:
1596 self.outputcache.prompt_count -= 1
1625 self.outputcache.prompt_count -= 1
1597 return self.handle_normal(line,continue_prompt)
1626 return self.handle_normal(line,continue_prompt)
1598 #return self.handle_normal('',continue_prompt)
1627 #return self.handle_normal('',continue_prompt)
1599
1628
1600 # print '***cont',continue_prompt # dbg
1629 # print '***cont',continue_prompt # dbg
1601 # special handlers are only allowed for single line statements
1630 # special handlers are only allowed for single line statements
1602 if continue_prompt and not self.rc.multi_line_specials:
1631 if continue_prompt and not self.rc.multi_line_specials:
1603 return self.handle_normal(line,continue_prompt)
1632 return self.handle_normal(line,continue_prompt)
1604
1633
1605 # For the rest, we need the structure of the input
1634 # For the rest, we need the structure of the input
1606 pre,iFun,theRest = self.split_user_input(line)
1635 pre,iFun,theRest = self.split_user_input(line)
1607 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1636 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1608
1637
1609 # First check for explicit escapes in the last/first character
1638 # First check for explicit escapes in the last/first character
1610 handler = None
1639 handler = None
1611 if line[-1] == self.ESC_HELP:
1640 if line[-1] == self.ESC_HELP:
1612 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1641 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1613 if handler is None:
1642 if handler is None:
1614 # look at the first character of iFun, NOT of line, so we skip
1643 # look at the first character of iFun, NOT of line, so we skip
1615 # leading whitespace in multiline input
1644 # leading whitespace in multiline input
1616 handler = self.esc_handlers.get(iFun[0:1])
1645 handler = self.esc_handlers.get(iFun[0:1])
1617 if handler is not None:
1646 if handler is not None:
1618 return handler(line,continue_prompt,pre,iFun,theRest)
1647 return handler(line,continue_prompt,pre,iFun,theRest)
1619 # Emacs ipython-mode tags certain input lines
1648 # Emacs ipython-mode tags certain input lines
1620 if line.endswith('# PYTHON-MODE'):
1649 if line.endswith('# PYTHON-MODE'):
1621 return self.handle_emacs(line,continue_prompt)
1650 return self.handle_emacs(line,continue_prompt)
1622
1651
1623 # Next, check if we can automatically execute this thing
1652 # Next, check if we can automatically execute this thing
1624
1653
1625 # Allow ! in multi-line statements if multi_line_specials is on:
1654 # Allow ! in multi-line statements if multi_line_specials is on:
1626 if continue_prompt and self.rc.multi_line_specials and \
1655 if continue_prompt and self.rc.multi_line_specials and \
1627 iFun.startswith(self.ESC_SHELL):
1656 iFun.startswith(self.ESC_SHELL):
1628 return self.handle_shell_escape(line,continue_prompt,
1657 return self.handle_shell_escape(line,continue_prompt,
1629 pre=pre,iFun=iFun,
1658 pre=pre,iFun=iFun,
1630 theRest=theRest)
1659 theRest=theRest)
1631
1660
1632 # Let's try to find if the input line is a magic fn
1661 # Let's try to find if the input line is a magic fn
1633 oinfo = None
1662 oinfo = None
1634 if hasattr(self,'magic_'+iFun):
1663 if hasattr(self,'magic_'+iFun):
1635 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1664 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1636 if oinfo['ismagic']:
1665 if oinfo['ismagic']:
1637 # Be careful not to call magics when a variable assignment is
1666 # Be careful not to call magics when a variable assignment is
1638 # being made (ls='hi', for example)
1667 # being made (ls='hi', for example)
1639 if self.rc.automagic and \
1668 if self.rc.automagic and \
1640 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1669 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1641 (self.rc.multi_line_specials or not continue_prompt):
1670 (self.rc.multi_line_specials or not continue_prompt):
1642 return self.handle_magic(line,continue_prompt,
1671 return self.handle_magic(line,continue_prompt,
1643 pre,iFun,theRest)
1672 pre,iFun,theRest)
1644 else:
1673 else:
1645 return self.handle_normal(line,continue_prompt)
1674 return self.handle_normal(line,continue_prompt)
1646
1675
1647 # If the rest of the line begins with an (in)equality, assginment or
1676 # If the rest of the line begins with an (in)equality, assginment or
1648 # function call, we should not call _ofind but simply execute it.
1677 # function call, we should not call _ofind but simply execute it.
1649 # This avoids spurious geattr() accesses on objects upon assignment.
1678 # This avoids spurious geattr() accesses on objects upon assignment.
1650 #
1679 #
1651 # It also allows users to assign to either alias or magic names true
1680 # It also allows users to assign to either alias or magic names true
1652 # python variables (the magic/alias systems always take second seat to
1681 # python variables (the magic/alias systems always take second seat to
1653 # true python code).
1682 # true python code).
1654 if theRest and theRest[0] in '!=()':
1683 if theRest and theRest[0] in '!=()':
1655 return self.handle_normal(line,continue_prompt)
1684 return self.handle_normal(line,continue_prompt)
1656
1685
1657 if oinfo is None:
1686 if oinfo is None:
1658 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1687 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1659
1688
1660 if not oinfo['found']:
1689 if not oinfo['found']:
1661 return self.handle_normal(line,continue_prompt)
1690 return self.handle_normal(line,continue_prompt)
1662 else:
1691 else:
1663 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1692 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1664 if oinfo['isalias']:
1693 if oinfo['isalias']:
1665 return self.handle_alias(line,continue_prompt,
1694 return self.handle_alias(line,continue_prompt,
1666 pre,iFun,theRest)
1695 pre,iFun,theRest)
1667
1696
1668 if self.rc.autocall and \
1697 if self.rc.autocall and \
1669 not self.re_exclude_auto.match(theRest) and \
1698 not self.re_exclude_auto.match(theRest) and \
1670 self.re_fun_name.match(iFun) and \
1699 self.re_fun_name.match(iFun) and \
1671 callable(oinfo['obj']) :
1700 callable(oinfo['obj']) :
1672 #print 'going auto' # dbg
1701 #print 'going auto' # dbg
1673 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1702 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1674 else:
1703 else:
1675 #print 'was callable?', callable(oinfo['obj']) # dbg
1704 #print 'was callable?', callable(oinfo['obj']) # dbg
1676 return self.handle_normal(line,continue_prompt)
1705 return self.handle_normal(line,continue_prompt)
1677
1706
1678 # If we get here, we have a normal Python line. Log and return.
1707 # If we get here, we have a normal Python line. Log and return.
1679 return self.handle_normal(line,continue_prompt)
1708 return self.handle_normal(line,continue_prompt)
1680
1709
1681 def _prefilter_dumb(self, line, continue_prompt):
1710 def _prefilter_dumb(self, line, continue_prompt):
1682 """simple prefilter function, for debugging"""
1711 """simple prefilter function, for debugging"""
1683 return self.handle_normal(line,continue_prompt)
1712 return self.handle_normal(line,continue_prompt)
1684
1713
1685 # Set the default prefilter() function (this can be user-overridden)
1714 # Set the default prefilter() function (this can be user-overridden)
1686 prefilter = _prefilter
1715 prefilter = _prefilter
1687
1716
1688 def handle_normal(self,line,continue_prompt=None,
1717 def handle_normal(self,line,continue_prompt=None,
1689 pre=None,iFun=None,theRest=None):
1718 pre=None,iFun=None,theRest=None):
1690 """Handle normal input lines. Use as a template for handlers."""
1719 """Handle normal input lines. Use as a template for handlers."""
1691
1720
1692 # With autoindent on, we need some way to exit the input loop, and I
1721 # With autoindent on, we need some way to exit the input loop, and I
1693 # don't want to force the user to have to backspace all the way to
1722 # don't want to force the user to have to backspace all the way to
1694 # clear the line. The rule will be in this case, that either two
1723 # clear the line. The rule will be in this case, that either two
1695 # lines of pure whitespace in a row, or a line of pure whitespace but
1724 # lines of pure whitespace in a row, or a line of pure whitespace but
1696 # of a size different to the indent level, will exit the input loop.
1725 # of a size different to the indent level, will exit the input loop.
1697 if (continue_prompt and self.autoindent and isspace(line) and
1726 if (continue_prompt and self.autoindent and isspace(line) and
1698 (line != self.indent_current or isspace(self.buffer[-1]))):
1727 (line != self.indent_current or isspace(self.buffer[-1]))):
1699 line = ''
1728 line = ''
1700
1729
1701 self.log(line,continue_prompt)
1730 self.log(line,continue_prompt)
1702 self.update_cache(line)
1731 self.update_cache(line)
1703 return line
1732 return line
1704
1733
1705 def handle_alias(self,line,continue_prompt=None,
1734 def handle_alias(self,line,continue_prompt=None,
1706 pre=None,iFun=None,theRest=None):
1735 pre=None,iFun=None,theRest=None):
1707 """Handle alias input lines. """
1736 """Handle alias input lines. """
1708
1737
1709 theRest = esc_quotes(theRest)
1738 theRest = esc_quotes(theRest)
1710 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1739 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1711 self.log(line_out,continue_prompt)
1740 self.log(line_out,continue_prompt)
1712 self.update_cache(line_out)
1741 self.update_cache(line_out)
1713 return line_out
1742 return line_out
1714
1743
1715 def handle_shell_escape(self, line, continue_prompt=None,
1744 def handle_shell_escape(self, line, continue_prompt=None,
1716 pre=None,iFun=None,theRest=None):
1745 pre=None,iFun=None,theRest=None):
1717 """Execute the line in a shell, empty return value"""
1746 """Execute the line in a shell, empty return value"""
1718
1747
1719 #print 'line in :', `line` # dbg
1748 #print 'line in :', `line` # dbg
1720 # Example of a special handler. Others follow a similar pattern.
1749 # Example of a special handler. Others follow a similar pattern.
1721 if continue_prompt: # multi-line statements
1750 if continue_prompt: # multi-line statements
1722 if iFun.startswith('!!'):
1751 if iFun.startswith('!!'):
1723 print 'SyntaxError: !! is not allowed in multiline statements'
1752 print 'SyntaxError: !! is not allowed in multiline statements'
1724 return pre
1753 return pre
1725 else:
1754 else:
1726 cmd = ("%s %s" % (iFun[1:],theRest)) #.replace('"','\\"')
1755 cmd = ("%s %s" % (iFun[1:],theRest)) #.replace('"','\\"')
1727 #line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1756 #line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1728 line_out = '%s%s.system(r"""%s"""[:-1])' % (pre,self.name,cmd + "_")
1757 line_out = '%s%s.system(r"""%s"""[:-1])' % (pre,self.name,cmd + "_")
1729 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1758 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1730 else: # single-line input
1759 else: # single-line input
1731 if line.startswith('!!'):
1760 if line.startswith('!!'):
1732 # rewrite iFun/theRest to properly hold the call to %sx and
1761 # rewrite iFun/theRest to properly hold the call to %sx and
1733 # the actual command to be executed, so handle_magic can work
1762 # the actual command to be executed, so handle_magic can work
1734 # correctly
1763 # correctly
1735 theRest = '%s %s' % (iFun[2:],theRest)
1764 theRest = '%s %s' % (iFun[2:],theRest)
1736 iFun = 'sx'
1765 iFun = 'sx'
1737 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1766 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1738 continue_prompt,pre,iFun,theRest)
1767 continue_prompt,pre,iFun,theRest)
1739 else:
1768 else:
1740 #cmd = esc_quotes(line[1:])
1769 #cmd = esc_quotes(line[1:])
1741 cmd=line[1:]
1770 cmd=line[1:]
1742 #line_out = '%s.system("%s")' % (self.name,cmd)
1771 #line_out = '%s.system("%s")' % (self.name,cmd)
1743 line_out = '%s.system(r"""%s"""[:-1])' % (self.name,cmd +"_")
1772 line_out = '%s.system(r"""%s"""[:-1])' % (self.name,cmd +"_")
1744 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1773 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1745 # update cache/log and return
1774 # update cache/log and return
1746 self.log(line_out,continue_prompt)
1775 self.log(line_out,continue_prompt)
1747 self.update_cache(line_out) # readline cache gets normal line
1776 self.update_cache(line_out) # readline cache gets normal line
1748 #print 'line out r:', `line_out` # dbg
1777 #print 'line out r:', `line_out` # dbg
1749 #print 'line out s:', line_out # dbg
1778 #print 'line out s:', line_out # dbg
1750 return line_out
1779 return line_out
1751
1780
1752 def handle_magic(self, line, continue_prompt=None,
1781 def handle_magic(self, line, continue_prompt=None,
1753 pre=None,iFun=None,theRest=None):
1782 pre=None,iFun=None,theRest=None):
1754 """Execute magic functions.
1783 """Execute magic functions.
1755
1784
1756 Also log them with a prepended # so the log is clean Python."""
1785 Also log them with a prepended # so the log is clean Python."""
1757
1786
1758 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1787 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1759 self.log(cmd,continue_prompt)
1788 self.log(cmd,continue_prompt)
1760 self.update_cache(line)
1789 self.update_cache(line)
1761 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1790 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1762 return cmd
1791 return cmd
1763
1792
1764 def handle_auto(self, line, continue_prompt=None,
1793 def handle_auto(self, line, continue_prompt=None,
1765 pre=None,iFun=None,theRest=None):
1794 pre=None,iFun=None,theRest=None):
1766 """Hande lines which can be auto-executed, quoting if requested."""
1795 """Hande lines which can be auto-executed, quoting if requested."""
1767
1796
1768 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1797 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1769
1798
1770 # This should only be active for single-line input!
1799 # This should only be active for single-line input!
1771 if continue_prompt:
1800 if continue_prompt:
1772 return line
1801 return line
1773
1802
1774 if pre == self.ESC_QUOTE:
1803 if pre == self.ESC_QUOTE:
1775 # Auto-quote splitting on whitespace
1804 # Auto-quote splitting on whitespace
1776 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1805 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1777 elif pre == self.ESC_QUOTE2:
1806 elif pre == self.ESC_QUOTE2:
1778 # Auto-quote whole string
1807 # Auto-quote whole string
1779 newcmd = '%s("%s")' % (iFun,theRest)
1808 newcmd = '%s("%s")' % (iFun,theRest)
1780 else:
1809 else:
1781 # Auto-paren
1810 # Auto-paren
1782 if theRest[0:1] in ('=','['):
1811 if theRest[0:1] in ('=','['):
1783 # Don't autocall in these cases. They can be either
1812 # Don't autocall in these cases. They can be either
1784 # rebindings of an existing callable's name, or item access
1813 # rebindings of an existing callable's name, or item access
1785 # for an object which is BOTH callable and implements
1814 # for an object which is BOTH callable and implements
1786 # __getitem__.
1815 # __getitem__.
1787 return '%s %s' % (iFun,theRest)
1816 return '%s %s' % (iFun,theRest)
1788 if theRest.endswith(';'):
1817 if theRest.endswith(';'):
1789 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1818 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1790 else:
1819 else:
1791 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1820 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1792
1821
1793 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1822 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1794 # log what is now valid Python, not the actual user input (without the
1823 # log what is now valid Python, not the actual user input (without the
1795 # final newline)
1824 # final newline)
1796 self.log(newcmd,continue_prompt)
1825 self.log(newcmd,continue_prompt)
1797 return newcmd
1826 return newcmd
1798
1827
1799 def handle_help(self, line, continue_prompt=None,
1828 def handle_help(self, line, continue_prompt=None,
1800 pre=None,iFun=None,theRest=None):
1829 pre=None,iFun=None,theRest=None):
1801 """Try to get some help for the object.
1830 """Try to get some help for the object.
1802
1831
1803 obj? or ?obj -> basic information.
1832 obj? or ?obj -> basic information.
1804 obj?? or ??obj -> more details.
1833 obj?? or ??obj -> more details.
1805 """
1834 """
1806
1835
1807 # We need to make sure that we don't process lines which would be
1836 # We need to make sure that we don't process lines which would be
1808 # otherwise valid python, such as "x=1 # what?"
1837 # otherwise valid python, such as "x=1 # what?"
1809 try:
1838 try:
1810 codeop.compile_command(line)
1839 codeop.compile_command(line)
1811 except SyntaxError:
1840 except SyntaxError:
1812 # We should only handle as help stuff which is NOT valid syntax
1841 # We should only handle as help stuff which is NOT valid syntax
1813 if line[0]==self.ESC_HELP:
1842 if line[0]==self.ESC_HELP:
1814 line = line[1:]
1843 line = line[1:]
1815 elif line[-1]==self.ESC_HELP:
1844 elif line[-1]==self.ESC_HELP:
1816 line = line[:-1]
1845 line = line[:-1]
1817 self.log('#?'+line)
1846 self.log('#?'+line)
1818 self.update_cache(line)
1847 self.update_cache(line)
1819 if line:
1848 if line:
1820 self.magic_pinfo(line)
1849 self.magic_pinfo(line)
1821 else:
1850 else:
1822 page(self.usage,screen_lines=self.rc.screen_length)
1851 page(self.usage,screen_lines=self.rc.screen_length)
1823 return '' # Empty string is needed here!
1852 return '' # Empty string is needed here!
1824 except:
1853 except:
1825 # Pass any other exceptions through to the normal handler
1854 # Pass any other exceptions through to the normal handler
1826 return self.handle_normal(line,continue_prompt)
1855 return self.handle_normal(line,continue_prompt)
1827 else:
1856 else:
1828 # If the code compiles ok, we should handle it normally
1857 # If the code compiles ok, we should handle it normally
1829 return self.handle_normal(line,continue_prompt)
1858 return self.handle_normal(line,continue_prompt)
1830
1859
1831 def handle_emacs(self,line,continue_prompt=None,
1860 def handle_emacs(self,line,continue_prompt=None,
1832 pre=None,iFun=None,theRest=None):
1861 pre=None,iFun=None,theRest=None):
1833 """Handle input lines marked by python-mode."""
1862 """Handle input lines marked by python-mode."""
1834
1863
1835 # Currently, nothing is done. Later more functionality can be added
1864 # Currently, nothing is done. Later more functionality can be added
1836 # here if needed.
1865 # here if needed.
1837
1866
1838 # The input cache shouldn't be updated
1867 # The input cache shouldn't be updated
1839
1868
1840 return line
1869 return line
1841
1870
1842 def write(self,data):
1871 def write(self,data):
1843 """Write a string to the default output"""
1872 """Write a string to the default output"""
1844 Term.cout.write(data)
1873 Term.cout.write(data)
1845
1874
1846 def write_err(self,data):
1875 def write_err(self,data):
1847 """Write a string to the default error output"""
1876 """Write a string to the default error output"""
1848 Term.cerr.write(data)
1877 Term.cerr.write(data)
1849
1878
1850 def exit(self):
1879 def exit(self):
1851 """Handle interactive exit.
1880 """Handle interactive exit.
1852
1881
1853 This method sets the exit_now attribute."""
1882 This method sets the exit_now attribute."""
1854
1883
1855 if self.rc.confirm_exit:
1884 if self.rc.confirm_exit:
1856 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1885 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1857 self.exit_now = True
1886 self.exit_now = True
1858 else:
1887 else:
1859 self.exit_now = True
1888 self.exit_now = True
1860 return self.exit_now
1889 return self.exit_now
1861
1890
1862 def safe_execfile(self,fname,*where,**kw):
1891 def safe_execfile(self,fname,*where,**kw):
1863 fname = os.path.expanduser(fname)
1892 fname = os.path.expanduser(fname)
1864
1893
1865 # find things also in current directory
1894 # find things also in current directory
1866 dname = os.path.dirname(fname)
1895 dname = os.path.dirname(fname)
1867 if not sys.path.count(dname):
1896 if not sys.path.count(dname):
1868 sys.path.append(dname)
1897 sys.path.append(dname)
1869
1898
1870 try:
1899 try:
1871 xfile = open(fname)
1900 xfile = open(fname)
1872 except:
1901 except:
1873 print >> Term.cerr, \
1902 print >> Term.cerr, \
1874 'Could not open file <%s> for safe execution.' % fname
1903 'Could not open file <%s> for safe execution.' % fname
1875 return None
1904 return None
1876
1905
1877 kw.setdefault('islog',0)
1906 kw.setdefault('islog',0)
1878 kw.setdefault('quiet',1)
1907 kw.setdefault('quiet',1)
1879 kw.setdefault('exit_ignore',0)
1908 kw.setdefault('exit_ignore',0)
1880 first = xfile.readline()
1909 first = xfile.readline()
1881 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
1910 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
1882 xfile.close()
1911 xfile.close()
1883 # line by line execution
1912 # line by line execution
1884 if first.startswith(_LOGHEAD) or kw['islog']:
1913 if first.startswith(_LOGHEAD) or kw['islog']:
1885 print 'Loading log file <%s> one line at a time...' % fname
1914 print 'Loading log file <%s> one line at a time...' % fname
1886 if kw['quiet']:
1915 if kw['quiet']:
1887 stdout_save = sys.stdout
1916 stdout_save = sys.stdout
1888 sys.stdout = StringIO.StringIO()
1917 sys.stdout = StringIO.StringIO()
1889 try:
1918 try:
1890 globs,locs = where[0:2]
1919 globs,locs = where[0:2]
1891 except:
1920 except:
1892 try:
1921 try:
1893 globs = locs = where[0]
1922 globs = locs = where[0]
1894 except:
1923 except:
1895 globs = locs = globals()
1924 globs = locs = globals()
1896 badblocks = []
1925 badblocks = []
1897
1926
1898 # we also need to identify indented blocks of code when replaying
1927 # we also need to identify indented blocks of code when replaying
1899 # logs and put them together before passing them to an exec
1928 # logs and put them together before passing them to an exec
1900 # statement. This takes a bit of regexp and look-ahead work in the
1929 # statement. This takes a bit of regexp and look-ahead work in the
1901 # file. It's easiest if we swallow the whole thing in memory
1930 # file. It's easiest if we swallow the whole thing in memory
1902 # first, and manually walk through the lines list moving the
1931 # first, and manually walk through the lines list moving the
1903 # counter ourselves.
1932 # counter ourselves.
1904 indent_re = re.compile('\s+\S')
1933 indent_re = re.compile('\s+\S')
1905 xfile = open(fname)
1934 xfile = open(fname)
1906 filelines = xfile.readlines()
1935 filelines = xfile.readlines()
1907 xfile.close()
1936 xfile.close()
1908 nlines = len(filelines)
1937 nlines = len(filelines)
1909 lnum = 0
1938 lnum = 0
1910 while lnum < nlines:
1939 while lnum < nlines:
1911 line = filelines[lnum]
1940 line = filelines[lnum]
1912 lnum += 1
1941 lnum += 1
1913 # don't re-insert logger status info into cache
1942 # don't re-insert logger status info into cache
1914 if line.startswith('#log#'):
1943 if line.startswith('#log#'):
1915 continue
1944 continue
1916 elif line.startswith('#%s'% self.ESC_MAGIC):
1945 elif line.startswith('#%s'% self.ESC_MAGIC):
1917 self.update_cache(line[1:])
1946 self.update_cache(line[1:])
1918 line = magic2python(line)
1947 line = magic2python(line)
1919 elif line.startswith('#!'):
1948 elif line.startswith('#!'):
1920 self.update_cache(line[1:])
1949 self.update_cache(line[1:])
1921 else:
1950 else:
1922 # build a block of code (maybe a single line) for execution
1951 # build a block of code (maybe a single line) for execution
1923 block = line
1952 block = line
1924 try:
1953 try:
1925 next = filelines[lnum] # lnum has already incremented
1954 next = filelines[lnum] # lnum has already incremented
1926 except:
1955 except:
1927 next = None
1956 next = None
1928 while next and indent_re.match(next):
1957 while next and indent_re.match(next):
1929 block += next
1958 block += next
1930 lnum += 1
1959 lnum += 1
1931 try:
1960 try:
1932 next = filelines[lnum]
1961 next = filelines[lnum]
1933 except:
1962 except:
1934 next = None
1963 next = None
1935 # now execute the block of one or more lines
1964 # now execute the block of one or more lines
1936 try:
1965 try:
1937 exec block in globs,locs
1966 exec block in globs,locs
1938 self.update_cache(block.rstrip())
1967 self.update_cache(block.rstrip())
1939 except SystemExit:
1968 except SystemExit:
1940 pass
1969 pass
1941 except:
1970 except:
1942 badblocks.append(block.rstrip())
1971 badblocks.append(block.rstrip())
1943 if kw['quiet']: # restore stdout
1972 if kw['quiet']: # restore stdout
1944 sys.stdout.close()
1973 sys.stdout.close()
1945 sys.stdout = stdout_save
1974 sys.stdout = stdout_save
1946 print 'Finished replaying log file <%s>' % fname
1975 print 'Finished replaying log file <%s>' % fname
1947 if badblocks:
1976 if badblocks:
1948 print >> sys.stderr, ('\nThe following lines/blocks in file '
1977 print >> sys.stderr, ('\nThe following lines/blocks in file '
1949 '<%s> reported errors:' % fname)
1978 '<%s> reported errors:' % fname)
1950
1979
1951 for badline in badblocks:
1980 for badline in badblocks:
1952 print >> sys.stderr, badline
1981 print >> sys.stderr, badline
1953 else: # regular file execution
1982 else: # regular file execution
1954 try:
1983 try:
1955 execfile(fname,*where)
1984 execfile(fname,*where)
1956 except SyntaxError:
1985 except SyntaxError:
1957 etype, evalue = sys.exc_info()[0:2]
1986 etype,evalue = sys.exc_info()[:2]
1958 self.SyntaxTB(etype,evalue,[])
1987 self.SyntaxTB(etype,evalue,[])
1959 warn('Failure executing file: <%s>' % fname)
1988 warn('Failure executing file: <%s>' % fname)
1960 except SystemExit,status:
1989 except SystemExit,status:
1961 if not kw['exit_ignore']:
1990 if not kw['exit_ignore']:
1962 self.InteractiveTB()
1991 self.InteractiveTB()
1963 warn('Failure executing file: <%s>' % fname)
1992 warn('Failure executing file: <%s>' % fname)
1964 except:
1993 except:
1965 self.InteractiveTB()
1994 self.InteractiveTB()
1966 warn('Failure executing file: <%s>' % fname)
1995 warn('Failure executing file: <%s>' % fname)
1967
1996
1968 #************************* end of file <iplib.py> *****************************
1997 #************************* end of file <iplib.py> *****************************
@@ -1,739 +1,735 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.1 or better.
5 Requires Python 2.1 or better.
6
6
7 This file contains the main make_IPython() starter function.
7 This file contains the main make_IPython() starter function.
8
8
9 $Id: ipmaker.py 964 2005-12-28 21:03:01Z fperez $"""
9 $Id: ipmaker.py 965 2005-12-28 23:23:09Z fperez $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2004 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
17
18 from IPython import Release
18 from IPython import Release
19 __author__ = '%s <%s>' % Release.authors['Fernando']
19 __author__ = '%s <%s>' % Release.authors['Fernando']
20 __license__ = Release.license
20 __license__ = Release.license
21 __version__ = Release.version
21 __version__ = Release.version
22
22
23 credits._Printer__data = """
23 credits._Printer__data = """
24 Python: %s
24 Python: %s
25
25
26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
27 See http://ipython.scipy.org for more information.""" \
27 See http://ipython.scipy.org for more information.""" \
28 % credits._Printer__data
28 % credits._Printer__data
29
29
30 copyright._Printer__data += """
30 copyright._Printer__data += """
31
31
32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
33 All Rights Reserved."""
33 All Rights Reserved."""
34
34
35 #****************************************************************************
35 #****************************************************************************
36 # Required modules
36 # Required modules
37
37
38 # From the standard library
38 # From the standard library
39 import __main__
39 import __main__
40 import __builtin__
40 import __builtin__
41 import os
41 import os
42 import re
42 import re
43 import sys
43 import sys
44 import types
44 import types
45 from pprint import pprint,pformat
45 from pprint import pprint,pformat
46
46
47 # Our own
47 # Our own
48 from IPython import DPyGetOpt
48 from IPython import DPyGetOpt
49 from IPython.Struct import Struct
49 from IPython.Struct import Struct
50 from IPython.OutputTrap import OutputTrap
50 from IPython.OutputTrap import OutputTrap
51 from IPython.ConfigLoader import ConfigLoader
51 from IPython.ConfigLoader import ConfigLoader
52 from IPython.iplib import InteractiveShell,qw_lol,import_fail_info
52 from IPython.iplib import InteractiveShell,qw_lol,import_fail_info
53 from IPython.usage import cmd_line_usage,interactive_usage
53 from IPython.usage import cmd_line_usage,interactive_usage
54 from IPython.Prompts import CachedOutput
54 from IPython.Prompts import CachedOutput
55 from IPython.genutils import *
55 from IPython.genutils import *
56
56
57 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
58 def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,
58 def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,
59 rc_override=None,shell_class=InteractiveShell,
59 rc_override=None,shell_class=InteractiveShell,
60 embedded=False,**kw):
60 embedded=False,**kw):
61 """This is a dump of IPython into a single function.
61 """This is a dump of IPython into a single function.
62
62
63 Later it will have to be broken up in a sensible manner.
63 Later it will have to be broken up in a sensible manner.
64
64
65 Arguments:
65 Arguments:
66
66
67 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
67 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
68 script name, b/c DPyGetOpt strips the first argument only for the real
68 script name, b/c DPyGetOpt strips the first argument only for the real
69 sys.argv.
69 sys.argv.
70
70
71 - user_ns: a dict to be used as the user's namespace."""
71 - user_ns: a dict to be used as the user's namespace."""
72
72
73 #----------------------------------------------------------------------
73 #----------------------------------------------------------------------
74 # Defaults and initialization
74 # Defaults and initialization
75
75
76 # For developer debugging, deactivates crash handler and uses pdb.
76 # For developer debugging, deactivates crash handler and uses pdb.
77 DEVDEBUG = False
77 DEVDEBUG = False
78
78
79 if argv is None:
79 if argv is None:
80 argv = sys.argv
80 argv = sys.argv
81
81
82 # __IP is the main global that lives throughout and represents the whole
82 # __IP is the main global that lives throughout and represents the whole
83 # application. If the user redefines it, all bets are off as to what
83 # application. If the user redefines it, all bets are off as to what
84 # happens.
84 # happens.
85
85
86 # __IP is the name of he global which the caller will have accessible as
86 # __IP is the name of he global which the caller will have accessible as
87 # __IP.name. We set its name via the first parameter passed to
87 # __IP.name. We set its name via the first parameter passed to
88 # InteractiveShell:
88 # InteractiveShell:
89
89
90 IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns,
90 IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns,
91 embedded=embedded,**kw)
91 embedded=embedded,**kw)
92
92
93 # Put 'help' in the user namespace
93 # Put 'help' in the user namespace
94 from site import _Helper
94 from site import _Helper
95 IP.user_ns['help'] = _Helper()
95 IP.user_ns['help'] = _Helper()
96
96
97
97 if DEVDEBUG:
98 if DEVDEBUG:
98 # For developer debugging only (global flag)
99 # For developer debugging only (global flag)
99 from IPython import ultraTB
100 from IPython import ultraTB
100 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101 else:
102 # IPython itself shouldn't crash. This will produce a detailed
103 # post-mortem if it does
104 from IPython import CrashHandler
105 sys.excepthook = CrashHandler.CrashHandler(IP)
106
102
107 IP.BANNER_PARTS = ['Python %s\n'
103 IP.BANNER_PARTS = ['Python %s\n'
108 'Type "copyright", "credits" or "license" '
104 'Type "copyright", "credits" or "license" '
109 'for more information.\n'
105 'for more information.\n'
110 % (sys.version.split('\n')[0],),
106 % (sys.version.split('\n')[0],),
111 "IPython %s -- An enhanced Interactive Python."
107 "IPython %s -- An enhanced Interactive Python."
112 % (__version__,),
108 % (__version__,),
113 """? -> Introduction to IPython's features.
109 """? -> Introduction to IPython's features.
114 %magic -> Information about IPython's 'magic' % functions.
110 %magic -> Information about IPython's 'magic' % functions.
115 help -> Python's own help system.
111 help -> Python's own help system.
116 object? -> Details about 'object'. ?object also works, ?? prints more.
112 object? -> Details about 'object'. ?object also works, ?? prints more.
117 """ ]
113 """ ]
118
114
119 IP.usage = interactive_usage
115 IP.usage = interactive_usage
120
116
121 # Platform-dependent suffix and directory names. We use _ipython instead
117 # Platform-dependent suffix and directory names. We use _ipython instead
122 # of .ipython under win32 b/c there's software that breaks with .named
118 # of .ipython under win32 b/c there's software that breaks with .named
123 # directories on that platform.
119 # directories on that platform.
124 if os.name == 'posix':
120 if os.name == 'posix':
125 rc_suffix = ''
121 rc_suffix = ''
126 ipdir_def = '.ipython'
122 ipdir_def = '.ipython'
127 else:
123 else:
128 rc_suffix = '.ini'
124 rc_suffix = '.ini'
129 ipdir_def = '_ipython'
125 ipdir_def = '_ipython'
130
126
131 # default directory for configuration
127 # default directory for configuration
132 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
128 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
133 os.path.join(IP.home_dir,ipdir_def)))
129 os.path.join(IP.home_dir,ipdir_def)))
134
130
135 # we need the directory where IPython itself is installed
131 # we need the directory where IPython itself is installed
136 import IPython
132 import IPython
137 IPython_dir = os.path.dirname(IPython.__file__)
133 IPython_dir = os.path.dirname(IPython.__file__)
138 del IPython
134 del IPython
139
135
140 #-------------------------------------------------------------------------
136 #-------------------------------------------------------------------------
141 # Command line handling
137 # Command line handling
142
138
143 # Valid command line options (uses DPyGetOpt syntax, like Perl's
139 # Valid command line options (uses DPyGetOpt syntax, like Perl's
144 # GetOpt::Long)
140 # GetOpt::Long)
145
141
146 # Any key not listed here gets deleted even if in the file (like session
142 # Any key not listed here gets deleted even if in the file (like session
147 # or profile). That's deliberate, to maintain the rc namespace clean.
143 # or profile). That's deliberate, to maintain the rc namespace clean.
148
144
149 # Each set of options appears twice: under _conv only the names are
145 # Each set of options appears twice: under _conv only the names are
150 # listed, indicating which type they must be converted to when reading the
146 # listed, indicating which type they must be converted to when reading the
151 # ipythonrc file. And under DPyGetOpt they are listed with the regular
147 # ipythonrc file. And under DPyGetOpt they are listed with the regular
152 # DPyGetOpt syntax (=s,=i,:f,etc).
148 # DPyGetOpt syntax (=s,=i,:f,etc).
153
149
154 # Make sure there's a space before each end of line (they get auto-joined!)
150 # Make sure there's a space before each end of line (they get auto-joined!)
155 cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i '
151 cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i '
156 'c=s classic|cl color_info! colors=s confirm_exit! '
152 'c=s classic|cl color_info! colors=s confirm_exit! '
157 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
153 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
158 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
154 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
159 'quick screen_length|sl=i prompts_pad_left=i '
155 'quick screen_length|sl=i prompts_pad_left=i '
160 'logfile|lf=s logplay|lp=s profile|p=s '
156 'logfile|lf=s logplay|lp=s profile|p=s '
161 'readline! readline_merge_completions! '
157 'readline! readline_merge_completions! '
162 'readline_omit__names! '
158 'readline_omit__names! '
163 'rcfile=s separate_in|si=s separate_out|so=s '
159 'rcfile=s separate_in|si=s separate_out|so=s '
164 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
160 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
165 'magic_docstrings system_verbose! '
161 'magic_docstrings system_verbose! '
166 'multi_line_specials! '
162 'multi_line_specials! '
167 'autoedit_syntax!')
163 'autoedit_syntax!')
168
164
169 # Options that can *only* appear at the cmd line (not in rcfiles).
165 # Options that can *only* appear at the cmd line (not in rcfiles).
170
166
171 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
167 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
172 # the 'C-c !' command in emacs automatically appends a -i option at the end.
168 # the 'C-c !' command in emacs automatically appends a -i option at the end.
173 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
169 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
174 'gthread! qthread! wthread! pylab! tk!')
170 'gthread! qthread! wthread! pylab! tk!')
175
171
176 # Build the actual name list to be used by DPyGetOpt
172 # Build the actual name list to be used by DPyGetOpt
177 opts_names = qw(cmdline_opts) + qw(cmdline_only)
173 opts_names = qw(cmdline_opts) + qw(cmdline_only)
178
174
179 # Set sensible command line defaults.
175 # Set sensible command line defaults.
180 # This should have everything from cmdline_opts and cmdline_only
176 # This should have everything from cmdline_opts and cmdline_only
181 opts_def = Struct(autocall = 1,
177 opts_def = Struct(autocall = 1,
182 autoedit_syntax = 1,
178 autoedit_syntax = 1,
183 autoindent=0,
179 autoindent=0,
184 automagic = 1,
180 automagic = 1,
185 banner = 1,
181 banner = 1,
186 cache_size = 1000,
182 cache_size = 1000,
187 c = '',
183 c = '',
188 classic = 0,
184 classic = 0,
189 colors = 'NoColor',
185 colors = 'NoColor',
190 color_info = 0,
186 color_info = 0,
191 confirm_exit = 1,
187 confirm_exit = 1,
192 debug = 0,
188 debug = 0,
193 deep_reload = 0,
189 deep_reload = 0,
194 editor = '0',
190 editor = '0',
195 help = 0,
191 help = 0,
196 ignore = 0,
192 ignore = 0,
197 ipythondir = ipythondir,
193 ipythondir = ipythondir,
198 log = 0,
194 log = 0,
199 logfile = '',
195 logfile = '',
200 logplay = '',
196 logplay = '',
201 multi_line_specials = 1,
197 multi_line_specials = 1,
202 messages = 1,
198 messages = 1,
203 nosep = 0,
199 nosep = 0,
204 pdb = 0,
200 pdb = 0,
205 pprint = 0,
201 pprint = 0,
206 profile = '',
202 profile = '',
207 prompt_in1 = 'In [\\#]: ',
203 prompt_in1 = 'In [\\#]: ',
208 prompt_in2 = ' .\\D.: ',
204 prompt_in2 = ' .\\D.: ',
209 prompt_out = 'Out[\\#]: ',
205 prompt_out = 'Out[\\#]: ',
210 prompts_pad_left = 1,
206 prompts_pad_left = 1,
211 quick = 0,
207 quick = 0,
212 readline = 1,
208 readline = 1,
213 readline_merge_completions = 1,
209 readline_merge_completions = 1,
214 readline_omit__names = 0,
210 readline_omit__names = 0,
215 rcfile = 'ipythonrc' + rc_suffix,
211 rcfile = 'ipythonrc' + rc_suffix,
216 screen_length = 0,
212 screen_length = 0,
217 separate_in = '\n',
213 separate_in = '\n',
218 separate_out = '\n',
214 separate_out = '\n',
219 separate_out2 = '',
215 separate_out2 = '',
220 system_verbose = 0,
216 system_verbose = 0,
221 gthread = 0,
217 gthread = 0,
222 qthread = 0,
218 qthread = 0,
223 wthread = 0,
219 wthread = 0,
224 pylab = 0,
220 pylab = 0,
225 tk = 0,
221 tk = 0,
226 upgrade = 0,
222 upgrade = 0,
227 Version = 0,
223 Version = 0,
228 xmode = 'Verbose',
224 xmode = 'Verbose',
229 wildcards_case_sensitive = 1,
225 wildcards_case_sensitive = 1,
230 magic_docstrings = 0, # undocumented, for doc generation
226 magic_docstrings = 0, # undocumented, for doc generation
231 )
227 )
232
228
233 # Things that will *only* appear in rcfiles (not at the command line).
229 # Things that will *only* appear in rcfiles (not at the command line).
234 # Make sure there's a space before each end of line (they get auto-joined!)
230 # Make sure there's a space before each end of line (they get auto-joined!)
235 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
231 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
236 qw_lol: 'import_some ',
232 qw_lol: 'import_some ',
237 # for things with embedded whitespace:
233 # for things with embedded whitespace:
238 list_strings:'execute alias readline_parse_and_bind ',
234 list_strings:'execute alias readline_parse_and_bind ',
239 # Regular strings need no conversion:
235 # Regular strings need no conversion:
240 None:'readline_remove_delims ',
236 None:'readline_remove_delims ',
241 }
237 }
242 # Default values for these
238 # Default values for these
243 rc_def = Struct(include = [],
239 rc_def = Struct(include = [],
244 import_mod = [],
240 import_mod = [],
245 import_all = [],
241 import_all = [],
246 import_some = [[]],
242 import_some = [[]],
247 execute = [],
243 execute = [],
248 execfile = [],
244 execfile = [],
249 alias = [],
245 alias = [],
250 readline_parse_and_bind = [],
246 readline_parse_and_bind = [],
251 readline_remove_delims = '',
247 readline_remove_delims = '',
252 )
248 )
253
249
254 # Build the type conversion dictionary from the above tables:
250 # Build the type conversion dictionary from the above tables:
255 typeconv = rcfile_opts.copy()
251 typeconv = rcfile_opts.copy()
256 typeconv.update(optstr2types(cmdline_opts))
252 typeconv.update(optstr2types(cmdline_opts))
257
253
258 # FIXME: the None key appears in both, put that back together by hand. Ugly!
254 # FIXME: the None key appears in both, put that back together by hand. Ugly!
259 typeconv[None] += ' ' + rcfile_opts[None]
255 typeconv[None] += ' ' + rcfile_opts[None]
260
256
261 # Remove quotes at ends of all strings (used to protect spaces)
257 # Remove quotes at ends of all strings (used to protect spaces)
262 typeconv[unquote_ends] = typeconv[None]
258 typeconv[unquote_ends] = typeconv[None]
263 del typeconv[None]
259 del typeconv[None]
264
260
265 # Build the list we'll use to make all config decisions with defaults:
261 # Build the list we'll use to make all config decisions with defaults:
266 opts_all = opts_def.copy()
262 opts_all = opts_def.copy()
267 opts_all.update(rc_def)
263 opts_all.update(rc_def)
268
264
269 # Build conflict resolver for recursive loading of config files:
265 # Build conflict resolver for recursive loading of config files:
270 # - preserve means the outermost file maintains the value, it is not
266 # - preserve means the outermost file maintains the value, it is not
271 # overwritten if an included file has the same key.
267 # overwritten if an included file has the same key.
272 # - add_flip applies + to the two values, so it better make sense to add
268 # - add_flip applies + to the two values, so it better make sense to add
273 # those types of keys. But it flips them first so that things loaded
269 # those types of keys. But it flips them first so that things loaded
274 # deeper in the inclusion chain have lower precedence.
270 # deeper in the inclusion chain have lower precedence.
275 conflict = {'preserve': ' '.join([ typeconv[int],
271 conflict = {'preserve': ' '.join([ typeconv[int],
276 typeconv[unquote_ends] ]),
272 typeconv[unquote_ends] ]),
277 'add_flip': ' '.join([ typeconv[qwflat],
273 'add_flip': ' '.join([ typeconv[qwflat],
278 typeconv[qw_lol],
274 typeconv[qw_lol],
279 typeconv[list_strings] ])
275 typeconv[list_strings] ])
280 }
276 }
281
277
282 # Now actually process the command line
278 # Now actually process the command line
283 getopt = DPyGetOpt.DPyGetOpt()
279 getopt = DPyGetOpt.DPyGetOpt()
284 getopt.setIgnoreCase(0)
280 getopt.setIgnoreCase(0)
285
281
286 getopt.parseConfiguration(opts_names)
282 getopt.parseConfiguration(opts_names)
287
283
288 try:
284 try:
289 getopt.processArguments(argv)
285 getopt.processArguments(argv)
290 except:
286 except:
291 print cmd_line_usage
287 print cmd_line_usage
292 warn('\nError in Arguments: ' + `sys.exc_value`)
288 warn('\nError in Arguments: ' + `sys.exc_value`)
293 sys.exit(1)
289 sys.exit(1)
294
290
295 # convert the options dict to a struct for much lighter syntax later
291 # convert the options dict to a struct for much lighter syntax later
296 opts = Struct(getopt.optionValues)
292 opts = Struct(getopt.optionValues)
297 args = getopt.freeValues
293 args = getopt.freeValues
298
294
299 # this is the struct (which has default values at this point) with which
295 # this is the struct (which has default values at this point) with which
300 # we make all decisions:
296 # we make all decisions:
301 opts_all.update(opts)
297 opts_all.update(opts)
302
298
303 # Options that force an immediate exit
299 # Options that force an immediate exit
304 if opts_all.help:
300 if opts_all.help:
305 page(cmd_line_usage)
301 page(cmd_line_usage)
306 sys.exit()
302 sys.exit()
307
303
308 if opts_all.Version:
304 if opts_all.Version:
309 print __version__
305 print __version__
310 sys.exit()
306 sys.exit()
311
307
312 if opts_all.magic_docstrings:
308 if opts_all.magic_docstrings:
313 IP.magic_magic('-latex')
309 IP.magic_magic('-latex')
314 sys.exit()
310 sys.exit()
315
311
316 # Create user config directory if it doesn't exist. This must be done
312 # Create user config directory if it doesn't exist. This must be done
317 # *after* getting the cmd line options.
313 # *after* getting the cmd line options.
318 if not os.path.isdir(opts_all.ipythondir):
314 if not os.path.isdir(opts_all.ipythondir):
319 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
315 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
320
316
321 # upgrade user config files while preserving a copy of the originals
317 # upgrade user config files while preserving a copy of the originals
322 if opts_all.upgrade:
318 if opts_all.upgrade:
323 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
319 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
324
320
325 # check mutually exclusive options in the *original* command line
321 # check mutually exclusive options in the *original* command line
326 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
322 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
327 qw('classic profile'),qw('classic rcfile')])
323 qw('classic profile'),qw('classic rcfile')])
328
324
329 # default logfilename used when -log is called.
325 # default logfilename used when -log is called.
330 IP.LOGDEF = 'ipython.log'
326 IP.LOGDEF = 'ipython.log'
331
327
332 #---------------------------------------------------------------------------
328 #---------------------------------------------------------------------------
333 # Log replay
329 # Log replay
334
330
335 # if -logplay, we need to 'become' the other session. That basically means
331 # if -logplay, we need to 'become' the other session. That basically means
336 # replacing the current command line environment with that of the old
332 # replacing the current command line environment with that of the old
337 # session and moving on.
333 # session and moving on.
338
334
339 # this is needed so that later we know we're in session reload mode, as
335 # this is needed so that later we know we're in session reload mode, as
340 # opts_all will get overwritten:
336 # opts_all will get overwritten:
341 load_logplay = 0
337 load_logplay = 0
342
338
343 if opts_all.logplay:
339 if opts_all.logplay:
344 load_logplay = opts_all.logplay
340 load_logplay = opts_all.logplay
345 opts_debug_save = opts_all.debug
341 opts_debug_save = opts_all.debug
346 try:
342 try:
347 logplay = open(opts_all.logplay)
343 logplay = open(opts_all.logplay)
348 except IOError:
344 except IOError:
349 if opts_all.debug: IP.InteractiveTB()
345 if opts_all.debug: IP.InteractiveTB()
350 warn('Could not open logplay file '+`opts_all.logplay`)
346 warn('Could not open logplay file '+`opts_all.logplay`)
351 # restore state as if nothing had happened and move on, but make
347 # restore state as if nothing had happened and move on, but make
352 # sure that later we don't try to actually load the session file
348 # sure that later we don't try to actually load the session file
353 logplay = None
349 logplay = None
354 load_logplay = 0
350 load_logplay = 0
355 del opts_all.logplay
351 del opts_all.logplay
356 else:
352 else:
357 try:
353 try:
358 logplay.readline()
354 logplay.readline()
359 logplay.readline();
355 logplay.readline();
360 # this reloads that session's command line
356 # this reloads that session's command line
361 cmd = logplay.readline()[6:]
357 cmd = logplay.readline()[6:]
362 exec cmd
358 exec cmd
363 # restore the true debug flag given so that the process of
359 # restore the true debug flag given so that the process of
364 # session loading itself can be monitored.
360 # session loading itself can be monitored.
365 opts.debug = opts_debug_save
361 opts.debug = opts_debug_save
366 # save the logplay flag so later we don't overwrite the log
362 # save the logplay flag so later we don't overwrite the log
367 opts.logplay = load_logplay
363 opts.logplay = load_logplay
368 # now we must update our own structure with defaults
364 # now we must update our own structure with defaults
369 opts_all.update(opts)
365 opts_all.update(opts)
370 # now load args
366 # now load args
371 cmd = logplay.readline()[6:]
367 cmd = logplay.readline()[6:]
372 exec cmd
368 exec cmd
373 logplay.close()
369 logplay.close()
374 except:
370 except:
375 logplay.close()
371 logplay.close()
376 if opts_all.debug: IP.InteractiveTB()
372 if opts_all.debug: IP.InteractiveTB()
377 warn("Logplay file lacking full configuration information.\n"
373 warn("Logplay file lacking full configuration information.\n"
378 "I'll try to read it, but some things may not work.")
374 "I'll try to read it, but some things may not work.")
379
375
380 #-------------------------------------------------------------------------
376 #-------------------------------------------------------------------------
381 # set up output traps: catch all output from files, being run, modules
377 # set up output traps: catch all output from files, being run, modules
382 # loaded, etc. Then give it to the user in a clean form at the end.
378 # loaded, etc. Then give it to the user in a clean form at the end.
383
379
384 msg_out = 'Output messages. '
380 msg_out = 'Output messages. '
385 msg_err = 'Error messages. '
381 msg_err = 'Error messages. '
386 msg_sep = '\n'
382 msg_sep = '\n'
387 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
383 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
388 msg_err,msg_sep,debug,
384 msg_err,msg_sep,debug,
389 quiet_out=1),
385 quiet_out=1),
390 user_exec = OutputTrap('User File Execution',msg_out,
386 user_exec = OutputTrap('User File Execution',msg_out,
391 msg_err,msg_sep,debug),
387 msg_err,msg_sep,debug),
392 logplay = OutputTrap('Log Loader',msg_out,
388 logplay = OutputTrap('Log Loader',msg_out,
393 msg_err,msg_sep,debug),
389 msg_err,msg_sep,debug),
394 summary = ''
390 summary = ''
395 )
391 )
396
392
397 #-------------------------------------------------------------------------
393 #-------------------------------------------------------------------------
398 # Process user ipythonrc-type configuration files
394 # Process user ipythonrc-type configuration files
399
395
400 # turn on output trapping and log to msg.config
396 # turn on output trapping and log to msg.config
401 # remember that with debug on, trapping is actually disabled
397 # remember that with debug on, trapping is actually disabled
402 msg.config.trap_all()
398 msg.config.trap_all()
403
399
404 # look for rcfile in current or default directory
400 # look for rcfile in current or default directory
405 try:
401 try:
406 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
402 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
407 except IOError:
403 except IOError:
408 if opts_all.debug: IP.InteractiveTB()
404 if opts_all.debug: IP.InteractiveTB()
409 warn('Configuration file %s not found. Ignoring request.'
405 warn('Configuration file %s not found. Ignoring request.'
410 % (opts_all.rcfile) )
406 % (opts_all.rcfile) )
411
407
412 # 'profiles' are a shorthand notation for config filenames
408 # 'profiles' are a shorthand notation for config filenames
413 if opts_all.profile:
409 if opts_all.profile:
414 try:
410 try:
415 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
411 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
416 + rc_suffix,
412 + rc_suffix,
417 opts_all.ipythondir)
413 opts_all.ipythondir)
418 except IOError:
414 except IOError:
419 if opts_all.debug: IP.InteractiveTB()
415 if opts_all.debug: IP.InteractiveTB()
420 opts.profile = '' # remove profile from options if invalid
416 opts.profile = '' # remove profile from options if invalid
421 warn('Profile configuration file %s not found. Ignoring request.'
417 warn('Profile configuration file %s not found. Ignoring request.'
422 % (opts_all.profile) )
418 % (opts_all.profile) )
423
419
424 # load the config file
420 # load the config file
425 rcfiledata = None
421 rcfiledata = None
426 if opts_all.quick:
422 if opts_all.quick:
427 print 'Launching IPython in quick mode. No config file read.'
423 print 'Launching IPython in quick mode. No config file read.'
428 elif opts_all.classic:
424 elif opts_all.classic:
429 print 'Launching IPython in classic mode. No config file read.'
425 print 'Launching IPython in classic mode. No config file read.'
430 elif opts_all.rcfile:
426 elif opts_all.rcfile:
431 try:
427 try:
432 cfg_loader = ConfigLoader(conflict)
428 cfg_loader = ConfigLoader(conflict)
433 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
429 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
434 'include',opts_all.ipythondir,
430 'include',opts_all.ipythondir,
435 purge = 1,
431 purge = 1,
436 unique = conflict['preserve'])
432 unique = conflict['preserve'])
437 except:
433 except:
438 IP.InteractiveTB()
434 IP.InteractiveTB()
439 warn('Problems loading configuration file '+
435 warn('Problems loading configuration file '+
440 `opts_all.rcfile`+
436 `opts_all.rcfile`+
441 '\nStarting with default -bare bones- configuration.')
437 '\nStarting with default -bare bones- configuration.')
442 else:
438 else:
443 warn('No valid configuration file found in either currrent directory\n'+
439 warn('No valid configuration file found in either currrent directory\n'+
444 'or in the IPython config. directory: '+`opts_all.ipythondir`+
440 'or in the IPython config. directory: '+`opts_all.ipythondir`+
445 '\nProceeding with internal defaults.')
441 '\nProceeding with internal defaults.')
446
442
447 #------------------------------------------------------------------------
443 #------------------------------------------------------------------------
448 # Set exception handlers in mode requested by user.
444 # Set exception handlers in mode requested by user.
449 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
445 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
450 IP.magic_xmode(opts_all.xmode)
446 IP.magic_xmode(opts_all.xmode)
451 otrap.release_out()
447 otrap.release_out()
452
448
453 #------------------------------------------------------------------------
449 #------------------------------------------------------------------------
454 # Execute user config
450 # Execute user config
455
451
456 # Create a valid config structure with the right precedence order:
452 # Create a valid config structure with the right precedence order:
457 # defaults < rcfile < command line. This needs to be in the instance, so
453 # defaults < rcfile < command line. This needs to be in the instance, so
458 # that method calls below that rely on it find it.
454 # that method calls below that rely on it find it.
459 IP.rc = rc_def.copy()
455 IP.rc = rc_def.copy()
460
456
461 # Work with a local alias inside this routine to avoid unnecessary
457 # Work with a local alias inside this routine to avoid unnecessary
462 # attribute lookups.
458 # attribute lookups.
463 IP_rc = IP.rc
459 IP_rc = IP.rc
464
460
465 IP_rc.update(opts_def)
461 IP_rc.update(opts_def)
466 if rcfiledata:
462 if rcfiledata:
467 # now we can update
463 # now we can update
468 IP_rc.update(rcfiledata)
464 IP_rc.update(rcfiledata)
469 IP_rc.update(opts)
465 IP_rc.update(opts)
470 IP_rc.update(rc_override)
466 IP_rc.update(rc_override)
471
467
472 # Store the original cmd line for reference:
468 # Store the original cmd line for reference:
473 IP_rc.opts = opts
469 IP_rc.opts = opts
474 IP_rc.args = args
470 IP_rc.args = args
475
471
476 # create a *runtime* Struct like rc for holding parameters which may be
472 # create a *runtime* Struct like rc for holding parameters which may be
477 # created and/or modified by runtime user extensions.
473 # created and/or modified by runtime user extensions.
478 IP.runtime_rc = Struct()
474 IP.runtime_rc = Struct()
479
475
480 # from this point on, all config should be handled through IP_rc,
476 # from this point on, all config should be handled through IP_rc,
481 # opts* shouldn't be used anymore.
477 # opts* shouldn't be used anymore.
482
478
483 # add personal .ipython dir to sys.path so that users can put things in
479 # add personal .ipython dir to sys.path so that users can put things in
484 # there for customization
480 # there for customization
485 sys.path.append(IP_rc.ipythondir)
481 sys.path.append(IP_rc.ipythondir)
486 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
482 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
487
483
488 # update IP_rc with some special things that need manual
484 # update IP_rc with some special things that need manual
489 # tweaks. Basically options which affect other options. I guess this
485 # tweaks. Basically options which affect other options. I guess this
490 # should just be written so that options are fully orthogonal and we
486 # should just be written so that options are fully orthogonal and we
491 # wouldn't worry about this stuff!
487 # wouldn't worry about this stuff!
492
488
493 if IP_rc.classic:
489 if IP_rc.classic:
494 IP_rc.quick = 1
490 IP_rc.quick = 1
495 IP_rc.cache_size = 0
491 IP_rc.cache_size = 0
496 IP_rc.pprint = 0
492 IP_rc.pprint = 0
497 IP_rc.prompt_in1 = '>>> '
493 IP_rc.prompt_in1 = '>>> '
498 IP_rc.prompt_in2 = '... '
494 IP_rc.prompt_in2 = '... '
499 IP_rc.prompt_out = ''
495 IP_rc.prompt_out = ''
500 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
496 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
501 IP_rc.colors = 'NoColor'
497 IP_rc.colors = 'NoColor'
502 IP_rc.xmode = 'Plain'
498 IP_rc.xmode = 'Plain'
503
499
504 # configure readline
500 # configure readline
505 # Define the history file for saving commands in between sessions
501 # Define the history file for saving commands in between sessions
506 if IP_rc.profile:
502 if IP_rc.profile:
507 histfname = 'history-%s' % IP_rc.profile
503 histfname = 'history-%s' % IP_rc.profile
508 else:
504 else:
509 histfname = 'history'
505 histfname = 'history'
510 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
506 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
511
507
512 # update exception handlers with rc file status
508 # update exception handlers with rc file status
513 otrap.trap_out() # I don't want these messages ever.
509 otrap.trap_out() # I don't want these messages ever.
514 IP.magic_xmode(IP_rc.xmode)
510 IP.magic_xmode(IP_rc.xmode)
515 otrap.release_out()
511 otrap.release_out()
516
512
517 # activate logging if requested and not reloading a log
513 # activate logging if requested and not reloading a log
518 if IP_rc.logplay:
514 if IP_rc.logplay:
519 IP.magic_logstart(IP_rc.logplay + ' append')
515 IP.magic_logstart(IP_rc.logplay + ' append')
520 elif IP_rc.logfile:
516 elif IP_rc.logfile:
521 IP.magic_logstart(IP_rc.logfile)
517 IP.magic_logstart(IP_rc.logfile)
522 elif IP_rc.log:
518 elif IP_rc.log:
523 IP.magic_logstart()
519 IP.magic_logstart()
524
520
525 # find user editor so that it we don't have to look it up constantly
521 # find user editor so that it we don't have to look it up constantly
526 if IP_rc.editor.strip()=='0':
522 if IP_rc.editor.strip()=='0':
527 try:
523 try:
528 ed = os.environ['EDITOR']
524 ed = os.environ['EDITOR']
529 except KeyError:
525 except KeyError:
530 if os.name == 'posix':
526 if os.name == 'posix':
531 ed = 'vi' # the only one guaranteed to be there!
527 ed = 'vi' # the only one guaranteed to be there!
532 else:
528 else:
533 ed = 'notepad' # same in Windows!
529 ed = 'notepad' # same in Windows!
534 IP_rc.editor = ed
530 IP_rc.editor = ed
535
531
536 # Keep track of whether this is an embedded instance or not (useful for
532 # Keep track of whether this is an embedded instance or not (useful for
537 # post-mortems).
533 # post-mortems).
538 IP_rc.embedded = IP.embedded
534 IP_rc.embedded = IP.embedded
539
535
540 # Recursive reload
536 # Recursive reload
541 try:
537 try:
542 from IPython import deep_reload
538 from IPython import deep_reload
543 if IP_rc.deep_reload:
539 if IP_rc.deep_reload:
544 __builtin__.reload = deep_reload.reload
540 __builtin__.reload = deep_reload.reload
545 else:
541 else:
546 __builtin__.dreload = deep_reload.reload
542 __builtin__.dreload = deep_reload.reload
547 del deep_reload
543 del deep_reload
548 except ImportError:
544 except ImportError:
549 pass
545 pass
550
546
551 # Save the current state of our namespace so that the interactive shell
547 # Save the current state of our namespace so that the interactive shell
552 # can later know which variables have been created by us from config files
548 # can later know which variables have been created by us from config files
553 # and loading. This way, loading a file (in any way) is treated just like
549 # and loading. This way, loading a file (in any way) is treated just like
554 # defining things on the command line, and %who works as expected.
550 # defining things on the command line, and %who works as expected.
555
551
556 # DON'T do anything that affects the namespace beyond this point!
552 # DON'T do anything that affects the namespace beyond this point!
557 IP.internal_ns.update(__main__.__dict__)
553 IP.internal_ns.update(__main__.__dict__)
558
554
559 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
555 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
560
556
561 # Now run through the different sections of the users's config
557 # Now run through the different sections of the users's config
562 if IP_rc.debug:
558 if IP_rc.debug:
563 print 'Trying to execute the following configuration structure:'
559 print 'Trying to execute the following configuration structure:'
564 print '(Things listed first are deeper in the inclusion tree and get'
560 print '(Things listed first are deeper in the inclusion tree and get'
565 print 'loaded first).\n'
561 print 'loaded first).\n'
566 pprint(IP_rc.__dict__)
562 pprint(IP_rc.__dict__)
567
563
568 for mod in IP_rc.import_mod:
564 for mod in IP_rc.import_mod:
569 try:
565 try:
570 exec 'import '+mod in IP.user_ns
566 exec 'import '+mod in IP.user_ns
571 except :
567 except :
572 IP.InteractiveTB()
568 IP.InteractiveTB()
573 import_fail_info(mod)
569 import_fail_info(mod)
574
570
575 for mod_fn in IP_rc.import_some:
571 for mod_fn in IP_rc.import_some:
576 if mod_fn == []: break
572 if mod_fn == []: break
577 mod,fn = mod_fn[0],','.join(mod_fn[1:])
573 mod,fn = mod_fn[0],','.join(mod_fn[1:])
578 try:
574 try:
579 exec 'from '+mod+' import '+fn in IP.user_ns
575 exec 'from '+mod+' import '+fn in IP.user_ns
580 except :
576 except :
581 IP.InteractiveTB()
577 IP.InteractiveTB()
582 import_fail_info(mod,fn)
578 import_fail_info(mod,fn)
583
579
584 for mod in IP_rc.import_all:
580 for mod in IP_rc.import_all:
585 try:
581 try:
586 exec 'from '+mod+' import *' in IP.user_ns
582 exec 'from '+mod+' import *' in IP.user_ns
587 except :
583 except :
588 IP.InteractiveTB()
584 IP.InteractiveTB()
589 import_fail_info(mod)
585 import_fail_info(mod)
590
586
591 for code in IP_rc.execute:
587 for code in IP_rc.execute:
592 try:
588 try:
593 exec code in IP.user_ns
589 exec code in IP.user_ns
594 except:
590 except:
595 IP.InteractiveTB()
591 IP.InteractiveTB()
596 warn('Failure executing code: ' + `code`)
592 warn('Failure executing code: ' + `code`)
597
593
598 # Execute the files the user wants in ipythonrc
594 # Execute the files the user wants in ipythonrc
599 for file in IP_rc.execfile:
595 for file in IP_rc.execfile:
600 try:
596 try:
601 file = filefind(file,sys.path+[IPython_dir])
597 file = filefind(file,sys.path+[IPython_dir])
602 except IOError:
598 except IOError:
603 warn(itpl('File $file not found. Skipping it.'))
599 warn(itpl('File $file not found. Skipping it.'))
604 else:
600 else:
605 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
601 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
606
602
607 # release stdout and stderr and save config log into a global summary
603 # release stdout and stderr and save config log into a global summary
608 msg.config.release_all()
604 msg.config.release_all()
609 if IP_rc.messages:
605 if IP_rc.messages:
610 msg.summary += msg.config.summary_all()
606 msg.summary += msg.config.summary_all()
611
607
612 #------------------------------------------------------------------------
608 #------------------------------------------------------------------------
613 # Setup interactive session
609 # Setup interactive session
614
610
615 # Now we should be fully configured. We can then execute files or load
611 # Now we should be fully configured. We can then execute files or load
616 # things only needed for interactive use. Then we'll open the shell.
612 # things only needed for interactive use. Then we'll open the shell.
617
613
618 # Take a snapshot of the user namespace before opening the shell. That way
614 # Take a snapshot of the user namespace before opening the shell. That way
619 # we'll be able to identify which things were interactively defined and
615 # we'll be able to identify which things were interactively defined and
620 # which were defined through config files.
616 # which were defined through config files.
621 IP.user_config_ns = IP.user_ns.copy()
617 IP.user_config_ns = IP.user_ns.copy()
622
618
623 # Force reading a file as if it were a session log. Slower but safer.
619 # Force reading a file as if it were a session log. Slower but safer.
624 if load_logplay:
620 if load_logplay:
625 print 'Replaying log...'
621 print 'Replaying log...'
626 try:
622 try:
627 if IP_rc.debug:
623 if IP_rc.debug:
628 logplay_quiet = 0
624 logplay_quiet = 0
629 else:
625 else:
630 logplay_quiet = 1
626 logplay_quiet = 1
631
627
632 msg.logplay.trap_all()
628 msg.logplay.trap_all()
633 IP.safe_execfile(load_logplay,IP.user_ns,
629 IP.safe_execfile(load_logplay,IP.user_ns,
634 islog = 1, quiet = logplay_quiet)
630 islog = 1, quiet = logplay_quiet)
635 msg.logplay.release_all()
631 msg.logplay.release_all()
636 if IP_rc.messages:
632 if IP_rc.messages:
637 msg.summary += msg.logplay.summary_all()
633 msg.summary += msg.logplay.summary_all()
638 except:
634 except:
639 warn('Problems replaying logfile %s.' % load_logplay)
635 warn('Problems replaying logfile %s.' % load_logplay)
640 IP.InteractiveTB()
636 IP.InteractiveTB()
641
637
642 # Load remaining files in command line
638 # Load remaining files in command line
643 msg.user_exec.trap_all()
639 msg.user_exec.trap_all()
644
640
645 # Do NOT execute files named in the command line as scripts to be loaded
641 # Do NOT execute files named in the command line as scripts to be loaded
646 # by embedded instances. Doing so has the potential for an infinite
642 # by embedded instances. Doing so has the potential for an infinite
647 # recursion if there are exceptions thrown in the process.
643 # recursion if there are exceptions thrown in the process.
648
644
649 # XXX FIXME: the execution of user files should be moved out to after
645 # XXX FIXME: the execution of user files should be moved out to after
650 # ipython is fully initialized, just as if they were run via %run at the
646 # ipython is fully initialized, just as if they were run via %run at the
651 # ipython prompt. This would also give them the benefit of ipython's
647 # ipython prompt. This would also give them the benefit of ipython's
652 # nice tracebacks.
648 # nice tracebacks.
653
649
654 if not embedded and IP_rc.args:
650 if not embedded and IP_rc.args:
655 name_save = IP.user_ns['__name__']
651 name_save = IP.user_ns['__name__']
656 IP.user_ns['__name__'] = '__main__'
652 IP.user_ns['__name__'] = '__main__'
657 try:
653 try:
658 # Set our own excepthook in case the user code tries to call it
654 # Set our own excepthook in case the user code tries to call it
659 # directly. This prevents triggering the IPython crash handler.
655 # directly. This prevents triggering the IPython crash handler.
660 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
656 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
661 for run in args:
657 for run in args:
662 IP.safe_execfile(run,IP.user_ns)
658 IP.safe_execfile(run,IP.user_ns)
663 finally:
659 finally:
664 # Reset our crash handler in place
660 # Reset our crash handler in place
665 sys.excepthook = old_excepthook
661 sys.excepthook = old_excepthook
666
662
667 IP.user_ns['__name__'] = name_save
663 IP.user_ns['__name__'] = name_save
668
664
669 msg.user_exec.release_all()
665 msg.user_exec.release_all()
670 if IP_rc.messages:
666 if IP_rc.messages:
671 msg.summary += msg.user_exec.summary_all()
667 msg.summary += msg.user_exec.summary_all()
672
668
673 # since we can't specify a null string on the cmd line, 0 is the equivalent:
669 # since we can't specify a null string on the cmd line, 0 is the equivalent:
674 if IP_rc.nosep:
670 if IP_rc.nosep:
675 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
671 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
676 if IP_rc.separate_in == '0': IP_rc.separate_in = ''
672 if IP_rc.separate_in == '0': IP_rc.separate_in = ''
677 if IP_rc.separate_out == '0': IP_rc.separate_out = ''
673 if IP_rc.separate_out == '0': IP_rc.separate_out = ''
678 if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = ''
674 if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = ''
679 IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n')
675 IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n')
680 IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n')
676 IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n')
681 IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n')
677 IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n')
682
678
683 # Determine how many lines at the bottom of the screen are needed for
679 # Determine how many lines at the bottom of the screen are needed for
684 # showing prompts, so we can know wheter long strings are to be printed or
680 # showing prompts, so we can know wheter long strings are to be printed or
685 # paged:
681 # paged:
686 num_lines_bot = IP_rc.separate_in.count('\n')+1
682 num_lines_bot = IP_rc.separate_in.count('\n')+1
687 IP_rc.screen_length = IP_rc.screen_length - num_lines_bot
683 IP_rc.screen_length = IP_rc.screen_length - num_lines_bot
688 # Initialize cache, set in/out prompts and printing system
684 # Initialize cache, set in/out prompts and printing system
689 IP.outputcache = CachedOutput(IP_rc.cache_size,
685 IP.outputcache = CachedOutput(IP_rc.cache_size,
690 IP_rc.pprint,
686 IP_rc.pprint,
691 input_sep = IP_rc.separate_in,
687 input_sep = IP_rc.separate_in,
692 output_sep = IP_rc.separate_out,
688 output_sep = IP_rc.separate_out,
693 output_sep2 = IP_rc.separate_out2,
689 output_sep2 = IP_rc.separate_out2,
694 ps1 = IP_rc.prompt_in1,
690 ps1 = IP_rc.prompt_in1,
695 ps2 = IP_rc.prompt_in2,
691 ps2 = IP_rc.prompt_in2,
696 ps_out = IP_rc.prompt_out,
692 ps_out = IP_rc.prompt_out,
697 user_ns = IP.user_ns,
693 user_ns = IP.user_ns,
698 input_hist = IP.input_hist,
694 input_hist = IP.input_hist,
699 pad_left = IP_rc.prompts_pad_left)
695 pad_left = IP_rc.prompts_pad_left)
700
696
701 # user may have over-ridden the default print hook:
697 # user may have over-ridden the default print hook:
702 try:
698 try:
703 IP.outputcache.__class__.display = IP.hooks.display
699 IP.outputcache.__class__.display = IP.hooks.display
704 except AttributeError:
700 except AttributeError:
705 pass
701 pass
706
702
707 # Set calling of pdb on exceptions
703 # Set calling of pdb on exceptions
708 IP.InteractiveTB.call_pdb = IP_rc.pdb
704 IP.InteractiveTB.call_pdb = IP_rc.pdb
709
705
710 # I don't like assigning globally to sys, because it means when embedding
706 # I don't like assigning globally to sys, because it means when embedding
711 # instances, each embedded instance overrides the previous choice. But
707 # instances, each embedded instance overrides the previous choice. But
712 # sys.displayhook seems to be called internally by exec, so I don't see a
708 # sys.displayhook seems to be called internally by exec, so I don't see a
713 # way around it.
709 # way around it.
714 sys.displayhook = IP.outputcache
710 sys.displayhook = IP.outputcache
715
711
716 # we need to know globally if we're caching i/o or not
712 # we need to know globally if we're caching i/o or not
717 IP.do_full_cache = IP.outputcache.do_full_cache
713 IP.do_full_cache = IP.outputcache.do_full_cache
718
714
719 # configure startup banner
715 # configure startup banner
720 if IP_rc.c: # regular python doesn't print the banner with -c
716 if IP_rc.c: # regular python doesn't print the banner with -c
721 IP_rc.banner = 0
717 IP_rc.banner = 0
722 if IP_rc.banner:
718 if IP_rc.banner:
723 BANN_P = IP.BANNER_PARTS
719 BANN_P = IP.BANNER_PARTS
724 else:
720 else:
725 BANN_P = []
721 BANN_P = []
726
722
727 if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile)
723 if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile)
728
724
729 # add message log (possibly empty)
725 # add message log (possibly empty)
730 if msg.summary: BANN_P.append(msg.summary)
726 if msg.summary: BANN_P.append(msg.summary)
731 # Final banner is a string
727 # Final banner is a string
732 IP.BANNER = '\n'.join(BANN_P)
728 IP.BANNER = '\n'.join(BANN_P)
733
729
734 # Finalize the IPython instance. This assumes the rc structure is fully
730 # Finalize the IPython instance. This assumes the rc structure is fully
735 # in place.
731 # in place.
736 IP.post_config_initialization()
732 IP.post_config_initialization()
737
733
738 return IP
734 return IP
739 #************************ end of file <ipmaker.py> **************************
735 #************************ end of file <ipmaker.py> **************************
@@ -1,796 +1,796 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 ultraTB.py -- Spice up your tracebacks!
3 ultraTB.py -- Spice up your tracebacks!
4
4
5 * ColorTB
5 * ColorTB
6 I've always found it a bit hard to visually parse tracebacks in Python. The
6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 ColorTB class is a solution to that problem. It colors the different parts of a
7 ColorTB class is a solution to that problem. It colors the different parts of a
8 traceback in a manner similar to what you would expect from a syntax-highlighting
8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 text editor.
9 text editor.
10
10
11 Installation instructions for ColorTB:
11 Installation instructions for ColorTB:
12 import sys,ultraTB
12 import sys,ultraTB
13 sys.excepthook = ultraTB.ColorTB()
13 sys.excepthook = ultraTB.ColorTB()
14
14
15 * VerboseTB
15 * VerboseTB
16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 of useful info when a traceback occurs. Ping originally had it spit out HTML
17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 and intended it for CGI programmers, but why should they have all the fun? I
18 and intended it for CGI programmers, but why should they have all the fun? I
19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 but kind of neat, and maybe useful for long-running programs that you believe
20 but kind of neat, and maybe useful for long-running programs that you believe
21 are bug-free. If a crash *does* occur in that type of program you want details.
21 are bug-free. If a crash *does* occur in that type of program you want details.
22 Give it a shot--you'll love it or you'll hate it.
22 Give it a shot--you'll love it or you'll hate it.
23
23
24 Note:
24 Note:
25
25
26 The Verbose mode prints the variables currently visible where the exception
26 The Verbose mode prints the variables currently visible where the exception
27 happened (shortening their strings if too long). This can potentially be
27 happened (shortening their strings if too long). This can potentially be
28 very slow, if you happen to have a huge data structure whose string
28 very slow, if you happen to have a huge data structure whose string
29 representation is complex to compute. Your computer may appear to freeze for
29 representation is complex to compute. Your computer may appear to freeze for
30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 with Ctrl-C (maybe hitting it more than once).
31 with Ctrl-C (maybe hitting it more than once).
32
32
33 If you encounter this kind of situation often, you may want to use the
33 If you encounter this kind of situation often, you may want to use the
34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 variables (but otherwise includes the information and context given by
35 variables (but otherwise includes the information and context given by
36 Verbose).
36 Verbose).
37
37
38
38
39 Installation instructions for ColorTB:
39 Installation instructions for ColorTB:
40 import sys,ultraTB
40 import sys,ultraTB
41 sys.excepthook = ultraTB.VerboseTB()
41 sys.excepthook = ultraTB.VerboseTB()
42
42
43 Note: Much of the code in this module was lifted verbatim from the standard
43 Note: Much of the code in this module was lifted verbatim from the standard
44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45
45
46 * Color schemes
46 * Color schemes
47 The colors are defined in the class TBTools through the use of the
47 The colors are defined in the class TBTools through the use of the
48 ColorSchemeTable class. Currently the following exist:
48 ColorSchemeTable class. Currently the following exist:
49
49
50 - NoColor: allows all of this module to be used in any terminal (the color
50 - NoColor: allows all of this module to be used in any terminal (the color
51 escapes are just dummy blank strings).
51 escapes are just dummy blank strings).
52
52
53 - Linux: is meant to look good in a terminal like the Linux console (black
53 - Linux: is meant to look good in a terminal like the Linux console (black
54 or very dark background).
54 or very dark background).
55
55
56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 in light background terminals.
57 in light background terminals.
58
58
59 You can implement other color schemes easily, the syntax is fairly
59 You can implement other color schemes easily, the syntax is fairly
60 self-explanatory. Please send back new schemes you develop to the author for
60 self-explanatory. Please send back new schemes you develop to the author for
61 possible inclusion in future releases.
61 possible inclusion in future releases.
62
62
63 $Id: ultraTB.py 958 2005-12-27 23:17:51Z fperez $"""
63 $Id: ultraTB.py 965 2005-12-28 23:23:09Z fperez $"""
64
64
65 #*****************************************************************************
65 #*****************************************************************************
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
67 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
67 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
68 #
68 #
69 # Distributed under the terms of the BSD License. The full license is in
69 # Distributed under the terms of the BSD License. The full license is in
70 # the file COPYING, distributed as part of this software.
70 # the file COPYING, distributed as part of this software.
71 #*****************************************************************************
71 #*****************************************************************************
72
72
73 from IPython import Release
73 from IPython import Release
74 __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+
74 __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+
75 Release.authors['Fernando'])
75 Release.authors['Fernando'])
76 __license__ = Release.license
76 __license__ = Release.license
77
77
78 # Required modules
78 # Required modules
79 import inspect
79 import inspect
80 import keyword
80 import keyword
81 import linecache
81 import linecache
82 import os
82 import os
83 import pydoc
83 import pydoc
84 import string
84 import string
85 import sys
85 import sys
86 import time
86 import time
87 import tokenize
87 import tokenize
88 import traceback
88 import traceback
89 import types
89 import types
90
90
91 # IPython's own modules
91 # IPython's own modules
92 # Modified pdb which doesn't damage IPython's readline handling
92 # Modified pdb which doesn't damage IPython's readline handling
93 from IPython import Debugger
93 from IPython import Debugger
94 from IPython.Struct import Struct
94 from IPython.Struct import Struct
95 from IPython.excolors import ExceptionColors
95 from IPython.excolors import ExceptionColors
96 from IPython.genutils import Term,uniq_stable,error,info
96 from IPython.genutils import Term,uniq_stable,error,info
97
97
98 #---------------------------------------------------------------------------
98 #---------------------------------------------------------------------------
99 # Code begins
99 # Code begins
100
100
101 def inspect_error():
101 def inspect_error():
102 """Print a message about internal inspect errors.
102 """Print a message about internal inspect errors.
103
103
104 These are unfortunately quite common."""
104 These are unfortunately quite common."""
105
105
106 error('Internal Python error in the inspect module.\n'
106 error('Internal Python error in the inspect module.\n'
107 'Below is the traceback from this internal error.\n')
107 'Below is the traceback from this internal error.\n')
108
108
109 class TBTools:
109 class TBTools:
110 """Basic tools used by all traceback printer classes."""
110 """Basic tools used by all traceback printer classes."""
111
111
112 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
112 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
113 # Whether to call the interactive pdb debugger after printing
113 # Whether to call the interactive pdb debugger after printing
114 # tracebacks or not
114 # tracebacks or not
115 self.call_pdb = call_pdb
115 self.call_pdb = call_pdb
116
116
117 # Create color table
117 # Create color table
118 self.color_scheme_table = ExceptionColors
118 self.color_scheme_table = ExceptionColors
119
119
120 self.set_colors(color_scheme)
120 self.set_colors(color_scheme)
121 self.old_scheme = color_scheme # save initial value for toggles
121 self.old_scheme = color_scheme # save initial value for toggles
122
122
123 if call_pdb:
123 if call_pdb:
124 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
124 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
125 else:
125 else:
126 self.pdb = None
126 self.pdb = None
127
127
128 def set_colors(self,*args,**kw):
128 def set_colors(self,*args,**kw):
129 """Shorthand access to the color table scheme selector method."""
129 """Shorthand access to the color table scheme selector method."""
130
130
131 self.color_scheme_table.set_active_scheme(*args,**kw)
131 self.color_scheme_table.set_active_scheme(*args,**kw)
132 # for convenience, set Colors to the active scheme
132 # for convenience, set Colors to the active scheme
133 self.Colors = self.color_scheme_table.active_colors
133 self.Colors = self.color_scheme_table.active_colors
134
134
135 def color_toggle(self):
135 def color_toggle(self):
136 """Toggle between the currently active color scheme and NoColor."""
136 """Toggle between the currently active color scheme and NoColor."""
137
137
138 if self.color_scheme_table.active_scheme_name == 'NoColor':
138 if self.color_scheme_table.active_scheme_name == 'NoColor':
139 self.color_scheme_table.set_active_scheme(self.old_scheme)
139 self.color_scheme_table.set_active_scheme(self.old_scheme)
140 self.Colors = self.color_scheme_table.active_colors
140 self.Colors = self.color_scheme_table.active_colors
141 else:
141 else:
142 self.old_scheme = self.color_scheme_table.active_scheme_name
142 self.old_scheme = self.color_scheme_table.active_scheme_name
143 self.color_scheme_table.set_active_scheme('NoColor')
143 self.color_scheme_table.set_active_scheme('NoColor')
144 self.Colors = self.color_scheme_table.active_colors
144 self.Colors = self.color_scheme_table.active_colors
145
145
146 #---------------------------------------------------------------------------
146 #---------------------------------------------------------------------------
147 class ListTB(TBTools):
147 class ListTB(TBTools):
148 """Print traceback information from a traceback list, with optional color.
148 """Print traceback information from a traceback list, with optional color.
149
149
150 Calling: requires 3 arguments:
150 Calling: requires 3 arguments:
151 (etype, evalue, elist)
151 (etype, evalue, elist)
152 as would be obtained by:
152 as would be obtained by:
153 etype, evalue, tb = sys.exc_info()
153 etype, evalue, tb = sys.exc_info()
154 if tb:
154 if tb:
155 elist = traceback.extract_tb(tb)
155 elist = traceback.extract_tb(tb)
156 else:
156 else:
157 elist = None
157 elist = None
158
158
159 It can thus be used by programs which need to process the traceback before
159 It can thus be used by programs which need to process the traceback before
160 printing (such as console replacements based on the code module from the
160 printing (such as console replacements based on the code module from the
161 standard library).
161 standard library).
162
162
163 Because they are meant to be called without a full traceback (only a
163 Because they are meant to be called without a full traceback (only a
164 list), instances of this class can't call the interactive pdb debugger."""
164 list), instances of this class can't call the interactive pdb debugger."""
165
165
166 def __init__(self,color_scheme = 'NoColor'):
166 def __init__(self,color_scheme = 'NoColor'):
167 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
167 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
168
168
169 def __call__(self, etype, value, elist):
169 def __call__(self, etype, value, elist):
170 print >> Term.cerr, self.text(etype,value,elist)
170 print >> Term.cerr, self.text(etype,value,elist)
171
171
172 def text(self,etype, value, elist,context=5):
172 def text(self,etype, value, elist,context=5):
173 """Return a color formatted string with the traceback info."""
173 """Return a color formatted string with the traceback info."""
174
174
175 Colors = self.Colors
175 Colors = self.Colors
176 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
176 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
177 if elist:
177 if elist:
178 out_string.append('Traceback %s(most recent call last)%s:' % \
178 out_string.append('Traceback %s(most recent call last)%s:' % \
179 (Colors.normalEm, Colors.Normal) + '\n')
179 (Colors.normalEm, Colors.Normal) + '\n')
180 out_string.extend(self._format_list(elist))
180 out_string.extend(self._format_list(elist))
181 lines = self._format_exception_only(etype, value)
181 lines = self._format_exception_only(etype, value)
182 for line in lines[:-1]:
182 for line in lines[:-1]:
183 out_string.append(" "+line)
183 out_string.append(" "+line)
184 out_string.append(lines[-1])
184 out_string.append(lines[-1])
185 return ''.join(out_string)
185 return ''.join(out_string)
186
186
187 def _format_list(self, extracted_list):
187 def _format_list(self, extracted_list):
188 """Format a list of traceback entry tuples for printing.
188 """Format a list of traceback entry tuples for printing.
189
189
190 Given a list of tuples as returned by extract_tb() or
190 Given a list of tuples as returned by extract_tb() or
191 extract_stack(), return a list of strings ready for printing.
191 extract_stack(), return a list of strings ready for printing.
192 Each string in the resulting list corresponds to the item with the
192 Each string in the resulting list corresponds to the item with the
193 same index in the argument list. Each string ends in a newline;
193 same index in the argument list. Each string ends in a newline;
194 the strings may contain internal newlines as well, for those items
194 the strings may contain internal newlines as well, for those items
195 whose source text line is not None.
195 whose source text line is not None.
196
196
197 Lifted almost verbatim from traceback.py
197 Lifted almost verbatim from traceback.py
198 """
198 """
199
199
200 Colors = self.Colors
200 Colors = self.Colors
201 list = []
201 list = []
202 for filename, lineno, name, line in extracted_list[:-1]:
202 for filename, lineno, name, line in extracted_list[:-1]:
203 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
203 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
204 (Colors.filename, filename, Colors.Normal,
204 (Colors.filename, filename, Colors.Normal,
205 Colors.lineno, lineno, Colors.Normal,
205 Colors.lineno, lineno, Colors.Normal,
206 Colors.name, name, Colors.Normal)
206 Colors.name, name, Colors.Normal)
207 if line:
207 if line:
208 item = item + ' %s\n' % line.strip()
208 item = item + ' %s\n' % line.strip()
209 list.append(item)
209 list.append(item)
210 # Emphasize the last entry
210 # Emphasize the last entry
211 filename, lineno, name, line = extracted_list[-1]
211 filename, lineno, name, line = extracted_list[-1]
212 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
212 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
213 (Colors.normalEm,
213 (Colors.normalEm,
214 Colors.filenameEm, filename, Colors.normalEm,
214 Colors.filenameEm, filename, Colors.normalEm,
215 Colors.linenoEm, lineno, Colors.normalEm,
215 Colors.linenoEm, lineno, Colors.normalEm,
216 Colors.nameEm, name, Colors.normalEm,
216 Colors.nameEm, name, Colors.normalEm,
217 Colors.Normal)
217 Colors.Normal)
218 if line:
218 if line:
219 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
219 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
220 Colors.Normal)
220 Colors.Normal)
221 list.append(item)
221 list.append(item)
222 return list
222 return list
223
223
224 def _format_exception_only(self, etype, value):
224 def _format_exception_only(self, etype, value):
225 """Format the exception part of a traceback.
225 """Format the exception part of a traceback.
226
226
227 The arguments are the exception type and value such as given by
227 The arguments are the exception type and value such as given by
228 sys.last_type and sys.last_value. The return value is a list of
228 sys.last_type and sys.last_value. The return value is a list of
229 strings, each ending in a newline. Normally, the list contains a
229 strings, each ending in a newline. Normally, the list contains a
230 single string; however, for SyntaxError exceptions, it contains
230 single string; however, for SyntaxError exceptions, it contains
231 several lines that (when printed) display detailed information
231 several lines that (when printed) display detailed information
232 about where the syntax error occurred. The message indicating
232 about where the syntax error occurred. The message indicating
233 which exception occurred is the always last string in the list.
233 which exception occurred is the always last string in the list.
234
234
235 Also lifted nearly verbatim from traceback.py
235 Also lifted nearly verbatim from traceback.py
236 """
236 """
237
237
238 Colors = self.Colors
238 Colors = self.Colors
239 list = []
239 list = []
240 if type(etype) == types.ClassType:
240 if type(etype) == types.ClassType:
241 stype = Colors.excName + etype.__name__ + Colors.Normal
241 stype = Colors.excName + etype.__name__ + Colors.Normal
242 else:
242 else:
243 stype = etype # String exceptions don't get special coloring
243 stype = etype # String exceptions don't get special coloring
244 if value is None:
244 if value is None:
245 list.append( str(stype) + '\n')
245 list.append( str(stype) + '\n')
246 else:
246 else:
247 if etype is SyntaxError:
247 if etype is SyntaxError:
248 try:
248 try:
249 msg, (filename, lineno, offset, line) = value
249 msg, (filename, lineno, offset, line) = value
250 except:
250 except:
251 pass
251 pass
252 else:
252 else:
253 #print 'filename is',filename # dbg
253 #print 'filename is',filename # dbg
254 if not filename: filename = "<string>"
254 if not filename: filename = "<string>"
255 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
255 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
256 (Colors.normalEm,
256 (Colors.normalEm,
257 Colors.filenameEm, filename, Colors.normalEm,
257 Colors.filenameEm, filename, Colors.normalEm,
258 Colors.linenoEm, lineno, Colors.Normal ))
258 Colors.linenoEm, lineno, Colors.Normal ))
259 if line is not None:
259 if line is not None:
260 i = 0
260 i = 0
261 while i < len(line) and line[i].isspace():
261 while i < len(line) and line[i].isspace():
262 i = i+1
262 i = i+1
263 list.append('%s %s%s\n' % (Colors.line,
263 list.append('%s %s%s\n' % (Colors.line,
264 line.strip(),
264 line.strip(),
265 Colors.Normal))
265 Colors.Normal))
266 if offset is not None:
266 if offset is not None:
267 s = ' '
267 s = ' '
268 for c in line[i:offset-1]:
268 for c in line[i:offset-1]:
269 if c.isspace():
269 if c.isspace():
270 s = s + c
270 s = s + c
271 else:
271 else:
272 s = s + ' '
272 s = s + ' '
273 list.append('%s%s^%s\n' % (Colors.caret, s,
273 list.append('%s%s^%s\n' % (Colors.caret, s,
274 Colors.Normal) )
274 Colors.Normal) )
275 value = msg
275 value = msg
276 s = self._some_str(value)
276 s = self._some_str(value)
277 if s:
277 if s:
278 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
278 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
279 Colors.Normal, s))
279 Colors.Normal, s))
280 else:
280 else:
281 list.append('%s\n' % str(stype))
281 list.append('%s\n' % str(stype))
282 return list
282 return list
283
283
284 def _some_str(self, value):
284 def _some_str(self, value):
285 # Lifted from traceback.py
285 # Lifted from traceback.py
286 try:
286 try:
287 return str(value)
287 return str(value)
288 except:
288 except:
289 return '<unprintable %s object>' % type(value).__name__
289 return '<unprintable %s object>' % type(value).__name__
290
290
291 #----------------------------------------------------------------------------
291 #----------------------------------------------------------------------------
292 class VerboseTB(TBTools):
292 class VerboseTB(TBTools):
293 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
293 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
294 of HTML. Requires inspect and pydoc. Crazy, man.
294 of HTML. Requires inspect and pydoc. Crazy, man.
295
295
296 Modified version which optionally strips the topmost entries from the
296 Modified version which optionally strips the topmost entries from the
297 traceback, to be used with alternate interpreters (because their own code
297 traceback, to be used with alternate interpreters (because their own code
298 would appear in the traceback)."""
298 would appear in the traceback)."""
299
299
300 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
300 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
301 call_pdb = 0, include_vars=1):
301 call_pdb = 0, include_vars=1):
302 """Specify traceback offset, headers and color scheme.
302 """Specify traceback offset, headers and color scheme.
303
303
304 Define how many frames to drop from the tracebacks. Calling it with
304 Define how many frames to drop from the tracebacks. Calling it with
305 tb_offset=1 allows use of this handler in interpreters which will have
305 tb_offset=1 allows use of this handler in interpreters which will have
306 their own code at the top of the traceback (VerboseTB will first
306 their own code at the top of the traceback (VerboseTB will first
307 remove that frame before printing the traceback info)."""
307 remove that frame before printing the traceback info)."""
308 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
308 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
309 self.tb_offset = tb_offset
309 self.tb_offset = tb_offset
310 self.long_header = long_header
310 self.long_header = long_header
311 self.include_vars = include_vars
311 self.include_vars = include_vars
312
312
313 def text(self, etype, evalue, etb, context=5):
313 def text(self, etype, evalue, etb, context=5):
314 """Return a nice text document describing the traceback."""
314 """Return a nice text document describing the traceback."""
315
315
316 # some locals
316 # some locals
317 Colors = self.Colors # just a shorthand + quicker name lookup
317 Colors = self.Colors # just a shorthand + quicker name lookup
318 ColorsNormal = Colors.Normal # used a lot
318 ColorsNormal = Colors.Normal # used a lot
319 indent_size = 8 # we need some space to put line numbers before
319 indent_size = 8 # we need some space to put line numbers before
320 indent = ' '*indent_size
320 indent = ' '*indent_size
321 numbers_width = indent_size - 1 # leave space between numbers & code
321 numbers_width = indent_size - 1 # leave space between numbers & code
322 text_repr = pydoc.text.repr
322 text_repr = pydoc.text.repr
323 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
323 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
324 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
324 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
325 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
325 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
326
326
327 # some internal-use functions
327 # some internal-use functions
328 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
328 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
329 def nullrepr(value, repr=text_repr): return ''
329 def nullrepr(value, repr=text_repr): return ''
330
330
331 # meat of the code begins
331 # meat of the code begins
332 if type(etype) is types.ClassType:
332 if type(etype) is types.ClassType:
333 etype = etype.__name__
333 etype = etype.__name__
334
334
335 if self.long_header:
335 if self.long_header:
336 # Header with the exception type, python version, and date
336 # Header with the exception type, python version, and date
337 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
337 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
338 date = time.ctime(time.time())
338 date = time.ctime(time.time())
339
339
340 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
340 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
341 exc, ' '*(75-len(str(etype))-len(pyver)),
341 exc, ' '*(75-len(str(etype))-len(pyver)),
342 pyver, string.rjust(date, 75) )
342 pyver, string.rjust(date, 75) )
343 head += "\nA problem occured executing Python code. Here is the sequence of function"\
343 head += "\nA problem occured executing Python code. Here is the sequence of function"\
344 "\ncalls leading up to the error, with the most recent (innermost) call last."
344 "\ncalls leading up to the error, with the most recent (innermost) call last."
345 else:
345 else:
346 # Simplified header
346 # Simplified header
347 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
347 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
348 string.rjust('Traceback (most recent call last)',
348 string.rjust('Traceback (most recent call last)',
349 75 - len(str(etype)) ) )
349 75 - len(str(etype)) ) )
350 frames = []
350 frames = []
351 # Flush cache before calling inspect. This helps alleviate some of the
351 # Flush cache before calling inspect. This helps alleviate some of the
352 # problems with python 2.3's inspect.py.
352 # problems with python 2.3's inspect.py.
353 linecache.checkcache()
353 linecache.checkcache()
354 # Drop topmost frames if requested
354 # Drop topmost frames if requested
355 try:
355 try:
356 records = inspect.getinnerframes(etb, context)[self.tb_offset:]
356 records = inspect.getinnerframes(etb, context)[self.tb_offset:]
357 except:
357 except:
358
358
359 # FIXME: I've been getting many crash reports from python 2.3
359 # FIXME: I've been getting many crash reports from python 2.3
360 # users, traceable to inspect.py. If I can find a small test-case
360 # users, traceable to inspect.py. If I can find a small test-case
361 # to reproduce this, I should either write a better workaround or
361 # to reproduce this, I should either write a better workaround or
362 # file a bug report against inspect (if that's the real problem).
362 # file a bug report against inspect (if that's the real problem).
363 # So far, I haven't been able to find an isolated example to
363 # So far, I haven't been able to find an isolated example to
364 # reproduce the problem.
364 # reproduce the problem.
365 inspect_error()
365 inspect_error()
366 traceback.print_exc(file=Term.cerr)
366 traceback.print_exc(file=Term.cerr)
367 info('\nUnfortunately, your original traceback can not be constructed.\n')
367 info('\nUnfortunately, your original traceback can not be constructed.\n')
368 return ''
368 return ''
369
369
370 # build some color string templates outside these nested loops
370 # build some color string templates outside these nested loops
371 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
371 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
372 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
372 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
373 ColorsNormal)
373 ColorsNormal)
374 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
374 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
375 (Colors.vName, Colors.valEm, ColorsNormal)
375 (Colors.vName, Colors.valEm, ColorsNormal)
376 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
376 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
377 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
377 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
378 Colors.vName, ColorsNormal)
378 Colors.vName, ColorsNormal)
379 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
379 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
380 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
380 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
381 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
381 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
382 ColorsNormal)
382 ColorsNormal)
383
383
384 # now, loop over all records printing context and info
384 # now, loop over all records printing context and info
385 abspath = os.path.abspath
385 abspath = os.path.abspath
386 for frame, file, lnum, func, lines, index in records:
386 for frame, file, lnum, func, lines, index in records:
387 #print '*** record:',file,lnum,func,lines,index # dbg
387 #print '*** record:',file,lnum,func,lines,index # dbg
388 try:
388 try:
389 file = file and abspath(file) or '?'
389 file = file and abspath(file) or '?'
390 except OSError:
390 except OSError:
391 # if file is '<console>' or something not in the filesystem,
391 # if file is '<console>' or something not in the filesystem,
392 # the abspath call will throw an OSError. Just ignore it and
392 # the abspath call will throw an OSError. Just ignore it and
393 # keep the original file string.
393 # keep the original file string.
394 pass
394 pass
395 link = tpl_link % file
395 link = tpl_link % file
396 try:
396 try:
397 args, varargs, varkw, locals = inspect.getargvalues(frame)
397 args, varargs, varkw, locals = inspect.getargvalues(frame)
398 except:
398 except:
399 # This can happen due to a bug in python2.3. We should be
399 # This can happen due to a bug in python2.3. We should be
400 # able to remove this try/except when 2.4 becomes a
400 # able to remove this try/except when 2.4 becomes a
401 # requirement. Bug details at http://python.org/sf/1005466
401 # requirement. Bug details at http://python.org/sf/1005466
402 inspect_error()
402 inspect_error()
403 traceback.print_exc(file=Term.cerr)
403 traceback.print_exc(file=Term.cerr)
404 info("\nIPython's exception reporting continues...\n")
404 info("\nIPython's exception reporting continues...\n")
405
405
406 if func == '?':
406 if func == '?':
407 call = ''
407 call = ''
408 else:
408 else:
409 # Decide whether to include variable details or not
409 # Decide whether to include variable details or not
410 var_repr = self.include_vars and eqrepr or nullrepr
410 var_repr = self.include_vars and eqrepr or nullrepr
411 try:
411 try:
412 call = tpl_call % (func,inspect.formatargvalues(args,
412 call = tpl_call % (func,inspect.formatargvalues(args,
413 varargs, varkw,
413 varargs, varkw,
414 locals,formatvalue=var_repr))
414 locals,formatvalue=var_repr))
415 except KeyError:
415 except KeyError:
416 # Very odd crash from inspect.formatargvalues(). The
416 # Very odd crash from inspect.formatargvalues(). The
417 # scenario under which it appeared was a call to
417 # scenario under which it appeared was a call to
418 # view(array,scale) in NumTut.view.view(), where scale had
418 # view(array,scale) in NumTut.view.view(), where scale had
419 # been defined as a scalar (it should be a tuple). Somehow
419 # been defined as a scalar (it should be a tuple). Somehow
420 # inspect messes up resolving the argument list of view()
420 # inspect messes up resolving the argument list of view()
421 # and barfs out. At some point I should dig into this one
421 # and barfs out. At some point I should dig into this one
422 # and file a bug report about it.
422 # and file a bug report about it.
423 inspect_error()
423 inspect_error()
424 traceback.print_exc(file=Term.cerr)
424 traceback.print_exc(file=Term.cerr)
425 info("\nIPython's exception reporting continues...\n")
425 info("\nIPython's exception reporting continues...\n")
426 call = tpl_call_fail % func
426 call = tpl_call_fail % func
427
427
428 # Initialize a list of names on the current line, which the
428 # Initialize a list of names on the current line, which the
429 # tokenizer below will populate.
429 # tokenizer below will populate.
430 names = []
430 names = []
431
431
432 def tokeneater(token_type, token, start, end, line):
432 def tokeneater(token_type, token, start, end, line):
433 """Stateful tokeneater which builds dotted names.
433 """Stateful tokeneater which builds dotted names.
434
434
435 The list of names it appends to (from the enclosing scope) can
435 The list of names it appends to (from the enclosing scope) can
436 contain repeated composite names. This is unavoidable, since
436 contain repeated composite names. This is unavoidable, since
437 there is no way to disambguate partial dotted structures until
437 there is no way to disambguate partial dotted structures until
438 the full list is known. The caller is responsible for pruning
438 the full list is known. The caller is responsible for pruning
439 the final list of duplicates before using it."""
439 the final list of duplicates before using it."""
440
440
441 # build composite names
441 # build composite names
442 if token == '.':
442 if token == '.':
443 try:
443 try:
444 names[-1] += '.'
444 names[-1] += '.'
445 # store state so the next token is added for x.y.z names
445 # store state so the next token is added for x.y.z names
446 tokeneater.name_cont = True
446 tokeneater.name_cont = True
447 return
447 return
448 except IndexError:
448 except IndexError:
449 pass
449 pass
450 if token_type == tokenize.NAME and token not in keyword.kwlist:
450 if token_type == tokenize.NAME and token not in keyword.kwlist:
451 if tokeneater.name_cont:
451 if tokeneater.name_cont:
452 # Dotted names
452 # Dotted names
453 names[-1] += token
453 names[-1] += token
454 tokeneater.name_cont = False
454 tokeneater.name_cont = False
455 else:
455 else:
456 # Regular new names. We append everything, the caller
456 # Regular new names. We append everything, the caller
457 # will be responsible for pruning the list later. It's
457 # will be responsible for pruning the list later. It's
458 # very tricky to try to prune as we go, b/c composite
458 # very tricky to try to prune as we go, b/c composite
459 # names can fool us. The pruning at the end is easy
459 # names can fool us. The pruning at the end is easy
460 # to do (or the caller can print a list with repeated
460 # to do (or the caller can print a list with repeated
461 # names if so desired.
461 # names if so desired.
462 names.append(token)
462 names.append(token)
463 elif token_type == tokenize.NEWLINE:
463 elif token_type == tokenize.NEWLINE:
464 raise IndexError
464 raise IndexError
465 # we need to store a bit of state in the tokenizer to build
465 # we need to store a bit of state in the tokenizer to build
466 # dotted names
466 # dotted names
467 tokeneater.name_cont = False
467 tokeneater.name_cont = False
468
468
469 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
469 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
470 line = getline(file, lnum[0])
470 line = getline(file, lnum[0])
471 lnum[0] += 1
471 lnum[0] += 1
472 return line
472 return line
473
473
474 # Build the list of names on this line of code where the exception
474 # Build the list of names on this line of code where the exception
475 # occurred.
475 # occurred.
476 try:
476 try:
477 # This builds the names list in-place by capturing it from the
477 # This builds the names list in-place by capturing it from the
478 # enclosing scope.
478 # enclosing scope.
479 tokenize.tokenize(linereader, tokeneater)
479 tokenize.tokenize(linereader, tokeneater)
480 except IndexError:
480 except IndexError:
481 # signals exit of tokenizer
481 # signals exit of tokenizer
482 pass
482 pass
483 except tokenize.TokenError,msg:
483 except tokenize.TokenError,msg:
484 _m = ("An unexpected error occurred while tokenizing input\n"
484 _m = ("An unexpected error occurred while tokenizing input\n"
485 "The following traceback may be corrupted or invalid\n"
485 "The following traceback may be corrupted or invalid\n"
486 "The error message is: %s\n" % msg)
486 "The error message is: %s\n" % msg)
487 error(_m)
487 error(_m)
488
488
489 # prune names list of duplicates, but keep the right order
489 # prune names list of duplicates, but keep the right order
490 unique_names = uniq_stable(names)
490 unique_names = uniq_stable(names)
491
491
492 # Start loop over vars
492 # Start loop over vars
493 lvals = []
493 lvals = []
494 if self.include_vars:
494 if self.include_vars:
495 for name_full in unique_names:
495 for name_full in unique_names:
496 name_base = name_full.split('.',1)[0]
496 name_base = name_full.split('.',1)[0]
497 if name_base in frame.f_code.co_varnames:
497 if name_base in frame.f_code.co_varnames:
498 if locals.has_key(name_base):
498 if locals.has_key(name_base):
499 try:
499 try:
500 value = repr(eval(name_full,locals))
500 value = repr(eval(name_full,locals))
501 except:
501 except:
502 value = undefined
502 value = undefined
503 else:
503 else:
504 value = undefined
504 value = undefined
505 name = tpl_local_var % name_full
505 name = tpl_local_var % name_full
506 else:
506 else:
507 if frame.f_globals.has_key(name_base):
507 if frame.f_globals.has_key(name_base):
508 try:
508 try:
509 value = repr(eval(name_full,frame.f_globals))
509 value = repr(eval(name_full,frame.f_globals))
510 except:
510 except:
511 value = undefined
511 value = undefined
512 else:
512 else:
513 value = undefined
513 value = undefined
514 name = tpl_global_var % name_full
514 name = tpl_global_var % name_full
515 lvals.append(tpl_name_val % (name,value))
515 lvals.append(tpl_name_val % (name,value))
516 if lvals:
516 if lvals:
517 lvals = '%s%s' % (indent,em_normal.join(lvals))
517 lvals = '%s%s' % (indent,em_normal.join(lvals))
518 else:
518 else:
519 lvals = ''
519 lvals = ''
520
520
521 level = '%s %s\n' % (link,call)
521 level = '%s %s\n' % (link,call)
522 excerpt = []
522 excerpt = []
523 if index is not None:
523 if index is not None:
524 i = lnum - index
524 i = lnum - index
525 for line in lines:
525 for line in lines:
526 if i == lnum:
526 if i == lnum:
527 # This is the line with the error
527 # This is the line with the error
528 pad = numbers_width - len(str(i))
528 pad = numbers_width - len(str(i))
529 if pad >= 3:
529 if pad >= 3:
530 marker = '-'*(pad-3) + '-> '
530 marker = '-'*(pad-3) + '-> '
531 elif pad == 2:
531 elif pad == 2:
532 marker = '> '
532 marker = '> '
533 elif pad == 1:
533 elif pad == 1:
534 marker = '>'
534 marker = '>'
535 else:
535 else:
536 marker = ''
536 marker = ''
537 num = '%s%s' % (marker,i)
537 num = '%s%s' % (marker,i)
538 line = tpl_line_em % (num,line)
538 line = tpl_line_em % (num,line)
539 else:
539 else:
540 num = '%*s' % (numbers_width,i)
540 num = '%*s' % (numbers_width,i)
541 line = tpl_line % (num,line)
541 line = tpl_line % (num,line)
542
542
543 excerpt.append(line)
543 excerpt.append(line)
544 if self.include_vars and i == lnum:
544 if self.include_vars and i == lnum:
545 excerpt.append('%s\n' % lvals)
545 excerpt.append('%s\n' % lvals)
546 i += 1
546 i += 1
547 frames.append('%s%s' % (level,''.join(excerpt)) )
547 frames.append('%s%s' % (level,''.join(excerpt)) )
548
548
549 # Get (safely) a string form of the exception info
549 # Get (safely) a string form of the exception info
550 try:
550 try:
551 etype_str,evalue_str = map(str,(etype,evalue))
551 etype_str,evalue_str = map(str,(etype,evalue))
552 except:
552 except:
553 # User exception is improperly defined.
553 # User exception is improperly defined.
554 etype,evalue = str,sys.exc_info()[:2]
554 etype,evalue = str,sys.exc_info()[:2]
555 etype_str,evalue_str = map(str,(etype,evalue))
555 etype_str,evalue_str = map(str,(etype,evalue))
556 # ... and format it
556 # ... and format it
557 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
557 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
558 ColorsNormal, evalue_str)]
558 ColorsNormal, evalue_str)]
559 if type(evalue) is types.InstanceType:
559 if type(evalue) is types.InstanceType:
560 try:
560 try:
561 names = [w for w in dir(evalue) if isinstance(w, basestring)]
561 names = [w for w in dir(evalue) if isinstance(w, basestring)]
562 except:
562 except:
563 # Every now and then, an object with funny inernals blows up
563 # Every now and then, an object with funny inernals blows up
564 # when dir() is called on it. We do the best we can to report
564 # when dir() is called on it. We do the best we can to report
565 # the problem and continue
565 # the problem and continue
566 _m = '%sException reporting error (object with broken dir())%s:'
566 _m = '%sException reporting error (object with broken dir())%s:'
567 exception.append(_m % (Colors.excName,ColorsNormal))
567 exception.append(_m % (Colors.excName,ColorsNormal))
568 etype_str,evalue_str = map(str,sys.exc_info()[:2])
568 etype_str,evalue_str = map(str,sys.exc_info()[:2])
569 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
569 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
570 ColorsNormal, evalue_str))
570 ColorsNormal, evalue_str))
571 names = []
571 names = []
572 for name in names:
572 for name in names:
573 value = text_repr(getattr(evalue, name))
573 value = text_repr(getattr(evalue, name))
574 exception.append('\n%s%s = %s' % (indent, name, value))
574 exception.append('\n%s%s = %s' % (indent, name, value))
575 # return all our info assembled as a single string
575 # return all our info assembled as a single string
576 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
576 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
577
577
578 def debugger(self):
578 def debugger(self):
579 """Call up the pdb debugger if desired, always clean up the tb reference.
579 """Call up the pdb debugger if desired, always clean up the tb reference.
580
580
581 If the call_pdb flag is set, the pdb interactive debugger is
581 If the call_pdb flag is set, the pdb interactive debugger is
582 invoked. In all cases, the self.tb reference to the current traceback
582 invoked. In all cases, the self.tb reference to the current traceback
583 is deleted to prevent lingering references which hamper memory
583 is deleted to prevent lingering references which hamper memory
584 management.
584 management.
585
585
586 Note that each call to pdb() does an 'import readline', so if your app
586 Note that each call to pdb() does an 'import readline', so if your app
587 requires a special setup for the readline completers, you'll have to
587 requires a special setup for the readline completers, you'll have to
588 fix that by hand after invoking the exception handler."""
588 fix that by hand after invoking the exception handler."""
589
589
590 if self.call_pdb:
590 if self.call_pdb:
591 if self.pdb is None:
591 if self.pdb is None:
592 self.pdb = Debugger.Pdb(
592 self.pdb = Debugger.Pdb(
593 self.color_scheme_table.active_scheme_name)
593 self.color_scheme_table.active_scheme_name)
594 # the system displayhook may have changed, restore the original
594 # the system displayhook may have changed, restore the original
595 # for pdb
595 # for pdb
596 dhook = sys.displayhook
596 dhook = sys.displayhook
597 sys.displayhook = sys.__displayhook__
597 sys.displayhook = sys.__displayhook__
598 self.pdb.reset()
598 self.pdb.reset()
599 # Find the right frame so we don't pop up inside ipython itself
599 # Find the right frame so we don't pop up inside ipython itself
600 etb = self.tb
600 etb = self.tb
601 while self.tb.tb_next is not None:
601 while self.tb.tb_next is not None:
602 self.tb = self.tb.tb_next
602 self.tb = self.tb.tb_next
603 try:
603 try:
604 if etb and etb.tb_next:
604 if etb and etb.tb_next:
605 etb = etb.tb_next
605 etb = etb.tb_next
606 self.pdb.botframe = etb.tb_frame
606 self.pdb.botframe = etb.tb_frame
607 self.pdb.interaction(self.tb.tb_frame, self.tb)
607 self.pdb.interaction(self.tb.tb_frame, self.tb)
608 except:
608 except:
609 print '*** ERROR ***'
609 print '*** ERROR ***'
610 print 'This version of pdb has a bug and crashed.'
610 print 'This version of pdb has a bug and crashed.'
611 print 'Returning to IPython...'
611 print 'Returning to IPython...'
612 sys.displayhook = dhook
612 sys.displayhook = dhook
613 del self.tb
613 del self.tb
614
614
615 def handler(self, info=None):
615 def handler(self, info=None):
616 (etype, evalue, etb) = info or sys.exc_info()
616 (etype, evalue, etb) = info or sys.exc_info()
617 self.tb = etb
617 self.tb = etb
618 print >> Term.cerr, self.text(etype, evalue, etb)
618 print >> Term.cerr, self.text(etype, evalue, etb)
619
619
620 # Changed so an instance can just be called as VerboseTB_inst() and print
620 # Changed so an instance can just be called as VerboseTB_inst() and print
621 # out the right info on its own.
621 # out the right info on its own.
622 def __call__(self, etype=None, evalue=None, etb=None):
622 def __call__(self, etype=None, evalue=None, etb=None):
623 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
623 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
624 if etb is not None:
624 if etb is None:
625 self.handler((etype, evalue, etb))
626 else:
627 self.handler()
625 self.handler()
626 else:
627 self.handler((etype, evalue, etb))
628 self.debugger()
628 self.debugger()
629
629
630 #----------------------------------------------------------------------------
630 #----------------------------------------------------------------------------
631 class FormattedTB(VerboseTB,ListTB):
631 class FormattedTB(VerboseTB,ListTB):
632 """Subclass ListTB but allow calling with a traceback.
632 """Subclass ListTB but allow calling with a traceback.
633
633
634 It can thus be used as a sys.excepthook for Python > 2.1.
634 It can thus be used as a sys.excepthook for Python > 2.1.
635
635
636 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
636 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
637
637
638 Allows a tb_offset to be specified. This is useful for situations where
638 Allows a tb_offset to be specified. This is useful for situations where
639 one needs to remove a number of topmost frames from the traceback (such as
639 one needs to remove a number of topmost frames from the traceback (such as
640 occurs with python programs that themselves execute other python code,
640 occurs with python programs that themselves execute other python code,
641 like Python shells). """
641 like Python shells). """
642
642
643 def __init__(self, mode = 'Plain', color_scheme='Linux',
643 def __init__(self, mode = 'Plain', color_scheme='Linux',
644 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
644 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
645
645
646 # NEVER change the order of this list. Put new modes at the end:
646 # NEVER change the order of this list. Put new modes at the end:
647 self.valid_modes = ['Plain','Context','Verbose']
647 self.valid_modes = ['Plain','Context','Verbose']
648 self.verbose_modes = self.valid_modes[1:3]
648 self.verbose_modes = self.valid_modes[1:3]
649
649
650 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
650 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
651 call_pdb=call_pdb,include_vars=include_vars)
651 call_pdb=call_pdb,include_vars=include_vars)
652 self.set_mode(mode)
652 self.set_mode(mode)
653
653
654 def _extract_tb(self,tb):
654 def _extract_tb(self,tb):
655 if tb:
655 if tb:
656 return traceback.extract_tb(tb)
656 return traceback.extract_tb(tb)
657 else:
657 else:
658 return None
658 return None
659
659
660 def text(self, etype, value, tb,context=5,mode=None):
660 def text(self, etype, value, tb,context=5,mode=None):
661 """Return formatted traceback.
661 """Return formatted traceback.
662
662
663 If the optional mode parameter is given, it overrides the current
663 If the optional mode parameter is given, it overrides the current
664 mode."""
664 mode."""
665
665
666 if mode is None:
666 if mode is None:
667 mode = self.mode
667 mode = self.mode
668 if mode in self.verbose_modes:
668 if mode in self.verbose_modes:
669 # verbose modes need a full traceback
669 # verbose modes need a full traceback
670 return VerboseTB.text(self,etype, value, tb,context=5)
670 return VerboseTB.text(self,etype, value, tb,context=5)
671 else:
671 else:
672 # We must check the source cache because otherwise we can print
672 # We must check the source cache because otherwise we can print
673 # out-of-date source code.
673 # out-of-date source code.
674 linecache.checkcache()
674 linecache.checkcache()
675 # Now we can extract and format the exception
675 # Now we can extract and format the exception
676 elist = self._extract_tb(tb)
676 elist = self._extract_tb(tb)
677 if len(elist) > self.tb_offset:
677 if len(elist) > self.tb_offset:
678 del elist[:self.tb_offset]
678 del elist[:self.tb_offset]
679 return ListTB.text(self,etype,value,elist)
679 return ListTB.text(self,etype,value,elist)
680
680
681 def set_mode(self,mode=None):
681 def set_mode(self,mode=None):
682 """Switch to the desired mode.
682 """Switch to the desired mode.
683
683
684 If mode is not specified, cycles through the available modes."""
684 If mode is not specified, cycles through the available modes."""
685
685
686 if not mode:
686 if not mode:
687 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
687 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
688 len(self.valid_modes)
688 len(self.valid_modes)
689 self.mode = self.valid_modes[new_idx]
689 self.mode = self.valid_modes[new_idx]
690 elif mode not in self.valid_modes:
690 elif mode not in self.valid_modes:
691 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
691 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
692 'Valid modes: '+str(self.valid_modes)
692 'Valid modes: '+str(self.valid_modes)
693 else:
693 else:
694 self.mode = mode
694 self.mode = mode
695 # include variable details only in 'Verbose' mode
695 # include variable details only in 'Verbose' mode
696 self.include_vars = (self.mode == self.valid_modes[2])
696 self.include_vars = (self.mode == self.valid_modes[2])
697
697
698 # some convenient shorcuts
698 # some convenient shorcuts
699 def plain(self):
699 def plain(self):
700 self.set_mode(self.valid_modes[0])
700 self.set_mode(self.valid_modes[0])
701
701
702 def context(self):
702 def context(self):
703 self.set_mode(self.valid_modes[1])
703 self.set_mode(self.valid_modes[1])
704
704
705 def verbose(self):
705 def verbose(self):
706 self.set_mode(self.valid_modes[2])
706 self.set_mode(self.valid_modes[2])
707
707
708 #----------------------------------------------------------------------------
708 #----------------------------------------------------------------------------
709 class AutoFormattedTB(FormattedTB):
709 class AutoFormattedTB(FormattedTB):
710 """A traceback printer which can be called on the fly.
710 """A traceback printer which can be called on the fly.
711
711
712 It will find out about exceptions by itself.
712 It will find out about exceptions by itself.
713
713
714 A brief example:
714 A brief example:
715
715
716 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
716 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
717 try:
717 try:
718 ...
718 ...
719 except:
719 except:
720 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
720 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
721 """
721 """
722 def __call__(self,etype=None,evalue=None,etb=None,
722 def __call__(self,etype=None,evalue=None,etb=None,
723 out=None,tb_offset=None):
723 out=None,tb_offset=None):
724 """Print out a formatted exception traceback.
724 """Print out a formatted exception traceback.
725
725
726 Optional arguments:
726 Optional arguments:
727 - out: an open file-like object to direct output to.
727 - out: an open file-like object to direct output to.
728
728
729 - tb_offset: the number of frames to skip over in the stack, on a
729 - tb_offset: the number of frames to skip over in the stack, on a
730 per-call basis (this overrides temporarily the instance's tb_offset
730 per-call basis (this overrides temporarily the instance's tb_offset
731 given at initialization time. """
731 given at initialization time. """
732
732
733 if out is None:
733 if out is None:
734 out = Term.cerr
734 out = Term.cerr
735 if tb_offset is not None:
735 if tb_offset is not None:
736 tb_offset, self.tb_offset = self.tb_offset, tb_offset
736 tb_offset, self.tb_offset = self.tb_offset, tb_offset
737 print >> out, self.text(etype, evalue, etb)
737 print >> out, self.text(etype, evalue, etb)
738 self.tb_offset = tb_offset
738 self.tb_offset = tb_offset
739 else:
739 else:
740 print >> out, self.text(etype, evalue, etb)
740 print >> out, self.text(etype, evalue, etb)
741 self.debugger()
741 self.debugger()
742
742
743 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
743 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
744 if etype is None:
744 if etype is None:
745 etype,value,tb = sys.exc_info()
745 etype,value,tb = sys.exc_info()
746 self.tb = tb
746 self.tb = tb
747 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
747 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
748
748
749 #---------------------------------------------------------------------------
749 #---------------------------------------------------------------------------
750 # A simple class to preserve Nathan's original functionality.
750 # A simple class to preserve Nathan's original functionality.
751 class ColorTB(FormattedTB):
751 class ColorTB(FormattedTB):
752 """Shorthand to initialize a FormattedTB in Linux colors mode."""
752 """Shorthand to initialize a FormattedTB in Linux colors mode."""
753 def __init__(self,color_scheme='Linux',call_pdb=0):
753 def __init__(self,color_scheme='Linux',call_pdb=0):
754 FormattedTB.__init__(self,color_scheme=color_scheme,
754 FormattedTB.__init__(self,color_scheme=color_scheme,
755 call_pdb=call_pdb)
755 call_pdb=call_pdb)
756
756
757 #----------------------------------------------------------------------------
757 #----------------------------------------------------------------------------
758 # module testing (minimal)
758 # module testing (minimal)
759 if __name__ == "__main__":
759 if __name__ == "__main__":
760 def spam(c, (d, e)):
760 def spam(c, (d, e)):
761 x = c + d
761 x = c + d
762 y = c * d
762 y = c * d
763 foo(x, y)
763 foo(x, y)
764
764
765 def foo(a, b, bar=1):
765 def foo(a, b, bar=1):
766 eggs(a, b + bar)
766 eggs(a, b + bar)
767
767
768 def eggs(f, g, z=globals()):
768 def eggs(f, g, z=globals()):
769 h = f + g
769 h = f + g
770 i = f - g
770 i = f - g
771 return h / i
771 return h / i
772
772
773 print ''
773 print ''
774 print '*** Before ***'
774 print '*** Before ***'
775 try:
775 try:
776 print spam(1, (2, 3))
776 print spam(1, (2, 3))
777 except:
777 except:
778 traceback.print_exc()
778 traceback.print_exc()
779 print ''
779 print ''
780
780
781 handler = ColorTB()
781 handler = ColorTB()
782 print '*** ColorTB ***'
782 print '*** ColorTB ***'
783 try:
783 try:
784 print spam(1, (2, 3))
784 print spam(1, (2, 3))
785 except:
785 except:
786 apply(handler, sys.exc_info() )
786 apply(handler, sys.exc_info() )
787 print ''
787 print ''
788
788
789 handler = VerboseTB()
789 handler = VerboseTB()
790 print '*** VerboseTB ***'
790 print '*** VerboseTB ***'
791 try:
791 try:
792 print spam(1, (2, 3))
792 print spam(1, (2, 3))
793 except:
793 except:
794 apply(handler, sys.exc_info() )
794 apply(handler, sys.exc_info() )
795 print ''
795 print ''
796
796
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now