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