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