##// END OF EJS Templates
Changed %hist to default to NOT printing numbers, added -p and -o options....
Fernando Perez -
Show More
@@ -1,254 +1,274 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ History related magics and functionality """
2 """ History related magics and functionality """
3
3
4 # Stdlib imports
4 # Stdlib imports
5 import fnmatch
5 import fnmatch
6 import os
6 import os
7
7
8 from IPython.utils.genutils import Term, ask_yes_no, warn
8 from IPython.utils.genutils import Term, ask_yes_no, warn
9 from IPython.core import ipapi
9 from IPython.core import ipapi
10
10
11 def magic_history(self, parameter_s = ''):
11 def magic_history(self, parameter_s = ''):
12 """Print input history (_i<n> variables), with most recent last.
12 """Print input history (_i<n> variables), with most recent last.
13
13
14 %history -> print at most 40 inputs (some may be multi-line)\\
14 %history -> print at most 40 inputs (some may be multi-line)\\
15 %history n -> print at most n inputs\\
15 %history n -> print at most n inputs\\
16 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
16 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
17
18 Each input's number <n> is shown, and is accessible as the
19 automatically generated variable _i<n>. Multi-line statements are
20 printed starting at a new line for easy copy/paste.
21
22
17
23 Options:
18 By default, input history is printed without line numbers so it can be
19 directly pasted into an editor.
20
21 With -n, each input's number <n> is shown, and is accessible as the
22 automatically generated variable _i<n> as well as In[<n>]. Multi-line
23 statements are printed starting at a new line for easy copy/paste.
24
24
25 -n: do NOT print line numbers. This is useful if you want to get a
25 Options:
26 printout of many lines which can be directly pasted into a text
27 editor.
28
26
27 -n: print line numbers for each input.
29 This feature is only available if numbered prompts are in use.
28 This feature is only available if numbered prompts are in use.
30
29
30 -o: also print outputs for each input.
31
32 -p: print classic '>>>' python prompts before each input. This is useful
33 for making documentation, and in conjunction with -o, for producing
34 doctest-ready output.
35
31 -t: (default) print the 'translated' history, as IPython understands it.
36 -t: (default) print the 'translated' history, as IPython understands it.
32 IPython filters your input and converts it all into valid Python source
37 IPython filters your input and converts it all into valid Python source
33 before executing it (things like magics or aliases are turned into
38 before executing it (things like magics or aliases are turned into
34 function calls, for example). With this option, you'll see the native
39 function calls, for example). With this option, you'll see the native
35 history instead of the user-entered version: '%cd /' will be seen as
40 history instead of the user-entered version: '%cd /' will be seen as
36 '_ip.magic("%cd /")' instead of '%cd /'.
41 '_ip.magic("%cd /")' instead of '%cd /'.
37
42
38 -r: print the 'raw' history, i.e. the actual commands you typed.
43 -r: print the 'raw' history, i.e. the actual commands you typed.
39
44
40 -g: treat the arg as a pattern to grep for in (full) history.
45 -g: treat the arg as a pattern to grep for in (full) history.
41 This includes the "shadow history" (almost all commands ever written).
46 This includes the "shadow history" (almost all commands ever written).
42 Use '%hist -g' to show full shadow history (may be very long).
47 Use '%hist -g' to show full shadow history (may be very long).
43 In shadow history, every index nuwber starts with 0.
48 In shadow history, every index nuwber starts with 0.
44
49
45 -f FILENAME: instead of printing the output to the screen, redirect it to
50 -f FILENAME: instead of printing the output to the screen, redirect it to
46 the given file. The file is always overwritten, though IPython asks for
51 the given file. The file is always overwritten, though IPython asks for
47 confirmation first if it already exists.
52 confirmation first if it already exists.
48 """
53 """
49
54
50 if not self.outputcache.do_full_cache:
55 if not self.outputcache.do_full_cache:
51 print 'This feature is only available if numbered prompts are in use.'
56 print 'This feature is only available if numbered prompts are in use.'
52 return
57 return
53 opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')
58 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
54
59
55 # Check if output to specific file was requested.
60 # Check if output to specific file was requested.
56 try:
61 try:
57 outfname = opts['f']
62 outfname = opts['f']
58 except KeyError:
63 except KeyError:
59 outfile = Term.cout # default
64 outfile = Term.cout # default
60 # We don't want to close stdout at the end!
65 # We don't want to close stdout at the end!
61 close_at_end = False
66 close_at_end = False
62 else:
67 else:
63 if os.path.exists(outfname):
68 if os.path.exists(outfname):
64 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
69 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
65 print 'Aborting.'
70 print 'Aborting.'
66 return
71 return
67
72
68 outfile = open(outfname,'w')
73 outfile = open(outfname,'w')
69 close_at_end = True
74 close_at_end = True
70
75
71 if 't' in opts:
76 if 't' in opts:
72 input_hist = self.input_hist
77 input_hist = self.input_hist
73 elif 'r' in opts:
78 elif 'r' in opts:
74 input_hist = self.input_hist_raw
79 input_hist = self.input_hist_raw
75 else:
80 else:
76 input_hist = self.input_hist
81 input_hist = self.input_hist
77
82
78 default_length = 40
83 default_length = 40
79 pattern = None
84 pattern = None
80 if 'g' in opts:
85 if 'g' in opts:
81 init = 1
86 init = 1
82 final = len(input_hist)
87 final = len(input_hist)
83 parts = parameter_s.split(None,1)
88 parts = parameter_s.split(None,1)
84 if len(parts) == 1:
89 if len(parts) == 1:
85 parts += '*'
90 parts += '*'
86 head, pattern = parts
91 head, pattern = parts
87 pattern = "*" + pattern + "*"
92 pattern = "*" + pattern + "*"
88 elif len(args) == 0:
93 elif len(args) == 0:
89 final = len(input_hist)
94 final = len(input_hist)
90 init = max(1,final-default_length)
95 init = max(1,final-default_length)
91 elif len(args) == 1:
96 elif len(args) == 1:
92 final = len(input_hist)
97 final = len(input_hist)
93 init = max(1,final-int(args[0]))
98 init = max(1,final-int(args[0]))
94 elif len(args) == 2:
99 elif len(args) == 2:
95 init,final = map(int,args)
100 init,final = map(int,args)
96 else:
101 else:
97 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
102 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
98 print self.magic_hist.__doc__
103 print self.magic_hist.__doc__
99 return
104 return
105
100 width = len(str(final))
106 width = len(str(final))
101 line_sep = ['','\n']
107 line_sep = ['','\n']
102 print_nums = not opts.has_key('n')
108 print_nums = 'n' in opts
109 print_outputs = 'o' in opts
110 pyprompts = 'p' in opts
103
111
104 found = False
112 found = False
105 if pattern is not None:
113 if pattern is not None:
106 sh = self.shadowhist.all()
114 sh = self.shadowhist.all()
107 for idx, s in sh:
115 for idx, s in sh:
108 if fnmatch.fnmatch(s, pattern):
116 if fnmatch.fnmatch(s, pattern):
109 print "0%d: %s" %(idx, s)
117 print "0%d: %s" %(idx, s)
110 found = True
118 found = True
111
119
112 if found:
120 if found:
113 print "==="
121 print "==="
114 print "shadow history ends, fetch by %rep <number> (must start with 0)"
122 print "shadow history ends, fetch by %rep <number> (must start with 0)"
115 print "=== start of normal history ==="
123 print "=== start of normal history ==="
116
124
117 for in_num in range(init,final):
125 for in_num in range(init,final):
118 inline = input_hist[in_num]
126 inline = input_hist[in_num]
119 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
127 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
120 continue
128 continue
121
129
122 multiline = int(inline.count('\n') > 1)
130 multiline = int(inline.count('\n') > 1)
123 if print_nums:
131 if print_nums:
124 print >> outfile, \
132 print >> outfile, \
125 '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
133 '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
126 print >> outfile, inline,
134 if pyprompts:
135 print >> outfile, '>>>',
136 if multiline:
137 lines = inline.splitlines()
138 print >> outfile, '\n... '.join(lines)
139 print >> outfile, '... '
140 else:
141 print >> outfile, inline,
142 else:
143 print >> outfile, inline,
144 output = self.shell.user_ns['Out'].get(in_num)
145 if output is not None:
146 print repr(output)
127
147
128 if close_at_end:
148 if close_at_end:
129 outfile.close()
149 outfile.close()
130
150
131
151
132 def magic_hist(self, parameter_s=''):
152 def magic_hist(self, parameter_s=''):
133 """Alternate name for %history."""
153 """Alternate name for %history."""
134 return self.magic_history(parameter_s)
154 return self.magic_history(parameter_s)
135
155
136
156
137 def rep_f(self, arg):
157 def rep_f(self, arg):
138 r""" Repeat a command, or get command to input line for editing
158 r""" Repeat a command, or get command to input line for editing
139
159
140 - %rep (no arguments):
160 - %rep (no arguments):
141
161
142 Place a string version of last computation result (stored in the special '_'
162 Place a string version of last computation result (stored in the special '_'
143 variable) to the next input prompt. Allows you to create elaborate command
163 variable) to the next input prompt. Allows you to create elaborate command
144 lines without using copy-paste::
164 lines without using copy-paste::
145
165
146 $ l = ["hei", "vaan"]
166 $ l = ["hei", "vaan"]
147 $ "".join(l)
167 $ "".join(l)
148 ==> heivaan
168 ==> heivaan
149 $ %rep
169 $ %rep
150 $ heivaan_ <== cursor blinking
170 $ heivaan_ <== cursor blinking
151
171
152 %rep 45
172 %rep 45
153
173
154 Place history line 45 to next input prompt. Use %hist to find out the
174 Place history line 45 to next input prompt. Use %hist to find out the
155 number.
175 number.
156
176
157 %rep 1-4 6-7 3
177 %rep 1-4 6-7 3
158
178
159 Repeat the specified lines immediately. Input slice syntax is the same as
179 Repeat the specified lines immediately. Input slice syntax is the same as
160 in %macro and %save.
180 in %macro and %save.
161
181
162 %rep foo
182 %rep foo
163
183
164 Place the most recent line that has the substring "foo" to next input.
184 Place the most recent line that has the substring "foo" to next input.
165 (e.g. 'svn ci -m foobar').
185 (e.g. 'svn ci -m foobar').
166 """
186 """
167
187
168 opts,args = self.parse_options(arg,'',mode='list')
188 opts,args = self.parse_options(arg,'',mode='list')
169 if not args:
189 if not args:
170 self.set_next_input(str(self.user_ns["_"]))
190 self.set_next_input(str(self.user_ns["_"]))
171 return
191 return
172
192
173 if len(args) == 1 and not '-' in args[0]:
193 if len(args) == 1 and not '-' in args[0]:
174 arg = args[0]
194 arg = args[0]
175 if len(arg) > 1 and arg.startswith('0'):
195 if len(arg) > 1 and arg.startswith('0'):
176 # get from shadow hist
196 # get from shadow hist
177 num = int(arg[1:])
197 num = int(arg[1:])
178 line = self.shadowhist.get(num)
198 line = self.shadowhist.get(num)
179 self.set_next_input(str(line))
199 self.set_next_input(str(line))
180 return
200 return
181 try:
201 try:
182 num = int(args[0])
202 num = int(args[0])
183 self.set_next_input(str(self.input_hist_raw[num]).rstrip())
203 self.set_next_input(str(self.input_hist_raw[num]).rstrip())
184 return
204 return
185 except ValueError:
205 except ValueError:
186 pass
206 pass
187
207
188 for h in reversed(self.input_hist_raw):
208 for h in reversed(self.input_hist_raw):
189 if 'rep' in h:
209 if 'rep' in h:
190 continue
210 continue
191 if fnmatch.fnmatch(h,'*' + arg + '*'):
211 if fnmatch.fnmatch(h,'*' + arg + '*'):
192 self.set_next_input(str(h).rstrip())
212 self.set_next_input(str(h).rstrip())
193 return
213 return
194
214
195 try:
215 try:
196 lines = self.extract_input_slices(args, True)
216 lines = self.extract_input_slices(args, True)
197 print "lines",lines
217 print "lines",lines
198 self.runlines(lines)
218 self.runlines(lines)
199 except ValueError:
219 except ValueError:
200 print "Not found in recent history:", args
220 print "Not found in recent history:", args
201
221
202
222
203 _sentinel = object()
223 _sentinel = object()
204
224
205 class ShadowHist(object):
225 class ShadowHist(object):
206 def __init__(self,db):
226 def __init__(self,db):
207 # cmd => idx mapping
227 # cmd => idx mapping
208 self.curidx = 0
228 self.curidx = 0
209 self.db = db
229 self.db = db
210 self.disabled = False
230 self.disabled = False
211
231
212 def inc_idx(self):
232 def inc_idx(self):
213 idx = self.db.get('shadowhist_idx', 1)
233 idx = self.db.get('shadowhist_idx', 1)
214 self.db['shadowhist_idx'] = idx + 1
234 self.db['shadowhist_idx'] = idx + 1
215 return idx
235 return idx
216
236
217 def add(self, ent):
237 def add(self, ent):
218 if self.disabled:
238 if self.disabled:
219 return
239 return
220 try:
240 try:
221 old = self.db.hget('shadowhist', ent, _sentinel)
241 old = self.db.hget('shadowhist', ent, _sentinel)
222 if old is not _sentinel:
242 if old is not _sentinel:
223 return
243 return
224 newidx = self.inc_idx()
244 newidx = self.inc_idx()
225 #print "new",newidx # dbg
245 #print "new",newidx # dbg
226 self.db.hset('shadowhist',ent, newidx)
246 self.db.hset('shadowhist',ent, newidx)
227 except:
247 except:
228 ipapi.get().showtraceback()
248 ipapi.get().showtraceback()
229 print "WARNING: disabling shadow history"
249 print "WARNING: disabling shadow history"
230 self.disabled = True
250 self.disabled = True
231
251
232 def all(self):
252 def all(self):
233 d = self.db.hdict('shadowhist')
253 d = self.db.hdict('shadowhist')
234 items = [(i,s) for (s,i) in d.items()]
254 items = [(i,s) for (s,i) in d.items()]
235 items.sort()
255 items.sort()
236 return items
256 return items
237
257
238 def get(self, idx):
258 def get(self, idx):
239 all = self.all()
259 all = self.all()
240
260
241 for k, v in all:
261 for k, v in all:
242 #print k,v
262 #print k,v
243 if k == idx:
263 if k == idx:
244 return v
264 return v
245
265
246
266
247 def init_ipython(ip):
267 def init_ipython(ip):
248 ip.define_magic("rep",rep_f)
268 ip.define_magic("rep",rep_f)
249 ip.define_magic("hist",magic_hist)
269 ip.define_magic("hist",magic_hist)
250 ip.define_magic("history",magic_history)
270 ip.define_magic("history",magic_history)
251
271
252 # XXX - ipy_completers are in quarantine, need to be updated to new apis
272 # XXX - ipy_completers are in quarantine, need to be updated to new apis
253 #import ipy_completers
273 #import ipy_completers
254 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
274 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,2528 +1,2527 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Main IPython Component
3 Main IPython Component
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from __future__ import with_statement
19 from __future__ import with_statement
20 from __future__ import absolute_import
20 from __future__ import absolute_import
21
21
22 import __builtin__
22 import __builtin__
23 import StringIO
23 import StringIO
24 import bdb
24 import bdb
25 import codeop
25 import codeop
26 import exceptions
26 import exceptions
27 import new
27 import new
28 import os
28 import os
29 import re
29 import re
30 import string
30 import string
31 import sys
31 import sys
32 import tempfile
32 import tempfile
33 from contextlib import nested
33 from contextlib import nested
34
34
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import history as ipcorehist
36 from IPython.core import history as ipcorehist
37 from IPython.core import prefilter
37 from IPython.core import prefilter
38 from IPython.core import shadowns
38 from IPython.core import shadowns
39 from IPython.core import ultratb
39 from IPython.core import ultratb
40 from IPython.core.alias import AliasManager
40 from IPython.core.alias import AliasManager
41 from IPython.core.builtin_trap import BuiltinTrap
41 from IPython.core.builtin_trap import BuiltinTrap
42 from IPython.core.component import Component
42 from IPython.core.component import Component
43 from IPython.core.display_trap import DisplayTrap
43 from IPython.core.display_trap import DisplayTrap
44 from IPython.core.error import TryNext, UsageError
44 from IPython.core.error import TryNext, UsageError
45 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
46 from IPython.core.logger import Logger
46 from IPython.core.logger import Logger
47 from IPython.core.magic import Magic
47 from IPython.core.magic import Magic
48 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prefilter import PrefilterManager
49 from IPython.core.prompts import CachedOutput
49 from IPython.core.prompts import CachedOutput
50 from IPython.core.pylabtools import pylab_activate
50 from IPython.core.pylabtools import pylab_activate
51 from IPython.core.usage import interactive_usage, default_banner
51 from IPython.core.usage import interactive_usage, default_banner
52 from IPython.external.Itpl import ItplNS
52 from IPython.external.Itpl import ItplNS
53 from IPython.lib.inputhook import enable_gui
53 from IPython.lib.inputhook import enable_gui
54 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.lib.backgroundjobs import BackgroundJobManager
55 from IPython.utils import PyColorize
55 from IPython.utils import PyColorize
56 from IPython.utils import pickleshare
56 from IPython.utils import pickleshare
57 from IPython.utils.genutils import get_ipython_dir
57 from IPython.utils.genutils import get_ipython_dir
58 from IPython.utils.ipstruct import Struct
58 from IPython.utils.ipstruct import Struct
59 from IPython.utils.platutils import toggle_set_term_title, set_term_title
59 from IPython.utils.platutils import toggle_set_term_title, set_term_title
60 from IPython.utils.strdispatch import StrDispatch
60 from IPython.utils.strdispatch import StrDispatch
61 from IPython.utils.syspathcontext import prepended_to_syspath
61 from IPython.utils.syspathcontext import prepended_to_syspath
62
62
63 # XXX - need to clean up this import * line
63 # XXX - need to clean up this import * line
64 from IPython.utils.genutils import *
64 from IPython.utils.genutils import *
65
65
66 # from IPython.utils import growl
66 # from IPython.utils import growl
67 # growl.start("IPython")
67 # growl.start("IPython")
68
68
69 from IPython.utils.traitlets import (
69 from IPython.utils.traitlets import (
70 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
70 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
71 )
71 )
72
72
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74 # Globals
74 # Globals
75 #-----------------------------------------------------------------------------
75 #-----------------------------------------------------------------------------
76
76
77 # store the builtin raw_input globally, and use this always, in case user code
77 # store the builtin raw_input globally, and use this always, in case user code
78 # overwrites it (like wx.py.PyShell does)
78 # overwrites it (like wx.py.PyShell does)
79 raw_input_original = raw_input
79 raw_input_original = raw_input
80
80
81 # compiled regexps for autoindent management
81 # compiled regexps for autoindent management
82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
83
83
84 #-----------------------------------------------------------------------------
84 #-----------------------------------------------------------------------------
85 # Utilities
85 # Utilities
86 #-----------------------------------------------------------------------------
86 #-----------------------------------------------------------------------------
87
87
88 ini_spaces_re = re.compile(r'^(\s+)')
88 ini_spaces_re = re.compile(r'^(\s+)')
89
89
90
90
91 def num_ini_spaces(strng):
91 def num_ini_spaces(strng):
92 """Return the number of initial spaces in a string"""
92 """Return the number of initial spaces in a string"""
93
93
94 ini_spaces = ini_spaces_re.match(strng)
94 ini_spaces = ini_spaces_re.match(strng)
95 if ini_spaces:
95 if ini_spaces:
96 return ini_spaces.end()
96 return ini_spaces.end()
97 else:
97 else:
98 return 0
98 return 0
99
99
100
100
101 def softspace(file, newvalue):
101 def softspace(file, newvalue):
102 """Copied from code.py, to remove the dependency"""
102 """Copied from code.py, to remove the dependency"""
103
103
104 oldvalue = 0
104 oldvalue = 0
105 try:
105 try:
106 oldvalue = file.softspace
106 oldvalue = file.softspace
107 except AttributeError:
107 except AttributeError:
108 pass
108 pass
109 try:
109 try:
110 file.softspace = newvalue
110 file.softspace = newvalue
111 except (AttributeError, TypeError):
111 except (AttributeError, TypeError):
112 # "attribute-less object" or "read-only attributes"
112 # "attribute-less object" or "read-only attributes"
113 pass
113 pass
114 return oldvalue
114 return oldvalue
115
115
116
116
117 def no_op(*a, **kw): pass
117 def no_op(*a, **kw): pass
118
118
119 class SpaceInInput(exceptions.Exception): pass
119 class SpaceInInput(exceptions.Exception): pass
120
120
121 class Bunch: pass
121 class Bunch: pass
122
122
123 class InputList(list):
123 class InputList(list):
124 """Class to store user input.
124 """Class to store user input.
125
125
126 It's basically a list, but slices return a string instead of a list, thus
126 It's basically a list, but slices return a string instead of a list, thus
127 allowing things like (assuming 'In' is an instance):
127 allowing things like (assuming 'In' is an instance):
128
128
129 exec In[4:7]
129 exec In[4:7]
130
130
131 or
131 or
132
132
133 exec In[5:9] + In[14] + In[21:25]"""
133 exec In[5:9] + In[14] + In[21:25]"""
134
134
135 def __getslice__(self,i,j):
135 def __getslice__(self,i,j):
136 return ''.join(list.__getslice__(self,i,j))
136 return ''.join(list.__getslice__(self,i,j))
137
137
138
138
139 class SyntaxTB(ultratb.ListTB):
139 class SyntaxTB(ultratb.ListTB):
140 """Extension which holds some state: the last exception value"""
140 """Extension which holds some state: the last exception value"""
141
141
142 def __init__(self,color_scheme = 'NoColor'):
142 def __init__(self,color_scheme = 'NoColor'):
143 ultratb.ListTB.__init__(self,color_scheme)
143 ultratb.ListTB.__init__(self,color_scheme)
144 self.last_syntax_error = None
144 self.last_syntax_error = None
145
145
146 def __call__(self, etype, value, elist):
146 def __call__(self, etype, value, elist):
147 self.last_syntax_error = value
147 self.last_syntax_error = value
148 ultratb.ListTB.__call__(self,etype,value,elist)
148 ultratb.ListTB.__call__(self,etype,value,elist)
149
149
150 def clear_err_state(self):
150 def clear_err_state(self):
151 """Return the current error state and clear it"""
151 """Return the current error state and clear it"""
152 e = self.last_syntax_error
152 e = self.last_syntax_error
153 self.last_syntax_error = None
153 self.last_syntax_error = None
154 return e
154 return e
155
155
156
156
157 def get_default_editor():
157 def get_default_editor():
158 try:
158 try:
159 ed = os.environ['EDITOR']
159 ed = os.environ['EDITOR']
160 except KeyError:
160 except KeyError:
161 if os.name == 'posix':
161 if os.name == 'posix':
162 ed = 'vi' # the only one guaranteed to be there!
162 ed = 'vi' # the only one guaranteed to be there!
163 else:
163 else:
164 ed = 'notepad' # same in Windows!
164 ed = 'notepad' # same in Windows!
165 return ed
165 return ed
166
166
167
167
168 def get_default_colors():
168 def get_default_colors():
169 if sys.platform=='darwin':
169 if sys.platform=='darwin':
170 return "LightBG"
170 return "LightBG"
171 elif os.name=='nt':
171 elif os.name=='nt':
172 return 'Linux'
172 return 'Linux'
173 else:
173 else:
174 return 'Linux'
174 return 'Linux'
175
175
176
176
177 class SeparateStr(Str):
177 class SeparateStr(Str):
178 """A Str subclass to validate separate_in, separate_out, etc.
178 """A Str subclass to validate separate_in, separate_out, etc.
179
179
180 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
180 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
181 """
181 """
182
182
183 def validate(self, obj, value):
183 def validate(self, obj, value):
184 if value == '0': value = ''
184 if value == '0': value = ''
185 value = value.replace('\\n','\n')
185 value = value.replace('\\n','\n')
186 return super(SeparateStr, self).validate(obj, value)
186 return super(SeparateStr, self).validate(obj, value)
187
187
188
188
189 def make_user_namespaces(user_ns=None, user_global_ns=None):
189 def make_user_namespaces(user_ns=None, user_global_ns=None):
190 """Return a valid local and global user interactive namespaces.
190 """Return a valid local and global user interactive namespaces.
191
191
192 This builds a dict with the minimal information needed to operate as a
192 This builds a dict with the minimal information needed to operate as a
193 valid IPython user namespace, which you can pass to the various
193 valid IPython user namespace, which you can pass to the various
194 embedding classes in ipython. The default implementation returns the
194 embedding classes in ipython. The default implementation returns the
195 same dict for both the locals and the globals to allow functions to
195 same dict for both the locals and the globals to allow functions to
196 refer to variables in the namespace. Customized implementations can
196 refer to variables in the namespace. Customized implementations can
197 return different dicts. The locals dictionary can actually be anything
197 return different dicts. The locals dictionary can actually be anything
198 following the basic mapping protocol of a dict, but the globals dict
198 following the basic mapping protocol of a dict, but the globals dict
199 must be a true dict, not even a subclass. It is recommended that any
199 must be a true dict, not even a subclass. It is recommended that any
200 custom object for the locals namespace synchronize with the globals
200 custom object for the locals namespace synchronize with the globals
201 dict somehow.
201 dict somehow.
202
202
203 Raises TypeError if the provided globals namespace is not a true dict.
203 Raises TypeError if the provided globals namespace is not a true dict.
204
204
205 Parameters
205 Parameters
206 ----------
206 ----------
207 user_ns : dict-like, optional
207 user_ns : dict-like, optional
208 The current user namespace. The items in this namespace should
208 The current user namespace. The items in this namespace should
209 be included in the output. If None, an appropriate blank
209 be included in the output. If None, an appropriate blank
210 namespace should be created.
210 namespace should be created.
211 user_global_ns : dict, optional
211 user_global_ns : dict, optional
212 The current user global namespace. The items in this namespace
212 The current user global namespace. The items in this namespace
213 should be included in the output. If None, an appropriate
213 should be included in the output. If None, an appropriate
214 blank namespace should be created.
214 blank namespace should be created.
215
215
216 Returns
216 Returns
217 -------
217 -------
218 A pair of dictionary-like object to be used as the local namespace
218 A pair of dictionary-like object to be used as the local namespace
219 of the interpreter and a dict to be used as the global namespace.
219 of the interpreter and a dict to be used as the global namespace.
220 """
220 """
221
221
222 if user_ns is None:
222 if user_ns is None:
223 # Set __name__ to __main__ to better match the behavior of the
223 # Set __name__ to __main__ to better match the behavior of the
224 # normal interpreter.
224 # normal interpreter.
225 user_ns = {'__name__' :'__main__',
225 user_ns = {'__name__' :'__main__',
226 '__builtins__' : __builtin__,
226 '__builtins__' : __builtin__,
227 }
227 }
228 else:
228 else:
229 user_ns.setdefault('__name__','__main__')
229 user_ns.setdefault('__name__','__main__')
230 user_ns.setdefault('__builtins__',__builtin__)
230 user_ns.setdefault('__builtins__',__builtin__)
231
231
232 if user_global_ns is None:
232 if user_global_ns is None:
233 user_global_ns = user_ns
233 user_global_ns = user_ns
234 if type(user_global_ns) is not dict:
234 if type(user_global_ns) is not dict:
235 raise TypeError("user_global_ns must be a true dict; got %r"
235 raise TypeError("user_global_ns must be a true dict; got %r"
236 % type(user_global_ns))
236 % type(user_global_ns))
237
237
238 return user_ns, user_global_ns
238 return user_ns, user_global_ns
239
239
240 #-----------------------------------------------------------------------------
240 #-----------------------------------------------------------------------------
241 # Main IPython class
241 # Main IPython class
242 #-----------------------------------------------------------------------------
242 #-----------------------------------------------------------------------------
243
243
244
244
245 class InteractiveShell(Component, Magic):
245 class InteractiveShell(Component, Magic):
246 """An enhanced, interactive shell for Python."""
246 """An enhanced, interactive shell for Python."""
247
247
248 autocall = Enum((0,1,2), default_value=1, config=True)
248 autocall = Enum((0,1,2), default_value=1, config=True)
249 autoedit_syntax = CBool(False, config=True)
249 autoedit_syntax = CBool(False, config=True)
250 autoindent = CBool(True, config=True)
250 autoindent = CBool(True, config=True)
251 automagic = CBool(True, config=True)
251 automagic = CBool(True, config=True)
252 banner = Str('')
252 banner = Str('')
253 banner1 = Str(default_banner, config=True)
253 banner1 = Str(default_banner, config=True)
254 banner2 = Str('', config=True)
254 banner2 = Str('', config=True)
255 cache_size = Int(1000, config=True)
255 cache_size = Int(1000, config=True)
256 color_info = CBool(True, config=True)
256 color_info = CBool(True, config=True)
257 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
257 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
258 default_value=get_default_colors(), config=True)
258 default_value=get_default_colors(), config=True)
259 confirm_exit = CBool(True, config=True)
259 confirm_exit = CBool(True, config=True)
260 debug = CBool(False, config=True)
260 debug = CBool(False, config=True)
261 deep_reload = CBool(False, config=True)
261 deep_reload = CBool(False, config=True)
262 # This display_banner only controls whether or not self.show_banner()
262 # This display_banner only controls whether or not self.show_banner()
263 # is called when mainloop/interact are called. The default is False
263 # is called when mainloop/interact are called. The default is False
264 # because for the terminal based application, the banner behavior
264 # because for the terminal based application, the banner behavior
265 # is controlled by Global.display_banner, which IPythonApp looks at
265 # is controlled by Global.display_banner, which IPythonApp looks at
266 # to determine if *it* should call show_banner() by hand or not.
266 # to determine if *it* should call show_banner() by hand or not.
267 display_banner = CBool(False) # This isn't configurable!
267 display_banner = CBool(False) # This isn't configurable!
268 embedded = CBool(False)
268 embedded = CBool(False)
269 embedded_active = CBool(False)
269 embedded_active = CBool(False)
270 editor = Str(get_default_editor(), config=True)
270 editor = Str(get_default_editor(), config=True)
271 filename = Str("<ipython console>")
271 filename = Str("<ipython console>")
272 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
272 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
273 logstart = CBool(False, config=True)
273 logstart = CBool(False, config=True)
274 logfile = Str('', config=True)
274 logfile = Str('', config=True)
275 logappend = Str('', config=True)
275 logappend = Str('', config=True)
276 object_info_string_level = Enum((0,1,2), default_value=0,
276 object_info_string_level = Enum((0,1,2), default_value=0,
277 config=True)
277 config=True)
278 pager = Str('less', config=True)
278 pager = Str('less', config=True)
279 pdb = CBool(False, config=True)
279 pdb = CBool(False, config=True)
280 pprint = CBool(True, config=True)
280 pprint = CBool(True, config=True)
281 profile = Str('', config=True)
281 profile = Str('', config=True)
282 prompt_in1 = Str('In [\\#]: ', config=True)
282 prompt_in1 = Str('In [\\#]: ', config=True)
283 prompt_in2 = Str(' .\\D.: ', config=True)
283 prompt_in2 = Str(' .\\D.: ', config=True)
284 prompt_out = Str('Out[\\#]: ', config=True)
284 prompt_out = Str('Out[\\#]: ', config=True)
285 prompts_pad_left = CBool(True, config=True)
285 prompts_pad_left = CBool(True, config=True)
286 quiet = CBool(False, config=True)
286 quiet = CBool(False, config=True)
287
287
288 readline_use = CBool(True, config=True)
288 readline_use = CBool(True, config=True)
289 readline_merge_completions = CBool(True, config=True)
289 readline_merge_completions = CBool(True, config=True)
290 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
290 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
291 readline_remove_delims = Str('-/~', config=True)
291 readline_remove_delims = Str('-/~', config=True)
292 readline_parse_and_bind = List([
292 readline_parse_and_bind = List([
293 'tab: complete',
293 'tab: complete',
294 '"\C-l": possible-completions',
294 '"\C-l": possible-completions',
295 'set show-all-if-ambiguous on',
295 'set show-all-if-ambiguous on',
296 '"\C-o": tab-insert',
296 '"\C-o": tab-insert',
297 '"\M-i": " "',
297 '"\M-i": " "',
298 '"\M-o": "\d\d\d\d"',
298 '"\M-o": "\d\d\d\d"',
299 '"\M-I": "\d\d\d\d"',
299 '"\M-I": "\d\d\d\d"',
300 '"\C-r": reverse-search-history',
300 '"\C-r": reverse-search-history',
301 '"\C-s": forward-search-history',
301 '"\C-s": forward-search-history',
302 '"\C-p": history-search-backward',
302 '"\C-p": history-search-backward',
303 '"\C-n": history-search-forward',
303 '"\C-n": history-search-forward',
304 '"\e[A": history-search-backward',
304 '"\e[A": history-search-backward',
305 '"\e[B": history-search-forward',
305 '"\e[B": history-search-forward',
306 '"\C-k": kill-line',
306 '"\C-k": kill-line',
307 '"\C-u": unix-line-discard',
307 '"\C-u": unix-line-discard',
308 ], allow_none=False, config=True)
308 ], allow_none=False, config=True)
309
309
310 screen_length = Int(0, config=True)
310 screen_length = Int(0, config=True)
311
311
312 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
312 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
313 separate_in = SeparateStr('\n', config=True)
313 separate_in = SeparateStr('\n', config=True)
314 separate_out = SeparateStr('', config=True)
314 separate_out = SeparateStr('', config=True)
315 separate_out2 = SeparateStr('', config=True)
315 separate_out2 = SeparateStr('', config=True)
316
316
317 system_header = Str('IPython system call: ', config=True)
317 system_header = Str('IPython system call: ', config=True)
318 system_verbose = CBool(False, config=True)
318 system_verbose = CBool(False, config=True)
319 term_title = CBool(False, config=True)
319 term_title = CBool(False, config=True)
320 wildcards_case_sensitive = CBool(True, config=True)
320 wildcards_case_sensitive = CBool(True, config=True)
321 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
321 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
322 default_value='Context', config=True)
322 default_value='Context', config=True)
323
323
324 autoexec = List(allow_none=False)
324 autoexec = List(allow_none=False)
325
325
326 # class attribute to indicate whether the class supports threads or not.
326 # class attribute to indicate whether the class supports threads or not.
327 # Subclasses with thread support should override this as needed.
327 # Subclasses with thread support should override this as needed.
328 isthreaded = False
328 isthreaded = False
329
329
330 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
330 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
331 user_ns=None, user_global_ns=None,
331 user_ns=None, user_global_ns=None,
332 banner1=None, banner2=None, display_banner=None,
332 banner1=None, banner2=None, display_banner=None,
333 custom_exceptions=((),None)):
333 custom_exceptions=((),None)):
334
334
335 # This is where traitlets with a config_key argument are updated
335 # This is where traitlets with a config_key argument are updated
336 # from the values on config.
336 # from the values on config.
337 super(InteractiveShell, self).__init__(parent, config=config)
337 super(InteractiveShell, self).__init__(parent, config=config)
338
338
339 # These are relatively independent and stateless
339 # These are relatively independent and stateless
340 self.init_ipython_dir(ipython_dir)
340 self.init_ipython_dir(ipython_dir)
341 self.init_instance_attrs()
341 self.init_instance_attrs()
342 self.init_term_title()
342 self.init_term_title()
343 self.init_usage(usage)
343 self.init_usage(usage)
344 self.init_banner(banner1, banner2, display_banner)
344 self.init_banner(banner1, banner2, display_banner)
345
345
346 # Create namespaces (user_ns, user_global_ns, etc.)
346 # Create namespaces (user_ns, user_global_ns, etc.)
347 self.init_create_namespaces(user_ns, user_global_ns)
347 self.init_create_namespaces(user_ns, user_global_ns)
348 # This has to be done after init_create_namespaces because it uses
348 # This has to be done after init_create_namespaces because it uses
349 # something in self.user_ns, but before init_sys_modules, which
349 # something in self.user_ns, but before init_sys_modules, which
350 # is the first thing to modify sys.
350 # is the first thing to modify sys.
351 self.save_sys_module_state()
351 self.save_sys_module_state()
352 self.init_sys_modules()
352 self.init_sys_modules()
353
353
354 self.init_history()
354 self.init_history()
355 self.init_encoding()
355 self.init_encoding()
356 self.init_prefilter()
356 self.init_prefilter()
357
357
358 Magic.__init__(self, self)
358 Magic.__init__(self, self)
359
359
360 self.init_syntax_highlighting()
360 self.init_syntax_highlighting()
361 self.init_hooks()
361 self.init_hooks()
362 self.init_pushd_popd_magic()
362 self.init_pushd_popd_magic()
363 self.init_traceback_handlers(custom_exceptions)
363 self.init_traceback_handlers(custom_exceptions)
364 self.init_user_ns()
364 self.init_user_ns()
365 self.init_logger()
365 self.init_logger()
366 self.init_alias()
366 self.init_alias()
367 self.init_builtins()
367 self.init_builtins()
368
368
369 # pre_config_initialization
369 # pre_config_initialization
370 self.init_shadow_hist()
370 self.init_shadow_hist()
371
371
372 # The next section should contain averything that was in ipmaker.
372 # The next section should contain averything that was in ipmaker.
373 self.init_logstart()
373 self.init_logstart()
374
374
375 # The following was in post_config_initialization
375 # The following was in post_config_initialization
376 self.init_inspector()
376 self.init_inspector()
377 self.init_readline()
377 self.init_readline()
378 self.init_prompts()
378 self.init_prompts()
379 self.init_displayhook()
379 self.init_displayhook()
380 self.init_reload_doctest()
380 self.init_reload_doctest()
381 self.init_magics()
381 self.init_magics()
382 self.init_pdb()
382 self.init_pdb()
383 self.hooks.late_startup_hook()
383 self.hooks.late_startup_hook()
384
384
385 def get_ipython(self):
385 def get_ipython(self):
386 """Return the currently running IPython instance."""
386 """Return the currently running IPython instance."""
387 return self
387 return self
388
388
389 #-------------------------------------------------------------------------
389 #-------------------------------------------------------------------------
390 # Traitlet changed handlers
390 # Traitlet changed handlers
391 #-------------------------------------------------------------------------
391 #-------------------------------------------------------------------------
392
392
393 def _banner1_changed(self):
393 def _banner1_changed(self):
394 self.compute_banner()
394 self.compute_banner()
395
395
396 def _banner2_changed(self):
396 def _banner2_changed(self):
397 self.compute_banner()
397 self.compute_banner()
398
398
399 def _ipython_dir_changed(self, name, new):
399 def _ipython_dir_changed(self, name, new):
400 if not os.path.isdir(new):
400 if not os.path.isdir(new):
401 os.makedirs(new, mode = 0777)
401 os.makedirs(new, mode = 0777)
402 if not os.path.isdir(self.ipython_extension_dir):
402 if not os.path.isdir(self.ipython_extension_dir):
403 os.makedirs(self.ipython_extension_dir, mode = 0777)
403 os.makedirs(self.ipython_extension_dir, mode = 0777)
404
404
405 @property
405 @property
406 def ipython_extension_dir(self):
406 def ipython_extension_dir(self):
407 return os.path.join(self.ipython_dir, 'extensions')
407 return os.path.join(self.ipython_dir, 'extensions')
408
408
409 @property
409 @property
410 def usable_screen_length(self):
410 def usable_screen_length(self):
411 if self.screen_length == 0:
411 if self.screen_length == 0:
412 return 0
412 return 0
413 else:
413 else:
414 num_lines_bot = self.separate_in.count('\n')+1
414 num_lines_bot = self.separate_in.count('\n')+1
415 return self.screen_length - num_lines_bot
415 return self.screen_length - num_lines_bot
416
416
417 def _term_title_changed(self, name, new_value):
417 def _term_title_changed(self, name, new_value):
418 self.init_term_title()
418 self.init_term_title()
419
419
420 def set_autoindent(self,value=None):
420 def set_autoindent(self,value=None):
421 """Set the autoindent flag, checking for readline support.
421 """Set the autoindent flag, checking for readline support.
422
422
423 If called with no arguments, it acts as a toggle."""
423 If called with no arguments, it acts as a toggle."""
424
424
425 if not self.has_readline:
425 if not self.has_readline:
426 if os.name == 'posix':
426 if os.name == 'posix':
427 warn("The auto-indent feature requires the readline library")
427 warn("The auto-indent feature requires the readline library")
428 self.autoindent = 0
428 self.autoindent = 0
429 return
429 return
430 if value is None:
430 if value is None:
431 self.autoindent = not self.autoindent
431 self.autoindent = not self.autoindent
432 else:
432 else:
433 self.autoindent = value
433 self.autoindent = value
434
434
435 #-------------------------------------------------------------------------
435 #-------------------------------------------------------------------------
436 # init_* methods called by __init__
436 # init_* methods called by __init__
437 #-------------------------------------------------------------------------
437 #-------------------------------------------------------------------------
438
438
439 def init_ipython_dir(self, ipython_dir):
439 def init_ipython_dir(self, ipython_dir):
440 if ipython_dir is not None:
440 if ipython_dir is not None:
441 self.ipython_dir = ipython_dir
441 self.ipython_dir = ipython_dir
442 self.config.Global.ipython_dir = self.ipython_dir
442 self.config.Global.ipython_dir = self.ipython_dir
443 return
443 return
444
444
445 if hasattr(self.config.Global, 'ipython_dir'):
445 if hasattr(self.config.Global, 'ipython_dir'):
446 self.ipython_dir = self.config.Global.ipython_dir
446 self.ipython_dir = self.config.Global.ipython_dir
447 else:
447 else:
448 self.ipython_dir = get_ipython_dir()
448 self.ipython_dir = get_ipython_dir()
449
449
450 # All children can just read this
450 # All children can just read this
451 self.config.Global.ipython_dir = self.ipython_dir
451 self.config.Global.ipython_dir = self.ipython_dir
452
452
453 def init_instance_attrs(self):
453 def init_instance_attrs(self):
454 self.jobs = BackgroundJobManager()
454 self.jobs = BackgroundJobManager()
455 self.more = False
455 self.more = False
456
456
457 # command compiler
457 # command compiler
458 self.compile = codeop.CommandCompiler()
458 self.compile = codeop.CommandCompiler()
459
459
460 # User input buffer
460 # User input buffer
461 self.buffer = []
461 self.buffer = []
462
462
463 # Make an empty namespace, which extension writers can rely on both
463 # Make an empty namespace, which extension writers can rely on both
464 # existing and NEVER being used by ipython itself. This gives them a
464 # existing and NEVER being used by ipython itself. This gives them a
465 # convenient location for storing additional information and state
465 # convenient location for storing additional information and state
466 # their extensions may require, without fear of collisions with other
466 # their extensions may require, without fear of collisions with other
467 # ipython names that may develop later.
467 # ipython names that may develop later.
468 self.meta = Struct()
468 self.meta = Struct()
469
469
470 # Object variable to store code object waiting execution. This is
470 # Object variable to store code object waiting execution. This is
471 # used mainly by the multithreaded shells, but it can come in handy in
471 # used mainly by the multithreaded shells, but it can come in handy in
472 # other situations. No need to use a Queue here, since it's a single
472 # other situations. No need to use a Queue here, since it's a single
473 # item which gets cleared once run.
473 # item which gets cleared once run.
474 self.code_to_run = None
474 self.code_to_run = None
475
475
476 # Flag to mark unconditional exit
476 # Flag to mark unconditional exit
477 self.exit_now = False
477 self.exit_now = False
478
478
479 # Temporary files used for various purposes. Deleted at exit.
479 # Temporary files used for various purposes. Deleted at exit.
480 self.tempfiles = []
480 self.tempfiles = []
481
481
482 # Keep track of readline usage (later set by init_readline)
482 # Keep track of readline usage (later set by init_readline)
483 self.has_readline = False
483 self.has_readline = False
484
484
485 # keep track of where we started running (mainly for crash post-mortem)
485 # keep track of where we started running (mainly for crash post-mortem)
486 # This is not being used anywhere currently.
486 # This is not being used anywhere currently.
487 self.starting_dir = os.getcwd()
487 self.starting_dir = os.getcwd()
488
488
489 # Indentation management
489 # Indentation management
490 self.indent_current_nsp = 0
490 self.indent_current_nsp = 0
491
491
492 def init_term_title(self):
492 def init_term_title(self):
493 # Enable or disable the terminal title.
493 # Enable or disable the terminal title.
494 if self.term_title:
494 if self.term_title:
495 toggle_set_term_title(True)
495 toggle_set_term_title(True)
496 set_term_title('IPython: ' + abbrev_cwd())
496 set_term_title('IPython: ' + abbrev_cwd())
497 else:
497 else:
498 toggle_set_term_title(False)
498 toggle_set_term_title(False)
499
499
500 def init_usage(self, usage=None):
500 def init_usage(self, usage=None):
501 if usage is None:
501 if usage is None:
502 self.usage = interactive_usage
502 self.usage = interactive_usage
503 else:
503 else:
504 self.usage = usage
504 self.usage = usage
505
505
506 def init_encoding(self):
506 def init_encoding(self):
507 # Get system encoding at startup time. Certain terminals (like Emacs
507 # Get system encoding at startup time. Certain terminals (like Emacs
508 # under Win32 have it set to None, and we need to have a known valid
508 # under Win32 have it set to None, and we need to have a known valid
509 # encoding to use in the raw_input() method
509 # encoding to use in the raw_input() method
510 try:
510 try:
511 self.stdin_encoding = sys.stdin.encoding or 'ascii'
511 self.stdin_encoding = sys.stdin.encoding or 'ascii'
512 except AttributeError:
512 except AttributeError:
513 self.stdin_encoding = 'ascii'
513 self.stdin_encoding = 'ascii'
514
514
515 def init_syntax_highlighting(self):
515 def init_syntax_highlighting(self):
516 # Python source parser/formatter for syntax highlighting
516 # Python source parser/formatter for syntax highlighting
517 pyformat = PyColorize.Parser().format
517 pyformat = PyColorize.Parser().format
518 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
518 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
519
519
520 def init_pushd_popd_magic(self):
520 def init_pushd_popd_magic(self):
521 # for pushd/popd management
521 # for pushd/popd management
522 try:
522 try:
523 self.home_dir = get_home_dir()
523 self.home_dir = get_home_dir()
524 except HomeDirError, msg:
524 except HomeDirError, msg:
525 fatal(msg)
525 fatal(msg)
526
526
527 self.dir_stack = []
527 self.dir_stack = []
528
528
529 def init_logger(self):
529 def init_logger(self):
530 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
530 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
531 # local shortcut, this is used a LOT
531 # local shortcut, this is used a LOT
532 self.log = self.logger.log
532 self.log = self.logger.log
533
533
534 def init_logstart(self):
534 def init_logstart(self):
535 if self.logappend:
535 if self.logappend:
536 self.magic_logstart(self.logappend + ' append')
536 self.magic_logstart(self.logappend + ' append')
537 elif self.logfile:
537 elif self.logfile:
538 self.magic_logstart(self.logfile)
538 self.magic_logstart(self.logfile)
539 elif self.logstart:
539 elif self.logstart:
540 self.magic_logstart()
540 self.magic_logstart()
541
541
542 def init_builtins(self):
542 def init_builtins(self):
543 self.builtin_trap = BuiltinTrap(self)
543 self.builtin_trap = BuiltinTrap(self)
544
544
545 def init_inspector(self):
545 def init_inspector(self):
546 # Object inspector
546 # Object inspector
547 self.inspector = oinspect.Inspector(oinspect.InspectColors,
547 self.inspector = oinspect.Inspector(oinspect.InspectColors,
548 PyColorize.ANSICodeColors,
548 PyColorize.ANSICodeColors,
549 'NoColor',
549 'NoColor',
550 self.object_info_string_level)
550 self.object_info_string_level)
551
551
552 def init_prompts(self):
552 def init_prompts(self):
553 # Initialize cache, set in/out prompts and printing system
553 # Initialize cache, set in/out prompts and printing system
554 self.outputcache = CachedOutput(self,
554 self.outputcache = CachedOutput(self,
555 self.cache_size,
555 self.cache_size,
556 self.pprint,
556 self.pprint,
557 input_sep = self.separate_in,
557 input_sep = self.separate_in,
558 output_sep = self.separate_out,
558 output_sep = self.separate_out,
559 output_sep2 = self.separate_out2,
559 output_sep2 = self.separate_out2,
560 ps1 = self.prompt_in1,
560 ps1 = self.prompt_in1,
561 ps2 = self.prompt_in2,
561 ps2 = self.prompt_in2,
562 ps_out = self.prompt_out,
562 ps_out = self.prompt_out,
563 pad_left = self.prompts_pad_left)
563 pad_left = self.prompts_pad_left)
564
564
565 # user may have over-ridden the default print hook:
565 # user may have over-ridden the default print hook:
566 try:
566 try:
567 self.outputcache.__class__.display = self.hooks.display
567 self.outputcache.__class__.display = self.hooks.display
568 except AttributeError:
568 except AttributeError:
569 pass
569 pass
570
570
571 def init_displayhook(self):
571 def init_displayhook(self):
572 self.display_trap = DisplayTrap(self, self.outputcache)
572 self.display_trap = DisplayTrap(self, self.outputcache)
573
573
574 def init_reload_doctest(self):
574 def init_reload_doctest(self):
575 # Do a proper resetting of doctest, including the necessary displayhook
575 # Do a proper resetting of doctest, including the necessary displayhook
576 # monkeypatching
576 # monkeypatching
577 try:
577 try:
578 doctest_reload()
578 doctest_reload()
579 except ImportError:
579 except ImportError:
580 warn("doctest module does not exist.")
580 warn("doctest module does not exist.")
581
581
582 #-------------------------------------------------------------------------
582 #-------------------------------------------------------------------------
583 # Things related to the banner
583 # Things related to the banner
584 #-------------------------------------------------------------------------
584 #-------------------------------------------------------------------------
585
585
586 def init_banner(self, banner1, banner2, display_banner):
586 def init_banner(self, banner1, banner2, display_banner):
587 if banner1 is not None:
587 if banner1 is not None:
588 self.banner1 = banner1
588 self.banner1 = banner1
589 if banner2 is not None:
589 if banner2 is not None:
590 self.banner2 = banner2
590 self.banner2 = banner2
591 if display_banner is not None:
591 if display_banner is not None:
592 self.display_banner = display_banner
592 self.display_banner = display_banner
593 self.compute_banner()
593 self.compute_banner()
594
594
595 def show_banner(self, banner=None):
595 def show_banner(self, banner=None):
596 if banner is None:
596 if banner is None:
597 banner = self.banner
597 banner = self.banner
598 self.write(banner)
598 self.write(banner)
599
599
600 def compute_banner(self):
600 def compute_banner(self):
601 self.banner = self.banner1 + '\n'
601 self.banner = self.banner1 + '\n'
602 if self.profile:
602 if self.profile:
603 self.banner += '\nIPython profile: %s\n' % self.profile
603 self.banner += '\nIPython profile: %s\n' % self.profile
604 if self.banner2:
604 if self.banner2:
605 self.banner += '\n' + self.banner2 + '\n'
605 self.banner += '\n' + self.banner2 + '\n'
606
606
607 #-------------------------------------------------------------------------
607 #-------------------------------------------------------------------------
608 # Things related to injections into the sys module
608 # Things related to injections into the sys module
609 #-------------------------------------------------------------------------
609 #-------------------------------------------------------------------------
610
610
611 def save_sys_module_state(self):
611 def save_sys_module_state(self):
612 """Save the state of hooks in the sys module.
612 """Save the state of hooks in the sys module.
613
613
614 This has to be called after self.user_ns is created.
614 This has to be called after self.user_ns is created.
615 """
615 """
616 self._orig_sys_module_state = {}
616 self._orig_sys_module_state = {}
617 self._orig_sys_module_state['stdin'] = sys.stdin
617 self._orig_sys_module_state['stdin'] = sys.stdin
618 self._orig_sys_module_state['stdout'] = sys.stdout
618 self._orig_sys_module_state['stdout'] = sys.stdout
619 self._orig_sys_module_state['stderr'] = sys.stderr
619 self._orig_sys_module_state['stderr'] = sys.stderr
620 self._orig_sys_module_state['excepthook'] = sys.excepthook
620 self._orig_sys_module_state['excepthook'] = sys.excepthook
621 try:
621 try:
622 self._orig_sys_modules_main_name = self.user_ns['__name__']
622 self._orig_sys_modules_main_name = self.user_ns['__name__']
623 except KeyError:
623 except KeyError:
624 pass
624 pass
625
625
626 def restore_sys_module_state(self):
626 def restore_sys_module_state(self):
627 """Restore the state of the sys module."""
627 """Restore the state of the sys module."""
628 try:
628 try:
629 for k, v in self._orig_sys_module_state.items():
629 for k, v in self._orig_sys_module_state.items():
630 setattr(sys, k, v)
630 setattr(sys, k, v)
631 except AttributeError:
631 except AttributeError:
632 pass
632 pass
633 try:
633 try:
634 delattr(sys, 'ipcompleter')
634 delattr(sys, 'ipcompleter')
635 except AttributeError:
635 except AttributeError:
636 pass
636 pass
637 # Reset what what done in self.init_sys_modules
637 # Reset what what done in self.init_sys_modules
638 try:
638 try:
639 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
639 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
640 except (AttributeError, KeyError):
640 except (AttributeError, KeyError):
641 pass
641 pass
642
642
643 #-------------------------------------------------------------------------
643 #-------------------------------------------------------------------------
644 # Things related to hooks
644 # Things related to hooks
645 #-------------------------------------------------------------------------
645 #-------------------------------------------------------------------------
646
646
647 def init_hooks(self):
647 def init_hooks(self):
648 # hooks holds pointers used for user-side customizations
648 # hooks holds pointers used for user-side customizations
649 self.hooks = Struct()
649 self.hooks = Struct()
650
650
651 self.strdispatchers = {}
651 self.strdispatchers = {}
652
652
653 # Set all default hooks, defined in the IPython.hooks module.
653 # Set all default hooks, defined in the IPython.hooks module.
654 import IPython.core.hooks
654 import IPython.core.hooks
655 hooks = IPython.core.hooks
655 hooks = IPython.core.hooks
656 for hook_name in hooks.__all__:
656 for hook_name in hooks.__all__:
657 # default hooks have priority 100, i.e. low; user hooks should have
657 # default hooks have priority 100, i.e. low; user hooks should have
658 # 0-100 priority
658 # 0-100 priority
659 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
659 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
660
660
661 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
661 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
662 """set_hook(name,hook) -> sets an internal IPython hook.
662 """set_hook(name,hook) -> sets an internal IPython hook.
663
663
664 IPython exposes some of its internal API as user-modifiable hooks. By
664 IPython exposes some of its internal API as user-modifiable hooks. By
665 adding your function to one of these hooks, you can modify IPython's
665 adding your function to one of these hooks, you can modify IPython's
666 behavior to call at runtime your own routines."""
666 behavior to call at runtime your own routines."""
667
667
668 # At some point in the future, this should validate the hook before it
668 # At some point in the future, this should validate the hook before it
669 # accepts it. Probably at least check that the hook takes the number
669 # accepts it. Probably at least check that the hook takes the number
670 # of args it's supposed to.
670 # of args it's supposed to.
671
671
672 f = new.instancemethod(hook,self,self.__class__)
672 f = new.instancemethod(hook,self,self.__class__)
673
673
674 # check if the hook is for strdispatcher first
674 # check if the hook is for strdispatcher first
675 if str_key is not None:
675 if str_key is not None:
676 sdp = self.strdispatchers.get(name, StrDispatch())
676 sdp = self.strdispatchers.get(name, StrDispatch())
677 sdp.add_s(str_key, f, priority )
677 sdp.add_s(str_key, f, priority )
678 self.strdispatchers[name] = sdp
678 self.strdispatchers[name] = sdp
679 return
679 return
680 if re_key is not None:
680 if re_key is not None:
681 sdp = self.strdispatchers.get(name, StrDispatch())
681 sdp = self.strdispatchers.get(name, StrDispatch())
682 sdp.add_re(re.compile(re_key), f, priority )
682 sdp.add_re(re.compile(re_key), f, priority )
683 self.strdispatchers[name] = sdp
683 self.strdispatchers[name] = sdp
684 return
684 return
685
685
686 dp = getattr(self.hooks, name, None)
686 dp = getattr(self.hooks, name, None)
687 if name not in IPython.core.hooks.__all__:
687 if name not in IPython.core.hooks.__all__:
688 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
688 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
689 if not dp:
689 if not dp:
690 dp = IPython.core.hooks.CommandChainDispatcher()
690 dp = IPython.core.hooks.CommandChainDispatcher()
691
691
692 try:
692 try:
693 dp.add(f,priority)
693 dp.add(f,priority)
694 except AttributeError:
694 except AttributeError:
695 # it was not commandchain, plain old func - replace
695 # it was not commandchain, plain old func - replace
696 dp = f
696 dp = f
697
697
698 setattr(self.hooks,name, dp)
698 setattr(self.hooks,name, dp)
699
699
700 #-------------------------------------------------------------------------
700 #-------------------------------------------------------------------------
701 # Things related to the "main" module
701 # Things related to the "main" module
702 #-------------------------------------------------------------------------
702 #-------------------------------------------------------------------------
703
703
704 def new_main_mod(self,ns=None):
704 def new_main_mod(self,ns=None):
705 """Return a new 'main' module object for user code execution.
705 """Return a new 'main' module object for user code execution.
706 """
706 """
707 main_mod = self._user_main_module
707 main_mod = self._user_main_module
708 init_fakemod_dict(main_mod,ns)
708 init_fakemod_dict(main_mod,ns)
709 return main_mod
709 return main_mod
710
710
711 def cache_main_mod(self,ns,fname):
711 def cache_main_mod(self,ns,fname):
712 """Cache a main module's namespace.
712 """Cache a main module's namespace.
713
713
714 When scripts are executed via %run, we must keep a reference to the
714 When scripts are executed via %run, we must keep a reference to the
715 namespace of their __main__ module (a FakeModule instance) around so
715 namespace of their __main__ module (a FakeModule instance) around so
716 that Python doesn't clear it, rendering objects defined therein
716 that Python doesn't clear it, rendering objects defined therein
717 useless.
717 useless.
718
718
719 This method keeps said reference in a private dict, keyed by the
719 This method keeps said reference in a private dict, keyed by the
720 absolute path of the module object (which corresponds to the script
720 absolute path of the module object (which corresponds to the script
721 path). This way, for multiple executions of the same script we only
721 path). This way, for multiple executions of the same script we only
722 keep one copy of the namespace (the last one), thus preventing memory
722 keep one copy of the namespace (the last one), thus preventing memory
723 leaks from old references while allowing the objects from the last
723 leaks from old references while allowing the objects from the last
724 execution to be accessible.
724 execution to be accessible.
725
725
726 Note: we can not allow the actual FakeModule instances to be deleted,
726 Note: we can not allow the actual FakeModule instances to be deleted,
727 because of how Python tears down modules (it hard-sets all their
727 because of how Python tears down modules (it hard-sets all their
728 references to None without regard for reference counts). This method
728 references to None without regard for reference counts). This method
729 must therefore make a *copy* of the given namespace, to allow the
729 must therefore make a *copy* of the given namespace, to allow the
730 original module's __dict__ to be cleared and reused.
730 original module's __dict__ to be cleared and reused.
731
731
732
732
733 Parameters
733 Parameters
734 ----------
734 ----------
735 ns : a namespace (a dict, typically)
735 ns : a namespace (a dict, typically)
736
736
737 fname : str
737 fname : str
738 Filename associated with the namespace.
738 Filename associated with the namespace.
739
739
740 Examples
740 Examples
741 --------
741 --------
742
742
743 In [10]: import IPython
743 In [10]: import IPython
744
744
745 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
745 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
746
746
747 In [12]: IPython.__file__ in _ip._main_ns_cache
747 In [12]: IPython.__file__ in _ip._main_ns_cache
748 Out[12]: True
748 Out[12]: True
749 """
749 """
750 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
750 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
751
751
752 def clear_main_mod_cache(self):
752 def clear_main_mod_cache(self):
753 """Clear the cache of main modules.
753 """Clear the cache of main modules.
754
754
755 Mainly for use by utilities like %reset.
755 Mainly for use by utilities like %reset.
756
756
757 Examples
757 Examples
758 --------
758 --------
759
759
760 In [15]: import IPython
760 In [15]: import IPython
761
761
762 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
762 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
763
763
764 In [17]: len(_ip._main_ns_cache) > 0
764 In [17]: len(_ip._main_ns_cache) > 0
765 Out[17]: True
765 Out[17]: True
766
766
767 In [18]: _ip.clear_main_mod_cache()
767 In [18]: _ip.clear_main_mod_cache()
768
768
769 In [19]: len(_ip._main_ns_cache) == 0
769 In [19]: len(_ip._main_ns_cache) == 0
770 Out[19]: True
770 Out[19]: True
771 """
771 """
772 self._main_ns_cache.clear()
772 self._main_ns_cache.clear()
773
773
774 #-------------------------------------------------------------------------
774 #-------------------------------------------------------------------------
775 # Things related to debugging
775 # Things related to debugging
776 #-------------------------------------------------------------------------
776 #-------------------------------------------------------------------------
777
777
778 def init_pdb(self):
778 def init_pdb(self):
779 # Set calling of pdb on exceptions
779 # Set calling of pdb on exceptions
780 # self.call_pdb is a property
780 # self.call_pdb is a property
781 self.call_pdb = self.pdb
781 self.call_pdb = self.pdb
782
782
783 def _get_call_pdb(self):
783 def _get_call_pdb(self):
784 return self._call_pdb
784 return self._call_pdb
785
785
786 def _set_call_pdb(self,val):
786 def _set_call_pdb(self,val):
787
787
788 if val not in (0,1,False,True):
788 if val not in (0,1,False,True):
789 raise ValueError,'new call_pdb value must be boolean'
789 raise ValueError,'new call_pdb value must be boolean'
790
790
791 # store value in instance
791 # store value in instance
792 self._call_pdb = val
792 self._call_pdb = val
793
793
794 # notify the actual exception handlers
794 # notify the actual exception handlers
795 self.InteractiveTB.call_pdb = val
795 self.InteractiveTB.call_pdb = val
796 if self.isthreaded:
796 if self.isthreaded:
797 try:
797 try:
798 self.sys_excepthook.call_pdb = val
798 self.sys_excepthook.call_pdb = val
799 except:
799 except:
800 warn('Failed to activate pdb for threaded exception handler')
800 warn('Failed to activate pdb for threaded exception handler')
801
801
802 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
802 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
803 'Control auto-activation of pdb at exceptions')
803 'Control auto-activation of pdb at exceptions')
804
804
805 def debugger(self,force=False):
805 def debugger(self,force=False):
806 """Call the pydb/pdb debugger.
806 """Call the pydb/pdb debugger.
807
807
808 Keywords:
808 Keywords:
809
809
810 - force(False): by default, this routine checks the instance call_pdb
810 - force(False): by default, this routine checks the instance call_pdb
811 flag and does not actually invoke the debugger if the flag is false.
811 flag and does not actually invoke the debugger if the flag is false.
812 The 'force' option forces the debugger to activate even if the flag
812 The 'force' option forces the debugger to activate even if the flag
813 is false.
813 is false.
814 """
814 """
815
815
816 if not (force or self.call_pdb):
816 if not (force or self.call_pdb):
817 return
817 return
818
818
819 if not hasattr(sys,'last_traceback'):
819 if not hasattr(sys,'last_traceback'):
820 error('No traceback has been produced, nothing to debug.')
820 error('No traceback has been produced, nothing to debug.')
821 return
821 return
822
822
823 # use pydb if available
823 # use pydb if available
824 if debugger.has_pydb:
824 if debugger.has_pydb:
825 from pydb import pm
825 from pydb import pm
826 else:
826 else:
827 # fallback to our internal debugger
827 # fallback to our internal debugger
828 pm = lambda : self.InteractiveTB.debugger(force=True)
828 pm = lambda : self.InteractiveTB.debugger(force=True)
829 self.history_saving_wrapper(pm)()
829 self.history_saving_wrapper(pm)()
830
830
831 #-------------------------------------------------------------------------
831 #-------------------------------------------------------------------------
832 # Things related to IPython's various namespaces
832 # Things related to IPython's various namespaces
833 #-------------------------------------------------------------------------
833 #-------------------------------------------------------------------------
834
834
835 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
835 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
836 # Create the namespace where the user will operate. user_ns is
836 # Create the namespace where the user will operate. user_ns is
837 # normally the only one used, and it is passed to the exec calls as
837 # normally the only one used, and it is passed to the exec calls as
838 # the locals argument. But we do carry a user_global_ns namespace
838 # the locals argument. But we do carry a user_global_ns namespace
839 # given as the exec 'globals' argument, This is useful in embedding
839 # given as the exec 'globals' argument, This is useful in embedding
840 # situations where the ipython shell opens in a context where the
840 # situations where the ipython shell opens in a context where the
841 # distinction between locals and globals is meaningful. For
841 # distinction between locals and globals is meaningful. For
842 # non-embedded contexts, it is just the same object as the user_ns dict.
842 # non-embedded contexts, it is just the same object as the user_ns dict.
843
843
844 # FIXME. For some strange reason, __builtins__ is showing up at user
844 # FIXME. For some strange reason, __builtins__ is showing up at user
845 # level as a dict instead of a module. This is a manual fix, but I
845 # level as a dict instead of a module. This is a manual fix, but I
846 # should really track down where the problem is coming from. Alex
846 # should really track down where the problem is coming from. Alex
847 # Schmolck reported this problem first.
847 # Schmolck reported this problem first.
848
848
849 # A useful post by Alex Martelli on this topic:
849 # A useful post by Alex Martelli on this topic:
850 # Re: inconsistent value from __builtins__
850 # Re: inconsistent value from __builtins__
851 # Von: Alex Martelli <aleaxit@yahoo.com>
851 # Von: Alex Martelli <aleaxit@yahoo.com>
852 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
852 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
853 # Gruppen: comp.lang.python
853 # Gruppen: comp.lang.python
854
854
855 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
855 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
856 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
856 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
857 # > <type 'dict'>
857 # > <type 'dict'>
858 # > >>> print type(__builtins__)
858 # > >>> print type(__builtins__)
859 # > <type 'module'>
859 # > <type 'module'>
860 # > Is this difference in return value intentional?
860 # > Is this difference in return value intentional?
861
861
862 # Well, it's documented that '__builtins__' can be either a dictionary
862 # Well, it's documented that '__builtins__' can be either a dictionary
863 # or a module, and it's been that way for a long time. Whether it's
863 # or a module, and it's been that way for a long time. Whether it's
864 # intentional (or sensible), I don't know. In any case, the idea is
864 # intentional (or sensible), I don't know. In any case, the idea is
865 # that if you need to access the built-in namespace directly, you
865 # that if you need to access the built-in namespace directly, you
866 # should start with "import __builtin__" (note, no 's') which will
866 # should start with "import __builtin__" (note, no 's') which will
867 # definitely give you a module. Yeah, it's somewhat confusing:-(.
867 # definitely give you a module. Yeah, it's somewhat confusing:-(.
868
868
869 # These routines return properly built dicts as needed by the rest of
869 # These routines return properly built dicts as needed by the rest of
870 # the code, and can also be used by extension writers to generate
870 # the code, and can also be used by extension writers to generate
871 # properly initialized namespaces.
871 # properly initialized namespaces.
872 user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
872 user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
873
873
874 # Assign namespaces
874 # Assign namespaces
875 # This is the namespace where all normal user variables live
875 # This is the namespace where all normal user variables live
876 self.user_ns = user_ns
876 self.user_ns = user_ns
877 self.user_global_ns = user_global_ns
877 self.user_global_ns = user_global_ns
878
878
879 # An auxiliary namespace that checks what parts of the user_ns were
879 # An auxiliary namespace that checks what parts of the user_ns were
880 # loaded at startup, so we can list later only variables defined in
880 # loaded at startup, so we can list later only variables defined in
881 # actual interactive use. Since it is always a subset of user_ns, it
881 # actual interactive use. Since it is always a subset of user_ns, it
882 # doesn't need to be separately tracked in the ns_table.
882 # doesn't need to be separately tracked in the ns_table.
883 self.user_config_ns = {}
883 self.user_config_ns = {}
884
884
885 # A namespace to keep track of internal data structures to prevent
885 # A namespace to keep track of internal data structures to prevent
886 # them from cluttering user-visible stuff. Will be updated later
886 # them from cluttering user-visible stuff. Will be updated later
887 self.internal_ns = {}
887 self.internal_ns = {}
888
888
889 # Now that FakeModule produces a real module, we've run into a nasty
889 # Now that FakeModule produces a real module, we've run into a nasty
890 # problem: after script execution (via %run), the module where the user
890 # problem: after script execution (via %run), the module where the user
891 # code ran is deleted. Now that this object is a true module (needed
891 # code ran is deleted. Now that this object is a true module (needed
892 # so docetst and other tools work correctly), the Python module
892 # so docetst and other tools work correctly), the Python module
893 # teardown mechanism runs over it, and sets to None every variable
893 # teardown mechanism runs over it, and sets to None every variable
894 # present in that module. Top-level references to objects from the
894 # present in that module. Top-level references to objects from the
895 # script survive, because the user_ns is updated with them. However,
895 # script survive, because the user_ns is updated with them. However,
896 # calling functions defined in the script that use other things from
896 # calling functions defined in the script that use other things from
897 # the script will fail, because the function's closure had references
897 # the script will fail, because the function's closure had references
898 # to the original objects, which are now all None. So we must protect
898 # to the original objects, which are now all None. So we must protect
899 # these modules from deletion by keeping a cache.
899 # these modules from deletion by keeping a cache.
900 #
900 #
901 # To avoid keeping stale modules around (we only need the one from the
901 # To avoid keeping stale modules around (we only need the one from the
902 # last run), we use a dict keyed with the full path to the script, so
902 # last run), we use a dict keyed with the full path to the script, so
903 # only the last version of the module is held in the cache. Note,
903 # only the last version of the module is held in the cache. Note,
904 # however, that we must cache the module *namespace contents* (their
904 # however, that we must cache the module *namespace contents* (their
905 # __dict__). Because if we try to cache the actual modules, old ones
905 # __dict__). Because if we try to cache the actual modules, old ones
906 # (uncached) could be destroyed while still holding references (such as
906 # (uncached) could be destroyed while still holding references (such as
907 # those held by GUI objects that tend to be long-lived)>
907 # those held by GUI objects that tend to be long-lived)>
908 #
908 #
909 # The %reset command will flush this cache. See the cache_main_mod()
909 # The %reset command will flush this cache. See the cache_main_mod()
910 # and clear_main_mod_cache() methods for details on use.
910 # and clear_main_mod_cache() methods for details on use.
911
911
912 # This is the cache used for 'main' namespaces
912 # This is the cache used for 'main' namespaces
913 self._main_ns_cache = {}
913 self._main_ns_cache = {}
914 # And this is the single instance of FakeModule whose __dict__ we keep
914 # And this is the single instance of FakeModule whose __dict__ we keep
915 # copying and clearing for reuse on each %run
915 # copying and clearing for reuse on each %run
916 self._user_main_module = FakeModule()
916 self._user_main_module = FakeModule()
917
917
918 # A table holding all the namespaces IPython deals with, so that
918 # A table holding all the namespaces IPython deals with, so that
919 # introspection facilities can search easily.
919 # introspection facilities can search easily.
920 self.ns_table = {'user':user_ns,
920 self.ns_table = {'user':user_ns,
921 'user_global':user_global_ns,
921 'user_global':user_global_ns,
922 'internal':self.internal_ns,
922 'internal':self.internal_ns,
923 'builtin':__builtin__.__dict__
923 'builtin':__builtin__.__dict__
924 }
924 }
925
925
926 # Similarly, track all namespaces where references can be held and that
926 # Similarly, track all namespaces where references can be held and that
927 # we can safely clear (so it can NOT include builtin). This one can be
927 # we can safely clear (so it can NOT include builtin). This one can be
928 # a simple list.
928 # a simple list.
929 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
929 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
930 self.internal_ns, self._main_ns_cache ]
930 self.internal_ns, self._main_ns_cache ]
931
931
932 def init_sys_modules(self):
932 def init_sys_modules(self):
933 # We need to insert into sys.modules something that looks like a
933 # We need to insert into sys.modules something that looks like a
934 # module but which accesses the IPython namespace, for shelve and
934 # module but which accesses the IPython namespace, for shelve and
935 # pickle to work interactively. Normally they rely on getting
935 # pickle to work interactively. Normally they rely on getting
936 # everything out of __main__, but for embedding purposes each IPython
936 # everything out of __main__, but for embedding purposes each IPython
937 # instance has its own private namespace, so we can't go shoving
937 # instance has its own private namespace, so we can't go shoving
938 # everything into __main__.
938 # everything into __main__.
939
939
940 # note, however, that we should only do this for non-embedded
940 # note, however, that we should only do this for non-embedded
941 # ipythons, which really mimic the __main__.__dict__ with their own
941 # ipythons, which really mimic the __main__.__dict__ with their own
942 # namespace. Embedded instances, on the other hand, should not do
942 # namespace. Embedded instances, on the other hand, should not do
943 # this because they need to manage the user local/global namespaces
943 # this because they need to manage the user local/global namespaces
944 # only, but they live within a 'normal' __main__ (meaning, they
944 # only, but they live within a 'normal' __main__ (meaning, they
945 # shouldn't overtake the execution environment of the script they're
945 # shouldn't overtake the execution environment of the script they're
946 # embedded in).
946 # embedded in).
947
947
948 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
948 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
949
949
950 try:
950 try:
951 main_name = self.user_ns['__name__']
951 main_name = self.user_ns['__name__']
952 except KeyError:
952 except KeyError:
953 raise KeyError('user_ns dictionary MUST have a "__name__" key')
953 raise KeyError('user_ns dictionary MUST have a "__name__" key')
954 else:
954 else:
955 sys.modules[main_name] = FakeModule(self.user_ns)
955 sys.modules[main_name] = FakeModule(self.user_ns)
956
956
957 def init_user_ns(self):
957 def init_user_ns(self):
958 """Initialize all user-visible namespaces to their minimum defaults.
958 """Initialize all user-visible namespaces to their minimum defaults.
959
959
960 Certain history lists are also initialized here, as they effectively
960 Certain history lists are also initialized here, as they effectively
961 act as user namespaces.
961 act as user namespaces.
962
962
963 Notes
963 Notes
964 -----
964 -----
965 All data structures here are only filled in, they are NOT reset by this
965 All data structures here are only filled in, they are NOT reset by this
966 method. If they were not empty before, data will simply be added to
966 method. If they were not empty before, data will simply be added to
967 therm.
967 therm.
968 """
968 """
969 # This function works in two parts: first we put a few things in
969 # This function works in two parts: first we put a few things in
970 # user_ns, and we sync that contents into user_config_ns so that these
970 # user_ns, and we sync that contents into user_config_ns so that these
971 # initial variables aren't shown by %who. After the sync, we add the
971 # initial variables aren't shown by %who. After the sync, we add the
972 # rest of what we *do* want the user to see with %who even on a new
972 # rest of what we *do* want the user to see with %who even on a new
973 # session.
973 # session.
974 ns = {}
974 ns = {}
975
975
976 # Put 'help' in the user namespace
976 # Put 'help' in the user namespace
977 try:
977 try:
978 from site import _Helper
978 from site import _Helper
979 ns['help'] = _Helper()
979 ns['help'] = _Helper()
980 except ImportError:
980 except ImportError:
981 warn('help() not available - check site.py')
981 warn('help() not available - check site.py')
982
982
983 # make global variables for user access to the histories
983 # make global variables for user access to the histories
984 ns['_ih'] = self.input_hist
984 ns['_ih'] = self.input_hist
985 ns['_oh'] = self.output_hist
985 ns['_oh'] = self.output_hist
986 ns['_dh'] = self.dir_hist
986 ns['_dh'] = self.dir_hist
987
987
988 ns['_sh'] = shadowns
988 ns['_sh'] = shadowns
989
989
990 # Sync what we've added so far to user_config_ns so these aren't seen
990 # Sync what we've added so far to user_config_ns so these aren't seen
991 # by %who
991 # by %who
992 self.user_config_ns.update(ns)
992 self.user_config_ns.update(ns)
993
993
994 # Now, continue adding more contents
994 # Now, continue adding more contents
995
995
996 # user aliases to input and output histories
996 # user aliases to input and output histories
997 ns['In'] = self.input_hist
997 ns['In'] = self.input_hist
998 ns['Out'] = self.output_hist
998 ns['Out'] = self.output_hist
999
999
1000 # Store myself as the public api!!!
1000 # Store myself as the public api!!!
1001 ns['get_ipython'] = self.get_ipython
1001 ns['get_ipython'] = self.get_ipython
1002
1002
1003 # And update the real user's namespace
1003 # And update the real user's namespace
1004 self.user_ns.update(ns)
1004 self.user_ns.update(ns)
1005
1005
1006
1006
1007 def reset(self):
1007 def reset(self):
1008 """Clear all internal namespaces.
1008 """Clear all internal namespaces.
1009
1009
1010 Note that this is much more aggressive than %reset, since it clears
1010 Note that this is much more aggressive than %reset, since it clears
1011 fully all namespaces, as well as all input/output lists.
1011 fully all namespaces, as well as all input/output lists.
1012 """
1012 """
1013 for ns in self.ns_refs_table:
1013 for ns in self.ns_refs_table:
1014 ns.clear()
1014 ns.clear()
1015
1015
1016 self.alias_manager.clear_aliases()
1016 self.alias_manager.clear_aliases()
1017
1017
1018 # Clear input and output histories
1018 # Clear input and output histories
1019 self.input_hist[:] = []
1019 self.input_hist[:] = []
1020 self.input_hist_raw[:] = []
1020 self.input_hist_raw[:] = []
1021 self.output_hist.clear()
1021 self.output_hist.clear()
1022
1022
1023 # Restore the user namespaces to minimal usability
1023 # Restore the user namespaces to minimal usability
1024 self.init_user_ns()
1024 self.init_user_ns()
1025
1025
1026 # Restore the default and user aliases
1026 # Restore the default and user aliases
1027 self.alias_manager.init_aliases()
1027 self.alias_manager.init_aliases()
1028
1028
1029 def push(self, variables, interactive=True):
1029 def push(self, variables, interactive=True):
1030 """Inject a group of variables into the IPython user namespace.
1030 """Inject a group of variables into the IPython user namespace.
1031
1031
1032 Parameters
1032 Parameters
1033 ----------
1033 ----------
1034 variables : dict, str or list/tuple of str
1034 variables : dict, str or list/tuple of str
1035 The variables to inject into the user's namespace. If a dict,
1035 The variables to inject into the user's namespace. If a dict,
1036 a simple update is done. If a str, the string is assumed to
1036 a simple update is done. If a str, the string is assumed to
1037 have variable names separated by spaces. A list/tuple of str
1037 have variable names separated by spaces. A list/tuple of str
1038 can also be used to give the variable names. If just the variable
1038 can also be used to give the variable names. If just the variable
1039 names are give (list/tuple/str) then the variable values looked
1039 names are give (list/tuple/str) then the variable values looked
1040 up in the callers frame.
1040 up in the callers frame.
1041 interactive : bool
1041 interactive : bool
1042 If True (default), the variables will be listed with the ``who``
1042 If True (default), the variables will be listed with the ``who``
1043 magic.
1043 magic.
1044 """
1044 """
1045 vdict = None
1045 vdict = None
1046
1046
1047 # We need a dict of name/value pairs to do namespace updates.
1047 # We need a dict of name/value pairs to do namespace updates.
1048 if isinstance(variables, dict):
1048 if isinstance(variables, dict):
1049 vdict = variables
1049 vdict = variables
1050 elif isinstance(variables, (basestring, list, tuple)):
1050 elif isinstance(variables, (basestring, list, tuple)):
1051 if isinstance(variables, basestring):
1051 if isinstance(variables, basestring):
1052 vlist = variables.split()
1052 vlist = variables.split()
1053 else:
1053 else:
1054 vlist = variables
1054 vlist = variables
1055 vdict = {}
1055 vdict = {}
1056 cf = sys._getframe(1)
1056 cf = sys._getframe(1)
1057 for name in vlist:
1057 for name in vlist:
1058 try:
1058 try:
1059 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1059 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1060 except:
1060 except:
1061 print ('Could not get variable %s from %s' %
1061 print ('Could not get variable %s from %s' %
1062 (name,cf.f_code.co_name))
1062 (name,cf.f_code.co_name))
1063 else:
1063 else:
1064 raise ValueError('variables must be a dict/str/list/tuple')
1064 raise ValueError('variables must be a dict/str/list/tuple')
1065
1065
1066 # Propagate variables to user namespace
1066 # Propagate variables to user namespace
1067 self.user_ns.update(vdict)
1067 self.user_ns.update(vdict)
1068
1068
1069 # And configure interactive visibility
1069 # And configure interactive visibility
1070 config_ns = self.user_config_ns
1070 config_ns = self.user_config_ns
1071 if interactive:
1071 if interactive:
1072 for name, val in vdict.iteritems():
1072 for name, val in vdict.iteritems():
1073 config_ns.pop(name, None)
1073 config_ns.pop(name, None)
1074 else:
1074 else:
1075 for name,val in vdict.iteritems():
1075 for name,val in vdict.iteritems():
1076 config_ns[name] = val
1076 config_ns[name] = val
1077
1077
1078 #-------------------------------------------------------------------------
1078 #-------------------------------------------------------------------------
1079 # Things related to history management
1079 # Things related to history management
1080 #-------------------------------------------------------------------------
1080 #-------------------------------------------------------------------------
1081
1081
1082 def init_history(self):
1082 def init_history(self):
1083 # List of input with multi-line handling.
1083 # List of input with multi-line handling.
1084 self.input_hist = InputList()
1084 self.input_hist = InputList()
1085 # This one will hold the 'raw' input history, without any
1085 # This one will hold the 'raw' input history, without any
1086 # pre-processing. This will allow users to retrieve the input just as
1086 # pre-processing. This will allow users to retrieve the input just as
1087 # it was exactly typed in by the user, with %hist -r.
1087 # it was exactly typed in by the user, with %hist -r.
1088 self.input_hist_raw = InputList()
1088 self.input_hist_raw = InputList()
1089
1089
1090 # list of visited directories
1090 # list of visited directories
1091 try:
1091 try:
1092 self.dir_hist = [os.getcwd()]
1092 self.dir_hist = [os.getcwd()]
1093 except OSError:
1093 except OSError:
1094 self.dir_hist = []
1094 self.dir_hist = []
1095
1095
1096 # dict of output history
1096 # dict of output history
1097 self.output_hist = {}
1097 self.output_hist = {}
1098
1098
1099 # Now the history file
1099 # Now the history file
1100 if self.profile:
1100 if self.profile:
1101 histfname = 'history-%s' % self.profile
1101 histfname = 'history-%s' % self.profile
1102 else:
1102 else:
1103 histfname = 'history'
1103 histfname = 'history'
1104 self.histfile = os.path.join(self.ipython_dir, histfname)
1104 self.histfile = os.path.join(self.ipython_dir, histfname)
1105
1105
1106 # Fill the history zero entry, user counter starts at 1
1106 # Fill the history zero entry, user counter starts at 1
1107 self.input_hist.append('\n')
1107 self.input_hist.append('\n')
1108 self.input_hist_raw.append('\n')
1108 self.input_hist_raw.append('\n')
1109
1109
1110 def init_shadow_hist(self):
1110 def init_shadow_hist(self):
1111 try:
1111 try:
1112 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1112 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1113 except exceptions.UnicodeDecodeError:
1113 except exceptions.UnicodeDecodeError:
1114 print "Your ipython_dir can't be decoded to unicode!"
1114 print "Your ipython_dir can't be decoded to unicode!"
1115 print "Please set HOME environment variable to something that"
1115 print "Please set HOME environment variable to something that"
1116 print r"only has ASCII characters, e.g. c:\home"
1116 print r"only has ASCII characters, e.g. c:\home"
1117 print "Now it is", self.ipython_dir
1117 print "Now it is", self.ipython_dir
1118 sys.exit()
1118 sys.exit()
1119 self.shadowhist = ipcorehist.ShadowHist(self.db)
1119 self.shadowhist = ipcorehist.ShadowHist(self.db)
1120
1120
1121 def savehist(self):
1121 def savehist(self):
1122 """Save input history to a file (via readline library)."""
1122 """Save input history to a file (via readline library)."""
1123
1123
1124 try:
1124 try:
1125 self.readline.write_history_file(self.histfile)
1125 self.readline.write_history_file(self.histfile)
1126 except:
1126 except:
1127 print 'Unable to save IPython command history to file: ' + \
1127 print 'Unable to save IPython command history to file: ' + \
1128 `self.histfile`
1128 `self.histfile`
1129
1129
1130 def reloadhist(self):
1130 def reloadhist(self):
1131 """Reload the input history from disk file."""
1131 """Reload the input history from disk file."""
1132
1132
1133 try:
1133 try:
1134 self.readline.clear_history()
1134 self.readline.clear_history()
1135 self.readline.read_history_file(self.shell.histfile)
1135 self.readline.read_history_file(self.shell.histfile)
1136 except AttributeError:
1136 except AttributeError:
1137 pass
1137 pass
1138
1138
1139 def history_saving_wrapper(self, func):
1139 def history_saving_wrapper(self, func):
1140 """ Wrap func for readline history saving
1140 """ Wrap func for readline history saving
1141
1141
1142 Convert func into callable that saves & restores
1142 Convert func into callable that saves & restores
1143 history around the call """
1143 history around the call """
1144
1144
1145 if not self.has_readline:
1145 if not self.has_readline:
1146 return func
1146 return func
1147
1147
1148 def wrapper():
1148 def wrapper():
1149 self.savehist()
1149 self.savehist()
1150 try:
1150 try:
1151 func()
1151 func()
1152 finally:
1152 finally:
1153 readline.read_history_file(self.histfile)
1153 readline.read_history_file(self.histfile)
1154 return wrapper
1154 return wrapper
1155
1155
1156 #-------------------------------------------------------------------------
1156 #-------------------------------------------------------------------------
1157 # Things related to exception handling and tracebacks (not debugging)
1157 # Things related to exception handling and tracebacks (not debugging)
1158 #-------------------------------------------------------------------------
1158 #-------------------------------------------------------------------------
1159
1159
1160 def init_traceback_handlers(self, custom_exceptions):
1160 def init_traceback_handlers(self, custom_exceptions):
1161 # Syntax error handler.
1161 # Syntax error handler.
1162 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1162 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1163
1163
1164 # The interactive one is initialized with an offset, meaning we always
1164 # The interactive one is initialized with an offset, meaning we always
1165 # want to remove the topmost item in the traceback, which is our own
1165 # want to remove the topmost item in the traceback, which is our own
1166 # internal code. Valid modes: ['Plain','Context','Verbose']
1166 # internal code. Valid modes: ['Plain','Context','Verbose']
1167 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1167 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1168 color_scheme='NoColor',
1168 color_scheme='NoColor',
1169 tb_offset = 1)
1169 tb_offset = 1)
1170
1170
1171 # The instance will store a pointer to the system-wide exception hook,
1171 # The instance will store a pointer to the system-wide exception hook,
1172 # so that runtime code (such as magics) can access it. This is because
1172 # so that runtime code (such as magics) can access it. This is because
1173 # during the read-eval loop, it may get temporarily overwritten.
1173 # during the read-eval loop, it may get temporarily overwritten.
1174 self.sys_excepthook = sys.excepthook
1174 self.sys_excepthook = sys.excepthook
1175
1175
1176 # and add any custom exception handlers the user may have specified
1176 # and add any custom exception handlers the user may have specified
1177 self.set_custom_exc(*custom_exceptions)
1177 self.set_custom_exc(*custom_exceptions)
1178
1178
1179 def set_custom_exc(self,exc_tuple,handler):
1179 def set_custom_exc(self,exc_tuple,handler):
1180 """set_custom_exc(exc_tuple,handler)
1180 """set_custom_exc(exc_tuple,handler)
1181
1181
1182 Set a custom exception handler, which will be called if any of the
1182 Set a custom exception handler, which will be called if any of the
1183 exceptions in exc_tuple occur in the mainloop (specifically, in the
1183 exceptions in exc_tuple occur in the mainloop (specifically, in the
1184 runcode() method.
1184 runcode() method.
1185
1185
1186 Inputs:
1186 Inputs:
1187
1187
1188 - exc_tuple: a *tuple* of valid exceptions to call the defined
1188 - exc_tuple: a *tuple* of valid exceptions to call the defined
1189 handler for. It is very important that you use a tuple, and NOT A
1189 handler for. It is very important that you use a tuple, and NOT A
1190 LIST here, because of the way Python's except statement works. If
1190 LIST here, because of the way Python's except statement works. If
1191 you only want to trap a single exception, use a singleton tuple:
1191 you only want to trap a single exception, use a singleton tuple:
1192
1192
1193 exc_tuple == (MyCustomException,)
1193 exc_tuple == (MyCustomException,)
1194
1194
1195 - handler: this must be defined as a function with the following
1195 - handler: this must be defined as a function with the following
1196 basic interface: def my_handler(self,etype,value,tb).
1196 basic interface: def my_handler(self,etype,value,tb).
1197
1197
1198 This will be made into an instance method (via new.instancemethod)
1198 This will be made into an instance method (via new.instancemethod)
1199 of IPython itself, and it will be called if any of the exceptions
1199 of IPython itself, and it will be called if any of the exceptions
1200 listed in the exc_tuple are caught. If the handler is None, an
1200 listed in the exc_tuple are caught. If the handler is None, an
1201 internal basic one is used, which just prints basic info.
1201 internal basic one is used, which just prints basic info.
1202
1202
1203 WARNING: by putting in your own exception handler into IPython's main
1203 WARNING: by putting in your own exception handler into IPython's main
1204 execution loop, you run a very good chance of nasty crashes. This
1204 execution loop, you run a very good chance of nasty crashes. This
1205 facility should only be used if you really know what you are doing."""
1205 facility should only be used if you really know what you are doing."""
1206
1206
1207 assert type(exc_tuple)==type(()) , \
1207 assert type(exc_tuple)==type(()) , \
1208 "The custom exceptions must be given AS A TUPLE."
1208 "The custom exceptions must be given AS A TUPLE."
1209
1209
1210 def dummy_handler(self,etype,value,tb):
1210 def dummy_handler(self,etype,value,tb):
1211 print '*** Simple custom exception handler ***'
1211 print '*** Simple custom exception handler ***'
1212 print 'Exception type :',etype
1212 print 'Exception type :',etype
1213 print 'Exception value:',value
1213 print 'Exception value:',value
1214 print 'Traceback :',tb
1214 print 'Traceback :',tb
1215 print 'Source code :','\n'.join(self.buffer)
1215 print 'Source code :','\n'.join(self.buffer)
1216
1216
1217 if handler is None: handler = dummy_handler
1217 if handler is None: handler = dummy_handler
1218
1218
1219 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1219 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1220 self.custom_exceptions = exc_tuple
1220 self.custom_exceptions = exc_tuple
1221
1221
1222 def excepthook(self, etype, value, tb):
1222 def excepthook(self, etype, value, tb):
1223 """One more defense for GUI apps that call sys.excepthook.
1223 """One more defense for GUI apps that call sys.excepthook.
1224
1224
1225 GUI frameworks like wxPython trap exceptions and call
1225 GUI frameworks like wxPython trap exceptions and call
1226 sys.excepthook themselves. I guess this is a feature that
1226 sys.excepthook themselves. I guess this is a feature that
1227 enables them to keep running after exceptions that would
1227 enables them to keep running after exceptions that would
1228 otherwise kill their mainloop. This is a bother for IPython
1228 otherwise kill their mainloop. This is a bother for IPython
1229 which excepts to catch all of the program exceptions with a try:
1229 which excepts to catch all of the program exceptions with a try:
1230 except: statement.
1230 except: statement.
1231
1231
1232 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1232 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1233 any app directly invokes sys.excepthook, it will look to the user like
1233 any app directly invokes sys.excepthook, it will look to the user like
1234 IPython crashed. In order to work around this, we can disable the
1234 IPython crashed. In order to work around this, we can disable the
1235 CrashHandler and replace it with this excepthook instead, which prints a
1235 CrashHandler and replace it with this excepthook instead, which prints a
1236 regular traceback using our InteractiveTB. In this fashion, apps which
1236 regular traceback using our InteractiveTB. In this fashion, apps which
1237 call sys.excepthook will generate a regular-looking exception from
1237 call sys.excepthook will generate a regular-looking exception from
1238 IPython, and the CrashHandler will only be triggered by real IPython
1238 IPython, and the CrashHandler will only be triggered by real IPython
1239 crashes.
1239 crashes.
1240
1240
1241 This hook should be used sparingly, only in places which are not likely
1241 This hook should be used sparingly, only in places which are not likely
1242 to be true IPython errors.
1242 to be true IPython errors.
1243 """
1243 """
1244 self.showtraceback((etype,value,tb),tb_offset=0)
1244 self.showtraceback((etype,value,tb),tb_offset=0)
1245
1245
1246 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1246 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1247 exception_only=False):
1247 exception_only=False):
1248 """Display the exception that just occurred.
1248 """Display the exception that just occurred.
1249
1249
1250 If nothing is known about the exception, this is the method which
1250 If nothing is known about the exception, this is the method which
1251 should be used throughout the code for presenting user tracebacks,
1251 should be used throughout the code for presenting user tracebacks,
1252 rather than directly invoking the InteractiveTB object.
1252 rather than directly invoking the InteractiveTB object.
1253
1253
1254 A specific showsyntaxerror() also exists, but this method can take
1254 A specific showsyntaxerror() also exists, but this method can take
1255 care of calling it if needed, so unless you are explicitly catching a
1255 care of calling it if needed, so unless you are explicitly catching a
1256 SyntaxError exception, don't try to analyze the stack manually and
1256 SyntaxError exception, don't try to analyze the stack manually and
1257 simply call this method."""
1257 simply call this method."""
1258
1258
1259 try:
1259 try:
1260 if exc_tuple is None:
1260 if exc_tuple is None:
1261 etype, value, tb = sys.exc_info()
1261 etype, value, tb = sys.exc_info()
1262 else:
1262 else:
1263 etype, value, tb = exc_tuple
1263 etype, value, tb = exc_tuple
1264
1264
1265 if etype is None:
1265 if etype is None:
1266 if hasattr(sys, 'last_type'):
1266 if hasattr(sys, 'last_type'):
1267 etype, value, tb = sys.last_type, sys.last_value, \
1267 etype, value, tb = sys.last_type, sys.last_value, \
1268 sys.last_traceback
1268 sys.last_traceback
1269 else:
1269 else:
1270 self.write('No traceback available to show.\n')
1270 self.write('No traceback available to show.\n')
1271 return
1271 return
1272
1272
1273 if etype is SyntaxError:
1273 if etype is SyntaxError:
1274 # Though this won't be called by syntax errors in the input
1274 # Though this won't be called by syntax errors in the input
1275 # line, there may be SyntaxError cases whith imported code.
1275 # line, there may be SyntaxError cases whith imported code.
1276 self.showsyntaxerror(filename)
1276 self.showsyntaxerror(filename)
1277 elif etype is UsageError:
1277 elif etype is UsageError:
1278 print "UsageError:", value
1278 print "UsageError:", value
1279 else:
1279 else:
1280 # WARNING: these variables are somewhat deprecated and not
1280 # WARNING: these variables are somewhat deprecated and not
1281 # necessarily safe to use in a threaded environment, but tools
1281 # necessarily safe to use in a threaded environment, but tools
1282 # like pdb depend on their existence, so let's set them. If we
1282 # like pdb depend on their existence, so let's set them. If we
1283 # find problems in the field, we'll need to revisit their use.
1283 # find problems in the field, we'll need to revisit their use.
1284 sys.last_type = etype
1284 sys.last_type = etype
1285 sys.last_value = value
1285 sys.last_value = value
1286 sys.last_traceback = tb
1286 sys.last_traceback = tb
1287
1287
1288 if etype in self.custom_exceptions:
1288 if etype in self.custom_exceptions:
1289 self.CustomTB(etype,value,tb)
1289 self.CustomTB(etype,value,tb)
1290 else:
1290 else:
1291 if exception_only:
1291 if exception_only:
1292 m = ('An exception has occurred, use %tb to see the '
1292 m = ('An exception has occurred, use %tb to see the '
1293 'full traceback.')
1293 'full traceback.')
1294 print m
1294 print m
1295 self.InteractiveTB.show_exception_only(etype, value)
1295 self.InteractiveTB.show_exception_only(etype, value)
1296 else:
1296 else:
1297 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1297 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1298 if self.InteractiveTB.call_pdb:
1298 if self.InteractiveTB.call_pdb:
1299 # pdb mucks up readline, fix it back
1299 # pdb mucks up readline, fix it back
1300 self.set_completer()
1300 self.set_completer()
1301
1301
1302 except KeyboardInterrupt:
1302 except KeyboardInterrupt:
1303 self.write("\nKeyboardInterrupt\n")
1303 self.write("\nKeyboardInterrupt\n")
1304
1304
1305
1305
1306 def showsyntaxerror(self, filename=None):
1306 def showsyntaxerror(self, filename=None):
1307 """Display the syntax error that just occurred.
1307 """Display the syntax error that just occurred.
1308
1308
1309 This doesn't display a stack trace because there isn't one.
1309 This doesn't display a stack trace because there isn't one.
1310
1310
1311 If a filename is given, it is stuffed in the exception instead
1311 If a filename is given, it is stuffed in the exception instead
1312 of what was there before (because Python's parser always uses
1312 of what was there before (because Python's parser always uses
1313 "<string>" when reading from a string).
1313 "<string>" when reading from a string).
1314 """
1314 """
1315 etype, value, last_traceback = sys.exc_info()
1315 etype, value, last_traceback = sys.exc_info()
1316
1316
1317 # See note about these variables in showtraceback() above
1317 # See note about these variables in showtraceback() above
1318 sys.last_type = etype
1318 sys.last_type = etype
1319 sys.last_value = value
1319 sys.last_value = value
1320 sys.last_traceback = last_traceback
1320 sys.last_traceback = last_traceback
1321
1321
1322 if filename and etype is SyntaxError:
1322 if filename and etype is SyntaxError:
1323 # Work hard to stuff the correct filename in the exception
1323 # Work hard to stuff the correct filename in the exception
1324 try:
1324 try:
1325 msg, (dummy_filename, lineno, offset, line) = value
1325 msg, (dummy_filename, lineno, offset, line) = value
1326 except:
1326 except:
1327 # Not the format we expect; leave it alone
1327 # Not the format we expect; leave it alone
1328 pass
1328 pass
1329 else:
1329 else:
1330 # Stuff in the right filename
1330 # Stuff in the right filename
1331 try:
1331 try:
1332 # Assume SyntaxError is a class exception
1332 # Assume SyntaxError is a class exception
1333 value = SyntaxError(msg, (filename, lineno, offset, line))
1333 value = SyntaxError(msg, (filename, lineno, offset, line))
1334 except:
1334 except:
1335 # If that failed, assume SyntaxError is a string
1335 # If that failed, assume SyntaxError is a string
1336 value = msg, (filename, lineno, offset, line)
1336 value = msg, (filename, lineno, offset, line)
1337 self.SyntaxTB(etype,value,[])
1337 self.SyntaxTB(etype,value,[])
1338
1338
1339 def edit_syntax_error(self):
1339 def edit_syntax_error(self):
1340 """The bottom half of the syntax error handler called in the main loop.
1340 """The bottom half of the syntax error handler called in the main loop.
1341
1341
1342 Loop until syntax error is fixed or user cancels.
1342 Loop until syntax error is fixed or user cancels.
1343 """
1343 """
1344
1344
1345 while self.SyntaxTB.last_syntax_error:
1345 while self.SyntaxTB.last_syntax_error:
1346 # copy and clear last_syntax_error
1346 # copy and clear last_syntax_error
1347 err = self.SyntaxTB.clear_err_state()
1347 err = self.SyntaxTB.clear_err_state()
1348 if not self._should_recompile(err):
1348 if not self._should_recompile(err):
1349 return
1349 return
1350 try:
1350 try:
1351 # may set last_syntax_error again if a SyntaxError is raised
1351 # may set last_syntax_error again if a SyntaxError is raised
1352 self.safe_execfile(err.filename,self.user_ns)
1352 self.safe_execfile(err.filename,self.user_ns)
1353 except:
1353 except:
1354 self.showtraceback()
1354 self.showtraceback()
1355 else:
1355 else:
1356 try:
1356 try:
1357 f = file(err.filename)
1357 f = file(err.filename)
1358 try:
1358 try:
1359 # This should be inside a display_trap block and I
1359 # This should be inside a display_trap block and I
1360 # think it is.
1360 # think it is.
1361 sys.displayhook(f.read())
1361 sys.displayhook(f.read())
1362 finally:
1362 finally:
1363 f.close()
1363 f.close()
1364 except:
1364 except:
1365 self.showtraceback()
1365 self.showtraceback()
1366
1366
1367 def _should_recompile(self,e):
1367 def _should_recompile(self,e):
1368 """Utility routine for edit_syntax_error"""
1368 """Utility routine for edit_syntax_error"""
1369
1369
1370 if e.filename in ('<ipython console>','<input>','<string>',
1370 if e.filename in ('<ipython console>','<input>','<string>',
1371 '<console>','<BackgroundJob compilation>',
1371 '<console>','<BackgroundJob compilation>',
1372 None):
1372 None):
1373
1373
1374 return False
1374 return False
1375 try:
1375 try:
1376 if (self.autoedit_syntax and
1376 if (self.autoedit_syntax and
1377 not self.ask_yes_no('Return to editor to correct syntax error? '
1377 not self.ask_yes_no('Return to editor to correct syntax error? '
1378 '[Y/n] ','y')):
1378 '[Y/n] ','y')):
1379 return False
1379 return False
1380 except EOFError:
1380 except EOFError:
1381 return False
1381 return False
1382
1382
1383 def int0(x):
1383 def int0(x):
1384 try:
1384 try:
1385 return int(x)
1385 return int(x)
1386 except TypeError:
1386 except TypeError:
1387 return 0
1387 return 0
1388 # always pass integer line and offset values to editor hook
1388 # always pass integer line and offset values to editor hook
1389 try:
1389 try:
1390 self.hooks.fix_error_editor(e.filename,
1390 self.hooks.fix_error_editor(e.filename,
1391 int0(e.lineno),int0(e.offset),e.msg)
1391 int0(e.lineno),int0(e.offset),e.msg)
1392 except TryNext:
1392 except TryNext:
1393 warn('Could not open editor')
1393 warn('Could not open editor')
1394 return False
1394 return False
1395 return True
1395 return True
1396
1396
1397 #-------------------------------------------------------------------------
1397 #-------------------------------------------------------------------------
1398 # Things related to tab completion
1398 # Things related to tab completion
1399 #-------------------------------------------------------------------------
1399 #-------------------------------------------------------------------------
1400
1400
1401 def complete(self, text):
1401 def complete(self, text):
1402 """Return a sorted list of all possible completions on text.
1402 """Return a sorted list of all possible completions on text.
1403
1403
1404 Inputs:
1404 Inputs:
1405
1405
1406 - text: a string of text to be completed on.
1406 - text: a string of text to be completed on.
1407
1407
1408 This is a wrapper around the completion mechanism, similar to what
1408 This is a wrapper around the completion mechanism, similar to what
1409 readline does at the command line when the TAB key is hit. By
1409 readline does at the command line when the TAB key is hit. By
1410 exposing it as a method, it can be used by other non-readline
1410 exposing it as a method, it can be used by other non-readline
1411 environments (such as GUIs) for text completion.
1411 environments (such as GUIs) for text completion.
1412
1412
1413 Simple usage example:
1413 Simple usage example:
1414
1414
1415 In [7]: x = 'hello'
1415 In [7]: x = 'hello'
1416
1416
1417 In [8]: x
1417 In [8]: x
1418 Out[8]: 'hello'
1418 Out[8]: 'hello'
1419
1419
1420 In [9]: print x
1420 In [9]: print x
1421 hello
1421 hello
1422
1422
1423 In [10]: _ip.complete('x.l')
1423 In [10]: _ip.complete('x.l')
1424 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1424 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1425 """
1425 """
1426
1426
1427 # Inject names into __builtin__ so we can complete on the added names.
1427 # Inject names into __builtin__ so we can complete on the added names.
1428 with self.builtin_trap:
1428 with self.builtin_trap:
1429 complete = self.Completer.complete
1429 complete = self.Completer.complete
1430 state = 0
1430 state = 0
1431 # use a dict so we get unique keys, since ipyhton's multiple
1431 # use a dict so we get unique keys, since ipyhton's multiple
1432 # completers can return duplicates. When we make 2.4 a requirement,
1432 # completers can return duplicates. When we make 2.4 a requirement,
1433 # start using sets instead, which are faster.
1433 # start using sets instead, which are faster.
1434 comps = {}
1434 comps = {}
1435 while True:
1435 while True:
1436 newcomp = complete(text,state,line_buffer=text)
1436 newcomp = complete(text,state,line_buffer=text)
1437 if newcomp is None:
1437 if newcomp is None:
1438 break
1438 break
1439 comps[newcomp] = 1
1439 comps[newcomp] = 1
1440 state += 1
1440 state += 1
1441 outcomps = comps.keys()
1441 outcomps = comps.keys()
1442 outcomps.sort()
1442 outcomps.sort()
1443 #print "T:",text,"OC:",outcomps # dbg
1443 #print "T:",text,"OC:",outcomps # dbg
1444 #print "vars:",self.user_ns.keys()
1444 #print "vars:",self.user_ns.keys()
1445 return outcomps
1445 return outcomps
1446
1446
1447 def set_custom_completer(self,completer,pos=0):
1447 def set_custom_completer(self,completer,pos=0):
1448 """Adds a new custom completer function.
1448 """Adds a new custom completer function.
1449
1449
1450 The position argument (defaults to 0) is the index in the completers
1450 The position argument (defaults to 0) is the index in the completers
1451 list where you want the completer to be inserted."""
1451 list where you want the completer to be inserted."""
1452
1452
1453 newcomp = new.instancemethod(completer,self.Completer,
1453 newcomp = new.instancemethod(completer,self.Completer,
1454 self.Completer.__class__)
1454 self.Completer.__class__)
1455 self.Completer.matchers.insert(pos,newcomp)
1455 self.Completer.matchers.insert(pos,newcomp)
1456
1456
1457 def set_completer(self):
1457 def set_completer(self):
1458 """Reset readline's completer to be our own."""
1458 """Reset readline's completer to be our own."""
1459 self.readline.set_completer(self.Completer.complete)
1459 self.readline.set_completer(self.Completer.complete)
1460
1460
1461 def set_completer_frame(self, frame=None):
1461 def set_completer_frame(self, frame=None):
1462 """Set the frame of the completer."""
1462 """Set the frame of the completer."""
1463 if frame:
1463 if frame:
1464 self.Completer.namespace = frame.f_locals
1464 self.Completer.namespace = frame.f_locals
1465 self.Completer.global_namespace = frame.f_globals
1465 self.Completer.global_namespace = frame.f_globals
1466 else:
1466 else:
1467 self.Completer.namespace = self.user_ns
1467 self.Completer.namespace = self.user_ns
1468 self.Completer.global_namespace = self.user_global_ns
1468 self.Completer.global_namespace = self.user_global_ns
1469
1469
1470 #-------------------------------------------------------------------------
1470 #-------------------------------------------------------------------------
1471 # Things related to readline
1471 # Things related to readline
1472 #-------------------------------------------------------------------------
1472 #-------------------------------------------------------------------------
1473
1473
1474 def init_readline(self):
1474 def init_readline(self):
1475 """Command history completion/saving/reloading."""
1475 """Command history completion/saving/reloading."""
1476
1476
1477 if self.readline_use:
1477 if self.readline_use:
1478 import IPython.utils.rlineimpl as readline
1478 import IPython.utils.rlineimpl as readline
1479
1479
1480 self.rl_next_input = None
1480 self.rl_next_input = None
1481 self.rl_do_indent = False
1481 self.rl_do_indent = False
1482
1482
1483 if not self.readline_use or not readline.have_readline:
1483 if not self.readline_use or not readline.have_readline:
1484 self.has_readline = False
1484 self.has_readline = False
1485 self.readline = None
1485 self.readline = None
1486 # Set a number of methods that depend on readline to be no-op
1486 # Set a number of methods that depend on readline to be no-op
1487 self.savehist = no_op
1487 self.savehist = no_op
1488 self.reloadhist = no_op
1488 self.reloadhist = no_op
1489 self.set_completer = no_op
1489 self.set_completer = no_op
1490 self.set_custom_completer = no_op
1490 self.set_custom_completer = no_op
1491 self.set_completer_frame = no_op
1491 self.set_completer_frame = no_op
1492 warn('Readline services not available or not loaded.')
1492 warn('Readline services not available or not loaded.')
1493 else:
1493 else:
1494 self.has_readline = True
1494 self.has_readline = True
1495 self.readline = readline
1495 self.readline = readline
1496 sys.modules['readline'] = readline
1496 sys.modules['readline'] = readline
1497 import atexit
1497 import atexit
1498 from IPython.core.completer import IPCompleter
1498 from IPython.core.completer import IPCompleter
1499 self.Completer = IPCompleter(self,
1499 self.Completer = IPCompleter(self,
1500 self.user_ns,
1500 self.user_ns,
1501 self.user_global_ns,
1501 self.user_global_ns,
1502 self.readline_omit__names,
1502 self.readline_omit__names,
1503 self.alias_manager.alias_table)
1503 self.alias_manager.alias_table)
1504 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1504 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1505 self.strdispatchers['complete_command'] = sdisp
1505 self.strdispatchers['complete_command'] = sdisp
1506 self.Completer.custom_completers = sdisp
1506 self.Completer.custom_completers = sdisp
1507 # Platform-specific configuration
1507 # Platform-specific configuration
1508 if os.name == 'nt':
1508 if os.name == 'nt':
1509 self.readline_startup_hook = readline.set_pre_input_hook
1509 self.readline_startup_hook = readline.set_pre_input_hook
1510 else:
1510 else:
1511 self.readline_startup_hook = readline.set_startup_hook
1511 self.readline_startup_hook = readline.set_startup_hook
1512
1512
1513 # Load user's initrc file (readline config)
1513 # Load user's initrc file (readline config)
1514 # Or if libedit is used, load editrc.
1514 # Or if libedit is used, load editrc.
1515 inputrc_name = os.environ.get('INPUTRC')
1515 inputrc_name = os.environ.get('INPUTRC')
1516 if inputrc_name is None:
1516 if inputrc_name is None:
1517 home_dir = get_home_dir()
1517 home_dir = get_home_dir()
1518 if home_dir is not None:
1518 if home_dir is not None:
1519 inputrc_name = '.inputrc'
1519 inputrc_name = '.inputrc'
1520 if readline.uses_libedit:
1520 if readline.uses_libedit:
1521 inputrc_name = '.editrc'
1521 inputrc_name = '.editrc'
1522 inputrc_name = os.path.join(home_dir, inputrc_name)
1522 inputrc_name = os.path.join(home_dir, inputrc_name)
1523 if os.path.isfile(inputrc_name):
1523 if os.path.isfile(inputrc_name):
1524 try:
1524 try:
1525 readline.read_init_file(inputrc_name)
1525 readline.read_init_file(inputrc_name)
1526 except:
1526 except:
1527 warn('Problems reading readline initialization file <%s>'
1527 warn('Problems reading readline initialization file <%s>'
1528 % inputrc_name)
1528 % inputrc_name)
1529
1529
1530 # save this in sys so embedded copies can restore it properly
1530 # save this in sys so embedded copies can restore it properly
1531 sys.ipcompleter = self.Completer.complete
1531 sys.ipcompleter = self.Completer.complete
1532 self.set_completer()
1532 self.set_completer()
1533
1533
1534 # Configure readline according to user's prefs
1534 # Configure readline according to user's prefs
1535 # This is only done if GNU readline is being used. If libedit
1535 # This is only done if GNU readline is being used. If libedit
1536 # is being used (as on Leopard) the readline config is
1536 # is being used (as on Leopard) the readline config is
1537 # not run as the syntax for libedit is different.
1537 # not run as the syntax for libedit is different.
1538 if not readline.uses_libedit:
1538 if not readline.uses_libedit:
1539 for rlcommand in self.readline_parse_and_bind:
1539 for rlcommand in self.readline_parse_and_bind:
1540 #print "loading rl:",rlcommand # dbg
1540 #print "loading rl:",rlcommand # dbg
1541 readline.parse_and_bind(rlcommand)
1541 readline.parse_and_bind(rlcommand)
1542
1542
1543 # Remove some chars from the delimiters list. If we encounter
1543 # Remove some chars from the delimiters list. If we encounter
1544 # unicode chars, discard them.
1544 # unicode chars, discard them.
1545 delims = readline.get_completer_delims().encode("ascii", "ignore")
1545 delims = readline.get_completer_delims().encode("ascii", "ignore")
1546 delims = delims.translate(string._idmap,
1546 delims = delims.translate(string._idmap,
1547 self.readline_remove_delims)
1547 self.readline_remove_delims)
1548 readline.set_completer_delims(delims)
1548 readline.set_completer_delims(delims)
1549 # otherwise we end up with a monster history after a while:
1549 # otherwise we end up with a monster history after a while:
1550 readline.set_history_length(1000)
1550 readline.set_history_length(1000)
1551 try:
1551 try:
1552 #print '*** Reading readline history' # dbg
1552 #print '*** Reading readline history' # dbg
1553 readline.read_history_file(self.histfile)
1553 readline.read_history_file(self.histfile)
1554 except IOError:
1554 except IOError:
1555 pass # It doesn't exist yet.
1555 pass # It doesn't exist yet.
1556
1556
1557 atexit.register(self.atexit_operations)
1557 atexit.register(self.atexit_operations)
1558 del atexit
1558 del atexit
1559
1559
1560 # Configure auto-indent for all platforms
1560 # Configure auto-indent for all platforms
1561 self.set_autoindent(self.autoindent)
1561 self.set_autoindent(self.autoindent)
1562
1562
1563 def set_next_input(self, s):
1563 def set_next_input(self, s):
1564 """ Sets the 'default' input string for the next command line.
1564 """ Sets the 'default' input string for the next command line.
1565
1565
1566 Requires readline.
1566 Requires readline.
1567
1567
1568 Example:
1568 Example:
1569
1569
1570 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1570 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1571 [D:\ipython]|2> Hello Word_ # cursor is here
1571 [D:\ipython]|2> Hello Word_ # cursor is here
1572 """
1572 """
1573
1573
1574 self.rl_next_input = s
1574 self.rl_next_input = s
1575
1575
1576 def pre_readline(self):
1576 def pre_readline(self):
1577 """readline hook to be used at the start of each line.
1577 """readline hook to be used at the start of each line.
1578
1578
1579 Currently it handles auto-indent only."""
1579 Currently it handles auto-indent only."""
1580
1580
1581 #debugx('self.indent_current_nsp','pre_readline:')
1581 #debugx('self.indent_current_nsp','pre_readline:')
1582
1582
1583 if self.rl_do_indent:
1583 if self.rl_do_indent:
1584 self.readline.insert_text(self._indent_current_str())
1584 self.readline.insert_text(self._indent_current_str())
1585 if self.rl_next_input is not None:
1585 if self.rl_next_input is not None:
1586 self.readline.insert_text(self.rl_next_input)
1586 self.readline.insert_text(self.rl_next_input)
1587 self.rl_next_input = None
1587 self.rl_next_input = None
1588
1588
1589 def _indent_current_str(self):
1589 def _indent_current_str(self):
1590 """return the current level of indentation as a string"""
1590 """return the current level of indentation as a string"""
1591 return self.indent_current_nsp * ' '
1591 return self.indent_current_nsp * ' '
1592
1592
1593 #-------------------------------------------------------------------------
1593 #-------------------------------------------------------------------------
1594 # Things related to magics
1594 # Things related to magics
1595 #-------------------------------------------------------------------------
1595 #-------------------------------------------------------------------------
1596
1596
1597 def init_magics(self):
1597 def init_magics(self):
1598 # Set user colors (don't do it in the constructor above so that it
1598 # Set user colors (don't do it in the constructor above so that it
1599 # doesn't crash if colors option is invalid)
1599 # doesn't crash if colors option is invalid)
1600 self.magic_colors(self.colors)
1600 self.magic_colors(self.colors)
1601 # History was moved to a separate module
1601 # History was moved to a separate module
1602 from . import history
1602 from . import history
1603 history.init_ipython(self)
1603 history.init_ipython(self)
1604
1604
1605 def magic(self,arg_s):
1605 def magic(self,arg_s):
1606 """Call a magic function by name.
1606 """Call a magic function by name.
1607
1607
1608 Input: a string containing the name of the magic function to call and any
1608 Input: a string containing the name of the magic function to call and any
1609 additional arguments to be passed to the magic.
1609 additional arguments to be passed to the magic.
1610
1610
1611 magic('name -opt foo bar') is equivalent to typing at the ipython
1611 magic('name -opt foo bar') is equivalent to typing at the ipython
1612 prompt:
1612 prompt:
1613
1613
1614 In[1]: %name -opt foo bar
1614 In[1]: %name -opt foo bar
1615
1615
1616 To call a magic without arguments, simply use magic('name').
1616 To call a magic without arguments, simply use magic('name').
1617
1617
1618 This provides a proper Python function to call IPython's magics in any
1618 This provides a proper Python function to call IPython's magics in any
1619 valid Python code you can type at the interpreter, including loops and
1619 valid Python code you can type at the interpreter, including loops and
1620 compound statements.
1620 compound statements.
1621 """
1621 """
1622
1623 args = arg_s.split(' ',1)
1622 args = arg_s.split(' ',1)
1624 magic_name = args[0]
1623 magic_name = args[0]
1625 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1624 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1626
1625
1627 try:
1626 try:
1628 magic_args = args[1]
1627 magic_args = args[1]
1629 except IndexError:
1628 except IndexError:
1630 magic_args = ''
1629 magic_args = ''
1631 fn = getattr(self,'magic_'+magic_name,None)
1630 fn = getattr(self,'magic_'+magic_name,None)
1632 if fn is None:
1631 if fn is None:
1633 error("Magic function `%s` not found." % magic_name)
1632 error("Magic function `%s` not found." % magic_name)
1634 else:
1633 else:
1635 magic_args = self.var_expand(magic_args,1)
1634 magic_args = self.var_expand(magic_args,1)
1636 with nested(self.builtin_trap,):
1635 with nested(self.builtin_trap,):
1637 result = fn(magic_args)
1636 result = fn(magic_args)
1638 return result
1637 return result
1639
1638
1640 def define_magic(self, magicname, func):
1639 def define_magic(self, magicname, func):
1641 """Expose own function as magic function for ipython
1640 """Expose own function as magic function for ipython
1642
1641
1643 def foo_impl(self,parameter_s=''):
1642 def foo_impl(self,parameter_s=''):
1644 'My very own magic!. (Use docstrings, IPython reads them).'
1643 'My very own magic!. (Use docstrings, IPython reads them).'
1645 print 'Magic function. Passed parameter is between < >:'
1644 print 'Magic function. Passed parameter is between < >:'
1646 print '<%s>' % parameter_s
1645 print '<%s>' % parameter_s
1647 print 'The self object is:',self
1646 print 'The self object is:',self
1648
1647
1649 self.define_magic('foo',foo_impl)
1648 self.define_magic('foo',foo_impl)
1650 """
1649 """
1651
1650
1652 import new
1651 import new
1653 im = new.instancemethod(func,self, self.__class__)
1652 im = new.instancemethod(func,self, self.__class__)
1654 old = getattr(self, "magic_" + magicname, None)
1653 old = getattr(self, "magic_" + magicname, None)
1655 setattr(self, "magic_" + magicname, im)
1654 setattr(self, "magic_" + magicname, im)
1656 return old
1655 return old
1657
1656
1658 #-------------------------------------------------------------------------
1657 #-------------------------------------------------------------------------
1659 # Things related to macros
1658 # Things related to macros
1660 #-------------------------------------------------------------------------
1659 #-------------------------------------------------------------------------
1661
1660
1662 def define_macro(self, name, themacro):
1661 def define_macro(self, name, themacro):
1663 """Define a new macro
1662 """Define a new macro
1664
1663
1665 Parameters
1664 Parameters
1666 ----------
1665 ----------
1667 name : str
1666 name : str
1668 The name of the macro.
1667 The name of the macro.
1669 themacro : str or Macro
1668 themacro : str or Macro
1670 The action to do upon invoking the macro. If a string, a new
1669 The action to do upon invoking the macro. If a string, a new
1671 Macro object is created by passing the string to it.
1670 Macro object is created by passing the string to it.
1672 """
1671 """
1673
1672
1674 from IPython.core import macro
1673 from IPython.core import macro
1675
1674
1676 if isinstance(themacro, basestring):
1675 if isinstance(themacro, basestring):
1677 themacro = macro.Macro(themacro)
1676 themacro = macro.Macro(themacro)
1678 if not isinstance(themacro, macro.Macro):
1677 if not isinstance(themacro, macro.Macro):
1679 raise ValueError('A macro must be a string or a Macro instance.')
1678 raise ValueError('A macro must be a string or a Macro instance.')
1680 self.user_ns[name] = themacro
1679 self.user_ns[name] = themacro
1681
1680
1682 #-------------------------------------------------------------------------
1681 #-------------------------------------------------------------------------
1683 # Things related to the running of system commands
1682 # Things related to the running of system commands
1684 #-------------------------------------------------------------------------
1683 #-------------------------------------------------------------------------
1685
1684
1686 def system(self, cmd):
1685 def system(self, cmd):
1687 """Make a system call, using IPython."""
1686 """Make a system call, using IPython."""
1688 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1687 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1689
1688
1690 #-------------------------------------------------------------------------
1689 #-------------------------------------------------------------------------
1691 # Things related to aliases
1690 # Things related to aliases
1692 #-------------------------------------------------------------------------
1691 #-------------------------------------------------------------------------
1693
1692
1694 def init_alias(self):
1693 def init_alias(self):
1695 self.alias_manager = AliasManager(self, config=self.config)
1694 self.alias_manager = AliasManager(self, config=self.config)
1696 self.ns_table['alias'] = self.alias_manager.alias_table,
1695 self.ns_table['alias'] = self.alias_manager.alias_table,
1697
1696
1698 #-------------------------------------------------------------------------
1697 #-------------------------------------------------------------------------
1699 # Things related to the running of code
1698 # Things related to the running of code
1700 #-------------------------------------------------------------------------
1699 #-------------------------------------------------------------------------
1701
1700
1702 def ex(self, cmd):
1701 def ex(self, cmd):
1703 """Execute a normal python statement in user namespace."""
1702 """Execute a normal python statement in user namespace."""
1704 with nested(self.builtin_trap,):
1703 with nested(self.builtin_trap,):
1705 exec cmd in self.user_global_ns, self.user_ns
1704 exec cmd in self.user_global_ns, self.user_ns
1706
1705
1707 def ev(self, expr):
1706 def ev(self, expr):
1708 """Evaluate python expression expr in user namespace.
1707 """Evaluate python expression expr in user namespace.
1709
1708
1710 Returns the result of evaluation
1709 Returns the result of evaluation
1711 """
1710 """
1712 with nested(self.builtin_trap,):
1711 with nested(self.builtin_trap,):
1713 return eval(expr, self.user_global_ns, self.user_ns)
1712 return eval(expr, self.user_global_ns, self.user_ns)
1714
1713
1715 def mainloop(self, display_banner=None):
1714 def mainloop(self, display_banner=None):
1716 """Start the mainloop.
1715 """Start the mainloop.
1717
1716
1718 If an optional banner argument is given, it will override the
1717 If an optional banner argument is given, it will override the
1719 internally created default banner.
1718 internally created default banner.
1720 """
1719 """
1721
1720
1722 with nested(self.builtin_trap, self.display_trap):
1721 with nested(self.builtin_trap, self.display_trap):
1723
1722
1724 # if you run stuff with -c <cmd>, raw hist is not updated
1723 # if you run stuff with -c <cmd>, raw hist is not updated
1725 # ensure that it's in sync
1724 # ensure that it's in sync
1726 if len(self.input_hist) != len (self.input_hist_raw):
1725 if len(self.input_hist) != len (self.input_hist_raw):
1727 self.input_hist_raw = InputList(self.input_hist)
1726 self.input_hist_raw = InputList(self.input_hist)
1728
1727
1729 while 1:
1728 while 1:
1730 try:
1729 try:
1731 self.interact(display_banner=display_banner)
1730 self.interact(display_banner=display_banner)
1732 #self.interact_with_readline()
1731 #self.interact_with_readline()
1733 # XXX for testing of a readline-decoupled repl loop, call
1732 # XXX for testing of a readline-decoupled repl loop, call
1734 # interact_with_readline above
1733 # interact_with_readline above
1735 break
1734 break
1736 except KeyboardInterrupt:
1735 except KeyboardInterrupt:
1737 # this should not be necessary, but KeyboardInterrupt
1736 # this should not be necessary, but KeyboardInterrupt
1738 # handling seems rather unpredictable...
1737 # handling seems rather unpredictable...
1739 self.write("\nKeyboardInterrupt in interact()\n")
1738 self.write("\nKeyboardInterrupt in interact()\n")
1740
1739
1741 def interact_prompt(self):
1740 def interact_prompt(self):
1742 """ Print the prompt (in read-eval-print loop)
1741 """ Print the prompt (in read-eval-print loop)
1743
1742
1744 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1743 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1745 used in standard IPython flow.
1744 used in standard IPython flow.
1746 """
1745 """
1747 if self.more:
1746 if self.more:
1748 try:
1747 try:
1749 prompt = self.hooks.generate_prompt(True)
1748 prompt = self.hooks.generate_prompt(True)
1750 except:
1749 except:
1751 self.showtraceback()
1750 self.showtraceback()
1752 if self.autoindent:
1751 if self.autoindent:
1753 self.rl_do_indent = True
1752 self.rl_do_indent = True
1754
1753
1755 else:
1754 else:
1756 try:
1755 try:
1757 prompt = self.hooks.generate_prompt(False)
1756 prompt = self.hooks.generate_prompt(False)
1758 except:
1757 except:
1759 self.showtraceback()
1758 self.showtraceback()
1760 self.write(prompt)
1759 self.write(prompt)
1761
1760
1762 def interact_handle_input(self,line):
1761 def interact_handle_input(self,line):
1763 """ Handle the input line (in read-eval-print loop)
1762 """ Handle the input line (in read-eval-print loop)
1764
1763
1765 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1764 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1766 used in standard IPython flow.
1765 used in standard IPython flow.
1767 """
1766 """
1768 if line.lstrip() == line:
1767 if line.lstrip() == line:
1769 self.shadowhist.add(line.strip())
1768 self.shadowhist.add(line.strip())
1770 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1769 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1771
1770
1772 if line.strip():
1771 if line.strip():
1773 if self.more:
1772 if self.more:
1774 self.input_hist_raw[-1] += '%s\n' % line
1773 self.input_hist_raw[-1] += '%s\n' % line
1775 else:
1774 else:
1776 self.input_hist_raw.append('%s\n' % line)
1775 self.input_hist_raw.append('%s\n' % line)
1777
1776
1778
1777
1779 self.more = self.push_line(lineout)
1778 self.more = self.push_line(lineout)
1780 if (self.SyntaxTB.last_syntax_error and
1779 if (self.SyntaxTB.last_syntax_error and
1781 self.autoedit_syntax):
1780 self.autoedit_syntax):
1782 self.edit_syntax_error()
1781 self.edit_syntax_error()
1783
1782
1784 def interact_with_readline(self):
1783 def interact_with_readline(self):
1785 """ Demo of using interact_handle_input, interact_prompt
1784 """ Demo of using interact_handle_input, interact_prompt
1786
1785
1787 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1786 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1788 it should work like this.
1787 it should work like this.
1789 """
1788 """
1790 self.readline_startup_hook(self.pre_readline)
1789 self.readline_startup_hook(self.pre_readline)
1791 while not self.exit_now:
1790 while not self.exit_now:
1792 self.interact_prompt()
1791 self.interact_prompt()
1793 if self.more:
1792 if self.more:
1794 self.rl_do_indent = True
1793 self.rl_do_indent = True
1795 else:
1794 else:
1796 self.rl_do_indent = False
1795 self.rl_do_indent = False
1797 line = raw_input_original().decode(self.stdin_encoding)
1796 line = raw_input_original().decode(self.stdin_encoding)
1798 self.interact_handle_input(line)
1797 self.interact_handle_input(line)
1799
1798
1800 def interact(self, display_banner=None):
1799 def interact(self, display_banner=None):
1801 """Closely emulate the interactive Python console."""
1800 """Closely emulate the interactive Python console."""
1802
1801
1803 # batch run -> do not interact
1802 # batch run -> do not interact
1804 if self.exit_now:
1803 if self.exit_now:
1805 return
1804 return
1806
1805
1807 if display_banner is None:
1806 if display_banner is None:
1808 display_banner = self.display_banner
1807 display_banner = self.display_banner
1809 if display_banner:
1808 if display_banner:
1810 self.show_banner()
1809 self.show_banner()
1811
1810
1812 more = 0
1811 more = 0
1813
1812
1814 # Mark activity in the builtins
1813 # Mark activity in the builtins
1815 __builtin__.__dict__['__IPYTHON__active'] += 1
1814 __builtin__.__dict__['__IPYTHON__active'] += 1
1816
1815
1817 if self.has_readline:
1816 if self.has_readline:
1818 self.readline_startup_hook(self.pre_readline)
1817 self.readline_startup_hook(self.pre_readline)
1819 # exit_now is set by a call to %Exit or %Quit, through the
1818 # exit_now is set by a call to %Exit or %Quit, through the
1820 # ask_exit callback.
1819 # ask_exit callback.
1821
1820
1822 while not self.exit_now:
1821 while not self.exit_now:
1823 self.hooks.pre_prompt_hook()
1822 self.hooks.pre_prompt_hook()
1824 if more:
1823 if more:
1825 try:
1824 try:
1826 prompt = self.hooks.generate_prompt(True)
1825 prompt = self.hooks.generate_prompt(True)
1827 except:
1826 except:
1828 self.showtraceback()
1827 self.showtraceback()
1829 if self.autoindent:
1828 if self.autoindent:
1830 self.rl_do_indent = True
1829 self.rl_do_indent = True
1831
1830
1832 else:
1831 else:
1833 try:
1832 try:
1834 prompt = self.hooks.generate_prompt(False)
1833 prompt = self.hooks.generate_prompt(False)
1835 except:
1834 except:
1836 self.showtraceback()
1835 self.showtraceback()
1837 try:
1836 try:
1838 line = self.raw_input(prompt, more)
1837 line = self.raw_input(prompt, more)
1839 if self.exit_now:
1838 if self.exit_now:
1840 # quick exit on sys.std[in|out] close
1839 # quick exit on sys.std[in|out] close
1841 break
1840 break
1842 if self.autoindent:
1841 if self.autoindent:
1843 self.rl_do_indent = False
1842 self.rl_do_indent = False
1844
1843
1845 except KeyboardInterrupt:
1844 except KeyboardInterrupt:
1846 #double-guard against keyboardinterrupts during kbdint handling
1845 #double-guard against keyboardinterrupts during kbdint handling
1847 try:
1846 try:
1848 self.write('\nKeyboardInterrupt\n')
1847 self.write('\nKeyboardInterrupt\n')
1849 self.resetbuffer()
1848 self.resetbuffer()
1850 # keep cache in sync with the prompt counter:
1849 # keep cache in sync with the prompt counter:
1851 self.outputcache.prompt_count -= 1
1850 self.outputcache.prompt_count -= 1
1852
1851
1853 if self.autoindent:
1852 if self.autoindent:
1854 self.indent_current_nsp = 0
1853 self.indent_current_nsp = 0
1855 more = 0
1854 more = 0
1856 except KeyboardInterrupt:
1855 except KeyboardInterrupt:
1857 pass
1856 pass
1858 except EOFError:
1857 except EOFError:
1859 if self.autoindent:
1858 if self.autoindent:
1860 self.rl_do_indent = False
1859 self.rl_do_indent = False
1861 if self.has_readline:
1860 if self.has_readline:
1862 self.readline_startup_hook(None)
1861 self.readline_startup_hook(None)
1863 self.write('\n')
1862 self.write('\n')
1864 self.exit()
1863 self.exit()
1865 except bdb.BdbQuit:
1864 except bdb.BdbQuit:
1866 warn('The Python debugger has exited with a BdbQuit exception.\n'
1865 warn('The Python debugger has exited with a BdbQuit exception.\n'
1867 'Because of how pdb handles the stack, it is impossible\n'
1866 'Because of how pdb handles the stack, it is impossible\n'
1868 'for IPython to properly format this particular exception.\n'
1867 'for IPython to properly format this particular exception.\n'
1869 'IPython will resume normal operation.')
1868 'IPython will resume normal operation.')
1870 except:
1869 except:
1871 # exceptions here are VERY RARE, but they can be triggered
1870 # exceptions here are VERY RARE, but they can be triggered
1872 # asynchronously by signal handlers, for example.
1871 # asynchronously by signal handlers, for example.
1873 self.showtraceback()
1872 self.showtraceback()
1874 else:
1873 else:
1875 more = self.push_line(line)
1874 more = self.push_line(line)
1876 if (self.SyntaxTB.last_syntax_error and
1875 if (self.SyntaxTB.last_syntax_error and
1877 self.autoedit_syntax):
1876 self.autoedit_syntax):
1878 self.edit_syntax_error()
1877 self.edit_syntax_error()
1879
1878
1880 # We are off again...
1879 # We are off again...
1881 __builtin__.__dict__['__IPYTHON__active'] -= 1
1880 __builtin__.__dict__['__IPYTHON__active'] -= 1
1882
1881
1883 # Turn off the exit flag, so the mainloop can be restarted if desired
1882 # Turn off the exit flag, so the mainloop can be restarted if desired
1884 self.exit_now = False
1883 self.exit_now = False
1885
1884
1886 def safe_execfile(self, fname, *where, **kw):
1885 def safe_execfile(self, fname, *where, **kw):
1887 """A safe version of the builtin execfile().
1886 """A safe version of the builtin execfile().
1888
1887
1889 This version will never throw an exception, but instead print
1888 This version will never throw an exception, but instead print
1890 helpful error messages to the screen. This only works on pure
1889 helpful error messages to the screen. This only works on pure
1891 Python files with the .py extension.
1890 Python files with the .py extension.
1892
1891
1893 Parameters
1892 Parameters
1894 ----------
1893 ----------
1895 fname : string
1894 fname : string
1896 The name of the file to be executed.
1895 The name of the file to be executed.
1897 where : tuple
1896 where : tuple
1898 One or two namespaces, passed to execfile() as (globals,locals).
1897 One or two namespaces, passed to execfile() as (globals,locals).
1899 If only one is given, it is passed as both.
1898 If only one is given, it is passed as both.
1900 exit_ignore : bool (False)
1899 exit_ignore : bool (False)
1901 If True, then silence SystemExit for non-zero status (it is always
1900 If True, then silence SystemExit for non-zero status (it is always
1902 silenced for zero status, as it is so common).
1901 silenced for zero status, as it is so common).
1903 """
1902 """
1904 kw.setdefault('exit_ignore', False)
1903 kw.setdefault('exit_ignore', False)
1905
1904
1906 fname = os.path.abspath(os.path.expanduser(fname))
1905 fname = os.path.abspath(os.path.expanduser(fname))
1907
1906
1908 # Make sure we have a .py file
1907 # Make sure we have a .py file
1909 if not fname.endswith('.py'):
1908 if not fname.endswith('.py'):
1910 warn('File must end with .py to be run using execfile: <%s>' % fname)
1909 warn('File must end with .py to be run using execfile: <%s>' % fname)
1911
1910
1912 # Make sure we can open the file
1911 # Make sure we can open the file
1913 try:
1912 try:
1914 with open(fname) as thefile:
1913 with open(fname) as thefile:
1915 pass
1914 pass
1916 except:
1915 except:
1917 warn('Could not open file <%s> for safe execution.' % fname)
1916 warn('Could not open file <%s> for safe execution.' % fname)
1918 return
1917 return
1919
1918
1920 # Find things also in current directory. This is needed to mimic the
1919 # Find things also in current directory. This is needed to mimic the
1921 # behavior of running a script from the system command line, where
1920 # behavior of running a script from the system command line, where
1922 # Python inserts the script's directory into sys.path
1921 # Python inserts the script's directory into sys.path
1923 dname = os.path.dirname(fname)
1922 dname = os.path.dirname(fname)
1924
1923
1925 with prepended_to_syspath(dname):
1924 with prepended_to_syspath(dname):
1926 try:
1925 try:
1927 execfile(fname,*where)
1926 execfile(fname,*where)
1928 except SystemExit, status:
1927 except SystemExit, status:
1929 # If the call was made with 0 or None exit status (sys.exit(0)
1928 # If the call was made with 0 or None exit status (sys.exit(0)
1930 # or sys.exit() ), don't bother showing a traceback, as both of
1929 # or sys.exit() ), don't bother showing a traceback, as both of
1931 # these are considered normal by the OS:
1930 # these are considered normal by the OS:
1932 # > python -c'import sys;sys.exit(0)'; echo $?
1931 # > python -c'import sys;sys.exit(0)'; echo $?
1933 # 0
1932 # 0
1934 # > python -c'import sys;sys.exit()'; echo $?
1933 # > python -c'import sys;sys.exit()'; echo $?
1935 # 0
1934 # 0
1936 # For other exit status, we show the exception unless
1935 # For other exit status, we show the exception unless
1937 # explicitly silenced, but only in short form.
1936 # explicitly silenced, but only in short form.
1938 if status.code not in (0, None) and not kw['exit_ignore']:
1937 if status.code not in (0, None) and not kw['exit_ignore']:
1939 self.showtraceback(exception_only=True)
1938 self.showtraceback(exception_only=True)
1940 except:
1939 except:
1941 self.showtraceback()
1940 self.showtraceback()
1942
1941
1943 def safe_execfile_ipy(self, fname):
1942 def safe_execfile_ipy(self, fname):
1944 """Like safe_execfile, but for .ipy files with IPython syntax.
1943 """Like safe_execfile, but for .ipy files with IPython syntax.
1945
1944
1946 Parameters
1945 Parameters
1947 ----------
1946 ----------
1948 fname : str
1947 fname : str
1949 The name of the file to execute. The filename must have a
1948 The name of the file to execute. The filename must have a
1950 .ipy extension.
1949 .ipy extension.
1951 """
1950 """
1952 fname = os.path.abspath(os.path.expanduser(fname))
1951 fname = os.path.abspath(os.path.expanduser(fname))
1953
1952
1954 # Make sure we have a .py file
1953 # Make sure we have a .py file
1955 if not fname.endswith('.ipy'):
1954 if not fname.endswith('.ipy'):
1956 warn('File must end with .py to be run using execfile: <%s>' % fname)
1955 warn('File must end with .py to be run using execfile: <%s>' % fname)
1957
1956
1958 # Make sure we can open the file
1957 # Make sure we can open the file
1959 try:
1958 try:
1960 with open(fname) as thefile:
1959 with open(fname) as thefile:
1961 pass
1960 pass
1962 except:
1961 except:
1963 warn('Could not open file <%s> for safe execution.' % fname)
1962 warn('Could not open file <%s> for safe execution.' % fname)
1964 return
1963 return
1965
1964
1966 # Find things also in current directory. This is needed to mimic the
1965 # Find things also in current directory. This is needed to mimic the
1967 # behavior of running a script from the system command line, where
1966 # behavior of running a script from the system command line, where
1968 # Python inserts the script's directory into sys.path
1967 # Python inserts the script's directory into sys.path
1969 dname = os.path.dirname(fname)
1968 dname = os.path.dirname(fname)
1970
1969
1971 with prepended_to_syspath(dname):
1970 with prepended_to_syspath(dname):
1972 try:
1971 try:
1973 with open(fname) as thefile:
1972 with open(fname) as thefile:
1974 script = thefile.read()
1973 script = thefile.read()
1975 # self.runlines currently captures all exceptions
1974 # self.runlines currently captures all exceptions
1976 # raise in user code. It would be nice if there were
1975 # raise in user code. It would be nice if there were
1977 # versions of runlines, execfile that did raise, so
1976 # versions of runlines, execfile that did raise, so
1978 # we could catch the errors.
1977 # we could catch the errors.
1979 self.runlines(script, clean=True)
1978 self.runlines(script, clean=True)
1980 except:
1979 except:
1981 self.showtraceback()
1980 self.showtraceback()
1982 warn('Unknown failure executing file: <%s>' % fname)
1981 warn('Unknown failure executing file: <%s>' % fname)
1983
1982
1984 def _is_secondary_block_start(self, s):
1983 def _is_secondary_block_start(self, s):
1985 if not s.endswith(':'):
1984 if not s.endswith(':'):
1986 return False
1985 return False
1987 if (s.startswith('elif') or
1986 if (s.startswith('elif') or
1988 s.startswith('else') or
1987 s.startswith('else') or
1989 s.startswith('except') or
1988 s.startswith('except') or
1990 s.startswith('finally')):
1989 s.startswith('finally')):
1991 return True
1990 return True
1992
1991
1993 def cleanup_ipy_script(self, script):
1992 def cleanup_ipy_script(self, script):
1994 """Make a script safe for self.runlines()
1993 """Make a script safe for self.runlines()
1995
1994
1996 Currently, IPython is lines based, with blocks being detected by
1995 Currently, IPython is lines based, with blocks being detected by
1997 empty lines. This is a problem for block based scripts that may
1996 empty lines. This is a problem for block based scripts that may
1998 not have empty lines after blocks. This script adds those empty
1997 not have empty lines after blocks. This script adds those empty
1999 lines to make scripts safe for running in the current line based
1998 lines to make scripts safe for running in the current line based
2000 IPython.
1999 IPython.
2001 """
2000 """
2002 res = []
2001 res = []
2003 lines = script.splitlines()
2002 lines = script.splitlines()
2004 level = 0
2003 level = 0
2005
2004
2006 for l in lines:
2005 for l in lines:
2007 lstripped = l.lstrip()
2006 lstripped = l.lstrip()
2008 stripped = l.strip()
2007 stripped = l.strip()
2009 if not stripped:
2008 if not stripped:
2010 continue
2009 continue
2011 newlevel = len(l) - len(lstripped)
2010 newlevel = len(l) - len(lstripped)
2012 if level > 0 and newlevel == 0 and \
2011 if level > 0 and newlevel == 0 and \
2013 not self._is_secondary_block_start(stripped):
2012 not self._is_secondary_block_start(stripped):
2014 # add empty line
2013 # add empty line
2015 res.append('')
2014 res.append('')
2016 res.append(l)
2015 res.append(l)
2017 level = newlevel
2016 level = newlevel
2018
2017
2019 return '\n'.join(res) + '\n'
2018 return '\n'.join(res) + '\n'
2020
2019
2021 def runlines(self, lines, clean=False):
2020 def runlines(self, lines, clean=False):
2022 """Run a string of one or more lines of source.
2021 """Run a string of one or more lines of source.
2023
2022
2024 This method is capable of running a string containing multiple source
2023 This method is capable of running a string containing multiple source
2025 lines, as if they had been entered at the IPython prompt. Since it
2024 lines, as if they had been entered at the IPython prompt. Since it
2026 exposes IPython's processing machinery, the given strings can contain
2025 exposes IPython's processing machinery, the given strings can contain
2027 magic calls (%magic), special shell access (!cmd), etc.
2026 magic calls (%magic), special shell access (!cmd), etc.
2028 """
2027 """
2029
2028
2030 if isinstance(lines, (list, tuple)):
2029 if isinstance(lines, (list, tuple)):
2031 lines = '\n'.join(lines)
2030 lines = '\n'.join(lines)
2032
2031
2033 if clean:
2032 if clean:
2034 lines = self.cleanup_ipy_script(lines)
2033 lines = self.cleanup_ipy_script(lines)
2035
2034
2036 # We must start with a clean buffer, in case this is run from an
2035 # We must start with a clean buffer, in case this is run from an
2037 # interactive IPython session (via a magic, for example).
2036 # interactive IPython session (via a magic, for example).
2038 self.resetbuffer()
2037 self.resetbuffer()
2039 lines = lines.splitlines()
2038 lines = lines.splitlines()
2040 more = 0
2039 more = 0
2041
2040
2042 with nested(self.builtin_trap, self.display_trap):
2041 with nested(self.builtin_trap, self.display_trap):
2043 for line in lines:
2042 for line in lines:
2044 # skip blank lines so we don't mess up the prompt counter, but do
2043 # skip blank lines so we don't mess up the prompt counter, but do
2045 # NOT skip even a blank line if we are in a code block (more is
2044 # NOT skip even a blank line if we are in a code block (more is
2046 # true)
2045 # true)
2047
2046
2048 if line or more:
2047 if line or more:
2049 # push to raw history, so hist line numbers stay in sync
2048 # push to raw history, so hist line numbers stay in sync
2050 self.input_hist_raw.append("# " + line + "\n")
2049 self.input_hist_raw.append("# " + line + "\n")
2051 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2050 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2052 more = self.push_line(prefiltered)
2051 more = self.push_line(prefiltered)
2053 # IPython's runsource returns None if there was an error
2052 # IPython's runsource returns None if there was an error
2054 # compiling the code. This allows us to stop processing right
2053 # compiling the code. This allows us to stop processing right
2055 # away, so the user gets the error message at the right place.
2054 # away, so the user gets the error message at the right place.
2056 if more is None:
2055 if more is None:
2057 break
2056 break
2058 else:
2057 else:
2059 self.input_hist_raw.append("\n")
2058 self.input_hist_raw.append("\n")
2060 # final newline in case the input didn't have it, so that the code
2059 # final newline in case the input didn't have it, so that the code
2061 # actually does get executed
2060 # actually does get executed
2062 if more:
2061 if more:
2063 self.push_line('\n')
2062 self.push_line('\n')
2064
2063
2065 def runsource(self, source, filename='<input>', symbol='single'):
2064 def runsource(self, source, filename='<input>', symbol='single'):
2066 """Compile and run some source in the interpreter.
2065 """Compile and run some source in the interpreter.
2067
2066
2068 Arguments are as for compile_command().
2067 Arguments are as for compile_command().
2069
2068
2070 One several things can happen:
2069 One several things can happen:
2071
2070
2072 1) The input is incorrect; compile_command() raised an
2071 1) The input is incorrect; compile_command() raised an
2073 exception (SyntaxError or OverflowError). A syntax traceback
2072 exception (SyntaxError or OverflowError). A syntax traceback
2074 will be printed by calling the showsyntaxerror() method.
2073 will be printed by calling the showsyntaxerror() method.
2075
2074
2076 2) The input is incomplete, and more input is required;
2075 2) The input is incomplete, and more input is required;
2077 compile_command() returned None. Nothing happens.
2076 compile_command() returned None. Nothing happens.
2078
2077
2079 3) The input is complete; compile_command() returned a code
2078 3) The input is complete; compile_command() returned a code
2080 object. The code is executed by calling self.runcode() (which
2079 object. The code is executed by calling self.runcode() (which
2081 also handles run-time exceptions, except for SystemExit).
2080 also handles run-time exceptions, except for SystemExit).
2082
2081
2083 The return value is:
2082 The return value is:
2084
2083
2085 - True in case 2
2084 - True in case 2
2086
2085
2087 - False in the other cases, unless an exception is raised, where
2086 - False in the other cases, unless an exception is raised, where
2088 None is returned instead. This can be used by external callers to
2087 None is returned instead. This can be used by external callers to
2089 know whether to continue feeding input or not.
2088 know whether to continue feeding input or not.
2090
2089
2091 The return value can be used to decide whether to use sys.ps1 or
2090 The return value can be used to decide whether to use sys.ps1 or
2092 sys.ps2 to prompt the next line."""
2091 sys.ps2 to prompt the next line."""
2093
2092
2094 # if the source code has leading blanks, add 'if 1:\n' to it
2093 # if the source code has leading blanks, add 'if 1:\n' to it
2095 # this allows execution of indented pasted code. It is tempting
2094 # this allows execution of indented pasted code. It is tempting
2096 # to add '\n' at the end of source to run commands like ' a=1'
2095 # to add '\n' at the end of source to run commands like ' a=1'
2097 # directly, but this fails for more complicated scenarios
2096 # directly, but this fails for more complicated scenarios
2098 source=source.encode(self.stdin_encoding)
2097 source=source.encode(self.stdin_encoding)
2099 if source[:1] in [' ', '\t']:
2098 if source[:1] in [' ', '\t']:
2100 source = 'if 1:\n%s' % source
2099 source = 'if 1:\n%s' % source
2101
2100
2102 try:
2101 try:
2103 code = self.compile(source,filename,symbol)
2102 code = self.compile(source,filename,symbol)
2104 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2103 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2105 # Case 1
2104 # Case 1
2106 self.showsyntaxerror(filename)
2105 self.showsyntaxerror(filename)
2107 return None
2106 return None
2108
2107
2109 if code is None:
2108 if code is None:
2110 # Case 2
2109 # Case 2
2111 return True
2110 return True
2112
2111
2113 # Case 3
2112 # Case 3
2114 # We store the code object so that threaded shells and
2113 # We store the code object so that threaded shells and
2115 # custom exception handlers can access all this info if needed.
2114 # custom exception handlers can access all this info if needed.
2116 # The source corresponding to this can be obtained from the
2115 # The source corresponding to this can be obtained from the
2117 # buffer attribute as '\n'.join(self.buffer).
2116 # buffer attribute as '\n'.join(self.buffer).
2118 self.code_to_run = code
2117 self.code_to_run = code
2119 # now actually execute the code object
2118 # now actually execute the code object
2120 if self.runcode(code) == 0:
2119 if self.runcode(code) == 0:
2121 return False
2120 return False
2122 else:
2121 else:
2123 return None
2122 return None
2124
2123
2125 def runcode(self,code_obj):
2124 def runcode(self,code_obj):
2126 """Execute a code object.
2125 """Execute a code object.
2127
2126
2128 When an exception occurs, self.showtraceback() is called to display a
2127 When an exception occurs, self.showtraceback() is called to display a
2129 traceback.
2128 traceback.
2130
2129
2131 Return value: a flag indicating whether the code to be run completed
2130 Return value: a flag indicating whether the code to be run completed
2132 successfully:
2131 successfully:
2133
2132
2134 - 0: successful execution.
2133 - 0: successful execution.
2135 - 1: an error occurred.
2134 - 1: an error occurred.
2136 """
2135 """
2137
2136
2138 # Set our own excepthook in case the user code tries to call it
2137 # Set our own excepthook in case the user code tries to call it
2139 # directly, so that the IPython crash handler doesn't get triggered
2138 # directly, so that the IPython crash handler doesn't get triggered
2140 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2139 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2141
2140
2142 # we save the original sys.excepthook in the instance, in case config
2141 # we save the original sys.excepthook in the instance, in case config
2143 # code (such as magics) needs access to it.
2142 # code (such as magics) needs access to it.
2144 self.sys_excepthook = old_excepthook
2143 self.sys_excepthook = old_excepthook
2145 outflag = 1 # happens in more places, so it's easier as default
2144 outflag = 1 # happens in more places, so it's easier as default
2146 try:
2145 try:
2147 try:
2146 try:
2148 self.hooks.pre_runcode_hook()
2147 self.hooks.pre_runcode_hook()
2149 exec code_obj in self.user_global_ns, self.user_ns
2148 exec code_obj in self.user_global_ns, self.user_ns
2150 finally:
2149 finally:
2151 # Reset our crash handler in place
2150 # Reset our crash handler in place
2152 sys.excepthook = old_excepthook
2151 sys.excepthook = old_excepthook
2153 except SystemExit:
2152 except SystemExit:
2154 self.resetbuffer()
2153 self.resetbuffer()
2155 self.showtraceback(exception_only=True)
2154 self.showtraceback(exception_only=True)
2156 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2155 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2157 except self.custom_exceptions:
2156 except self.custom_exceptions:
2158 etype,value,tb = sys.exc_info()
2157 etype,value,tb = sys.exc_info()
2159 self.CustomTB(etype,value,tb)
2158 self.CustomTB(etype,value,tb)
2160 except:
2159 except:
2161 self.showtraceback()
2160 self.showtraceback()
2162 else:
2161 else:
2163 outflag = 0
2162 outflag = 0
2164 if softspace(sys.stdout, 0):
2163 if softspace(sys.stdout, 0):
2165 print
2164 print
2166 # Flush out code object which has been run (and source)
2165 # Flush out code object which has been run (and source)
2167 self.code_to_run = None
2166 self.code_to_run = None
2168 return outflag
2167 return outflag
2169
2168
2170 def push_line(self, line):
2169 def push_line(self, line):
2171 """Push a line to the interpreter.
2170 """Push a line to the interpreter.
2172
2171
2173 The line should not have a trailing newline; it may have
2172 The line should not have a trailing newline; it may have
2174 internal newlines. The line is appended to a buffer and the
2173 internal newlines. The line is appended to a buffer and the
2175 interpreter's runsource() method is called with the
2174 interpreter's runsource() method is called with the
2176 concatenated contents of the buffer as source. If this
2175 concatenated contents of the buffer as source. If this
2177 indicates that the command was executed or invalid, the buffer
2176 indicates that the command was executed or invalid, the buffer
2178 is reset; otherwise, the command is incomplete, and the buffer
2177 is reset; otherwise, the command is incomplete, and the buffer
2179 is left as it was after the line was appended. The return
2178 is left as it was after the line was appended. The return
2180 value is 1 if more input is required, 0 if the line was dealt
2179 value is 1 if more input is required, 0 if the line was dealt
2181 with in some way (this is the same as runsource()).
2180 with in some way (this is the same as runsource()).
2182 """
2181 """
2183
2182
2184 # autoindent management should be done here, and not in the
2183 # autoindent management should be done here, and not in the
2185 # interactive loop, since that one is only seen by keyboard input. We
2184 # interactive loop, since that one is only seen by keyboard input. We
2186 # need this done correctly even for code run via runlines (which uses
2185 # need this done correctly even for code run via runlines (which uses
2187 # push).
2186 # push).
2188
2187
2189 #print 'push line: <%s>' % line # dbg
2188 #print 'push line: <%s>' % line # dbg
2190 for subline in line.splitlines():
2189 for subline in line.splitlines():
2191 self._autoindent_update(subline)
2190 self._autoindent_update(subline)
2192 self.buffer.append(line)
2191 self.buffer.append(line)
2193 more = self.runsource('\n'.join(self.buffer), self.filename)
2192 more = self.runsource('\n'.join(self.buffer), self.filename)
2194 if not more:
2193 if not more:
2195 self.resetbuffer()
2194 self.resetbuffer()
2196 return more
2195 return more
2197
2196
2198 def _autoindent_update(self,line):
2197 def _autoindent_update(self,line):
2199 """Keep track of the indent level."""
2198 """Keep track of the indent level."""
2200
2199
2201 #debugx('line')
2200 #debugx('line')
2202 #debugx('self.indent_current_nsp')
2201 #debugx('self.indent_current_nsp')
2203 if self.autoindent:
2202 if self.autoindent:
2204 if line:
2203 if line:
2205 inisp = num_ini_spaces(line)
2204 inisp = num_ini_spaces(line)
2206 if inisp < self.indent_current_nsp:
2205 if inisp < self.indent_current_nsp:
2207 self.indent_current_nsp = inisp
2206 self.indent_current_nsp = inisp
2208
2207
2209 if line[-1] == ':':
2208 if line[-1] == ':':
2210 self.indent_current_nsp += 4
2209 self.indent_current_nsp += 4
2211 elif dedent_re.match(line):
2210 elif dedent_re.match(line):
2212 self.indent_current_nsp -= 4
2211 self.indent_current_nsp -= 4
2213 else:
2212 else:
2214 self.indent_current_nsp = 0
2213 self.indent_current_nsp = 0
2215
2214
2216 def resetbuffer(self):
2215 def resetbuffer(self):
2217 """Reset the input buffer."""
2216 """Reset the input buffer."""
2218 self.buffer[:] = []
2217 self.buffer[:] = []
2219
2218
2220 def raw_input(self,prompt='',continue_prompt=False):
2219 def raw_input(self,prompt='',continue_prompt=False):
2221 """Write a prompt and read a line.
2220 """Write a prompt and read a line.
2222
2221
2223 The returned line does not include the trailing newline.
2222 The returned line does not include the trailing newline.
2224 When the user enters the EOF key sequence, EOFError is raised.
2223 When the user enters the EOF key sequence, EOFError is raised.
2225
2224
2226 Optional inputs:
2225 Optional inputs:
2227
2226
2228 - prompt(''): a string to be printed to prompt the user.
2227 - prompt(''): a string to be printed to prompt the user.
2229
2228
2230 - continue_prompt(False): whether this line is the first one or a
2229 - continue_prompt(False): whether this line is the first one or a
2231 continuation in a sequence of inputs.
2230 continuation in a sequence of inputs.
2232 """
2231 """
2233 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2232 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2234
2233
2235 # Code run by the user may have modified the readline completer state.
2234 # Code run by the user may have modified the readline completer state.
2236 # We must ensure that our completer is back in place.
2235 # We must ensure that our completer is back in place.
2237
2236
2238 if self.has_readline:
2237 if self.has_readline:
2239 self.set_completer()
2238 self.set_completer()
2240
2239
2241 try:
2240 try:
2242 line = raw_input_original(prompt).decode(self.stdin_encoding)
2241 line = raw_input_original(prompt).decode(self.stdin_encoding)
2243 except ValueError:
2242 except ValueError:
2244 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2243 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2245 " or sys.stdout.close()!\nExiting IPython!")
2244 " or sys.stdout.close()!\nExiting IPython!")
2246 self.ask_exit()
2245 self.ask_exit()
2247 return ""
2246 return ""
2248
2247
2249 # Try to be reasonably smart about not re-indenting pasted input more
2248 # Try to be reasonably smart about not re-indenting pasted input more
2250 # than necessary. We do this by trimming out the auto-indent initial
2249 # than necessary. We do this by trimming out the auto-indent initial
2251 # spaces, if the user's actual input started itself with whitespace.
2250 # spaces, if the user's actual input started itself with whitespace.
2252 #debugx('self.buffer[-1]')
2251 #debugx('self.buffer[-1]')
2253
2252
2254 if self.autoindent:
2253 if self.autoindent:
2255 if num_ini_spaces(line) > self.indent_current_nsp:
2254 if num_ini_spaces(line) > self.indent_current_nsp:
2256 line = line[self.indent_current_nsp:]
2255 line = line[self.indent_current_nsp:]
2257 self.indent_current_nsp = 0
2256 self.indent_current_nsp = 0
2258
2257
2259 # store the unfiltered input before the user has any chance to modify
2258 # store the unfiltered input before the user has any chance to modify
2260 # it.
2259 # it.
2261 if line.strip():
2260 if line.strip():
2262 if continue_prompt:
2261 if continue_prompt:
2263 self.input_hist_raw[-1] += '%s\n' % line
2262 self.input_hist_raw[-1] += '%s\n' % line
2264 if self.has_readline and self.readline_use:
2263 if self.has_readline and self.readline_use:
2265 try:
2264 try:
2266 histlen = self.readline.get_current_history_length()
2265 histlen = self.readline.get_current_history_length()
2267 if histlen > 1:
2266 if histlen > 1:
2268 newhist = self.input_hist_raw[-1].rstrip()
2267 newhist = self.input_hist_raw[-1].rstrip()
2269 self.readline.remove_history_item(histlen-1)
2268 self.readline.remove_history_item(histlen-1)
2270 self.readline.replace_history_item(histlen-2,
2269 self.readline.replace_history_item(histlen-2,
2271 newhist.encode(self.stdin_encoding))
2270 newhist.encode(self.stdin_encoding))
2272 except AttributeError:
2271 except AttributeError:
2273 pass # re{move,place}_history_item are new in 2.4.
2272 pass # re{move,place}_history_item are new in 2.4.
2274 else:
2273 else:
2275 self.input_hist_raw.append('%s\n' % line)
2274 self.input_hist_raw.append('%s\n' % line)
2276 # only entries starting at first column go to shadow history
2275 # only entries starting at first column go to shadow history
2277 if line.lstrip() == line:
2276 if line.lstrip() == line:
2278 self.shadowhist.add(line.strip())
2277 self.shadowhist.add(line.strip())
2279 elif not continue_prompt:
2278 elif not continue_prompt:
2280 self.input_hist_raw.append('\n')
2279 self.input_hist_raw.append('\n')
2281 try:
2280 try:
2282 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2281 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2283 except:
2282 except:
2284 # blanket except, in case a user-defined prefilter crashes, so it
2283 # blanket except, in case a user-defined prefilter crashes, so it
2285 # can't take all of ipython with it.
2284 # can't take all of ipython with it.
2286 self.showtraceback()
2285 self.showtraceback()
2287 return ''
2286 return ''
2288 else:
2287 else:
2289 return lineout
2288 return lineout
2290
2289
2291 #-------------------------------------------------------------------------
2290 #-------------------------------------------------------------------------
2292 # Working with components
2291 # Working with components
2293 #-------------------------------------------------------------------------
2292 #-------------------------------------------------------------------------
2294
2293
2295 def get_component(self, name=None, klass=None):
2294 def get_component(self, name=None, klass=None):
2296 """Fetch a component by name and klass in my tree."""
2295 """Fetch a component by name and klass in my tree."""
2297 c = Component.get_instances(root=self, name=name, klass=klass)
2296 c = Component.get_instances(root=self, name=name, klass=klass)
2298 if len(c) == 0:
2297 if len(c) == 0:
2299 return None
2298 return None
2300 if len(c) == 1:
2299 if len(c) == 1:
2301 return c[0]
2300 return c[0]
2302 else:
2301 else:
2303 return c
2302 return c
2304
2303
2305 #-------------------------------------------------------------------------
2304 #-------------------------------------------------------------------------
2306 # IPython extensions
2305 # IPython extensions
2307 #-------------------------------------------------------------------------
2306 #-------------------------------------------------------------------------
2308
2307
2309 def load_extension(self, module_str):
2308 def load_extension(self, module_str):
2310 """Load an IPython extension by its module name.
2309 """Load an IPython extension by its module name.
2311
2310
2312 An IPython extension is an importable Python module that has
2311 An IPython extension is an importable Python module that has
2313 a function with the signature::
2312 a function with the signature::
2314
2313
2315 def load_ipython_extension(ipython):
2314 def load_ipython_extension(ipython):
2316 # Do things with ipython
2315 # Do things with ipython
2317
2316
2318 This function is called after your extension is imported and the
2317 This function is called after your extension is imported and the
2319 currently active :class:`InteractiveShell` instance is passed as
2318 currently active :class:`InteractiveShell` instance is passed as
2320 the only argument. You can do anything you want with IPython at
2319 the only argument. You can do anything you want with IPython at
2321 that point, including defining new magic and aliases, adding new
2320 that point, including defining new magic and aliases, adding new
2322 components, etc.
2321 components, etc.
2323
2322
2324 The :func:`load_ipython_extension` will be called again is you
2323 The :func:`load_ipython_extension` will be called again is you
2325 load or reload the extension again. It is up to the extension
2324 load or reload the extension again. It is up to the extension
2326 author to add code to manage that.
2325 author to add code to manage that.
2327
2326
2328 You can put your extension modules anywhere you want, as long as
2327 You can put your extension modules anywhere you want, as long as
2329 they can be imported by Python's standard import mechanism. However,
2328 they can be imported by Python's standard import mechanism. However,
2330 to make it easy to write extensions, you can also put your extensions
2329 to make it easy to write extensions, you can also put your extensions
2331 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2330 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2332 is added to ``sys.path`` automatically.
2331 is added to ``sys.path`` automatically.
2333 """
2332 """
2334 from IPython.utils.syspathcontext import prepended_to_syspath
2333 from IPython.utils.syspathcontext import prepended_to_syspath
2335
2334
2336 if module_str not in sys.modules:
2335 if module_str not in sys.modules:
2337 with prepended_to_syspath(self.ipython_extension_dir):
2336 with prepended_to_syspath(self.ipython_extension_dir):
2338 __import__(module_str)
2337 __import__(module_str)
2339 mod = sys.modules[module_str]
2338 mod = sys.modules[module_str]
2340 return self._call_load_ipython_extension(mod)
2339 return self._call_load_ipython_extension(mod)
2341
2340
2342 def unload_extension(self, module_str):
2341 def unload_extension(self, module_str):
2343 """Unload an IPython extension by its module name.
2342 """Unload an IPython extension by its module name.
2344
2343
2345 This function looks up the extension's name in ``sys.modules`` and
2344 This function looks up the extension's name in ``sys.modules`` and
2346 simply calls ``mod.unload_ipython_extension(self)``.
2345 simply calls ``mod.unload_ipython_extension(self)``.
2347 """
2346 """
2348 if module_str in sys.modules:
2347 if module_str in sys.modules:
2349 mod = sys.modules[module_str]
2348 mod = sys.modules[module_str]
2350 self._call_unload_ipython_extension(mod)
2349 self._call_unload_ipython_extension(mod)
2351
2350
2352 def reload_extension(self, module_str):
2351 def reload_extension(self, module_str):
2353 """Reload an IPython extension by calling reload.
2352 """Reload an IPython extension by calling reload.
2354
2353
2355 If the module has not been loaded before,
2354 If the module has not been loaded before,
2356 :meth:`InteractiveShell.load_extension` is called. Otherwise
2355 :meth:`InteractiveShell.load_extension` is called. Otherwise
2357 :func:`reload` is called and then the :func:`load_ipython_extension`
2356 :func:`reload` is called and then the :func:`load_ipython_extension`
2358 function of the module, if it exists is called.
2357 function of the module, if it exists is called.
2359 """
2358 """
2360 from IPython.utils.syspathcontext import prepended_to_syspath
2359 from IPython.utils.syspathcontext import prepended_to_syspath
2361
2360
2362 with prepended_to_syspath(self.ipython_extension_dir):
2361 with prepended_to_syspath(self.ipython_extension_dir):
2363 if module_str in sys.modules:
2362 if module_str in sys.modules:
2364 mod = sys.modules[module_str]
2363 mod = sys.modules[module_str]
2365 reload(mod)
2364 reload(mod)
2366 self._call_load_ipython_extension(mod)
2365 self._call_load_ipython_extension(mod)
2367 else:
2366 else:
2368 self.load_extension(module_str)
2367 self.load_extension(module_str)
2369
2368
2370 def _call_load_ipython_extension(self, mod):
2369 def _call_load_ipython_extension(self, mod):
2371 if hasattr(mod, 'load_ipython_extension'):
2370 if hasattr(mod, 'load_ipython_extension'):
2372 return mod.load_ipython_extension(self)
2371 return mod.load_ipython_extension(self)
2373
2372
2374 def _call_unload_ipython_extension(self, mod):
2373 def _call_unload_ipython_extension(self, mod):
2375 if hasattr(mod, 'unload_ipython_extension'):
2374 if hasattr(mod, 'unload_ipython_extension'):
2376 return mod.unload_ipython_extension(self)
2375 return mod.unload_ipython_extension(self)
2377
2376
2378 #-------------------------------------------------------------------------
2377 #-------------------------------------------------------------------------
2379 # Things related to the prefilter
2378 # Things related to the prefilter
2380 #-------------------------------------------------------------------------
2379 #-------------------------------------------------------------------------
2381
2380
2382 def init_prefilter(self):
2381 def init_prefilter(self):
2383 self.prefilter_manager = PrefilterManager(self, config=self.config)
2382 self.prefilter_manager = PrefilterManager(self, config=self.config)
2384 # Ultimately this will be refactored in the new interpreter code, but
2383 # Ultimately this will be refactored in the new interpreter code, but
2385 # for now, we should expose the main prefilter method (there's legacy
2384 # for now, we should expose the main prefilter method (there's legacy
2386 # code out there that may rely on this).
2385 # code out there that may rely on this).
2387 self.prefilter = self.prefilter_manager.prefilter_lines
2386 self.prefilter = self.prefilter_manager.prefilter_lines
2388
2387
2389 #-------------------------------------------------------------------------
2388 #-------------------------------------------------------------------------
2390 # Utilities
2389 # Utilities
2391 #-------------------------------------------------------------------------
2390 #-------------------------------------------------------------------------
2392
2391
2393 def getoutput(self, cmd):
2392 def getoutput(self, cmd):
2394 return getoutput(self.var_expand(cmd,depth=2),
2393 return getoutput(self.var_expand(cmd,depth=2),
2395 header=self.system_header,
2394 header=self.system_header,
2396 verbose=self.system_verbose)
2395 verbose=self.system_verbose)
2397
2396
2398 def getoutputerror(self, cmd):
2397 def getoutputerror(self, cmd):
2399 return getoutputerror(self.var_expand(cmd,depth=2),
2398 return getoutputerror(self.var_expand(cmd,depth=2),
2400 header=self.system_header,
2399 header=self.system_header,
2401 verbose=self.system_verbose)
2400 verbose=self.system_verbose)
2402
2401
2403 def var_expand(self,cmd,depth=0):
2402 def var_expand(self,cmd,depth=0):
2404 """Expand python variables in a string.
2403 """Expand python variables in a string.
2405
2404
2406 The depth argument indicates how many frames above the caller should
2405 The depth argument indicates how many frames above the caller should
2407 be walked to look for the local namespace where to expand variables.
2406 be walked to look for the local namespace where to expand variables.
2408
2407
2409 The global namespace for expansion is always the user's interactive
2408 The global namespace for expansion is always the user's interactive
2410 namespace.
2409 namespace.
2411 """
2410 """
2412
2411
2413 return str(ItplNS(cmd,
2412 return str(ItplNS(cmd,
2414 self.user_ns, # globals
2413 self.user_ns, # globals
2415 # Skip our own frame in searching for locals:
2414 # Skip our own frame in searching for locals:
2416 sys._getframe(depth+1).f_locals # locals
2415 sys._getframe(depth+1).f_locals # locals
2417 ))
2416 ))
2418
2417
2419 def mktempfile(self,data=None):
2418 def mktempfile(self,data=None):
2420 """Make a new tempfile and return its filename.
2419 """Make a new tempfile and return its filename.
2421
2420
2422 This makes a call to tempfile.mktemp, but it registers the created
2421 This makes a call to tempfile.mktemp, but it registers the created
2423 filename internally so ipython cleans it up at exit time.
2422 filename internally so ipython cleans it up at exit time.
2424
2423
2425 Optional inputs:
2424 Optional inputs:
2426
2425
2427 - data(None): if data is given, it gets written out to the temp file
2426 - data(None): if data is given, it gets written out to the temp file
2428 immediately, and the file is closed again."""
2427 immediately, and the file is closed again."""
2429
2428
2430 filename = tempfile.mktemp('.py','ipython_edit_')
2429 filename = tempfile.mktemp('.py','ipython_edit_')
2431 self.tempfiles.append(filename)
2430 self.tempfiles.append(filename)
2432
2431
2433 if data:
2432 if data:
2434 tmp_file = open(filename,'w')
2433 tmp_file = open(filename,'w')
2435 tmp_file.write(data)
2434 tmp_file.write(data)
2436 tmp_file.close()
2435 tmp_file.close()
2437 return filename
2436 return filename
2438
2437
2439 def write(self,data):
2438 def write(self,data):
2440 """Write a string to the default output"""
2439 """Write a string to the default output"""
2441 Term.cout.write(data)
2440 Term.cout.write(data)
2442
2441
2443 def write_err(self,data):
2442 def write_err(self,data):
2444 """Write a string to the default error output"""
2443 """Write a string to the default error output"""
2445 Term.cerr.write(data)
2444 Term.cerr.write(data)
2446
2445
2447 def ask_yes_no(self,prompt,default=True):
2446 def ask_yes_no(self,prompt,default=True):
2448 if self.quiet:
2447 if self.quiet:
2449 return True
2448 return True
2450 return ask_yes_no(prompt,default)
2449 return ask_yes_no(prompt,default)
2451
2450
2452 #-------------------------------------------------------------------------
2451 #-------------------------------------------------------------------------
2453 # Things related to GUI support and pylab
2452 # Things related to GUI support and pylab
2454 #-------------------------------------------------------------------------
2453 #-------------------------------------------------------------------------
2455
2454
2456 def enable_pylab(self, gui=None):
2455 def enable_pylab(self, gui=None):
2457 """Activate pylab support at runtime.
2456 """Activate pylab support at runtime.
2458
2457
2459 This turns on support for matplotlib, preloads into the interactive
2458 This turns on support for matplotlib, preloads into the interactive
2460 namespace all of numpy and pylab, and configures IPython to correcdtly
2459 namespace all of numpy and pylab, and configures IPython to correcdtly
2461 interact with the GUI event loop. The GUI backend to be used can be
2460 interact with the GUI event loop. The GUI backend to be used can be
2462 optionally selected with the optional :param:`gui` argument.
2461 optionally selected with the optional :param:`gui` argument.
2463
2462
2464 Parameters
2463 Parameters
2465 ----------
2464 ----------
2466 gui : optional, string
2465 gui : optional, string
2467
2466
2468 If given, dictates the choice of matplotlib GUI backend to use
2467 If given, dictates the choice of matplotlib GUI backend to use
2469 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2468 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2470 'gtk'), otherwise we use the default chosen by matplotlib (as
2469 'gtk'), otherwise we use the default chosen by matplotlib (as
2471 dictated by the matplotlib build-time options plus the user's
2470 dictated by the matplotlib build-time options plus the user's
2472 matplotlibrc configuration file).
2471 matplotlibrc configuration file).
2473 """
2472 """
2474 # We want to prevent the loading of pylab to pollute the user's
2473 # We want to prevent the loading of pylab to pollute the user's
2475 # namespace as shown by the %who* magics, so we execute the activation
2474 # namespace as shown by the %who* magics, so we execute the activation
2476 # code in an empty namespace, and we update *both* user_ns and
2475 # code in an empty namespace, and we update *both* user_ns and
2477 # user_config_ns with this information.
2476 # user_config_ns with this information.
2478 ns = {}
2477 ns = {}
2479 gui = pylab_activate(ns, gui)
2478 gui = pylab_activate(ns, gui)
2480 self.user_ns.update(ns)
2479 self.user_ns.update(ns)
2481 self.user_config_ns.update(ns)
2480 self.user_config_ns.update(ns)
2482 # Now we must activate the gui pylab wants to use, and fix %run to take
2481 # Now we must activate the gui pylab wants to use, and fix %run to take
2483 # plot updates into account
2482 # plot updates into account
2484 enable_gui(gui)
2483 enable_gui(gui)
2485 self.magic_run = self._pylab_magic_run
2484 self.magic_run = self._pylab_magic_run
2486
2485
2487 #-------------------------------------------------------------------------
2486 #-------------------------------------------------------------------------
2488 # Things related to IPython exiting
2487 # Things related to IPython exiting
2489 #-------------------------------------------------------------------------
2488 #-------------------------------------------------------------------------
2490
2489
2491 def ask_exit(self):
2490 def ask_exit(self):
2492 """ Ask the shell to exit. Can be overiden and used as a callback. """
2491 """ Ask the shell to exit. Can be overiden and used as a callback. """
2493 self.exit_now = True
2492 self.exit_now = True
2494
2493
2495 def exit(self):
2494 def exit(self):
2496 """Handle interactive exit.
2495 """Handle interactive exit.
2497
2496
2498 This method calls the ask_exit callback."""
2497 This method calls the ask_exit callback."""
2499 if self.confirm_exit:
2498 if self.confirm_exit:
2500 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2499 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2501 self.ask_exit()
2500 self.ask_exit()
2502 else:
2501 else:
2503 self.ask_exit()
2502 self.ask_exit()
2504
2503
2505 def atexit_operations(self):
2504 def atexit_operations(self):
2506 """This will be executed at the time of exit.
2505 """This will be executed at the time of exit.
2507
2506
2508 Saving of persistent data should be performed here.
2507 Saving of persistent data should be performed here.
2509 """
2508 """
2510 self.savehist()
2509 self.savehist()
2511
2510
2512 # Cleanup all tempfiles left around
2511 # Cleanup all tempfiles left around
2513 for tfile in self.tempfiles:
2512 for tfile in self.tempfiles:
2514 try:
2513 try:
2515 os.unlink(tfile)
2514 os.unlink(tfile)
2516 except OSError:
2515 except OSError:
2517 pass
2516 pass
2518
2517
2519 # Clear all user namespaces to release all references cleanly.
2518 # Clear all user namespaces to release all references cleanly.
2520 self.reset()
2519 self.reset()
2521
2520
2522 # Run user hooks
2521 # Run user hooks
2523 self.hooks.shutdown_hook()
2522 self.hooks.shutdown_hook()
2524
2523
2525 def cleanup(self):
2524 def cleanup(self):
2526 self.restore_sys_module_state()
2525 self.restore_sys_module_state()
2527
2526
2528
2527
@@ -1,196 +1,264 b''
1 """Tests for various magic functions.
1 """Tests for various magic functions.
2
2
3 Needs to be run by nose (to make ipython session available).
3 Needs to be run by nose (to make ipython session available).
4 """
4 """
5 from __future__ import absolute_import
5 from __future__ import absolute_import
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Imports
8 # Imports
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 # stdlib
11 # stdlib
12 import os
12 import os
13 import sys
13 import sys
14 import tempfile
14 import tempfile
15 import types
15 import types
16 from cStringIO import StringIO
16 from cStringIO import StringIO
17
17
18 # third-party
18 # third-party
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 # our own
21 # our own
22 from IPython.utils import genutils
22 from IPython.utils import genutils
23 from IPython.utils.platutils import find_cmd, get_long_path_name
23 from IPython.utils.platutils import find_cmd, get_long_path_name
24 from IPython.testing import decorators as dec
24 from IPython.testing import decorators as dec
25 from IPython.testing import tools as tt
25 from IPython.testing import tools as tt
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Test functions begin
28 # Test functions begin
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 def test_rehashx():
30 def test_rehashx():
31 # clear up everything
31 # clear up everything
32 _ip = get_ipython()
32 _ip = get_ipython()
33 _ip.alias_manager.alias_table.clear()
33 _ip.alias_manager.alias_table.clear()
34 del _ip.db['syscmdlist']
34 del _ip.db['syscmdlist']
35
35
36 _ip.magic('rehashx')
36 _ip.magic('rehashx')
37 # Practically ALL ipython development systems will have more than 10 aliases
37 # Practically ALL ipython development systems will have more than 10 aliases
38
38
39 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
39 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
40 for key, val in _ip.alias_manager.alias_table.items():
40 for key, val in _ip.alias_manager.alias_table.items():
41 # we must strip dots from alias names
41 # we must strip dots from alias names
42 nt.assert_true('.' not in key)
42 nt.assert_true('.' not in key)
43
43
44 # rehashx must fill up syscmdlist
44 # rehashx must fill up syscmdlist
45 scoms = _ip.db['syscmdlist']
45 scoms = _ip.db['syscmdlist']
46 yield (nt.assert_true, len(scoms) > 10)
46 yield (nt.assert_true, len(scoms) > 10)
47
47
48
48
49 def doctest_hist_f():
49 def doctest_hist_f():
50 """Test %hist -f with temporary filename.
50 """Test %hist -f with temporary filename.
51
51
52 In [9]: import tempfile
52 In [9]: import tempfile
53
53
54 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
54 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
55
55
56 In [11]: %hist -n -f $tfile 3
56 In [11]: %hist -n -f $tfile 3
57 """
57 """
58
58
59
59
60 def doctest_hist_r():
60 def doctest_hist_r():
61 """Test %hist -r
61 """Test %hist -r
62
62
63 XXX - This test is not recording the output correctly. Not sure why...
63 XXX - This test is not recording the output correctly. For some reason, in
64 testing mode the raw history isn't getting populated. No idea why.
65 Disabling the output checking for now, though at least we do run it.
64
66
65 In [20]: 'hist' in _ip.lsmagic()
67 In [1]: 'hist' in _ip.lsmagic()
66 Out[20]: True
68 Out[1]: True
67
69
68 In [6]: x=1
70 In [2]: x=1
69
71
70 In [7]: %hist -n -r 2
72 In [3]: %hist -r 2
71 x=1 # random
73 x=1 # random
72 hist -n -r 2 # random
74 %hist -r 2
73 """
75 """
74
76
77 def doctest_hist_op():
78 """Test %hist -op
79
80 In [1]: class b:
81 ...: pass
82 ...:
83
84 In [2]: class s(b):
85 ...: def __str__(self):
86 ...: return 's'
87 ...:
88
89 In [3]:
90
91 In [4]: class r(b):
92 ...: def __repr__(self):
93 ...: return 'r'
94 ...:
95
96 In [5]: class sr(s,r): pass
97 ...:
98
99 In [6]:
100
101 In [7]: bb=b()
102
103 In [8]: ss=s()
104
105 In [9]: rr=r()
106
107 In [10]: ssrr=sr()
108
109 In [11]: bb
110 Out[11]: <...b instance at ...>
111
112 In [12]: ss
113 Out[12]: <...s instance at ...>
114
115 In [13]:
116
117 In [14]: %hist -op
118 >>> class b:
119 ... pass
120 ...
121 >>> class s(b):
122 ... def __str__(self):
123 ... return 's'
124 ...
125 >>>
126 >>> class r(b):
127 ... def __repr__(self):
128 ... return 'r'
129 ...
130 >>> class sr(s,r): pass
131 >>>
132 >>> bb=b()
133 >>> ss=s()
134 >>> rr=r()
135 >>> ssrr=sr()
136 >>> bb
137 <...b instance at ...>
138 >>> ss
139 <...s instance at ...>
140 >>>
141 >>> get_ipython().magic("hist -op")
142 """
75
143
76 def test_shist():
144 def test_shist():
77 # Simple tests of ShadowHist class - test generator.
145 # Simple tests of ShadowHist class - test generator.
78 import os, shutil, tempfile
146 import os, shutil, tempfile
79
147
80 from IPython.utils import pickleshare
148 from IPython.utils import pickleshare
81 from IPython.core.history import ShadowHist
149 from IPython.core.history import ShadowHist
82
150
83 tfile = tempfile.mktemp('','tmp-ipython-')
151 tfile = tempfile.mktemp('','tmp-ipython-')
84
152
85 db = pickleshare.PickleShareDB(tfile)
153 db = pickleshare.PickleShareDB(tfile)
86 s = ShadowHist(db)
154 s = ShadowHist(db)
87 s.add('hello')
155 s.add('hello')
88 s.add('world')
156 s.add('world')
89 s.add('hello')
157 s.add('hello')
90 s.add('hello')
158 s.add('hello')
91 s.add('karhu')
159 s.add('karhu')
92
160
93 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
161 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
94
162
95 yield nt.assert_equal,s.get(2),'world'
163 yield nt.assert_equal,s.get(2),'world'
96
164
97 shutil.rmtree(tfile)
165 shutil.rmtree(tfile)
98
166
99
167
100 # XXX failing for now, until we get clearcmd out of quarantine. But we should
168 # XXX failing for now, until we get clearcmd out of quarantine. But we should
101 # fix this and revert the skip to happen only if numpy is not around.
169 # fix this and revert the skip to happen only if numpy is not around.
102 #@dec.skipif_not_numpy
170 #@dec.skipif_not_numpy
103 @dec.skipknownfailure
171 @dec.skipknownfailure
104 def test_numpy_clear_array_undec():
172 def test_numpy_clear_array_undec():
105 from IPython.extensions import clearcmd
173 from IPython.extensions import clearcmd
106
174
107 _ip.ex('import numpy as np')
175 _ip.ex('import numpy as np')
108 _ip.ex('a = np.empty(2)')
176 _ip.ex('a = np.empty(2)')
109 yield (nt.assert_true, 'a' in _ip.user_ns)
177 yield (nt.assert_true, 'a' in _ip.user_ns)
110 _ip.magic('clear array')
178 _ip.magic('clear array')
111 yield (nt.assert_false, 'a' in _ip.user_ns)
179 yield (nt.assert_false, 'a' in _ip.user_ns)
112
180
113
181
114 # Multiple tests for clipboard pasting
182 # Multiple tests for clipboard pasting
115 @dec.parametric
183 @dec.parametric
116 def test_paste():
184 def test_paste():
117 _ip = get_ipython()
185 _ip = get_ipython()
118 def paste(txt, flags='-q'):
186 def paste(txt, flags='-q'):
119 """Paste input text, by default in quiet mode"""
187 """Paste input text, by default in quiet mode"""
120 hooks.clipboard_get = lambda : txt
188 hooks.clipboard_get = lambda : txt
121 _ip.magic('paste '+flags)
189 _ip.magic('paste '+flags)
122
190
123 # Inject fake clipboard hook but save original so we can restore it later
191 # Inject fake clipboard hook but save original so we can restore it later
124 hooks = _ip.hooks
192 hooks = _ip.hooks
125 user_ns = _ip.user_ns
193 user_ns = _ip.user_ns
126 original_clip = hooks.clipboard_get
194 original_clip = hooks.clipboard_get
127
195
128 try:
196 try:
129 # This try/except with an emtpy except clause is here only because
197 # This try/except with an emtpy except clause is here only because
130 # try/yield/finally is invalid syntax in Python 2.4. This will be
198 # try/yield/finally is invalid syntax in Python 2.4. This will be
131 # removed when we drop 2.4-compatibility, and the emtpy except below
199 # removed when we drop 2.4-compatibility, and the emtpy except below
132 # will be changed to a finally.
200 # will be changed to a finally.
133
201
134 # Run tests with fake clipboard function
202 # Run tests with fake clipboard function
135 user_ns.pop('x', None)
203 user_ns.pop('x', None)
136 paste('x=1')
204 paste('x=1')
137 yield nt.assert_equal(user_ns['x'], 1)
205 yield nt.assert_equal(user_ns['x'], 1)
138
206
139 user_ns.pop('x', None)
207 user_ns.pop('x', None)
140 paste('>>> x=2')
208 paste('>>> x=2')
141 yield nt.assert_equal(user_ns['x'], 2)
209 yield nt.assert_equal(user_ns['x'], 2)
142
210
143 paste("""
211 paste("""
144 >>> x = [1,2,3]
212 >>> x = [1,2,3]
145 >>> y = []
213 >>> y = []
146 >>> for i in x:
214 >>> for i in x:
147 ... y.append(i**2)
215 ... y.append(i**2)
148 ...
216 ...
149 """)
217 """)
150 yield nt.assert_equal(user_ns['x'], [1,2,3])
218 yield nt.assert_equal(user_ns['x'], [1,2,3])
151 yield nt.assert_equal(user_ns['y'], [1,4,9])
219 yield nt.assert_equal(user_ns['y'], [1,4,9])
152
220
153 # Now, test that paste -r works
221 # Now, test that paste -r works
154 user_ns.pop('x', None)
222 user_ns.pop('x', None)
155 yield nt.assert_false('x' in user_ns)
223 yield nt.assert_false('x' in user_ns)
156 _ip.magic('paste -r')
224 _ip.magic('paste -r')
157 yield nt.assert_equal(user_ns['x'], [1,2,3])
225 yield nt.assert_equal(user_ns['x'], [1,2,3])
158
226
159 # Also test paste echoing, by temporarily faking the writer
227 # Also test paste echoing, by temporarily faking the writer
160 w = StringIO()
228 w = StringIO()
161 writer = _ip.write
229 writer = _ip.write
162 _ip.write = w.write
230 _ip.write = w.write
163 code = """
231 code = """
164 a = 100
232 a = 100
165 b = 200"""
233 b = 200"""
166 try:
234 try:
167 paste(code,'')
235 paste(code,'')
168 out = w.getvalue()
236 out = w.getvalue()
169 finally:
237 finally:
170 _ip.write = writer
238 _ip.write = writer
171 yield nt.assert_equal(user_ns['a'], 100)
239 yield nt.assert_equal(user_ns['a'], 100)
172 yield nt.assert_equal(user_ns['b'], 200)
240 yield nt.assert_equal(user_ns['b'], 200)
173 yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
241 yield nt.assert_equal(out, code+"\n## -- End pasted text --\n")
174
242
175 finally:
243 finally:
176 # This should be in a finally clause, instead of the bare except above.
244 # This should be in a finally clause, instead of the bare except above.
177 # Restore original hook
245 # Restore original hook
178 hooks.clipboard_get = original_clip
246 hooks.clipboard_get = original_clip
179
247
180
248
181 def test_time():
249 def test_time():
182 _ip.magic('time None')
250 _ip.magic('time None')
183
251
184
252
185 def doctest_time():
253 def doctest_time():
186 """
254 """
187 In [10]: %time None
255 In [10]: %time None
188 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
256 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
189 Wall time: 0.00 s
257 Wall time: 0.00 s
190 """
258 """
191
259
192 def test_doctest_mode():
260 def test_doctest_mode():
193 "Toggle doctest_mode twice, it should be a no-op and run without error"
261 "Toggle doctest_mode twice, it should be a no-op and run without error"
194 _ip.magic('doctest_mode')
262 _ip.magic('doctest_mode')
195 _ip.magic('doctest_mode')
263 _ip.magic('doctest_mode')
196
264
@@ -1,171 +1,160 b''
1 """Global IPython app to support test running.
1 """Global IPython app to support test running.
2
2
3 We must start our own ipython object and heavily muck with it so that all the
3 We must start our own ipython object and heavily muck with it so that all the
4 modifications IPython makes to system behavior don't send the doctest machinery
4 modifications IPython makes to system behavior don't send the doctest machinery
5 into a fit. This code should be considered a gross hack, but it gets the job
5 into a fit. This code should be considered a gross hack, but it gets the job
6 done.
6 done.
7 """
7 """
8
8
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Module imports
12 # Module imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # From the standard library
15 # From the standard library
16 import __builtin__
16 import __builtin__
17 import commands
17 import commands
18 import new
18 import new
19 import os
19 import os
20 import sys
20 import sys
21
21
22 from . import tools
22 from . import tools
23 from IPython.utils.genutils import Term
23 from IPython.utils.genutils import Term
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Functions
26 # Functions
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 # Hack to modify the %run command so we can sync the user's namespace with the
29 # Hack to modify the %run command so we can sync the user's namespace with the
30 # test globals. Once we move over to a clean magic system, this will be done
30 # test globals. Once we move over to a clean magic system, this will be done
31 # with much less ugliness.
31 # with much less ugliness.
32
32
33 class py_file_finder(object):
33 class py_file_finder(object):
34 def __init__(self,test_filename):
34 def __init__(self,test_filename):
35 self.test_filename = test_filename
35 self.test_filename = test_filename
36
36
37 def __call__(self,name):
37 def __call__(self,name):
38 from IPython.utils.genutils import get_py_filename
38 from IPython.utils.genutils import get_py_filename
39 try:
39 try:
40 return get_py_filename(name)
40 return get_py_filename(name)
41 except IOError:
41 except IOError:
42 test_dir = os.path.dirname(self.test_filename)
42 test_dir = os.path.dirname(self.test_filename)
43 new_path = os.path.join(test_dir,name)
43 new_path = os.path.join(test_dir,name)
44 return get_py_filename(new_path)
44 return get_py_filename(new_path)
45
45
46
46
47 def _run_ns_sync(self,arg_s,runner=None):
47 def _run_ns_sync(self,arg_s,runner=None):
48 """Modified version of %run that syncs testing namespaces.
48 """Modified version of %run that syncs testing namespaces.
49
49
50 This is strictly needed for running doctests that call %run.
50 This is strictly needed for running doctests that call %run.
51 """
51 """
52 #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg
52 #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg
53
53
54 _ip = get_ipython()
54 _ip = get_ipython()
55 finder = py_file_finder(arg_s)
55 finder = py_file_finder(arg_s)
56 out = _ip.magic_run_ori(arg_s,runner,finder)
56 out = _ip.magic_run_ori(arg_s,runner,finder)
57 return out
57 return out
58
58
59
59
60 class ipnsdict(dict):
60 class ipnsdict(dict):
61 """A special subclass of dict for use as an IPython namespace in doctests.
61 """A special subclass of dict for use as an IPython namespace in doctests.
62
62
63 This subclass adds a simple checkpointing capability so that when testing
63 This subclass adds a simple checkpointing capability so that when testing
64 machinery clears it (we use it as the test execution context), it doesn't
64 machinery clears it (we use it as the test execution context), it doesn't
65 get completely destroyed.
65 get completely destroyed.
66 """
66 """
67
67
68 def __init__(self,*a):
68 def __init__(self,*a):
69 dict.__init__(self,*a)
69 dict.__init__(self,*a)
70 self._savedict = {}
70 self._savedict = {}
71
71
72 def clear(self):
72 def clear(self):
73 dict.clear(self)
73 dict.clear(self)
74 self.update(self._savedict)
74 self.update(self._savedict)
75
75
76 def _checkpoint(self):
76 def _checkpoint(self):
77 self._savedict.clear()
77 self._savedict.clear()
78 self._savedict.update(self)
78 self._savedict.update(self)
79
79
80 def update(self,other):
80 def update(self,other):
81 self._checkpoint()
81 self._checkpoint()
82 dict.update(self,other)
82 dict.update(self,other)
83
83
84 # If '_' is in the namespace, python won't set it when executing code,
84 # If '_' is in the namespace, python won't set it when executing code,
85 # and we have examples that test it. So we ensure that the namespace
85 # and we have examples that test it. So we ensure that the namespace
86 # is always 'clean' of it before it's used for test code execution.
86 # is always 'clean' of it before it's used for test code execution.
87 self.pop('_',None)
87 self.pop('_',None)
88
88
89 # The builtins namespace must *always* be the real __builtin__ module,
89 # The builtins namespace must *always* be the real __builtin__ module,
90 # else weird stuff happens. The main ipython code does have provisions
90 # else weird stuff happens. The main ipython code does have provisions
91 # to ensure this after %run, but since in this class we do some
91 # to ensure this after %run, but since in this class we do some
92 # aggressive low-level cleaning of the execution namespace, we need to
92 # aggressive low-level cleaning of the execution namespace, we need to
93 # correct for that ourselves, to ensure consitency with the 'real'
93 # correct for that ourselves, to ensure consitency with the 'real'
94 # ipython.
94 # ipython.
95 self['__builtins__'] = __builtin__
95 self['__builtins__'] = __builtin__
96
96
97
97
98 def get_ipython():
98 def get_ipython():
99 # This will get replaced by the real thing once we start IPython below
99 # This will get replaced by the real thing once we start IPython below
100 return start_ipython()
100 return start_ipython()
101
101
102 def start_ipython():
102 def start_ipython():
103 """Start a global IPython shell, which we need for IPython-specific syntax.
103 """Start a global IPython shell, which we need for IPython-specific syntax.
104 """
104 """
105 global get_ipython
105 global get_ipython
106
106
107 # This function should only ever run once!
107 # This function should only ever run once!
108 if hasattr(start_ipython,'already_called'):
108 if hasattr(start_ipython,'already_called'):
109 return
109 return
110 start_ipython.already_called = True
110 start_ipython.already_called = True
111
111
112 # Ok, first time we're called, go ahead
112 # Ok, first time we're called, go ahead
113 from IPython.core import ipapp, iplib
113 from IPython.core import ipapp, iplib
114
114
115 def xsys(cmd):
115 def xsys(cmd):
116 """Execute a command and print its output.
116 """Execute a command and print its output.
117
117
118 This is just a convenience function to replace the IPython system call
118 This is just a convenience function to replace the IPython system call
119 with one that is more doctest-friendly.
119 with one that is more doctest-friendly.
120 """
120 """
121 cmd = _ip.var_expand(cmd,depth=1)
121 cmd = _ip.var_expand(cmd,depth=1)
122 sys.stdout.write(commands.getoutput(cmd))
122 sys.stdout.write(commands.getoutput(cmd))
123 sys.stdout.flush()
123 sys.stdout.flush()
124
124
125 # Store certain global objects that IPython modifies
125 # Store certain global objects that IPython modifies
126 _displayhook = sys.displayhook
126 _displayhook = sys.displayhook
127 _excepthook = sys.excepthook
127 _excepthook = sys.excepthook
128 _main = sys.modules.get('__main__')
128 _main = sys.modules.get('__main__')
129
129
130 argv = tools.default_argv()
130 argv = tools.default_argv()
131
131
132 # Start IPython instance. We customize it to start with minimal frills.
132 # Start IPython instance. We customize it to start with minimal frills.
133 user_ns,global_ns = iplib.make_user_namespaces(ipnsdict(),{})
133 user_ns,global_ns = iplib.make_user_namespaces(ipnsdict(),{})
134 ip = ipapp.IPythonApp(argv, user_ns=user_ns, user_global_ns=global_ns)
134 ip = ipapp.IPythonApp(argv, user_ns=user_ns, user_global_ns=global_ns)
135 ip.initialize()
135 ip.initialize()
136 ip.shell.builtin_trap.set()
136 ip.shell.builtin_trap.set()
137 # Set stderr to stdout so nose can doctest exceptions
137
138 ## Term.cerr = sys.stdout
138 # Set error printing to stdout so nose can doctest exceptions
139 ## sys.stderr = sys.stdout
140 ip.shell.InteractiveTB.out_stream = 'stdout'
139 ip.shell.InteractiveTB.out_stream = 'stdout'
141 # Butcher the logger
142 ip.shell.log = lambda *a,**k: None
143
140
144 # Deactivate the various python system hooks added by ipython for
141 # Deactivate the various python system hooks added by ipython for
145 # interactive convenience so we don't confuse the doctest system
142 # interactive convenience so we don't confuse the doctest system
146 sys.modules['__main__'] = _main
143 sys.modules['__main__'] = _main
147 sys.displayhook = _displayhook
144 sys.displayhook = _displayhook
148 sys.excepthook = _excepthook
145 sys.excepthook = _excepthook
149
146
150 # So that ipython magics and aliases can be doctested (they work by making
147 # So that ipython magics and aliases can be doctested (they work by making
151 # a call into a global _ip object). Also make the top-level get_ipython
148 # a call into a global _ip object). Also make the top-level get_ipython
152 # now return this without calling here again
149 # now return this without calling here again
153 _ip = ip.shell
150 _ip = ip.shell
154 get_ipython = _ip.get_ipython
151 get_ipython = _ip.get_ipython
155 __builtin__._ip = _ip
152 __builtin__._ip = _ip
156 __builtin__.get_ipython = get_ipython
153 __builtin__.get_ipython = get_ipython
157
154
158 # Modify the IPython system call with one that uses getoutput, so that we
155 # Modify the IPython system call with one that uses getoutput, so that we
159 # can capture subcommands and print them to Python's stdout, otherwise the
156 # can capture subcommands and print them to Python's stdout, otherwise the
160 # doctest machinery would miss them.
157 # doctest machinery would miss them.
161 ip.shell.system = xsys
158 ip.shell.system = xsys
162
159
163 # XXX - For some very bizarre reason, the loading of %history by default is
164 # failing. This needs to be fixed later, but for now at least this ensures
165 # that tests that use %hist run to completion.
166 from IPython.core import history
167 history.init_ipython(ip.shell)
168 if not hasattr(ip.shell,'magic_history'):
169 raise RuntimeError("Can't load magics, aborting")
170
171 return _ip
160 return _ip
General Comments 0
You need to be logged in to leave comments. Login now