##// END OF EJS Templates
Merging with upstream trunk
Fernando Perez -
r1928:b205e2d2 merge
parent child Browse files
Show More
@@ -0,0 +1,41 b''
1 """Minimal script to reproduce our nasty reference counting bug.
2
3 The problem is related to https://bugs.launchpad.net/ipython/+bug/269966
4
5 The original fix for that appeared to work, but John D. Hunter found a
6 matplotlib example which, when run twice in a row, would break. The problem
7 were references held by open figures to internals of Tkinter.
8
9 This code reproduces the problem that John saw, without matplotlib.
10
11 This script is meant to be called by other parts of the test suite that call it
12 via %run as if it were executed interactively by the user. As of 2009-04-13,
13 test_magic.py calls it.
14 """
15
16 #-----------------------------------------------------------------------------
17 # Module imports
18 #-----------------------------------------------------------------------------
19 import sys
20
21 from IPython import ipapi
22
23 #-----------------------------------------------------------------------------
24 # Globals
25 #-----------------------------------------------------------------------------
26 ip = ipapi.get()
27
28 if not '_refbug_cache' in ip.user_ns:
29 ip.user_ns['_refbug_cache'] = []
30
31
32 aglobal = 'Hello'
33 def f():
34 return aglobal
35
36 cache = ip.user_ns['_refbug_cache']
37 cache.append(f)
38
39 def call_f():
40 for func in cache:
41 print 'lowercased:',func().lower()
@@ -0,0 +1,17 b''
1 """Tests for the FakeModule objects.
2 """
3
4 import nose.tools as nt
5
6 from IPython.FakeModule import FakeModule, init_fakemod_dict
7
8 # Make a fakemod and check a few properties
9 def test_mk_fakemod():
10 fm = FakeModule()
11 yield nt.assert_true,fm
12 yield nt.assert_true,lambda : hasattr(fm,'__file__')
13
14 def test_mk_fakemod_fromdict():
15 """Test making a FakeModule object with initial data"""
16 fm = FakeModule(dict(hello=True))
17 nt.assert_true(fm.hello)
@@ -1,42 +1,66 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Class which mimics a module.
3 Class which mimics a module.
4
4
5 Needed to allow pickle to correctly resolve namespaces during IPython
5 Needed to allow pickle to correctly resolve namespaces during IPython
6 sessions.
6 sessions.
7 """
7 """
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu>
10 # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 import types
16 import types
17
17
18 def init_fakemod_dict(fm,adict=None):
19 """Initialize a FakeModule instance __dict__.
20
21 Kept as a standalone function and not a method so the FakeModule API can
22 remain basically empty.
23
24 This should be considered for private IPython use, used in managing
25 namespaces for %run.
26
27 Parameters
28 ----------
29
30 fm : FakeModule instance
31
32 adict : dict, optional
33 """
34
35 dct = {}
36 # It seems pydoc (and perhaps others) needs any module instance to
37 # implement a __nonzero__ method, so we add it if missing:
38 dct.setdefault('__nonzero__',lambda : True)
39 dct.setdefault('__file__',__file__)
40
41 if adict is not None:
42 dct.update(adict)
43
44 # Hard assignment of the object's __dict__. This is nasty but deliberate.
45 fm.__dict__.clear()
46 fm.__dict__.update(dct)
47
48
18 class FakeModule(types.ModuleType):
49 class FakeModule(types.ModuleType):
19 """Simple class with attribute access to fake a module.
50 """Simple class with attribute access to fake a module.
20
51
21 This is not meant to replace a module, but to allow inserting a fake
52 This is not meant to replace a module, but to allow inserting a fake
22 module in sys.modules so that systems which rely on run-time module
53 module in sys.modules so that systems which rely on run-time module
23 importing (like shelve and pickle) work correctly in interactive IPython
54 importing (like shelve and pickle) work correctly in interactive IPython
24 sessions.
55 sessions.
25
56
26 Do NOT use this code for anything other than this IPython private hack."""
57 Do NOT use this code for anything other than this IPython private hack."""
27
58
28 def __init__(self,adict=None):
59 def __init__(self,adict=None):
29
60
30 # tmp to force __dict__ instance creation, else self.__dict__ fails
61 # tmp to force __dict__ instance creation, else self.__dict__ fails
31 self.__iptmp = None
62 self.__iptmp = None
32
33 # It seems pydoc (and perhaps others) needs any module instance to
34 # implement a __nonzero__ method, so we add it if missing:
35 self.__dict__.setdefault('__nonzero__',lambda : True)
36 self.__dict__.setdefault('__file__',__file__)
37
38 # cleanup our temp trick
63 # cleanup our temp trick
39 del self.__iptmp
64 del self.__iptmp
40
65 # Now, initialize the actual data in the instance dict.
41 if adict is not None:
66 init_fakemod_dict(self,adict)
42 self.__dict__.update(adict)
@@ -1,3425 +1,3446 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #*****************************************************************************
5 #*****************************************************************************
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 #****************************************************************************
13 #****************************************************************************
14 # Modules and globals
14 # Modules and globals
15
15
16 # Python standard modules
16 # Python standard modules
17 import __builtin__
17 import __builtin__
18 import bdb
18 import bdb
19 import inspect
19 import inspect
20 import os
20 import os
21 import pdb
21 import pdb
22 import pydoc
22 import pydoc
23 import sys
23 import sys
24 import re
24 import re
25 import tempfile
25 import tempfile
26 import time
26 import time
27 import cPickle as pickle
27 import cPickle as pickle
28 import textwrap
28 import textwrap
29 from cStringIO import StringIO
29 from cStringIO import StringIO
30 from getopt import getopt,GetoptError
30 from getopt import getopt,GetoptError
31 from pprint import pprint, pformat
31 from pprint import pprint, pformat
32
32
33 # cProfile was added in Python2.5
33 # cProfile was added in Python2.5
34 try:
34 try:
35 import cProfile as profile
35 import cProfile as profile
36 import pstats
36 import pstats
37 except ImportError:
37 except ImportError:
38 # profile isn't bundled by default in Debian for license reasons
38 # profile isn't bundled by default in Debian for license reasons
39 try:
39 try:
40 import profile,pstats
40 import profile,pstats
41 except ImportError:
41 except ImportError:
42 profile = pstats = None
42 profile = pstats = None
43
43
44 # Homebrewed
44 # Homebrewed
45 import IPython
45 import IPython
46 from IPython import Debugger, OInspect, wildcard
46 from IPython import Debugger, OInspect, wildcard
47 from IPython.FakeModule import FakeModule
47 from IPython.FakeModule import FakeModule
48 from IPython.Itpl import Itpl, itpl, printpl,itplns
48 from IPython.Itpl import Itpl, itpl, printpl,itplns
49 from IPython.PyColorize import Parser
49 from IPython.PyColorize import Parser
50 from IPython.ipstruct import Struct
50 from IPython.ipstruct import Struct
51 from IPython.macro import Macro
51 from IPython.macro import Macro
52 from IPython.genutils import *
52 from IPython.genutils import *
53 from IPython import platutils
53 from IPython import platutils
54 import IPython.generics
54 import IPython.generics
55 import IPython.ipapi
55 import IPython.ipapi
56 from IPython.ipapi import UsageError
56 from IPython.ipapi import UsageError
57 from IPython.testing import decorators as testdec
57 from IPython.testing import decorators as testdec
58
58
59 #***************************************************************************
59 #***************************************************************************
60 # Utility functions
60 # Utility functions
61 def on_off(tag):
61 def on_off(tag):
62 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
62 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
63 return ['OFF','ON'][tag]
63 return ['OFF','ON'][tag]
64
64
65 class Bunch: pass
65 class Bunch: pass
66
66
67 def compress_dhist(dh):
67 def compress_dhist(dh):
68 head, tail = dh[:-10], dh[-10:]
68 head, tail = dh[:-10], dh[-10:]
69
69
70 newhead = []
70 newhead = []
71 done = set()
71 done = set()
72 for h in head:
72 for h in head:
73 if h in done:
73 if h in done:
74 continue
74 continue
75 newhead.append(h)
75 newhead.append(h)
76 done.add(h)
76 done.add(h)
77
77
78 return newhead + tail
78 return newhead + tail
79
79
80
80
81 #***************************************************************************
81 #***************************************************************************
82 # Main class implementing Magic functionality
82 # Main class implementing Magic functionality
83 class Magic:
83 class Magic:
84 """Magic functions for InteractiveShell.
84 """Magic functions for InteractiveShell.
85
85
86 Shell functions which can be reached as %function_name. All magic
86 Shell functions which can be reached as %function_name. All magic
87 functions should accept a string, which they can parse for their own
87 functions should accept a string, which they can parse for their own
88 needs. This can make some functions easier to type, eg `%cd ../`
88 needs. This can make some functions easier to type, eg `%cd ../`
89 vs. `%cd("../")`
89 vs. `%cd("../")`
90
90
91 ALL definitions MUST begin with the prefix magic_. The user won't need it
91 ALL definitions MUST begin with the prefix magic_. The user won't need it
92 at the command line, but it is is needed in the definition. """
92 at the command line, but it is is needed in the definition. """
93
93
94 # class globals
94 # class globals
95 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
95 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
96 'Automagic is ON, % prefix NOT needed for magic functions.']
96 'Automagic is ON, % prefix NOT needed for magic functions.']
97
97
98 #......................................................................
98 #......................................................................
99 # some utility functions
99 # some utility functions
100
100
101 def __init__(self,shell):
101 def __init__(self,shell):
102
102
103 self.options_table = {}
103 self.options_table = {}
104 if profile is None:
104 if profile is None:
105 self.magic_prun = self.profile_missing_notice
105 self.magic_prun = self.profile_missing_notice
106 self.shell = shell
106 self.shell = shell
107
107
108 # namespace for holding state we may need
108 # namespace for holding state we may need
109 self._magic_state = Bunch()
109 self._magic_state = Bunch()
110
110
111 def profile_missing_notice(self, *args, **kwargs):
111 def profile_missing_notice(self, *args, **kwargs):
112 error("""\
112 error("""\
113 The profile module could not be found. It has been removed from the standard
113 The profile module could not be found. It has been removed from the standard
114 python packages because of its non-free license. To use profiling, install the
114 python packages because of its non-free license. To use profiling, install the
115 python-profiler package from non-free.""")
115 python-profiler package from non-free.""")
116
116
117 def default_option(self,fn,optstr):
117 def default_option(self,fn,optstr):
118 """Make an entry in the options_table for fn, with value optstr"""
118 """Make an entry in the options_table for fn, with value optstr"""
119
119
120 if fn not in self.lsmagic():
120 if fn not in self.lsmagic():
121 error("%s is not a magic function" % fn)
121 error("%s is not a magic function" % fn)
122 self.options_table[fn] = optstr
122 self.options_table[fn] = optstr
123
123
124 def lsmagic(self):
124 def lsmagic(self):
125 """Return a list of currently available magic functions.
125 """Return a list of currently available magic functions.
126
126
127 Gives a list of the bare names after mangling (['ls','cd', ...], not
127 Gives a list of the bare names after mangling (['ls','cd', ...], not
128 ['magic_ls','magic_cd',...]"""
128 ['magic_ls','magic_cd',...]"""
129
129
130 # FIXME. This needs a cleanup, in the way the magics list is built.
130 # FIXME. This needs a cleanup, in the way the magics list is built.
131
131
132 # magics in class definition
132 # magics in class definition
133 class_magic = lambda fn: fn.startswith('magic_') and \
133 class_magic = lambda fn: fn.startswith('magic_') and \
134 callable(Magic.__dict__[fn])
134 callable(Magic.__dict__[fn])
135 # in instance namespace (run-time user additions)
135 # in instance namespace (run-time user additions)
136 inst_magic = lambda fn: fn.startswith('magic_') and \
136 inst_magic = lambda fn: fn.startswith('magic_') and \
137 callable(self.__dict__[fn])
137 callable(self.__dict__[fn])
138 # and bound magics by user (so they can access self):
138 # and bound magics by user (so they can access self):
139 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
139 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
140 callable(self.__class__.__dict__[fn])
140 callable(self.__class__.__dict__[fn])
141 magics = filter(class_magic,Magic.__dict__.keys()) + \
141 magics = filter(class_magic,Magic.__dict__.keys()) + \
142 filter(inst_magic,self.__dict__.keys()) + \
142 filter(inst_magic,self.__dict__.keys()) + \
143 filter(inst_bound_magic,self.__class__.__dict__.keys())
143 filter(inst_bound_magic,self.__class__.__dict__.keys())
144 out = []
144 out = []
145 for fn in set(magics):
145 for fn in set(magics):
146 out.append(fn.replace('magic_','',1))
146 out.append(fn.replace('magic_','',1))
147 out.sort()
147 out.sort()
148 return out
148 return out
149
149
150 def extract_input_slices(self,slices,raw=False):
150 def extract_input_slices(self,slices,raw=False):
151 """Return as a string a set of input history slices.
151 """Return as a string a set of input history slices.
152
152
153 Inputs:
153 Inputs:
154
154
155 - slices: the set of slices is given as a list of strings (like
155 - slices: the set of slices is given as a list of strings (like
156 ['1','4:8','9'], since this function is for use by magic functions
156 ['1','4:8','9'], since this function is for use by magic functions
157 which get their arguments as strings.
157 which get their arguments as strings.
158
158
159 Optional inputs:
159 Optional inputs:
160
160
161 - raw(False): by default, the processed input is used. If this is
161 - raw(False): by default, the processed input is used. If this is
162 true, the raw input history is used instead.
162 true, the raw input history is used instead.
163
163
164 Note that slices can be called with two notations:
164 Note that slices can be called with two notations:
165
165
166 N:M -> standard python form, means including items N...(M-1).
166 N:M -> standard python form, means including items N...(M-1).
167
167
168 N-M -> include items N..M (closed endpoint)."""
168 N-M -> include items N..M (closed endpoint)."""
169
169
170 if raw:
170 if raw:
171 hist = self.shell.input_hist_raw
171 hist = self.shell.input_hist_raw
172 else:
172 else:
173 hist = self.shell.input_hist
173 hist = self.shell.input_hist
174
174
175 cmds = []
175 cmds = []
176 for chunk in slices:
176 for chunk in slices:
177 if ':' in chunk:
177 if ':' in chunk:
178 ini,fin = map(int,chunk.split(':'))
178 ini,fin = map(int,chunk.split(':'))
179 elif '-' in chunk:
179 elif '-' in chunk:
180 ini,fin = map(int,chunk.split('-'))
180 ini,fin = map(int,chunk.split('-'))
181 fin += 1
181 fin += 1
182 else:
182 else:
183 ini = int(chunk)
183 ini = int(chunk)
184 fin = ini+1
184 fin = ini+1
185 cmds.append(hist[ini:fin])
185 cmds.append(hist[ini:fin])
186 return cmds
186 return cmds
187
187
188 def _ofind(self, oname, namespaces=None):
188 def _ofind(self, oname, namespaces=None):
189 """Find an object in the available namespaces.
189 """Find an object in the available namespaces.
190
190
191 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
191 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
192
192
193 Has special code to detect magic functions.
193 Has special code to detect magic functions.
194 """
194 """
195
195
196 oname = oname.strip()
196 oname = oname.strip()
197
197
198 alias_ns = None
198 alias_ns = None
199 if namespaces is None:
199 if namespaces is None:
200 # Namespaces to search in:
200 # Namespaces to search in:
201 # Put them in a list. The order is important so that we
201 # Put them in a list. The order is important so that we
202 # find things in the same order that Python finds them.
202 # find things in the same order that Python finds them.
203 namespaces = [ ('Interactive', self.shell.user_ns),
203 namespaces = [ ('Interactive', self.shell.user_ns),
204 ('IPython internal', self.shell.internal_ns),
204 ('IPython internal', self.shell.internal_ns),
205 ('Python builtin', __builtin__.__dict__),
205 ('Python builtin', __builtin__.__dict__),
206 ('Alias', self.shell.alias_table),
206 ('Alias', self.shell.alias_table),
207 ]
207 ]
208 alias_ns = self.shell.alias_table
208 alias_ns = self.shell.alias_table
209
209
210 # initialize results to 'null'
210 # initialize results to 'null'
211 found = 0; obj = None; ospace = None; ds = None;
211 found = 0; obj = None; ospace = None; ds = None;
212 ismagic = 0; isalias = 0; parent = None
212 ismagic = 0; isalias = 0; parent = None
213
213
214 # Look for the given name by splitting it in parts. If the head is
214 # Look for the given name by splitting it in parts. If the head is
215 # found, then we look for all the remaining parts as members, and only
215 # found, then we look for all the remaining parts as members, and only
216 # declare success if we can find them all.
216 # declare success if we can find them all.
217 oname_parts = oname.split('.')
217 oname_parts = oname.split('.')
218 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
218 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
219 for nsname,ns in namespaces:
219 for nsname,ns in namespaces:
220 try:
220 try:
221 obj = ns[oname_head]
221 obj = ns[oname_head]
222 except KeyError:
222 except KeyError:
223 continue
223 continue
224 else:
224 else:
225 #print 'oname_rest:', oname_rest # dbg
225 #print 'oname_rest:', oname_rest # dbg
226 for part in oname_rest:
226 for part in oname_rest:
227 try:
227 try:
228 parent = obj
228 parent = obj
229 obj = getattr(obj,part)
229 obj = getattr(obj,part)
230 except:
230 except:
231 # Blanket except b/c some badly implemented objects
231 # Blanket except b/c some badly implemented objects
232 # allow __getattr__ to raise exceptions other than
232 # allow __getattr__ to raise exceptions other than
233 # AttributeError, which then crashes IPython.
233 # AttributeError, which then crashes IPython.
234 break
234 break
235 else:
235 else:
236 # If we finish the for loop (no break), we got all members
236 # If we finish the for loop (no break), we got all members
237 found = 1
237 found = 1
238 ospace = nsname
238 ospace = nsname
239 if ns == alias_ns:
239 if ns == alias_ns:
240 isalias = 1
240 isalias = 1
241 break # namespace loop
241 break # namespace loop
242
242
243 # Try to see if it's magic
243 # Try to see if it's magic
244 if not found:
244 if not found:
245 if oname.startswith(self.shell.ESC_MAGIC):
245 if oname.startswith(self.shell.ESC_MAGIC):
246 oname = oname[1:]
246 oname = oname[1:]
247 obj = getattr(self,'magic_'+oname,None)
247 obj = getattr(self,'magic_'+oname,None)
248 if obj is not None:
248 if obj is not None:
249 found = 1
249 found = 1
250 ospace = 'IPython internal'
250 ospace = 'IPython internal'
251 ismagic = 1
251 ismagic = 1
252
252
253 # Last try: special-case some literals like '', [], {}, etc:
253 # Last try: special-case some literals like '', [], {}, etc:
254 if not found and oname_head in ["''",'""','[]','{}','()']:
254 if not found and oname_head in ["''",'""','[]','{}','()']:
255 obj = eval(oname_head)
255 obj = eval(oname_head)
256 found = 1
256 found = 1
257 ospace = 'Interactive'
257 ospace = 'Interactive'
258
258
259 return {'found':found, 'obj':obj, 'namespace':ospace,
259 return {'found':found, 'obj':obj, 'namespace':ospace,
260 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
260 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
261
261
262 def arg_err(self,func):
262 def arg_err(self,func):
263 """Print docstring if incorrect arguments were passed"""
263 """Print docstring if incorrect arguments were passed"""
264 print 'Error in arguments:'
264 print 'Error in arguments:'
265 print OInspect.getdoc(func)
265 print OInspect.getdoc(func)
266
266
267 def format_latex(self,strng):
267 def format_latex(self,strng):
268 """Format a string for latex inclusion."""
268 """Format a string for latex inclusion."""
269
269
270 # Characters that need to be escaped for latex:
270 # Characters that need to be escaped for latex:
271 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
271 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
272 # Magic command names as headers:
272 # Magic command names as headers:
273 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
273 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
274 re.MULTILINE)
274 re.MULTILINE)
275 # Magic commands
275 # Magic commands
276 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
276 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
277 re.MULTILINE)
277 re.MULTILINE)
278 # Paragraph continue
278 # Paragraph continue
279 par_re = re.compile(r'\\$',re.MULTILINE)
279 par_re = re.compile(r'\\$',re.MULTILINE)
280
280
281 # The "\n" symbol
281 # The "\n" symbol
282 newline_re = re.compile(r'\\n')
282 newline_re = re.compile(r'\\n')
283
283
284 # Now build the string for output:
284 # Now build the string for output:
285 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
285 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
286 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
286 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
287 strng)
287 strng)
288 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
288 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
289 strng = par_re.sub(r'\\\\',strng)
289 strng = par_re.sub(r'\\\\',strng)
290 strng = escape_re.sub(r'\\\1',strng)
290 strng = escape_re.sub(r'\\\1',strng)
291 strng = newline_re.sub(r'\\textbackslash{}n',strng)
291 strng = newline_re.sub(r'\\textbackslash{}n',strng)
292 return strng
292 return strng
293
293
294 def format_screen(self,strng):
294 def format_screen(self,strng):
295 """Format a string for screen printing.
295 """Format a string for screen printing.
296
296
297 This removes some latex-type format codes."""
297 This removes some latex-type format codes."""
298 # Paragraph continue
298 # Paragraph continue
299 par_re = re.compile(r'\\$',re.MULTILINE)
299 par_re = re.compile(r'\\$',re.MULTILINE)
300 strng = par_re.sub('',strng)
300 strng = par_re.sub('',strng)
301 return strng
301 return strng
302
302
303 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
303 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
304 """Parse options passed to an argument string.
304 """Parse options passed to an argument string.
305
305
306 The interface is similar to that of getopt(), but it returns back a
306 The interface is similar to that of getopt(), but it returns back a
307 Struct with the options as keys and the stripped argument string still
307 Struct with the options as keys and the stripped argument string still
308 as a string.
308 as a string.
309
309
310 arg_str is quoted as a true sys.argv vector by using shlex.split.
310 arg_str is quoted as a true sys.argv vector by using shlex.split.
311 This allows us to easily expand variables, glob files, quote
311 This allows us to easily expand variables, glob files, quote
312 arguments, etc.
312 arguments, etc.
313
313
314 Options:
314 Options:
315 -mode: default 'string'. If given as 'list', the argument string is
315 -mode: default 'string'. If given as 'list', the argument string is
316 returned as a list (split on whitespace) instead of a string.
316 returned as a list (split on whitespace) instead of a string.
317
317
318 -list_all: put all option values in lists. Normally only options
318 -list_all: put all option values in lists. Normally only options
319 appearing more than once are put in a list.
319 appearing more than once are put in a list.
320
320
321 -posix (True): whether to split the input line in POSIX mode or not,
321 -posix (True): whether to split the input line in POSIX mode or not,
322 as per the conventions outlined in the shlex module from the
322 as per the conventions outlined in the shlex module from the
323 standard library."""
323 standard library."""
324
324
325 # inject default options at the beginning of the input line
325 # inject default options at the beginning of the input line
326 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
326 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
327 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
327 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
328
328
329 mode = kw.get('mode','string')
329 mode = kw.get('mode','string')
330 if mode not in ['string','list']:
330 if mode not in ['string','list']:
331 raise ValueError,'incorrect mode given: %s' % mode
331 raise ValueError,'incorrect mode given: %s' % mode
332 # Get options
332 # Get options
333 list_all = kw.get('list_all',0)
333 list_all = kw.get('list_all',0)
334 posix = kw.get('posix',True)
334 posix = kw.get('posix',True)
335
335
336 # Check if we have more than one argument to warrant extra processing:
336 # Check if we have more than one argument to warrant extra processing:
337 odict = {} # Dictionary with options
337 odict = {} # Dictionary with options
338 args = arg_str.split()
338 args = arg_str.split()
339 if len(args) >= 1:
339 if len(args) >= 1:
340 # If the list of inputs only has 0 or 1 thing in it, there's no
340 # If the list of inputs only has 0 or 1 thing in it, there's no
341 # need to look for options
341 # need to look for options
342 argv = arg_split(arg_str,posix)
342 argv = arg_split(arg_str,posix)
343 # Do regular option processing
343 # Do regular option processing
344 try:
344 try:
345 opts,args = getopt(argv,opt_str,*long_opts)
345 opts,args = getopt(argv,opt_str,*long_opts)
346 except GetoptError,e:
346 except GetoptError,e:
347 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
347 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
348 " ".join(long_opts)))
348 " ".join(long_opts)))
349 for o,a in opts:
349 for o,a in opts:
350 if o.startswith('--'):
350 if o.startswith('--'):
351 o = o[2:]
351 o = o[2:]
352 else:
352 else:
353 o = o[1:]
353 o = o[1:]
354 try:
354 try:
355 odict[o].append(a)
355 odict[o].append(a)
356 except AttributeError:
356 except AttributeError:
357 odict[o] = [odict[o],a]
357 odict[o] = [odict[o],a]
358 except KeyError:
358 except KeyError:
359 if list_all:
359 if list_all:
360 odict[o] = [a]
360 odict[o] = [a]
361 else:
361 else:
362 odict[o] = a
362 odict[o] = a
363
363
364 # Prepare opts,args for return
364 # Prepare opts,args for return
365 opts = Struct(odict)
365 opts = Struct(odict)
366 if mode == 'string':
366 if mode == 'string':
367 args = ' '.join(args)
367 args = ' '.join(args)
368
368
369 return opts,args
369 return opts,args
370
370
371 #......................................................................
371 #......................................................................
372 # And now the actual magic functions
372 # And now the actual magic functions
373
373
374 # Functions for IPython shell work (vars,funcs, config, etc)
374 # Functions for IPython shell work (vars,funcs, config, etc)
375 def magic_lsmagic(self, parameter_s = ''):
375 def magic_lsmagic(self, parameter_s = ''):
376 """List currently available magic functions."""
376 """List currently available magic functions."""
377 mesc = self.shell.ESC_MAGIC
377 mesc = self.shell.ESC_MAGIC
378 print 'Available magic functions:\n'+mesc+\
378 print 'Available magic functions:\n'+mesc+\
379 (' '+mesc).join(self.lsmagic())
379 (' '+mesc).join(self.lsmagic())
380 print '\n' + Magic.auto_status[self.shell.rc.automagic]
380 print '\n' + Magic.auto_status[self.shell.rc.automagic]
381 return None
381 return None
382
382
383 def magic_magic(self, parameter_s = ''):
383 def magic_magic(self, parameter_s = ''):
384 """Print information about the magic function system.
384 """Print information about the magic function system.
385
385
386 Supported formats: -latex, -brief, -rest
386 Supported formats: -latex, -brief, -rest
387 """
387 """
388
388
389 mode = ''
389 mode = ''
390 try:
390 try:
391 if parameter_s.split()[0] == '-latex':
391 if parameter_s.split()[0] == '-latex':
392 mode = 'latex'
392 mode = 'latex'
393 if parameter_s.split()[0] == '-brief':
393 if parameter_s.split()[0] == '-brief':
394 mode = 'brief'
394 mode = 'brief'
395 if parameter_s.split()[0] == '-rest':
395 if parameter_s.split()[0] == '-rest':
396 mode = 'rest'
396 mode = 'rest'
397 rest_docs = []
397 rest_docs = []
398 except:
398 except:
399 pass
399 pass
400
400
401 magic_docs = []
401 magic_docs = []
402 for fname in self.lsmagic():
402 for fname in self.lsmagic():
403 mname = 'magic_' + fname
403 mname = 'magic_' + fname
404 for space in (Magic,self,self.__class__):
404 for space in (Magic,self,self.__class__):
405 try:
405 try:
406 fn = space.__dict__[mname]
406 fn = space.__dict__[mname]
407 except KeyError:
407 except KeyError:
408 pass
408 pass
409 else:
409 else:
410 break
410 break
411 if mode == 'brief':
411 if mode == 'brief':
412 # only first line
412 # only first line
413 if fn.__doc__:
413 if fn.__doc__:
414 fndoc = fn.__doc__.split('\n',1)[0]
414 fndoc = fn.__doc__.split('\n',1)[0]
415 else:
415 else:
416 fndoc = 'No documentation'
416 fndoc = 'No documentation'
417 else:
417 else:
418 if fn.__doc__:
418 if fn.__doc__:
419 fndoc = fn.__doc__.rstrip()
419 fndoc = fn.__doc__.rstrip()
420 else:
420 else:
421 fndoc = 'No documentation'
421 fndoc = 'No documentation'
422
422
423
423
424 if mode == 'rest':
424 if mode == 'rest':
425 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(self.shell.ESC_MAGIC,
425 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(self.shell.ESC_MAGIC,
426 fname,fndoc))
426 fname,fndoc))
427
427
428 else:
428 else:
429 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
429 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
430 fname,fndoc))
430 fname,fndoc))
431
431
432 magic_docs = ''.join(magic_docs)
432 magic_docs = ''.join(magic_docs)
433
433
434 if mode == 'rest':
434 if mode == 'rest':
435 return "".join(rest_docs)
435 return "".join(rest_docs)
436
436
437 if mode == 'latex':
437 if mode == 'latex':
438 print self.format_latex(magic_docs)
438 print self.format_latex(magic_docs)
439 return
439 return
440 else:
440 else:
441 magic_docs = self.format_screen(magic_docs)
441 magic_docs = self.format_screen(magic_docs)
442 if mode == 'brief':
442 if mode == 'brief':
443 return magic_docs
443 return magic_docs
444
444
445 outmsg = """
445 outmsg = """
446 IPython's 'magic' functions
446 IPython's 'magic' functions
447 ===========================
447 ===========================
448
448
449 The magic function system provides a series of functions which allow you to
449 The magic function system provides a series of functions which allow you to
450 control the behavior of IPython itself, plus a lot of system-type
450 control the behavior of IPython itself, plus a lot of system-type
451 features. All these functions are prefixed with a % character, but parameters
451 features. All these functions are prefixed with a % character, but parameters
452 are given without parentheses or quotes.
452 are given without parentheses or quotes.
453
453
454 NOTE: If you have 'automagic' enabled (via the command line option or with the
454 NOTE: If you have 'automagic' enabled (via the command line option or with the
455 %automagic function), you don't need to type in the % explicitly. By default,
455 %automagic function), you don't need to type in the % explicitly. By default,
456 IPython ships with automagic on, so you should only rarely need the % escape.
456 IPython ships with automagic on, so you should only rarely need the % escape.
457
457
458 Example: typing '%cd mydir' (without the quotes) changes you working directory
458 Example: typing '%cd mydir' (without the quotes) changes you working directory
459 to 'mydir', if it exists.
459 to 'mydir', if it exists.
460
460
461 You can define your own magic functions to extend the system. See the supplied
461 You can define your own magic functions to extend the system. See the supplied
462 ipythonrc and example-magic.py files for details (in your ipython
462 ipythonrc and example-magic.py files for details (in your ipython
463 configuration directory, typically $HOME/.ipython/).
463 configuration directory, typically $HOME/.ipython/).
464
464
465 You can also define your own aliased names for magic functions. In your
465 You can also define your own aliased names for magic functions. In your
466 ipythonrc file, placing a line like:
466 ipythonrc file, placing a line like:
467
467
468 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
468 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
469
469
470 will define %pf as a new name for %profile.
470 will define %pf as a new name for %profile.
471
471
472 You can also call magics in code using the ipmagic() function, which IPython
472 You can also call magics in code using the ipmagic() function, which IPython
473 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
473 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
474
474
475 For a list of the available magic functions, use %lsmagic. For a description
475 For a list of the available magic functions, use %lsmagic. For a description
476 of any of them, type %magic_name?, e.g. '%cd?'.
476 of any of them, type %magic_name?, e.g. '%cd?'.
477
477
478 Currently the magic system has the following functions:\n"""
478 Currently the magic system has the following functions:\n"""
479
479
480 mesc = self.shell.ESC_MAGIC
480 mesc = self.shell.ESC_MAGIC
481 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
481 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
482 "\n\n%s%s\n\n%s" % (outmsg,
482 "\n\n%s%s\n\n%s" % (outmsg,
483 magic_docs,mesc,mesc,
483 magic_docs,mesc,mesc,
484 (' '+mesc).join(self.lsmagic()),
484 (' '+mesc).join(self.lsmagic()),
485 Magic.auto_status[self.shell.rc.automagic] ) )
485 Magic.auto_status[self.shell.rc.automagic] ) )
486
486
487 page(outmsg,screen_lines=self.shell.rc.screen_length)
487 page(outmsg,screen_lines=self.shell.rc.screen_length)
488
488
489
489
490 def magic_autoindent(self, parameter_s = ''):
490 def magic_autoindent(self, parameter_s = ''):
491 """Toggle autoindent on/off (if available)."""
491 """Toggle autoindent on/off (if available)."""
492
492
493 self.shell.set_autoindent()
493 self.shell.set_autoindent()
494 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
494 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
495
495
496
496
497 def magic_automagic(self, parameter_s = ''):
497 def magic_automagic(self, parameter_s = ''):
498 """Make magic functions callable without having to type the initial %.
498 """Make magic functions callable without having to type the initial %.
499
499
500 Without argumentsl toggles on/off (when off, you must call it as
500 Without argumentsl toggles on/off (when off, you must call it as
501 %automagic, of course). With arguments it sets the value, and you can
501 %automagic, of course). With arguments it sets the value, and you can
502 use any of (case insensitive):
502 use any of (case insensitive):
503
503
504 - on,1,True: to activate
504 - on,1,True: to activate
505
505
506 - off,0,False: to deactivate.
506 - off,0,False: to deactivate.
507
507
508 Note that magic functions have lowest priority, so if there's a
508 Note that magic functions have lowest priority, so if there's a
509 variable whose name collides with that of a magic fn, automagic won't
509 variable whose name collides with that of a magic fn, automagic won't
510 work for that function (you get the variable instead). However, if you
510 work for that function (you get the variable instead). However, if you
511 delete the variable (del var), the previously shadowed magic function
511 delete the variable (del var), the previously shadowed magic function
512 becomes visible to automagic again."""
512 becomes visible to automagic again."""
513
513
514 rc = self.shell.rc
514 rc = self.shell.rc
515 arg = parameter_s.lower()
515 arg = parameter_s.lower()
516 if parameter_s in ('on','1','true'):
516 if parameter_s in ('on','1','true'):
517 rc.automagic = True
517 rc.automagic = True
518 elif parameter_s in ('off','0','false'):
518 elif parameter_s in ('off','0','false'):
519 rc.automagic = False
519 rc.automagic = False
520 else:
520 else:
521 rc.automagic = not rc.automagic
521 rc.automagic = not rc.automagic
522 print '\n' + Magic.auto_status[rc.automagic]
522 print '\n' + Magic.auto_status[rc.automagic]
523
523
524 @testdec.skip_doctest
524 @testdec.skip_doctest
525 def magic_autocall(self, parameter_s = ''):
525 def magic_autocall(self, parameter_s = ''):
526 """Make functions callable without having to type parentheses.
526 """Make functions callable without having to type parentheses.
527
527
528 Usage:
528 Usage:
529
529
530 %autocall [mode]
530 %autocall [mode]
531
531
532 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
532 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
533 value is toggled on and off (remembering the previous state).
533 value is toggled on and off (remembering the previous state).
534
534
535 In more detail, these values mean:
535 In more detail, these values mean:
536
536
537 0 -> fully disabled
537 0 -> fully disabled
538
538
539 1 -> active, but do not apply if there are no arguments on the line.
539 1 -> active, but do not apply if there are no arguments on the line.
540
540
541 In this mode, you get:
541 In this mode, you get:
542
542
543 In [1]: callable
543 In [1]: callable
544 Out[1]: <built-in function callable>
544 Out[1]: <built-in function callable>
545
545
546 In [2]: callable 'hello'
546 In [2]: callable 'hello'
547 ------> callable('hello')
547 ------> callable('hello')
548 Out[2]: False
548 Out[2]: False
549
549
550 2 -> Active always. Even if no arguments are present, the callable
550 2 -> Active always. Even if no arguments are present, the callable
551 object is called:
551 object is called:
552
552
553 In [2]: float
553 In [2]: float
554 ------> float()
554 ------> float()
555 Out[2]: 0.0
555 Out[2]: 0.0
556
556
557 Note that even with autocall off, you can still use '/' at the start of
557 Note that even with autocall off, you can still use '/' at the start of
558 a line to treat the first argument on the command line as a function
558 a line to treat the first argument on the command line as a function
559 and add parentheses to it:
559 and add parentheses to it:
560
560
561 In [8]: /str 43
561 In [8]: /str 43
562 ------> str(43)
562 ------> str(43)
563 Out[8]: '43'
563 Out[8]: '43'
564
564
565 # all-random (note for auto-testing)
565 # all-random (note for auto-testing)
566 """
566 """
567
567
568 rc = self.shell.rc
568 rc = self.shell.rc
569
569
570 if parameter_s:
570 if parameter_s:
571 arg = int(parameter_s)
571 arg = int(parameter_s)
572 else:
572 else:
573 arg = 'toggle'
573 arg = 'toggle'
574
574
575 if not arg in (0,1,2,'toggle'):
575 if not arg in (0,1,2,'toggle'):
576 error('Valid modes: (0->Off, 1->Smart, 2->Full')
576 error('Valid modes: (0->Off, 1->Smart, 2->Full')
577 return
577 return
578
578
579 if arg in (0,1,2):
579 if arg in (0,1,2):
580 rc.autocall = arg
580 rc.autocall = arg
581 else: # toggle
581 else: # toggle
582 if rc.autocall:
582 if rc.autocall:
583 self._magic_state.autocall_save = rc.autocall
583 self._magic_state.autocall_save = rc.autocall
584 rc.autocall = 0
584 rc.autocall = 0
585 else:
585 else:
586 try:
586 try:
587 rc.autocall = self._magic_state.autocall_save
587 rc.autocall = self._magic_state.autocall_save
588 except AttributeError:
588 except AttributeError:
589 rc.autocall = self._magic_state.autocall_save = 1
589 rc.autocall = self._magic_state.autocall_save = 1
590
590
591 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
591 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
592
592
593 def magic_system_verbose(self, parameter_s = ''):
593 def magic_system_verbose(self, parameter_s = ''):
594 """Set verbose printing of system calls.
594 """Set verbose printing of system calls.
595
595
596 If called without an argument, act as a toggle"""
596 If called without an argument, act as a toggle"""
597
597
598 if parameter_s:
598 if parameter_s:
599 val = bool(eval(parameter_s))
599 val = bool(eval(parameter_s))
600 else:
600 else:
601 val = None
601 val = None
602
602
603 self.shell.rc_set_toggle('system_verbose',val)
603 self.shell.rc_set_toggle('system_verbose',val)
604 print "System verbose printing is:",\
604 print "System verbose printing is:",\
605 ['OFF','ON'][self.shell.rc.system_verbose]
605 ['OFF','ON'][self.shell.rc.system_verbose]
606
606
607
607
608 def magic_page(self, parameter_s=''):
608 def magic_page(self, parameter_s=''):
609 """Pretty print the object and display it through a pager.
609 """Pretty print the object and display it through a pager.
610
610
611 %page [options] OBJECT
611 %page [options] OBJECT
612
612
613 If no object is given, use _ (last output).
613 If no object is given, use _ (last output).
614
614
615 Options:
615 Options:
616
616
617 -r: page str(object), don't pretty-print it."""
617 -r: page str(object), don't pretty-print it."""
618
618
619 # After a function contributed by Olivier Aubert, slightly modified.
619 # After a function contributed by Olivier Aubert, slightly modified.
620
620
621 # Process options/args
621 # Process options/args
622 opts,args = self.parse_options(parameter_s,'r')
622 opts,args = self.parse_options(parameter_s,'r')
623 raw = 'r' in opts
623 raw = 'r' in opts
624
624
625 oname = args and args or '_'
625 oname = args and args or '_'
626 info = self._ofind(oname)
626 info = self._ofind(oname)
627 if info['found']:
627 if info['found']:
628 txt = (raw and str or pformat)( info['obj'] )
628 txt = (raw and str or pformat)( info['obj'] )
629 page(txt)
629 page(txt)
630 else:
630 else:
631 print 'Object `%s` not found' % oname
631 print 'Object `%s` not found' % oname
632
632
633 def magic_profile(self, parameter_s=''):
633 def magic_profile(self, parameter_s=''):
634 """Print your currently active IPyhton profile."""
634 """Print your currently active IPyhton profile."""
635 if self.shell.rc.profile:
635 if self.shell.rc.profile:
636 printpl('Current IPython profile: $self.shell.rc.profile.')
636 printpl('Current IPython profile: $self.shell.rc.profile.')
637 else:
637 else:
638 print 'No profile active.'
638 print 'No profile active.'
639
639
640 def magic_pinfo(self, parameter_s='', namespaces=None):
640 def magic_pinfo(self, parameter_s='', namespaces=None):
641 """Provide detailed information about an object.
641 """Provide detailed information about an object.
642
642
643 '%pinfo object' is just a synonym for object? or ?object."""
643 '%pinfo object' is just a synonym for object? or ?object."""
644
644
645 #print 'pinfo par: <%s>' % parameter_s # dbg
645 #print 'pinfo par: <%s>' % parameter_s # dbg
646
646
647
647
648 # detail_level: 0 -> obj? , 1 -> obj??
648 # detail_level: 0 -> obj? , 1 -> obj??
649 detail_level = 0
649 detail_level = 0
650 # We need to detect if we got called as 'pinfo pinfo foo', which can
650 # We need to detect if we got called as 'pinfo pinfo foo', which can
651 # happen if the user types 'pinfo foo?' at the cmd line.
651 # happen if the user types 'pinfo foo?' at the cmd line.
652 pinfo,qmark1,oname,qmark2 = \
652 pinfo,qmark1,oname,qmark2 = \
653 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
653 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
654 if pinfo or qmark1 or qmark2:
654 if pinfo or qmark1 or qmark2:
655 detail_level = 1
655 detail_level = 1
656 if "*" in oname:
656 if "*" in oname:
657 self.magic_psearch(oname)
657 self.magic_psearch(oname)
658 else:
658 else:
659 self._inspect('pinfo', oname, detail_level=detail_level,
659 self._inspect('pinfo', oname, detail_level=detail_level,
660 namespaces=namespaces)
660 namespaces=namespaces)
661
661
662 def magic_pdef(self, parameter_s='', namespaces=None):
662 def magic_pdef(self, parameter_s='', namespaces=None):
663 """Print the definition header for any callable object.
663 """Print the definition header for any callable object.
664
664
665 If the object is a class, print the constructor information."""
665 If the object is a class, print the constructor information."""
666 self._inspect('pdef',parameter_s, namespaces)
666 self._inspect('pdef',parameter_s, namespaces)
667
667
668 def magic_pdoc(self, parameter_s='', namespaces=None):
668 def magic_pdoc(self, parameter_s='', namespaces=None):
669 """Print the docstring for an object.
669 """Print the docstring for an object.
670
670
671 If the given object is a class, it will print both the class and the
671 If the given object is a class, it will print both the class and the
672 constructor docstrings."""
672 constructor docstrings."""
673 self._inspect('pdoc',parameter_s, namespaces)
673 self._inspect('pdoc',parameter_s, namespaces)
674
674
675 def magic_psource(self, parameter_s='', namespaces=None):
675 def magic_psource(self, parameter_s='', namespaces=None):
676 """Print (or run through pager) the source code for an object."""
676 """Print (or run through pager) the source code for an object."""
677 self._inspect('psource',parameter_s, namespaces)
677 self._inspect('psource',parameter_s, namespaces)
678
678
679 def magic_pfile(self, parameter_s=''):
679 def magic_pfile(self, parameter_s=''):
680 """Print (or run through pager) the file where an object is defined.
680 """Print (or run through pager) the file where an object is defined.
681
681
682 The file opens at the line where the object definition begins. IPython
682 The file opens at the line where the object definition begins. IPython
683 will honor the environment variable PAGER if set, and otherwise will
683 will honor the environment variable PAGER if set, and otherwise will
684 do its best to print the file in a convenient form.
684 do its best to print the file in a convenient form.
685
685
686 If the given argument is not an object currently defined, IPython will
686 If the given argument is not an object currently defined, IPython will
687 try to interpret it as a filename (automatically adding a .py extension
687 try to interpret it as a filename (automatically adding a .py extension
688 if needed). You can thus use %pfile as a syntax highlighting code
688 if needed). You can thus use %pfile as a syntax highlighting code
689 viewer."""
689 viewer."""
690
690
691 # first interpret argument as an object name
691 # first interpret argument as an object name
692 out = self._inspect('pfile',parameter_s)
692 out = self._inspect('pfile',parameter_s)
693 # if not, try the input as a filename
693 # if not, try the input as a filename
694 if out == 'not found':
694 if out == 'not found':
695 try:
695 try:
696 filename = get_py_filename(parameter_s)
696 filename = get_py_filename(parameter_s)
697 except IOError,msg:
697 except IOError,msg:
698 print msg
698 print msg
699 return
699 return
700 page(self.shell.inspector.format(file(filename).read()))
700 page(self.shell.inspector.format(file(filename).read()))
701
701
702 def _inspect(self,meth,oname,namespaces=None,**kw):
702 def _inspect(self,meth,oname,namespaces=None,**kw):
703 """Generic interface to the inspector system.
703 """Generic interface to the inspector system.
704
704
705 This function is meant to be called by pdef, pdoc & friends."""
705 This function is meant to be called by pdef, pdoc & friends."""
706
706
707 #oname = oname.strip()
707 #oname = oname.strip()
708 #print '1- oname: <%r>' % oname # dbg
708 #print '1- oname: <%r>' % oname # dbg
709 try:
709 try:
710 oname = oname.strip().encode('ascii')
710 oname = oname.strip().encode('ascii')
711 #print '2- oname: <%r>' % oname # dbg
711 #print '2- oname: <%r>' % oname # dbg
712 except UnicodeEncodeError:
712 except UnicodeEncodeError:
713 print 'Python identifiers can only contain ascii characters.'
713 print 'Python identifiers can only contain ascii characters.'
714 return 'not found'
714 return 'not found'
715
715
716 info = Struct(self._ofind(oname, namespaces))
716 info = Struct(self._ofind(oname, namespaces))
717
717
718 if info.found:
718 if info.found:
719 try:
719 try:
720 IPython.generics.inspect_object(info.obj)
720 IPython.generics.inspect_object(info.obj)
721 return
721 return
722 except IPython.ipapi.TryNext:
722 except IPython.ipapi.TryNext:
723 pass
723 pass
724 # Get the docstring of the class property if it exists.
724 # Get the docstring of the class property if it exists.
725 path = oname.split('.')
725 path = oname.split('.')
726 root = '.'.join(path[:-1])
726 root = '.'.join(path[:-1])
727 if info.parent is not None:
727 if info.parent is not None:
728 try:
728 try:
729 target = getattr(info.parent, '__class__')
729 target = getattr(info.parent, '__class__')
730 # The object belongs to a class instance.
730 # The object belongs to a class instance.
731 try:
731 try:
732 target = getattr(target, path[-1])
732 target = getattr(target, path[-1])
733 # The class defines the object.
733 # The class defines the object.
734 if isinstance(target, property):
734 if isinstance(target, property):
735 oname = root + '.__class__.' + path[-1]
735 oname = root + '.__class__.' + path[-1]
736 info = Struct(self._ofind(oname))
736 info = Struct(self._ofind(oname))
737 except AttributeError: pass
737 except AttributeError: pass
738 except AttributeError: pass
738 except AttributeError: pass
739
739
740 pmethod = getattr(self.shell.inspector,meth)
740 pmethod = getattr(self.shell.inspector,meth)
741 formatter = info.ismagic and self.format_screen or None
741 formatter = info.ismagic and self.format_screen or None
742 if meth == 'pdoc':
742 if meth == 'pdoc':
743 pmethod(info.obj,oname,formatter)
743 pmethod(info.obj,oname,formatter)
744 elif meth == 'pinfo':
744 elif meth == 'pinfo':
745 pmethod(info.obj,oname,formatter,info,**kw)
745 pmethod(info.obj,oname,formatter,info,**kw)
746 else:
746 else:
747 pmethod(info.obj,oname)
747 pmethod(info.obj,oname)
748 else:
748 else:
749 print 'Object `%s` not found.' % oname
749 print 'Object `%s` not found.' % oname
750 return 'not found' # so callers can take other action
750 return 'not found' # so callers can take other action
751
751
752 def magic_psearch(self, parameter_s=''):
752 def magic_psearch(self, parameter_s=''):
753 """Search for object in namespaces by wildcard.
753 """Search for object in namespaces by wildcard.
754
754
755 %psearch [options] PATTERN [OBJECT TYPE]
755 %psearch [options] PATTERN [OBJECT TYPE]
756
756
757 Note: ? can be used as a synonym for %psearch, at the beginning or at
757 Note: ? can be used as a synonym for %psearch, at the beginning or at
758 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
758 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
759 rest of the command line must be unchanged (options come first), so
759 rest of the command line must be unchanged (options come first), so
760 for example the following forms are equivalent
760 for example the following forms are equivalent
761
761
762 %psearch -i a* function
762 %psearch -i a* function
763 -i a* function?
763 -i a* function?
764 ?-i a* function
764 ?-i a* function
765
765
766 Arguments:
766 Arguments:
767
767
768 PATTERN
768 PATTERN
769
769
770 where PATTERN is a string containing * as a wildcard similar to its
770 where PATTERN is a string containing * as a wildcard similar to its
771 use in a shell. The pattern is matched in all namespaces on the
771 use in a shell. The pattern is matched in all namespaces on the
772 search path. By default objects starting with a single _ are not
772 search path. By default objects starting with a single _ are not
773 matched, many IPython generated objects have a single
773 matched, many IPython generated objects have a single
774 underscore. The default is case insensitive matching. Matching is
774 underscore. The default is case insensitive matching. Matching is
775 also done on the attributes of objects and not only on the objects
775 also done on the attributes of objects and not only on the objects
776 in a module.
776 in a module.
777
777
778 [OBJECT TYPE]
778 [OBJECT TYPE]
779
779
780 Is the name of a python type from the types module. The name is
780 Is the name of a python type from the types module. The name is
781 given in lowercase without the ending type, ex. StringType is
781 given in lowercase without the ending type, ex. StringType is
782 written string. By adding a type here only objects matching the
782 written string. By adding a type here only objects matching the
783 given type are matched. Using all here makes the pattern match all
783 given type are matched. Using all here makes the pattern match all
784 types (this is the default).
784 types (this is the default).
785
785
786 Options:
786 Options:
787
787
788 -a: makes the pattern match even objects whose names start with a
788 -a: makes the pattern match even objects whose names start with a
789 single underscore. These names are normally ommitted from the
789 single underscore. These names are normally ommitted from the
790 search.
790 search.
791
791
792 -i/-c: make the pattern case insensitive/sensitive. If neither of
792 -i/-c: make the pattern case insensitive/sensitive. If neither of
793 these options is given, the default is read from your ipythonrc
793 these options is given, the default is read from your ipythonrc
794 file. The option name which sets this value is
794 file. The option name which sets this value is
795 'wildcards_case_sensitive'. If this option is not specified in your
795 'wildcards_case_sensitive'. If this option is not specified in your
796 ipythonrc file, IPython's internal default is to do a case sensitive
796 ipythonrc file, IPython's internal default is to do a case sensitive
797 search.
797 search.
798
798
799 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
799 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
800 specifiy can be searched in any of the following namespaces:
800 specifiy can be searched in any of the following namespaces:
801 'builtin', 'user', 'user_global','internal', 'alias', where
801 'builtin', 'user', 'user_global','internal', 'alias', where
802 'builtin' and 'user' are the search defaults. Note that you should
802 'builtin' and 'user' are the search defaults. Note that you should
803 not use quotes when specifying namespaces.
803 not use quotes when specifying namespaces.
804
804
805 'Builtin' contains the python module builtin, 'user' contains all
805 'Builtin' contains the python module builtin, 'user' contains all
806 user data, 'alias' only contain the shell aliases and no python
806 user data, 'alias' only contain the shell aliases and no python
807 objects, 'internal' contains objects used by IPython. The
807 objects, 'internal' contains objects used by IPython. The
808 'user_global' namespace is only used by embedded IPython instances,
808 'user_global' namespace is only used by embedded IPython instances,
809 and it contains module-level globals. You can add namespaces to the
809 and it contains module-level globals. You can add namespaces to the
810 search with -s or exclude them with -e (these options can be given
810 search with -s or exclude them with -e (these options can be given
811 more than once).
811 more than once).
812
812
813 Examples:
813 Examples:
814
814
815 %psearch a* -> objects beginning with an a
815 %psearch a* -> objects beginning with an a
816 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
816 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
817 %psearch a* function -> all functions beginning with an a
817 %psearch a* function -> all functions beginning with an a
818 %psearch re.e* -> objects beginning with an e in module re
818 %psearch re.e* -> objects beginning with an e in module re
819 %psearch r*.e* -> objects that start with e in modules starting in r
819 %psearch r*.e* -> objects that start with e in modules starting in r
820 %psearch r*.* string -> all strings in modules beginning with r
820 %psearch r*.* string -> all strings in modules beginning with r
821
821
822 Case sensitve search:
822 Case sensitve search:
823
823
824 %psearch -c a* list all object beginning with lower case a
824 %psearch -c a* list all object beginning with lower case a
825
825
826 Show objects beginning with a single _:
826 Show objects beginning with a single _:
827
827
828 %psearch -a _* list objects beginning with a single underscore"""
828 %psearch -a _* list objects beginning with a single underscore"""
829 try:
829 try:
830 parameter_s = parameter_s.encode('ascii')
830 parameter_s = parameter_s.encode('ascii')
831 except UnicodeEncodeError:
831 except UnicodeEncodeError:
832 print 'Python identifiers can only contain ascii characters.'
832 print 'Python identifiers can only contain ascii characters.'
833 return
833 return
834
834
835 # default namespaces to be searched
835 # default namespaces to be searched
836 def_search = ['user','builtin']
836 def_search = ['user','builtin']
837
837
838 # Process options/args
838 # Process options/args
839 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
839 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
840 opt = opts.get
840 opt = opts.get
841 shell = self.shell
841 shell = self.shell
842 psearch = shell.inspector.psearch
842 psearch = shell.inspector.psearch
843
843
844 # select case options
844 # select case options
845 if opts.has_key('i'):
845 if opts.has_key('i'):
846 ignore_case = True
846 ignore_case = True
847 elif opts.has_key('c'):
847 elif opts.has_key('c'):
848 ignore_case = False
848 ignore_case = False
849 else:
849 else:
850 ignore_case = not shell.rc.wildcards_case_sensitive
850 ignore_case = not shell.rc.wildcards_case_sensitive
851
851
852 # Build list of namespaces to search from user options
852 # Build list of namespaces to search from user options
853 def_search.extend(opt('s',[]))
853 def_search.extend(opt('s',[]))
854 ns_exclude = ns_exclude=opt('e',[])
854 ns_exclude = ns_exclude=opt('e',[])
855 ns_search = [nm for nm in def_search if nm not in ns_exclude]
855 ns_search = [nm for nm in def_search if nm not in ns_exclude]
856
856
857 # Call the actual search
857 # Call the actual search
858 try:
858 try:
859 psearch(args,shell.ns_table,ns_search,
859 psearch(args,shell.ns_table,ns_search,
860 show_all=opt('a'),ignore_case=ignore_case)
860 show_all=opt('a'),ignore_case=ignore_case)
861 except:
861 except:
862 shell.showtraceback()
862 shell.showtraceback()
863
863
864 def magic_who_ls(self, parameter_s=''):
864 def magic_who_ls(self, parameter_s=''):
865 """Return a sorted list of all interactive variables.
865 """Return a sorted list of all interactive variables.
866
866
867 If arguments are given, only variables of types matching these
867 If arguments are given, only variables of types matching these
868 arguments are returned."""
868 arguments are returned."""
869
869
870 user_ns = self.shell.user_ns
870 user_ns = self.shell.user_ns
871 internal_ns = self.shell.internal_ns
871 internal_ns = self.shell.internal_ns
872 user_config_ns = self.shell.user_config_ns
872 user_config_ns = self.shell.user_config_ns
873 out = []
873 out = []
874 typelist = parameter_s.split()
874 typelist = parameter_s.split()
875
875
876 for i in user_ns:
876 for i in user_ns:
877 if not (i.startswith('_') or i.startswith('_i')) \
877 if not (i.startswith('_') or i.startswith('_i')) \
878 and not (i in internal_ns or i in user_config_ns):
878 and not (i in internal_ns or i in user_config_ns):
879 if typelist:
879 if typelist:
880 if type(user_ns[i]).__name__ in typelist:
880 if type(user_ns[i]).__name__ in typelist:
881 out.append(i)
881 out.append(i)
882 else:
882 else:
883 out.append(i)
883 out.append(i)
884 out.sort()
884 out.sort()
885 return out
885 return out
886
886
887 def magic_who(self, parameter_s=''):
887 def magic_who(self, parameter_s=''):
888 """Print all interactive variables, with some minimal formatting.
888 """Print all interactive variables, with some minimal formatting.
889
889
890 If any arguments are given, only variables whose type matches one of
890 If any arguments are given, only variables whose type matches one of
891 these are printed. For example:
891 these are printed. For example:
892
892
893 %who function str
893 %who function str
894
894
895 will only list functions and strings, excluding all other types of
895 will only list functions and strings, excluding all other types of
896 variables. To find the proper type names, simply use type(var) at a
896 variables. To find the proper type names, simply use type(var) at a
897 command line to see how python prints type names. For example:
897 command line to see how python prints type names. For example:
898
898
899 In [1]: type('hello')\\
899 In [1]: type('hello')\\
900 Out[1]: <type 'str'>
900 Out[1]: <type 'str'>
901
901
902 indicates that the type name for strings is 'str'.
902 indicates that the type name for strings is 'str'.
903
903
904 %who always excludes executed names loaded through your configuration
904 %who always excludes executed names loaded through your configuration
905 file and things which are internal to IPython.
905 file and things which are internal to IPython.
906
906
907 This is deliberate, as typically you may load many modules and the
907 This is deliberate, as typically you may load many modules and the
908 purpose of %who is to show you only what you've manually defined."""
908 purpose of %who is to show you only what you've manually defined."""
909
909
910 varlist = self.magic_who_ls(parameter_s)
910 varlist = self.magic_who_ls(parameter_s)
911 if not varlist:
911 if not varlist:
912 if parameter_s:
912 if parameter_s:
913 print 'No variables match your requested type.'
913 print 'No variables match your requested type.'
914 else:
914 else:
915 print 'Interactive namespace is empty.'
915 print 'Interactive namespace is empty.'
916 return
916 return
917
917
918 # if we have variables, move on...
918 # if we have variables, move on...
919 count = 0
919 count = 0
920 for i in varlist:
920 for i in varlist:
921 print i+'\t',
921 print i+'\t',
922 count += 1
922 count += 1
923 if count > 8:
923 if count > 8:
924 count = 0
924 count = 0
925 print
925 print
926 print
926 print
927
927
928 def magic_whos(self, parameter_s=''):
928 def magic_whos(self, parameter_s=''):
929 """Like %who, but gives some extra information about each variable.
929 """Like %who, but gives some extra information about each variable.
930
930
931 The same type filtering of %who can be applied here.
931 The same type filtering of %who can be applied here.
932
932
933 For all variables, the type is printed. Additionally it prints:
933 For all variables, the type is printed. Additionally it prints:
934
934
935 - For {},[],(): their length.
935 - For {},[],(): their length.
936
936
937 - For numpy and Numeric arrays, a summary with shape, number of
937 - For numpy and Numeric arrays, a summary with shape, number of
938 elements, typecode and size in memory.
938 elements, typecode and size in memory.
939
939
940 - Everything else: a string representation, snipping their middle if
940 - Everything else: a string representation, snipping their middle if
941 too long."""
941 too long."""
942
942
943 varnames = self.magic_who_ls(parameter_s)
943 varnames = self.magic_who_ls(parameter_s)
944 if not varnames:
944 if not varnames:
945 if parameter_s:
945 if parameter_s:
946 print 'No variables match your requested type.'
946 print 'No variables match your requested type.'
947 else:
947 else:
948 print 'Interactive namespace is empty.'
948 print 'Interactive namespace is empty.'
949 return
949 return
950
950
951 # if we have variables, move on...
951 # if we have variables, move on...
952
952
953 # for these types, show len() instead of data:
953 # for these types, show len() instead of data:
954 seq_types = [types.DictType,types.ListType,types.TupleType]
954 seq_types = [types.DictType,types.ListType,types.TupleType]
955
955
956 # for numpy/Numeric arrays, display summary info
956 # for numpy/Numeric arrays, display summary info
957 try:
957 try:
958 import numpy
958 import numpy
959 except ImportError:
959 except ImportError:
960 ndarray_type = None
960 ndarray_type = None
961 else:
961 else:
962 ndarray_type = numpy.ndarray.__name__
962 ndarray_type = numpy.ndarray.__name__
963 try:
963 try:
964 import Numeric
964 import Numeric
965 except ImportError:
965 except ImportError:
966 array_type = None
966 array_type = None
967 else:
967 else:
968 array_type = Numeric.ArrayType.__name__
968 array_type = Numeric.ArrayType.__name__
969
969
970 # Find all variable names and types so we can figure out column sizes
970 # Find all variable names and types so we can figure out column sizes
971 def get_vars(i):
971 def get_vars(i):
972 return self.shell.user_ns[i]
972 return self.shell.user_ns[i]
973
973
974 # some types are well known and can be shorter
974 # some types are well known and can be shorter
975 abbrevs = {'IPython.macro.Macro' : 'Macro'}
975 abbrevs = {'IPython.macro.Macro' : 'Macro'}
976 def type_name(v):
976 def type_name(v):
977 tn = type(v).__name__
977 tn = type(v).__name__
978 return abbrevs.get(tn,tn)
978 return abbrevs.get(tn,tn)
979
979
980 varlist = map(get_vars,varnames)
980 varlist = map(get_vars,varnames)
981
981
982 typelist = []
982 typelist = []
983 for vv in varlist:
983 for vv in varlist:
984 tt = type_name(vv)
984 tt = type_name(vv)
985
985
986 if tt=='instance':
986 if tt=='instance':
987 typelist.append( abbrevs.get(str(vv.__class__),
987 typelist.append( abbrevs.get(str(vv.__class__),
988 str(vv.__class__)))
988 str(vv.__class__)))
989 else:
989 else:
990 typelist.append(tt)
990 typelist.append(tt)
991
991
992 # column labels and # of spaces as separator
992 # column labels and # of spaces as separator
993 varlabel = 'Variable'
993 varlabel = 'Variable'
994 typelabel = 'Type'
994 typelabel = 'Type'
995 datalabel = 'Data/Info'
995 datalabel = 'Data/Info'
996 colsep = 3
996 colsep = 3
997 # variable format strings
997 # variable format strings
998 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
998 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
999 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
999 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1000 aformat = "%s: %s elems, type `%s`, %s bytes"
1000 aformat = "%s: %s elems, type `%s`, %s bytes"
1001 # find the size of the columns to format the output nicely
1001 # find the size of the columns to format the output nicely
1002 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1002 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1003 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1003 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1004 # table header
1004 # table header
1005 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1005 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1006 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1006 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1007 # and the table itself
1007 # and the table itself
1008 kb = 1024
1008 kb = 1024
1009 Mb = 1048576 # kb**2
1009 Mb = 1048576 # kb**2
1010 for vname,var,vtype in zip(varnames,varlist,typelist):
1010 for vname,var,vtype in zip(varnames,varlist,typelist):
1011 print itpl(vformat),
1011 print itpl(vformat),
1012 if vtype in seq_types:
1012 if vtype in seq_types:
1013 print len(var)
1013 print len(var)
1014 elif vtype in [array_type,ndarray_type]:
1014 elif vtype in [array_type,ndarray_type]:
1015 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1015 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1016 if vtype==ndarray_type:
1016 if vtype==ndarray_type:
1017 # numpy
1017 # numpy
1018 vsize = var.size
1018 vsize = var.size
1019 vbytes = vsize*var.itemsize
1019 vbytes = vsize*var.itemsize
1020 vdtype = var.dtype
1020 vdtype = var.dtype
1021 else:
1021 else:
1022 # Numeric
1022 # Numeric
1023 vsize = Numeric.size(var)
1023 vsize = Numeric.size(var)
1024 vbytes = vsize*var.itemsize()
1024 vbytes = vsize*var.itemsize()
1025 vdtype = var.typecode()
1025 vdtype = var.typecode()
1026
1026
1027 if vbytes < 100000:
1027 if vbytes < 100000:
1028 print aformat % (vshape,vsize,vdtype,vbytes)
1028 print aformat % (vshape,vsize,vdtype,vbytes)
1029 else:
1029 else:
1030 print aformat % (vshape,vsize,vdtype,vbytes),
1030 print aformat % (vshape,vsize,vdtype,vbytes),
1031 if vbytes < Mb:
1031 if vbytes < Mb:
1032 print '(%s kb)' % (vbytes/kb,)
1032 print '(%s kb)' % (vbytes/kb,)
1033 else:
1033 else:
1034 print '(%s Mb)' % (vbytes/Mb,)
1034 print '(%s Mb)' % (vbytes/Mb,)
1035 else:
1035 else:
1036 try:
1036 try:
1037 vstr = str(var)
1037 vstr = str(var)
1038 except UnicodeEncodeError:
1038 except UnicodeEncodeError:
1039 vstr = unicode(var).encode(sys.getdefaultencoding(),
1039 vstr = unicode(var).encode(sys.getdefaultencoding(),
1040 'backslashreplace')
1040 'backslashreplace')
1041 vstr = vstr.replace('\n','\\n')
1041 vstr = vstr.replace('\n','\\n')
1042 if len(vstr) < 50:
1042 if len(vstr) < 50:
1043 print vstr
1043 print vstr
1044 else:
1044 else:
1045 printpl(vfmt_short)
1045 printpl(vfmt_short)
1046
1046
1047 def magic_reset(self, parameter_s=''):
1047 def magic_reset(self, parameter_s=''):
1048 """Resets the namespace by removing all names defined by the user.
1048 """Resets the namespace by removing all names defined by the user.
1049
1049
1050 Input/Output history are left around in case you need them.
1050 Input/Output history are left around in case you need them.
1051
1051
1052 Parameters
1052 Parameters
1053 ----------
1053 ----------
1054 -y : force reset without asking for confirmation.
1054 -y : force reset without asking for confirmation.
1055
1055
1056 Examples
1056 Examples
1057 --------
1057 --------
1058 In [6]: a = 1
1058 In [6]: a = 1
1059
1059
1060 In [7]: a
1060 In [7]: a
1061 Out[7]: 1
1061 Out[7]: 1
1062
1062
1063 In [8]: 'a' in _ip.user_ns
1063 In [8]: 'a' in _ip.user_ns
1064 Out[8]: True
1064 Out[8]: True
1065
1065
1066 In [9]: %reset -f
1066 In [9]: %reset -f
1067
1067
1068 In [10]: 'a' in _ip.user_ns
1068 In [10]: 'a' in _ip.user_ns
1069 Out[10]: False
1069 Out[10]: False
1070 """
1070 """
1071
1071
1072 if parameter_s == '-f':
1072 if parameter_s == '-f':
1073 ans = True
1073 ans = True
1074 else:
1074 else:
1075 ans = self.shell.ask_yes_no(
1075 ans = self.shell.ask_yes_no(
1076 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1076 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1077 if not ans:
1077 if not ans:
1078 print 'Nothing done.'
1078 print 'Nothing done.'
1079 return
1079 return
1080 user_ns = self.shell.user_ns
1080 user_ns = self.shell.user_ns
1081 for i in self.magic_who_ls():
1081 for i in self.magic_who_ls():
1082 del(user_ns[i])
1082 del(user_ns[i])
1083
1083
1084 # Also flush the private list of module references kept for script
1084 # Also flush the private list of module references kept for script
1085 # execution protection
1085 # execution protection
1086 self.shell.clear_main_mod_cache()
1086 self.shell.clear_main_mod_cache()
1087
1087
1088 def magic_logstart(self,parameter_s=''):
1088 def magic_logstart(self,parameter_s=''):
1089 """Start logging anywhere in a session.
1089 """Start logging anywhere in a session.
1090
1090
1091 %logstart [-o|-r|-t] [log_name [log_mode]]
1091 %logstart [-o|-r|-t] [log_name [log_mode]]
1092
1092
1093 If no name is given, it defaults to a file named 'ipython_log.py' in your
1093 If no name is given, it defaults to a file named 'ipython_log.py' in your
1094 current directory, in 'rotate' mode (see below).
1094 current directory, in 'rotate' mode (see below).
1095
1095
1096 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1096 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1097 history up to that point and then continues logging.
1097 history up to that point and then continues logging.
1098
1098
1099 %logstart takes a second optional parameter: logging mode. This can be one
1099 %logstart takes a second optional parameter: logging mode. This can be one
1100 of (note that the modes are given unquoted):\\
1100 of (note that the modes are given unquoted):\\
1101 append: well, that says it.\\
1101 append: well, that says it.\\
1102 backup: rename (if exists) to name~ and start name.\\
1102 backup: rename (if exists) to name~ and start name.\\
1103 global: single logfile in your home dir, appended to.\\
1103 global: single logfile in your home dir, appended to.\\
1104 over : overwrite existing log.\\
1104 over : overwrite existing log.\\
1105 rotate: create rotating logs name.1~, name.2~, etc.
1105 rotate: create rotating logs name.1~, name.2~, etc.
1106
1106
1107 Options:
1107 Options:
1108
1108
1109 -o: log also IPython's output. In this mode, all commands which
1109 -o: log also IPython's output. In this mode, all commands which
1110 generate an Out[NN] prompt are recorded to the logfile, right after
1110 generate an Out[NN] prompt are recorded to the logfile, right after
1111 their corresponding input line. The output lines are always
1111 their corresponding input line. The output lines are always
1112 prepended with a '#[Out]# ' marker, so that the log remains valid
1112 prepended with a '#[Out]# ' marker, so that the log remains valid
1113 Python code.
1113 Python code.
1114
1114
1115 Since this marker is always the same, filtering only the output from
1115 Since this marker is always the same, filtering only the output from
1116 a log is very easy, using for example a simple awk call:
1116 a log is very easy, using for example a simple awk call:
1117
1117
1118 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1118 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1119
1119
1120 -r: log 'raw' input. Normally, IPython's logs contain the processed
1120 -r: log 'raw' input. Normally, IPython's logs contain the processed
1121 input, so that user lines are logged in their final form, converted
1121 input, so that user lines are logged in their final form, converted
1122 into valid Python. For example, %Exit is logged as
1122 into valid Python. For example, %Exit is logged as
1123 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1123 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1124 exactly as typed, with no transformations applied.
1124 exactly as typed, with no transformations applied.
1125
1125
1126 -t: put timestamps before each input line logged (these are put in
1126 -t: put timestamps before each input line logged (these are put in
1127 comments)."""
1127 comments)."""
1128
1128
1129 opts,par = self.parse_options(parameter_s,'ort')
1129 opts,par = self.parse_options(parameter_s,'ort')
1130 log_output = 'o' in opts
1130 log_output = 'o' in opts
1131 log_raw_input = 'r' in opts
1131 log_raw_input = 'r' in opts
1132 timestamp = 't' in opts
1132 timestamp = 't' in opts
1133
1133
1134 rc = self.shell.rc
1134 rc = self.shell.rc
1135 logger = self.shell.logger
1135 logger = self.shell.logger
1136
1136
1137 # if no args are given, the defaults set in the logger constructor by
1137 # if no args are given, the defaults set in the logger constructor by
1138 # ipytohn remain valid
1138 # ipytohn remain valid
1139 if par:
1139 if par:
1140 try:
1140 try:
1141 logfname,logmode = par.split()
1141 logfname,logmode = par.split()
1142 except:
1142 except:
1143 logfname = par
1143 logfname = par
1144 logmode = 'backup'
1144 logmode = 'backup'
1145 else:
1145 else:
1146 logfname = logger.logfname
1146 logfname = logger.logfname
1147 logmode = logger.logmode
1147 logmode = logger.logmode
1148 # put logfname into rc struct as if it had been called on the command
1148 # put logfname into rc struct as if it had been called on the command
1149 # line, so it ends up saved in the log header Save it in case we need
1149 # line, so it ends up saved in the log header Save it in case we need
1150 # to restore it...
1150 # to restore it...
1151 old_logfile = rc.opts.get('logfile','')
1151 old_logfile = rc.opts.get('logfile','')
1152 if logfname:
1152 if logfname:
1153 logfname = os.path.expanduser(logfname)
1153 logfname = os.path.expanduser(logfname)
1154 rc.opts.logfile = logfname
1154 rc.opts.logfile = logfname
1155 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1155 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1156 try:
1156 try:
1157 started = logger.logstart(logfname,loghead,logmode,
1157 started = logger.logstart(logfname,loghead,logmode,
1158 log_output,timestamp,log_raw_input)
1158 log_output,timestamp,log_raw_input)
1159 except:
1159 except:
1160 rc.opts.logfile = old_logfile
1160 rc.opts.logfile = old_logfile
1161 warn("Couldn't start log: %s" % sys.exc_info()[1])
1161 warn("Couldn't start log: %s" % sys.exc_info()[1])
1162 else:
1162 else:
1163 # log input history up to this point, optionally interleaving
1163 # log input history up to this point, optionally interleaving
1164 # output if requested
1164 # output if requested
1165
1165
1166 if timestamp:
1166 if timestamp:
1167 # disable timestamping for the previous history, since we've
1167 # disable timestamping for the previous history, since we've
1168 # lost those already (no time machine here).
1168 # lost those already (no time machine here).
1169 logger.timestamp = False
1169 logger.timestamp = False
1170
1170
1171 if log_raw_input:
1171 if log_raw_input:
1172 input_hist = self.shell.input_hist_raw
1172 input_hist = self.shell.input_hist_raw
1173 else:
1173 else:
1174 input_hist = self.shell.input_hist
1174 input_hist = self.shell.input_hist
1175
1175
1176 if log_output:
1176 if log_output:
1177 log_write = logger.log_write
1177 log_write = logger.log_write
1178 output_hist = self.shell.output_hist
1178 output_hist = self.shell.output_hist
1179 for n in range(1,len(input_hist)-1):
1179 for n in range(1,len(input_hist)-1):
1180 log_write(input_hist[n].rstrip())
1180 log_write(input_hist[n].rstrip())
1181 if n in output_hist:
1181 if n in output_hist:
1182 log_write(repr(output_hist[n]),'output')
1182 log_write(repr(output_hist[n]),'output')
1183 else:
1183 else:
1184 logger.log_write(input_hist[1:])
1184 logger.log_write(input_hist[1:])
1185 if timestamp:
1185 if timestamp:
1186 # re-enable timestamping
1186 # re-enable timestamping
1187 logger.timestamp = True
1187 logger.timestamp = True
1188
1188
1189 print ('Activating auto-logging. '
1189 print ('Activating auto-logging. '
1190 'Current session state plus future input saved.')
1190 'Current session state plus future input saved.')
1191 logger.logstate()
1191 logger.logstate()
1192
1192
1193 def magic_logstop(self,parameter_s=''):
1193 def magic_logstop(self,parameter_s=''):
1194 """Fully stop logging and close log file.
1194 """Fully stop logging and close log file.
1195
1195
1196 In order to start logging again, a new %logstart call needs to be made,
1196 In order to start logging again, a new %logstart call needs to be made,
1197 possibly (though not necessarily) with a new filename, mode and other
1197 possibly (though not necessarily) with a new filename, mode and other
1198 options."""
1198 options."""
1199 self.logger.logstop()
1199 self.logger.logstop()
1200
1200
1201 def magic_logoff(self,parameter_s=''):
1201 def magic_logoff(self,parameter_s=''):
1202 """Temporarily stop logging.
1202 """Temporarily stop logging.
1203
1203
1204 You must have previously started logging."""
1204 You must have previously started logging."""
1205 self.shell.logger.switch_log(0)
1205 self.shell.logger.switch_log(0)
1206
1206
1207 def magic_logon(self,parameter_s=''):
1207 def magic_logon(self,parameter_s=''):
1208 """Restart logging.
1208 """Restart logging.
1209
1209
1210 This function is for restarting logging which you've temporarily
1210 This function is for restarting logging which you've temporarily
1211 stopped with %logoff. For starting logging for the first time, you
1211 stopped with %logoff. For starting logging for the first time, you
1212 must use the %logstart function, which allows you to specify an
1212 must use the %logstart function, which allows you to specify an
1213 optional log filename."""
1213 optional log filename."""
1214
1214
1215 self.shell.logger.switch_log(1)
1215 self.shell.logger.switch_log(1)
1216
1216
1217 def magic_logstate(self,parameter_s=''):
1217 def magic_logstate(self,parameter_s=''):
1218 """Print the status of the logging system."""
1218 """Print the status of the logging system."""
1219
1219
1220 self.shell.logger.logstate()
1220 self.shell.logger.logstate()
1221
1221
1222 def magic_pdb(self, parameter_s=''):
1222 def magic_pdb(self, parameter_s=''):
1223 """Control the automatic calling of the pdb interactive debugger.
1223 """Control the automatic calling of the pdb interactive debugger.
1224
1224
1225 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1225 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1226 argument it works as a toggle.
1226 argument it works as a toggle.
1227
1227
1228 When an exception is triggered, IPython can optionally call the
1228 When an exception is triggered, IPython can optionally call the
1229 interactive pdb debugger after the traceback printout. %pdb toggles
1229 interactive pdb debugger after the traceback printout. %pdb toggles
1230 this feature on and off.
1230 this feature on and off.
1231
1231
1232 The initial state of this feature is set in your ipythonrc
1232 The initial state of this feature is set in your ipythonrc
1233 configuration file (the variable is called 'pdb').
1233 configuration file (the variable is called 'pdb').
1234
1234
1235 If you want to just activate the debugger AFTER an exception has fired,
1235 If you want to just activate the debugger AFTER an exception has fired,
1236 without having to type '%pdb on' and rerunning your code, you can use
1236 without having to type '%pdb on' and rerunning your code, you can use
1237 the %debug magic."""
1237 the %debug magic."""
1238
1238
1239 par = parameter_s.strip().lower()
1239 par = parameter_s.strip().lower()
1240
1240
1241 if par:
1241 if par:
1242 try:
1242 try:
1243 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1243 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1244 except KeyError:
1244 except KeyError:
1245 print ('Incorrect argument. Use on/1, off/0, '
1245 print ('Incorrect argument. Use on/1, off/0, '
1246 'or nothing for a toggle.')
1246 'or nothing for a toggle.')
1247 return
1247 return
1248 else:
1248 else:
1249 # toggle
1249 # toggle
1250 new_pdb = not self.shell.call_pdb
1250 new_pdb = not self.shell.call_pdb
1251
1251
1252 # set on the shell
1252 # set on the shell
1253 self.shell.call_pdb = new_pdb
1253 self.shell.call_pdb = new_pdb
1254 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1254 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1255
1255
1256 def magic_debug(self, parameter_s=''):
1256 def magic_debug(self, parameter_s=''):
1257 """Activate the interactive debugger in post-mortem mode.
1257 """Activate the interactive debugger in post-mortem mode.
1258
1258
1259 If an exception has just occurred, this lets you inspect its stack
1259 If an exception has just occurred, this lets you inspect its stack
1260 frames interactively. Note that this will always work only on the last
1260 frames interactively. Note that this will always work only on the last
1261 traceback that occurred, so you must call this quickly after an
1261 traceback that occurred, so you must call this quickly after an
1262 exception that you wish to inspect has fired, because if another one
1262 exception that you wish to inspect has fired, because if another one
1263 occurs, it clobbers the previous one.
1263 occurs, it clobbers the previous one.
1264
1264
1265 If you want IPython to automatically do this on every exception, see
1265 If you want IPython to automatically do this on every exception, see
1266 the %pdb magic for more details.
1266 the %pdb magic for more details.
1267 """
1267 """
1268
1268
1269 self.shell.debugger(force=True)
1269 self.shell.debugger(force=True)
1270
1270
1271 @testdec.skip_doctest
1271 @testdec.skip_doctest
1272 def magic_prun(self, parameter_s ='',user_mode=1,
1272 def magic_prun(self, parameter_s ='',user_mode=1,
1273 opts=None,arg_lst=None,prog_ns=None):
1273 opts=None,arg_lst=None,prog_ns=None):
1274
1274
1275 """Run a statement through the python code profiler.
1275 """Run a statement through the python code profiler.
1276
1276
1277 Usage:
1277 Usage:
1278 %prun [options] statement
1278 %prun [options] statement
1279
1279
1280 The given statement (which doesn't require quote marks) is run via the
1280 The given statement (which doesn't require quote marks) is run via the
1281 python profiler in a manner similar to the profile.run() function.
1281 python profiler in a manner similar to the profile.run() function.
1282 Namespaces are internally managed to work correctly; profile.run
1282 Namespaces are internally managed to work correctly; profile.run
1283 cannot be used in IPython because it makes certain assumptions about
1283 cannot be used in IPython because it makes certain assumptions about
1284 namespaces which do not hold under IPython.
1284 namespaces which do not hold under IPython.
1285
1285
1286 Options:
1286 Options:
1287
1287
1288 -l <limit>: you can place restrictions on what or how much of the
1288 -l <limit>: you can place restrictions on what or how much of the
1289 profile gets printed. The limit value can be:
1289 profile gets printed. The limit value can be:
1290
1290
1291 * A string: only information for function names containing this string
1291 * A string: only information for function names containing this string
1292 is printed.
1292 is printed.
1293
1293
1294 * An integer: only these many lines are printed.
1294 * An integer: only these many lines are printed.
1295
1295
1296 * A float (between 0 and 1): this fraction of the report is printed
1296 * A float (between 0 and 1): this fraction of the report is printed
1297 (for example, use a limit of 0.4 to see the topmost 40% only).
1297 (for example, use a limit of 0.4 to see the topmost 40% only).
1298
1298
1299 You can combine several limits with repeated use of the option. For
1299 You can combine several limits with repeated use of the option. For
1300 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1300 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1301 information about class constructors.
1301 information about class constructors.
1302
1302
1303 -r: return the pstats.Stats object generated by the profiling. This
1303 -r: return the pstats.Stats object generated by the profiling. This
1304 object has all the information about the profile in it, and you can
1304 object has all the information about the profile in it, and you can
1305 later use it for further analysis or in other functions.
1305 later use it for further analysis or in other functions.
1306
1306
1307 -s <key>: sort profile by given key. You can provide more than one key
1307 -s <key>: sort profile by given key. You can provide more than one key
1308 by using the option several times: '-s key1 -s key2 -s key3...'. The
1308 by using the option several times: '-s key1 -s key2 -s key3...'. The
1309 default sorting key is 'time'.
1309 default sorting key is 'time'.
1310
1310
1311 The following is copied verbatim from the profile documentation
1311 The following is copied verbatim from the profile documentation
1312 referenced below:
1312 referenced below:
1313
1313
1314 When more than one key is provided, additional keys are used as
1314 When more than one key is provided, additional keys are used as
1315 secondary criteria when the there is equality in all keys selected
1315 secondary criteria when the there is equality in all keys selected
1316 before them.
1316 before them.
1317
1317
1318 Abbreviations can be used for any key names, as long as the
1318 Abbreviations can be used for any key names, as long as the
1319 abbreviation is unambiguous. The following are the keys currently
1319 abbreviation is unambiguous. The following are the keys currently
1320 defined:
1320 defined:
1321
1321
1322 Valid Arg Meaning
1322 Valid Arg Meaning
1323 "calls" call count
1323 "calls" call count
1324 "cumulative" cumulative time
1324 "cumulative" cumulative time
1325 "file" file name
1325 "file" file name
1326 "module" file name
1326 "module" file name
1327 "pcalls" primitive call count
1327 "pcalls" primitive call count
1328 "line" line number
1328 "line" line number
1329 "name" function name
1329 "name" function name
1330 "nfl" name/file/line
1330 "nfl" name/file/line
1331 "stdname" standard name
1331 "stdname" standard name
1332 "time" internal time
1332 "time" internal time
1333
1333
1334 Note that all sorts on statistics are in descending order (placing
1334 Note that all sorts on statistics are in descending order (placing
1335 most time consuming items first), where as name, file, and line number
1335 most time consuming items first), where as name, file, and line number
1336 searches are in ascending order (i.e., alphabetical). The subtle
1336 searches are in ascending order (i.e., alphabetical). The subtle
1337 distinction between "nfl" and "stdname" is that the standard name is a
1337 distinction between "nfl" and "stdname" is that the standard name is a
1338 sort of the name as printed, which means that the embedded line
1338 sort of the name as printed, which means that the embedded line
1339 numbers get compared in an odd way. For example, lines 3, 20, and 40
1339 numbers get compared in an odd way. For example, lines 3, 20, and 40
1340 would (if the file names were the same) appear in the string order
1340 would (if the file names were the same) appear in the string order
1341 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1341 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1342 line numbers. In fact, sort_stats("nfl") is the same as
1342 line numbers. In fact, sort_stats("nfl") is the same as
1343 sort_stats("name", "file", "line").
1343 sort_stats("name", "file", "line").
1344
1344
1345 -T <filename>: save profile results as shown on screen to a text
1345 -T <filename>: save profile results as shown on screen to a text
1346 file. The profile is still shown on screen.
1346 file. The profile is still shown on screen.
1347
1347
1348 -D <filename>: save (via dump_stats) profile statistics to given
1348 -D <filename>: save (via dump_stats) profile statistics to given
1349 filename. This data is in a format understod by the pstats module, and
1349 filename. This data is in a format understod by the pstats module, and
1350 is generated by a call to the dump_stats() method of profile
1350 is generated by a call to the dump_stats() method of profile
1351 objects. The profile is still shown on screen.
1351 objects. The profile is still shown on screen.
1352
1352
1353 If you want to run complete programs under the profiler's control, use
1353 If you want to run complete programs under the profiler's control, use
1354 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1354 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1355 contains profiler specific options as described here.
1355 contains profiler specific options as described here.
1356
1356
1357 You can read the complete documentation for the profile module with::
1357 You can read the complete documentation for the profile module with::
1358
1358
1359 In [1]: import profile; profile.help()
1359 In [1]: import profile; profile.help()
1360 """
1360 """
1361
1361
1362 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1362 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1363 # protect user quote marks
1363 # protect user quote marks
1364 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1364 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1365
1365
1366 if user_mode: # regular user call
1366 if user_mode: # regular user call
1367 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1367 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1368 list_all=1)
1368 list_all=1)
1369 namespace = self.shell.user_ns
1369 namespace = self.shell.user_ns
1370 else: # called to run a program by %run -p
1370 else: # called to run a program by %run -p
1371 try:
1371 try:
1372 filename = get_py_filename(arg_lst[0])
1372 filename = get_py_filename(arg_lst[0])
1373 except IOError,msg:
1373 except IOError,msg:
1374 error(msg)
1374 error(msg)
1375 return
1375 return
1376
1376
1377 arg_str = 'execfile(filename,prog_ns)'
1377 arg_str = 'execfile(filename,prog_ns)'
1378 namespace = locals()
1378 namespace = locals()
1379
1379
1380 opts.merge(opts_def)
1380 opts.merge(opts_def)
1381
1381
1382 prof = profile.Profile()
1382 prof = profile.Profile()
1383 try:
1383 try:
1384 prof = prof.runctx(arg_str,namespace,namespace)
1384 prof = prof.runctx(arg_str,namespace,namespace)
1385 sys_exit = ''
1385 sys_exit = ''
1386 except SystemExit:
1386 except SystemExit:
1387 sys_exit = """*** SystemExit exception caught in code being profiled."""
1387 sys_exit = """*** SystemExit exception caught in code being profiled."""
1388
1388
1389 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1389 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1390
1390
1391 lims = opts.l
1391 lims = opts.l
1392 if lims:
1392 if lims:
1393 lims = [] # rebuild lims with ints/floats/strings
1393 lims = [] # rebuild lims with ints/floats/strings
1394 for lim in opts.l:
1394 for lim in opts.l:
1395 try:
1395 try:
1396 lims.append(int(lim))
1396 lims.append(int(lim))
1397 except ValueError:
1397 except ValueError:
1398 try:
1398 try:
1399 lims.append(float(lim))
1399 lims.append(float(lim))
1400 except ValueError:
1400 except ValueError:
1401 lims.append(lim)
1401 lims.append(lim)
1402
1402
1403 # Trap output.
1403 # Trap output.
1404 stdout_trap = StringIO()
1404 stdout_trap = StringIO()
1405
1405
1406 if hasattr(stats,'stream'):
1406 if hasattr(stats,'stream'):
1407 # In newer versions of python, the stats object has a 'stream'
1407 # In newer versions of python, the stats object has a 'stream'
1408 # attribute to write into.
1408 # attribute to write into.
1409 stats.stream = stdout_trap
1409 stats.stream = stdout_trap
1410 stats.print_stats(*lims)
1410 stats.print_stats(*lims)
1411 else:
1411 else:
1412 # For older versions, we manually redirect stdout during printing
1412 # For older versions, we manually redirect stdout during printing
1413 sys_stdout = sys.stdout
1413 sys_stdout = sys.stdout
1414 try:
1414 try:
1415 sys.stdout = stdout_trap
1415 sys.stdout = stdout_trap
1416 stats.print_stats(*lims)
1416 stats.print_stats(*lims)
1417 finally:
1417 finally:
1418 sys.stdout = sys_stdout
1418 sys.stdout = sys_stdout
1419
1419
1420 output = stdout_trap.getvalue()
1420 output = stdout_trap.getvalue()
1421 output = output.rstrip()
1421 output = output.rstrip()
1422
1422
1423 page(output,screen_lines=self.shell.rc.screen_length)
1423 page(output,screen_lines=self.shell.rc.screen_length)
1424 print sys_exit,
1424 print sys_exit,
1425
1425
1426 dump_file = opts.D[0]
1426 dump_file = opts.D[0]
1427 text_file = opts.T[0]
1427 text_file = opts.T[0]
1428 if dump_file:
1428 if dump_file:
1429 prof.dump_stats(dump_file)
1429 prof.dump_stats(dump_file)
1430 print '\n*** Profile stats marshalled to file',\
1430 print '\n*** Profile stats marshalled to file',\
1431 `dump_file`+'.',sys_exit
1431 `dump_file`+'.',sys_exit
1432 if text_file:
1432 if text_file:
1433 pfile = file(text_file,'w')
1433 pfile = file(text_file,'w')
1434 pfile.write(output)
1434 pfile.write(output)
1435 pfile.close()
1435 pfile.close()
1436 print '\n*** Profile printout saved to text file',\
1436 print '\n*** Profile printout saved to text file',\
1437 `text_file`+'.',sys_exit
1437 `text_file`+'.',sys_exit
1438
1438
1439 if opts.has_key('r'):
1439 if opts.has_key('r'):
1440 return stats
1440 return stats
1441 else:
1441 else:
1442 return None
1442 return None
1443
1443
1444 @testdec.skip_doctest
1444 @testdec.skip_doctest
1445 def magic_run(self, parameter_s ='',runner=None,
1445 def magic_run(self, parameter_s ='',runner=None,
1446 file_finder=get_py_filename):
1446 file_finder=get_py_filename):
1447 """Run the named file inside IPython as a program.
1447 """Run the named file inside IPython as a program.
1448
1448
1449 Usage:\\
1449 Usage:\\
1450 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1450 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1451
1451
1452 Parameters after the filename are passed as command-line arguments to
1452 Parameters after the filename are passed as command-line arguments to
1453 the program (put in sys.argv). Then, control returns to IPython's
1453 the program (put in sys.argv). Then, control returns to IPython's
1454 prompt.
1454 prompt.
1455
1455
1456 This is similar to running at a system prompt:\\
1456 This is similar to running at a system prompt:\\
1457 $ python file args\\
1457 $ python file args\\
1458 but with the advantage of giving you IPython's tracebacks, and of
1458 but with the advantage of giving you IPython's tracebacks, and of
1459 loading all variables into your interactive namespace for further use
1459 loading all variables into your interactive namespace for further use
1460 (unless -p is used, see below).
1460 (unless -p is used, see below).
1461
1461
1462 The file is executed in a namespace initially consisting only of
1462 The file is executed in a namespace initially consisting only of
1463 __name__=='__main__' and sys.argv constructed as indicated. It thus
1463 __name__=='__main__' and sys.argv constructed as indicated. It thus
1464 sees its environment as if it were being run as a stand-alone program
1464 sees its environment as if it were being run as a stand-alone program
1465 (except for sharing global objects such as previously imported
1465 (except for sharing global objects such as previously imported
1466 modules). But after execution, the IPython interactive namespace gets
1466 modules). But after execution, the IPython interactive namespace gets
1467 updated with all variables defined in the program (except for __name__
1467 updated with all variables defined in the program (except for __name__
1468 and sys.argv). This allows for very convenient loading of code for
1468 and sys.argv). This allows for very convenient loading of code for
1469 interactive work, while giving each program a 'clean sheet' to run in.
1469 interactive work, while giving each program a 'clean sheet' to run in.
1470
1470
1471 Options:
1471 Options:
1472
1472
1473 -n: __name__ is NOT set to '__main__', but to the running file's name
1473 -n: __name__ is NOT set to '__main__', but to the running file's name
1474 without extension (as python does under import). This allows running
1474 without extension (as python does under import). This allows running
1475 scripts and reloading the definitions in them without calling code
1475 scripts and reloading the definitions in them without calling code
1476 protected by an ' if __name__ == "__main__" ' clause.
1476 protected by an ' if __name__ == "__main__" ' clause.
1477
1477
1478 -i: run the file in IPython's namespace instead of an empty one. This
1478 -i: run the file in IPython's namespace instead of an empty one. This
1479 is useful if you are experimenting with code written in a text editor
1479 is useful if you are experimenting with code written in a text editor
1480 which depends on variables defined interactively.
1480 which depends on variables defined interactively.
1481
1481
1482 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1482 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1483 being run. This is particularly useful if IPython is being used to
1483 being run. This is particularly useful if IPython is being used to
1484 run unittests, which always exit with a sys.exit() call. In such
1484 run unittests, which always exit with a sys.exit() call. In such
1485 cases you are interested in the output of the test results, not in
1485 cases you are interested in the output of the test results, not in
1486 seeing a traceback of the unittest module.
1486 seeing a traceback of the unittest module.
1487
1487
1488 -t: print timing information at the end of the run. IPython will give
1488 -t: print timing information at the end of the run. IPython will give
1489 you an estimated CPU time consumption for your script, which under
1489 you an estimated CPU time consumption for your script, which under
1490 Unix uses the resource module to avoid the wraparound problems of
1490 Unix uses the resource module to avoid the wraparound problems of
1491 time.clock(). Under Unix, an estimate of time spent on system tasks
1491 time.clock(). Under Unix, an estimate of time spent on system tasks
1492 is also given (for Windows platforms this is reported as 0.0).
1492 is also given (for Windows platforms this is reported as 0.0).
1493
1493
1494 If -t is given, an additional -N<N> option can be given, where <N>
1494 If -t is given, an additional -N<N> option can be given, where <N>
1495 must be an integer indicating how many times you want the script to
1495 must be an integer indicating how many times you want the script to
1496 run. The final timing report will include total and per run results.
1496 run. The final timing report will include total and per run results.
1497
1497
1498 For example (testing the script uniq_stable.py):
1498 For example (testing the script uniq_stable.py):
1499
1499
1500 In [1]: run -t uniq_stable
1500 In [1]: run -t uniq_stable
1501
1501
1502 IPython CPU timings (estimated):\\
1502 IPython CPU timings (estimated):\\
1503 User : 0.19597 s.\\
1503 User : 0.19597 s.\\
1504 System: 0.0 s.\\
1504 System: 0.0 s.\\
1505
1505
1506 In [2]: run -t -N5 uniq_stable
1506 In [2]: run -t -N5 uniq_stable
1507
1507
1508 IPython CPU timings (estimated):\\
1508 IPython CPU timings (estimated):\\
1509 Total runs performed: 5\\
1509 Total runs performed: 5\\
1510 Times : Total Per run\\
1510 Times : Total Per run\\
1511 User : 0.910862 s, 0.1821724 s.\\
1511 User : 0.910862 s, 0.1821724 s.\\
1512 System: 0.0 s, 0.0 s.
1512 System: 0.0 s, 0.0 s.
1513
1513
1514 -d: run your program under the control of pdb, the Python debugger.
1514 -d: run your program under the control of pdb, the Python debugger.
1515 This allows you to execute your program step by step, watch variables,
1515 This allows you to execute your program step by step, watch variables,
1516 etc. Internally, what IPython does is similar to calling:
1516 etc. Internally, what IPython does is similar to calling:
1517
1517
1518 pdb.run('execfile("YOURFILENAME")')
1518 pdb.run('execfile("YOURFILENAME")')
1519
1519
1520 with a breakpoint set on line 1 of your file. You can change the line
1520 with a breakpoint set on line 1 of your file. You can change the line
1521 number for this automatic breakpoint to be <N> by using the -bN option
1521 number for this automatic breakpoint to be <N> by using the -bN option
1522 (where N must be an integer). For example:
1522 (where N must be an integer). For example:
1523
1523
1524 %run -d -b40 myscript
1524 %run -d -b40 myscript
1525
1525
1526 will set the first breakpoint at line 40 in myscript.py. Note that
1526 will set the first breakpoint at line 40 in myscript.py. Note that
1527 the first breakpoint must be set on a line which actually does
1527 the first breakpoint must be set on a line which actually does
1528 something (not a comment or docstring) for it to stop execution.
1528 something (not a comment or docstring) for it to stop execution.
1529
1529
1530 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1530 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1531 first enter 'c' (without qoutes) to start execution up to the first
1531 first enter 'c' (without qoutes) to start execution up to the first
1532 breakpoint.
1532 breakpoint.
1533
1533
1534 Entering 'help' gives information about the use of the debugger. You
1534 Entering 'help' gives information about the use of the debugger. You
1535 can easily see pdb's full documentation with "import pdb;pdb.help()"
1535 can easily see pdb's full documentation with "import pdb;pdb.help()"
1536 at a prompt.
1536 at a prompt.
1537
1537
1538 -p: run program under the control of the Python profiler module (which
1538 -p: run program under the control of the Python profiler module (which
1539 prints a detailed report of execution times, function calls, etc).
1539 prints a detailed report of execution times, function calls, etc).
1540
1540
1541 You can pass other options after -p which affect the behavior of the
1541 You can pass other options after -p which affect the behavior of the
1542 profiler itself. See the docs for %prun for details.
1542 profiler itself. See the docs for %prun for details.
1543
1543
1544 In this mode, the program's variables do NOT propagate back to the
1544 In this mode, the program's variables do NOT propagate back to the
1545 IPython interactive namespace (because they remain in the namespace
1545 IPython interactive namespace (because they remain in the namespace
1546 where the profiler executes them).
1546 where the profiler executes them).
1547
1547
1548 Internally this triggers a call to %prun, see its documentation for
1548 Internally this triggers a call to %prun, see its documentation for
1549 details on the options available specifically for profiling.
1549 details on the options available specifically for profiling.
1550
1550
1551 There is one special usage for which the text above doesn't apply:
1551 There is one special usage for which the text above doesn't apply:
1552 if the filename ends with .ipy, the file is run as ipython script,
1552 if the filename ends with .ipy, the file is run as ipython script,
1553 just as if the commands were written on IPython prompt.
1553 just as if the commands were written on IPython prompt.
1554 """
1554 """
1555
1555
1556 # get arguments and set sys.argv for program to be run.
1556 # get arguments and set sys.argv for program to be run.
1557 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1557 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1558 mode='list',list_all=1)
1558 mode='list',list_all=1)
1559
1559
1560 try:
1560 try:
1561 filename = file_finder(arg_lst[0])
1561 filename = file_finder(arg_lst[0])
1562 except IndexError:
1562 except IndexError:
1563 warn('you must provide at least a filename.')
1563 warn('you must provide at least a filename.')
1564 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1564 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1565 return
1565 return
1566 except IOError,msg:
1566 except IOError,msg:
1567 error(msg)
1567 error(msg)
1568 return
1568 return
1569
1569
1570 if filename.lower().endswith('.ipy'):
1570 if filename.lower().endswith('.ipy'):
1571 self.api.runlines(open(filename).read())
1571 self.api.runlines(open(filename).read())
1572 return
1572 return
1573
1573
1574 # Control the response to exit() calls made by the script being run
1574 # Control the response to exit() calls made by the script being run
1575 exit_ignore = opts.has_key('e')
1575 exit_ignore = opts.has_key('e')
1576
1576
1577 # Make sure that the running script gets a proper sys.argv as if it
1577 # Make sure that the running script gets a proper sys.argv as if it
1578 # were run from a system shell.
1578 # were run from a system shell.
1579 save_argv = sys.argv # save it for later restoring
1579 save_argv = sys.argv # save it for later restoring
1580 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1580 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1581
1581
1582 if opts.has_key('i'):
1582 if opts.has_key('i'):
1583 # Run in user's interactive namespace
1583 # Run in user's interactive namespace
1584 prog_ns = self.shell.user_ns
1584 prog_ns = self.shell.user_ns
1585 __name__save = self.shell.user_ns['__name__']
1585 __name__save = self.shell.user_ns['__name__']
1586 prog_ns['__name__'] = '__main__'
1586 prog_ns['__name__'] = '__main__'
1587 main_mod = FakeModule(prog_ns)
1587 main_mod = self.shell.new_main_mod(prog_ns)
1588 else:
1588 else:
1589 # Run in a fresh, empty namespace
1589 # Run in a fresh, empty namespace
1590 if opts.has_key('n'):
1590 if opts.has_key('n'):
1591 name = os.path.splitext(os.path.basename(filename))[0]
1591 name = os.path.splitext(os.path.basename(filename))[0]
1592 else:
1592 else:
1593 name = '__main__'
1593 name = '__main__'
1594 main_mod = FakeModule()
1594
1595 main_mod = self.shell.new_main_mod()
1595 prog_ns = main_mod.__dict__
1596 prog_ns = main_mod.__dict__
1596 prog_ns['__name__'] = name
1597 prog_ns['__name__'] = name
1597
1598 # The shell MUST hold a reference to main_mod so after %run exits,
1599 # the python deletion mechanism doesn't zero it out (leaving
1600 # dangling references). However, we should drop old versions of
1601 # main_mod. There is now a proper API to manage this caching in
1602 # the main shell object, we use that.
1603 self.shell.cache_main_mod(main_mod)
1604
1598
1605 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1599 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1606 # set the __file__ global in the script's namespace
1600 # set the __file__ global in the script's namespace
1607 prog_ns['__file__'] = filename
1601 prog_ns['__file__'] = filename
1608
1602
1609 # pickle fix. See iplib for an explanation. But we need to make sure
1603 # pickle fix. See iplib for an explanation. But we need to make sure
1610 # that, if we overwrite __main__, we replace it at the end
1604 # that, if we overwrite __main__, we replace it at the end
1611 main_mod_name = prog_ns['__name__']
1605 main_mod_name = prog_ns['__name__']
1612
1606
1613 if main_mod_name == '__main__':
1607 if main_mod_name == '__main__':
1614 restore_main = sys.modules['__main__']
1608 restore_main = sys.modules['__main__']
1615 else:
1609 else:
1616 restore_main = False
1610 restore_main = False
1617
1611
1618 # This needs to be undone at the end to prevent holding references to
1612 # This needs to be undone at the end to prevent holding references to
1619 # every single object ever created.
1613 # every single object ever created.
1620 sys.modules[main_mod_name] = main_mod
1614 sys.modules[main_mod_name] = main_mod
1621
1615
1622 stats = None
1616 stats = None
1623 try:
1617 try:
1624 self.shell.savehist()
1618 self.shell.savehist()
1625
1619
1626 if opts.has_key('p'):
1620 if opts.has_key('p'):
1627 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1621 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1628 else:
1622 else:
1629 if opts.has_key('d'):
1623 if opts.has_key('d'):
1630 deb = Debugger.Pdb(self.shell.rc.colors)
1624 deb = Debugger.Pdb(self.shell.rc.colors)
1631 # reset Breakpoint state, which is moronically kept
1625 # reset Breakpoint state, which is moronically kept
1632 # in a class
1626 # in a class
1633 bdb.Breakpoint.next = 1
1627 bdb.Breakpoint.next = 1
1634 bdb.Breakpoint.bplist = {}
1628 bdb.Breakpoint.bplist = {}
1635 bdb.Breakpoint.bpbynumber = [None]
1629 bdb.Breakpoint.bpbynumber = [None]
1636 # Set an initial breakpoint to stop execution
1630 # Set an initial breakpoint to stop execution
1637 maxtries = 10
1631 maxtries = 10
1638 bp = int(opts.get('b',[1])[0])
1632 bp = int(opts.get('b',[1])[0])
1639 checkline = deb.checkline(filename,bp)
1633 checkline = deb.checkline(filename,bp)
1640 if not checkline:
1634 if not checkline:
1641 for bp in range(bp+1,bp+maxtries+1):
1635 for bp in range(bp+1,bp+maxtries+1):
1642 if deb.checkline(filename,bp):
1636 if deb.checkline(filename,bp):
1643 break
1637 break
1644 else:
1638 else:
1645 msg = ("\nI failed to find a valid line to set "
1639 msg = ("\nI failed to find a valid line to set "
1646 "a breakpoint\n"
1640 "a breakpoint\n"
1647 "after trying up to line: %s.\n"
1641 "after trying up to line: %s.\n"
1648 "Please set a valid breakpoint manually "
1642 "Please set a valid breakpoint manually "
1649 "with the -b option." % bp)
1643 "with the -b option." % bp)
1650 error(msg)
1644 error(msg)
1651 return
1645 return
1652 # if we find a good linenumber, set the breakpoint
1646 # if we find a good linenumber, set the breakpoint
1653 deb.do_break('%s:%s' % (filename,bp))
1647 deb.do_break('%s:%s' % (filename,bp))
1654 # Start file run
1648 # Start file run
1655 print "NOTE: Enter 'c' at the",
1649 print "NOTE: Enter 'c' at the",
1656 print "%s prompt to start your script." % deb.prompt
1650 print "%s prompt to start your script." % deb.prompt
1657 try:
1651 try:
1658 deb.run('execfile("%s")' % filename,prog_ns)
1652 deb.run('execfile("%s")' % filename,prog_ns)
1659
1653
1660 except:
1654 except:
1661 etype, value, tb = sys.exc_info()
1655 etype, value, tb = sys.exc_info()
1662 # Skip three frames in the traceback: the %run one,
1656 # Skip three frames in the traceback: the %run one,
1663 # one inside bdb.py, and the command-line typed by the
1657 # one inside bdb.py, and the command-line typed by the
1664 # user (run by exec in pdb itself).
1658 # user (run by exec in pdb itself).
1665 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1659 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1666 else:
1660 else:
1667 if runner is None:
1661 if runner is None:
1668 runner = self.shell.safe_execfile
1662 runner = self.shell.safe_execfile
1669 if opts.has_key('t'):
1663 if opts.has_key('t'):
1670 # timed execution
1664 # timed execution
1671 try:
1665 try:
1672 nruns = int(opts['N'][0])
1666 nruns = int(opts['N'][0])
1673 if nruns < 1:
1667 if nruns < 1:
1674 error('Number of runs must be >=1')
1668 error('Number of runs must be >=1')
1675 return
1669 return
1676 except (KeyError):
1670 except (KeyError):
1677 nruns = 1
1671 nruns = 1
1678 if nruns == 1:
1672 if nruns == 1:
1679 t0 = clock2()
1673 t0 = clock2()
1680 runner(filename,prog_ns,prog_ns,
1674 runner(filename,prog_ns,prog_ns,
1681 exit_ignore=exit_ignore)
1675 exit_ignore=exit_ignore)
1682 t1 = clock2()
1676 t1 = clock2()
1683 t_usr = t1[0]-t0[0]
1677 t_usr = t1[0]-t0[0]
1684 t_sys = t1[1]-t1[1]
1678 t_sys = t1[1]-t1[1]
1685 print "\nIPython CPU timings (estimated):"
1679 print "\nIPython CPU timings (estimated):"
1686 print " User : %10s s." % t_usr
1680 print " User : %10s s." % t_usr
1687 print " System: %10s s." % t_sys
1681 print " System: %10s s." % t_sys
1688 else:
1682 else:
1689 runs = range(nruns)
1683 runs = range(nruns)
1690 t0 = clock2()
1684 t0 = clock2()
1691 for nr in runs:
1685 for nr in runs:
1692 runner(filename,prog_ns,prog_ns,
1686 runner(filename,prog_ns,prog_ns,
1693 exit_ignore=exit_ignore)
1687 exit_ignore=exit_ignore)
1694 t1 = clock2()
1688 t1 = clock2()
1695 t_usr = t1[0]-t0[0]
1689 t_usr = t1[0]-t0[0]
1696 t_sys = t1[1]-t1[1]
1690 t_sys = t1[1]-t1[1]
1697 print "\nIPython CPU timings (estimated):"
1691 print "\nIPython CPU timings (estimated):"
1698 print "Total runs performed:",nruns
1692 print "Total runs performed:",nruns
1699 print " Times : %10s %10s" % ('Total','Per run')
1693 print " Times : %10s %10s" % ('Total','Per run')
1700 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1694 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1701 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1695 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1702
1696
1703 else:
1697 else:
1704 # regular execution
1698 # regular execution
1705 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1699 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1700
1706 if opts.has_key('i'):
1701 if opts.has_key('i'):
1707 self.shell.user_ns['__name__'] = __name__save
1702 self.shell.user_ns['__name__'] = __name__save
1708 else:
1703 else:
1704 # The shell MUST hold a reference to prog_ns so after %run
1705 # exits, the python deletion mechanism doesn't zero it out
1706 # (leaving dangling references).
1707 self.shell.cache_main_mod(prog_ns,filename)
1709 # update IPython interactive namespace
1708 # update IPython interactive namespace
1710 del prog_ns['__name__']
1709 del prog_ns['__name__']
1711 self.shell.user_ns.update(prog_ns)
1710 self.shell.user_ns.update(prog_ns)
1712 finally:
1711 finally:
1713 # Ensure key global structures are restored
1712 # Ensure key global structures are restored
1714 sys.argv = save_argv
1713 sys.argv = save_argv
1715 if restore_main:
1714 if restore_main:
1716 sys.modules['__main__'] = restore_main
1715 sys.modules['__main__'] = restore_main
1717 else:
1716 else:
1718 # Remove from sys.modules the reference to main_mod we'd
1717 # Remove from sys.modules the reference to main_mod we'd
1719 # added. Otherwise it will trap references to objects
1718 # added. Otherwise it will trap references to objects
1720 # contained therein.
1719 # contained therein.
1721 del sys.modules[main_mod_name]
1720 del sys.modules[main_mod_name]
1721
1722 self.shell.reloadhist()
1722 self.shell.reloadhist()
1723
1723
1724 return stats
1724 return stats
1725
1725
1726 def magic_runlog(self, parameter_s =''):
1726 def magic_runlog(self, parameter_s =''):
1727 """Run files as logs.
1727 """Run files as logs.
1728
1728
1729 Usage:\\
1729 Usage:\\
1730 %runlog file1 file2 ...
1730 %runlog file1 file2 ...
1731
1731
1732 Run the named files (treating them as log files) in sequence inside
1732 Run the named files (treating them as log files) in sequence inside
1733 the interpreter, and return to the prompt. This is much slower than
1733 the interpreter, and return to the prompt. This is much slower than
1734 %run because each line is executed in a try/except block, but it
1734 %run because each line is executed in a try/except block, but it
1735 allows running files with syntax errors in them.
1735 allows running files with syntax errors in them.
1736
1736
1737 Normally IPython will guess when a file is one of its own logfiles, so
1737 Normally IPython will guess when a file is one of its own logfiles, so
1738 you can typically use %run even for logs. This shorthand allows you to
1738 you can typically use %run even for logs. This shorthand allows you to
1739 force any file to be treated as a log file."""
1739 force any file to be treated as a log file."""
1740
1740
1741 for f in parameter_s.split():
1741 for f in parameter_s.split():
1742 self.shell.safe_execfile(f,self.shell.user_ns,
1742 self.shell.safe_execfile(f,self.shell.user_ns,
1743 self.shell.user_ns,islog=1)
1743 self.shell.user_ns,islog=1)
1744
1744
1745 @testdec.skip_doctest
1745 @testdec.skip_doctest
1746 def magic_timeit(self, parameter_s =''):
1746 def magic_timeit(self, parameter_s =''):
1747 """Time execution of a Python statement or expression
1747 """Time execution of a Python statement or expression
1748
1748
1749 Usage:\\
1749 Usage:\\
1750 %timeit [-n<N> -r<R> [-t|-c]] statement
1750 %timeit [-n<N> -r<R> [-t|-c]] statement
1751
1751
1752 Time execution of a Python statement or expression using the timeit
1752 Time execution of a Python statement or expression using the timeit
1753 module.
1753 module.
1754
1754
1755 Options:
1755 Options:
1756 -n<N>: execute the given statement <N> times in a loop. If this value
1756 -n<N>: execute the given statement <N> times in a loop. If this value
1757 is not given, a fitting value is chosen.
1757 is not given, a fitting value is chosen.
1758
1758
1759 -r<R>: repeat the loop iteration <R> times and take the best result.
1759 -r<R>: repeat the loop iteration <R> times and take the best result.
1760 Default: 3
1760 Default: 3
1761
1761
1762 -t: use time.time to measure the time, which is the default on Unix.
1762 -t: use time.time to measure the time, which is the default on Unix.
1763 This function measures wall time.
1763 This function measures wall time.
1764
1764
1765 -c: use time.clock to measure the time, which is the default on
1765 -c: use time.clock to measure the time, which is the default on
1766 Windows and measures wall time. On Unix, resource.getrusage is used
1766 Windows and measures wall time. On Unix, resource.getrusage is used
1767 instead and returns the CPU user time.
1767 instead and returns the CPU user time.
1768
1768
1769 -p<P>: use a precision of <P> digits to display the timing result.
1769 -p<P>: use a precision of <P> digits to display the timing result.
1770 Default: 3
1770 Default: 3
1771
1771
1772
1772
1773 Examples:
1773 Examples:
1774
1774
1775 In [1]: %timeit pass
1775 In [1]: %timeit pass
1776 10000000 loops, best of 3: 53.3 ns per loop
1776 10000000 loops, best of 3: 53.3 ns per loop
1777
1777
1778 In [2]: u = None
1778 In [2]: u = None
1779
1779
1780 In [3]: %timeit u is None
1780 In [3]: %timeit u is None
1781 10000000 loops, best of 3: 184 ns per loop
1781 10000000 loops, best of 3: 184 ns per loop
1782
1782
1783 In [4]: %timeit -r 4 u == None
1783 In [4]: %timeit -r 4 u == None
1784 1000000 loops, best of 4: 242 ns per loop
1784 1000000 loops, best of 4: 242 ns per loop
1785
1785
1786 In [5]: import time
1786 In [5]: import time
1787
1787
1788 In [6]: %timeit -n1 time.sleep(2)
1788 In [6]: %timeit -n1 time.sleep(2)
1789 1 loops, best of 3: 2 s per loop
1789 1 loops, best of 3: 2 s per loop
1790
1790
1791
1791
1792 The times reported by %timeit will be slightly higher than those
1792 The times reported by %timeit will be slightly higher than those
1793 reported by the timeit.py script when variables are accessed. This is
1793 reported by the timeit.py script when variables are accessed. This is
1794 due to the fact that %timeit executes the statement in the namespace
1794 due to the fact that %timeit executes the statement in the namespace
1795 of the shell, compared with timeit.py, which uses a single setup
1795 of the shell, compared with timeit.py, which uses a single setup
1796 statement to import function or create variables. Generally, the bias
1796 statement to import function or create variables. Generally, the bias
1797 does not matter as long as results from timeit.py are not mixed with
1797 does not matter as long as results from timeit.py are not mixed with
1798 those from %timeit."""
1798 those from %timeit."""
1799
1799
1800 import timeit
1800 import timeit
1801 import math
1801 import math
1802
1802
1803 units = [u"s", u"ms", u"\xb5s", u"ns"]
1803 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1804 # certain terminals. Until we figure out a robust way of
1805 # auto-detecting if the terminal can deal with it, use plain 'us' for
1806 # microseconds. I am really NOT happy about disabling the proper
1807 # 'micro' prefix, but crashing is worse... If anyone knows what the
1808 # right solution for this is, I'm all ears...
1809 #
1810 # Note: using
1811 #
1812 # s = u'\xb5'
1813 # s.encode(sys.getdefaultencoding())
1814 #
1815 # is not sufficient, as I've seen terminals where that fails but
1816 # print s
1817 #
1818 # succeeds
1819 #
1820 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1821
1822 #units = [u"s", u"ms",u'\xb5',"ns"]
1823 units = [u"s", u"ms",u'us',"ns"]
1824
1804 scaling = [1, 1e3, 1e6, 1e9]
1825 scaling = [1, 1e3, 1e6, 1e9]
1805
1826
1806 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1827 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1807 posix=False)
1828 posix=False)
1808 if stmt == "":
1829 if stmt == "":
1809 return
1830 return
1810 timefunc = timeit.default_timer
1831 timefunc = timeit.default_timer
1811 number = int(getattr(opts, "n", 0))
1832 number = int(getattr(opts, "n", 0))
1812 repeat = int(getattr(opts, "r", timeit.default_repeat))
1833 repeat = int(getattr(opts, "r", timeit.default_repeat))
1813 precision = int(getattr(opts, "p", 3))
1834 precision = int(getattr(opts, "p", 3))
1814 if hasattr(opts, "t"):
1835 if hasattr(opts, "t"):
1815 timefunc = time.time
1836 timefunc = time.time
1816 if hasattr(opts, "c"):
1837 if hasattr(opts, "c"):
1817 timefunc = clock
1838 timefunc = clock
1818
1839
1819 timer = timeit.Timer(timer=timefunc)
1840 timer = timeit.Timer(timer=timefunc)
1820 # this code has tight coupling to the inner workings of timeit.Timer,
1841 # this code has tight coupling to the inner workings of timeit.Timer,
1821 # but is there a better way to achieve that the code stmt has access
1842 # but is there a better way to achieve that the code stmt has access
1822 # to the shell namespace?
1843 # to the shell namespace?
1823
1844
1824 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1845 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1825 'setup': "pass"}
1846 'setup': "pass"}
1826 # Track compilation time so it can be reported if too long
1847 # Track compilation time so it can be reported if too long
1827 # Minimum time above which compilation time will be reported
1848 # Minimum time above which compilation time will be reported
1828 tc_min = 0.1
1849 tc_min = 0.1
1829
1850
1830 t0 = clock()
1851 t0 = clock()
1831 code = compile(src, "<magic-timeit>", "exec")
1852 code = compile(src, "<magic-timeit>", "exec")
1832 tc = clock()-t0
1853 tc = clock()-t0
1833
1854
1834 ns = {}
1855 ns = {}
1835 exec code in self.shell.user_ns, ns
1856 exec code in self.shell.user_ns, ns
1836 timer.inner = ns["inner"]
1857 timer.inner = ns["inner"]
1837
1858
1838 if number == 0:
1859 if number == 0:
1839 # determine number so that 0.2 <= total time < 2.0
1860 # determine number so that 0.2 <= total time < 2.0
1840 number = 1
1861 number = 1
1841 for i in range(1, 10):
1862 for i in range(1, 10):
1842 number *= 10
1843 if timer.timeit(number) >= 0.2:
1863 if timer.timeit(number) >= 0.2:
1844 break
1864 break
1865 number *= 10
1845
1866
1846 best = min(timer.repeat(repeat, number)) / number
1867 best = min(timer.repeat(repeat, number)) / number
1847
1868
1848 if best > 0.0:
1869 if best > 0.0:
1849 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1870 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1850 else:
1871 else:
1851 order = 3
1872 order = 3
1852 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1873 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1853 precision,
1874 precision,
1854 best * scaling[order],
1875 best * scaling[order],
1855 units[order])
1876 units[order])
1856 if tc > tc_min:
1877 if tc > tc_min:
1857 print "Compiler time: %.2f s" % tc
1878 print "Compiler time: %.2f s" % tc
1858
1879
1859 @testdec.skip_doctest
1880 @testdec.skip_doctest
1860 def magic_time(self,parameter_s = ''):
1881 def magic_time(self,parameter_s = ''):
1861 """Time execution of a Python statement or expression.
1882 """Time execution of a Python statement or expression.
1862
1883
1863 The CPU and wall clock times are printed, and the value of the
1884 The CPU and wall clock times are printed, and the value of the
1864 expression (if any) is returned. Note that under Win32, system time
1885 expression (if any) is returned. Note that under Win32, system time
1865 is always reported as 0, since it can not be measured.
1886 is always reported as 0, since it can not be measured.
1866
1887
1867 This function provides very basic timing functionality. In Python
1888 This function provides very basic timing functionality. In Python
1868 2.3, the timeit module offers more control and sophistication, so this
1889 2.3, the timeit module offers more control and sophistication, so this
1869 could be rewritten to use it (patches welcome).
1890 could be rewritten to use it (patches welcome).
1870
1891
1871 Some examples:
1892 Some examples:
1872
1893
1873 In [1]: time 2**128
1894 In [1]: time 2**128
1874 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1895 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1875 Wall time: 0.00
1896 Wall time: 0.00
1876 Out[1]: 340282366920938463463374607431768211456L
1897 Out[1]: 340282366920938463463374607431768211456L
1877
1898
1878 In [2]: n = 1000000
1899 In [2]: n = 1000000
1879
1900
1880 In [3]: time sum(range(n))
1901 In [3]: time sum(range(n))
1881 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1902 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1882 Wall time: 1.37
1903 Wall time: 1.37
1883 Out[3]: 499999500000L
1904 Out[3]: 499999500000L
1884
1905
1885 In [4]: time print 'hello world'
1906 In [4]: time print 'hello world'
1886 hello world
1907 hello world
1887 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1908 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1888 Wall time: 0.00
1909 Wall time: 0.00
1889
1910
1890 Note that the time needed by Python to compile the given expression
1911 Note that the time needed by Python to compile the given expression
1891 will be reported if it is more than 0.1s. In this example, the
1912 will be reported if it is more than 0.1s. In this example, the
1892 actual exponentiation is done by Python at compilation time, so while
1913 actual exponentiation is done by Python at compilation time, so while
1893 the expression can take a noticeable amount of time to compute, that
1914 the expression can take a noticeable amount of time to compute, that
1894 time is purely due to the compilation:
1915 time is purely due to the compilation:
1895
1916
1896 In [5]: time 3**9999;
1917 In [5]: time 3**9999;
1897 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1918 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1898 Wall time: 0.00 s
1919 Wall time: 0.00 s
1899
1920
1900 In [6]: time 3**999999;
1921 In [6]: time 3**999999;
1901 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1922 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1902 Wall time: 0.00 s
1923 Wall time: 0.00 s
1903 Compiler : 0.78 s
1924 Compiler : 0.78 s
1904 """
1925 """
1905
1926
1906 # fail immediately if the given expression can't be compiled
1927 # fail immediately if the given expression can't be compiled
1907
1928
1908 expr = self.shell.prefilter(parameter_s,False)
1929 expr = self.shell.prefilter(parameter_s,False)
1909
1930
1910 # Minimum time above which compilation time will be reported
1931 # Minimum time above which compilation time will be reported
1911 tc_min = 0.1
1932 tc_min = 0.1
1912
1933
1913 try:
1934 try:
1914 mode = 'eval'
1935 mode = 'eval'
1915 t0 = clock()
1936 t0 = clock()
1916 code = compile(expr,'<timed eval>',mode)
1937 code = compile(expr,'<timed eval>',mode)
1917 tc = clock()-t0
1938 tc = clock()-t0
1918 except SyntaxError:
1939 except SyntaxError:
1919 mode = 'exec'
1940 mode = 'exec'
1920 t0 = clock()
1941 t0 = clock()
1921 code = compile(expr,'<timed exec>',mode)
1942 code = compile(expr,'<timed exec>',mode)
1922 tc = clock()-t0
1943 tc = clock()-t0
1923 # skew measurement as little as possible
1944 # skew measurement as little as possible
1924 glob = self.shell.user_ns
1945 glob = self.shell.user_ns
1925 clk = clock2
1946 clk = clock2
1926 wtime = time.time
1947 wtime = time.time
1927 # time execution
1948 # time execution
1928 wall_st = wtime()
1949 wall_st = wtime()
1929 if mode=='eval':
1950 if mode=='eval':
1930 st = clk()
1951 st = clk()
1931 out = eval(code,glob)
1952 out = eval(code,glob)
1932 end = clk()
1953 end = clk()
1933 else:
1954 else:
1934 st = clk()
1955 st = clk()
1935 exec code in glob
1956 exec code in glob
1936 end = clk()
1957 end = clk()
1937 out = None
1958 out = None
1938 wall_end = wtime()
1959 wall_end = wtime()
1939 # Compute actual times and report
1960 # Compute actual times and report
1940 wall_time = wall_end-wall_st
1961 wall_time = wall_end-wall_st
1941 cpu_user = end[0]-st[0]
1962 cpu_user = end[0]-st[0]
1942 cpu_sys = end[1]-st[1]
1963 cpu_sys = end[1]-st[1]
1943 cpu_tot = cpu_user+cpu_sys
1964 cpu_tot = cpu_user+cpu_sys
1944 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1965 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1945 (cpu_user,cpu_sys,cpu_tot)
1966 (cpu_user,cpu_sys,cpu_tot)
1946 print "Wall time: %.2f s" % wall_time
1967 print "Wall time: %.2f s" % wall_time
1947 if tc > tc_min:
1968 if tc > tc_min:
1948 print "Compiler : %.2f s" % tc
1969 print "Compiler : %.2f s" % tc
1949 return out
1970 return out
1950
1971
1951 @testdec.skip_doctest
1972 @testdec.skip_doctest
1952 def magic_macro(self,parameter_s = ''):
1973 def magic_macro(self,parameter_s = ''):
1953 """Define a set of input lines as a macro for future re-execution.
1974 """Define a set of input lines as a macro for future re-execution.
1954
1975
1955 Usage:\\
1976 Usage:\\
1956 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1977 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1957
1978
1958 Options:
1979 Options:
1959
1980
1960 -r: use 'raw' input. By default, the 'processed' history is used,
1981 -r: use 'raw' input. By default, the 'processed' history is used,
1961 so that magics are loaded in their transformed version to valid
1982 so that magics are loaded in their transformed version to valid
1962 Python. If this option is given, the raw input as typed as the
1983 Python. If this option is given, the raw input as typed as the
1963 command line is used instead.
1984 command line is used instead.
1964
1985
1965 This will define a global variable called `name` which is a string
1986 This will define a global variable called `name` which is a string
1966 made of joining the slices and lines you specify (n1,n2,... numbers
1987 made of joining the slices and lines you specify (n1,n2,... numbers
1967 above) from your input history into a single string. This variable
1988 above) from your input history into a single string. This variable
1968 acts like an automatic function which re-executes those lines as if
1989 acts like an automatic function which re-executes those lines as if
1969 you had typed them. You just type 'name' at the prompt and the code
1990 you had typed them. You just type 'name' at the prompt and the code
1970 executes.
1991 executes.
1971
1992
1972 The notation for indicating number ranges is: n1-n2 means 'use line
1993 The notation for indicating number ranges is: n1-n2 means 'use line
1973 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1994 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1974 using the lines numbered 5,6 and 7.
1995 using the lines numbered 5,6 and 7.
1975
1996
1976 Note: as a 'hidden' feature, you can also use traditional python slice
1997 Note: as a 'hidden' feature, you can also use traditional python slice
1977 notation, where N:M means numbers N through M-1.
1998 notation, where N:M means numbers N through M-1.
1978
1999
1979 For example, if your history contains (%hist prints it):
2000 For example, if your history contains (%hist prints it):
1980
2001
1981 44: x=1
2002 44: x=1
1982 45: y=3
2003 45: y=3
1983 46: z=x+y
2004 46: z=x+y
1984 47: print x
2005 47: print x
1985 48: a=5
2006 48: a=5
1986 49: print 'x',x,'y',y
2007 49: print 'x',x,'y',y
1987
2008
1988 you can create a macro with lines 44 through 47 (included) and line 49
2009 you can create a macro with lines 44 through 47 (included) and line 49
1989 called my_macro with:
2010 called my_macro with:
1990
2011
1991 In [55]: %macro my_macro 44-47 49
2012 In [55]: %macro my_macro 44-47 49
1992
2013
1993 Now, typing `my_macro` (without quotes) will re-execute all this code
2014 Now, typing `my_macro` (without quotes) will re-execute all this code
1994 in one pass.
2015 in one pass.
1995
2016
1996 You don't need to give the line-numbers in order, and any given line
2017 You don't need to give the line-numbers in order, and any given line
1997 number can appear multiple times. You can assemble macros with any
2018 number can appear multiple times. You can assemble macros with any
1998 lines from your input history in any order.
2019 lines from your input history in any order.
1999
2020
2000 The macro is a simple object which holds its value in an attribute,
2021 The macro is a simple object which holds its value in an attribute,
2001 but IPython's display system checks for macros and executes them as
2022 but IPython's display system checks for macros and executes them as
2002 code instead of printing them when you type their name.
2023 code instead of printing them when you type their name.
2003
2024
2004 You can view a macro's contents by explicitly printing it with:
2025 You can view a macro's contents by explicitly printing it with:
2005
2026
2006 'print macro_name'.
2027 'print macro_name'.
2007
2028
2008 For one-off cases which DON'T contain magic function calls in them you
2029 For one-off cases which DON'T contain magic function calls in them you
2009 can obtain similar results by explicitly executing slices from your
2030 can obtain similar results by explicitly executing slices from your
2010 input history with:
2031 input history with:
2011
2032
2012 In [60]: exec In[44:48]+In[49]"""
2033 In [60]: exec In[44:48]+In[49]"""
2013
2034
2014 opts,args = self.parse_options(parameter_s,'r',mode='list')
2035 opts,args = self.parse_options(parameter_s,'r',mode='list')
2015 if not args:
2036 if not args:
2016 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2037 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2017 macs.sort()
2038 macs.sort()
2018 return macs
2039 return macs
2019 if len(args) == 1:
2040 if len(args) == 1:
2020 raise UsageError(
2041 raise UsageError(
2021 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2042 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2022 name,ranges = args[0], args[1:]
2043 name,ranges = args[0], args[1:]
2023
2044
2024 #print 'rng',ranges # dbg
2045 #print 'rng',ranges # dbg
2025 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2046 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2026 macro = Macro(lines)
2047 macro = Macro(lines)
2027 self.shell.user_ns.update({name:macro})
2048 self.shell.user_ns.update({name:macro})
2028 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2049 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2029 print 'Macro contents:'
2050 print 'Macro contents:'
2030 print macro,
2051 print macro,
2031
2052
2032 def magic_save(self,parameter_s = ''):
2053 def magic_save(self,parameter_s = ''):
2033 """Save a set of lines to a given filename.
2054 """Save a set of lines to a given filename.
2034
2055
2035 Usage:\\
2056 Usage:\\
2036 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2057 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2037
2058
2038 Options:
2059 Options:
2039
2060
2040 -r: use 'raw' input. By default, the 'processed' history is used,
2061 -r: use 'raw' input. By default, the 'processed' history is used,
2041 so that magics are loaded in their transformed version to valid
2062 so that magics are loaded in their transformed version to valid
2042 Python. If this option is given, the raw input as typed as the
2063 Python. If this option is given, the raw input as typed as the
2043 command line is used instead.
2064 command line is used instead.
2044
2065
2045 This function uses the same syntax as %macro for line extraction, but
2066 This function uses the same syntax as %macro for line extraction, but
2046 instead of creating a macro it saves the resulting string to the
2067 instead of creating a macro it saves the resulting string to the
2047 filename you specify.
2068 filename you specify.
2048
2069
2049 It adds a '.py' extension to the file if you don't do so yourself, and
2070 It adds a '.py' extension to the file if you don't do so yourself, and
2050 it asks for confirmation before overwriting existing files."""
2071 it asks for confirmation before overwriting existing files."""
2051
2072
2052 opts,args = self.parse_options(parameter_s,'r',mode='list')
2073 opts,args = self.parse_options(parameter_s,'r',mode='list')
2053 fname,ranges = args[0], args[1:]
2074 fname,ranges = args[0], args[1:]
2054 if not fname.endswith('.py'):
2075 if not fname.endswith('.py'):
2055 fname += '.py'
2076 fname += '.py'
2056 if os.path.isfile(fname):
2077 if os.path.isfile(fname):
2057 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2078 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2058 if ans.lower() not in ['y','yes']:
2079 if ans.lower() not in ['y','yes']:
2059 print 'Operation cancelled.'
2080 print 'Operation cancelled.'
2060 return
2081 return
2061 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2082 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2062 f = file(fname,'w')
2083 f = file(fname,'w')
2063 f.write(cmds)
2084 f.write(cmds)
2064 f.close()
2085 f.close()
2065 print 'The following commands were written to file `%s`:' % fname
2086 print 'The following commands were written to file `%s`:' % fname
2066 print cmds
2087 print cmds
2067
2088
2068 def _edit_macro(self,mname,macro):
2089 def _edit_macro(self,mname,macro):
2069 """open an editor with the macro data in a file"""
2090 """open an editor with the macro data in a file"""
2070 filename = self.shell.mktempfile(macro.value)
2091 filename = self.shell.mktempfile(macro.value)
2071 self.shell.hooks.editor(filename)
2092 self.shell.hooks.editor(filename)
2072
2093
2073 # and make a new macro object, to replace the old one
2094 # and make a new macro object, to replace the old one
2074 mfile = open(filename)
2095 mfile = open(filename)
2075 mvalue = mfile.read()
2096 mvalue = mfile.read()
2076 mfile.close()
2097 mfile.close()
2077 self.shell.user_ns[mname] = Macro(mvalue)
2098 self.shell.user_ns[mname] = Macro(mvalue)
2078
2099
2079 def magic_ed(self,parameter_s=''):
2100 def magic_ed(self,parameter_s=''):
2080 """Alias to %edit."""
2101 """Alias to %edit."""
2081 return self.magic_edit(parameter_s)
2102 return self.magic_edit(parameter_s)
2082
2103
2083 @testdec.skip_doctest
2104 @testdec.skip_doctest
2084 def magic_edit(self,parameter_s='',last_call=['','']):
2105 def magic_edit(self,parameter_s='',last_call=['','']):
2085 """Bring up an editor and execute the resulting code.
2106 """Bring up an editor and execute the resulting code.
2086
2107
2087 Usage:
2108 Usage:
2088 %edit [options] [args]
2109 %edit [options] [args]
2089
2110
2090 %edit runs IPython's editor hook. The default version of this hook is
2111 %edit runs IPython's editor hook. The default version of this hook is
2091 set to call the __IPYTHON__.rc.editor command. This is read from your
2112 set to call the __IPYTHON__.rc.editor command. This is read from your
2092 environment variable $EDITOR. If this isn't found, it will default to
2113 environment variable $EDITOR. If this isn't found, it will default to
2093 vi under Linux/Unix and to notepad under Windows. See the end of this
2114 vi under Linux/Unix and to notepad under Windows. See the end of this
2094 docstring for how to change the editor hook.
2115 docstring for how to change the editor hook.
2095
2116
2096 You can also set the value of this editor via the command line option
2117 You can also set the value of this editor via the command line option
2097 '-editor' or in your ipythonrc file. This is useful if you wish to use
2118 '-editor' or in your ipythonrc file. This is useful if you wish to use
2098 specifically for IPython an editor different from your typical default
2119 specifically for IPython an editor different from your typical default
2099 (and for Windows users who typically don't set environment variables).
2120 (and for Windows users who typically don't set environment variables).
2100
2121
2101 This command allows you to conveniently edit multi-line code right in
2122 This command allows you to conveniently edit multi-line code right in
2102 your IPython session.
2123 your IPython session.
2103
2124
2104 If called without arguments, %edit opens up an empty editor with a
2125 If called without arguments, %edit opens up an empty editor with a
2105 temporary file and will execute the contents of this file when you
2126 temporary file and will execute the contents of this file when you
2106 close it (don't forget to save it!).
2127 close it (don't forget to save it!).
2107
2128
2108
2129
2109 Options:
2130 Options:
2110
2131
2111 -n <number>: open the editor at a specified line number. By default,
2132 -n <number>: open the editor at a specified line number. By default,
2112 the IPython editor hook uses the unix syntax 'editor +N filename', but
2133 the IPython editor hook uses the unix syntax 'editor +N filename', but
2113 you can configure this by providing your own modified hook if your
2134 you can configure this by providing your own modified hook if your
2114 favorite editor supports line-number specifications with a different
2135 favorite editor supports line-number specifications with a different
2115 syntax.
2136 syntax.
2116
2137
2117 -p: this will call the editor with the same data as the previous time
2138 -p: this will call the editor with the same data as the previous time
2118 it was used, regardless of how long ago (in your current session) it
2139 it was used, regardless of how long ago (in your current session) it
2119 was.
2140 was.
2120
2141
2121 -r: use 'raw' input. This option only applies to input taken from the
2142 -r: use 'raw' input. This option only applies to input taken from the
2122 user's history. By default, the 'processed' history is used, so that
2143 user's history. By default, the 'processed' history is used, so that
2123 magics are loaded in their transformed version to valid Python. If
2144 magics are loaded in their transformed version to valid Python. If
2124 this option is given, the raw input as typed as the command line is
2145 this option is given, the raw input as typed as the command line is
2125 used instead. When you exit the editor, it will be executed by
2146 used instead. When you exit the editor, it will be executed by
2126 IPython's own processor.
2147 IPython's own processor.
2127
2148
2128 -x: do not execute the edited code immediately upon exit. This is
2149 -x: do not execute the edited code immediately upon exit. This is
2129 mainly useful if you are editing programs which need to be called with
2150 mainly useful if you are editing programs which need to be called with
2130 command line arguments, which you can then do using %run.
2151 command line arguments, which you can then do using %run.
2131
2152
2132
2153
2133 Arguments:
2154 Arguments:
2134
2155
2135 If arguments are given, the following possibilites exist:
2156 If arguments are given, the following possibilites exist:
2136
2157
2137 - The arguments are numbers or pairs of colon-separated numbers (like
2158 - The arguments are numbers or pairs of colon-separated numbers (like
2138 1 4:8 9). These are interpreted as lines of previous input to be
2159 1 4:8 9). These are interpreted as lines of previous input to be
2139 loaded into the editor. The syntax is the same of the %macro command.
2160 loaded into the editor. The syntax is the same of the %macro command.
2140
2161
2141 - If the argument doesn't start with a number, it is evaluated as a
2162 - If the argument doesn't start with a number, it is evaluated as a
2142 variable and its contents loaded into the editor. You can thus edit
2163 variable and its contents loaded into the editor. You can thus edit
2143 any string which contains python code (including the result of
2164 any string which contains python code (including the result of
2144 previous edits).
2165 previous edits).
2145
2166
2146 - If the argument is the name of an object (other than a string),
2167 - If the argument is the name of an object (other than a string),
2147 IPython will try to locate the file where it was defined and open the
2168 IPython will try to locate the file where it was defined and open the
2148 editor at the point where it is defined. You can use `%edit function`
2169 editor at the point where it is defined. You can use `%edit function`
2149 to load an editor exactly at the point where 'function' is defined,
2170 to load an editor exactly at the point where 'function' is defined,
2150 edit it and have the file be executed automatically.
2171 edit it and have the file be executed automatically.
2151
2172
2152 If the object is a macro (see %macro for details), this opens up your
2173 If the object is a macro (see %macro for details), this opens up your
2153 specified editor with a temporary file containing the macro's data.
2174 specified editor with a temporary file containing the macro's data.
2154 Upon exit, the macro is reloaded with the contents of the file.
2175 Upon exit, the macro is reloaded with the contents of the file.
2155
2176
2156 Note: opening at an exact line is only supported under Unix, and some
2177 Note: opening at an exact line is only supported under Unix, and some
2157 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2178 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2158 '+NUMBER' parameter necessary for this feature. Good editors like
2179 '+NUMBER' parameter necessary for this feature. Good editors like
2159 (X)Emacs, vi, jed, pico and joe all do.
2180 (X)Emacs, vi, jed, pico and joe all do.
2160
2181
2161 - If the argument is not found as a variable, IPython will look for a
2182 - If the argument is not found as a variable, IPython will look for a
2162 file with that name (adding .py if necessary) and load it into the
2183 file with that name (adding .py if necessary) and load it into the
2163 editor. It will execute its contents with execfile() when you exit,
2184 editor. It will execute its contents with execfile() when you exit,
2164 loading any code in the file into your interactive namespace.
2185 loading any code in the file into your interactive namespace.
2165
2186
2166 After executing your code, %edit will return as output the code you
2187 After executing your code, %edit will return as output the code you
2167 typed in the editor (except when it was an existing file). This way
2188 typed in the editor (except when it was an existing file). This way
2168 you can reload the code in further invocations of %edit as a variable,
2189 you can reload the code in further invocations of %edit as a variable,
2169 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2190 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2170 the output.
2191 the output.
2171
2192
2172 Note that %edit is also available through the alias %ed.
2193 Note that %edit is also available through the alias %ed.
2173
2194
2174 This is an example of creating a simple function inside the editor and
2195 This is an example of creating a simple function inside the editor and
2175 then modifying it. First, start up the editor:
2196 then modifying it. First, start up the editor:
2176
2197
2177 In [1]: ed
2198 In [1]: ed
2178 Editing... done. Executing edited code...
2199 Editing... done. Executing edited code...
2179 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2200 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2180
2201
2181 We can then call the function foo():
2202 We can then call the function foo():
2182
2203
2183 In [2]: foo()
2204 In [2]: foo()
2184 foo() was defined in an editing session
2205 foo() was defined in an editing session
2185
2206
2186 Now we edit foo. IPython automatically loads the editor with the
2207 Now we edit foo. IPython automatically loads the editor with the
2187 (temporary) file where foo() was previously defined:
2208 (temporary) file where foo() was previously defined:
2188
2209
2189 In [3]: ed foo
2210 In [3]: ed foo
2190 Editing... done. Executing edited code...
2211 Editing... done. Executing edited code...
2191
2212
2192 And if we call foo() again we get the modified version:
2213 And if we call foo() again we get the modified version:
2193
2214
2194 In [4]: foo()
2215 In [4]: foo()
2195 foo() has now been changed!
2216 foo() has now been changed!
2196
2217
2197 Here is an example of how to edit a code snippet successive
2218 Here is an example of how to edit a code snippet successive
2198 times. First we call the editor:
2219 times. First we call the editor:
2199
2220
2200 In [5]: ed
2221 In [5]: ed
2201 Editing... done. Executing edited code...
2222 Editing... done. Executing edited code...
2202 hello
2223 hello
2203 Out[5]: "print 'hello'n"
2224 Out[5]: "print 'hello'n"
2204
2225
2205 Now we call it again with the previous output (stored in _):
2226 Now we call it again with the previous output (stored in _):
2206
2227
2207 In [6]: ed _
2228 In [6]: ed _
2208 Editing... done. Executing edited code...
2229 Editing... done. Executing edited code...
2209 hello world
2230 hello world
2210 Out[6]: "print 'hello world'n"
2231 Out[6]: "print 'hello world'n"
2211
2232
2212 Now we call it with the output #8 (stored in _8, also as Out[8]):
2233 Now we call it with the output #8 (stored in _8, also as Out[8]):
2213
2234
2214 In [7]: ed _8
2235 In [7]: ed _8
2215 Editing... done. Executing edited code...
2236 Editing... done. Executing edited code...
2216 hello again
2237 hello again
2217 Out[7]: "print 'hello again'n"
2238 Out[7]: "print 'hello again'n"
2218
2239
2219
2240
2220 Changing the default editor hook:
2241 Changing the default editor hook:
2221
2242
2222 If you wish to write your own editor hook, you can put it in a
2243 If you wish to write your own editor hook, you can put it in a
2223 configuration file which you load at startup time. The default hook
2244 configuration file which you load at startup time. The default hook
2224 is defined in the IPython.hooks module, and you can use that as a
2245 is defined in the IPython.hooks module, and you can use that as a
2225 starting example for further modifications. That file also has
2246 starting example for further modifications. That file also has
2226 general instructions on how to set a new hook for use once you've
2247 general instructions on how to set a new hook for use once you've
2227 defined it."""
2248 defined it."""
2228
2249
2229 # FIXME: This function has become a convoluted mess. It needs a
2250 # FIXME: This function has become a convoluted mess. It needs a
2230 # ground-up rewrite with clean, simple logic.
2251 # ground-up rewrite with clean, simple logic.
2231
2252
2232 def make_filename(arg):
2253 def make_filename(arg):
2233 "Make a filename from the given args"
2254 "Make a filename from the given args"
2234 try:
2255 try:
2235 filename = get_py_filename(arg)
2256 filename = get_py_filename(arg)
2236 except IOError:
2257 except IOError:
2237 if args.endswith('.py'):
2258 if args.endswith('.py'):
2238 filename = arg
2259 filename = arg
2239 else:
2260 else:
2240 filename = None
2261 filename = None
2241 return filename
2262 return filename
2242
2263
2243 # custom exceptions
2264 # custom exceptions
2244 class DataIsObject(Exception): pass
2265 class DataIsObject(Exception): pass
2245
2266
2246 opts,args = self.parse_options(parameter_s,'prxn:')
2267 opts,args = self.parse_options(parameter_s,'prxn:')
2247 # Set a few locals from the options for convenience:
2268 # Set a few locals from the options for convenience:
2248 opts_p = opts.has_key('p')
2269 opts_p = opts.has_key('p')
2249 opts_r = opts.has_key('r')
2270 opts_r = opts.has_key('r')
2250
2271
2251 # Default line number value
2272 # Default line number value
2252 lineno = opts.get('n',None)
2273 lineno = opts.get('n',None)
2253
2274
2254 if opts_p:
2275 if opts_p:
2255 args = '_%s' % last_call[0]
2276 args = '_%s' % last_call[0]
2256 if not self.shell.user_ns.has_key(args):
2277 if not self.shell.user_ns.has_key(args):
2257 args = last_call[1]
2278 args = last_call[1]
2258
2279
2259 # use last_call to remember the state of the previous call, but don't
2280 # use last_call to remember the state of the previous call, but don't
2260 # let it be clobbered by successive '-p' calls.
2281 # let it be clobbered by successive '-p' calls.
2261 try:
2282 try:
2262 last_call[0] = self.shell.outputcache.prompt_count
2283 last_call[0] = self.shell.outputcache.prompt_count
2263 if not opts_p:
2284 if not opts_p:
2264 last_call[1] = parameter_s
2285 last_call[1] = parameter_s
2265 except:
2286 except:
2266 pass
2287 pass
2267
2288
2268 # by default this is done with temp files, except when the given
2289 # by default this is done with temp files, except when the given
2269 # arg is a filename
2290 # arg is a filename
2270 use_temp = 1
2291 use_temp = 1
2271
2292
2272 if re.match(r'\d',args):
2293 if re.match(r'\d',args):
2273 # Mode where user specifies ranges of lines, like in %macro.
2294 # Mode where user specifies ranges of lines, like in %macro.
2274 # This means that you can't edit files whose names begin with
2295 # This means that you can't edit files whose names begin with
2275 # numbers this way. Tough.
2296 # numbers this way. Tough.
2276 ranges = args.split()
2297 ranges = args.split()
2277 data = ''.join(self.extract_input_slices(ranges,opts_r))
2298 data = ''.join(self.extract_input_slices(ranges,opts_r))
2278 elif args.endswith('.py'):
2299 elif args.endswith('.py'):
2279 filename = make_filename(args)
2300 filename = make_filename(args)
2280 data = ''
2301 data = ''
2281 use_temp = 0
2302 use_temp = 0
2282 elif args:
2303 elif args:
2283 try:
2304 try:
2284 # Load the parameter given as a variable. If not a string,
2305 # Load the parameter given as a variable. If not a string,
2285 # process it as an object instead (below)
2306 # process it as an object instead (below)
2286
2307
2287 #print '*** args',args,'type',type(args) # dbg
2308 #print '*** args',args,'type',type(args) # dbg
2288 data = eval(args,self.shell.user_ns)
2309 data = eval(args,self.shell.user_ns)
2289 if not type(data) in StringTypes:
2310 if not type(data) in StringTypes:
2290 raise DataIsObject
2311 raise DataIsObject
2291
2312
2292 except (NameError,SyntaxError):
2313 except (NameError,SyntaxError):
2293 # given argument is not a variable, try as a filename
2314 # given argument is not a variable, try as a filename
2294 filename = make_filename(args)
2315 filename = make_filename(args)
2295 if filename is None:
2316 if filename is None:
2296 warn("Argument given (%s) can't be found as a variable "
2317 warn("Argument given (%s) can't be found as a variable "
2297 "or as a filename." % args)
2318 "or as a filename." % args)
2298 return
2319 return
2299
2320
2300 data = ''
2321 data = ''
2301 use_temp = 0
2322 use_temp = 0
2302 except DataIsObject:
2323 except DataIsObject:
2303
2324
2304 # macros have a special edit function
2325 # macros have a special edit function
2305 if isinstance(data,Macro):
2326 if isinstance(data,Macro):
2306 self._edit_macro(args,data)
2327 self._edit_macro(args,data)
2307 return
2328 return
2308
2329
2309 # For objects, try to edit the file where they are defined
2330 # For objects, try to edit the file where they are defined
2310 try:
2331 try:
2311 filename = inspect.getabsfile(data)
2332 filename = inspect.getabsfile(data)
2312 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2333 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2313 # class created by %edit? Try to find source
2334 # class created by %edit? Try to find source
2314 # by looking for method definitions instead, the
2335 # by looking for method definitions instead, the
2315 # __module__ in those classes is FakeModule.
2336 # __module__ in those classes is FakeModule.
2316 attrs = [getattr(data, aname) for aname in dir(data)]
2337 attrs = [getattr(data, aname) for aname in dir(data)]
2317 for attr in attrs:
2338 for attr in attrs:
2318 if not inspect.ismethod(attr):
2339 if not inspect.ismethod(attr):
2319 continue
2340 continue
2320 filename = inspect.getabsfile(attr)
2341 filename = inspect.getabsfile(attr)
2321 if filename and 'fakemodule' not in filename.lower():
2342 if filename and 'fakemodule' not in filename.lower():
2322 # change the attribute to be the edit target instead
2343 # change the attribute to be the edit target instead
2323 data = attr
2344 data = attr
2324 break
2345 break
2325
2346
2326 datafile = 1
2347 datafile = 1
2327 except TypeError:
2348 except TypeError:
2328 filename = make_filename(args)
2349 filename = make_filename(args)
2329 datafile = 1
2350 datafile = 1
2330 warn('Could not find file where `%s` is defined.\n'
2351 warn('Could not find file where `%s` is defined.\n'
2331 'Opening a file named `%s`' % (args,filename))
2352 'Opening a file named `%s`' % (args,filename))
2332 # Now, make sure we can actually read the source (if it was in
2353 # Now, make sure we can actually read the source (if it was in
2333 # a temp file it's gone by now).
2354 # a temp file it's gone by now).
2334 if datafile:
2355 if datafile:
2335 try:
2356 try:
2336 if lineno is None:
2357 if lineno is None:
2337 lineno = inspect.getsourcelines(data)[1]
2358 lineno = inspect.getsourcelines(data)[1]
2338 except IOError:
2359 except IOError:
2339 filename = make_filename(args)
2360 filename = make_filename(args)
2340 if filename is None:
2361 if filename is None:
2341 warn('The file `%s` where `%s` was defined cannot '
2362 warn('The file `%s` where `%s` was defined cannot '
2342 'be read.' % (filename,data))
2363 'be read.' % (filename,data))
2343 return
2364 return
2344 use_temp = 0
2365 use_temp = 0
2345 else:
2366 else:
2346 data = ''
2367 data = ''
2347
2368
2348 if use_temp:
2369 if use_temp:
2349 filename = self.shell.mktempfile(data)
2370 filename = self.shell.mktempfile(data)
2350 print 'IPython will make a temporary file named:',filename
2371 print 'IPython will make a temporary file named:',filename
2351
2372
2352 # do actual editing here
2373 # do actual editing here
2353 print 'Editing...',
2374 print 'Editing...',
2354 sys.stdout.flush()
2375 sys.stdout.flush()
2355 try:
2376 try:
2356 self.shell.hooks.editor(filename,lineno)
2377 self.shell.hooks.editor(filename,lineno)
2357 except IPython.ipapi.TryNext:
2378 except IPython.ipapi.TryNext:
2358 warn('Could not open editor')
2379 warn('Could not open editor')
2359 return
2380 return
2360
2381
2361 # XXX TODO: should this be generalized for all string vars?
2382 # XXX TODO: should this be generalized for all string vars?
2362 # For now, this is special-cased to blocks created by cpaste
2383 # For now, this is special-cased to blocks created by cpaste
2363 if args.strip() == 'pasted_block':
2384 if args.strip() == 'pasted_block':
2364 self.shell.user_ns['pasted_block'] = file_read(filename)
2385 self.shell.user_ns['pasted_block'] = file_read(filename)
2365
2386
2366 if opts.has_key('x'): # -x prevents actual execution
2387 if opts.has_key('x'): # -x prevents actual execution
2367 print
2388 print
2368 else:
2389 else:
2369 print 'done. Executing edited code...'
2390 print 'done. Executing edited code...'
2370 if opts_r:
2391 if opts_r:
2371 self.shell.runlines(file_read(filename))
2392 self.shell.runlines(file_read(filename))
2372 else:
2393 else:
2373 self.shell.safe_execfile(filename,self.shell.user_ns,
2394 self.shell.safe_execfile(filename,self.shell.user_ns,
2374 self.shell.user_ns)
2395 self.shell.user_ns)
2375
2396
2376
2397
2377 if use_temp:
2398 if use_temp:
2378 try:
2399 try:
2379 return open(filename).read()
2400 return open(filename).read()
2380 except IOError,msg:
2401 except IOError,msg:
2381 if msg.filename == filename:
2402 if msg.filename == filename:
2382 warn('File not found. Did you forget to save?')
2403 warn('File not found. Did you forget to save?')
2383 return
2404 return
2384 else:
2405 else:
2385 self.shell.showtraceback()
2406 self.shell.showtraceback()
2386
2407
2387 def magic_xmode(self,parameter_s = ''):
2408 def magic_xmode(self,parameter_s = ''):
2388 """Switch modes for the exception handlers.
2409 """Switch modes for the exception handlers.
2389
2410
2390 Valid modes: Plain, Context and Verbose.
2411 Valid modes: Plain, Context and Verbose.
2391
2412
2392 If called without arguments, acts as a toggle."""
2413 If called without arguments, acts as a toggle."""
2393
2414
2394 def xmode_switch_err(name):
2415 def xmode_switch_err(name):
2395 warn('Error changing %s exception modes.\n%s' %
2416 warn('Error changing %s exception modes.\n%s' %
2396 (name,sys.exc_info()[1]))
2417 (name,sys.exc_info()[1]))
2397
2418
2398 shell = self.shell
2419 shell = self.shell
2399 new_mode = parameter_s.strip().capitalize()
2420 new_mode = parameter_s.strip().capitalize()
2400 try:
2421 try:
2401 shell.InteractiveTB.set_mode(mode=new_mode)
2422 shell.InteractiveTB.set_mode(mode=new_mode)
2402 print 'Exception reporting mode:',shell.InteractiveTB.mode
2423 print 'Exception reporting mode:',shell.InteractiveTB.mode
2403 except:
2424 except:
2404 xmode_switch_err('user')
2425 xmode_switch_err('user')
2405
2426
2406 # threaded shells use a special handler in sys.excepthook
2427 # threaded shells use a special handler in sys.excepthook
2407 if shell.isthreaded:
2428 if shell.isthreaded:
2408 try:
2429 try:
2409 shell.sys_excepthook.set_mode(mode=new_mode)
2430 shell.sys_excepthook.set_mode(mode=new_mode)
2410 except:
2431 except:
2411 xmode_switch_err('threaded')
2432 xmode_switch_err('threaded')
2412
2433
2413 def magic_colors(self,parameter_s = ''):
2434 def magic_colors(self,parameter_s = ''):
2414 """Switch color scheme for prompts, info system and exception handlers.
2435 """Switch color scheme for prompts, info system and exception handlers.
2415
2436
2416 Currently implemented schemes: NoColor, Linux, LightBG.
2437 Currently implemented schemes: NoColor, Linux, LightBG.
2417
2438
2418 Color scheme names are not case-sensitive."""
2439 Color scheme names are not case-sensitive."""
2419
2440
2420 def color_switch_err(name):
2441 def color_switch_err(name):
2421 warn('Error changing %s color schemes.\n%s' %
2442 warn('Error changing %s color schemes.\n%s' %
2422 (name,sys.exc_info()[1]))
2443 (name,sys.exc_info()[1]))
2423
2444
2424
2445
2425 new_scheme = parameter_s.strip()
2446 new_scheme = parameter_s.strip()
2426 if not new_scheme:
2447 if not new_scheme:
2427 raise UsageError(
2448 raise UsageError(
2428 "%colors: you must specify a color scheme. See '%colors?'")
2449 "%colors: you must specify a color scheme. See '%colors?'")
2429 return
2450 return
2430 # local shortcut
2451 # local shortcut
2431 shell = self.shell
2452 shell = self.shell
2432
2453
2433 import IPython.rlineimpl as readline
2454 import IPython.rlineimpl as readline
2434
2455
2435 if not readline.have_readline and sys.platform == "win32":
2456 if not readline.have_readline and sys.platform == "win32":
2436 msg = """\
2457 msg = """\
2437 Proper color support under MS Windows requires the pyreadline library.
2458 Proper color support under MS Windows requires the pyreadline library.
2438 You can find it at:
2459 You can find it at:
2439 http://ipython.scipy.org/moin/PyReadline/Intro
2460 http://ipython.scipy.org/moin/PyReadline/Intro
2440 Gary's readline needs the ctypes module, from:
2461 Gary's readline needs the ctypes module, from:
2441 http://starship.python.net/crew/theller/ctypes
2462 http://starship.python.net/crew/theller/ctypes
2442 (Note that ctypes is already part of Python versions 2.5 and newer).
2463 (Note that ctypes is already part of Python versions 2.5 and newer).
2443
2464
2444 Defaulting color scheme to 'NoColor'"""
2465 Defaulting color scheme to 'NoColor'"""
2445 new_scheme = 'NoColor'
2466 new_scheme = 'NoColor'
2446 warn(msg)
2467 warn(msg)
2447
2468
2448 # readline option is 0
2469 # readline option is 0
2449 if not shell.has_readline:
2470 if not shell.has_readline:
2450 new_scheme = 'NoColor'
2471 new_scheme = 'NoColor'
2451
2472
2452 # Set prompt colors
2473 # Set prompt colors
2453 try:
2474 try:
2454 shell.outputcache.set_colors(new_scheme)
2475 shell.outputcache.set_colors(new_scheme)
2455 except:
2476 except:
2456 color_switch_err('prompt')
2477 color_switch_err('prompt')
2457 else:
2478 else:
2458 shell.rc.colors = \
2479 shell.rc.colors = \
2459 shell.outputcache.color_table.active_scheme_name
2480 shell.outputcache.color_table.active_scheme_name
2460 # Set exception colors
2481 # Set exception colors
2461 try:
2482 try:
2462 shell.InteractiveTB.set_colors(scheme = new_scheme)
2483 shell.InteractiveTB.set_colors(scheme = new_scheme)
2463 shell.SyntaxTB.set_colors(scheme = new_scheme)
2484 shell.SyntaxTB.set_colors(scheme = new_scheme)
2464 except:
2485 except:
2465 color_switch_err('exception')
2486 color_switch_err('exception')
2466
2487
2467 # threaded shells use a verbose traceback in sys.excepthook
2488 # threaded shells use a verbose traceback in sys.excepthook
2468 if shell.isthreaded:
2489 if shell.isthreaded:
2469 try:
2490 try:
2470 shell.sys_excepthook.set_colors(scheme=new_scheme)
2491 shell.sys_excepthook.set_colors(scheme=new_scheme)
2471 except:
2492 except:
2472 color_switch_err('system exception handler')
2493 color_switch_err('system exception handler')
2473
2494
2474 # Set info (for 'object?') colors
2495 # Set info (for 'object?') colors
2475 if shell.rc.color_info:
2496 if shell.rc.color_info:
2476 try:
2497 try:
2477 shell.inspector.set_active_scheme(new_scheme)
2498 shell.inspector.set_active_scheme(new_scheme)
2478 except:
2499 except:
2479 color_switch_err('object inspector')
2500 color_switch_err('object inspector')
2480 else:
2501 else:
2481 shell.inspector.set_active_scheme('NoColor')
2502 shell.inspector.set_active_scheme('NoColor')
2482
2503
2483 def magic_color_info(self,parameter_s = ''):
2504 def magic_color_info(self,parameter_s = ''):
2484 """Toggle color_info.
2505 """Toggle color_info.
2485
2506
2486 The color_info configuration parameter controls whether colors are
2507 The color_info configuration parameter controls whether colors are
2487 used for displaying object details (by things like %psource, %pfile or
2508 used for displaying object details (by things like %psource, %pfile or
2488 the '?' system). This function toggles this value with each call.
2509 the '?' system). This function toggles this value with each call.
2489
2510
2490 Note that unless you have a fairly recent pager (less works better
2511 Note that unless you have a fairly recent pager (less works better
2491 than more) in your system, using colored object information displays
2512 than more) in your system, using colored object information displays
2492 will not work properly. Test it and see."""
2513 will not work properly. Test it and see."""
2493
2514
2494 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2515 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2495 self.magic_colors(self.shell.rc.colors)
2516 self.magic_colors(self.shell.rc.colors)
2496 print 'Object introspection functions have now coloring:',
2517 print 'Object introspection functions have now coloring:',
2497 print ['OFF','ON'][self.shell.rc.color_info]
2518 print ['OFF','ON'][self.shell.rc.color_info]
2498
2519
2499 def magic_Pprint(self, parameter_s=''):
2520 def magic_Pprint(self, parameter_s=''):
2500 """Toggle pretty printing on/off."""
2521 """Toggle pretty printing on/off."""
2501
2522
2502 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2523 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2503 print 'Pretty printing has been turned', \
2524 print 'Pretty printing has been turned', \
2504 ['OFF','ON'][self.shell.rc.pprint]
2525 ['OFF','ON'][self.shell.rc.pprint]
2505
2526
2506 def magic_exit(self, parameter_s=''):
2527 def magic_exit(self, parameter_s=''):
2507 """Exit IPython, confirming if configured to do so.
2528 """Exit IPython, confirming if configured to do so.
2508
2529
2509 You can configure whether IPython asks for confirmation upon exit by
2530 You can configure whether IPython asks for confirmation upon exit by
2510 setting the confirm_exit flag in the ipythonrc file."""
2531 setting the confirm_exit flag in the ipythonrc file."""
2511
2532
2512 self.shell.exit()
2533 self.shell.exit()
2513
2534
2514 def magic_quit(self, parameter_s=''):
2535 def magic_quit(self, parameter_s=''):
2515 """Exit IPython, confirming if configured to do so (like %exit)"""
2536 """Exit IPython, confirming if configured to do so (like %exit)"""
2516
2537
2517 self.shell.exit()
2538 self.shell.exit()
2518
2539
2519 def magic_Exit(self, parameter_s=''):
2540 def magic_Exit(self, parameter_s=''):
2520 """Exit IPython without confirmation."""
2541 """Exit IPython without confirmation."""
2521
2542
2522 self.shell.ask_exit()
2543 self.shell.ask_exit()
2523
2544
2524 #......................................................................
2545 #......................................................................
2525 # Functions to implement unix shell-type things
2546 # Functions to implement unix shell-type things
2526
2547
2527 @testdec.skip_doctest
2548 @testdec.skip_doctest
2528 def magic_alias(self, parameter_s = ''):
2549 def magic_alias(self, parameter_s = ''):
2529 """Define an alias for a system command.
2550 """Define an alias for a system command.
2530
2551
2531 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2552 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2532
2553
2533 Then, typing 'alias_name params' will execute the system command 'cmd
2554 Then, typing 'alias_name params' will execute the system command 'cmd
2534 params' (from your underlying operating system).
2555 params' (from your underlying operating system).
2535
2556
2536 Aliases have lower precedence than magic functions and Python normal
2557 Aliases have lower precedence than magic functions and Python normal
2537 variables, so if 'foo' is both a Python variable and an alias, the
2558 variables, so if 'foo' is both a Python variable and an alias, the
2538 alias can not be executed until 'del foo' removes the Python variable.
2559 alias can not be executed until 'del foo' removes the Python variable.
2539
2560
2540 You can use the %l specifier in an alias definition to represent the
2561 You can use the %l specifier in an alias definition to represent the
2541 whole line when the alias is called. For example:
2562 whole line when the alias is called. For example:
2542
2563
2543 In [2]: alias all echo "Input in brackets: <%l>"
2564 In [2]: alias all echo "Input in brackets: <%l>"
2544 In [3]: all hello world
2565 In [3]: all hello world
2545 Input in brackets: <hello world>
2566 Input in brackets: <hello world>
2546
2567
2547 You can also define aliases with parameters using %s specifiers (one
2568 You can also define aliases with parameters using %s specifiers (one
2548 per parameter):
2569 per parameter):
2549
2570
2550 In [1]: alias parts echo first %s second %s
2571 In [1]: alias parts echo first %s second %s
2551 In [2]: %parts A B
2572 In [2]: %parts A B
2552 first A second B
2573 first A second B
2553 In [3]: %parts A
2574 In [3]: %parts A
2554 Incorrect number of arguments: 2 expected.
2575 Incorrect number of arguments: 2 expected.
2555 parts is an alias to: 'echo first %s second %s'
2576 parts is an alias to: 'echo first %s second %s'
2556
2577
2557 Note that %l and %s are mutually exclusive. You can only use one or
2578 Note that %l and %s are mutually exclusive. You can only use one or
2558 the other in your aliases.
2579 the other in your aliases.
2559
2580
2560 Aliases expand Python variables just like system calls using ! or !!
2581 Aliases expand Python variables just like system calls using ! or !!
2561 do: all expressions prefixed with '$' get expanded. For details of
2582 do: all expressions prefixed with '$' get expanded. For details of
2562 the semantic rules, see PEP-215:
2583 the semantic rules, see PEP-215:
2563 http://www.python.org/peps/pep-0215.html. This is the library used by
2584 http://www.python.org/peps/pep-0215.html. This is the library used by
2564 IPython for variable expansion. If you want to access a true shell
2585 IPython for variable expansion. If you want to access a true shell
2565 variable, an extra $ is necessary to prevent its expansion by IPython:
2586 variable, an extra $ is necessary to prevent its expansion by IPython:
2566
2587
2567 In [6]: alias show echo
2588 In [6]: alias show echo
2568 In [7]: PATH='A Python string'
2589 In [7]: PATH='A Python string'
2569 In [8]: show $PATH
2590 In [8]: show $PATH
2570 A Python string
2591 A Python string
2571 In [9]: show $$PATH
2592 In [9]: show $$PATH
2572 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2593 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2573
2594
2574 You can use the alias facility to acess all of $PATH. See the %rehash
2595 You can use the alias facility to acess all of $PATH. See the %rehash
2575 and %rehashx functions, which automatically create aliases for the
2596 and %rehashx functions, which automatically create aliases for the
2576 contents of your $PATH.
2597 contents of your $PATH.
2577
2598
2578 If called with no parameters, %alias prints the current alias table."""
2599 If called with no parameters, %alias prints the current alias table."""
2579
2600
2580 par = parameter_s.strip()
2601 par = parameter_s.strip()
2581 if not par:
2602 if not par:
2582 stored = self.db.get('stored_aliases', {} )
2603 stored = self.db.get('stored_aliases', {} )
2583 atab = self.shell.alias_table
2604 atab = self.shell.alias_table
2584 aliases = atab.keys()
2605 aliases = atab.keys()
2585 aliases.sort()
2606 aliases.sort()
2586 res = []
2607 res = []
2587 showlast = []
2608 showlast = []
2588 for alias in aliases:
2609 for alias in aliases:
2589 special = False
2610 special = False
2590 try:
2611 try:
2591 tgt = atab[alias][1]
2612 tgt = atab[alias][1]
2592 except (TypeError, AttributeError):
2613 except (TypeError, AttributeError):
2593 # unsubscriptable? probably a callable
2614 # unsubscriptable? probably a callable
2594 tgt = atab[alias]
2615 tgt = atab[alias]
2595 special = True
2616 special = True
2596 # 'interesting' aliases
2617 # 'interesting' aliases
2597 if (alias in stored or
2618 if (alias in stored or
2598 special or
2619 special or
2599 alias.lower() != os.path.splitext(tgt)[0].lower() or
2620 alias.lower() != os.path.splitext(tgt)[0].lower() or
2600 ' ' in tgt):
2621 ' ' in tgt):
2601 showlast.append((alias, tgt))
2622 showlast.append((alias, tgt))
2602 else:
2623 else:
2603 res.append((alias, tgt ))
2624 res.append((alias, tgt ))
2604
2625
2605 # show most interesting aliases last
2626 # show most interesting aliases last
2606 res.extend(showlast)
2627 res.extend(showlast)
2607 print "Total number of aliases:",len(aliases)
2628 print "Total number of aliases:",len(aliases)
2608 return res
2629 return res
2609 try:
2630 try:
2610 alias,cmd = par.split(None,1)
2631 alias,cmd = par.split(None,1)
2611 except:
2632 except:
2612 print OInspect.getdoc(self.magic_alias)
2633 print OInspect.getdoc(self.magic_alias)
2613 else:
2634 else:
2614 nargs = cmd.count('%s')
2635 nargs = cmd.count('%s')
2615 if nargs>0 and cmd.find('%l')>=0:
2636 if nargs>0 and cmd.find('%l')>=0:
2616 error('The %s and %l specifiers are mutually exclusive '
2637 error('The %s and %l specifiers are mutually exclusive '
2617 'in alias definitions.')
2638 'in alias definitions.')
2618 else: # all looks OK
2639 else: # all looks OK
2619 self.shell.alias_table[alias] = (nargs,cmd)
2640 self.shell.alias_table[alias] = (nargs,cmd)
2620 self.shell.alias_table_validate(verbose=0)
2641 self.shell.alias_table_validate(verbose=0)
2621 # end magic_alias
2642 # end magic_alias
2622
2643
2623 def magic_unalias(self, parameter_s = ''):
2644 def magic_unalias(self, parameter_s = ''):
2624 """Remove an alias"""
2645 """Remove an alias"""
2625
2646
2626 aname = parameter_s.strip()
2647 aname = parameter_s.strip()
2627 if aname in self.shell.alias_table:
2648 if aname in self.shell.alias_table:
2628 del self.shell.alias_table[aname]
2649 del self.shell.alias_table[aname]
2629 stored = self.db.get('stored_aliases', {} )
2650 stored = self.db.get('stored_aliases', {} )
2630 if aname in stored:
2651 if aname in stored:
2631 print "Removing %stored alias",aname
2652 print "Removing %stored alias",aname
2632 del stored[aname]
2653 del stored[aname]
2633 self.db['stored_aliases'] = stored
2654 self.db['stored_aliases'] = stored
2634
2655
2635
2656
2636 def magic_rehashx(self, parameter_s = ''):
2657 def magic_rehashx(self, parameter_s = ''):
2637 """Update the alias table with all executable files in $PATH.
2658 """Update the alias table with all executable files in $PATH.
2638
2659
2639 This version explicitly checks that every entry in $PATH is a file
2660 This version explicitly checks that every entry in $PATH is a file
2640 with execute access (os.X_OK), so it is much slower than %rehash.
2661 with execute access (os.X_OK), so it is much slower than %rehash.
2641
2662
2642 Under Windows, it checks executability as a match agains a
2663 Under Windows, it checks executability as a match agains a
2643 '|'-separated string of extensions, stored in the IPython config
2664 '|'-separated string of extensions, stored in the IPython config
2644 variable win_exec_ext. This defaults to 'exe|com|bat'.
2665 variable win_exec_ext. This defaults to 'exe|com|bat'.
2645
2666
2646 This function also resets the root module cache of module completer,
2667 This function also resets the root module cache of module completer,
2647 used on slow filesystems.
2668 used on slow filesystems.
2648 """
2669 """
2649
2670
2650
2671
2651 ip = self.api
2672 ip = self.api
2652
2673
2653 # for the benefit of module completer in ipy_completers.py
2674 # for the benefit of module completer in ipy_completers.py
2654 del ip.db['rootmodules']
2675 del ip.db['rootmodules']
2655
2676
2656 path = [os.path.abspath(os.path.expanduser(p)) for p in
2677 path = [os.path.abspath(os.path.expanduser(p)) for p in
2657 os.environ.get('PATH','').split(os.pathsep)]
2678 os.environ.get('PATH','').split(os.pathsep)]
2658 path = filter(os.path.isdir,path)
2679 path = filter(os.path.isdir,path)
2659
2680
2660 alias_table = self.shell.alias_table
2681 alias_table = self.shell.alias_table
2661 syscmdlist = []
2682 syscmdlist = []
2662 if os.name == 'posix':
2683 if os.name == 'posix':
2663 isexec = lambda fname:os.path.isfile(fname) and \
2684 isexec = lambda fname:os.path.isfile(fname) and \
2664 os.access(fname,os.X_OK)
2685 os.access(fname,os.X_OK)
2665 else:
2686 else:
2666
2687
2667 try:
2688 try:
2668 winext = os.environ['pathext'].replace(';','|').replace('.','')
2689 winext = os.environ['pathext'].replace(';','|').replace('.','')
2669 except KeyError:
2690 except KeyError:
2670 winext = 'exe|com|bat|py'
2691 winext = 'exe|com|bat|py'
2671 if 'py' not in winext:
2692 if 'py' not in winext:
2672 winext += '|py'
2693 winext += '|py'
2673 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2694 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2674 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2695 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2675 savedir = os.getcwd()
2696 savedir = os.getcwd()
2676 try:
2697 try:
2677 # write the whole loop for posix/Windows so we don't have an if in
2698 # write the whole loop for posix/Windows so we don't have an if in
2678 # the innermost part
2699 # the innermost part
2679 if os.name == 'posix':
2700 if os.name == 'posix':
2680 for pdir in path:
2701 for pdir in path:
2681 os.chdir(pdir)
2702 os.chdir(pdir)
2682 for ff in os.listdir(pdir):
2703 for ff in os.listdir(pdir):
2683 if isexec(ff) and ff not in self.shell.no_alias:
2704 if isexec(ff) and ff not in self.shell.no_alias:
2684 # each entry in the alias table must be (N,name),
2705 # each entry in the alias table must be (N,name),
2685 # where N is the number of positional arguments of the
2706 # where N is the number of positional arguments of the
2686 # alias.
2707 # alias.
2687 # Dots will be removed from alias names, since ipython
2708 # Dots will be removed from alias names, since ipython
2688 # assumes names with dots to be python code
2709 # assumes names with dots to be python code
2689 alias_table[ff.replace('.','')] = (0,ff)
2710 alias_table[ff.replace('.','')] = (0,ff)
2690 syscmdlist.append(ff)
2711 syscmdlist.append(ff)
2691 else:
2712 else:
2692 for pdir in path:
2713 for pdir in path:
2693 os.chdir(pdir)
2714 os.chdir(pdir)
2694 for ff in os.listdir(pdir):
2715 for ff in os.listdir(pdir):
2695 base, ext = os.path.splitext(ff)
2716 base, ext = os.path.splitext(ff)
2696 if isexec(ff) and base.lower() not in self.shell.no_alias:
2717 if isexec(ff) and base.lower() not in self.shell.no_alias:
2697 if ext.lower() == '.exe':
2718 if ext.lower() == '.exe':
2698 ff = base
2719 ff = base
2699 alias_table[base.lower().replace('.','')] = (0,ff)
2720 alias_table[base.lower().replace('.','')] = (0,ff)
2700 syscmdlist.append(ff)
2721 syscmdlist.append(ff)
2701 # Make sure the alias table doesn't contain keywords or builtins
2722 # Make sure the alias table doesn't contain keywords or builtins
2702 self.shell.alias_table_validate()
2723 self.shell.alias_table_validate()
2703 # Call again init_auto_alias() so we get 'rm -i' and other
2724 # Call again init_auto_alias() so we get 'rm -i' and other
2704 # modified aliases since %rehashx will probably clobber them
2725 # modified aliases since %rehashx will probably clobber them
2705
2726
2706 # no, we don't want them. if %rehashx clobbers them, good,
2727 # no, we don't want them. if %rehashx clobbers them, good,
2707 # we'll probably get better versions
2728 # we'll probably get better versions
2708 # self.shell.init_auto_alias()
2729 # self.shell.init_auto_alias()
2709 db = ip.db
2730 db = ip.db
2710 db['syscmdlist'] = syscmdlist
2731 db['syscmdlist'] = syscmdlist
2711 finally:
2732 finally:
2712 os.chdir(savedir)
2733 os.chdir(savedir)
2713
2734
2714 def magic_pwd(self, parameter_s = ''):
2735 def magic_pwd(self, parameter_s = ''):
2715 """Return the current working directory path."""
2736 """Return the current working directory path."""
2716 return os.getcwd()
2737 return os.getcwd()
2717
2738
2718 def magic_cd(self, parameter_s=''):
2739 def magic_cd(self, parameter_s=''):
2719 """Change the current working directory.
2740 """Change the current working directory.
2720
2741
2721 This command automatically maintains an internal list of directories
2742 This command automatically maintains an internal list of directories
2722 you visit during your IPython session, in the variable _dh. The
2743 you visit during your IPython session, in the variable _dh. The
2723 command %dhist shows this history nicely formatted. You can also
2744 command %dhist shows this history nicely formatted. You can also
2724 do 'cd -<tab>' to see directory history conveniently.
2745 do 'cd -<tab>' to see directory history conveniently.
2725
2746
2726 Usage:
2747 Usage:
2727
2748
2728 cd 'dir': changes to directory 'dir'.
2749 cd 'dir': changes to directory 'dir'.
2729
2750
2730 cd -: changes to the last visited directory.
2751 cd -: changes to the last visited directory.
2731
2752
2732 cd -<n>: changes to the n-th directory in the directory history.
2753 cd -<n>: changes to the n-th directory in the directory history.
2733
2754
2734 cd --foo: change to directory that matches 'foo' in history
2755 cd --foo: change to directory that matches 'foo' in history
2735
2756
2736 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2757 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2737 (note: cd <bookmark_name> is enough if there is no
2758 (note: cd <bookmark_name> is enough if there is no
2738 directory <bookmark_name>, but a bookmark with the name exists.)
2759 directory <bookmark_name>, but a bookmark with the name exists.)
2739 'cd -b <tab>' allows you to tab-complete bookmark names.
2760 'cd -b <tab>' allows you to tab-complete bookmark names.
2740
2761
2741 Options:
2762 Options:
2742
2763
2743 -q: quiet. Do not print the working directory after the cd command is
2764 -q: quiet. Do not print the working directory after the cd command is
2744 executed. By default IPython's cd command does print this directory,
2765 executed. By default IPython's cd command does print this directory,
2745 since the default prompts do not display path information.
2766 since the default prompts do not display path information.
2746
2767
2747 Note that !cd doesn't work for this purpose because the shell where
2768 Note that !cd doesn't work for this purpose because the shell where
2748 !command runs is immediately discarded after executing 'command'."""
2769 !command runs is immediately discarded after executing 'command'."""
2749
2770
2750 parameter_s = parameter_s.strip()
2771 parameter_s = parameter_s.strip()
2751 #bkms = self.shell.persist.get("bookmarks",{})
2772 #bkms = self.shell.persist.get("bookmarks",{})
2752
2773
2753 oldcwd = os.getcwd()
2774 oldcwd = os.getcwd()
2754 numcd = re.match(r'(-)(\d+)$',parameter_s)
2775 numcd = re.match(r'(-)(\d+)$',parameter_s)
2755 # jump in directory history by number
2776 # jump in directory history by number
2756 if numcd:
2777 if numcd:
2757 nn = int(numcd.group(2))
2778 nn = int(numcd.group(2))
2758 try:
2779 try:
2759 ps = self.shell.user_ns['_dh'][nn]
2780 ps = self.shell.user_ns['_dh'][nn]
2760 except IndexError:
2781 except IndexError:
2761 print 'The requested directory does not exist in history.'
2782 print 'The requested directory does not exist in history.'
2762 return
2783 return
2763 else:
2784 else:
2764 opts = {}
2785 opts = {}
2765 elif parameter_s.startswith('--'):
2786 elif parameter_s.startswith('--'):
2766 ps = None
2787 ps = None
2767 fallback = None
2788 fallback = None
2768 pat = parameter_s[2:]
2789 pat = parameter_s[2:]
2769 dh = self.shell.user_ns['_dh']
2790 dh = self.shell.user_ns['_dh']
2770 # first search only by basename (last component)
2791 # first search only by basename (last component)
2771 for ent in reversed(dh):
2792 for ent in reversed(dh):
2772 if pat in os.path.basename(ent) and os.path.isdir(ent):
2793 if pat in os.path.basename(ent) and os.path.isdir(ent):
2773 ps = ent
2794 ps = ent
2774 break
2795 break
2775
2796
2776 if fallback is None and pat in ent and os.path.isdir(ent):
2797 if fallback is None and pat in ent and os.path.isdir(ent):
2777 fallback = ent
2798 fallback = ent
2778
2799
2779 # if we have no last part match, pick the first full path match
2800 # if we have no last part match, pick the first full path match
2780 if ps is None:
2801 if ps is None:
2781 ps = fallback
2802 ps = fallback
2782
2803
2783 if ps is None:
2804 if ps is None:
2784 print "No matching entry in directory history"
2805 print "No matching entry in directory history"
2785 return
2806 return
2786 else:
2807 else:
2787 opts = {}
2808 opts = {}
2788
2809
2789
2810
2790 else:
2811 else:
2791 #turn all non-space-escaping backslashes to slashes,
2812 #turn all non-space-escaping backslashes to slashes,
2792 # for c:\windows\directory\names\
2813 # for c:\windows\directory\names\
2793 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2814 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2794 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2815 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2795 # jump to previous
2816 # jump to previous
2796 if ps == '-':
2817 if ps == '-':
2797 try:
2818 try:
2798 ps = self.shell.user_ns['_dh'][-2]
2819 ps = self.shell.user_ns['_dh'][-2]
2799 except IndexError:
2820 except IndexError:
2800 raise UsageError('%cd -: No previous directory to change to.')
2821 raise UsageError('%cd -: No previous directory to change to.')
2801 # jump to bookmark if needed
2822 # jump to bookmark if needed
2802 else:
2823 else:
2803 if not os.path.isdir(ps) or opts.has_key('b'):
2824 if not os.path.isdir(ps) or opts.has_key('b'):
2804 bkms = self.db.get('bookmarks', {})
2825 bkms = self.db.get('bookmarks', {})
2805
2826
2806 if bkms.has_key(ps):
2827 if bkms.has_key(ps):
2807 target = bkms[ps]
2828 target = bkms[ps]
2808 print '(bookmark:%s) -> %s' % (ps,target)
2829 print '(bookmark:%s) -> %s' % (ps,target)
2809 ps = target
2830 ps = target
2810 else:
2831 else:
2811 if opts.has_key('b'):
2832 if opts.has_key('b'):
2812 raise UsageError("Bookmark '%s' not found. "
2833 raise UsageError("Bookmark '%s' not found. "
2813 "Use '%%bookmark -l' to see your bookmarks." % ps)
2834 "Use '%%bookmark -l' to see your bookmarks." % ps)
2814
2835
2815 # at this point ps should point to the target dir
2836 # at this point ps should point to the target dir
2816 if ps:
2837 if ps:
2817 try:
2838 try:
2818 os.chdir(os.path.expanduser(ps))
2839 os.chdir(os.path.expanduser(ps))
2819 if self.shell.rc.term_title:
2840 if self.shell.rc.term_title:
2820 #print 'set term title:',self.shell.rc.term_title # dbg
2841 #print 'set term title:',self.shell.rc.term_title # dbg
2821 platutils.set_term_title('IPy ' + abbrev_cwd())
2842 platutils.set_term_title('IPy ' + abbrev_cwd())
2822 except OSError:
2843 except OSError:
2823 print sys.exc_info()[1]
2844 print sys.exc_info()[1]
2824 else:
2845 else:
2825 cwd = os.getcwd()
2846 cwd = os.getcwd()
2826 dhist = self.shell.user_ns['_dh']
2847 dhist = self.shell.user_ns['_dh']
2827 if oldcwd != cwd:
2848 if oldcwd != cwd:
2828 dhist.append(cwd)
2849 dhist.append(cwd)
2829 self.db['dhist'] = compress_dhist(dhist)[-100:]
2850 self.db['dhist'] = compress_dhist(dhist)[-100:]
2830
2851
2831 else:
2852 else:
2832 os.chdir(self.shell.home_dir)
2853 os.chdir(self.shell.home_dir)
2833 if self.shell.rc.term_title:
2854 if self.shell.rc.term_title:
2834 platutils.set_term_title("IPy ~")
2855 platutils.set_term_title("IPy ~")
2835 cwd = os.getcwd()
2856 cwd = os.getcwd()
2836 dhist = self.shell.user_ns['_dh']
2857 dhist = self.shell.user_ns['_dh']
2837
2858
2838 if oldcwd != cwd:
2859 if oldcwd != cwd:
2839 dhist.append(cwd)
2860 dhist.append(cwd)
2840 self.db['dhist'] = compress_dhist(dhist)[-100:]
2861 self.db['dhist'] = compress_dhist(dhist)[-100:]
2841 if not 'q' in opts and self.shell.user_ns['_dh']:
2862 if not 'q' in opts and self.shell.user_ns['_dh']:
2842 print self.shell.user_ns['_dh'][-1]
2863 print self.shell.user_ns['_dh'][-1]
2843
2864
2844
2865
2845 def magic_env(self, parameter_s=''):
2866 def magic_env(self, parameter_s=''):
2846 """List environment variables."""
2867 """List environment variables."""
2847
2868
2848 return os.environ.data
2869 return os.environ.data
2849
2870
2850 def magic_pushd(self, parameter_s=''):
2871 def magic_pushd(self, parameter_s=''):
2851 """Place the current dir on stack and change directory.
2872 """Place the current dir on stack and change directory.
2852
2873
2853 Usage:\\
2874 Usage:\\
2854 %pushd ['dirname']
2875 %pushd ['dirname']
2855 """
2876 """
2856
2877
2857 dir_s = self.shell.dir_stack
2878 dir_s = self.shell.dir_stack
2858 tgt = os.path.expanduser(parameter_s)
2879 tgt = os.path.expanduser(parameter_s)
2859 cwd = os.getcwd().replace(self.home_dir,'~')
2880 cwd = os.getcwd().replace(self.home_dir,'~')
2860 if tgt:
2881 if tgt:
2861 self.magic_cd(parameter_s)
2882 self.magic_cd(parameter_s)
2862 dir_s.insert(0,cwd)
2883 dir_s.insert(0,cwd)
2863 return self.magic_dirs()
2884 return self.magic_dirs()
2864
2885
2865 def magic_popd(self, parameter_s=''):
2886 def magic_popd(self, parameter_s=''):
2866 """Change to directory popped off the top of the stack.
2887 """Change to directory popped off the top of the stack.
2867 """
2888 """
2868 if not self.shell.dir_stack:
2889 if not self.shell.dir_stack:
2869 raise UsageError("%popd on empty stack")
2890 raise UsageError("%popd on empty stack")
2870 top = self.shell.dir_stack.pop(0)
2891 top = self.shell.dir_stack.pop(0)
2871 self.magic_cd(top)
2892 self.magic_cd(top)
2872 print "popd ->",top
2893 print "popd ->",top
2873
2894
2874 def magic_dirs(self, parameter_s=''):
2895 def magic_dirs(self, parameter_s=''):
2875 """Return the current directory stack."""
2896 """Return the current directory stack."""
2876
2897
2877 return self.shell.dir_stack
2898 return self.shell.dir_stack
2878
2899
2879 def magic_dhist(self, parameter_s=''):
2900 def magic_dhist(self, parameter_s=''):
2880 """Print your history of visited directories.
2901 """Print your history of visited directories.
2881
2902
2882 %dhist -> print full history\\
2903 %dhist -> print full history\\
2883 %dhist n -> print last n entries only\\
2904 %dhist n -> print last n entries only\\
2884 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2905 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2885
2906
2886 This history is automatically maintained by the %cd command, and
2907 This history is automatically maintained by the %cd command, and
2887 always available as the global list variable _dh. You can use %cd -<n>
2908 always available as the global list variable _dh. You can use %cd -<n>
2888 to go to directory number <n>.
2909 to go to directory number <n>.
2889
2910
2890 Note that most of time, you should view directory history by entering
2911 Note that most of time, you should view directory history by entering
2891 cd -<TAB>.
2912 cd -<TAB>.
2892
2913
2893 """
2914 """
2894
2915
2895 dh = self.shell.user_ns['_dh']
2916 dh = self.shell.user_ns['_dh']
2896 if parameter_s:
2917 if parameter_s:
2897 try:
2918 try:
2898 args = map(int,parameter_s.split())
2919 args = map(int,parameter_s.split())
2899 except:
2920 except:
2900 self.arg_err(Magic.magic_dhist)
2921 self.arg_err(Magic.magic_dhist)
2901 return
2922 return
2902 if len(args) == 1:
2923 if len(args) == 1:
2903 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2924 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2904 elif len(args) == 2:
2925 elif len(args) == 2:
2905 ini,fin = args
2926 ini,fin = args
2906 else:
2927 else:
2907 self.arg_err(Magic.magic_dhist)
2928 self.arg_err(Magic.magic_dhist)
2908 return
2929 return
2909 else:
2930 else:
2910 ini,fin = 0,len(dh)
2931 ini,fin = 0,len(dh)
2911 nlprint(dh,
2932 nlprint(dh,
2912 header = 'Directory history (kept in _dh)',
2933 header = 'Directory history (kept in _dh)',
2913 start=ini,stop=fin)
2934 start=ini,stop=fin)
2914
2935
2915 @testdec.skip_doctest
2936 @testdec.skip_doctest
2916 def magic_sc(self, parameter_s=''):
2937 def magic_sc(self, parameter_s=''):
2917 """Shell capture - execute a shell command and capture its output.
2938 """Shell capture - execute a shell command and capture its output.
2918
2939
2919 DEPRECATED. Suboptimal, retained for backwards compatibility.
2940 DEPRECATED. Suboptimal, retained for backwards compatibility.
2920
2941
2921 You should use the form 'var = !command' instead. Example:
2942 You should use the form 'var = !command' instead. Example:
2922
2943
2923 "%sc -l myfiles = ls ~" should now be written as
2944 "%sc -l myfiles = ls ~" should now be written as
2924
2945
2925 "myfiles = !ls ~"
2946 "myfiles = !ls ~"
2926
2947
2927 myfiles.s, myfiles.l and myfiles.n still apply as documented
2948 myfiles.s, myfiles.l and myfiles.n still apply as documented
2928 below.
2949 below.
2929
2950
2930 --
2951 --
2931 %sc [options] varname=command
2952 %sc [options] varname=command
2932
2953
2933 IPython will run the given command using commands.getoutput(), and
2954 IPython will run the given command using commands.getoutput(), and
2934 will then update the user's interactive namespace with a variable
2955 will then update the user's interactive namespace with a variable
2935 called varname, containing the value of the call. Your command can
2956 called varname, containing the value of the call. Your command can
2936 contain shell wildcards, pipes, etc.
2957 contain shell wildcards, pipes, etc.
2937
2958
2938 The '=' sign in the syntax is mandatory, and the variable name you
2959 The '=' sign in the syntax is mandatory, and the variable name you
2939 supply must follow Python's standard conventions for valid names.
2960 supply must follow Python's standard conventions for valid names.
2940
2961
2941 (A special format without variable name exists for internal use)
2962 (A special format without variable name exists for internal use)
2942
2963
2943 Options:
2964 Options:
2944
2965
2945 -l: list output. Split the output on newlines into a list before
2966 -l: list output. Split the output on newlines into a list before
2946 assigning it to the given variable. By default the output is stored
2967 assigning it to the given variable. By default the output is stored
2947 as a single string.
2968 as a single string.
2948
2969
2949 -v: verbose. Print the contents of the variable.
2970 -v: verbose. Print the contents of the variable.
2950
2971
2951 In most cases you should not need to split as a list, because the
2972 In most cases you should not need to split as a list, because the
2952 returned value is a special type of string which can automatically
2973 returned value is a special type of string which can automatically
2953 provide its contents either as a list (split on newlines) or as a
2974 provide its contents either as a list (split on newlines) or as a
2954 space-separated string. These are convenient, respectively, either
2975 space-separated string. These are convenient, respectively, either
2955 for sequential processing or to be passed to a shell command.
2976 for sequential processing or to be passed to a shell command.
2956
2977
2957 For example:
2978 For example:
2958
2979
2959 # all-random
2980 # all-random
2960
2981
2961 # Capture into variable a
2982 # Capture into variable a
2962 In [1]: sc a=ls *py
2983 In [1]: sc a=ls *py
2963
2984
2964 # a is a string with embedded newlines
2985 # a is a string with embedded newlines
2965 In [2]: a
2986 In [2]: a
2966 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2987 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2967
2988
2968 # which can be seen as a list:
2989 # which can be seen as a list:
2969 In [3]: a.l
2990 In [3]: a.l
2970 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2991 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2971
2992
2972 # or as a whitespace-separated string:
2993 # or as a whitespace-separated string:
2973 In [4]: a.s
2994 In [4]: a.s
2974 Out[4]: 'setup.py win32_manual_post_install.py'
2995 Out[4]: 'setup.py win32_manual_post_install.py'
2975
2996
2976 # a.s is useful to pass as a single command line:
2997 # a.s is useful to pass as a single command line:
2977 In [5]: !wc -l $a.s
2998 In [5]: !wc -l $a.s
2978 146 setup.py
2999 146 setup.py
2979 130 win32_manual_post_install.py
3000 130 win32_manual_post_install.py
2980 276 total
3001 276 total
2981
3002
2982 # while the list form is useful to loop over:
3003 # while the list form is useful to loop over:
2983 In [6]: for f in a.l:
3004 In [6]: for f in a.l:
2984 ...: !wc -l $f
3005 ...: !wc -l $f
2985 ...:
3006 ...:
2986 146 setup.py
3007 146 setup.py
2987 130 win32_manual_post_install.py
3008 130 win32_manual_post_install.py
2988
3009
2989 Similiarly, the lists returned by the -l option are also special, in
3010 Similiarly, the lists returned by the -l option are also special, in
2990 the sense that you can equally invoke the .s attribute on them to
3011 the sense that you can equally invoke the .s attribute on them to
2991 automatically get a whitespace-separated string from their contents:
3012 automatically get a whitespace-separated string from their contents:
2992
3013
2993 In [7]: sc -l b=ls *py
3014 In [7]: sc -l b=ls *py
2994
3015
2995 In [8]: b
3016 In [8]: b
2996 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3017 Out[8]: ['setup.py', 'win32_manual_post_install.py']
2997
3018
2998 In [9]: b.s
3019 In [9]: b.s
2999 Out[9]: 'setup.py win32_manual_post_install.py'
3020 Out[9]: 'setup.py win32_manual_post_install.py'
3000
3021
3001 In summary, both the lists and strings used for ouptut capture have
3022 In summary, both the lists and strings used for ouptut capture have
3002 the following special attributes:
3023 the following special attributes:
3003
3024
3004 .l (or .list) : value as list.
3025 .l (or .list) : value as list.
3005 .n (or .nlstr): value as newline-separated string.
3026 .n (or .nlstr): value as newline-separated string.
3006 .s (or .spstr): value as space-separated string.
3027 .s (or .spstr): value as space-separated string.
3007 """
3028 """
3008
3029
3009 opts,args = self.parse_options(parameter_s,'lv')
3030 opts,args = self.parse_options(parameter_s,'lv')
3010 # Try to get a variable name and command to run
3031 # Try to get a variable name and command to run
3011 try:
3032 try:
3012 # the variable name must be obtained from the parse_options
3033 # the variable name must be obtained from the parse_options
3013 # output, which uses shlex.split to strip options out.
3034 # output, which uses shlex.split to strip options out.
3014 var,_ = args.split('=',1)
3035 var,_ = args.split('=',1)
3015 var = var.strip()
3036 var = var.strip()
3016 # But the the command has to be extracted from the original input
3037 # But the the command has to be extracted from the original input
3017 # parameter_s, not on what parse_options returns, to avoid the
3038 # parameter_s, not on what parse_options returns, to avoid the
3018 # quote stripping which shlex.split performs on it.
3039 # quote stripping which shlex.split performs on it.
3019 _,cmd = parameter_s.split('=',1)
3040 _,cmd = parameter_s.split('=',1)
3020 except ValueError:
3041 except ValueError:
3021 var,cmd = '',''
3042 var,cmd = '',''
3022 # If all looks ok, proceed
3043 # If all looks ok, proceed
3023 out,err = self.shell.getoutputerror(cmd)
3044 out,err = self.shell.getoutputerror(cmd)
3024 if err:
3045 if err:
3025 print >> Term.cerr,err
3046 print >> Term.cerr,err
3026 if opts.has_key('l'):
3047 if opts.has_key('l'):
3027 out = SList(out.split('\n'))
3048 out = SList(out.split('\n'))
3028 else:
3049 else:
3029 out = LSString(out)
3050 out = LSString(out)
3030 if opts.has_key('v'):
3051 if opts.has_key('v'):
3031 print '%s ==\n%s' % (var,pformat(out))
3052 print '%s ==\n%s' % (var,pformat(out))
3032 if var:
3053 if var:
3033 self.shell.user_ns.update({var:out})
3054 self.shell.user_ns.update({var:out})
3034 else:
3055 else:
3035 return out
3056 return out
3036
3057
3037 def magic_sx(self, parameter_s=''):
3058 def magic_sx(self, parameter_s=''):
3038 """Shell execute - run a shell command and capture its output.
3059 """Shell execute - run a shell command and capture its output.
3039
3060
3040 %sx command
3061 %sx command
3041
3062
3042 IPython will run the given command using commands.getoutput(), and
3063 IPython will run the given command using commands.getoutput(), and
3043 return the result formatted as a list (split on '\\n'). Since the
3064 return the result formatted as a list (split on '\\n'). Since the
3044 output is _returned_, it will be stored in ipython's regular output
3065 output is _returned_, it will be stored in ipython's regular output
3045 cache Out[N] and in the '_N' automatic variables.
3066 cache Out[N] and in the '_N' automatic variables.
3046
3067
3047 Notes:
3068 Notes:
3048
3069
3049 1) If an input line begins with '!!', then %sx is automatically
3070 1) If an input line begins with '!!', then %sx is automatically
3050 invoked. That is, while:
3071 invoked. That is, while:
3051 !ls
3072 !ls
3052 causes ipython to simply issue system('ls'), typing
3073 causes ipython to simply issue system('ls'), typing
3053 !!ls
3074 !!ls
3054 is a shorthand equivalent to:
3075 is a shorthand equivalent to:
3055 %sx ls
3076 %sx ls
3056
3077
3057 2) %sx differs from %sc in that %sx automatically splits into a list,
3078 2) %sx differs from %sc in that %sx automatically splits into a list,
3058 like '%sc -l'. The reason for this is to make it as easy as possible
3079 like '%sc -l'. The reason for this is to make it as easy as possible
3059 to process line-oriented shell output via further python commands.
3080 to process line-oriented shell output via further python commands.
3060 %sc is meant to provide much finer control, but requires more
3081 %sc is meant to provide much finer control, but requires more
3061 typing.
3082 typing.
3062
3083
3063 3) Just like %sc -l, this is a list with special attributes:
3084 3) Just like %sc -l, this is a list with special attributes:
3064
3085
3065 .l (or .list) : value as list.
3086 .l (or .list) : value as list.
3066 .n (or .nlstr): value as newline-separated string.
3087 .n (or .nlstr): value as newline-separated string.
3067 .s (or .spstr): value as whitespace-separated string.
3088 .s (or .spstr): value as whitespace-separated string.
3068
3089
3069 This is very useful when trying to use such lists as arguments to
3090 This is very useful when trying to use such lists as arguments to
3070 system commands."""
3091 system commands."""
3071
3092
3072 if parameter_s:
3093 if parameter_s:
3073 out,err = self.shell.getoutputerror(parameter_s)
3094 out,err = self.shell.getoutputerror(parameter_s)
3074 if err:
3095 if err:
3075 print >> Term.cerr,err
3096 print >> Term.cerr,err
3076 return SList(out.split('\n'))
3097 return SList(out.split('\n'))
3077
3098
3078 def magic_bg(self, parameter_s=''):
3099 def magic_bg(self, parameter_s=''):
3079 """Run a job in the background, in a separate thread.
3100 """Run a job in the background, in a separate thread.
3080
3101
3081 For example,
3102 For example,
3082
3103
3083 %bg myfunc(x,y,z=1)
3104 %bg myfunc(x,y,z=1)
3084
3105
3085 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3106 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3086 execution starts, a message will be printed indicating the job
3107 execution starts, a message will be printed indicating the job
3087 number. If your job number is 5, you can use
3108 number. If your job number is 5, you can use
3088
3109
3089 myvar = jobs.result(5) or myvar = jobs[5].result
3110 myvar = jobs.result(5) or myvar = jobs[5].result
3090
3111
3091 to assign this result to variable 'myvar'.
3112 to assign this result to variable 'myvar'.
3092
3113
3093 IPython has a job manager, accessible via the 'jobs' object. You can
3114 IPython has a job manager, accessible via the 'jobs' object. You can
3094 type jobs? to get more information about it, and use jobs.<TAB> to see
3115 type jobs? to get more information about it, and use jobs.<TAB> to see
3095 its attributes. All attributes not starting with an underscore are
3116 its attributes. All attributes not starting with an underscore are
3096 meant for public use.
3117 meant for public use.
3097
3118
3098 In particular, look at the jobs.new() method, which is used to create
3119 In particular, look at the jobs.new() method, which is used to create
3099 new jobs. This magic %bg function is just a convenience wrapper
3120 new jobs. This magic %bg function is just a convenience wrapper
3100 around jobs.new(), for expression-based jobs. If you want to create a
3121 around jobs.new(), for expression-based jobs. If you want to create a
3101 new job with an explicit function object and arguments, you must call
3122 new job with an explicit function object and arguments, you must call
3102 jobs.new() directly.
3123 jobs.new() directly.
3103
3124
3104 The jobs.new docstring also describes in detail several important
3125 The jobs.new docstring also describes in detail several important
3105 caveats associated with a thread-based model for background job
3126 caveats associated with a thread-based model for background job
3106 execution. Type jobs.new? for details.
3127 execution. Type jobs.new? for details.
3107
3128
3108 You can check the status of all jobs with jobs.status().
3129 You can check the status of all jobs with jobs.status().
3109
3130
3110 The jobs variable is set by IPython into the Python builtin namespace.
3131 The jobs variable is set by IPython into the Python builtin namespace.
3111 If you ever declare a variable named 'jobs', you will shadow this
3132 If you ever declare a variable named 'jobs', you will shadow this
3112 name. You can either delete your global jobs variable to regain
3133 name. You can either delete your global jobs variable to regain
3113 access to the job manager, or make a new name and assign it manually
3134 access to the job manager, or make a new name and assign it manually
3114 to the manager (stored in IPython's namespace). For example, to
3135 to the manager (stored in IPython's namespace). For example, to
3115 assign the job manager to the Jobs name, use:
3136 assign the job manager to the Jobs name, use:
3116
3137
3117 Jobs = __builtins__.jobs"""
3138 Jobs = __builtins__.jobs"""
3118
3139
3119 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3140 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3120
3141
3121 def magic_r(self, parameter_s=''):
3142 def magic_r(self, parameter_s=''):
3122 """Repeat previous input.
3143 """Repeat previous input.
3123
3144
3124 Note: Consider using the more powerfull %rep instead!
3145 Note: Consider using the more powerfull %rep instead!
3125
3146
3126 If given an argument, repeats the previous command which starts with
3147 If given an argument, repeats the previous command which starts with
3127 the same string, otherwise it just repeats the previous input.
3148 the same string, otherwise it just repeats the previous input.
3128
3149
3129 Shell escaped commands (with ! as first character) are not recognized
3150 Shell escaped commands (with ! as first character) are not recognized
3130 by this system, only pure python code and magic commands.
3151 by this system, only pure python code and magic commands.
3131 """
3152 """
3132
3153
3133 start = parameter_s.strip()
3154 start = parameter_s.strip()
3134 esc_magic = self.shell.ESC_MAGIC
3155 esc_magic = self.shell.ESC_MAGIC
3135 # Identify magic commands even if automagic is on (which means
3156 # Identify magic commands even if automagic is on (which means
3136 # the in-memory version is different from that typed by the user).
3157 # the in-memory version is different from that typed by the user).
3137 if self.shell.rc.automagic:
3158 if self.shell.rc.automagic:
3138 start_magic = esc_magic+start
3159 start_magic = esc_magic+start
3139 else:
3160 else:
3140 start_magic = start
3161 start_magic = start
3141 # Look through the input history in reverse
3162 # Look through the input history in reverse
3142 for n in range(len(self.shell.input_hist)-2,0,-1):
3163 for n in range(len(self.shell.input_hist)-2,0,-1):
3143 input = self.shell.input_hist[n]
3164 input = self.shell.input_hist[n]
3144 # skip plain 'r' lines so we don't recurse to infinity
3165 # skip plain 'r' lines so we don't recurse to infinity
3145 if input != '_ip.magic("r")\n' and \
3166 if input != '_ip.magic("r")\n' and \
3146 (input.startswith(start) or input.startswith(start_magic)):
3167 (input.startswith(start) or input.startswith(start_magic)):
3147 #print 'match',`input` # dbg
3168 #print 'match',`input` # dbg
3148 print 'Executing:',input,
3169 print 'Executing:',input,
3149 self.shell.runlines(input)
3170 self.shell.runlines(input)
3150 return
3171 return
3151 print 'No previous input matching `%s` found.' % start
3172 print 'No previous input matching `%s` found.' % start
3152
3173
3153
3174
3154 def magic_bookmark(self, parameter_s=''):
3175 def magic_bookmark(self, parameter_s=''):
3155 """Manage IPython's bookmark system.
3176 """Manage IPython's bookmark system.
3156
3177
3157 %bookmark <name> - set bookmark to current dir
3178 %bookmark <name> - set bookmark to current dir
3158 %bookmark <name> <dir> - set bookmark to <dir>
3179 %bookmark <name> <dir> - set bookmark to <dir>
3159 %bookmark -l - list all bookmarks
3180 %bookmark -l - list all bookmarks
3160 %bookmark -d <name> - remove bookmark
3181 %bookmark -d <name> - remove bookmark
3161 %bookmark -r - remove all bookmarks
3182 %bookmark -r - remove all bookmarks
3162
3183
3163 You can later on access a bookmarked folder with:
3184 You can later on access a bookmarked folder with:
3164 %cd -b <name>
3185 %cd -b <name>
3165 or simply '%cd <name>' if there is no directory called <name> AND
3186 or simply '%cd <name>' if there is no directory called <name> AND
3166 there is such a bookmark defined.
3187 there is such a bookmark defined.
3167
3188
3168 Your bookmarks persist through IPython sessions, but they are
3189 Your bookmarks persist through IPython sessions, but they are
3169 associated with each profile."""
3190 associated with each profile."""
3170
3191
3171 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3192 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3172 if len(args) > 2:
3193 if len(args) > 2:
3173 raise UsageError("%bookmark: too many arguments")
3194 raise UsageError("%bookmark: too many arguments")
3174
3195
3175 bkms = self.db.get('bookmarks',{})
3196 bkms = self.db.get('bookmarks',{})
3176
3197
3177 if opts.has_key('d'):
3198 if opts.has_key('d'):
3178 try:
3199 try:
3179 todel = args[0]
3200 todel = args[0]
3180 except IndexError:
3201 except IndexError:
3181 raise UsageError(
3202 raise UsageError(
3182 "%bookmark -d: must provide a bookmark to delete")
3203 "%bookmark -d: must provide a bookmark to delete")
3183 else:
3204 else:
3184 try:
3205 try:
3185 del bkms[todel]
3206 del bkms[todel]
3186 except KeyError:
3207 except KeyError:
3187 raise UsageError(
3208 raise UsageError(
3188 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3209 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3189
3210
3190 elif opts.has_key('r'):
3211 elif opts.has_key('r'):
3191 bkms = {}
3212 bkms = {}
3192 elif opts.has_key('l'):
3213 elif opts.has_key('l'):
3193 bks = bkms.keys()
3214 bks = bkms.keys()
3194 bks.sort()
3215 bks.sort()
3195 if bks:
3216 if bks:
3196 size = max(map(len,bks))
3217 size = max(map(len,bks))
3197 else:
3218 else:
3198 size = 0
3219 size = 0
3199 fmt = '%-'+str(size)+'s -> %s'
3220 fmt = '%-'+str(size)+'s -> %s'
3200 print 'Current bookmarks:'
3221 print 'Current bookmarks:'
3201 for bk in bks:
3222 for bk in bks:
3202 print fmt % (bk,bkms[bk])
3223 print fmt % (bk,bkms[bk])
3203 else:
3224 else:
3204 if not args:
3225 if not args:
3205 raise UsageError("%bookmark: You must specify the bookmark name")
3226 raise UsageError("%bookmark: You must specify the bookmark name")
3206 elif len(args)==1:
3227 elif len(args)==1:
3207 bkms[args[0]] = os.getcwd()
3228 bkms[args[0]] = os.getcwd()
3208 elif len(args)==2:
3229 elif len(args)==2:
3209 bkms[args[0]] = args[1]
3230 bkms[args[0]] = args[1]
3210 self.db['bookmarks'] = bkms
3231 self.db['bookmarks'] = bkms
3211
3232
3212 def magic_pycat(self, parameter_s=''):
3233 def magic_pycat(self, parameter_s=''):
3213 """Show a syntax-highlighted file through a pager.
3234 """Show a syntax-highlighted file through a pager.
3214
3235
3215 This magic is similar to the cat utility, but it will assume the file
3236 This magic is similar to the cat utility, but it will assume the file
3216 to be Python source and will show it with syntax highlighting. """
3237 to be Python source and will show it with syntax highlighting. """
3217
3238
3218 try:
3239 try:
3219 filename = get_py_filename(parameter_s)
3240 filename = get_py_filename(parameter_s)
3220 cont = file_read(filename)
3241 cont = file_read(filename)
3221 except IOError:
3242 except IOError:
3222 try:
3243 try:
3223 cont = eval(parameter_s,self.user_ns)
3244 cont = eval(parameter_s,self.user_ns)
3224 except NameError:
3245 except NameError:
3225 cont = None
3246 cont = None
3226 if cont is None:
3247 if cont is None:
3227 print "Error: no such file or variable"
3248 print "Error: no such file or variable"
3228 return
3249 return
3229
3250
3230 page(self.shell.pycolorize(cont),
3251 page(self.shell.pycolorize(cont),
3231 screen_lines=self.shell.rc.screen_length)
3252 screen_lines=self.shell.rc.screen_length)
3232
3253
3233 def magic_cpaste(self, parameter_s=''):
3254 def magic_cpaste(self, parameter_s=''):
3234 """Allows you to paste & execute a pre-formatted code block from clipboard.
3255 """Allows you to paste & execute a pre-formatted code block from clipboard.
3235
3256
3236 You must terminate the block with '--' (two minus-signs) alone on the
3257 You must terminate the block with '--' (two minus-signs) alone on the
3237 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3258 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3238 is the new sentinel for this operation)
3259 is the new sentinel for this operation)
3239
3260
3240 The block is dedented prior to execution to enable execution of method
3261 The block is dedented prior to execution to enable execution of method
3241 definitions. '>' and '+' characters at the beginning of a line are
3262 definitions. '>' and '+' characters at the beginning of a line are
3242 ignored, to allow pasting directly from e-mails, diff files and
3263 ignored, to allow pasting directly from e-mails, diff files and
3243 doctests (the '...' continuation prompt is also stripped). The
3264 doctests (the '...' continuation prompt is also stripped). The
3244 executed block is also assigned to variable named 'pasted_block' for
3265 executed block is also assigned to variable named 'pasted_block' for
3245 later editing with '%edit pasted_block'.
3266 later editing with '%edit pasted_block'.
3246
3267
3247 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3268 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3248 This assigns the pasted block to variable 'foo' as string, without
3269 This assigns the pasted block to variable 'foo' as string, without
3249 dedenting or executing it (preceding >>> and + is still stripped)
3270 dedenting or executing it (preceding >>> and + is still stripped)
3250
3271
3251 '%cpaste -r' re-executes the block previously entered by cpaste.
3272 '%cpaste -r' re-executes the block previously entered by cpaste.
3252
3273
3253 Do not be alarmed by garbled output on Windows (it's a readline bug).
3274 Do not be alarmed by garbled output on Windows (it's a readline bug).
3254 Just press enter and type -- (and press enter again) and the block
3275 Just press enter and type -- (and press enter again) and the block
3255 will be what was just pasted.
3276 will be what was just pasted.
3256
3277
3257 IPython statements (magics, shell escapes) are not supported (yet).
3278 IPython statements (magics, shell escapes) are not supported (yet).
3258 """
3279 """
3259 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3280 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3260 par = args.strip()
3281 par = args.strip()
3261 if opts.has_key('r'):
3282 if opts.has_key('r'):
3262 b = self.user_ns.get('pasted_block', None)
3283 b = self.user_ns.get('pasted_block', None)
3263 if b is None:
3284 if b is None:
3264 raise UsageError('No previous pasted block available')
3285 raise UsageError('No previous pasted block available')
3265 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3286 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3266 exec b in self.user_ns
3287 exec b in self.user_ns
3267 return
3288 return
3268
3289
3269 sentinel = opts.get('s','--')
3290 sentinel = opts.get('s','--')
3270
3291
3271 # Regular expressions that declare text we strip from the input:
3292 # Regular expressions that declare text we strip from the input:
3272 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3293 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3273 r'^\s*(\s?>)+', # Python input prompt
3294 r'^\s*(\s?>)+', # Python input prompt
3274 r'^\s*\.{3,}', # Continuation prompts
3295 r'^\s*\.{3,}', # Continuation prompts
3275 r'^\++',
3296 r'^\++',
3276 ]
3297 ]
3277
3298
3278 strip_from_start = map(re.compile,strip_re)
3299 strip_from_start = map(re.compile,strip_re)
3279
3300
3280 from IPython import iplib
3301 from IPython import iplib
3281 lines = []
3302 lines = []
3282 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3303 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3283 while 1:
3304 while 1:
3284 l = iplib.raw_input_original(':')
3305 l = iplib.raw_input_original(':')
3285 if l ==sentinel:
3306 if l ==sentinel:
3286 break
3307 break
3287
3308
3288 for pat in strip_from_start:
3309 for pat in strip_from_start:
3289 l = pat.sub('',l)
3310 l = pat.sub('',l)
3290 lines.append(l)
3311 lines.append(l)
3291
3312
3292 block = "\n".join(lines) + '\n'
3313 block = "\n".join(lines) + '\n'
3293 #print "block:\n",block
3314 #print "block:\n",block
3294 if not par:
3315 if not par:
3295 b = textwrap.dedent(block)
3316 b = textwrap.dedent(block)
3296 self.user_ns['pasted_block'] = b
3317 self.user_ns['pasted_block'] = b
3297 exec b in self.user_ns
3318 exec b in self.user_ns
3298 else:
3319 else:
3299 self.user_ns[par] = SList(block.splitlines())
3320 self.user_ns[par] = SList(block.splitlines())
3300 print "Block assigned to '%s'" % par
3321 print "Block assigned to '%s'" % par
3301
3322
3302 def magic_quickref(self,arg):
3323 def magic_quickref(self,arg):
3303 """ Show a quick reference sheet """
3324 """ Show a quick reference sheet """
3304 import IPython.usage
3325 import IPython.usage
3305 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
3326 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
3306
3327
3307 page(qr)
3328 page(qr)
3308
3329
3309 def magic_upgrade(self,arg):
3330 def magic_upgrade(self,arg):
3310 """ Upgrade your IPython installation
3331 """ Upgrade your IPython installation
3311
3332
3312 This will copy the config files that don't yet exist in your
3333 This will copy the config files that don't yet exist in your
3313 ipython dir from the system config dir. Use this after upgrading
3334 ipython dir from the system config dir. Use this after upgrading
3314 IPython if you don't wish to delete your .ipython dir.
3335 IPython if you don't wish to delete your .ipython dir.
3315
3336
3316 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3337 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3317 new users)
3338 new users)
3318
3339
3319 """
3340 """
3320 ip = self.getapi()
3341 ip = self.getapi()
3321 ipinstallation = path(IPython.__file__).dirname()
3342 ipinstallation = path(IPython.__file__).dirname()
3322 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py')
3343 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py')
3323 src_config = ipinstallation / 'UserConfig'
3344 src_config = ipinstallation / 'UserConfig'
3324 userdir = path(ip.options.ipythondir)
3345 userdir = path(ip.options.ipythondir)
3325 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3346 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3326 print ">",cmd
3347 print ">",cmd
3327 shell(cmd)
3348 shell(cmd)
3328 if arg == '-nolegacy':
3349 if arg == '-nolegacy':
3329 legacy = userdir.files('ipythonrc*')
3350 legacy = userdir.files('ipythonrc*')
3330 print "Nuking legacy files:",legacy
3351 print "Nuking legacy files:",legacy
3331
3352
3332 [p.remove() for p in legacy]
3353 [p.remove() for p in legacy]
3333 suffix = (sys.platform == 'win32' and '.ini' or '')
3354 suffix = (sys.platform == 'win32' and '.ini' or '')
3334 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3355 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3335
3356
3336
3357
3337 def magic_doctest_mode(self,parameter_s=''):
3358 def magic_doctest_mode(self,parameter_s=''):
3338 """Toggle doctest mode on and off.
3359 """Toggle doctest mode on and off.
3339
3360
3340 This mode allows you to toggle the prompt behavior between normal
3361 This mode allows you to toggle the prompt behavior between normal
3341 IPython prompts and ones that are as similar to the default IPython
3362 IPython prompts and ones that are as similar to the default IPython
3342 interpreter as possible.
3363 interpreter as possible.
3343
3364
3344 It also supports the pasting of code snippets that have leading '>>>'
3365 It also supports the pasting of code snippets that have leading '>>>'
3345 and '...' prompts in them. This means that you can paste doctests from
3366 and '...' prompts in them. This means that you can paste doctests from
3346 files or docstrings (even if they have leading whitespace), and the
3367 files or docstrings (even if they have leading whitespace), and the
3347 code will execute correctly. You can then use '%history -tn' to see
3368 code will execute correctly. You can then use '%history -tn' to see
3348 the translated history without line numbers; this will give you the
3369 the translated history without line numbers; this will give you the
3349 input after removal of all the leading prompts and whitespace, which
3370 input after removal of all the leading prompts and whitespace, which
3350 can be pasted back into an editor.
3371 can be pasted back into an editor.
3351
3372
3352 With these features, you can switch into this mode easily whenever you
3373 With these features, you can switch into this mode easily whenever you
3353 need to do testing and changes to doctests, without having to leave
3374 need to do testing and changes to doctests, without having to leave
3354 your existing IPython session.
3375 your existing IPython session.
3355 """
3376 """
3356
3377
3357 # XXX - Fix this to have cleaner activate/deactivate calls.
3378 # XXX - Fix this to have cleaner activate/deactivate calls.
3358 from IPython.Extensions import InterpreterPasteInput as ipaste
3379 from IPython.Extensions import InterpreterPasteInput as ipaste
3359 from IPython.ipstruct import Struct
3380 from IPython.ipstruct import Struct
3360
3381
3361 # Shorthands
3382 # Shorthands
3362 shell = self.shell
3383 shell = self.shell
3363 oc = shell.outputcache
3384 oc = shell.outputcache
3364 rc = shell.rc
3385 rc = shell.rc
3365 meta = shell.meta
3386 meta = shell.meta
3366 # dstore is a data store kept in the instance metadata bag to track any
3387 # dstore is a data store kept in the instance metadata bag to track any
3367 # changes we make, so we can undo them later.
3388 # changes we make, so we can undo them later.
3368 dstore = meta.setdefault('doctest_mode',Struct())
3389 dstore = meta.setdefault('doctest_mode',Struct())
3369 save_dstore = dstore.setdefault
3390 save_dstore = dstore.setdefault
3370
3391
3371 # save a few values we'll need to recover later
3392 # save a few values we'll need to recover later
3372 mode = save_dstore('mode',False)
3393 mode = save_dstore('mode',False)
3373 save_dstore('rc_pprint',rc.pprint)
3394 save_dstore('rc_pprint',rc.pprint)
3374 save_dstore('xmode',shell.InteractiveTB.mode)
3395 save_dstore('xmode',shell.InteractiveTB.mode)
3375 save_dstore('rc_separate_out',rc.separate_out)
3396 save_dstore('rc_separate_out',rc.separate_out)
3376 save_dstore('rc_separate_out2',rc.separate_out2)
3397 save_dstore('rc_separate_out2',rc.separate_out2)
3377 save_dstore('rc_prompts_pad_left',rc.prompts_pad_left)
3398 save_dstore('rc_prompts_pad_left',rc.prompts_pad_left)
3378 save_dstore('rc_separate_in',rc.separate_in)
3399 save_dstore('rc_separate_in',rc.separate_in)
3379
3400
3380 if mode == False:
3401 if mode == False:
3381 # turn on
3402 # turn on
3382 ipaste.activate_prefilter()
3403 ipaste.activate_prefilter()
3383
3404
3384 oc.prompt1.p_template = '>>> '
3405 oc.prompt1.p_template = '>>> '
3385 oc.prompt2.p_template = '... '
3406 oc.prompt2.p_template = '... '
3386 oc.prompt_out.p_template = ''
3407 oc.prompt_out.p_template = ''
3387
3408
3388 # Prompt separators like plain python
3409 # Prompt separators like plain python
3389 oc.input_sep = oc.prompt1.sep = ''
3410 oc.input_sep = oc.prompt1.sep = ''
3390 oc.output_sep = ''
3411 oc.output_sep = ''
3391 oc.output_sep2 = ''
3412 oc.output_sep2 = ''
3392
3413
3393 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3414 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3394 oc.prompt_out.pad_left = False
3415 oc.prompt_out.pad_left = False
3395
3416
3396 rc.pprint = False
3417 rc.pprint = False
3397
3418
3398 shell.magic_xmode('Plain')
3419 shell.magic_xmode('Plain')
3399
3420
3400 else:
3421 else:
3401 # turn off
3422 # turn off
3402 ipaste.deactivate_prefilter()
3423 ipaste.deactivate_prefilter()
3403
3424
3404 oc.prompt1.p_template = rc.prompt_in1
3425 oc.prompt1.p_template = rc.prompt_in1
3405 oc.prompt2.p_template = rc.prompt_in2
3426 oc.prompt2.p_template = rc.prompt_in2
3406 oc.prompt_out.p_template = rc.prompt_out
3427 oc.prompt_out.p_template = rc.prompt_out
3407
3428
3408 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3429 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3409
3430
3410 oc.output_sep = dstore.rc_separate_out
3431 oc.output_sep = dstore.rc_separate_out
3411 oc.output_sep2 = dstore.rc_separate_out2
3432 oc.output_sep2 = dstore.rc_separate_out2
3412
3433
3413 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3434 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3414 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3435 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3415
3436
3416 rc.pprint = dstore.rc_pprint
3437 rc.pprint = dstore.rc_pprint
3417
3438
3418 shell.magic_xmode(dstore.xmode)
3439 shell.magic_xmode(dstore.xmode)
3419
3440
3420 # Store new mode and inform
3441 # Store new mode and inform
3421 dstore.mode = bool(1-int(mode))
3442 dstore.mode = bool(1-int(mode))
3422 print 'Doctest mode is:',
3443 print 'Doctest mode is:',
3423 print ['OFF','ON'][dstore.mode]
3444 print ['OFF','ON'][dstore.mode]
3424
3445
3425 # end Magic
3446 # end Magic
@@ -1,622 +1,622 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4 """
4 """
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2008-2009 The IPython Development Team
7 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Required modules
15 # Required modules
16 import __builtin__
16 import __builtin__
17 import os
17 import os
18 import socket
18 import socket
19 import sys
19 import sys
20 import time
20 import time
21
21
22 # IPython's own
22 # IPython's own
23 from IPython import ColorANSI
23 from IPython import ColorANSI
24 from IPython import Release
24 from IPython import Release
25 from IPython.external.Itpl import ItplNS
25 from IPython.external.Itpl import ItplNS
26 from IPython.ipapi import TryNext
26 from IPython.ipapi import TryNext
27 from IPython.ipstruct import Struct
27 from IPython.ipstruct import Struct
28 from IPython.macro import Macro
28 from IPython.macro import Macro
29
29
30 from IPython.genutils import *
30 from IPython.genutils import *
31
31
32 #****************************************************************************
32 #****************************************************************************
33 #Color schemes for Prompts.
33 #Color schemes for Prompts.
34
34
35 PromptColors = ColorANSI.ColorSchemeTable()
35 PromptColors = ColorANSI.ColorSchemeTable()
36 InputColors = ColorANSI.InputTermColors # just a shorthand
36 InputColors = ColorANSI.InputTermColors # just a shorthand
37 Colors = ColorANSI.TermColors # just a shorthand
37 Colors = ColorANSI.TermColors # just a shorthand
38
38
39 PromptColors.add_scheme(ColorANSI.ColorScheme(
39 PromptColors.add_scheme(ColorANSI.ColorScheme(
40 'NoColor',
40 'NoColor',
41 in_prompt = InputColors.NoColor, # Input prompt
41 in_prompt = InputColors.NoColor, # Input prompt
42 in_number = InputColors.NoColor, # Input prompt number
42 in_number = InputColors.NoColor, # Input prompt number
43 in_prompt2 = InputColors.NoColor, # Continuation prompt
43 in_prompt2 = InputColors.NoColor, # Continuation prompt
44 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
44 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
45
45
46 out_prompt = Colors.NoColor, # Output prompt
46 out_prompt = Colors.NoColor, # Output prompt
47 out_number = Colors.NoColor, # Output prompt number
47 out_number = Colors.NoColor, # Output prompt number
48
48
49 normal = Colors.NoColor # color off (usu. Colors.Normal)
49 normal = Colors.NoColor # color off (usu. Colors.Normal)
50 ))
50 ))
51
51
52 # make some schemes as instances so we can copy them for modification easily:
52 # make some schemes as instances so we can copy them for modification easily:
53 __PColLinux = ColorANSI.ColorScheme(
53 __PColLinux = ColorANSI.ColorScheme(
54 'Linux',
54 'Linux',
55 in_prompt = InputColors.Green,
55 in_prompt = InputColors.Green,
56 in_number = InputColors.LightGreen,
56 in_number = InputColors.LightGreen,
57 in_prompt2 = InputColors.Green,
57 in_prompt2 = InputColors.Green,
58 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
58 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
59
59
60 out_prompt = Colors.Red,
60 out_prompt = Colors.Red,
61 out_number = Colors.LightRed,
61 out_number = Colors.LightRed,
62
62
63 normal = Colors.Normal
63 normal = Colors.Normal
64 )
64 )
65 # Don't forget to enter it into the table!
65 # Don't forget to enter it into the table!
66 PromptColors.add_scheme(__PColLinux)
66 PromptColors.add_scheme(__PColLinux)
67
67
68 # Slightly modified Linux for light backgrounds
68 # Slightly modified Linux for light backgrounds
69 __PColLightBG = __PColLinux.copy('LightBG')
69 __PColLightBG = __PColLinux.copy('LightBG')
70
70
71 __PColLightBG.colors.update(
71 __PColLightBG.colors.update(
72 in_prompt = InputColors.Blue,
72 in_prompt = InputColors.Blue,
73 in_number = InputColors.LightBlue,
73 in_number = InputColors.LightBlue,
74 in_prompt2 = InputColors.Blue
74 in_prompt2 = InputColors.Blue
75 )
75 )
76 PromptColors.add_scheme(__PColLightBG)
76 PromptColors.add_scheme(__PColLightBG)
77
77
78 del Colors,InputColors
78 del Colors,InputColors
79
79
80 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
81 def multiple_replace(dict, text):
81 def multiple_replace(dict, text):
82 """ Replace in 'text' all occurences of any key in the given
82 """ Replace in 'text' all occurences of any key in the given
83 dictionary by its corresponding value. Returns the new string."""
83 dictionary by its corresponding value. Returns the new string."""
84
84
85 # Function by Xavier Defrang, originally found at:
85 # Function by Xavier Defrang, originally found at:
86 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
86 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
87
87
88 # Create a regular expression from the dictionary keys
88 # Create a regular expression from the dictionary keys
89 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
89 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
90 # For each match, look-up corresponding value in dictionary
90 # For each match, look-up corresponding value in dictionary
91 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
91 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
92
92
93 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
94 # Special characters that can be used in prompt templates, mainly bash-like
94 # Special characters that can be used in prompt templates, mainly bash-like
95
95
96 # If $HOME isn't defined (Windows), make it an absurd string so that it can
96 # If $HOME isn't defined (Windows), make it an absurd string so that it can
97 # never be expanded out into '~'. Basically anything which can never be a
97 # never be expanded out into '~'. Basically anything which can never be a
98 # reasonable directory name will do, we just want the $HOME -> '~' operation
98 # reasonable directory name will do, we just want the $HOME -> '~' operation
99 # to become a no-op. We pre-compute $HOME here so it's not done on every
99 # to become a no-op. We pre-compute $HOME here so it's not done on every
100 # prompt call.
100 # prompt call.
101
101
102 # FIXME:
102 # FIXME:
103
103
104 # - This should be turned into a class which does proper namespace management,
104 # - This should be turned into a class which does proper namespace management,
105 # since the prompt specials need to be evaluated in a certain namespace.
105 # since the prompt specials need to be evaluated in a certain namespace.
106 # Currently it's just globals, which need to be managed manually by code
106 # Currently it's just globals, which need to be managed manually by code
107 # below.
107 # below.
108
108
109 # - I also need to split up the color schemes from the prompt specials
109 # - I also need to split up the color schemes from the prompt specials
110 # somehow. I don't have a clean design for that quite yet.
110 # somehow. I don't have a clean design for that quite yet.
111
111
112 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
112 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
113
113
114 # We precompute a few more strings here for the prompt_specials, which are
114 # We precompute a few more strings here for the prompt_specials, which are
115 # fixed once ipython starts. This reduces the runtime overhead of computing
115 # fixed once ipython starts. This reduces the runtime overhead of computing
116 # prompt strings.
116 # prompt strings.
117 USER = os.environ.get("USER")
117 USER = os.environ.get("USER")
118 HOSTNAME = socket.gethostname()
118 HOSTNAME = socket.gethostname()
119 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
119 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
120 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
120 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
121
121
122 prompt_specials_color = {
122 prompt_specials_color = {
123 # Prompt/history count
123 # Prompt/history count
124 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
124 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
126 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
126 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
127 # can get numbers displayed in whatever color they want.
127 # can get numbers displayed in whatever color they want.
128 r'\N': '${self.cache.prompt_count}',
128 r'\N': '${self.cache.prompt_count}',
129 # Prompt/history count, with the actual digits replaced by dots. Used
129 # Prompt/history count, with the actual digits replaced by dots. Used
130 # mainly in continuation prompts (prompt_in2)
130 # mainly in continuation prompts (prompt_in2)
131 r'\D': '${"."*len(str(self.cache.prompt_count))}',
131 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
132 # Current working directory
132 # Current working directory
133 r'\w': '${os.getcwd()}',
133 r'\w': '${os.getcwd()}',
134 # Current time
134 # Current time
135 r'\t' : '${time.strftime("%H:%M:%S")}',
135 r'\t' : '${time.strftime("%H:%M:%S")}',
136 # Basename of current working directory.
136 # Basename of current working directory.
137 # (use os.sep to make this portable across OSes)
137 # (use os.sep to make this portable across OSes)
138 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
138 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
139 # These X<N> are an extension to the normal bash prompts. They return
139 # These X<N> are an extension to the normal bash prompts. They return
140 # N terms of the path, after replacing $HOME with '~'
140 # N terms of the path, after replacing $HOME with '~'
141 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
141 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
142 r'\X1': '${self.cwd_filt(1)}',
142 r'\X1': '${self.cwd_filt(1)}',
143 r'\X2': '${self.cwd_filt(2)}',
143 r'\X2': '${self.cwd_filt(2)}',
144 r'\X3': '${self.cwd_filt(3)}',
144 r'\X3': '${self.cwd_filt(3)}',
145 r'\X4': '${self.cwd_filt(4)}',
145 r'\X4': '${self.cwd_filt(4)}',
146 r'\X5': '${self.cwd_filt(5)}',
146 r'\X5': '${self.cwd_filt(5)}',
147 # Y<N> are similar to X<N>, but they show '~' if it's the directory
147 # Y<N> are similar to X<N>, but they show '~' if it's the directory
148 # N+1 in the list. Somewhat like %cN in tcsh.
148 # N+1 in the list. Somewhat like %cN in tcsh.
149 r'\Y0': '${self.cwd_filt2(0)}',
149 r'\Y0': '${self.cwd_filt2(0)}',
150 r'\Y1': '${self.cwd_filt2(1)}',
150 r'\Y1': '${self.cwd_filt2(1)}',
151 r'\Y2': '${self.cwd_filt2(2)}',
151 r'\Y2': '${self.cwd_filt2(2)}',
152 r'\Y3': '${self.cwd_filt2(3)}',
152 r'\Y3': '${self.cwd_filt2(3)}',
153 r'\Y4': '${self.cwd_filt2(4)}',
153 r'\Y4': '${self.cwd_filt2(4)}',
154 r'\Y5': '${self.cwd_filt2(5)}',
154 r'\Y5': '${self.cwd_filt2(5)}',
155 # Hostname up to first .
155 # Hostname up to first .
156 r'\h': HOSTNAME_SHORT,
156 r'\h': HOSTNAME_SHORT,
157 # Full hostname
157 # Full hostname
158 r'\H': HOSTNAME,
158 r'\H': HOSTNAME,
159 # Username of current user
159 # Username of current user
160 r'\u': USER,
160 r'\u': USER,
161 # Escaped '\'
161 # Escaped '\'
162 '\\\\': '\\',
162 '\\\\': '\\',
163 # Newline
163 # Newline
164 r'\n': '\n',
164 r'\n': '\n',
165 # Carriage return
165 # Carriage return
166 r'\r': '\r',
166 r'\r': '\r',
167 # Release version
167 # Release version
168 r'\v': Release.version,
168 r'\v': Release.version,
169 # Root symbol ($ or #)
169 # Root symbol ($ or #)
170 r'\$': ROOT_SYMBOL,
170 r'\$': ROOT_SYMBOL,
171 }
171 }
172
172
173 # A copy of the prompt_specials dictionary but with all color escapes removed,
173 # A copy of the prompt_specials dictionary but with all color escapes removed,
174 # so we can correctly compute the prompt length for the auto_rewrite method.
174 # so we can correctly compute the prompt length for the auto_rewrite method.
175 prompt_specials_nocolor = prompt_specials_color.copy()
175 prompt_specials_nocolor = prompt_specials_color.copy()
176 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
176 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
177 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
177 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
178
178
179 # Add in all the InputTermColors color escapes as valid prompt characters.
179 # Add in all the InputTermColors color escapes as valid prompt characters.
180 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
180 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
181 # with a color name which may begin with a letter used by any other of the
181 # with a color name which may begin with a letter used by any other of the
182 # allowed specials. This of course means that \\C will never be allowed for
182 # allowed specials. This of course means that \\C will never be allowed for
183 # anything else.
183 # anything else.
184 input_colors = ColorANSI.InputTermColors
184 input_colors = ColorANSI.InputTermColors
185 for _color in dir(input_colors):
185 for _color in dir(input_colors):
186 if _color[0] != '_':
186 if _color[0] != '_':
187 c_name = r'\C_'+_color
187 c_name = r'\C_'+_color
188 prompt_specials_color[c_name] = getattr(input_colors,_color)
188 prompt_specials_color[c_name] = getattr(input_colors,_color)
189 prompt_specials_nocolor[c_name] = ''
189 prompt_specials_nocolor[c_name] = ''
190
190
191 # we default to no color for safety. Note that prompt_specials is a global
191 # we default to no color for safety. Note that prompt_specials is a global
192 # variable used by all prompt objects.
192 # variable used by all prompt objects.
193 prompt_specials = prompt_specials_nocolor
193 prompt_specials = prompt_specials_nocolor
194
194
195 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
196 def str_safe(arg):
196 def str_safe(arg):
197 """Convert to a string, without ever raising an exception.
197 """Convert to a string, without ever raising an exception.
198
198
199 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
199 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
200 error message."""
200 error message."""
201
201
202 try:
202 try:
203 out = str(arg)
203 out = str(arg)
204 except UnicodeError:
204 except UnicodeError:
205 try:
205 try:
206 out = arg.encode('utf_8','replace')
206 out = arg.encode('utf_8','replace')
207 except Exception,msg:
207 except Exception,msg:
208 # let's keep this little duplication here, so that the most common
208 # let's keep this little duplication here, so that the most common
209 # case doesn't suffer from a double try wrapping.
209 # case doesn't suffer from a double try wrapping.
210 out = '<ERROR: %s>' % msg
210 out = '<ERROR: %s>' % msg
211 except Exception,msg:
211 except Exception,msg:
212 out = '<ERROR: %s>' % msg
212 out = '<ERROR: %s>' % msg
213 return out
213 return out
214
214
215 class BasePrompt(object):
215 class BasePrompt(object):
216 """Interactive prompt similar to Mathematica's."""
216 """Interactive prompt similar to Mathematica's."""
217
217
218 def _get_p_template(self):
218 def _get_p_template(self):
219 return self._p_template
219 return self._p_template
220
220
221 def _set_p_template(self,val):
221 def _set_p_template(self,val):
222 self._p_template = val
222 self._p_template = val
223 self.set_p_str()
223 self.set_p_str()
224
224
225 p_template = property(_get_p_template,_set_p_template,
225 p_template = property(_get_p_template,_set_p_template,
226 doc='Template for prompt string creation')
226 doc='Template for prompt string creation')
227
227
228 def __init__(self,cache,sep,prompt,pad_left=False):
228 def __init__(self,cache,sep,prompt,pad_left=False):
229
229
230 # Hack: we access information about the primary prompt through the
230 # Hack: we access information about the primary prompt through the
231 # cache argument. We need this, because we want the secondary prompt
231 # cache argument. We need this, because we want the secondary prompt
232 # to be aligned with the primary one. Color table info is also shared
232 # to be aligned with the primary one. Color table info is also shared
233 # by all prompt classes through the cache. Nice OO spaghetti code!
233 # by all prompt classes through the cache. Nice OO spaghetti code!
234 self.cache = cache
234 self.cache = cache
235 self.sep = sep
235 self.sep = sep
236
236
237 # regexp to count the number of spaces at the end of a prompt
237 # regexp to count the number of spaces at the end of a prompt
238 # expression, useful for prompt auto-rewriting
238 # expression, useful for prompt auto-rewriting
239 self.rspace = re.compile(r'(\s*)$')
239 self.rspace = re.compile(r'(\s*)$')
240 # Flag to left-pad prompt strings to match the length of the primary
240 # Flag to left-pad prompt strings to match the length of the primary
241 # prompt
241 # prompt
242 self.pad_left = pad_left
242 self.pad_left = pad_left
243
243
244 # Set template to create each actual prompt (where numbers change).
244 # Set template to create each actual prompt (where numbers change).
245 # Use a property
245 # Use a property
246 self.p_template = prompt
246 self.p_template = prompt
247 self.set_p_str()
247 self.set_p_str()
248
248
249 def set_p_str(self):
249 def set_p_str(self):
250 """ Set the interpolating prompt strings.
250 """ Set the interpolating prompt strings.
251
251
252 This must be called every time the color settings change, because the
252 This must be called every time the color settings change, because the
253 prompt_specials global may have changed."""
253 prompt_specials global may have changed."""
254
254
255 import os,time # needed in locals for prompt string handling
255 import os,time # needed in locals for prompt string handling
256 loc = locals()
256 loc = locals()
257 try:
257 try:
258 self.p_str = ItplNS('%s%s%s' %
258 self.p_str = ItplNS('%s%s%s' %
259 ('${self.sep}${self.col_p}',
259 ('${self.sep}${self.col_p}',
260 multiple_replace(prompt_specials, self.p_template),
260 multiple_replace(prompt_specials, self.p_template),
261 '${self.col_norm}'),self.cache.user_ns,loc)
261 '${self.col_norm}'),self.cache.user_ns,loc)
262
262
263 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
263 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
264 self.p_template),
264 self.p_template),
265 self.cache.user_ns,loc)
265 self.cache.user_ns,loc)
266 except:
266 except:
267 print "Illegal prompt template (check $ usage!):",self.p_template
267 print "Illegal prompt template (check $ usage!):",self.p_template
268 self.p_str = self.p_template
268 self.p_str = self.p_template
269 self.p_str_nocolor = self.p_template
269 self.p_str_nocolor = self.p_template
270
270
271 def write(self,msg): # dbg
271 def write(self,msg): # dbg
272 sys.stdout.write(msg)
272 sys.stdout.write(msg)
273 return ''
273 return ''
274
274
275 def __str__(self):
275 def __str__(self):
276 """Return a string form of the prompt.
276 """Return a string form of the prompt.
277
277
278 This for is useful for continuation and output prompts, since it is
278 This for is useful for continuation and output prompts, since it is
279 left-padded to match lengths with the primary one (if the
279 left-padded to match lengths with the primary one (if the
280 self.pad_left attribute is set)."""
280 self.pad_left attribute is set)."""
281
281
282 out_str = str_safe(self.p_str)
282 out_str = str_safe(self.p_str)
283 if self.pad_left:
283 if self.pad_left:
284 # We must find the amount of padding required to match lengths,
284 # We must find the amount of padding required to match lengths,
285 # taking the color escapes (which are invisible on-screen) into
285 # taking the color escapes (which are invisible on-screen) into
286 # account.
286 # account.
287 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
287 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
288 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
288 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
289 return format % out_str
289 return format % out_str
290 else:
290 else:
291 return out_str
291 return out_str
292
292
293 # these path filters are put in as methods so that we can control the
293 # these path filters are put in as methods so that we can control the
294 # namespace where the prompt strings get evaluated
294 # namespace where the prompt strings get evaluated
295 def cwd_filt(self,depth):
295 def cwd_filt(self,depth):
296 """Return the last depth elements of the current working directory.
296 """Return the last depth elements of the current working directory.
297
297
298 $HOME is always replaced with '~'.
298 $HOME is always replaced with '~'.
299 If depth==0, the full path is returned."""
299 If depth==0, the full path is returned."""
300
300
301 cwd = os.getcwd().replace(HOME,"~")
301 cwd = os.getcwd().replace(HOME,"~")
302 out = os.sep.join(cwd.split(os.sep)[-depth:])
302 out = os.sep.join(cwd.split(os.sep)[-depth:])
303 if out:
303 if out:
304 return out
304 return out
305 else:
305 else:
306 return os.sep
306 return os.sep
307
307
308 def cwd_filt2(self,depth):
308 def cwd_filt2(self,depth):
309 """Return the last depth elements of the current working directory.
309 """Return the last depth elements of the current working directory.
310
310
311 $HOME is always replaced with '~'.
311 $HOME is always replaced with '~'.
312 If depth==0, the full path is returned."""
312 If depth==0, the full path is returned."""
313
313
314 full_cwd = os.getcwd()
314 full_cwd = os.getcwd()
315 cwd = full_cwd.replace(HOME,"~").split(os.sep)
315 cwd = full_cwd.replace(HOME,"~").split(os.sep)
316 if '~' in cwd and len(cwd) == depth+1:
316 if '~' in cwd and len(cwd) == depth+1:
317 depth += 1
317 depth += 1
318 drivepart = ''
318 drivepart = ''
319 if sys.platform == 'win32' and len(cwd) > depth:
319 if sys.platform == 'win32' and len(cwd) > depth:
320 drivepart = os.path.splitdrive(full_cwd)[0]
320 drivepart = os.path.splitdrive(full_cwd)[0]
321 out = drivepart + '/'.join(cwd[-depth:])
321 out = drivepart + '/'.join(cwd[-depth:])
322
322
323 if out:
323 if out:
324 return out
324 return out
325 else:
325 else:
326 return os.sep
326 return os.sep
327
327
328 def __nonzero__(self):
328 def __nonzero__(self):
329 """Implement boolean behavior.
329 """Implement boolean behavior.
330
330
331 Checks whether the p_str attribute is non-empty"""
331 Checks whether the p_str attribute is non-empty"""
332
332
333 return bool(self.p_template)
333 return bool(self.p_template)
334
334
335 class Prompt1(BasePrompt):
335 class Prompt1(BasePrompt):
336 """Input interactive prompt similar to Mathematica's."""
336 """Input interactive prompt similar to Mathematica's."""
337
337
338 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
338 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
339 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
339 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
340
340
341 def set_colors(self):
341 def set_colors(self):
342 self.set_p_str()
342 self.set_p_str()
343 Colors = self.cache.color_table.active_colors # shorthand
343 Colors = self.cache.color_table.active_colors # shorthand
344 self.col_p = Colors.in_prompt
344 self.col_p = Colors.in_prompt
345 self.col_num = Colors.in_number
345 self.col_num = Colors.in_number
346 self.col_norm = Colors.in_normal
346 self.col_norm = Colors.in_normal
347 # We need a non-input version of these escapes for the '--->'
347 # We need a non-input version of these escapes for the '--->'
348 # auto-call prompts used in the auto_rewrite() method.
348 # auto-call prompts used in the auto_rewrite() method.
349 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
349 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
350 self.col_norm_ni = Colors.normal
350 self.col_norm_ni = Colors.normal
351
351
352 def __str__(self):
352 def __str__(self):
353 self.cache.prompt_count += 1
353 self.cache.prompt_count += 1
354 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
354 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
355 return str_safe(self.p_str)
355 return str_safe(self.p_str)
356
356
357 def auto_rewrite(self):
357 def auto_rewrite(self):
358 """Print a string of the form '--->' which lines up with the previous
358 """Print a string of the form '--->' which lines up with the previous
359 input string. Useful for systems which re-write the user input when
359 input string. Useful for systems which re-write the user input when
360 handling automatically special syntaxes."""
360 handling automatically special syntaxes."""
361
361
362 curr = str(self.cache.last_prompt)
362 curr = str(self.cache.last_prompt)
363 nrspaces = len(self.rspace.search(curr).group())
363 nrspaces = len(self.rspace.search(curr).group())
364 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
364 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
365 ' '*nrspaces,self.col_norm_ni)
365 ' '*nrspaces,self.col_norm_ni)
366
366
367 class PromptOut(BasePrompt):
367 class PromptOut(BasePrompt):
368 """Output interactive prompt similar to Mathematica's."""
368 """Output interactive prompt similar to Mathematica's."""
369
369
370 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
370 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
371 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
371 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
372 if not self.p_template:
372 if not self.p_template:
373 self.__str__ = lambda: ''
373 self.__str__ = lambda: ''
374
374
375 def set_colors(self):
375 def set_colors(self):
376 self.set_p_str()
376 self.set_p_str()
377 Colors = self.cache.color_table.active_colors # shorthand
377 Colors = self.cache.color_table.active_colors # shorthand
378 self.col_p = Colors.out_prompt
378 self.col_p = Colors.out_prompt
379 self.col_num = Colors.out_number
379 self.col_num = Colors.out_number
380 self.col_norm = Colors.normal
380 self.col_norm = Colors.normal
381
381
382 class Prompt2(BasePrompt):
382 class Prompt2(BasePrompt):
383 """Interactive continuation prompt."""
383 """Interactive continuation prompt."""
384
384
385 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
385 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
386 self.cache = cache
386 self.cache = cache
387 self.p_template = prompt
387 self.p_template = prompt
388 self.pad_left = pad_left
388 self.pad_left = pad_left
389 self.set_p_str()
389 self.set_p_str()
390
390
391 def set_p_str(self):
391 def set_p_str(self):
392 import os,time # needed in locals for prompt string handling
392 import os,time # needed in locals for prompt string handling
393 loc = locals()
393 loc = locals()
394 self.p_str = ItplNS('%s%s%s' %
394 self.p_str = ItplNS('%s%s%s' %
395 ('${self.col_p2}',
395 ('${self.col_p2}',
396 multiple_replace(prompt_specials, self.p_template),
396 multiple_replace(prompt_specials, self.p_template),
397 '$self.col_norm'),
397 '$self.col_norm'),
398 self.cache.user_ns,loc)
398 self.cache.user_ns,loc)
399 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
399 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
400 self.p_template),
400 self.p_template),
401 self.cache.user_ns,loc)
401 self.cache.user_ns,loc)
402
402
403 def set_colors(self):
403 def set_colors(self):
404 self.set_p_str()
404 self.set_p_str()
405 Colors = self.cache.color_table.active_colors
405 Colors = self.cache.color_table.active_colors
406 self.col_p2 = Colors.in_prompt2
406 self.col_p2 = Colors.in_prompt2
407 self.col_norm = Colors.in_normal
407 self.col_norm = Colors.in_normal
408 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
408 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
409 # updated their prompt_in2 definitions. Remove eventually.
409 # updated their prompt_in2 definitions. Remove eventually.
410 self.col_p = Colors.out_prompt
410 self.col_p = Colors.out_prompt
411 self.col_num = Colors.out_number
411 self.col_num = Colors.out_number
412
412
413
413
414 #-----------------------------------------------------------------------------
414 #-----------------------------------------------------------------------------
415 class CachedOutput:
415 class CachedOutput:
416 """Class for printing output from calculations while keeping a cache of
416 """Class for printing output from calculations while keeping a cache of
417 reults. It dynamically creates global variables prefixed with _ which
417 reults. It dynamically creates global variables prefixed with _ which
418 contain these results.
418 contain these results.
419
419
420 Meant to be used as a sys.displayhook replacement, providing numbered
420 Meant to be used as a sys.displayhook replacement, providing numbered
421 prompts and cache services.
421 prompts and cache services.
422
422
423 Initialize with initial and final values for cache counter (this defines
423 Initialize with initial and final values for cache counter (this defines
424 the maximum size of the cache."""
424 the maximum size of the cache."""
425
425
426 def __init__(self,shell,cache_size,Pprint,
426 def __init__(self,shell,cache_size,Pprint,
427 colors='NoColor',input_sep='\n',
427 colors='NoColor',input_sep='\n',
428 output_sep='\n',output_sep2='',
428 output_sep='\n',output_sep2='',
429 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
429 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
430
430
431 cache_size_min = 3
431 cache_size_min = 3
432 if cache_size <= 0:
432 if cache_size <= 0:
433 self.do_full_cache = 0
433 self.do_full_cache = 0
434 cache_size = 0
434 cache_size = 0
435 elif cache_size < cache_size_min:
435 elif cache_size < cache_size_min:
436 self.do_full_cache = 0
436 self.do_full_cache = 0
437 cache_size = 0
437 cache_size = 0
438 warn('caching was disabled (min value for cache size is %s).' %
438 warn('caching was disabled (min value for cache size is %s).' %
439 cache_size_min,level=3)
439 cache_size_min,level=3)
440 else:
440 else:
441 self.do_full_cache = 1
441 self.do_full_cache = 1
442
442
443 self.cache_size = cache_size
443 self.cache_size = cache_size
444 self.input_sep = input_sep
444 self.input_sep = input_sep
445
445
446 # we need a reference to the user-level namespace
446 # we need a reference to the user-level namespace
447 self.shell = shell
447 self.shell = shell
448 self.user_ns = shell.user_ns
448 self.user_ns = shell.user_ns
449 # and to the user's input
449 # and to the user's input
450 self.input_hist = shell.input_hist
450 self.input_hist = shell.input_hist
451 # and to the user's logger, for logging output
451 # and to the user's logger, for logging output
452 self.logger = shell.logger
452 self.logger = shell.logger
453
453
454 # Set input prompt strings and colors
454 # Set input prompt strings and colors
455 if cache_size == 0:
455 if cache_size == 0:
456 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
456 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
457 or ps1.find(r'\N') > -1:
457 or ps1.find(r'\N') > -1:
458 ps1 = '>>> '
458 ps1 = '>>> '
459 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
459 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
460 or ps2.find(r'\N') > -1:
460 or ps2.find(r'\N') > -1:
461 ps2 = '... '
461 ps2 = '... '
462 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
462 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
463 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
463 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
464 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
464 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
465
465
466 self.color_table = PromptColors
466 self.color_table = PromptColors
467 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
467 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
468 pad_left=pad_left)
468 pad_left=pad_left)
469 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
469 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
470 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
470 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
471 pad_left=pad_left)
471 pad_left=pad_left)
472 self.set_colors(colors)
472 self.set_colors(colors)
473
473
474 # other more normal stuff
474 # other more normal stuff
475 # b/c each call to the In[] prompt raises it by 1, even the first.
475 # b/c each call to the In[] prompt raises it by 1, even the first.
476 self.prompt_count = 0
476 self.prompt_count = 0
477 # Store the last prompt string each time, we need it for aligning
477 # Store the last prompt string each time, we need it for aligning
478 # continuation and auto-rewrite prompts
478 # continuation and auto-rewrite prompts
479 self.last_prompt = ''
479 self.last_prompt = ''
480 self.Pprint = Pprint
480 self.Pprint = Pprint
481 self.output_sep = output_sep
481 self.output_sep = output_sep
482 self.output_sep2 = output_sep2
482 self.output_sep2 = output_sep2
483 self._,self.__,self.___ = '','',''
483 self._,self.__,self.___ = '','',''
484 self.pprint_types = map(type,[(),[],{}])
484 self.pprint_types = map(type,[(),[],{}])
485
485
486 # these are deliberately global:
486 # these are deliberately global:
487 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
487 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
488 self.user_ns.update(to_user_ns)
488 self.user_ns.update(to_user_ns)
489
489
490 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
490 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
491 if p_str is None:
491 if p_str is None:
492 if self.do_full_cache:
492 if self.do_full_cache:
493 return cache_def
493 return cache_def
494 else:
494 else:
495 return no_cache_def
495 return no_cache_def
496 else:
496 else:
497 return p_str
497 return p_str
498
498
499 def set_colors(self,colors):
499 def set_colors(self,colors):
500 """Set the active color scheme and configure colors for the three
500 """Set the active color scheme and configure colors for the three
501 prompt subsystems."""
501 prompt subsystems."""
502
502
503 # FIXME: the prompt_specials global should be gobbled inside this
503 # FIXME: the prompt_specials global should be gobbled inside this
504 # class instead. Do it when cleaning up the whole 3-prompt system.
504 # class instead. Do it when cleaning up the whole 3-prompt system.
505 global prompt_specials
505 global prompt_specials
506 if colors.lower()=='nocolor':
506 if colors.lower()=='nocolor':
507 prompt_specials = prompt_specials_nocolor
507 prompt_specials = prompt_specials_nocolor
508 else:
508 else:
509 prompt_specials = prompt_specials_color
509 prompt_specials = prompt_specials_color
510
510
511 self.color_table.set_active_scheme(colors)
511 self.color_table.set_active_scheme(colors)
512 self.prompt1.set_colors()
512 self.prompt1.set_colors()
513 self.prompt2.set_colors()
513 self.prompt2.set_colors()
514 self.prompt_out.set_colors()
514 self.prompt_out.set_colors()
515
515
516 def __call__(self,arg=None):
516 def __call__(self,arg=None):
517 """Printing with history cache management.
517 """Printing with history cache management.
518
518
519 This is invoked everytime the interpreter needs to print, and is
519 This is invoked everytime the interpreter needs to print, and is
520 activated by setting the variable sys.displayhook to it."""
520 activated by setting the variable sys.displayhook to it."""
521
521
522 # If something injected a '_' variable in __builtin__, delete
522 # If something injected a '_' variable in __builtin__, delete
523 # ipython's automatic one so we don't clobber that. gettext() in
523 # ipython's automatic one so we don't clobber that. gettext() in
524 # particular uses _, so we need to stay away from it.
524 # particular uses _, so we need to stay away from it.
525 if '_' in __builtin__.__dict__:
525 if '_' in __builtin__.__dict__:
526 try:
526 try:
527 del self.user_ns['_']
527 del self.user_ns['_']
528 except KeyError:
528 except KeyError:
529 pass
529 pass
530 if arg is not None:
530 if arg is not None:
531 cout_write = Term.cout.write # fast lookup
531 cout_write = Term.cout.write # fast lookup
532 # first handle the cache and counters
532 # first handle the cache and counters
533
533
534 # do not print output if input ends in ';'
534 # do not print output if input ends in ';'
535 try:
535 try:
536 if self.input_hist[self.prompt_count].endswith(';\n'):
536 if self.input_hist[self.prompt_count].endswith(';\n'):
537 return
537 return
538 except IndexError:
538 except IndexError:
539 # some uses of ipshellembed may fail here
539 # some uses of ipshellembed may fail here
540 pass
540 pass
541 # don't use print, puts an extra space
541 # don't use print, puts an extra space
542 cout_write(self.output_sep)
542 cout_write(self.output_sep)
543 outprompt = self.shell.hooks.generate_output_prompt()
543 outprompt = self.shell.hooks.generate_output_prompt()
544 if self.do_full_cache:
544 if self.do_full_cache:
545 cout_write(outprompt)
545 cout_write(outprompt)
546
546
547 # and now call a possibly user-defined print mechanism
547 # and now call a possibly user-defined print mechanism
548 manipulated_val = self.display(arg)
548 manipulated_val = self.display(arg)
549
549
550 # user display hooks can change the variable to be stored in
550 # user display hooks can change the variable to be stored in
551 # output history
551 # output history
552
552
553 if manipulated_val is not None:
553 if manipulated_val is not None:
554 arg = manipulated_val
554 arg = manipulated_val
555
555
556 # avoid recursive reference when displaying _oh/Out
556 # avoid recursive reference when displaying _oh/Out
557 if arg is not self.user_ns['_oh']:
557 if arg is not self.user_ns['_oh']:
558 self.update(arg)
558 self.update(arg)
559
559
560 if self.logger.log_output:
560 if self.logger.log_output:
561 self.logger.log_write(repr(arg),'output')
561 self.logger.log_write(repr(arg),'output')
562 cout_write(self.output_sep2)
562 cout_write(self.output_sep2)
563 Term.cout.flush()
563 Term.cout.flush()
564
564
565 def _display(self,arg):
565 def _display(self,arg):
566 """Default printer method, uses pprint.
566 """Default printer method, uses pprint.
567
567
568 Do ip.set_hook("result_display", my_displayhook) for custom result
568 Do ip.set_hook("result_display", my_displayhook) for custom result
569 display, e.g. when your own objects need special formatting.
569 display, e.g. when your own objects need special formatting.
570 """
570 """
571 try:
571 try:
572 return IPython.generics.result_display(arg)
572 return IPython.generics.result_display(arg)
573 except TryNext:
573 except TryNext:
574 return self.shell.hooks.result_display(arg)
574 return self.shell.hooks.result_display(arg)
575
575
576 # Assign the default display method:
576 # Assign the default display method:
577 display = _display
577 display = _display
578
578
579 def update(self,arg):
579 def update(self,arg):
580 #print '***cache_count', self.cache_count # dbg
580 #print '***cache_count', self.cache_count # dbg
581 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
581 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
582 warn('Output cache limit (currently '+
582 warn('Output cache limit (currently '+
583 `self.cache_size`+' entries) hit.\n'
583 `self.cache_size`+' entries) hit.\n'
584 'Flushing cache and resetting history counter...\n'
584 'Flushing cache and resetting history counter...\n'
585 'The only history variables available will be _,__,___ and _1\n'
585 'The only history variables available will be _,__,___ and _1\n'
586 'with the current result.')
586 'with the current result.')
587
587
588 self.flush()
588 self.flush()
589 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
589 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
590 # we cause buggy behavior for things like gettext).
590 # we cause buggy behavior for things like gettext).
591 if '_' not in __builtin__.__dict__:
591 if '_' not in __builtin__.__dict__:
592 self.___ = self.__
592 self.___ = self.__
593 self.__ = self._
593 self.__ = self._
594 self._ = arg
594 self._ = arg
595 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
595 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
596
596
597 # hackish access to top-level namespace to create _1,_2... dynamically
597 # hackish access to top-level namespace to create _1,_2... dynamically
598 to_main = {}
598 to_main = {}
599 if self.do_full_cache:
599 if self.do_full_cache:
600 new_result = '_'+`self.prompt_count`
600 new_result = '_'+`self.prompt_count`
601 to_main[new_result] = arg
601 to_main[new_result] = arg
602 self.user_ns.update(to_main)
602 self.user_ns.update(to_main)
603 self.user_ns['_oh'][self.prompt_count] = arg
603 self.user_ns['_oh'][self.prompt_count] = arg
604
604
605 def flush(self):
605 def flush(self):
606 if not self.do_full_cache:
606 if not self.do_full_cache:
607 raise ValueError,"You shouldn't have reached the cache flush "\
607 raise ValueError,"You shouldn't have reached the cache flush "\
608 "if full caching is not enabled!"
608 "if full caching is not enabled!"
609 # delete auto-generated vars from global namespace
609 # delete auto-generated vars from global namespace
610
610
611 for n in range(1,self.prompt_count + 1):
611 for n in range(1,self.prompt_count + 1):
612 key = '_'+`n`
612 key = '_'+`n`
613 try:
613 try:
614 del self.user_ns[key]
614 del self.user_ns[key]
615 except: pass
615 except: pass
616 self.user_ns['_oh'].clear()
616 self.user_ns['_oh'].clear()
617
617
618 if '_' not in __builtin__.__dict__:
618 if '_' not in __builtin__.__dict__:
619 self.user_ns.update({'_':None,'__':None, '___':None})
619 self.user_ns.update({'_':None,'__':None, '___':None})
620 import gc
620 import gc
621 gc.collect() # xxx needed?
621 gc.collect() # xxx needed?
622
622
@@ -1,121 +1,121 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Release data for the IPython project."""
2 """Release data for the IPython project."""
3
3
4 #*****************************************************************************
4 #*****************************************************************************
5 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
5 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
6 #
6 #
7 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
7 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
8 # <n8gray@caltech.edu>
8 # <n8gray@caltech.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 # Name of the package for release purposes. This is the name which labels
14 # Name of the package for release purposes. This is the name which labels
15 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
15 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
16 name = 'ipython'
16 name = 'ipython'
17
17
18 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
18 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
19 # the new substring. We have to avoid using either dashes or underscores,
19 # the new substring. We have to avoid using either dashes or underscores,
20 # because bdist_rpm does not accept dashes (an RPM) convention, and
20 # because bdist_rpm does not accept dashes (an RPM) convention, and
21 # bdist_deb does not accept underscores (a Debian convention).
21 # bdist_deb does not accept underscores (a Debian convention).
22
22
23 development = False # change this to False to do a release
23 development = True # change this to False to do a release
24 version_base = '0.9.1'
24 version_base = '0.10'
25 branch = 'ipython'
25 branch = 'ipython'
26 revision = '1143'
26 revision = '1163'
27
27
28 if development:
28 if development:
29 if branch == 'ipython':
29 if branch == 'ipython':
30 version = '%s.bzr.r%s' % (version_base, revision)
30 version = '%s.bzr.r%s' % (version_base, revision)
31 else:
31 else:
32 version = '%s.bzr.r%s.%s' % (version_base, revision, branch)
32 version = '%s.bzr.r%s.%s' % (version_base, revision, branch)
33 else:
33 else:
34 version = version_base
34 version = version_base
35
35
36
36
37 description = "An interactive computing environment for Python"
37 description = "An interactive computing environment for Python"
38
38
39 long_description = \
39 long_description = \
40 """
40 """
41 The goal of IPython is to create a comprehensive environment for
41 The goal of IPython is to create a comprehensive environment for
42 interactive and exploratory computing. To support this goal, IPython
42 interactive and exploratory computing. To support this goal, IPython
43 has two main components:
43 has two main components:
44
44
45 * An enhanced interactive Python shell.
45 * An enhanced interactive Python shell.
46
46
47 * An architecture for interactive parallel computing.
47 * An architecture for interactive parallel computing.
48
48
49 The enhanced interactive Python shell has the following main features:
49 The enhanced interactive Python shell has the following main features:
50
50
51 * Comprehensive object introspection.
51 * Comprehensive object introspection.
52
52
53 * Input history, persistent across sessions.
53 * Input history, persistent across sessions.
54
54
55 * Caching of output results during a session with automatically generated
55 * Caching of output results during a session with automatically generated
56 references.
56 references.
57
57
58 * Readline based name completion.
58 * Readline based name completion.
59
59
60 * Extensible system of 'magic' commands for controlling the environment and
60 * Extensible system of 'magic' commands for controlling the environment and
61 performing many tasks related either to IPython or the operating system.
61 performing many tasks related either to IPython or the operating system.
62
62
63 * Configuration system with easy switching between different setups (simpler
63 * Configuration system with easy switching between different setups (simpler
64 than changing $PYTHONSTARTUP environment variables every time).
64 than changing $PYTHONSTARTUP environment variables every time).
65
65
66 * Session logging and reloading.
66 * Session logging and reloading.
67
67
68 * Extensible syntax processing for special purpose situations.
68 * Extensible syntax processing for special purpose situations.
69
69
70 * Access to the system shell with user-extensible alias system.
70 * Access to the system shell with user-extensible alias system.
71
71
72 * Easily embeddable in other Python programs and wxPython GUIs.
72 * Easily embeddable in other Python programs and wxPython GUIs.
73
73
74 * Integrated access to the pdb debugger and the Python profiler.
74 * Integrated access to the pdb debugger and the Python profiler.
75
75
76 The parallel computing architecture has the following main features:
76 The parallel computing architecture has the following main features:
77
77
78 * Quickly parallelize Python code from an interactive Python/IPython session.
78 * Quickly parallelize Python code from an interactive Python/IPython session.
79
79
80 * A flexible and dynamic process model that be deployed on anything from
80 * A flexible and dynamic process model that be deployed on anything from
81 multicore workstations to supercomputers.
81 multicore workstations to supercomputers.
82
82
83 * An architecture that supports many different styles of parallelism, from
83 * An architecture that supports many different styles of parallelism, from
84 message passing to task farming.
84 message passing to task farming.
85
85
86 * Both blocking and fully asynchronous interfaces.
86 * Both blocking and fully asynchronous interfaces.
87
87
88 * High level APIs that enable many things to be parallelized in a few lines
88 * High level APIs that enable many things to be parallelized in a few lines
89 of code.
89 of code.
90
90
91 * Share live parallel jobs with other users securely.
91 * Share live parallel jobs with other users securely.
92
92
93 * Dynamically load balanced task farming system.
93 * Dynamically load balanced task farming system.
94
94
95 * Robust error handling in parallel code.
95 * Robust error handling in parallel code.
96
96
97 The latest development version is always available from IPython's `Launchpad
97 The latest development version is always available from IPython's `Launchpad
98 site <http://launchpad.net/ipython>`_.
98 site <http://launchpad.net/ipython>`_.
99 """
99 """
100
100
101 license = 'BSD'
101 license = 'BSD'
102
102
103 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
103 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
104 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
104 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
105 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
105 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
106 'Ville' : ('Ville Vainio','vivainio@gmail.com'),
106 'Ville' : ('Ville Vainio','vivainio@gmail.com'),
107 'Brian' : ('Brian E Granger', 'ellisonbg@gmail.com'),
107 'Brian' : ('Brian E Granger', 'ellisonbg@gmail.com'),
108 'Min' : ('Min Ragan-Kelley', 'benjaminrk@gmail.com')
108 'Min' : ('Min Ragan-Kelley', 'benjaminrk@gmail.com')
109 }
109 }
110
110
111 author = 'The IPython Development Team'
111 author = 'The IPython Development Team'
112
112
113 author_email = 'ipython-dev@scipy.org'
113 author_email = 'ipython-dev@scipy.org'
114
114
115 url = 'http://ipython.scipy.org'
115 url = 'http://ipython.scipy.org'
116
116
117 download_url = 'http://ipython.scipy.org/dist'
117 download_url = 'http://ipython.scipy.org/dist'
118
118
119 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
119 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
120
120
121 keywords = ['Interactive','Interpreter','Shell','Parallel','Distributed']
121 keywords = ['Interactive','Interpreter','Shell','Parallel','Distributed']
@@ -1,2790 +1,2865 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 Requires Python 2.4 or newer.
5 Requires Python 2.4 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8 """
8 """
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
11 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #
16 #
17 # Note: this code originally subclassed code.InteractiveConsole from the
17 # Note: this code originally subclassed code.InteractiveConsole from the
18 # Python standard library. Over time, all of that class has been copied
18 # Python standard library. Over time, all of that class has been copied
19 # verbatim here for modifications which could not be accomplished by
19 # verbatim here for modifications which could not be accomplished by
20 # subclassing. At this point, there are no dependencies at all on the code
20 # subclassing. At this point, there are no dependencies at all on the code
21 # module anymore (it is not even imported). The Python License (sec. 2)
21 # module anymore (it is not even imported). The Python License (sec. 2)
22 # allows for this, but it's always nice to acknowledge credit where credit is
22 # allows for this, but it's always nice to acknowledge credit where credit is
23 # due.
23 # due.
24 #*****************************************************************************
24 #*****************************************************************************
25
25
26 #****************************************************************************
26 #****************************************************************************
27 # Modules and globals
27 # Modules and globals
28
28
29 # Python standard modules
29 # Python standard modules
30 import __main__
30 import __main__
31 import __builtin__
31 import __builtin__
32 import StringIO
32 import StringIO
33 import bdb
33 import bdb
34 import cPickle as pickle
34 import cPickle as pickle
35 import codeop
35 import codeop
36 import exceptions
36 import exceptions
37 import glob
37 import glob
38 import inspect
38 import inspect
39 import keyword
39 import keyword
40 import new
40 import new
41 import os
41 import os
42 import pydoc
42 import pydoc
43 import re
43 import re
44 import shutil
44 import shutil
45 import string
45 import string
46 import sys
46 import sys
47 import tempfile
47 import tempfile
48 import traceback
48 import traceback
49 import types
49 import types
50 from pprint import pprint, pformat
50 from pprint import pprint, pformat
51
51
52 # IPython's own modules
52 # IPython's own modules
53 #import IPython
53 #import IPython
54 from IPython import Debugger,OInspect,PyColorize,ultraTB
54 from IPython import Debugger,OInspect,PyColorize,ultraTB
55 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
55 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
56 from IPython.Extensions import pickleshare
56 from IPython.Extensions import pickleshare
57 from IPython.FakeModule import FakeModule
57 from IPython.FakeModule import FakeModule, init_fakemod_dict
58 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
58 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
59 from IPython.Logger import Logger
59 from IPython.Logger import Logger
60 from IPython.Magic import Magic
60 from IPython.Magic import Magic
61 from IPython.Prompts import CachedOutput
61 from IPython.Prompts import CachedOutput
62 from IPython.ipstruct import Struct
62 from IPython.ipstruct import Struct
63 from IPython.background_jobs import BackgroundJobManager
63 from IPython.background_jobs import BackgroundJobManager
64 from IPython.usage import cmd_line_usage,interactive_usage
64 from IPython.usage import cmd_line_usage,interactive_usage
65 from IPython.genutils import *
65 from IPython.genutils import *
66 from IPython.strdispatch import StrDispatch
66 from IPython.strdispatch import StrDispatch
67 import IPython.ipapi
67 import IPython.ipapi
68 import IPython.history
68 import IPython.history
69 import IPython.prefilter as prefilter
69 import IPython.prefilter as prefilter
70 import IPython.shadowns
70 import IPython.shadowns
71 # Globals
71 # Globals
72
72
73 # store the builtin raw_input globally, and use this always, in case user code
73 # store the builtin raw_input globally, and use this always, in case user code
74 # overwrites it (like wx.py.PyShell does)
74 # overwrites it (like wx.py.PyShell does)
75 raw_input_original = raw_input
75 raw_input_original = raw_input
76
76
77 # compiled regexps for autoindent management
77 # compiled regexps for autoindent management
78 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
78 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
79
79
80
80
81 #****************************************************************************
81 #****************************************************************************
82 # Some utility function definitions
82 # Some utility function definitions
83
83
84 ini_spaces_re = re.compile(r'^(\s+)')
84 ini_spaces_re = re.compile(r'^(\s+)')
85
85
86 def num_ini_spaces(strng):
86 def num_ini_spaces(strng):
87 """Return the number of initial spaces in a string"""
87 """Return the number of initial spaces in a string"""
88
88
89 ini_spaces = ini_spaces_re.match(strng)
89 ini_spaces = ini_spaces_re.match(strng)
90 if ini_spaces:
90 if ini_spaces:
91 return ini_spaces.end()
91 return ini_spaces.end()
92 else:
92 else:
93 return 0
93 return 0
94
94
95 def softspace(file, newvalue):
95 def softspace(file, newvalue):
96 """Copied from code.py, to remove the dependency"""
96 """Copied from code.py, to remove the dependency"""
97
97
98 oldvalue = 0
98 oldvalue = 0
99 try:
99 try:
100 oldvalue = file.softspace
100 oldvalue = file.softspace
101 except AttributeError:
101 except AttributeError:
102 pass
102 pass
103 try:
103 try:
104 file.softspace = newvalue
104 file.softspace = newvalue
105 except (AttributeError, TypeError):
105 except (AttributeError, TypeError):
106 # "attribute-less object" or "read-only attributes"
106 # "attribute-less object" or "read-only attributes"
107 pass
107 pass
108 return oldvalue
108 return oldvalue
109
109
110
110
111 def user_setup(ipythondir,rc_suffix,mode='install',interactive=True):
112 """Install or upgrade the user configuration directory.
113
114 Can be called when running for the first time or to upgrade the user's
115 .ipython/ directory.
116
117 Parameters
118 ----------
119 ipythondir : path
120 The directory to be used for installation/upgrade. In 'install' mode,
121 if this path already exists, the function exits immediately.
122
123 rc_suffix : str
124 Extension for the config files. On *nix platforms it is typically the
125 empty string, while Windows normally uses '.ini'.
126
127 mode : str, optional
128 Valid modes are 'install' and 'upgrade'.
129
130 interactive : bool, optional
131 If False, do not wait for user input on any errors. Normally after
132 printing its status information, this function waits for the user to
133 hit Return before proceeding. This is because the default use case is
134 when first installing the IPython configuration, so we want the user to
135 acknowledge the initial message, which contains some useful
136 information.
137 """
138
139 # For automatic use, deactivate all i/o
140 if interactive:
141 def wait():
142 try:
143 raw_input("Please press <RETURN> to start IPython.")
144 except EOFError:
145 print >> Term.cout
146 print '*'*70
147
148 def printf(s):
149 print s
150 else:
151 wait = lambda : None
152 printf = lambda s : None
153
154 # Install mode should be re-entrant: if the install dir already exists,
155 # bail out cleanly
156 if mode == 'install' and os.path.isdir(ipythondir):
157 return
158
159 cwd = os.getcwd() # remember where we started
160 glb = glob.glob
161
162 printf('*'*70)
163 if mode == 'install':
164 printf(
165 """Welcome to IPython. I will try to create a personal configuration directory
166 where you can customize many aspects of IPython's functionality in:\n""")
167 else:
168 printf('I am going to upgrade your configuration in:')
169
170 printf(ipythondir)
171
172 rcdirend = os.path.join('IPython','UserConfig')
173 cfg = lambda d: os.path.join(d,rcdirend)
174 try:
175 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
176 printf("Initializing from configuration: %s" % rcdir)
177 except IndexError:
178 warning = """
179 Installation error. IPython's directory was not found.
180
181 Check the following:
182
183 The ipython/IPython directory should be in a directory belonging to your
184 PYTHONPATH environment variable (that is, it should be in a directory
185 belonging to sys.path). You can copy it explicitly there or just link to it.
186
187 IPython will create a minimal default configuration for you.
188
189 """
190 warn(warning)
191 wait()
192
193 if sys.platform =='win32':
194 inif = 'ipythonrc.ini'
195 else:
196 inif = 'ipythonrc'
197 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults',
198 inif : '# intentionally left blank' }
199 os.makedirs(ipythondir, mode = 0777)
200 for f, cont in minimal_setup.items():
201 # In 2.5, this can be more cleanly done using 'with'
202 fobj = file(ipythondir + '/' + f,'w')
203 fobj.write(cont)
204 fobj.close()
205
206 return
207
208 if mode == 'install':
209 try:
210 shutil.copytree(rcdir,ipythondir)
211 os.chdir(ipythondir)
212 rc_files = glb("ipythonrc*")
213 for rc_file in rc_files:
214 os.rename(rc_file,rc_file+rc_suffix)
215 except:
216 warning = """
217
218 There was a problem with the installation:
219 %s
220 Try to correct it or contact the developers if you think it's a bug.
221 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
222 warn(warning)
223 wait()
224 return
225
226 elif mode == 'upgrade':
227 try:
228 os.chdir(ipythondir)
229 except:
230 printf("""
231 Can not upgrade: changing to directory %s failed. Details:
232 %s
233 """ % (ipythondir,sys.exc_info()[1]) )
234 wait()
235 return
236 else:
237 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
238 for new_full_path in sources:
239 new_filename = os.path.basename(new_full_path)
240 if new_filename.startswith('ipythonrc'):
241 new_filename = new_filename + rc_suffix
242 # The config directory should only contain files, skip any
243 # directories which may be there (like CVS)
244 if os.path.isdir(new_full_path):
245 continue
246 if os.path.exists(new_filename):
247 old_file = new_filename+'.old'
248 if os.path.exists(old_file):
249 os.remove(old_file)
250 os.rename(new_filename,old_file)
251 shutil.copy(new_full_path,new_filename)
252 else:
253 raise ValueError('unrecognized mode for install: %r' % mode)
254
255 # Fix line-endings to those native to each platform in the config
256 # directory.
257 try:
258 os.chdir(ipythondir)
259 except:
260 printf("""
261 Problem: changing to directory %s failed.
262 Details:
263 %s
264
265 Some configuration files may have incorrect line endings. This should not
266 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) )
267 wait()
268 else:
269 for fname in glb('ipythonrc*'):
270 try:
271 native_line_ends(fname,backup=0)
272 except IOError:
273 pass
274
275 if mode == 'install':
276 printf("""
277 Successful installation!
278
279 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
280 IPython manual (there are both HTML and PDF versions supplied with the
281 distribution) to make sure that your system environment is properly configured
282 to take advantage of IPython's features.
283
284 Important note: the configuration system has changed! The old system is
285 still in place, but its setting may be partly overridden by the settings in
286 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
287 if some of the new settings bother you.
288
289 """)
290 else:
291 printf("""
292 Successful upgrade!
293
294 All files in your directory:
295 %(ipythondir)s
296 which would have been overwritten by the upgrade were backed up with a .old
297 extension. If you had made particular customizations in those files you may
298 want to merge them back into the new files.""" % locals() )
299 wait()
300 os.chdir(cwd)
301
111 #****************************************************************************
302 #****************************************************************************
112 # Local use exceptions
303 # Local use exceptions
113 class SpaceInInput(exceptions.Exception): pass
304 class SpaceInInput(exceptions.Exception): pass
114
305
115
306
116 #****************************************************************************
307 #****************************************************************************
117 # Local use classes
308 # Local use classes
118 class Bunch: pass
309 class Bunch: pass
119
310
120 class Undefined: pass
311 class Undefined: pass
121
312
122 class Quitter(object):
313 class Quitter(object):
123 """Simple class to handle exit, similar to Python 2.5's.
314 """Simple class to handle exit, similar to Python 2.5's.
124
315
125 It handles exiting in an ipython-safe manner, which the one in Python 2.5
316 It handles exiting in an ipython-safe manner, which the one in Python 2.5
126 doesn't do (obviously, since it doesn't know about ipython)."""
317 doesn't do (obviously, since it doesn't know about ipython)."""
127
318
128 def __init__(self,shell,name):
319 def __init__(self,shell,name):
129 self.shell = shell
320 self.shell = shell
130 self.name = name
321 self.name = name
131
322
132 def __repr__(self):
323 def __repr__(self):
133 return 'Type %s() to exit.' % self.name
324 return 'Type %s() to exit.' % self.name
134 __str__ = __repr__
325 __str__ = __repr__
135
326
136 def __call__(self):
327 def __call__(self):
137 self.shell.exit()
328 self.shell.exit()
138
329
139 class InputList(list):
330 class InputList(list):
140 """Class to store user input.
331 """Class to store user input.
141
332
142 It's basically a list, but slices return a string instead of a list, thus
333 It's basically a list, but slices return a string instead of a list, thus
143 allowing things like (assuming 'In' is an instance):
334 allowing things like (assuming 'In' is an instance):
144
335
145 exec In[4:7]
336 exec In[4:7]
146
337
147 or
338 or
148
339
149 exec In[5:9] + In[14] + In[21:25]"""
340 exec In[5:9] + In[14] + In[21:25]"""
150
341
151 def __getslice__(self,i,j):
342 def __getslice__(self,i,j):
152 return ''.join(list.__getslice__(self,i,j))
343 return ''.join(list.__getslice__(self,i,j))
153
344
154 class SyntaxTB(ultraTB.ListTB):
345 class SyntaxTB(ultraTB.ListTB):
155 """Extension which holds some state: the last exception value"""
346 """Extension which holds some state: the last exception value"""
156
347
157 def __init__(self,color_scheme = 'NoColor'):
348 def __init__(self,color_scheme = 'NoColor'):
158 ultraTB.ListTB.__init__(self,color_scheme)
349 ultraTB.ListTB.__init__(self,color_scheme)
159 self.last_syntax_error = None
350 self.last_syntax_error = None
160
351
161 def __call__(self, etype, value, elist):
352 def __call__(self, etype, value, elist):
162 self.last_syntax_error = value
353 self.last_syntax_error = value
163 ultraTB.ListTB.__call__(self,etype,value,elist)
354 ultraTB.ListTB.__call__(self,etype,value,elist)
164
355
165 def clear_err_state(self):
356 def clear_err_state(self):
166 """Return the current error state and clear it"""
357 """Return the current error state and clear it"""
167 e = self.last_syntax_error
358 e = self.last_syntax_error
168 self.last_syntax_error = None
359 self.last_syntax_error = None
169 return e
360 return e
170
361
171 #****************************************************************************
362 #****************************************************************************
172 # Main IPython class
363 # Main IPython class
173
364
174 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
365 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
175 # until a full rewrite is made. I've cleaned all cross-class uses of
366 # until a full rewrite is made. I've cleaned all cross-class uses of
176 # attributes and methods, but too much user code out there relies on the
367 # attributes and methods, but too much user code out there relies on the
177 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
368 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
178 #
369 #
179 # But at least now, all the pieces have been separated and we could, in
370 # But at least now, all the pieces have been separated and we could, in
180 # principle, stop using the mixin. This will ease the transition to the
371 # principle, stop using the mixin. This will ease the transition to the
181 # chainsaw branch.
372 # chainsaw branch.
182
373
183 # For reference, the following is the list of 'self.foo' uses in the Magic
374 # For reference, the following is the list of 'self.foo' uses in the Magic
184 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
375 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
185 # class, to prevent clashes.
376 # class, to prevent clashes.
186
377
187 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
378 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
188 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
379 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
189 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
380 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
190 # 'self.value']
381 # 'self.value']
191
382
192 class InteractiveShell(object,Magic):
383 class InteractiveShell(object,Magic):
193 """An enhanced console for Python."""
384 """An enhanced console for Python."""
194
385
195 # class attribute to indicate whether the class supports threads or not.
386 # class attribute to indicate whether the class supports threads or not.
196 # Subclasses with thread support should override this as needed.
387 # Subclasses with thread support should override this as needed.
197 isthreaded = False
388 isthreaded = False
198
389
199 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
390 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
200 user_ns=None,user_global_ns=None,banner2='',
391 user_ns=None,user_global_ns=None,banner2='',
201 custom_exceptions=((),None),embedded=False):
392 custom_exceptions=((),None),embedded=False):
202
393
203 # log system
394 # log system
204 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
395 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
205
396
206 # Job manager (for jobs run as background threads)
397 # Job manager (for jobs run as background threads)
207 self.jobs = BackgroundJobManager()
398 self.jobs = BackgroundJobManager()
208
399
209 # Store the actual shell's name
400 # Store the actual shell's name
210 self.name = name
401 self.name = name
211 self.more = False
402 self.more = False
212
403
213 # We need to know whether the instance is meant for embedding, since
404 # We need to know whether the instance is meant for embedding, since
214 # global/local namespaces need to be handled differently in that case
405 # global/local namespaces need to be handled differently in that case
215 self.embedded = embedded
406 self.embedded = embedded
216 if embedded:
407 if embedded:
217 # Control variable so users can, from within the embedded instance,
408 # Control variable so users can, from within the embedded instance,
218 # permanently deactivate it.
409 # permanently deactivate it.
219 self.embedded_active = True
410 self.embedded_active = True
220
411
221 # command compiler
412 # command compiler
222 self.compile = codeop.CommandCompiler()
413 self.compile = codeop.CommandCompiler()
223
414
224 # User input buffer
415 # User input buffer
225 self.buffer = []
416 self.buffer = []
226
417
227 # Default name given in compilation of code
418 # Default name given in compilation of code
228 self.filename = '<ipython console>'
419 self.filename = '<ipython console>'
229
420
230 # Install our own quitter instead of the builtins. For python2.3-2.4,
421 # Install our own quitter instead of the builtins. For python2.3-2.4,
231 # this brings in behavior like 2.5, and for 2.5 it's identical.
422 # this brings in behavior like 2.5, and for 2.5 it's identical.
232 __builtin__.exit = Quitter(self,'exit')
423 __builtin__.exit = Quitter(self,'exit')
233 __builtin__.quit = Quitter(self,'quit')
424 __builtin__.quit = Quitter(self,'quit')
234
425
235 # Make an empty namespace, which extension writers can rely on both
426 # Make an empty namespace, which extension writers can rely on both
236 # existing and NEVER being used by ipython itself. This gives them a
427 # existing and NEVER being used by ipython itself. This gives them a
237 # convenient location for storing additional information and state
428 # convenient location for storing additional information and state
238 # their extensions may require, without fear of collisions with other
429 # their extensions may require, without fear of collisions with other
239 # ipython names that may develop later.
430 # ipython names that may develop later.
240 self.meta = Struct()
431 self.meta = Struct()
241
432
242 # Create the namespace where the user will operate. user_ns is
433 # Create the namespace where the user will operate. user_ns is
243 # normally the only one used, and it is passed to the exec calls as
434 # normally the only one used, and it is passed to the exec calls as
244 # the locals argument. But we do carry a user_global_ns namespace
435 # the locals argument. But we do carry a user_global_ns namespace
245 # given as the exec 'globals' argument, This is useful in embedding
436 # given as the exec 'globals' argument, This is useful in embedding
246 # situations where the ipython shell opens in a context where the
437 # situations where the ipython shell opens in a context where the
247 # distinction between locals and globals is meaningful. For
438 # distinction between locals and globals is meaningful. For
248 # non-embedded contexts, it is just the same object as the user_ns dict.
439 # non-embedded contexts, it is just the same object as the user_ns dict.
249
440
250 # FIXME. For some strange reason, __builtins__ is showing up at user
441 # FIXME. For some strange reason, __builtins__ is showing up at user
251 # level as a dict instead of a module. This is a manual fix, but I
442 # level as a dict instead of a module. This is a manual fix, but I
252 # should really track down where the problem is coming from. Alex
443 # should really track down where the problem is coming from. Alex
253 # Schmolck reported this problem first.
444 # Schmolck reported this problem first.
254
445
255 # A useful post by Alex Martelli on this topic:
446 # A useful post by Alex Martelli on this topic:
256 # Re: inconsistent value from __builtins__
447 # Re: inconsistent value from __builtins__
257 # Von: Alex Martelli <aleaxit@yahoo.com>
448 # Von: Alex Martelli <aleaxit@yahoo.com>
258 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
449 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
259 # Gruppen: comp.lang.python
450 # Gruppen: comp.lang.python
260
451
261 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
452 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
262 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
453 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
263 # > <type 'dict'>
454 # > <type 'dict'>
264 # > >>> print type(__builtins__)
455 # > >>> print type(__builtins__)
265 # > <type 'module'>
456 # > <type 'module'>
266 # > Is this difference in return value intentional?
457 # > Is this difference in return value intentional?
267
458
268 # Well, it's documented that '__builtins__' can be either a dictionary
459 # Well, it's documented that '__builtins__' can be either a dictionary
269 # or a module, and it's been that way for a long time. Whether it's
460 # or a module, and it's been that way for a long time. Whether it's
270 # intentional (or sensible), I don't know. In any case, the idea is
461 # intentional (or sensible), I don't know. In any case, the idea is
271 # that if you need to access the built-in namespace directly, you
462 # that if you need to access the built-in namespace directly, you
272 # should start with "import __builtin__" (note, no 's') which will
463 # should start with "import __builtin__" (note, no 's') which will
273 # definitely give you a module. Yeah, it's somewhat confusing:-(.
464 # definitely give you a module. Yeah, it's somewhat confusing:-(.
274
465
275 # These routines return properly built dicts as needed by the rest of
466 # These routines return properly built dicts as needed by the rest of
276 # the code, and can also be used by extension writers to generate
467 # the code, and can also be used by extension writers to generate
277 # properly initialized namespaces.
468 # properly initialized namespaces.
278 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
469 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
279 user_global_ns)
470 user_global_ns)
280
471
281 # Assign namespaces
472 # Assign namespaces
282 # This is the namespace where all normal user variables live
473 # This is the namespace where all normal user variables live
283 self.user_ns = user_ns
474 self.user_ns = user_ns
284 self.user_global_ns = user_global_ns
475 self.user_global_ns = user_global_ns
285
476
286 # An auxiliary namespace that checks what parts of the user_ns were
477 # An auxiliary namespace that checks what parts of the user_ns were
287 # loaded at startup, so we can list later only variables defined in
478 # loaded at startup, so we can list later only variables defined in
288 # actual interactive use. Since it is always a subset of user_ns, it
479 # actual interactive use. Since it is always a subset of user_ns, it
289 # doesn't need to be seaparately tracked in the ns_table
480 # doesn't need to be seaparately tracked in the ns_table
290 self.user_config_ns = {}
481 self.user_config_ns = {}
291
482
292 # A namespace to keep track of internal data structures to prevent
483 # A namespace to keep track of internal data structures to prevent
293 # them from cluttering user-visible stuff. Will be updated later
484 # them from cluttering user-visible stuff. Will be updated later
294 self.internal_ns = {}
485 self.internal_ns = {}
295
486
296 # Namespace of system aliases. Each entry in the alias
487 # Namespace of system aliases. Each entry in the alias
297 # table must be a 2-tuple of the form (N,name), where N is the number
488 # table must be a 2-tuple of the form (N,name), where N is the number
298 # of positional arguments of the alias.
489 # of positional arguments of the alias.
299 self.alias_table = {}
490 self.alias_table = {}
300
491
301 # Now that FakeModule produces a real module, we've run into a nasty
492 # Now that FakeModule produces a real module, we've run into a nasty
302 # problem: after script execution (via %run), the module where the user
493 # problem: after script execution (via %run), the module where the user
303 # code ran is deleted. Now that this object is a true module (needed
494 # code ran is deleted. Now that this object is a true module (needed
304 # so docetst and other tools work correctly), the Python module
495 # so docetst and other tools work correctly), the Python module
305 # teardown mechanism runs over it, and sets to None every variable
496 # teardown mechanism runs over it, and sets to None every variable
306 # present in that module. Top-level references to objects from the
497 # present in that module. Top-level references to objects from the
307 # script survive, because the user_ns is updated with them. However,
498 # script survive, because the user_ns is updated with them. However,
308 # calling functions defined in the script that use other things from
499 # calling functions defined in the script that use other things from
309 # the script will fail, because the function's closure had references
500 # the script will fail, because the function's closure had references
310 # to the original objects, which are now all None. So we must protect
501 # to the original objects, which are now all None. So we must protect
311 # these modules from deletion by keeping a cache. To avoid keeping
502 # these modules from deletion by keeping a cache.
312 # stale modules around (we only need the one from the last run), we use
503 #
313 # a dict keyed with the full path to the script, so only the last
504 # To avoid keeping stale modules around (we only need the one from the
314 # version of the module is held in the cache. The %reset command will
505 # last run), we use a dict keyed with the full path to the script, so
315 # flush this cache. See the cache_main_mod() and clear_main_mod_cache()
506 # only the last version of the module is held in the cache. Note,
316 # methods for details on use.
507 # however, that we must cache the module *namespace contents* (their
317 self._user_main_modules = {}
508 # __dict__). Because if we try to cache the actual modules, old ones
509 # (uncached) could be destroyed while still holding references (such as
510 # those held by GUI objects that tend to be long-lived)>
511 #
512 # The %reset command will flush this cache. See the cache_main_mod()
513 # and clear_main_mod_cache() methods for details on use.
514
515 # This is the cache used for 'main' namespaces
516 self._main_ns_cache = {}
517 # And this is the single instance of FakeModule whose __dict__ we keep
518 # copying and clearing for reuse on each %run
519 self._user_main_module = FakeModule()
318
520
319 # A table holding all the namespaces IPython deals with, so that
521 # A table holding all the namespaces IPython deals with, so that
320 # introspection facilities can search easily.
522 # introspection facilities can search easily.
321 self.ns_table = {'user':user_ns,
523 self.ns_table = {'user':user_ns,
322 'user_global':user_global_ns,
524 'user_global':user_global_ns,
323 'alias':self.alias_table,
525 'alias':self.alias_table,
324 'internal':self.internal_ns,
526 'internal':self.internal_ns,
325 'builtin':__builtin__.__dict__
527 'builtin':__builtin__.__dict__
326 }
528 }
327
529
328 # Similarly, track all namespaces where references can be held and that
530 # Similarly, track all namespaces where references can be held and that
329 # we can safely clear (so it can NOT include builtin). This one can be
531 # we can safely clear (so it can NOT include builtin). This one can be
330 # a simple list.
532 # a simple list.
331 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
533 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
332 self.alias_table, self.internal_ns,
534 self.alias_table, self.internal_ns,
333 self._user_main_modules ]
535 self._main_ns_cache ]
334
536
335 # We need to insert into sys.modules something that looks like a
537 # We need to insert into sys.modules something that looks like a
336 # module but which accesses the IPython namespace, for shelve and
538 # module but which accesses the IPython namespace, for shelve and
337 # pickle to work interactively. Normally they rely on getting
539 # pickle to work interactively. Normally they rely on getting
338 # everything out of __main__, but for embedding purposes each IPython
540 # everything out of __main__, but for embedding purposes each IPython
339 # instance has its own private namespace, so we can't go shoving
541 # instance has its own private namespace, so we can't go shoving
340 # everything into __main__.
542 # everything into __main__.
341
543
342 # note, however, that we should only do this for non-embedded
544 # note, however, that we should only do this for non-embedded
343 # ipythons, which really mimic the __main__.__dict__ with their own
545 # ipythons, which really mimic the __main__.__dict__ with their own
344 # namespace. Embedded instances, on the other hand, should not do
546 # namespace. Embedded instances, on the other hand, should not do
345 # this because they need to manage the user local/global namespaces
547 # this because they need to manage the user local/global namespaces
346 # only, but they live within a 'normal' __main__ (meaning, they
548 # only, but they live within a 'normal' __main__ (meaning, they
347 # shouldn't overtake the execution environment of the script they're
549 # shouldn't overtake the execution environment of the script they're
348 # embedded in).
550 # embedded in).
349
551
350 if not embedded:
552 if not embedded:
351 try:
553 try:
352 main_name = self.user_ns['__name__']
554 main_name = self.user_ns['__name__']
353 except KeyError:
555 except KeyError:
354 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
556 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
355 else:
557 else:
356 #print "pickle hack in place" # dbg
558 #print "pickle hack in place" # dbg
357 #print 'main_name:',main_name # dbg
559 #print 'main_name:',main_name # dbg
358 sys.modules[main_name] = FakeModule(self.user_ns)
560 sys.modules[main_name] = FakeModule(self.user_ns)
359
561
360 # List of input with multi-line handling.
562 # List of input with multi-line handling.
361 self.input_hist = InputList()
563 self.input_hist = InputList()
362 # This one will hold the 'raw' input history, without any
564 # This one will hold the 'raw' input history, without any
363 # pre-processing. This will allow users to retrieve the input just as
565 # pre-processing. This will allow users to retrieve the input just as
364 # it was exactly typed in by the user, with %hist -r.
566 # it was exactly typed in by the user, with %hist -r.
365 self.input_hist_raw = InputList()
567 self.input_hist_raw = InputList()
366
568
367 # list of visited directories
569 # list of visited directories
368 try:
570 try:
369 self.dir_hist = [os.getcwd()]
571 self.dir_hist = [os.getcwd()]
370 except OSError:
572 except OSError:
371 self.dir_hist = []
573 self.dir_hist = []
372
574
373 # dict of output history
575 # dict of output history
374 self.output_hist = {}
576 self.output_hist = {}
375
577
376 # Get system encoding at startup time. Certain terminals (like Emacs
578 # Get system encoding at startup time. Certain terminals (like Emacs
377 # under Win32 have it set to None, and we need to have a known valid
579 # under Win32 have it set to None, and we need to have a known valid
378 # encoding to use in the raw_input() method
580 # encoding to use in the raw_input() method
379 try:
581 try:
380 self.stdin_encoding = sys.stdin.encoding or 'ascii'
582 self.stdin_encoding = sys.stdin.encoding or 'ascii'
381 except AttributeError:
583 except AttributeError:
382 self.stdin_encoding = 'ascii'
584 self.stdin_encoding = 'ascii'
383
585
384 # dict of things NOT to alias (keywords, builtins and some magics)
586 # dict of things NOT to alias (keywords, builtins and some magics)
385 no_alias = {}
587 no_alias = {}
386 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
588 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
387 for key in keyword.kwlist + no_alias_magics:
589 for key in keyword.kwlist + no_alias_magics:
388 no_alias[key] = 1
590 no_alias[key] = 1
389 no_alias.update(__builtin__.__dict__)
591 no_alias.update(__builtin__.__dict__)
390 self.no_alias = no_alias
592 self.no_alias = no_alias
391
593
392 # Object variable to store code object waiting execution. This is
594 # Object variable to store code object waiting execution. This is
393 # used mainly by the multithreaded shells, but it can come in handy in
595 # used mainly by the multithreaded shells, but it can come in handy in
394 # other situations. No need to use a Queue here, since it's a single
596 # other situations. No need to use a Queue here, since it's a single
395 # item which gets cleared once run.
597 # item which gets cleared once run.
396 self.code_to_run = None
598 self.code_to_run = None
397
599
398 # escapes for automatic behavior on the command line
600 # escapes for automatic behavior on the command line
399 self.ESC_SHELL = '!'
601 self.ESC_SHELL = '!'
400 self.ESC_SH_CAP = '!!'
602 self.ESC_SH_CAP = '!!'
401 self.ESC_HELP = '?'
603 self.ESC_HELP = '?'
402 self.ESC_MAGIC = '%'
604 self.ESC_MAGIC = '%'
403 self.ESC_QUOTE = ','
605 self.ESC_QUOTE = ','
404 self.ESC_QUOTE2 = ';'
606 self.ESC_QUOTE2 = ';'
405 self.ESC_PAREN = '/'
607 self.ESC_PAREN = '/'
406
608
407 # And their associated handlers
609 # And their associated handlers
408 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
610 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
409 self.ESC_QUOTE : self.handle_auto,
611 self.ESC_QUOTE : self.handle_auto,
410 self.ESC_QUOTE2 : self.handle_auto,
612 self.ESC_QUOTE2 : self.handle_auto,
411 self.ESC_MAGIC : self.handle_magic,
613 self.ESC_MAGIC : self.handle_magic,
412 self.ESC_HELP : self.handle_help,
614 self.ESC_HELP : self.handle_help,
413 self.ESC_SHELL : self.handle_shell_escape,
615 self.ESC_SHELL : self.handle_shell_escape,
414 self.ESC_SH_CAP : self.handle_shell_escape,
616 self.ESC_SH_CAP : self.handle_shell_escape,
415 }
617 }
416
618
417 # class initializations
619 # class initializations
418 Magic.__init__(self,self)
620 Magic.__init__(self,self)
419
621
420 # Python source parser/formatter for syntax highlighting
622 # Python source parser/formatter for syntax highlighting
421 pyformat = PyColorize.Parser().format
623 pyformat = PyColorize.Parser().format
422 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
624 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
423
625
424 # hooks holds pointers used for user-side customizations
626 # hooks holds pointers used for user-side customizations
425 self.hooks = Struct()
627 self.hooks = Struct()
426
628
427 self.strdispatchers = {}
629 self.strdispatchers = {}
428
630
429 # Set all default hooks, defined in the IPython.hooks module.
631 # Set all default hooks, defined in the IPython.hooks module.
430 hooks = IPython.hooks
632 hooks = IPython.hooks
431 for hook_name in hooks.__all__:
633 for hook_name in hooks.__all__:
432 # default hooks have priority 100, i.e. low; user hooks should have
634 # default hooks have priority 100, i.e. low; user hooks should have
433 # 0-100 priority
635 # 0-100 priority
434 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
636 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
435 #print "bound hook",hook_name
637 #print "bound hook",hook_name
436
638
437 # Flag to mark unconditional exit
639 # Flag to mark unconditional exit
438 self.exit_now = False
640 self.exit_now = False
439
641
440 self.usage_min = """\
642 self.usage_min = """\
441 An enhanced console for Python.
643 An enhanced console for Python.
442 Some of its features are:
644 Some of its features are:
443 - Readline support if the readline library is present.
645 - Readline support if the readline library is present.
444 - Tab completion in the local namespace.
646 - Tab completion in the local namespace.
445 - Logging of input, see command-line options.
647 - Logging of input, see command-line options.
446 - System shell escape via ! , eg !ls.
648 - System shell escape via ! , eg !ls.
447 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
649 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
448 - Keeps track of locally defined variables via %who, %whos.
650 - Keeps track of locally defined variables via %who, %whos.
449 - Show object information with a ? eg ?x or x? (use ?? for more info).
651 - Show object information with a ? eg ?x or x? (use ?? for more info).
450 """
652 """
451 if usage: self.usage = usage
653 if usage: self.usage = usage
452 else: self.usage = self.usage_min
654 else: self.usage = self.usage_min
453
655
454 # Storage
656 # Storage
455 self.rc = rc # This will hold all configuration information
657 self.rc = rc # This will hold all configuration information
456 self.pager = 'less'
658 self.pager = 'less'
457 # temporary files used for various purposes. Deleted at exit.
659 # temporary files used for various purposes. Deleted at exit.
458 self.tempfiles = []
660 self.tempfiles = []
459
661
460 # Keep track of readline usage (later set by init_readline)
662 # Keep track of readline usage (later set by init_readline)
461 self.has_readline = False
663 self.has_readline = False
462
664
463 # template for logfile headers. It gets resolved at runtime by the
665 # template for logfile headers. It gets resolved at runtime by the
464 # logstart method.
666 # logstart method.
465 self.loghead_tpl = \
667 self.loghead_tpl = \
466 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
668 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
467 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
669 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
468 #log# opts = %s
670 #log# opts = %s
469 #log# args = %s
671 #log# args = %s
470 #log# It is safe to make manual edits below here.
672 #log# It is safe to make manual edits below here.
471 #log#-----------------------------------------------------------------------
673 #log#-----------------------------------------------------------------------
472 """
674 """
473 # for pushd/popd management
675 # for pushd/popd management
474 try:
676 try:
475 self.home_dir = get_home_dir()
677 self.home_dir = get_home_dir()
476 except HomeDirError,msg:
678 except HomeDirError,msg:
477 fatal(msg)
679 fatal(msg)
478
680
479 self.dir_stack = []
681 self.dir_stack = []
480
682
481 # Functions to call the underlying shell.
683 # Functions to call the underlying shell.
482
684
483 # The first is similar to os.system, but it doesn't return a value,
685 # The first is similar to os.system, but it doesn't return a value,
484 # and it allows interpolation of variables in the user's namespace.
686 # and it allows interpolation of variables in the user's namespace.
485 self.system = lambda cmd: \
687 self.system = lambda cmd: \
486 self.hooks.shell_hook(self.var_expand(cmd,depth=2))
688 self.hooks.shell_hook(self.var_expand(cmd,depth=2))
487
689
488 # These are for getoutput and getoutputerror:
690 # These are for getoutput and getoutputerror:
489 self.getoutput = lambda cmd: \
691 self.getoutput = lambda cmd: \
490 getoutput(self.var_expand(cmd,depth=2),
692 getoutput(self.var_expand(cmd,depth=2),
491 header=self.rc.system_header,
693 header=self.rc.system_header,
492 verbose=self.rc.system_verbose)
694 verbose=self.rc.system_verbose)
493
695
494 self.getoutputerror = lambda cmd: \
696 self.getoutputerror = lambda cmd: \
495 getoutputerror(self.var_expand(cmd,depth=2),
697 getoutputerror(self.var_expand(cmd,depth=2),
496 header=self.rc.system_header,
698 header=self.rc.system_header,
497 verbose=self.rc.system_verbose)
699 verbose=self.rc.system_verbose)
498
700
499
701
500 # keep track of where we started running (mainly for crash post-mortem)
702 # keep track of where we started running (mainly for crash post-mortem)
501 self.starting_dir = os.getcwd()
703 self.starting_dir = os.getcwd()
502
704
503 # Various switches which can be set
705 # Various switches which can be set
504 self.CACHELENGTH = 5000 # this is cheap, it's just text
706 self.CACHELENGTH = 5000 # this is cheap, it's just text
505 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
707 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
506 self.banner2 = banner2
708 self.banner2 = banner2
507
709
508 # TraceBack handlers:
710 # TraceBack handlers:
509
711
510 # Syntax error handler.
712 # Syntax error handler.
511 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
713 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
512
714
513 # The interactive one is initialized with an offset, meaning we always
715 # The interactive one is initialized with an offset, meaning we always
514 # want to remove the topmost item in the traceback, which is our own
716 # want to remove the topmost item in the traceback, which is our own
515 # internal code. Valid modes: ['Plain','Context','Verbose']
717 # internal code. Valid modes: ['Plain','Context','Verbose']
516 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
718 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
517 color_scheme='NoColor',
719 color_scheme='NoColor',
518 tb_offset = 1)
720 tb_offset = 1)
519
721
520 # IPython itself shouldn't crash. This will produce a detailed
722 # IPython itself shouldn't crash. This will produce a detailed
521 # post-mortem if it does. But we only install the crash handler for
723 # post-mortem if it does. But we only install the crash handler for
522 # non-threaded shells, the threaded ones use a normal verbose reporter
724 # non-threaded shells, the threaded ones use a normal verbose reporter
523 # and lose the crash handler. This is because exceptions in the main
725 # and lose the crash handler. This is because exceptions in the main
524 # thread (such as in GUI code) propagate directly to sys.excepthook,
726 # thread (such as in GUI code) propagate directly to sys.excepthook,
525 # and there's no point in printing crash dumps for every user exception.
727 # and there's no point in printing crash dumps for every user exception.
526 if self.isthreaded:
728 if self.isthreaded:
527 ipCrashHandler = ultraTB.FormattedTB()
729 ipCrashHandler = ultraTB.FormattedTB()
528 else:
730 else:
529 from IPython import CrashHandler
731 from IPython import CrashHandler
530 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
732 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
531 self.set_crash_handler(ipCrashHandler)
733 self.set_crash_handler(ipCrashHandler)
532
734
533 # and add any custom exception handlers the user may have specified
735 # and add any custom exception handlers the user may have specified
534 self.set_custom_exc(*custom_exceptions)
736 self.set_custom_exc(*custom_exceptions)
535
737
536 # indentation management
738 # indentation management
537 self.autoindent = False
739 self.autoindent = False
538 self.indent_current_nsp = 0
740 self.indent_current_nsp = 0
539
741
540 # Make some aliases automatically
742 # Make some aliases automatically
541 # Prepare list of shell aliases to auto-define
743 # Prepare list of shell aliases to auto-define
542 if os.name == 'posix':
744 if os.name == 'posix':
543 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
745 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
544 'mv mv -i','rm rm -i','cp cp -i',
746 'mv mv -i','rm rm -i','cp cp -i',
545 'cat cat','less less','clear clear',
747 'cat cat','less less','clear clear',
546 # a better ls
748 # a better ls
547 'ls ls -F',
749 'ls ls -F',
548 # long ls
750 # long ls
549 'll ls -lF')
751 'll ls -lF')
550 # Extra ls aliases with color, which need special treatment on BSD
752 # Extra ls aliases with color, which need special treatment on BSD
551 # variants
753 # variants
552 ls_extra = ( # color ls
754 ls_extra = ( # color ls
553 'lc ls -F -o --color',
755 'lc ls -F -o --color',
554 # ls normal files only
756 # ls normal files only
555 'lf ls -F -o --color %l | grep ^-',
757 'lf ls -F -o --color %l | grep ^-',
556 # ls symbolic links
758 # ls symbolic links
557 'lk ls -F -o --color %l | grep ^l',
759 'lk ls -F -o --color %l | grep ^l',
558 # directories or links to directories,
760 # directories or links to directories,
559 'ldir ls -F -o --color %l | grep /$',
761 'ldir ls -F -o --color %l | grep /$',
560 # things which are executable
762 # things which are executable
561 'lx ls -F -o --color %l | grep ^-..x',
763 'lx ls -F -o --color %l | grep ^-..x',
562 )
764 )
563 # The BSDs don't ship GNU ls, so they don't understand the
765 # The BSDs don't ship GNU ls, so they don't understand the
564 # --color switch out of the box
766 # --color switch out of the box
565 if 'bsd' in sys.platform:
767 if 'bsd' in sys.platform:
566 ls_extra = ( # ls normal files only
768 ls_extra = ( # ls normal files only
567 'lf ls -lF | grep ^-',
769 'lf ls -lF | grep ^-',
568 # ls symbolic links
770 # ls symbolic links
569 'lk ls -lF | grep ^l',
771 'lk ls -lF | grep ^l',
570 # directories or links to directories,
772 # directories or links to directories,
571 'ldir ls -lF | grep /$',
773 'ldir ls -lF | grep /$',
572 # things which are executable
774 # things which are executable
573 'lx ls -lF | grep ^-..x',
775 'lx ls -lF | grep ^-..x',
574 )
776 )
575 auto_alias = auto_alias + ls_extra
777 auto_alias = auto_alias + ls_extra
576 elif os.name in ['nt','dos']:
778 elif os.name in ['nt','dos']:
577 auto_alias = ('ls dir /on',
779 auto_alias = ('ls dir /on',
578 'ddir dir /ad /on', 'ldir dir /ad /on',
780 'ddir dir /ad /on', 'ldir dir /ad /on',
579 'mkdir mkdir','rmdir rmdir','echo echo',
781 'mkdir mkdir','rmdir rmdir','echo echo',
580 'ren ren','cls cls','copy copy')
782 'ren ren','cls cls','copy copy')
581 else:
783 else:
582 auto_alias = ()
784 auto_alias = ()
583 self.auto_alias = [s.split(None,1) for s in auto_alias]
785 self.auto_alias = [s.split(None,1) for s in auto_alias]
584
786
585 # Produce a public API instance
787 # Produce a public API instance
586 self.api = IPython.ipapi.IPApi(self)
788 self.api = IPython.ipapi.IPApi(self)
587
789
588 # Initialize all user-visible namespaces
790 # Initialize all user-visible namespaces
589 self.init_namespaces()
791 self.init_namespaces()
590
792
591 # Call the actual (public) initializer
793 # Call the actual (public) initializer
592 self.init_auto_alias()
794 self.init_auto_alias()
593
795
594 # track which builtins we add, so we can clean up later
796 # track which builtins we add, so we can clean up later
595 self.builtins_added = {}
797 self.builtins_added = {}
596 # This method will add the necessary builtins for operation, but
798 # This method will add the necessary builtins for operation, but
597 # tracking what it did via the builtins_added dict.
799 # tracking what it did via the builtins_added dict.
598
800
599 #TODO: remove this, redundant
801 #TODO: remove this, redundant
600 self.add_builtins()
802 self.add_builtins()
601 # end __init__
803 # end __init__
602
804
603 def var_expand(self,cmd,depth=0):
805 def var_expand(self,cmd,depth=0):
604 """Expand python variables in a string.
806 """Expand python variables in a string.
605
807
606 The depth argument indicates how many frames above the caller should
808 The depth argument indicates how many frames above the caller should
607 be walked to look for the local namespace where to expand variables.
809 be walked to look for the local namespace where to expand variables.
608
810
609 The global namespace for expansion is always the user's interactive
811 The global namespace for expansion is always the user's interactive
610 namespace.
812 namespace.
611 """
813 """
612
814
613 return str(ItplNS(cmd,
815 return str(ItplNS(cmd,
614 self.user_ns, # globals
816 self.user_ns, # globals
615 # Skip our own frame in searching for locals:
817 # Skip our own frame in searching for locals:
616 sys._getframe(depth+1).f_locals # locals
818 sys._getframe(depth+1).f_locals # locals
617 ))
819 ))
618
820
619 def pre_config_initialization(self):
821 def pre_config_initialization(self):
620 """Pre-configuration init method
822 """Pre-configuration init method
621
823
622 This is called before the configuration files are processed to
824 This is called before the configuration files are processed to
623 prepare the services the config files might need.
825 prepare the services the config files might need.
624
826
625 self.rc already has reasonable default values at this point.
827 self.rc already has reasonable default values at this point.
626 """
828 """
627 rc = self.rc
829 rc = self.rc
628 try:
830 try:
629 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
831 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
630 except exceptions.UnicodeDecodeError:
832 except exceptions.UnicodeDecodeError:
631 print "Your ipythondir can't be decoded to unicode!"
833 print "Your ipythondir can't be decoded to unicode!"
632 print "Please set HOME environment variable to something that"
834 print "Please set HOME environment variable to something that"
633 print r"only has ASCII characters, e.g. c:\home"
835 print r"only has ASCII characters, e.g. c:\home"
634 print "Now it is",rc.ipythondir
836 print "Now it is",rc.ipythondir
635 sys.exit()
837 sys.exit()
636 self.shadowhist = IPython.history.ShadowHist(self.db)
838 self.shadowhist = IPython.history.ShadowHist(self.db)
637
839
638 def post_config_initialization(self):
840 def post_config_initialization(self):
639 """Post configuration init method
841 """Post configuration init method
640
842
641 This is called after the configuration files have been processed to
843 This is called after the configuration files have been processed to
642 'finalize' the initialization."""
844 'finalize' the initialization."""
643
845
644 rc = self.rc
846 rc = self.rc
645
847
646 # Object inspector
848 # Object inspector
647 self.inspector = OInspect.Inspector(OInspect.InspectColors,
849 self.inspector = OInspect.Inspector(OInspect.InspectColors,
648 PyColorize.ANSICodeColors,
850 PyColorize.ANSICodeColors,
649 'NoColor',
851 'NoColor',
650 rc.object_info_string_level)
852 rc.object_info_string_level)
651
853
652 self.rl_next_input = None
854 self.rl_next_input = None
653 self.rl_do_indent = False
855 self.rl_do_indent = False
654 # Load readline proper
856 # Load readline proper
655 if rc.readline:
857 if rc.readline:
656 self.init_readline()
858 self.init_readline()
657
859
658 # local shortcut, this is used a LOT
860 # local shortcut, this is used a LOT
659 self.log = self.logger.log
861 self.log = self.logger.log
660
862
661 # Initialize cache, set in/out prompts and printing system
863 # Initialize cache, set in/out prompts and printing system
662 self.outputcache = CachedOutput(self,
864 self.outputcache = CachedOutput(self,
663 rc.cache_size,
865 rc.cache_size,
664 rc.pprint,
866 rc.pprint,
665 input_sep = rc.separate_in,
867 input_sep = rc.separate_in,
666 output_sep = rc.separate_out,
868 output_sep = rc.separate_out,
667 output_sep2 = rc.separate_out2,
869 output_sep2 = rc.separate_out2,
668 ps1 = rc.prompt_in1,
870 ps1 = rc.prompt_in1,
669 ps2 = rc.prompt_in2,
871 ps2 = rc.prompt_in2,
670 ps_out = rc.prompt_out,
872 ps_out = rc.prompt_out,
671 pad_left = rc.prompts_pad_left)
873 pad_left = rc.prompts_pad_left)
672
874
673 # user may have over-ridden the default print hook:
875 # user may have over-ridden the default print hook:
674 try:
876 try:
675 self.outputcache.__class__.display = self.hooks.display
877 self.outputcache.__class__.display = self.hooks.display
676 except AttributeError:
878 except AttributeError:
677 pass
879 pass
678
880
679 # I don't like assigning globally to sys, because it means when
881 # I don't like assigning globally to sys, because it means when
680 # embedding instances, each embedded instance overrides the previous
882 # embedding instances, each embedded instance overrides the previous
681 # choice. But sys.displayhook seems to be called internally by exec,
883 # choice. But sys.displayhook seems to be called internally by exec,
682 # so I don't see a way around it. We first save the original and then
884 # so I don't see a way around it. We first save the original and then
683 # overwrite it.
885 # overwrite it.
684 self.sys_displayhook = sys.displayhook
886 self.sys_displayhook = sys.displayhook
685 sys.displayhook = self.outputcache
887 sys.displayhook = self.outputcache
686
888
687 # Do a proper resetting of doctest, including the necessary displayhook
889 # Do a proper resetting of doctest, including the necessary displayhook
688 # monkeypatching
890 # monkeypatching
689 try:
891 try:
690 doctest_reload()
892 doctest_reload()
691 except ImportError:
893 except ImportError:
692 warn("doctest module does not exist.")
894 warn("doctest module does not exist.")
693
895
694 # Set user colors (don't do it in the constructor above so that it
896 # Set user colors (don't do it in the constructor above so that it
695 # doesn't crash if colors option is invalid)
897 # doesn't crash if colors option is invalid)
696 self.magic_colors(rc.colors)
898 self.magic_colors(rc.colors)
697
899
698 # Set calling of pdb on exceptions
900 # Set calling of pdb on exceptions
699 self.call_pdb = rc.pdb
901 self.call_pdb = rc.pdb
700
902
701 # Load user aliases
903 # Load user aliases
702 for alias in rc.alias:
904 for alias in rc.alias:
703 self.magic_alias(alias)
905 self.magic_alias(alias)
704
906
705 self.hooks.late_startup_hook()
907 self.hooks.late_startup_hook()
706
908
707 for cmd in self.rc.autoexec:
909 for cmd in self.rc.autoexec:
708 #print "autoexec>",cmd #dbg
910 #print "autoexec>",cmd #dbg
709 self.api.runlines(cmd)
911 self.api.runlines(cmd)
710
912
711 batchrun = False
913 batchrun = False
712 for batchfile in [path(arg) for arg in self.rc.args
914 for batchfile in [path(arg) for arg in self.rc.args
713 if arg.lower().endswith('.ipy')]:
915 if arg.lower().endswith('.ipy')]:
714 if not batchfile.isfile():
916 if not batchfile.isfile():
715 print "No such batch file:", batchfile
917 print "No such batch file:", batchfile
716 continue
918 continue
717 self.api.runlines(batchfile.text())
919 self.api.runlines(batchfile.text())
718 batchrun = True
920 batchrun = True
719 # without -i option, exit after running the batch file
921 # without -i option, exit after running the batch file
720 if batchrun and not self.rc.interact:
922 if batchrun and not self.rc.interact:
721 self.ask_exit()
923 self.ask_exit()
722
924
723 def init_namespaces(self):
925 def init_namespaces(self):
724 """Initialize all user-visible namespaces to their minimum defaults.
926 """Initialize all user-visible namespaces to their minimum defaults.
725
927
726 Certain history lists are also initialized here, as they effectively
928 Certain history lists are also initialized here, as they effectively
727 act as user namespaces.
929 act as user namespaces.
728
930
729 Note
931 Note
730 ----
932 ----
731 All data structures here are only filled in, they are NOT reset by this
933 All data structures here are only filled in, they are NOT reset by this
732 method. If they were not empty before, data will simply be added to
934 method. If they were not empty before, data will simply be added to
733 therm.
935 therm.
734 """
936 """
735 # The user namespace MUST have a pointer to the shell itself.
937 # The user namespace MUST have a pointer to the shell itself.
736 self.user_ns[self.name] = self
938 self.user_ns[self.name] = self
737
939
738 # Store the public api instance
940 # Store the public api instance
739 self.user_ns['_ip'] = self.api
941 self.user_ns['_ip'] = self.api
740
942
741 # make global variables for user access to the histories
943 # make global variables for user access to the histories
742 self.user_ns['_ih'] = self.input_hist
944 self.user_ns['_ih'] = self.input_hist
743 self.user_ns['_oh'] = self.output_hist
945 self.user_ns['_oh'] = self.output_hist
744 self.user_ns['_dh'] = self.dir_hist
946 self.user_ns['_dh'] = self.dir_hist
745
947
746 # user aliases to input and output histories
948 # user aliases to input and output histories
747 self.user_ns['In'] = self.input_hist
949 self.user_ns['In'] = self.input_hist
748 self.user_ns['Out'] = self.output_hist
950 self.user_ns['Out'] = self.output_hist
749
951
750 self.user_ns['_sh'] = IPython.shadowns
952 self.user_ns['_sh'] = IPython.shadowns
751
953
752 # Fill the history zero entry, user counter starts at 1
954 # Fill the history zero entry, user counter starts at 1
753 self.input_hist.append('\n')
955 self.input_hist.append('\n')
754 self.input_hist_raw.append('\n')
956 self.input_hist_raw.append('\n')
755
957
756 def add_builtins(self):
958 def add_builtins(self):
757 """Store ipython references into the builtin namespace.
959 """Store ipython references into the builtin namespace.
758
960
759 Some parts of ipython operate via builtins injected here, which hold a
961 Some parts of ipython operate via builtins injected here, which hold a
760 reference to IPython itself."""
962 reference to IPython itself."""
761
963
762 # TODO: deprecate all of these, they are unsafe
964 # TODO: deprecate all of these, they are unsafe
763 builtins_new = dict(__IPYTHON__ = self,
965 builtins_new = dict(__IPYTHON__ = self,
764 ip_set_hook = self.set_hook,
966 ip_set_hook = self.set_hook,
765 jobs = self.jobs,
967 jobs = self.jobs,
766 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
968 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
767 ipalias = wrap_deprecated(self.ipalias),
969 ipalias = wrap_deprecated(self.ipalias),
768 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
970 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
769 #_ip = self.api
971 #_ip = self.api
770 )
972 )
771 for biname,bival in builtins_new.items():
973 for biname,bival in builtins_new.items():
772 try:
974 try:
773 # store the orignal value so we can restore it
975 # store the orignal value so we can restore it
774 self.builtins_added[biname] = __builtin__.__dict__[biname]
976 self.builtins_added[biname] = __builtin__.__dict__[biname]
775 except KeyError:
977 except KeyError:
776 # or mark that it wasn't defined, and we'll just delete it at
978 # or mark that it wasn't defined, and we'll just delete it at
777 # cleanup
979 # cleanup
778 self.builtins_added[biname] = Undefined
980 self.builtins_added[biname] = Undefined
779 __builtin__.__dict__[biname] = bival
981 __builtin__.__dict__[biname] = bival
780
982
781 # Keep in the builtins a flag for when IPython is active. We set it
983 # Keep in the builtins a flag for when IPython is active. We set it
782 # with setdefault so that multiple nested IPythons don't clobber one
984 # with setdefault so that multiple nested IPythons don't clobber one
783 # another. Each will increase its value by one upon being activated,
985 # another. Each will increase its value by one upon being activated,
784 # which also gives us a way to determine the nesting level.
986 # which also gives us a way to determine the nesting level.
785 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
987 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
786
988
787 def clean_builtins(self):
989 def clean_builtins(self):
788 """Remove any builtins which might have been added by add_builtins, or
990 """Remove any builtins which might have been added by add_builtins, or
789 restore overwritten ones to their previous values."""
991 restore overwritten ones to their previous values."""
790 for biname,bival in self.builtins_added.items():
992 for biname,bival in self.builtins_added.items():
791 if bival is Undefined:
993 if bival is Undefined:
792 del __builtin__.__dict__[biname]
994 del __builtin__.__dict__[biname]
793 else:
995 else:
794 __builtin__.__dict__[biname] = bival
996 __builtin__.__dict__[biname] = bival
795 self.builtins_added.clear()
997 self.builtins_added.clear()
796
998
797 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
999 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
798 """set_hook(name,hook) -> sets an internal IPython hook.
1000 """set_hook(name,hook) -> sets an internal IPython hook.
799
1001
800 IPython exposes some of its internal API as user-modifiable hooks. By
1002 IPython exposes some of its internal API as user-modifiable hooks. By
801 adding your function to one of these hooks, you can modify IPython's
1003 adding your function to one of these hooks, you can modify IPython's
802 behavior to call at runtime your own routines."""
1004 behavior to call at runtime your own routines."""
803
1005
804 # At some point in the future, this should validate the hook before it
1006 # At some point in the future, this should validate the hook before it
805 # accepts it. Probably at least check that the hook takes the number
1007 # accepts it. Probably at least check that the hook takes the number
806 # of args it's supposed to.
1008 # of args it's supposed to.
807
1009
808 f = new.instancemethod(hook,self,self.__class__)
1010 f = new.instancemethod(hook,self,self.__class__)
809
1011
810 # check if the hook is for strdispatcher first
1012 # check if the hook is for strdispatcher first
811 if str_key is not None:
1013 if str_key is not None:
812 sdp = self.strdispatchers.get(name, StrDispatch())
1014 sdp = self.strdispatchers.get(name, StrDispatch())
813 sdp.add_s(str_key, f, priority )
1015 sdp.add_s(str_key, f, priority )
814 self.strdispatchers[name] = sdp
1016 self.strdispatchers[name] = sdp
815 return
1017 return
816 if re_key is not None:
1018 if re_key is not None:
817 sdp = self.strdispatchers.get(name, StrDispatch())
1019 sdp = self.strdispatchers.get(name, StrDispatch())
818 sdp.add_re(re.compile(re_key), f, priority )
1020 sdp.add_re(re.compile(re_key), f, priority )
819 self.strdispatchers[name] = sdp
1021 self.strdispatchers[name] = sdp
820 return
1022 return
821
1023
822 dp = getattr(self.hooks, name, None)
1024 dp = getattr(self.hooks, name, None)
823 if name not in IPython.hooks.__all__:
1025 if name not in IPython.hooks.__all__:
824 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
1026 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
825 if not dp:
1027 if not dp:
826 dp = IPython.hooks.CommandChainDispatcher()
1028 dp = IPython.hooks.CommandChainDispatcher()
827
1029
828 try:
1030 try:
829 dp.add(f,priority)
1031 dp.add(f,priority)
830 except AttributeError:
1032 except AttributeError:
831 # it was not commandchain, plain old func - replace
1033 # it was not commandchain, plain old func - replace
832 dp = f
1034 dp = f
833
1035
834 setattr(self.hooks,name, dp)
1036 setattr(self.hooks,name, dp)
835
1037
836
1038
837 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
1039 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
838
1040
839 def set_crash_handler(self,crashHandler):
1041 def set_crash_handler(self,crashHandler):
840 """Set the IPython crash handler.
1042 """Set the IPython crash handler.
841
1043
842 This must be a callable with a signature suitable for use as
1044 This must be a callable with a signature suitable for use as
843 sys.excepthook."""
1045 sys.excepthook."""
844
1046
845 # Install the given crash handler as the Python exception hook
1047 # Install the given crash handler as the Python exception hook
846 sys.excepthook = crashHandler
1048 sys.excepthook = crashHandler
847
1049
848 # The instance will store a pointer to this, so that runtime code
1050 # The instance will store a pointer to this, so that runtime code
849 # (such as magics) can access it. This is because during the
1051 # (such as magics) can access it. This is because during the
850 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1052 # read-eval loop, it gets temporarily overwritten (to deal with GUI
851 # frameworks).
1053 # frameworks).
852 self.sys_excepthook = sys.excepthook
1054 self.sys_excepthook = sys.excepthook
853
1055
854
1056
855 def set_custom_exc(self,exc_tuple,handler):
1057 def set_custom_exc(self,exc_tuple,handler):
856 """set_custom_exc(exc_tuple,handler)
1058 """set_custom_exc(exc_tuple,handler)
857
1059
858 Set a custom exception handler, which will be called if any of the
1060 Set a custom exception handler, which will be called if any of the
859 exceptions in exc_tuple occur in the mainloop (specifically, in the
1061 exceptions in exc_tuple occur in the mainloop (specifically, in the
860 runcode() method.
1062 runcode() method.
861
1063
862 Inputs:
1064 Inputs:
863
1065
864 - exc_tuple: a *tuple* of valid exceptions to call the defined
1066 - exc_tuple: a *tuple* of valid exceptions to call the defined
865 handler for. It is very important that you use a tuple, and NOT A
1067 handler for. It is very important that you use a tuple, and NOT A
866 LIST here, because of the way Python's except statement works. If
1068 LIST here, because of the way Python's except statement works. If
867 you only want to trap a single exception, use a singleton tuple:
1069 you only want to trap a single exception, use a singleton tuple:
868
1070
869 exc_tuple == (MyCustomException,)
1071 exc_tuple == (MyCustomException,)
870
1072
871 - handler: this must be defined as a function with the following
1073 - handler: this must be defined as a function with the following
872 basic interface: def my_handler(self,etype,value,tb).
1074 basic interface: def my_handler(self,etype,value,tb).
873
1075
874 This will be made into an instance method (via new.instancemethod)
1076 This will be made into an instance method (via new.instancemethod)
875 of IPython itself, and it will be called if any of the exceptions
1077 of IPython itself, and it will be called if any of the exceptions
876 listed in the exc_tuple are caught. If the handler is None, an
1078 listed in the exc_tuple are caught. If the handler is None, an
877 internal basic one is used, which just prints basic info.
1079 internal basic one is used, which just prints basic info.
878
1080
879 WARNING: by putting in your own exception handler into IPython's main
1081 WARNING: by putting in your own exception handler into IPython's main
880 execution loop, you run a very good chance of nasty crashes. This
1082 execution loop, you run a very good chance of nasty crashes. This
881 facility should only be used if you really know what you are doing."""
1083 facility should only be used if you really know what you are doing."""
882
1084
883 assert type(exc_tuple)==type(()) , \
1085 assert type(exc_tuple)==type(()) , \
884 "The custom exceptions must be given AS A TUPLE."
1086 "The custom exceptions must be given AS A TUPLE."
885
1087
886 def dummy_handler(self,etype,value,tb):
1088 def dummy_handler(self,etype,value,tb):
887 print '*** Simple custom exception handler ***'
1089 print '*** Simple custom exception handler ***'
888 print 'Exception type :',etype
1090 print 'Exception type :',etype
889 print 'Exception value:',value
1091 print 'Exception value:',value
890 print 'Traceback :',tb
1092 print 'Traceback :',tb
891 print 'Source code :','\n'.join(self.buffer)
1093 print 'Source code :','\n'.join(self.buffer)
892
1094
893 if handler is None: handler = dummy_handler
1095 if handler is None: handler = dummy_handler
894
1096
895 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1097 self.CustomTB = new.instancemethod(handler,self,self.__class__)
896 self.custom_exceptions = exc_tuple
1098 self.custom_exceptions = exc_tuple
897
1099
898 def set_custom_completer(self,completer,pos=0):
1100 def set_custom_completer(self,completer,pos=0):
899 """set_custom_completer(completer,pos=0)
1101 """set_custom_completer(completer,pos=0)
900
1102
901 Adds a new custom completer function.
1103 Adds a new custom completer function.
902
1104
903 The position argument (defaults to 0) is the index in the completers
1105 The position argument (defaults to 0) is the index in the completers
904 list where you want the completer to be inserted."""
1106 list where you want the completer to be inserted."""
905
1107
906 newcomp = new.instancemethod(completer,self.Completer,
1108 newcomp = new.instancemethod(completer,self.Completer,
907 self.Completer.__class__)
1109 self.Completer.__class__)
908 self.Completer.matchers.insert(pos,newcomp)
1110 self.Completer.matchers.insert(pos,newcomp)
909
1111
910 def set_completer(self):
1112 def set_completer(self):
911 """reset readline's completer to be our own."""
1113 """reset readline's completer to be our own."""
912 self.readline.set_completer(self.Completer.complete)
1114 self.readline.set_completer(self.Completer.complete)
913
1115
914 def _get_call_pdb(self):
1116 def _get_call_pdb(self):
915 return self._call_pdb
1117 return self._call_pdb
916
1118
917 def _set_call_pdb(self,val):
1119 def _set_call_pdb(self,val):
918
1120
919 if val not in (0,1,False,True):
1121 if val not in (0,1,False,True):
920 raise ValueError,'new call_pdb value must be boolean'
1122 raise ValueError,'new call_pdb value must be boolean'
921
1123
922 # store value in instance
1124 # store value in instance
923 self._call_pdb = val
1125 self._call_pdb = val
924
1126
925 # notify the actual exception handlers
1127 # notify the actual exception handlers
926 self.InteractiveTB.call_pdb = val
1128 self.InteractiveTB.call_pdb = val
927 if self.isthreaded:
1129 if self.isthreaded:
928 try:
1130 try:
929 self.sys_excepthook.call_pdb = val
1131 self.sys_excepthook.call_pdb = val
930 except:
1132 except:
931 warn('Failed to activate pdb for threaded exception handler')
1133 warn('Failed to activate pdb for threaded exception handler')
932
1134
933 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1135 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
934 'Control auto-activation of pdb at exceptions')
1136 'Control auto-activation of pdb at exceptions')
935
1137
936 # These special functions get installed in the builtin namespace, to
1138 # These special functions get installed in the builtin namespace, to
937 # provide programmatic (pure python) access to magics, aliases and system
1139 # provide programmatic (pure python) access to magics, aliases and system
938 # calls. This is important for logging, user scripting, and more.
1140 # calls. This is important for logging, user scripting, and more.
939
1141
940 # We are basically exposing, via normal python functions, the three
1142 # We are basically exposing, via normal python functions, the three
941 # mechanisms in which ipython offers special call modes (magics for
1143 # mechanisms in which ipython offers special call modes (magics for
942 # internal control, aliases for direct system access via pre-selected
1144 # internal control, aliases for direct system access via pre-selected
943 # names, and !cmd for calling arbitrary system commands).
1145 # names, and !cmd for calling arbitrary system commands).
944
1146
945 def ipmagic(self,arg_s):
1147 def ipmagic(self,arg_s):
946 """Call a magic function by name.
1148 """Call a magic function by name.
947
1149
948 Input: a string containing the name of the magic function to call and any
1150 Input: a string containing the name of the magic function to call and any
949 additional arguments to be passed to the magic.
1151 additional arguments to be passed to the magic.
950
1152
951 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
1153 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
952 prompt:
1154 prompt:
953
1155
954 In[1]: %name -opt foo bar
1156 In[1]: %name -opt foo bar
955
1157
956 To call a magic without arguments, simply use ipmagic('name').
1158 To call a magic without arguments, simply use ipmagic('name').
957
1159
958 This provides a proper Python function to call IPython's magics in any
1160 This provides a proper Python function to call IPython's magics in any
959 valid Python code you can type at the interpreter, including loops and
1161 valid Python code you can type at the interpreter, including loops and
960 compound statements. It is added by IPython to the Python builtin
1162 compound statements. It is added by IPython to the Python builtin
961 namespace upon initialization."""
1163 namespace upon initialization."""
962
1164
963 args = arg_s.split(' ',1)
1165 args = arg_s.split(' ',1)
964 magic_name = args[0]
1166 magic_name = args[0]
965 magic_name = magic_name.lstrip(self.ESC_MAGIC)
1167 magic_name = magic_name.lstrip(self.ESC_MAGIC)
966
1168
967 try:
1169 try:
968 magic_args = args[1]
1170 magic_args = args[1]
969 except IndexError:
1171 except IndexError:
970 magic_args = ''
1172 magic_args = ''
971 fn = getattr(self,'magic_'+magic_name,None)
1173 fn = getattr(self,'magic_'+magic_name,None)
972 if fn is None:
1174 if fn is None:
973 error("Magic function `%s` not found." % magic_name)
1175 error("Magic function `%s` not found." % magic_name)
974 else:
1176 else:
975 magic_args = self.var_expand(magic_args,1)
1177 magic_args = self.var_expand(magic_args,1)
976 return fn(magic_args)
1178 return fn(magic_args)
977
1179
978 def ipalias(self,arg_s):
1180 def ipalias(self,arg_s):
979 """Call an alias by name.
1181 """Call an alias by name.
980
1182
981 Input: a string containing the name of the alias to call and any
1183 Input: a string containing the name of the alias to call and any
982 additional arguments to be passed to the magic.
1184 additional arguments to be passed to the magic.
983
1185
984 ipalias('name -opt foo bar') is equivalent to typing at the ipython
1186 ipalias('name -opt foo bar') is equivalent to typing at the ipython
985 prompt:
1187 prompt:
986
1188
987 In[1]: name -opt foo bar
1189 In[1]: name -opt foo bar
988
1190
989 To call an alias without arguments, simply use ipalias('name').
1191 To call an alias without arguments, simply use ipalias('name').
990
1192
991 This provides a proper Python function to call IPython's aliases in any
1193 This provides a proper Python function to call IPython's aliases in any
992 valid Python code you can type at the interpreter, including loops and
1194 valid Python code you can type at the interpreter, including loops and
993 compound statements. It is added by IPython to the Python builtin
1195 compound statements. It is added by IPython to the Python builtin
994 namespace upon initialization."""
1196 namespace upon initialization."""
995
1197
996 args = arg_s.split(' ',1)
1198 args = arg_s.split(' ',1)
997 alias_name = args[0]
1199 alias_name = args[0]
998 try:
1200 try:
999 alias_args = args[1]
1201 alias_args = args[1]
1000 except IndexError:
1202 except IndexError:
1001 alias_args = ''
1203 alias_args = ''
1002 if alias_name in self.alias_table:
1204 if alias_name in self.alias_table:
1003 self.call_alias(alias_name,alias_args)
1205 self.call_alias(alias_name,alias_args)
1004 else:
1206 else:
1005 error("Alias `%s` not found." % alias_name)
1207 error("Alias `%s` not found." % alias_name)
1006
1208
1007 def ipsystem(self,arg_s):
1209 def ipsystem(self,arg_s):
1008 """Make a system call, using IPython."""
1210 """Make a system call, using IPython."""
1009
1211
1010 self.system(arg_s)
1212 self.system(arg_s)
1011
1213
1012 def complete(self,text):
1214 def complete(self,text):
1013 """Return a sorted list of all possible completions on text.
1215 """Return a sorted list of all possible completions on text.
1014
1216
1015 Inputs:
1217 Inputs:
1016
1218
1017 - text: a string of text to be completed on.
1219 - text: a string of text to be completed on.
1018
1220
1019 This is a wrapper around the completion mechanism, similar to what
1221 This is a wrapper around the completion mechanism, similar to what
1020 readline does at the command line when the TAB key is hit. By
1222 readline does at the command line when the TAB key is hit. By
1021 exposing it as a method, it can be used by other non-readline
1223 exposing it as a method, it can be used by other non-readline
1022 environments (such as GUIs) for text completion.
1224 environments (such as GUIs) for text completion.
1023
1225
1024 Simple usage example:
1226 Simple usage example:
1025
1227
1026 In [7]: x = 'hello'
1228 In [7]: x = 'hello'
1027
1229
1028 In [8]: x
1230 In [8]: x
1029 Out[8]: 'hello'
1231 Out[8]: 'hello'
1030
1232
1031 In [9]: print x
1233 In [9]: print x
1032 hello
1234 hello
1033
1235
1034 In [10]: _ip.IP.complete('x.l')
1236 In [10]: _ip.IP.complete('x.l')
1035 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1237 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1036 """
1238 """
1037
1239
1038 complete = self.Completer.complete
1240 complete = self.Completer.complete
1039 state = 0
1241 state = 0
1040 # use a dict so we get unique keys, since ipyhton's multiple
1242 # use a dict so we get unique keys, since ipyhton's multiple
1041 # completers can return duplicates. When we make 2.4 a requirement,
1243 # completers can return duplicates. When we make 2.4 a requirement,
1042 # start using sets instead, which are faster.
1244 # start using sets instead, which are faster.
1043 comps = {}
1245 comps = {}
1044 while True:
1246 while True:
1045 newcomp = complete(text,state,line_buffer=text)
1247 newcomp = complete(text,state,line_buffer=text)
1046 if newcomp is None:
1248 if newcomp is None:
1047 break
1249 break
1048 comps[newcomp] = 1
1250 comps[newcomp] = 1
1049 state += 1
1251 state += 1
1050 outcomps = comps.keys()
1252 outcomps = comps.keys()
1051 outcomps.sort()
1253 outcomps.sort()
1052 #print "T:",text,"OC:",outcomps # dbg
1254 #print "T:",text,"OC:",outcomps # dbg
1053 #print "vars:",self.user_ns.keys()
1255 #print "vars:",self.user_ns.keys()
1054 return outcomps
1256 return outcomps
1055
1257
1056 def set_completer_frame(self, frame=None):
1258 def set_completer_frame(self, frame=None):
1057 if frame:
1259 if frame:
1058 self.Completer.namespace = frame.f_locals
1260 self.Completer.namespace = frame.f_locals
1059 self.Completer.global_namespace = frame.f_globals
1261 self.Completer.global_namespace = frame.f_globals
1060 else:
1262 else:
1061 self.Completer.namespace = self.user_ns
1263 self.Completer.namespace = self.user_ns
1062 self.Completer.global_namespace = self.user_global_ns
1264 self.Completer.global_namespace = self.user_global_ns
1063
1265
1064 def init_auto_alias(self):
1266 def init_auto_alias(self):
1065 """Define some aliases automatically.
1267 """Define some aliases automatically.
1066
1268
1067 These are ALL parameter-less aliases"""
1269 These are ALL parameter-less aliases"""
1068
1270
1069 for alias,cmd in self.auto_alias:
1271 for alias,cmd in self.auto_alias:
1070 self.getapi().defalias(alias,cmd)
1272 self.getapi().defalias(alias,cmd)
1071
1273
1072
1274
1073 def alias_table_validate(self,verbose=0):
1275 def alias_table_validate(self,verbose=0):
1074 """Update information about the alias table.
1276 """Update information about the alias table.
1075
1277
1076 In particular, make sure no Python keywords/builtins are in it."""
1278 In particular, make sure no Python keywords/builtins are in it."""
1077
1279
1078 no_alias = self.no_alias
1280 no_alias = self.no_alias
1079 for k in self.alias_table.keys():
1281 for k in self.alias_table.keys():
1080 if k in no_alias:
1282 if k in no_alias:
1081 del self.alias_table[k]
1283 del self.alias_table[k]
1082 if verbose:
1284 if verbose:
1083 print ("Deleting alias <%s>, it's a Python "
1285 print ("Deleting alias <%s>, it's a Python "
1084 "keyword or builtin." % k)
1286 "keyword or builtin." % k)
1085
1287
1086 def set_autoindent(self,value=None):
1288 def set_autoindent(self,value=None):
1087 """Set the autoindent flag, checking for readline support.
1289 """Set the autoindent flag, checking for readline support.
1088
1290
1089 If called with no arguments, it acts as a toggle."""
1291 If called with no arguments, it acts as a toggle."""
1090
1292
1091 if not self.has_readline:
1293 if not self.has_readline:
1092 if os.name == 'posix':
1294 if os.name == 'posix':
1093 warn("The auto-indent feature requires the readline library")
1295 warn("The auto-indent feature requires the readline library")
1094 self.autoindent = 0
1296 self.autoindent = 0
1095 return
1297 return
1096 if value is None:
1298 if value is None:
1097 self.autoindent = not self.autoindent
1299 self.autoindent = not self.autoindent
1098 else:
1300 else:
1099 self.autoindent = value
1301 self.autoindent = value
1100
1302
1101 def rc_set_toggle(self,rc_field,value=None):
1303 def rc_set_toggle(self,rc_field,value=None):
1102 """Set or toggle a field in IPython's rc config. structure.
1304 """Set or toggle a field in IPython's rc config. structure.
1103
1305
1104 If called with no arguments, it acts as a toggle.
1306 If called with no arguments, it acts as a toggle.
1105
1307
1106 If called with a non-existent field, the resulting AttributeError
1308 If called with a non-existent field, the resulting AttributeError
1107 exception will propagate out."""
1309 exception will propagate out."""
1108
1310
1109 rc_val = getattr(self.rc,rc_field)
1311 rc_val = getattr(self.rc,rc_field)
1110 if value is None:
1312 if value is None:
1111 value = not rc_val
1313 value = not rc_val
1112 setattr(self.rc,rc_field,value)
1314 setattr(self.rc,rc_field,value)
1113
1315
1114 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1316 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1115 """Install the user configuration directory.
1317 """Install the user configuration directory.
1116
1318
1117 Can be called when running for the first time or to upgrade the user's
1319 Note
1118 .ipython/ directory with the mode parameter. Valid modes are 'install'
1320 ----
1119 and 'upgrade'."""
1321 DEPRECATED: use the top-level user_setup() function instead.
1120
1322 """
1121 def wait():
1323 return user_setup(ipythondir,rc_suffix,mode)
1122 try:
1123 raw_input("Please press <RETURN> to start IPython.")
1124 except EOFError:
1125 print >> Term.cout
1126 print '*'*70
1127
1128 cwd = os.getcwd() # remember where we started
1129 glb = glob.glob
1130 print '*'*70
1131 if mode == 'install':
1132 print \
1133 """Welcome to IPython. I will try to create a personal configuration directory
1134 where you can customize many aspects of IPython's functionality in:\n"""
1135 else:
1136 print 'I am going to upgrade your configuration in:'
1137
1138 print ipythondir
1139
1140 rcdirend = os.path.join('IPython','UserConfig')
1141 cfg = lambda d: os.path.join(d,rcdirend)
1142 try:
1143 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1144 print "Initializing from configuration",rcdir
1145 except IndexError:
1146 warning = """
1147 Installation error. IPython's directory was not found.
1148
1149 Check the following:
1150
1151 The ipython/IPython directory should be in a directory belonging to your
1152 PYTHONPATH environment variable (that is, it should be in a directory
1153 belonging to sys.path). You can copy it explicitly there or just link to it.
1154
1155 IPython will create a minimal default configuration for you.
1156
1157 """
1158 warn(warning)
1159 wait()
1160
1161 if sys.platform =='win32':
1162 inif = 'ipythonrc.ini'
1163 else:
1164 inif = 'ipythonrc'
1165 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults',
1166 inif : '# intentionally left blank' }
1167 os.makedirs(ipythondir, mode = 0777)
1168 for f, cont in minimal_setup.items():
1169 open(ipythondir + '/' + f,'w').write(cont)
1170
1171 return
1172
1173 if mode == 'install':
1174 try:
1175 shutil.copytree(rcdir,ipythondir)
1176 os.chdir(ipythondir)
1177 rc_files = glb("ipythonrc*")
1178 for rc_file in rc_files:
1179 os.rename(rc_file,rc_file+rc_suffix)
1180 except:
1181 warning = """
1182
1183 There was a problem with the installation:
1184 %s
1185 Try to correct it or contact the developers if you think it's a bug.
1186 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1187 warn(warning)
1188 wait()
1189 return
1190
1191 elif mode == 'upgrade':
1192 try:
1193 os.chdir(ipythondir)
1194 except:
1195 print """
1196 Can not upgrade: changing to directory %s failed. Details:
1197 %s
1198 """ % (ipythondir,sys.exc_info()[1])
1199 wait()
1200 return
1201 else:
1202 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1203 for new_full_path in sources:
1204 new_filename = os.path.basename(new_full_path)
1205 if new_filename.startswith('ipythonrc'):
1206 new_filename = new_filename + rc_suffix
1207 # The config directory should only contain files, skip any
1208 # directories which may be there (like CVS)
1209 if os.path.isdir(new_full_path):
1210 continue
1211 if os.path.exists(new_filename):
1212 old_file = new_filename+'.old'
1213 if os.path.exists(old_file):
1214 os.remove(old_file)
1215 os.rename(new_filename,old_file)
1216 shutil.copy(new_full_path,new_filename)
1217 else:
1218 raise ValueError,'unrecognized mode for install:',`mode`
1219
1220 # Fix line-endings to those native to each platform in the config
1221 # directory.
1222 try:
1223 os.chdir(ipythondir)
1224 except:
1225 print """
1226 Problem: changing to directory %s failed.
1227 Details:
1228 %s
1229
1230 Some configuration files may have incorrect line endings. This should not
1231 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1232 wait()
1233 else:
1234 for fname in glb('ipythonrc*'):
1235 try:
1236 native_line_ends(fname,backup=0)
1237 except IOError:
1238 pass
1239
1240 if mode == 'install':
1241 print """
1242 Successful installation!
1243
1244 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1245 IPython manual (there are both HTML and PDF versions supplied with the
1246 distribution) to make sure that your system environment is properly configured
1247 to take advantage of IPython's features.
1248
1249 Important note: the configuration system has changed! The old system is
1250 still in place, but its setting may be partly overridden by the settings in
1251 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1252 if some of the new settings bother you.
1253
1254 """
1255 else:
1256 print """
1257 Successful upgrade!
1258
1259 All files in your directory:
1260 %(ipythondir)s
1261 which would have been overwritten by the upgrade were backed up with a .old
1262 extension. If you had made particular customizations in those files you may
1263 want to merge them back into the new files.""" % locals()
1264 wait()
1265 os.chdir(cwd)
1266 # end user_setup()
1267
1324
1268 def atexit_operations(self):
1325 def atexit_operations(self):
1269 """This will be executed at the time of exit.
1326 """This will be executed at the time of exit.
1270
1327
1271 Saving of persistent data should be performed here. """
1328 Saving of persistent data should be performed here. """
1272
1329
1273 #print '*** IPython exit cleanup ***' # dbg
1330 #print '*** IPython exit cleanup ***' # dbg
1274 # input history
1331 # input history
1275 self.savehist()
1332 self.savehist()
1276
1333
1277 # Cleanup all tempfiles left around
1334 # Cleanup all tempfiles left around
1278 for tfile in self.tempfiles:
1335 for tfile in self.tempfiles:
1279 try:
1336 try:
1280 os.unlink(tfile)
1337 os.unlink(tfile)
1281 except OSError:
1338 except OSError:
1282 pass
1339 pass
1283
1340
1284 # Clear all user namespaces to release all references cleanly.
1341 # Clear all user namespaces to release all references cleanly.
1285 self.reset()
1342 self.reset()
1286
1343
1287 # Run user hooks
1344 # Run user hooks
1288 self.hooks.shutdown_hook()
1345 self.hooks.shutdown_hook()
1289
1346
1290 def reset(self):
1347 def reset(self):
1291 """Clear all internal namespaces.
1348 """Clear all internal namespaces.
1292
1349
1293 Note that this is much more aggressive than %reset, since it clears
1350 Note that this is much more aggressive than %reset, since it clears
1294 fully all namespaces, as well as all input/output lists.
1351 fully all namespaces, as well as all input/output lists.
1295 """
1352 """
1296 for ns in self.ns_refs_table:
1353 for ns in self.ns_refs_table:
1297 ns.clear()
1354 ns.clear()
1298
1355
1299 # Clear input and output histories
1356 # Clear input and output histories
1300 self.input_hist[:] = []
1357 self.input_hist[:] = []
1301 self.input_hist_raw[:] = []
1358 self.input_hist_raw[:] = []
1302 self.output_hist.clear()
1359 self.output_hist.clear()
1303 # Restore the user namespaces to minimal usability
1360 # Restore the user namespaces to minimal usability
1304 self.init_namespaces()
1361 self.init_namespaces()
1305
1362
1306 def savehist(self):
1363 def savehist(self):
1307 """Save input history to a file (via readline library)."""
1364 """Save input history to a file (via readline library)."""
1308
1365
1309 if not self.has_readline:
1366 if not self.has_readline:
1310 return
1367 return
1311
1368
1312 try:
1369 try:
1313 self.readline.write_history_file(self.histfile)
1370 self.readline.write_history_file(self.histfile)
1314 except:
1371 except:
1315 print 'Unable to save IPython command history to file: ' + \
1372 print 'Unable to save IPython command history to file: ' + \
1316 `self.histfile`
1373 `self.histfile`
1317
1374
1318 def reloadhist(self):
1375 def reloadhist(self):
1319 """Reload the input history from disk file."""
1376 """Reload the input history from disk file."""
1320
1377
1321 if self.has_readline:
1378 if self.has_readline:
1322 try:
1379 try:
1323 self.readline.clear_history()
1380 self.readline.clear_history()
1324 self.readline.read_history_file(self.shell.histfile)
1381 self.readline.read_history_file(self.shell.histfile)
1325 except AttributeError:
1382 except AttributeError:
1326 pass
1383 pass
1327
1384
1328
1385
1329 def history_saving_wrapper(self, func):
1386 def history_saving_wrapper(self, func):
1330 """ Wrap func for readline history saving
1387 """ Wrap func for readline history saving
1331
1388
1332 Convert func into callable that saves & restores
1389 Convert func into callable that saves & restores
1333 history around the call """
1390 history around the call """
1334
1391
1335 if not self.has_readline:
1392 if not self.has_readline:
1336 return func
1393 return func
1337
1394
1338 def wrapper():
1395 def wrapper():
1339 self.savehist()
1396 self.savehist()
1340 try:
1397 try:
1341 func()
1398 func()
1342 finally:
1399 finally:
1343 readline.read_history_file(self.histfile)
1400 readline.read_history_file(self.histfile)
1344 return wrapper
1401 return wrapper
1345
1402
1346 def pre_readline(self):
1403 def pre_readline(self):
1347 """readline hook to be used at the start of each line.
1404 """readline hook to be used at the start of each line.
1348
1405
1349 Currently it handles auto-indent only."""
1406 Currently it handles auto-indent only."""
1350
1407
1351 #debugx('self.indent_current_nsp','pre_readline:')
1408 #debugx('self.indent_current_nsp','pre_readline:')
1352
1409
1353 if self.rl_do_indent:
1410 if self.rl_do_indent:
1354 self.readline.insert_text(self.indent_current_str())
1411 self.readline.insert_text(self.indent_current_str())
1355 if self.rl_next_input is not None:
1412 if self.rl_next_input is not None:
1356 self.readline.insert_text(self.rl_next_input)
1413 self.readline.insert_text(self.rl_next_input)
1357 self.rl_next_input = None
1414 self.rl_next_input = None
1358
1415
1359 def init_readline(self):
1416 def init_readline(self):
1360 """Command history completion/saving/reloading."""
1417 """Command history completion/saving/reloading."""
1361
1418
1362
1419
1363 import IPython.rlineimpl as readline
1420 import IPython.rlineimpl as readline
1364
1421
1365 if not readline.have_readline:
1422 if not readline.have_readline:
1366 self.has_readline = 0
1423 self.has_readline = 0
1367 self.readline = None
1424 self.readline = None
1368 # no point in bugging windows users with this every time:
1425 # no point in bugging windows users with this every time:
1369 warn('Readline services not available on this platform.')
1426 warn('Readline services not available on this platform.')
1370 else:
1427 else:
1371 sys.modules['readline'] = readline
1428 sys.modules['readline'] = readline
1372 import atexit
1429 import atexit
1373 from IPython.completer import IPCompleter
1430 from IPython.completer import IPCompleter
1374 self.Completer = IPCompleter(self,
1431 self.Completer = IPCompleter(self,
1375 self.user_ns,
1432 self.user_ns,
1376 self.user_global_ns,
1433 self.user_global_ns,
1377 self.rc.readline_omit__names,
1434 self.rc.readline_omit__names,
1378 self.alias_table)
1435 self.alias_table)
1379 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1436 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1380 self.strdispatchers['complete_command'] = sdisp
1437 self.strdispatchers['complete_command'] = sdisp
1381 self.Completer.custom_completers = sdisp
1438 self.Completer.custom_completers = sdisp
1382 # Platform-specific configuration
1439 # Platform-specific configuration
1383 if os.name == 'nt':
1440 if os.name == 'nt':
1384 self.readline_startup_hook = readline.set_pre_input_hook
1441 self.readline_startup_hook = readline.set_pre_input_hook
1385 else:
1442 else:
1386 self.readline_startup_hook = readline.set_startup_hook
1443 self.readline_startup_hook = readline.set_startup_hook
1387
1444
1388 # Load user's initrc file (readline config)
1445 # Load user's initrc file (readline config)
1389 # Or if libedit is used, load editrc.
1446 # Or if libedit is used, load editrc.
1390 inputrc_name = os.environ.get('INPUTRC')
1447 inputrc_name = os.environ.get('INPUTRC')
1391 if inputrc_name is None:
1448 if inputrc_name is None:
1392 home_dir = get_home_dir()
1449 home_dir = get_home_dir()
1393 if home_dir is not None:
1450 if home_dir is not None:
1394 inputrc_name = '.inputrc'
1451 inputrc_name = '.inputrc'
1395 if readline.uses_libedit:
1452 if readline.uses_libedit:
1396 inputrc_name = '.editrc'
1453 inputrc_name = '.editrc'
1397 inputrc_name = os.path.join(home_dir, inputrc_name)
1454 inputrc_name = os.path.join(home_dir, inputrc_name)
1398 if os.path.isfile(inputrc_name):
1455 if os.path.isfile(inputrc_name):
1399 try:
1456 try:
1400 readline.read_init_file(inputrc_name)
1457 readline.read_init_file(inputrc_name)
1401 except:
1458 except:
1402 warn('Problems reading readline initialization file <%s>'
1459 warn('Problems reading readline initialization file <%s>'
1403 % inputrc_name)
1460 % inputrc_name)
1404
1461
1405 self.has_readline = 1
1462 self.has_readline = 1
1406 self.readline = readline
1463 self.readline = readline
1407 # save this in sys so embedded copies can restore it properly
1464 # save this in sys so embedded copies can restore it properly
1408 sys.ipcompleter = self.Completer.complete
1465 sys.ipcompleter = self.Completer.complete
1409 self.set_completer()
1466 self.set_completer()
1410
1467
1411 # Configure readline according to user's prefs
1468 # Configure readline according to user's prefs
1412 # This is only done if GNU readline is being used. If libedit
1469 # This is only done if GNU readline is being used. If libedit
1413 # is being used (as on Leopard) the readline config is
1470 # is being used (as on Leopard) the readline config is
1414 # not run as the syntax for libedit is different.
1471 # not run as the syntax for libedit is different.
1415 if not readline.uses_libedit:
1472 if not readline.uses_libedit:
1416 for rlcommand in self.rc.readline_parse_and_bind:
1473 for rlcommand in self.rc.readline_parse_and_bind:
1417 #print "loading rl:",rlcommand # dbg
1474 #print "loading rl:",rlcommand # dbg
1418 readline.parse_and_bind(rlcommand)
1475 readline.parse_and_bind(rlcommand)
1419
1476
1420 # remove some chars from the delimiters list
1477 # remove some chars from the delimiters list
1421 delims = readline.get_completer_delims()
1478 delims = readline.get_completer_delims()
1422 delims = delims.translate(string._idmap,
1479 delims = delims.translate(string._idmap,
1423 self.rc.readline_remove_delims)
1480 self.rc.readline_remove_delims)
1424 readline.set_completer_delims(delims)
1481 readline.set_completer_delims(delims)
1425 # otherwise we end up with a monster history after a while:
1482 # otherwise we end up with a monster history after a while:
1426 readline.set_history_length(1000)
1483 readline.set_history_length(1000)
1427 try:
1484 try:
1428 #print '*** Reading readline history' # dbg
1485 #print '*** Reading readline history' # dbg
1429 readline.read_history_file(self.histfile)
1486 readline.read_history_file(self.histfile)
1430 except IOError:
1487 except IOError:
1431 pass # It doesn't exist yet.
1488 pass # It doesn't exist yet.
1432
1489
1433 atexit.register(self.atexit_operations)
1490 atexit.register(self.atexit_operations)
1434 del atexit
1491 del atexit
1435
1492
1436 # Configure auto-indent for all platforms
1493 # Configure auto-indent for all platforms
1437 self.set_autoindent(self.rc.autoindent)
1494 self.set_autoindent(self.rc.autoindent)
1438
1495
1439 def ask_yes_no(self,prompt,default=True):
1496 def ask_yes_no(self,prompt,default=True):
1440 if self.rc.quiet:
1497 if self.rc.quiet:
1441 return True
1498 return True
1442 return ask_yes_no(prompt,default)
1499 return ask_yes_no(prompt,default)
1443
1500
1444 def cache_main_mod(self,mod):
1501 def new_main_mod(self,ns=None):
1445 """Cache a main module.
1502 """Return a new 'main' module object for user code execution.
1503 """
1504 main_mod = self._user_main_module
1505 init_fakemod_dict(main_mod,ns)
1506 return main_mod
1507
1508 def cache_main_mod(self,ns,fname):
1509 """Cache a main module's namespace.
1446
1510
1447 When scripts are executed via %run, we must keep a reference to their
1511 When scripts are executed via %run, we must keep a reference to the
1448 __main__ module (a FakeModule instance) around so that Python doesn't
1512 namespace of their __main__ module (a FakeModule instance) around so
1449 clear it, rendering objects defined therein useless.
1513 that Python doesn't clear it, rendering objects defined therein
1514 useless.
1450
1515
1451 This method keeps said reference in a private dict, keyed by the
1516 This method keeps said reference in a private dict, keyed by the
1452 absolute path of the module object (which corresponds to the script
1517 absolute path of the module object (which corresponds to the script
1453 path). This way, for multiple executions of the same script we only
1518 path). This way, for multiple executions of the same script we only
1454 keep one copy of __main__ (the last one), thus preventing memory leaks
1519 keep one copy of the namespace (the last one), thus preventing memory
1455 from old references while allowing the objects from the last execution
1520 leaks from old references while allowing the objects from the last
1456 to be accessible.
1521 execution to be accessible.
1522
1523 Note: we can not allow the actual FakeModule instances to be deleted,
1524 because of how Python tears down modules (it hard-sets all their
1525 references to None without regard for reference counts). This method
1526 must therefore make a *copy* of the given namespace, to allow the
1527 original module's __dict__ to be cleared and reused.
1457
1528
1529
1458 Parameters
1530 Parameters
1459 ----------
1531 ----------
1460 mod : a module object
1532 ns : a namespace (a dict, typically)
1533
1534 fname : str
1535 Filename associated with the namespace.
1461
1536
1462 Examples
1537 Examples
1463 --------
1538 --------
1464
1539
1465 In [10]: import IPython
1540 In [10]: import IPython
1466
1541
1467 In [11]: _ip.IP.cache_main_mod(IPython)
1542 In [11]: _ip.IP.cache_main_mod(IPython.__dict__,IPython.__file__)
1468
1543
1469 In [12]: IPython.__file__ in _ip.IP._user_main_modules
1544 In [12]: IPython.__file__ in _ip.IP._main_ns_cache
1470 Out[12]: True
1545 Out[12]: True
1471 """
1546 """
1472 self._user_main_modules[os.path.abspath(mod.__file__) ] = mod
1547 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
1473
1548
1474 def clear_main_mod_cache(self):
1549 def clear_main_mod_cache(self):
1475 """Clear the cache of main modules.
1550 """Clear the cache of main modules.
1476
1551
1477 Mainly for use by utilities like %reset.
1552 Mainly for use by utilities like %reset.
1478
1553
1479 Examples
1554 Examples
1480 --------
1555 --------
1481
1556
1482 In [15]: import IPython
1557 In [15]: import IPython
1483
1558
1484 In [16]: _ip.IP.cache_main_mod(IPython)
1559 In [16]: _ip.IP.cache_main_mod(IPython.__dict__,IPython.__file__)
1485
1560
1486 In [17]: len(_ip.IP._user_main_modules) > 0
1561 In [17]: len(_ip.IP._main_ns_cache) > 0
1487 Out[17]: True
1562 Out[17]: True
1488
1563
1489 In [18]: _ip.IP.clear_main_mod_cache()
1564 In [18]: _ip.IP.clear_main_mod_cache()
1490
1565
1491 In [19]: len(_ip.IP._user_main_modules) == 0
1566 In [19]: len(_ip.IP._main_ns_cache) == 0
1492 Out[19]: True
1567 Out[19]: True
1493 """
1568 """
1494 self._user_main_modules.clear()
1569 self._main_ns_cache.clear()
1495
1570
1496 def _should_recompile(self,e):
1571 def _should_recompile(self,e):
1497 """Utility routine for edit_syntax_error"""
1572 """Utility routine for edit_syntax_error"""
1498
1573
1499 if e.filename in ('<ipython console>','<input>','<string>',
1574 if e.filename in ('<ipython console>','<input>','<string>',
1500 '<console>','<BackgroundJob compilation>',
1575 '<console>','<BackgroundJob compilation>',
1501 None):
1576 None):
1502
1577
1503 return False
1578 return False
1504 try:
1579 try:
1505 if (self.rc.autoedit_syntax and
1580 if (self.rc.autoedit_syntax and
1506 not self.ask_yes_no('Return to editor to correct syntax error? '
1581 not self.ask_yes_no('Return to editor to correct syntax error? '
1507 '[Y/n] ','y')):
1582 '[Y/n] ','y')):
1508 return False
1583 return False
1509 except EOFError:
1584 except EOFError:
1510 return False
1585 return False
1511
1586
1512 def int0(x):
1587 def int0(x):
1513 try:
1588 try:
1514 return int(x)
1589 return int(x)
1515 except TypeError:
1590 except TypeError:
1516 return 0
1591 return 0
1517 # always pass integer line and offset values to editor hook
1592 # always pass integer line and offset values to editor hook
1518 try:
1593 try:
1519 self.hooks.fix_error_editor(e.filename,
1594 self.hooks.fix_error_editor(e.filename,
1520 int0(e.lineno),int0(e.offset),e.msg)
1595 int0(e.lineno),int0(e.offset),e.msg)
1521 except IPython.ipapi.TryNext:
1596 except IPython.ipapi.TryNext:
1522 warn('Could not open editor')
1597 warn('Could not open editor')
1523 return False
1598 return False
1524 return True
1599 return True
1525
1600
1526 def edit_syntax_error(self):
1601 def edit_syntax_error(self):
1527 """The bottom half of the syntax error handler called in the main loop.
1602 """The bottom half of the syntax error handler called in the main loop.
1528
1603
1529 Loop until syntax error is fixed or user cancels.
1604 Loop until syntax error is fixed or user cancels.
1530 """
1605 """
1531
1606
1532 while self.SyntaxTB.last_syntax_error:
1607 while self.SyntaxTB.last_syntax_error:
1533 # copy and clear last_syntax_error
1608 # copy and clear last_syntax_error
1534 err = self.SyntaxTB.clear_err_state()
1609 err = self.SyntaxTB.clear_err_state()
1535 if not self._should_recompile(err):
1610 if not self._should_recompile(err):
1536 return
1611 return
1537 try:
1612 try:
1538 # may set last_syntax_error again if a SyntaxError is raised
1613 # may set last_syntax_error again if a SyntaxError is raised
1539 self.safe_execfile(err.filename,self.user_ns)
1614 self.safe_execfile(err.filename,self.user_ns)
1540 except:
1615 except:
1541 self.showtraceback()
1616 self.showtraceback()
1542 else:
1617 else:
1543 try:
1618 try:
1544 f = file(err.filename)
1619 f = file(err.filename)
1545 try:
1620 try:
1546 sys.displayhook(f.read())
1621 sys.displayhook(f.read())
1547 finally:
1622 finally:
1548 f.close()
1623 f.close()
1549 except:
1624 except:
1550 self.showtraceback()
1625 self.showtraceback()
1551
1626
1552 def showsyntaxerror(self, filename=None):
1627 def showsyntaxerror(self, filename=None):
1553 """Display the syntax error that just occurred.
1628 """Display the syntax error that just occurred.
1554
1629
1555 This doesn't display a stack trace because there isn't one.
1630 This doesn't display a stack trace because there isn't one.
1556
1631
1557 If a filename is given, it is stuffed in the exception instead
1632 If a filename is given, it is stuffed in the exception instead
1558 of what was there before (because Python's parser always uses
1633 of what was there before (because Python's parser always uses
1559 "<string>" when reading from a string).
1634 "<string>" when reading from a string).
1560 """
1635 """
1561 etype, value, last_traceback = sys.exc_info()
1636 etype, value, last_traceback = sys.exc_info()
1562
1637
1563 # See note about these variables in showtraceback() below
1638 # See note about these variables in showtraceback() below
1564 sys.last_type = etype
1639 sys.last_type = etype
1565 sys.last_value = value
1640 sys.last_value = value
1566 sys.last_traceback = last_traceback
1641 sys.last_traceback = last_traceback
1567
1642
1568 if filename and etype is SyntaxError:
1643 if filename and etype is SyntaxError:
1569 # Work hard to stuff the correct filename in the exception
1644 # Work hard to stuff the correct filename in the exception
1570 try:
1645 try:
1571 msg, (dummy_filename, lineno, offset, line) = value
1646 msg, (dummy_filename, lineno, offset, line) = value
1572 except:
1647 except:
1573 # Not the format we expect; leave it alone
1648 # Not the format we expect; leave it alone
1574 pass
1649 pass
1575 else:
1650 else:
1576 # Stuff in the right filename
1651 # Stuff in the right filename
1577 try:
1652 try:
1578 # Assume SyntaxError is a class exception
1653 # Assume SyntaxError is a class exception
1579 value = SyntaxError(msg, (filename, lineno, offset, line))
1654 value = SyntaxError(msg, (filename, lineno, offset, line))
1580 except:
1655 except:
1581 # If that failed, assume SyntaxError is a string
1656 # If that failed, assume SyntaxError is a string
1582 value = msg, (filename, lineno, offset, line)
1657 value = msg, (filename, lineno, offset, line)
1583 self.SyntaxTB(etype,value,[])
1658 self.SyntaxTB(etype,value,[])
1584
1659
1585 def debugger(self,force=False):
1660 def debugger(self,force=False):
1586 """Call the pydb/pdb debugger.
1661 """Call the pydb/pdb debugger.
1587
1662
1588 Keywords:
1663 Keywords:
1589
1664
1590 - force(False): by default, this routine checks the instance call_pdb
1665 - force(False): by default, this routine checks the instance call_pdb
1591 flag and does not actually invoke the debugger if the flag is false.
1666 flag and does not actually invoke the debugger if the flag is false.
1592 The 'force' option forces the debugger to activate even if the flag
1667 The 'force' option forces the debugger to activate even if the flag
1593 is false.
1668 is false.
1594 """
1669 """
1595
1670
1596 if not (force or self.call_pdb):
1671 if not (force or self.call_pdb):
1597 return
1672 return
1598
1673
1599 if not hasattr(sys,'last_traceback'):
1674 if not hasattr(sys,'last_traceback'):
1600 error('No traceback has been produced, nothing to debug.')
1675 error('No traceback has been produced, nothing to debug.')
1601 return
1676 return
1602
1677
1603 # use pydb if available
1678 # use pydb if available
1604 if Debugger.has_pydb:
1679 if Debugger.has_pydb:
1605 from pydb import pm
1680 from pydb import pm
1606 else:
1681 else:
1607 # fallback to our internal debugger
1682 # fallback to our internal debugger
1608 pm = lambda : self.InteractiveTB.debugger(force=True)
1683 pm = lambda : self.InteractiveTB.debugger(force=True)
1609 self.history_saving_wrapper(pm)()
1684 self.history_saving_wrapper(pm)()
1610
1685
1611 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1686 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1612 """Display the exception that just occurred.
1687 """Display the exception that just occurred.
1613
1688
1614 If nothing is known about the exception, this is the method which
1689 If nothing is known about the exception, this is the method which
1615 should be used throughout the code for presenting user tracebacks,
1690 should be used throughout the code for presenting user tracebacks,
1616 rather than directly invoking the InteractiveTB object.
1691 rather than directly invoking the InteractiveTB object.
1617
1692
1618 A specific showsyntaxerror() also exists, but this method can take
1693 A specific showsyntaxerror() also exists, but this method can take
1619 care of calling it if needed, so unless you are explicitly catching a
1694 care of calling it if needed, so unless you are explicitly catching a
1620 SyntaxError exception, don't try to analyze the stack manually and
1695 SyntaxError exception, don't try to analyze the stack manually and
1621 simply call this method."""
1696 simply call this method."""
1622
1697
1623
1698
1624 # Though this won't be called by syntax errors in the input line,
1699 # Though this won't be called by syntax errors in the input line,
1625 # there may be SyntaxError cases whith imported code.
1700 # there may be SyntaxError cases whith imported code.
1626
1701
1627 try:
1702 try:
1628 if exc_tuple is None:
1703 if exc_tuple is None:
1629 etype, value, tb = sys.exc_info()
1704 etype, value, tb = sys.exc_info()
1630 else:
1705 else:
1631 etype, value, tb = exc_tuple
1706 etype, value, tb = exc_tuple
1632
1707
1633 if etype is SyntaxError:
1708 if etype is SyntaxError:
1634 self.showsyntaxerror(filename)
1709 self.showsyntaxerror(filename)
1635 elif etype is IPython.ipapi.UsageError:
1710 elif etype is IPython.ipapi.UsageError:
1636 print "UsageError:", value
1711 print "UsageError:", value
1637 else:
1712 else:
1638 # WARNING: these variables are somewhat deprecated and not
1713 # WARNING: these variables are somewhat deprecated and not
1639 # necessarily safe to use in a threaded environment, but tools
1714 # necessarily safe to use in a threaded environment, but tools
1640 # like pdb depend on their existence, so let's set them. If we
1715 # like pdb depend on their existence, so let's set them. If we
1641 # find problems in the field, we'll need to revisit their use.
1716 # find problems in the field, we'll need to revisit their use.
1642 sys.last_type = etype
1717 sys.last_type = etype
1643 sys.last_value = value
1718 sys.last_value = value
1644 sys.last_traceback = tb
1719 sys.last_traceback = tb
1645
1720
1646 if etype in self.custom_exceptions:
1721 if etype in self.custom_exceptions:
1647 self.CustomTB(etype,value,tb)
1722 self.CustomTB(etype,value,tb)
1648 else:
1723 else:
1649 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1724 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1650 if self.InteractiveTB.call_pdb and self.has_readline:
1725 if self.InteractiveTB.call_pdb and self.has_readline:
1651 # pdb mucks up readline, fix it back
1726 # pdb mucks up readline, fix it back
1652 self.set_completer()
1727 self.set_completer()
1653 except KeyboardInterrupt:
1728 except KeyboardInterrupt:
1654 self.write("\nKeyboardInterrupt\n")
1729 self.write("\nKeyboardInterrupt\n")
1655
1730
1656 def mainloop(self,banner=None):
1731 def mainloop(self,banner=None):
1657 """Creates the local namespace and starts the mainloop.
1732 """Creates the local namespace and starts the mainloop.
1658
1733
1659 If an optional banner argument is given, it will override the
1734 If an optional banner argument is given, it will override the
1660 internally created default banner."""
1735 internally created default banner."""
1661
1736
1662 if self.rc.c: # Emulate Python's -c option
1737 if self.rc.c: # Emulate Python's -c option
1663 self.exec_init_cmd()
1738 self.exec_init_cmd()
1664 if banner is None:
1739 if banner is None:
1665 if not self.rc.banner:
1740 if not self.rc.banner:
1666 banner = ''
1741 banner = ''
1667 # banner is string? Use it directly!
1742 # banner is string? Use it directly!
1668 elif isinstance(self.rc.banner,basestring):
1743 elif isinstance(self.rc.banner,basestring):
1669 banner = self.rc.banner
1744 banner = self.rc.banner
1670 else:
1745 else:
1671 banner = self.BANNER+self.banner2
1746 banner = self.BANNER+self.banner2
1672
1747
1673 # if you run stuff with -c <cmd>, raw hist is not updated
1748 # if you run stuff with -c <cmd>, raw hist is not updated
1674 # ensure that it's in sync
1749 # ensure that it's in sync
1675 if len(self.input_hist) != len (self.input_hist_raw):
1750 if len(self.input_hist) != len (self.input_hist_raw):
1676 self.input_hist_raw = InputList(self.input_hist)
1751 self.input_hist_raw = InputList(self.input_hist)
1677
1752
1678 while 1:
1753 while 1:
1679 try:
1754 try:
1680 self.interact(banner)
1755 self.interact(banner)
1681 #self.interact_with_readline()
1756 #self.interact_with_readline()
1682
1757
1683 # XXX for testing of a readline-decoupled repl loop, call
1758 # XXX for testing of a readline-decoupled repl loop, call
1684 # interact_with_readline above
1759 # interact_with_readline above
1685
1760
1686 break
1761 break
1687 except KeyboardInterrupt:
1762 except KeyboardInterrupt:
1688 # this should not be necessary, but KeyboardInterrupt
1763 # this should not be necessary, but KeyboardInterrupt
1689 # handling seems rather unpredictable...
1764 # handling seems rather unpredictable...
1690 self.write("\nKeyboardInterrupt in interact()\n")
1765 self.write("\nKeyboardInterrupt in interact()\n")
1691
1766
1692 def exec_init_cmd(self):
1767 def exec_init_cmd(self):
1693 """Execute a command given at the command line.
1768 """Execute a command given at the command line.
1694
1769
1695 This emulates Python's -c option."""
1770 This emulates Python's -c option."""
1696
1771
1697 #sys.argv = ['-c']
1772 #sys.argv = ['-c']
1698 self.push(self.prefilter(self.rc.c, False))
1773 self.push(self.prefilter(self.rc.c, False))
1699 if not self.rc.interact:
1774 if not self.rc.interact:
1700 self.ask_exit()
1775 self.ask_exit()
1701
1776
1702 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1777 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1703 """Embeds IPython into a running python program.
1778 """Embeds IPython into a running python program.
1704
1779
1705 Input:
1780 Input:
1706
1781
1707 - header: An optional header message can be specified.
1782 - header: An optional header message can be specified.
1708
1783
1709 - local_ns, global_ns: working namespaces. If given as None, the
1784 - local_ns, global_ns: working namespaces. If given as None, the
1710 IPython-initialized one is updated with __main__.__dict__, so that
1785 IPython-initialized one is updated with __main__.__dict__, so that
1711 program variables become visible but user-specific configuration
1786 program variables become visible but user-specific configuration
1712 remains possible.
1787 remains possible.
1713
1788
1714 - stack_depth: specifies how many levels in the stack to go to
1789 - stack_depth: specifies how many levels in the stack to go to
1715 looking for namespaces (when local_ns and global_ns are None). This
1790 looking for namespaces (when local_ns and global_ns are None). This
1716 allows an intermediate caller to make sure that this function gets
1791 allows an intermediate caller to make sure that this function gets
1717 the namespace from the intended level in the stack. By default (0)
1792 the namespace from the intended level in the stack. By default (0)
1718 it will get its locals and globals from the immediate caller.
1793 it will get its locals and globals from the immediate caller.
1719
1794
1720 Warning: it's possible to use this in a program which is being run by
1795 Warning: it's possible to use this in a program which is being run by
1721 IPython itself (via %run), but some funny things will happen (a few
1796 IPython itself (via %run), but some funny things will happen (a few
1722 globals get overwritten). In the future this will be cleaned up, as
1797 globals get overwritten). In the future this will be cleaned up, as
1723 there is no fundamental reason why it can't work perfectly."""
1798 there is no fundamental reason why it can't work perfectly."""
1724
1799
1725 # Get locals and globals from caller
1800 # Get locals and globals from caller
1726 if local_ns is None or global_ns is None:
1801 if local_ns is None or global_ns is None:
1727 call_frame = sys._getframe(stack_depth).f_back
1802 call_frame = sys._getframe(stack_depth).f_back
1728
1803
1729 if local_ns is None:
1804 if local_ns is None:
1730 local_ns = call_frame.f_locals
1805 local_ns = call_frame.f_locals
1731 if global_ns is None:
1806 if global_ns is None:
1732 global_ns = call_frame.f_globals
1807 global_ns = call_frame.f_globals
1733
1808
1734 # Update namespaces and fire up interpreter
1809 # Update namespaces and fire up interpreter
1735
1810
1736 # The global one is easy, we can just throw it in
1811 # The global one is easy, we can just throw it in
1737 self.user_global_ns = global_ns
1812 self.user_global_ns = global_ns
1738
1813
1739 # but the user/local one is tricky: ipython needs it to store internal
1814 # but the user/local one is tricky: ipython needs it to store internal
1740 # data, but we also need the locals. We'll copy locals in the user
1815 # data, but we also need the locals. We'll copy locals in the user
1741 # one, but will track what got copied so we can delete them at exit.
1816 # one, but will track what got copied so we can delete them at exit.
1742 # This is so that a later embedded call doesn't see locals from a
1817 # This is so that a later embedded call doesn't see locals from a
1743 # previous call (which most likely existed in a separate scope).
1818 # previous call (which most likely existed in a separate scope).
1744 local_varnames = local_ns.keys()
1819 local_varnames = local_ns.keys()
1745 self.user_ns.update(local_ns)
1820 self.user_ns.update(local_ns)
1746 #self.user_ns['local_ns'] = local_ns # dbg
1821 #self.user_ns['local_ns'] = local_ns # dbg
1747
1822
1748 # Patch for global embedding to make sure that things don't overwrite
1823 # Patch for global embedding to make sure that things don't overwrite
1749 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1824 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1750 # FIXME. Test this a bit more carefully (the if.. is new)
1825 # FIXME. Test this a bit more carefully (the if.. is new)
1751 if local_ns is None and global_ns is None:
1826 if local_ns is None and global_ns is None:
1752 self.user_global_ns.update(__main__.__dict__)
1827 self.user_global_ns.update(__main__.__dict__)
1753
1828
1754 # make sure the tab-completer has the correct frame information, so it
1829 # make sure the tab-completer has the correct frame information, so it
1755 # actually completes using the frame's locals/globals
1830 # actually completes using the frame's locals/globals
1756 self.set_completer_frame()
1831 self.set_completer_frame()
1757
1832
1758 # before activating the interactive mode, we need to make sure that
1833 # before activating the interactive mode, we need to make sure that
1759 # all names in the builtin namespace needed by ipython point to
1834 # all names in the builtin namespace needed by ipython point to
1760 # ourselves, and not to other instances.
1835 # ourselves, and not to other instances.
1761 self.add_builtins()
1836 self.add_builtins()
1762
1837
1763 self.interact(header)
1838 self.interact(header)
1764
1839
1765 # now, purge out the user namespace from anything we might have added
1840 # now, purge out the user namespace from anything we might have added
1766 # from the caller's local namespace
1841 # from the caller's local namespace
1767 delvar = self.user_ns.pop
1842 delvar = self.user_ns.pop
1768 for var in local_varnames:
1843 for var in local_varnames:
1769 delvar(var,None)
1844 delvar(var,None)
1770 # and clean builtins we may have overridden
1845 # and clean builtins we may have overridden
1771 self.clean_builtins()
1846 self.clean_builtins()
1772
1847
1773 def interact_prompt(self):
1848 def interact_prompt(self):
1774 """ Print the prompt (in read-eval-print loop)
1849 """ Print the prompt (in read-eval-print loop)
1775
1850
1776 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1851 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1777 used in standard IPython flow.
1852 used in standard IPython flow.
1778 """
1853 """
1779 if self.more:
1854 if self.more:
1780 try:
1855 try:
1781 prompt = self.hooks.generate_prompt(True)
1856 prompt = self.hooks.generate_prompt(True)
1782 except:
1857 except:
1783 self.showtraceback()
1858 self.showtraceback()
1784 if self.autoindent:
1859 if self.autoindent:
1785 self.rl_do_indent = True
1860 self.rl_do_indent = True
1786
1861
1787 else:
1862 else:
1788 try:
1863 try:
1789 prompt = self.hooks.generate_prompt(False)
1864 prompt = self.hooks.generate_prompt(False)
1790 except:
1865 except:
1791 self.showtraceback()
1866 self.showtraceback()
1792 self.write(prompt)
1867 self.write(prompt)
1793
1868
1794 def interact_handle_input(self,line):
1869 def interact_handle_input(self,line):
1795 """ Handle the input line (in read-eval-print loop)
1870 """ Handle the input line (in read-eval-print loop)
1796
1871
1797 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1872 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1798 used in standard IPython flow.
1873 used in standard IPython flow.
1799 """
1874 """
1800 if line.lstrip() == line:
1875 if line.lstrip() == line:
1801 self.shadowhist.add(line.strip())
1876 self.shadowhist.add(line.strip())
1802 lineout = self.prefilter(line,self.more)
1877 lineout = self.prefilter(line,self.more)
1803
1878
1804 if line.strip():
1879 if line.strip():
1805 if self.more:
1880 if self.more:
1806 self.input_hist_raw[-1] += '%s\n' % line
1881 self.input_hist_raw[-1] += '%s\n' % line
1807 else:
1882 else:
1808 self.input_hist_raw.append('%s\n' % line)
1883 self.input_hist_raw.append('%s\n' % line)
1809
1884
1810
1885
1811 self.more = self.push(lineout)
1886 self.more = self.push(lineout)
1812 if (self.SyntaxTB.last_syntax_error and
1887 if (self.SyntaxTB.last_syntax_error and
1813 self.rc.autoedit_syntax):
1888 self.rc.autoedit_syntax):
1814 self.edit_syntax_error()
1889 self.edit_syntax_error()
1815
1890
1816 def interact_with_readline(self):
1891 def interact_with_readline(self):
1817 """ Demo of using interact_handle_input, interact_prompt
1892 """ Demo of using interact_handle_input, interact_prompt
1818
1893
1819 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1894 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1820 it should work like this.
1895 it should work like this.
1821 """
1896 """
1822 self.readline_startup_hook(self.pre_readline)
1897 self.readline_startup_hook(self.pre_readline)
1823 while not self.exit_now:
1898 while not self.exit_now:
1824 self.interact_prompt()
1899 self.interact_prompt()
1825 if self.more:
1900 if self.more:
1826 self.rl_do_indent = True
1901 self.rl_do_indent = True
1827 else:
1902 else:
1828 self.rl_do_indent = False
1903 self.rl_do_indent = False
1829 line = raw_input_original().decode(self.stdin_encoding)
1904 line = raw_input_original().decode(self.stdin_encoding)
1830 self.interact_handle_input(line)
1905 self.interact_handle_input(line)
1831
1906
1832
1907
1833 def interact(self, banner=None):
1908 def interact(self, banner=None):
1834 """Closely emulate the interactive Python console.
1909 """Closely emulate the interactive Python console.
1835
1910
1836 The optional banner argument specify the banner to print
1911 The optional banner argument specify the banner to print
1837 before the first interaction; by default it prints a banner
1912 before the first interaction; by default it prints a banner
1838 similar to the one printed by the real Python interpreter,
1913 similar to the one printed by the real Python interpreter,
1839 followed by the current class name in parentheses (so as not
1914 followed by the current class name in parentheses (so as not
1840 to confuse this with the real interpreter -- since it's so
1915 to confuse this with the real interpreter -- since it's so
1841 close!).
1916 close!).
1842
1917
1843 """
1918 """
1844
1919
1845 if self.exit_now:
1920 if self.exit_now:
1846 # batch run -> do not interact
1921 # batch run -> do not interact
1847 return
1922 return
1848 cprt = 'Type "copyright", "credits" or "license" for more information.'
1923 cprt = 'Type "copyright", "credits" or "license" for more information.'
1849 if banner is None:
1924 if banner is None:
1850 self.write("Python %s on %s\n%s\n(%s)\n" %
1925 self.write("Python %s on %s\n%s\n(%s)\n" %
1851 (sys.version, sys.platform, cprt,
1926 (sys.version, sys.platform, cprt,
1852 self.__class__.__name__))
1927 self.__class__.__name__))
1853 else:
1928 else:
1854 self.write(banner)
1929 self.write(banner)
1855
1930
1856 more = 0
1931 more = 0
1857
1932
1858 # Mark activity in the builtins
1933 # Mark activity in the builtins
1859 __builtin__.__dict__['__IPYTHON__active'] += 1
1934 __builtin__.__dict__['__IPYTHON__active'] += 1
1860
1935
1861 if self.has_readline:
1936 if self.has_readline:
1862 self.readline_startup_hook(self.pre_readline)
1937 self.readline_startup_hook(self.pre_readline)
1863 # exit_now is set by a call to %Exit or %Quit, through the
1938 # exit_now is set by a call to %Exit or %Quit, through the
1864 # ask_exit callback.
1939 # ask_exit callback.
1865
1940
1866 while not self.exit_now:
1941 while not self.exit_now:
1867 self.hooks.pre_prompt_hook()
1942 self.hooks.pre_prompt_hook()
1868 if more:
1943 if more:
1869 try:
1944 try:
1870 prompt = self.hooks.generate_prompt(True)
1945 prompt = self.hooks.generate_prompt(True)
1871 except:
1946 except:
1872 self.showtraceback()
1947 self.showtraceback()
1873 if self.autoindent:
1948 if self.autoindent:
1874 self.rl_do_indent = True
1949 self.rl_do_indent = True
1875
1950
1876 else:
1951 else:
1877 try:
1952 try:
1878 prompt = self.hooks.generate_prompt(False)
1953 prompt = self.hooks.generate_prompt(False)
1879 except:
1954 except:
1880 self.showtraceback()
1955 self.showtraceback()
1881 try:
1956 try:
1882 line = self.raw_input(prompt,more)
1957 line = self.raw_input(prompt,more)
1883 if self.exit_now:
1958 if self.exit_now:
1884 # quick exit on sys.std[in|out] close
1959 # quick exit on sys.std[in|out] close
1885 break
1960 break
1886 if self.autoindent:
1961 if self.autoindent:
1887 self.rl_do_indent = False
1962 self.rl_do_indent = False
1888
1963
1889 except KeyboardInterrupt:
1964 except KeyboardInterrupt:
1890 #double-guard against keyboardinterrupts during kbdint handling
1965 #double-guard against keyboardinterrupts during kbdint handling
1891 try:
1966 try:
1892 self.write('\nKeyboardInterrupt\n')
1967 self.write('\nKeyboardInterrupt\n')
1893 self.resetbuffer()
1968 self.resetbuffer()
1894 # keep cache in sync with the prompt counter:
1969 # keep cache in sync with the prompt counter:
1895 self.outputcache.prompt_count -= 1
1970 self.outputcache.prompt_count -= 1
1896
1971
1897 if self.autoindent:
1972 if self.autoindent:
1898 self.indent_current_nsp = 0
1973 self.indent_current_nsp = 0
1899 more = 0
1974 more = 0
1900 except KeyboardInterrupt:
1975 except KeyboardInterrupt:
1901 pass
1976 pass
1902 except EOFError:
1977 except EOFError:
1903 if self.autoindent:
1978 if self.autoindent:
1904 self.rl_do_indent = False
1979 self.rl_do_indent = False
1905 self.readline_startup_hook(None)
1980 self.readline_startup_hook(None)
1906 self.write('\n')
1981 self.write('\n')
1907 self.exit()
1982 self.exit()
1908 except bdb.BdbQuit:
1983 except bdb.BdbQuit:
1909 warn('The Python debugger has exited with a BdbQuit exception.\n'
1984 warn('The Python debugger has exited with a BdbQuit exception.\n'
1910 'Because of how pdb handles the stack, it is impossible\n'
1985 'Because of how pdb handles the stack, it is impossible\n'
1911 'for IPython to properly format this particular exception.\n'
1986 'for IPython to properly format this particular exception.\n'
1912 'IPython will resume normal operation.')
1987 'IPython will resume normal operation.')
1913 except:
1988 except:
1914 # exceptions here are VERY RARE, but they can be triggered
1989 # exceptions here are VERY RARE, but they can be triggered
1915 # asynchronously by signal handlers, for example.
1990 # asynchronously by signal handlers, for example.
1916 self.showtraceback()
1991 self.showtraceback()
1917 else:
1992 else:
1918 more = self.push(line)
1993 more = self.push(line)
1919 if (self.SyntaxTB.last_syntax_error and
1994 if (self.SyntaxTB.last_syntax_error and
1920 self.rc.autoedit_syntax):
1995 self.rc.autoedit_syntax):
1921 self.edit_syntax_error()
1996 self.edit_syntax_error()
1922
1997
1923 # We are off again...
1998 # We are off again...
1924 __builtin__.__dict__['__IPYTHON__active'] -= 1
1999 __builtin__.__dict__['__IPYTHON__active'] -= 1
1925
2000
1926 def excepthook(self, etype, value, tb):
2001 def excepthook(self, etype, value, tb):
1927 """One more defense for GUI apps that call sys.excepthook.
2002 """One more defense for GUI apps that call sys.excepthook.
1928
2003
1929 GUI frameworks like wxPython trap exceptions and call
2004 GUI frameworks like wxPython trap exceptions and call
1930 sys.excepthook themselves. I guess this is a feature that
2005 sys.excepthook themselves. I guess this is a feature that
1931 enables them to keep running after exceptions that would
2006 enables them to keep running after exceptions that would
1932 otherwise kill their mainloop. This is a bother for IPython
2007 otherwise kill their mainloop. This is a bother for IPython
1933 which excepts to catch all of the program exceptions with a try:
2008 which excepts to catch all of the program exceptions with a try:
1934 except: statement.
2009 except: statement.
1935
2010
1936 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
2011 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1937 any app directly invokes sys.excepthook, it will look to the user like
2012 any app directly invokes sys.excepthook, it will look to the user like
1938 IPython crashed. In order to work around this, we can disable the
2013 IPython crashed. In order to work around this, we can disable the
1939 CrashHandler and replace it with this excepthook instead, which prints a
2014 CrashHandler and replace it with this excepthook instead, which prints a
1940 regular traceback using our InteractiveTB. In this fashion, apps which
2015 regular traceback using our InteractiveTB. In this fashion, apps which
1941 call sys.excepthook will generate a regular-looking exception from
2016 call sys.excepthook will generate a regular-looking exception from
1942 IPython, and the CrashHandler will only be triggered by real IPython
2017 IPython, and the CrashHandler will only be triggered by real IPython
1943 crashes.
2018 crashes.
1944
2019
1945 This hook should be used sparingly, only in places which are not likely
2020 This hook should be used sparingly, only in places which are not likely
1946 to be true IPython errors.
2021 to be true IPython errors.
1947 """
2022 """
1948 self.showtraceback((etype,value,tb),tb_offset=0)
2023 self.showtraceback((etype,value,tb),tb_offset=0)
1949
2024
1950 def expand_aliases(self,fn,rest):
2025 def expand_aliases(self,fn,rest):
1951 """ Expand multiple levels of aliases:
2026 """ Expand multiple levels of aliases:
1952
2027
1953 if:
2028 if:
1954
2029
1955 alias foo bar /tmp
2030 alias foo bar /tmp
1956 alias baz foo
2031 alias baz foo
1957
2032
1958 then:
2033 then:
1959
2034
1960 baz huhhahhei -> bar /tmp huhhahhei
2035 baz huhhahhei -> bar /tmp huhhahhei
1961
2036
1962 """
2037 """
1963 line = fn + " " + rest
2038 line = fn + " " + rest
1964
2039
1965 done = set()
2040 done = set()
1966 while 1:
2041 while 1:
1967 pre,fn,rest = prefilter.splitUserInput(line,
2042 pre,fn,rest = prefilter.splitUserInput(line,
1968 prefilter.shell_line_split)
2043 prefilter.shell_line_split)
1969 if fn in self.alias_table:
2044 if fn in self.alias_table:
1970 if fn in done:
2045 if fn in done:
1971 warn("Cyclic alias definition, repeated '%s'" % fn)
2046 warn("Cyclic alias definition, repeated '%s'" % fn)
1972 return ""
2047 return ""
1973 done.add(fn)
2048 done.add(fn)
1974
2049
1975 l2 = self.transform_alias(fn,rest)
2050 l2 = self.transform_alias(fn,rest)
1976 # dir -> dir
2051 # dir -> dir
1977 # print "alias",line, "->",l2 #dbg
2052 # print "alias",line, "->",l2 #dbg
1978 if l2 == line:
2053 if l2 == line:
1979 break
2054 break
1980 # ls -> ls -F should not recurse forever
2055 # ls -> ls -F should not recurse forever
1981 if l2.split(None,1)[0] == line.split(None,1)[0]:
2056 if l2.split(None,1)[0] == line.split(None,1)[0]:
1982 line = l2
2057 line = l2
1983 break
2058 break
1984
2059
1985 line=l2
2060 line=l2
1986
2061
1987
2062
1988 # print "al expand to",line #dbg
2063 # print "al expand to",line #dbg
1989 else:
2064 else:
1990 break
2065 break
1991
2066
1992 return line
2067 return line
1993
2068
1994 def transform_alias(self, alias,rest=''):
2069 def transform_alias(self, alias,rest=''):
1995 """ Transform alias to system command string.
2070 """ Transform alias to system command string.
1996 """
2071 """
1997 trg = self.alias_table[alias]
2072 trg = self.alias_table[alias]
1998
2073
1999 nargs,cmd = trg
2074 nargs,cmd = trg
2000 # print trg #dbg
2075 # print trg #dbg
2001 if ' ' in cmd and os.path.isfile(cmd):
2076 if ' ' in cmd and os.path.isfile(cmd):
2002 cmd = '"%s"' % cmd
2077 cmd = '"%s"' % cmd
2003
2078
2004 # Expand the %l special to be the user's input line
2079 # Expand the %l special to be the user's input line
2005 if cmd.find('%l') >= 0:
2080 if cmd.find('%l') >= 0:
2006 cmd = cmd.replace('%l',rest)
2081 cmd = cmd.replace('%l',rest)
2007 rest = ''
2082 rest = ''
2008 if nargs==0:
2083 if nargs==0:
2009 # Simple, argument-less aliases
2084 # Simple, argument-less aliases
2010 cmd = '%s %s' % (cmd,rest)
2085 cmd = '%s %s' % (cmd,rest)
2011 else:
2086 else:
2012 # Handle aliases with positional arguments
2087 # Handle aliases with positional arguments
2013 args = rest.split(None,nargs)
2088 args = rest.split(None,nargs)
2014 if len(args)< nargs:
2089 if len(args)< nargs:
2015 error('Alias <%s> requires %s arguments, %s given.' %
2090 error('Alias <%s> requires %s arguments, %s given.' %
2016 (alias,nargs,len(args)))
2091 (alias,nargs,len(args)))
2017 return None
2092 return None
2018 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
2093 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
2019 # Now call the macro, evaluating in the user's namespace
2094 # Now call the macro, evaluating in the user's namespace
2020 #print 'new command: <%r>' % cmd # dbg
2095 #print 'new command: <%r>' % cmd # dbg
2021 return cmd
2096 return cmd
2022
2097
2023 def call_alias(self,alias,rest=''):
2098 def call_alias(self,alias,rest=''):
2024 """Call an alias given its name and the rest of the line.
2099 """Call an alias given its name and the rest of the line.
2025
2100
2026 This is only used to provide backwards compatibility for users of
2101 This is only used to provide backwards compatibility for users of
2027 ipalias(), use of which is not recommended for anymore."""
2102 ipalias(), use of which is not recommended for anymore."""
2028
2103
2029 # Now call the macro, evaluating in the user's namespace
2104 # Now call the macro, evaluating in the user's namespace
2030 cmd = self.transform_alias(alias, rest)
2105 cmd = self.transform_alias(alias, rest)
2031 try:
2106 try:
2032 self.system(cmd)
2107 self.system(cmd)
2033 except:
2108 except:
2034 self.showtraceback()
2109 self.showtraceback()
2035
2110
2036 def indent_current_str(self):
2111 def indent_current_str(self):
2037 """return the current level of indentation as a string"""
2112 """return the current level of indentation as a string"""
2038 return self.indent_current_nsp * ' '
2113 return self.indent_current_nsp * ' '
2039
2114
2040 def autoindent_update(self,line):
2115 def autoindent_update(self,line):
2041 """Keep track of the indent level."""
2116 """Keep track of the indent level."""
2042
2117
2043 #debugx('line')
2118 #debugx('line')
2044 #debugx('self.indent_current_nsp')
2119 #debugx('self.indent_current_nsp')
2045 if self.autoindent:
2120 if self.autoindent:
2046 if line:
2121 if line:
2047 inisp = num_ini_spaces(line)
2122 inisp = num_ini_spaces(line)
2048 if inisp < self.indent_current_nsp:
2123 if inisp < self.indent_current_nsp:
2049 self.indent_current_nsp = inisp
2124 self.indent_current_nsp = inisp
2050
2125
2051 if line[-1] == ':':
2126 if line[-1] == ':':
2052 self.indent_current_nsp += 4
2127 self.indent_current_nsp += 4
2053 elif dedent_re.match(line):
2128 elif dedent_re.match(line):
2054 self.indent_current_nsp -= 4
2129 self.indent_current_nsp -= 4
2055 else:
2130 else:
2056 self.indent_current_nsp = 0
2131 self.indent_current_nsp = 0
2057
2132
2058 def runlines(self,lines):
2133 def runlines(self,lines):
2059 """Run a string of one or more lines of source.
2134 """Run a string of one or more lines of source.
2060
2135
2061 This method is capable of running a string containing multiple source
2136 This method is capable of running a string containing multiple source
2062 lines, as if they had been entered at the IPython prompt. Since it
2137 lines, as if they had been entered at the IPython prompt. Since it
2063 exposes IPython's processing machinery, the given strings can contain
2138 exposes IPython's processing machinery, the given strings can contain
2064 magic calls (%magic), special shell access (!cmd), etc."""
2139 magic calls (%magic), special shell access (!cmd), etc."""
2065
2140
2066 # We must start with a clean buffer, in case this is run from an
2141 # We must start with a clean buffer, in case this is run from an
2067 # interactive IPython session (via a magic, for example).
2142 # interactive IPython session (via a magic, for example).
2068 self.resetbuffer()
2143 self.resetbuffer()
2069 lines = lines.split('\n')
2144 lines = lines.split('\n')
2070 more = 0
2145 more = 0
2071
2146
2072 for line in lines:
2147 for line in lines:
2073 # skip blank lines so we don't mess up the prompt counter, but do
2148 # skip blank lines so we don't mess up the prompt counter, but do
2074 # NOT skip even a blank line if we are in a code block (more is
2149 # NOT skip even a blank line if we are in a code block (more is
2075 # true)
2150 # true)
2076
2151
2077 if line or more:
2152 if line or more:
2078 # push to raw history, so hist line numbers stay in sync
2153 # push to raw history, so hist line numbers stay in sync
2079 self.input_hist_raw.append("# " + line + "\n")
2154 self.input_hist_raw.append("# " + line + "\n")
2080 more = self.push(self.prefilter(line,more))
2155 more = self.push(self.prefilter(line,more))
2081 # IPython's runsource returns None if there was an error
2156 # IPython's runsource returns None if there was an error
2082 # compiling the code. This allows us to stop processing right
2157 # compiling the code. This allows us to stop processing right
2083 # away, so the user gets the error message at the right place.
2158 # away, so the user gets the error message at the right place.
2084 if more is None:
2159 if more is None:
2085 break
2160 break
2086 else:
2161 else:
2087 self.input_hist_raw.append("\n")
2162 self.input_hist_raw.append("\n")
2088 # final newline in case the input didn't have it, so that the code
2163 # final newline in case the input didn't have it, so that the code
2089 # actually does get executed
2164 # actually does get executed
2090 if more:
2165 if more:
2091 self.push('\n')
2166 self.push('\n')
2092
2167
2093 def runsource(self, source, filename='<input>', symbol='single'):
2168 def runsource(self, source, filename='<input>', symbol='single'):
2094 """Compile and run some source in the interpreter.
2169 """Compile and run some source in the interpreter.
2095
2170
2096 Arguments are as for compile_command().
2171 Arguments are as for compile_command().
2097
2172
2098 One several things can happen:
2173 One several things can happen:
2099
2174
2100 1) The input is incorrect; compile_command() raised an
2175 1) The input is incorrect; compile_command() raised an
2101 exception (SyntaxError or OverflowError). A syntax traceback
2176 exception (SyntaxError or OverflowError). A syntax traceback
2102 will be printed by calling the showsyntaxerror() method.
2177 will be printed by calling the showsyntaxerror() method.
2103
2178
2104 2) The input is incomplete, and more input is required;
2179 2) The input is incomplete, and more input is required;
2105 compile_command() returned None. Nothing happens.
2180 compile_command() returned None. Nothing happens.
2106
2181
2107 3) The input is complete; compile_command() returned a code
2182 3) The input is complete; compile_command() returned a code
2108 object. The code is executed by calling self.runcode() (which
2183 object. The code is executed by calling self.runcode() (which
2109 also handles run-time exceptions, except for SystemExit).
2184 also handles run-time exceptions, except for SystemExit).
2110
2185
2111 The return value is:
2186 The return value is:
2112
2187
2113 - True in case 2
2188 - True in case 2
2114
2189
2115 - False in the other cases, unless an exception is raised, where
2190 - False in the other cases, unless an exception is raised, where
2116 None is returned instead. This can be used by external callers to
2191 None is returned instead. This can be used by external callers to
2117 know whether to continue feeding input or not.
2192 know whether to continue feeding input or not.
2118
2193
2119 The return value can be used to decide whether to use sys.ps1 or
2194 The return value can be used to decide whether to use sys.ps1 or
2120 sys.ps2 to prompt the next line."""
2195 sys.ps2 to prompt the next line."""
2121
2196
2122 # if the source code has leading blanks, add 'if 1:\n' to it
2197 # if the source code has leading blanks, add 'if 1:\n' to it
2123 # this allows execution of indented pasted code. It is tempting
2198 # this allows execution of indented pasted code. It is tempting
2124 # to add '\n' at the end of source to run commands like ' a=1'
2199 # to add '\n' at the end of source to run commands like ' a=1'
2125 # directly, but this fails for more complicated scenarios
2200 # directly, but this fails for more complicated scenarios
2126 source=source.encode(self.stdin_encoding)
2201 source=source.encode(self.stdin_encoding)
2127 if source[:1] in [' ', '\t']:
2202 if source[:1] in [' ', '\t']:
2128 source = 'if 1:\n%s' % source
2203 source = 'if 1:\n%s' % source
2129
2204
2130 try:
2205 try:
2131 code = self.compile(source,filename,symbol)
2206 code = self.compile(source,filename,symbol)
2132 except (OverflowError, SyntaxError, ValueError, TypeError):
2207 except (OverflowError, SyntaxError, ValueError, TypeError):
2133 # Case 1
2208 # Case 1
2134 self.showsyntaxerror(filename)
2209 self.showsyntaxerror(filename)
2135 return None
2210 return None
2136
2211
2137 if code is None:
2212 if code is None:
2138 # Case 2
2213 # Case 2
2139 return True
2214 return True
2140
2215
2141 # Case 3
2216 # Case 3
2142 # We store the code object so that threaded shells and
2217 # We store the code object so that threaded shells and
2143 # custom exception handlers can access all this info if needed.
2218 # custom exception handlers can access all this info if needed.
2144 # The source corresponding to this can be obtained from the
2219 # The source corresponding to this can be obtained from the
2145 # buffer attribute as '\n'.join(self.buffer).
2220 # buffer attribute as '\n'.join(self.buffer).
2146 self.code_to_run = code
2221 self.code_to_run = code
2147 # now actually execute the code object
2222 # now actually execute the code object
2148 if self.runcode(code) == 0:
2223 if self.runcode(code) == 0:
2149 return False
2224 return False
2150 else:
2225 else:
2151 return None
2226 return None
2152
2227
2153 def runcode(self,code_obj):
2228 def runcode(self,code_obj):
2154 """Execute a code object.
2229 """Execute a code object.
2155
2230
2156 When an exception occurs, self.showtraceback() is called to display a
2231 When an exception occurs, self.showtraceback() is called to display a
2157 traceback.
2232 traceback.
2158
2233
2159 Return value: a flag indicating whether the code to be run completed
2234 Return value: a flag indicating whether the code to be run completed
2160 successfully:
2235 successfully:
2161
2236
2162 - 0: successful execution.
2237 - 0: successful execution.
2163 - 1: an error occurred.
2238 - 1: an error occurred.
2164 """
2239 """
2165
2240
2166 # Set our own excepthook in case the user code tries to call it
2241 # Set our own excepthook in case the user code tries to call it
2167 # directly, so that the IPython crash handler doesn't get triggered
2242 # directly, so that the IPython crash handler doesn't get triggered
2168 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2243 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2169
2244
2170 # we save the original sys.excepthook in the instance, in case config
2245 # we save the original sys.excepthook in the instance, in case config
2171 # code (such as magics) needs access to it.
2246 # code (such as magics) needs access to it.
2172 self.sys_excepthook = old_excepthook
2247 self.sys_excepthook = old_excepthook
2173 outflag = 1 # happens in more places, so it's easier as default
2248 outflag = 1 # happens in more places, so it's easier as default
2174 try:
2249 try:
2175 try:
2250 try:
2176 self.hooks.pre_runcode_hook()
2251 self.hooks.pre_runcode_hook()
2177 exec code_obj in self.user_global_ns, self.user_ns
2252 exec code_obj in self.user_global_ns, self.user_ns
2178 finally:
2253 finally:
2179 # Reset our crash handler in place
2254 # Reset our crash handler in place
2180 sys.excepthook = old_excepthook
2255 sys.excepthook = old_excepthook
2181 except SystemExit:
2256 except SystemExit:
2182 self.resetbuffer()
2257 self.resetbuffer()
2183 self.showtraceback()
2258 self.showtraceback()
2184 warn("Type %exit or %quit to exit IPython "
2259 warn("Type %exit or %quit to exit IPython "
2185 "(%Exit or %Quit do so unconditionally).",level=1)
2260 "(%Exit or %Quit do so unconditionally).",level=1)
2186 except self.custom_exceptions:
2261 except self.custom_exceptions:
2187 etype,value,tb = sys.exc_info()
2262 etype,value,tb = sys.exc_info()
2188 self.CustomTB(etype,value,tb)
2263 self.CustomTB(etype,value,tb)
2189 except:
2264 except:
2190 self.showtraceback()
2265 self.showtraceback()
2191 else:
2266 else:
2192 outflag = 0
2267 outflag = 0
2193 if softspace(sys.stdout, 0):
2268 if softspace(sys.stdout, 0):
2194 print
2269 print
2195 # Flush out code object which has been run (and source)
2270 # Flush out code object which has been run (and source)
2196 self.code_to_run = None
2271 self.code_to_run = None
2197 return outflag
2272 return outflag
2198
2273
2199 def push(self, line):
2274 def push(self, line):
2200 """Push a line to the interpreter.
2275 """Push a line to the interpreter.
2201
2276
2202 The line should not have a trailing newline; it may have
2277 The line should not have a trailing newline; it may have
2203 internal newlines. The line is appended to a buffer and the
2278 internal newlines. The line is appended to a buffer and the
2204 interpreter's runsource() method is called with the
2279 interpreter's runsource() method is called with the
2205 concatenated contents of the buffer as source. If this
2280 concatenated contents of the buffer as source. If this
2206 indicates that the command was executed or invalid, the buffer
2281 indicates that the command was executed or invalid, the buffer
2207 is reset; otherwise, the command is incomplete, and the buffer
2282 is reset; otherwise, the command is incomplete, and the buffer
2208 is left as it was after the line was appended. The return
2283 is left as it was after the line was appended. The return
2209 value is 1 if more input is required, 0 if the line was dealt
2284 value is 1 if more input is required, 0 if the line was dealt
2210 with in some way (this is the same as runsource()).
2285 with in some way (this is the same as runsource()).
2211 """
2286 """
2212
2287
2213 # autoindent management should be done here, and not in the
2288 # autoindent management should be done here, and not in the
2214 # interactive loop, since that one is only seen by keyboard input. We
2289 # interactive loop, since that one is only seen by keyboard input. We
2215 # need this done correctly even for code run via runlines (which uses
2290 # need this done correctly even for code run via runlines (which uses
2216 # push).
2291 # push).
2217
2292
2218 #print 'push line: <%s>' % line # dbg
2293 #print 'push line: <%s>' % line # dbg
2219 for subline in line.splitlines():
2294 for subline in line.splitlines():
2220 self.autoindent_update(subline)
2295 self.autoindent_update(subline)
2221 self.buffer.append(line)
2296 self.buffer.append(line)
2222 more = self.runsource('\n'.join(self.buffer), self.filename)
2297 more = self.runsource('\n'.join(self.buffer), self.filename)
2223 if not more:
2298 if not more:
2224 self.resetbuffer()
2299 self.resetbuffer()
2225 return more
2300 return more
2226
2301
2227 def split_user_input(self, line):
2302 def split_user_input(self, line):
2228 # This is really a hold-over to support ipapi and some extensions
2303 # This is really a hold-over to support ipapi and some extensions
2229 return prefilter.splitUserInput(line)
2304 return prefilter.splitUserInput(line)
2230
2305
2231 def resetbuffer(self):
2306 def resetbuffer(self):
2232 """Reset the input buffer."""
2307 """Reset the input buffer."""
2233 self.buffer[:] = []
2308 self.buffer[:] = []
2234
2309
2235 def raw_input(self,prompt='',continue_prompt=False):
2310 def raw_input(self,prompt='',continue_prompt=False):
2236 """Write a prompt and read a line.
2311 """Write a prompt and read a line.
2237
2312
2238 The returned line does not include the trailing newline.
2313 The returned line does not include the trailing newline.
2239 When the user enters the EOF key sequence, EOFError is raised.
2314 When the user enters the EOF key sequence, EOFError is raised.
2240
2315
2241 Optional inputs:
2316 Optional inputs:
2242
2317
2243 - prompt(''): a string to be printed to prompt the user.
2318 - prompt(''): a string to be printed to prompt the user.
2244
2319
2245 - continue_prompt(False): whether this line is the first one or a
2320 - continue_prompt(False): whether this line is the first one or a
2246 continuation in a sequence of inputs.
2321 continuation in a sequence of inputs.
2247 """
2322 """
2248
2323
2249 # Code run by the user may have modified the readline completer state.
2324 # Code run by the user may have modified the readline completer state.
2250 # We must ensure that our completer is back in place.
2325 # We must ensure that our completer is back in place.
2251 if self.has_readline:
2326 if self.has_readline:
2252 self.set_completer()
2327 self.set_completer()
2253
2328
2254 try:
2329 try:
2255 line = raw_input_original(prompt).decode(self.stdin_encoding)
2330 line = raw_input_original(prompt).decode(self.stdin_encoding)
2256 except ValueError:
2331 except ValueError:
2257 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2332 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2258 " or sys.stdout.close()!\nExiting IPython!")
2333 " or sys.stdout.close()!\nExiting IPython!")
2259 self.ask_exit()
2334 self.ask_exit()
2260 return ""
2335 return ""
2261
2336
2262 # Try to be reasonably smart about not re-indenting pasted input more
2337 # Try to be reasonably smart about not re-indenting pasted input more
2263 # than necessary. We do this by trimming out the auto-indent initial
2338 # than necessary. We do this by trimming out the auto-indent initial
2264 # spaces, if the user's actual input started itself with whitespace.
2339 # spaces, if the user's actual input started itself with whitespace.
2265 #debugx('self.buffer[-1]')
2340 #debugx('self.buffer[-1]')
2266
2341
2267 if self.autoindent:
2342 if self.autoindent:
2268 if num_ini_spaces(line) > self.indent_current_nsp:
2343 if num_ini_spaces(line) > self.indent_current_nsp:
2269 line = line[self.indent_current_nsp:]
2344 line = line[self.indent_current_nsp:]
2270 self.indent_current_nsp = 0
2345 self.indent_current_nsp = 0
2271
2346
2272 # store the unfiltered input before the user has any chance to modify
2347 # store the unfiltered input before the user has any chance to modify
2273 # it.
2348 # it.
2274 if line.strip():
2349 if line.strip():
2275 if continue_prompt:
2350 if continue_prompt:
2276 self.input_hist_raw[-1] += '%s\n' % line
2351 self.input_hist_raw[-1] += '%s\n' % line
2277 if self.has_readline: # and some config option is set?
2352 if self.has_readline: # and some config option is set?
2278 try:
2353 try:
2279 histlen = self.readline.get_current_history_length()
2354 histlen = self.readline.get_current_history_length()
2280 if histlen > 1:
2355 if histlen > 1:
2281 newhist = self.input_hist_raw[-1].rstrip()
2356 newhist = self.input_hist_raw[-1].rstrip()
2282 self.readline.remove_history_item(histlen-1)
2357 self.readline.remove_history_item(histlen-1)
2283 self.readline.replace_history_item(histlen-2,
2358 self.readline.replace_history_item(histlen-2,
2284 newhist.encode(self.stdin_encoding))
2359 newhist.encode(self.stdin_encoding))
2285 except AttributeError:
2360 except AttributeError:
2286 pass # re{move,place}_history_item are new in 2.4.
2361 pass # re{move,place}_history_item are new in 2.4.
2287 else:
2362 else:
2288 self.input_hist_raw.append('%s\n' % line)
2363 self.input_hist_raw.append('%s\n' % line)
2289 # only entries starting at first column go to shadow history
2364 # only entries starting at first column go to shadow history
2290 if line.lstrip() == line:
2365 if line.lstrip() == line:
2291 self.shadowhist.add(line.strip())
2366 self.shadowhist.add(line.strip())
2292 elif not continue_prompt:
2367 elif not continue_prompt:
2293 self.input_hist_raw.append('\n')
2368 self.input_hist_raw.append('\n')
2294 try:
2369 try:
2295 lineout = self.prefilter(line,continue_prompt)
2370 lineout = self.prefilter(line,continue_prompt)
2296 except:
2371 except:
2297 # blanket except, in case a user-defined prefilter crashes, so it
2372 # blanket except, in case a user-defined prefilter crashes, so it
2298 # can't take all of ipython with it.
2373 # can't take all of ipython with it.
2299 self.showtraceback()
2374 self.showtraceback()
2300 return ''
2375 return ''
2301 else:
2376 else:
2302 return lineout
2377 return lineout
2303
2378
2304 def _prefilter(self, line, continue_prompt):
2379 def _prefilter(self, line, continue_prompt):
2305 """Calls different preprocessors, depending on the form of line."""
2380 """Calls different preprocessors, depending on the form of line."""
2306
2381
2307 # All handlers *must* return a value, even if it's blank ('').
2382 # All handlers *must* return a value, even if it's blank ('').
2308
2383
2309 # Lines are NOT logged here. Handlers should process the line as
2384 # Lines are NOT logged here. Handlers should process the line as
2310 # needed, update the cache AND log it (so that the input cache array
2385 # needed, update the cache AND log it (so that the input cache array
2311 # stays synced).
2386 # stays synced).
2312
2387
2313 #.....................................................................
2388 #.....................................................................
2314 # Code begins
2389 # Code begins
2315
2390
2316 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2391 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2317
2392
2318 # save the line away in case we crash, so the post-mortem handler can
2393 # save the line away in case we crash, so the post-mortem handler can
2319 # record it
2394 # record it
2320 self._last_input_line = line
2395 self._last_input_line = line
2321
2396
2322 #print '***line: <%s>' % line # dbg
2397 #print '***line: <%s>' % line # dbg
2323
2398
2324 if not line:
2399 if not line:
2325 # Return immediately on purely empty lines, so that if the user
2400 # Return immediately on purely empty lines, so that if the user
2326 # previously typed some whitespace that started a continuation
2401 # previously typed some whitespace that started a continuation
2327 # prompt, he can break out of that loop with just an empty line.
2402 # prompt, he can break out of that loop with just an empty line.
2328 # This is how the default python prompt works.
2403 # This is how the default python prompt works.
2329
2404
2330 # Only return if the accumulated input buffer was just whitespace!
2405 # Only return if the accumulated input buffer was just whitespace!
2331 if ''.join(self.buffer).isspace():
2406 if ''.join(self.buffer).isspace():
2332 self.buffer[:] = []
2407 self.buffer[:] = []
2333 return ''
2408 return ''
2334
2409
2335 line_info = prefilter.LineInfo(line, continue_prompt)
2410 line_info = prefilter.LineInfo(line, continue_prompt)
2336
2411
2337 # the input history needs to track even empty lines
2412 # the input history needs to track even empty lines
2338 stripped = line.strip()
2413 stripped = line.strip()
2339
2414
2340 if not stripped:
2415 if not stripped:
2341 if not continue_prompt:
2416 if not continue_prompt:
2342 self.outputcache.prompt_count -= 1
2417 self.outputcache.prompt_count -= 1
2343 return self.handle_normal(line_info)
2418 return self.handle_normal(line_info)
2344
2419
2345 # print '***cont',continue_prompt # dbg
2420 # print '***cont',continue_prompt # dbg
2346 # special handlers are only allowed for single line statements
2421 # special handlers are only allowed for single line statements
2347 if continue_prompt and not self.rc.multi_line_specials:
2422 if continue_prompt and not self.rc.multi_line_specials:
2348 return self.handle_normal(line_info)
2423 return self.handle_normal(line_info)
2349
2424
2350
2425
2351 # See whether any pre-existing handler can take care of it
2426 # See whether any pre-existing handler can take care of it
2352 rewritten = self.hooks.input_prefilter(stripped)
2427 rewritten = self.hooks.input_prefilter(stripped)
2353 if rewritten != stripped: # ok, some prefilter did something
2428 if rewritten != stripped: # ok, some prefilter did something
2354 rewritten = line_info.pre + rewritten # add indentation
2429 rewritten = line_info.pre + rewritten # add indentation
2355 return self.handle_normal(prefilter.LineInfo(rewritten,
2430 return self.handle_normal(prefilter.LineInfo(rewritten,
2356 continue_prompt))
2431 continue_prompt))
2357
2432
2358 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2433 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2359
2434
2360 return prefilter.prefilter(line_info, self)
2435 return prefilter.prefilter(line_info, self)
2361
2436
2362
2437
2363 def _prefilter_dumb(self, line, continue_prompt):
2438 def _prefilter_dumb(self, line, continue_prompt):
2364 """simple prefilter function, for debugging"""
2439 """simple prefilter function, for debugging"""
2365 return self.handle_normal(line,continue_prompt)
2440 return self.handle_normal(line,continue_prompt)
2366
2441
2367
2442
2368 def multiline_prefilter(self, line, continue_prompt):
2443 def multiline_prefilter(self, line, continue_prompt):
2369 """ Run _prefilter for each line of input
2444 """ Run _prefilter for each line of input
2370
2445
2371 Covers cases where there are multiple lines in the user entry,
2446 Covers cases where there are multiple lines in the user entry,
2372 which is the case when the user goes back to a multiline history
2447 which is the case when the user goes back to a multiline history
2373 entry and presses enter.
2448 entry and presses enter.
2374
2449
2375 """
2450 """
2376 out = []
2451 out = []
2377 for l in line.rstrip('\n').split('\n'):
2452 for l in line.rstrip('\n').split('\n'):
2378 out.append(self._prefilter(l, continue_prompt))
2453 out.append(self._prefilter(l, continue_prompt))
2379 return '\n'.join(out)
2454 return '\n'.join(out)
2380
2455
2381 # Set the default prefilter() function (this can be user-overridden)
2456 # Set the default prefilter() function (this can be user-overridden)
2382 prefilter = multiline_prefilter
2457 prefilter = multiline_prefilter
2383
2458
2384 def handle_normal(self,line_info):
2459 def handle_normal(self,line_info):
2385 """Handle normal input lines. Use as a template for handlers."""
2460 """Handle normal input lines. Use as a template for handlers."""
2386
2461
2387 # With autoindent on, we need some way to exit the input loop, and I
2462 # With autoindent on, we need some way to exit the input loop, and I
2388 # don't want to force the user to have to backspace all the way to
2463 # don't want to force the user to have to backspace all the way to
2389 # clear the line. The rule will be in this case, that either two
2464 # clear the line. The rule will be in this case, that either two
2390 # lines of pure whitespace in a row, or a line of pure whitespace but
2465 # lines of pure whitespace in a row, or a line of pure whitespace but
2391 # of a size different to the indent level, will exit the input loop.
2466 # of a size different to the indent level, will exit the input loop.
2392 line = line_info.line
2467 line = line_info.line
2393 continue_prompt = line_info.continue_prompt
2468 continue_prompt = line_info.continue_prompt
2394
2469
2395 if (continue_prompt and self.autoindent and line.isspace() and
2470 if (continue_prompt and self.autoindent and line.isspace() and
2396 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2471 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2397 (self.buffer[-1]).isspace() )):
2472 (self.buffer[-1]).isspace() )):
2398 line = ''
2473 line = ''
2399
2474
2400 self.log(line,line,continue_prompt)
2475 self.log(line,line,continue_prompt)
2401 return line
2476 return line
2402
2477
2403 def handle_alias(self,line_info):
2478 def handle_alias(self,line_info):
2404 """Handle alias input lines. """
2479 """Handle alias input lines. """
2405 tgt = self.alias_table[line_info.iFun]
2480 tgt = self.alias_table[line_info.iFun]
2406 # print "=>",tgt #dbg
2481 # print "=>",tgt #dbg
2407 if callable(tgt):
2482 if callable(tgt):
2408 if '$' in line_info.line:
2483 if '$' in line_info.line:
2409 call_meth = '(_ip, _ip.itpl(%s))'
2484 call_meth = '(_ip, _ip.itpl(%s))'
2410 else:
2485 else:
2411 call_meth = '(_ip,%s)'
2486 call_meth = '(_ip,%s)'
2412 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2487 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2413 line_info.iFun,
2488 line_info.iFun,
2414 make_quoted_expr(line_info.line))
2489 make_quoted_expr(line_info.line))
2415 else:
2490 else:
2416 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2491 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2417
2492
2418 # pre is needed, because it carries the leading whitespace. Otherwise
2493 # pre is needed, because it carries the leading whitespace. Otherwise
2419 # aliases won't work in indented sections.
2494 # aliases won't work in indented sections.
2420 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2495 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2421 make_quoted_expr( transformed ))
2496 make_quoted_expr( transformed ))
2422
2497
2423 self.log(line_info.line,line_out,line_info.continue_prompt)
2498 self.log(line_info.line,line_out,line_info.continue_prompt)
2424 #print 'line out:',line_out # dbg
2499 #print 'line out:',line_out # dbg
2425 return line_out
2500 return line_out
2426
2501
2427 def handle_shell_escape(self, line_info):
2502 def handle_shell_escape(self, line_info):
2428 """Execute the line in a shell, empty return value"""
2503 """Execute the line in a shell, empty return value"""
2429 #print 'line in :', `line` # dbg
2504 #print 'line in :', `line` # dbg
2430 line = line_info.line
2505 line = line_info.line
2431 if line.lstrip().startswith('!!'):
2506 if line.lstrip().startswith('!!'):
2432 # rewrite LineInfo's line, iFun and theRest to properly hold the
2507 # rewrite LineInfo's line, iFun and theRest to properly hold the
2433 # call to %sx and the actual command to be executed, so
2508 # call to %sx and the actual command to be executed, so
2434 # handle_magic can work correctly. Note that this works even if
2509 # handle_magic can work correctly. Note that this works even if
2435 # the line is indented, so it handles multi_line_specials
2510 # the line is indented, so it handles multi_line_specials
2436 # properly.
2511 # properly.
2437 new_rest = line.lstrip()[2:]
2512 new_rest = line.lstrip()[2:]
2438 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2513 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2439 line_info.iFun = 'sx'
2514 line_info.iFun = 'sx'
2440 line_info.theRest = new_rest
2515 line_info.theRest = new_rest
2441 return self.handle_magic(line_info)
2516 return self.handle_magic(line_info)
2442 else:
2517 else:
2443 cmd = line.lstrip().lstrip('!')
2518 cmd = line.lstrip().lstrip('!')
2444 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2519 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2445 make_quoted_expr(cmd))
2520 make_quoted_expr(cmd))
2446 # update cache/log and return
2521 # update cache/log and return
2447 self.log(line,line_out,line_info.continue_prompt)
2522 self.log(line,line_out,line_info.continue_prompt)
2448 return line_out
2523 return line_out
2449
2524
2450 def handle_magic(self, line_info):
2525 def handle_magic(self, line_info):
2451 """Execute magic functions."""
2526 """Execute magic functions."""
2452 iFun = line_info.iFun
2527 iFun = line_info.iFun
2453 theRest = line_info.theRest
2528 theRest = line_info.theRest
2454 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2529 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2455 make_quoted_expr(iFun + " " + theRest))
2530 make_quoted_expr(iFun + " " + theRest))
2456 self.log(line_info.line,cmd,line_info.continue_prompt)
2531 self.log(line_info.line,cmd,line_info.continue_prompt)
2457 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2532 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2458 return cmd
2533 return cmd
2459
2534
2460 def handle_auto(self, line_info):
2535 def handle_auto(self, line_info):
2461 """Hande lines which can be auto-executed, quoting if requested."""
2536 """Hande lines which can be auto-executed, quoting if requested."""
2462
2537
2463 line = line_info.line
2538 line = line_info.line
2464 iFun = line_info.iFun
2539 iFun = line_info.iFun
2465 theRest = line_info.theRest
2540 theRest = line_info.theRest
2466 pre = line_info.pre
2541 pre = line_info.pre
2467 continue_prompt = line_info.continue_prompt
2542 continue_prompt = line_info.continue_prompt
2468 obj = line_info.ofind(self)['obj']
2543 obj = line_info.ofind(self)['obj']
2469
2544
2470 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2545 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2471
2546
2472 # This should only be active for single-line input!
2547 # This should only be active for single-line input!
2473 if continue_prompt:
2548 if continue_prompt:
2474 self.log(line,line,continue_prompt)
2549 self.log(line,line,continue_prompt)
2475 return line
2550 return line
2476
2551
2477 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2552 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2478 auto_rewrite = True
2553 auto_rewrite = True
2479
2554
2480 if pre == self.ESC_QUOTE:
2555 if pre == self.ESC_QUOTE:
2481 # Auto-quote splitting on whitespace
2556 # Auto-quote splitting on whitespace
2482 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2557 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2483 elif pre == self.ESC_QUOTE2:
2558 elif pre == self.ESC_QUOTE2:
2484 # Auto-quote whole string
2559 # Auto-quote whole string
2485 newcmd = '%s("%s")' % (iFun,theRest)
2560 newcmd = '%s("%s")' % (iFun,theRest)
2486 elif pre == self.ESC_PAREN:
2561 elif pre == self.ESC_PAREN:
2487 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2562 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2488 else:
2563 else:
2489 # Auto-paren.
2564 # Auto-paren.
2490 # We only apply it to argument-less calls if the autocall
2565 # We only apply it to argument-less calls if the autocall
2491 # parameter is set to 2. We only need to check that autocall is <
2566 # parameter is set to 2. We only need to check that autocall is <
2492 # 2, since this function isn't called unless it's at least 1.
2567 # 2, since this function isn't called unless it's at least 1.
2493 if not theRest and (self.rc.autocall < 2) and not force_auto:
2568 if not theRest and (self.rc.autocall < 2) and not force_auto:
2494 newcmd = '%s %s' % (iFun,theRest)
2569 newcmd = '%s %s' % (iFun,theRest)
2495 auto_rewrite = False
2570 auto_rewrite = False
2496 else:
2571 else:
2497 if not force_auto and theRest.startswith('['):
2572 if not force_auto and theRest.startswith('['):
2498 if hasattr(obj,'__getitem__'):
2573 if hasattr(obj,'__getitem__'):
2499 # Don't autocall in this case: item access for an object
2574 # Don't autocall in this case: item access for an object
2500 # which is BOTH callable and implements __getitem__.
2575 # which is BOTH callable and implements __getitem__.
2501 newcmd = '%s %s' % (iFun,theRest)
2576 newcmd = '%s %s' % (iFun,theRest)
2502 auto_rewrite = False
2577 auto_rewrite = False
2503 else:
2578 else:
2504 # if the object doesn't support [] access, go ahead and
2579 # if the object doesn't support [] access, go ahead and
2505 # autocall
2580 # autocall
2506 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2581 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2507 elif theRest.endswith(';'):
2582 elif theRest.endswith(';'):
2508 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2583 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2509 else:
2584 else:
2510 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2585 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2511
2586
2512 if auto_rewrite:
2587 if auto_rewrite:
2513 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2588 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2514
2589
2515 try:
2590 try:
2516 # plain ascii works better w/ pyreadline, on some machines, so
2591 # plain ascii works better w/ pyreadline, on some machines, so
2517 # we use it and only print uncolored rewrite if we have unicode
2592 # we use it and only print uncolored rewrite if we have unicode
2518 rw = str(rw)
2593 rw = str(rw)
2519 print >>Term.cout, rw
2594 print >>Term.cout, rw
2520 except UnicodeEncodeError:
2595 except UnicodeEncodeError:
2521 print "-------------->" + newcmd
2596 print "-------------->" + newcmd
2522
2597
2523 # log what is now valid Python, not the actual user input (without the
2598 # log what is now valid Python, not the actual user input (without the
2524 # final newline)
2599 # final newline)
2525 self.log(line,newcmd,continue_prompt)
2600 self.log(line,newcmd,continue_prompt)
2526 return newcmd
2601 return newcmd
2527
2602
2528 def handle_help(self, line_info):
2603 def handle_help(self, line_info):
2529 """Try to get some help for the object.
2604 """Try to get some help for the object.
2530
2605
2531 obj? or ?obj -> basic information.
2606 obj? or ?obj -> basic information.
2532 obj?? or ??obj -> more details.
2607 obj?? or ??obj -> more details.
2533 """
2608 """
2534
2609
2535 line = line_info.line
2610 line = line_info.line
2536 # We need to make sure that we don't process lines which would be
2611 # We need to make sure that we don't process lines which would be
2537 # otherwise valid python, such as "x=1 # what?"
2612 # otherwise valid python, such as "x=1 # what?"
2538 try:
2613 try:
2539 codeop.compile_command(line)
2614 codeop.compile_command(line)
2540 except SyntaxError:
2615 except SyntaxError:
2541 # We should only handle as help stuff which is NOT valid syntax
2616 # We should only handle as help stuff which is NOT valid syntax
2542 if line[0]==self.ESC_HELP:
2617 if line[0]==self.ESC_HELP:
2543 line = line[1:]
2618 line = line[1:]
2544 elif line[-1]==self.ESC_HELP:
2619 elif line[-1]==self.ESC_HELP:
2545 line = line[:-1]
2620 line = line[:-1]
2546 self.log(line,'#?'+line,line_info.continue_prompt)
2621 self.log(line,'#?'+line,line_info.continue_prompt)
2547 if line:
2622 if line:
2548 #print 'line:<%r>' % line # dbg
2623 #print 'line:<%r>' % line # dbg
2549 self.magic_pinfo(line)
2624 self.magic_pinfo(line)
2550 else:
2625 else:
2551 page(self.usage,screen_lines=self.rc.screen_length)
2626 page(self.usage,screen_lines=self.rc.screen_length)
2552 return '' # Empty string is needed here!
2627 return '' # Empty string is needed here!
2553 except:
2628 except:
2554 # Pass any other exceptions through to the normal handler
2629 # Pass any other exceptions through to the normal handler
2555 return self.handle_normal(line_info)
2630 return self.handle_normal(line_info)
2556 else:
2631 else:
2557 # If the code compiles ok, we should handle it normally
2632 # If the code compiles ok, we should handle it normally
2558 return self.handle_normal(line_info)
2633 return self.handle_normal(line_info)
2559
2634
2560 def getapi(self):
2635 def getapi(self):
2561 """ Get an IPApi object for this shell instance
2636 """ Get an IPApi object for this shell instance
2562
2637
2563 Getting an IPApi object is always preferable to accessing the shell
2638 Getting an IPApi object is always preferable to accessing the shell
2564 directly, but this holds true especially for extensions.
2639 directly, but this holds true especially for extensions.
2565
2640
2566 It should always be possible to implement an extension with IPApi
2641 It should always be possible to implement an extension with IPApi
2567 alone. If not, contact maintainer to request an addition.
2642 alone. If not, contact maintainer to request an addition.
2568
2643
2569 """
2644 """
2570 return self.api
2645 return self.api
2571
2646
2572 def handle_emacs(self, line_info):
2647 def handle_emacs(self, line_info):
2573 """Handle input lines marked by python-mode."""
2648 """Handle input lines marked by python-mode."""
2574
2649
2575 # Currently, nothing is done. Later more functionality can be added
2650 # Currently, nothing is done. Later more functionality can be added
2576 # here if needed.
2651 # here if needed.
2577
2652
2578 # The input cache shouldn't be updated
2653 # The input cache shouldn't be updated
2579 return line_info.line
2654 return line_info.line
2580
2655
2581
2656
2582 def mktempfile(self,data=None):
2657 def mktempfile(self,data=None):
2583 """Make a new tempfile and return its filename.
2658 """Make a new tempfile and return its filename.
2584
2659
2585 This makes a call to tempfile.mktemp, but it registers the created
2660 This makes a call to tempfile.mktemp, but it registers the created
2586 filename internally so ipython cleans it up at exit time.
2661 filename internally so ipython cleans it up at exit time.
2587
2662
2588 Optional inputs:
2663 Optional inputs:
2589
2664
2590 - data(None): if data is given, it gets written out to the temp file
2665 - data(None): if data is given, it gets written out to the temp file
2591 immediately, and the file is closed again."""
2666 immediately, and the file is closed again."""
2592
2667
2593 filename = tempfile.mktemp('.py','ipython_edit_')
2668 filename = tempfile.mktemp('.py','ipython_edit_')
2594 self.tempfiles.append(filename)
2669 self.tempfiles.append(filename)
2595
2670
2596 if data:
2671 if data:
2597 tmp_file = open(filename,'w')
2672 tmp_file = open(filename,'w')
2598 tmp_file.write(data)
2673 tmp_file.write(data)
2599 tmp_file.close()
2674 tmp_file.close()
2600 return filename
2675 return filename
2601
2676
2602 def write(self,data):
2677 def write(self,data):
2603 """Write a string to the default output"""
2678 """Write a string to the default output"""
2604 Term.cout.write(data)
2679 Term.cout.write(data)
2605
2680
2606 def write_err(self,data):
2681 def write_err(self,data):
2607 """Write a string to the default error output"""
2682 """Write a string to the default error output"""
2608 Term.cerr.write(data)
2683 Term.cerr.write(data)
2609
2684
2610 def ask_exit(self):
2685 def ask_exit(self):
2611 """ Call for exiting. Can be overiden and used as a callback. """
2686 """ Call for exiting. Can be overiden and used as a callback. """
2612 self.exit_now = True
2687 self.exit_now = True
2613
2688
2614 def exit(self):
2689 def exit(self):
2615 """Handle interactive exit.
2690 """Handle interactive exit.
2616
2691
2617 This method calls the ask_exit callback."""
2692 This method calls the ask_exit callback."""
2618
2693
2619 if self.rc.confirm_exit:
2694 if self.rc.confirm_exit:
2620 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2695 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2621 self.ask_exit()
2696 self.ask_exit()
2622 else:
2697 else:
2623 self.ask_exit()
2698 self.ask_exit()
2624
2699
2625 def safe_execfile(self,fname,*where,**kw):
2700 def safe_execfile(self,fname,*where,**kw):
2626 """A safe version of the builtin execfile().
2701 """A safe version of the builtin execfile().
2627
2702
2628 This version will never throw an exception, and knows how to handle
2703 This version will never throw an exception, and knows how to handle
2629 ipython logs as well.
2704 ipython logs as well.
2630
2705
2631 :Parameters:
2706 :Parameters:
2632 fname : string
2707 fname : string
2633 Name of the file to be executed.
2708 Name of the file to be executed.
2634
2709
2635 where : tuple
2710 where : tuple
2636 One or two namespaces, passed to execfile() as (globals,locals).
2711 One or two namespaces, passed to execfile() as (globals,locals).
2637 If only one is given, it is passed as both.
2712 If only one is given, it is passed as both.
2638
2713
2639 :Keywords:
2714 :Keywords:
2640 islog : boolean (False)
2715 islog : boolean (False)
2641
2716
2642 quiet : boolean (True)
2717 quiet : boolean (True)
2643
2718
2644 exit_ignore : boolean (False)
2719 exit_ignore : boolean (False)
2645 """
2720 """
2646
2721
2647 def syspath_cleanup():
2722 def syspath_cleanup():
2648 """Internal cleanup routine for sys.path."""
2723 """Internal cleanup routine for sys.path."""
2649 if add_dname:
2724 if add_dname:
2650 try:
2725 try:
2651 sys.path.remove(dname)
2726 sys.path.remove(dname)
2652 except ValueError:
2727 except ValueError:
2653 # For some reason the user has already removed it, ignore.
2728 # For some reason the user has already removed it, ignore.
2654 pass
2729 pass
2655
2730
2656 fname = os.path.expanduser(fname)
2731 fname = os.path.expanduser(fname)
2657
2732
2658 # Find things also in current directory. This is needed to mimic the
2733 # Find things also in current directory. This is needed to mimic the
2659 # behavior of running a script from the system command line, where
2734 # behavior of running a script from the system command line, where
2660 # Python inserts the script's directory into sys.path
2735 # Python inserts the script's directory into sys.path
2661 dname = os.path.dirname(os.path.abspath(fname))
2736 dname = os.path.dirname(os.path.abspath(fname))
2662 add_dname = False
2737 add_dname = False
2663 if dname not in sys.path:
2738 if dname not in sys.path:
2664 sys.path.insert(0,dname)
2739 sys.path.insert(0,dname)
2665 add_dname = True
2740 add_dname = True
2666
2741
2667 try:
2742 try:
2668 xfile = open(fname)
2743 xfile = open(fname)
2669 except:
2744 except:
2670 print >> Term.cerr, \
2745 print >> Term.cerr, \
2671 'Could not open file <%s> for safe execution.' % fname
2746 'Could not open file <%s> for safe execution.' % fname
2672 syspath_cleanup()
2747 syspath_cleanup()
2673 return None
2748 return None
2674
2749
2675 kw.setdefault('islog',0)
2750 kw.setdefault('islog',0)
2676 kw.setdefault('quiet',1)
2751 kw.setdefault('quiet',1)
2677 kw.setdefault('exit_ignore',0)
2752 kw.setdefault('exit_ignore',0)
2678
2753
2679 first = xfile.readline()
2754 first = xfile.readline()
2680 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2755 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2681 xfile.close()
2756 xfile.close()
2682 # line by line execution
2757 # line by line execution
2683 if first.startswith(loghead) or kw['islog']:
2758 if first.startswith(loghead) or kw['islog']:
2684 print 'Loading log file <%s> one line at a time...' % fname
2759 print 'Loading log file <%s> one line at a time...' % fname
2685 if kw['quiet']:
2760 if kw['quiet']:
2686 stdout_save = sys.stdout
2761 stdout_save = sys.stdout
2687 sys.stdout = StringIO.StringIO()
2762 sys.stdout = StringIO.StringIO()
2688 try:
2763 try:
2689 globs,locs = where[0:2]
2764 globs,locs = where[0:2]
2690 except:
2765 except:
2691 try:
2766 try:
2692 globs = locs = where[0]
2767 globs = locs = where[0]
2693 except:
2768 except:
2694 globs = locs = globals()
2769 globs = locs = globals()
2695 badblocks = []
2770 badblocks = []
2696
2771
2697 # we also need to identify indented blocks of code when replaying
2772 # we also need to identify indented blocks of code when replaying
2698 # logs and put them together before passing them to an exec
2773 # logs and put them together before passing them to an exec
2699 # statement. This takes a bit of regexp and look-ahead work in the
2774 # statement. This takes a bit of regexp and look-ahead work in the
2700 # file. It's easiest if we swallow the whole thing in memory
2775 # file. It's easiest if we swallow the whole thing in memory
2701 # first, and manually walk through the lines list moving the
2776 # first, and manually walk through the lines list moving the
2702 # counter ourselves.
2777 # counter ourselves.
2703 indent_re = re.compile('\s+\S')
2778 indent_re = re.compile('\s+\S')
2704 xfile = open(fname)
2779 xfile = open(fname)
2705 filelines = xfile.readlines()
2780 filelines = xfile.readlines()
2706 xfile.close()
2781 xfile.close()
2707 nlines = len(filelines)
2782 nlines = len(filelines)
2708 lnum = 0
2783 lnum = 0
2709 while lnum < nlines:
2784 while lnum < nlines:
2710 line = filelines[lnum]
2785 line = filelines[lnum]
2711 lnum += 1
2786 lnum += 1
2712 # don't re-insert logger status info into cache
2787 # don't re-insert logger status info into cache
2713 if line.startswith('#log#'):
2788 if line.startswith('#log#'):
2714 continue
2789 continue
2715 else:
2790 else:
2716 # build a block of code (maybe a single line) for execution
2791 # build a block of code (maybe a single line) for execution
2717 block = line
2792 block = line
2718 try:
2793 try:
2719 next = filelines[lnum] # lnum has already incremented
2794 next = filelines[lnum] # lnum has already incremented
2720 except:
2795 except:
2721 next = None
2796 next = None
2722 while next and indent_re.match(next):
2797 while next and indent_re.match(next):
2723 block += next
2798 block += next
2724 lnum += 1
2799 lnum += 1
2725 try:
2800 try:
2726 next = filelines[lnum]
2801 next = filelines[lnum]
2727 except:
2802 except:
2728 next = None
2803 next = None
2729 # now execute the block of one or more lines
2804 # now execute the block of one or more lines
2730 try:
2805 try:
2731 exec block in globs,locs
2806 exec block in globs,locs
2732 except SystemExit:
2807 except SystemExit:
2733 pass
2808 pass
2734 except:
2809 except:
2735 badblocks.append(block.rstrip())
2810 badblocks.append(block.rstrip())
2736 if kw['quiet']: # restore stdout
2811 if kw['quiet']: # restore stdout
2737 sys.stdout.close()
2812 sys.stdout.close()
2738 sys.stdout = stdout_save
2813 sys.stdout = stdout_save
2739 print 'Finished replaying log file <%s>' % fname
2814 print 'Finished replaying log file <%s>' % fname
2740 if badblocks:
2815 if badblocks:
2741 print >> sys.stderr, ('\nThe following lines/blocks in file '
2816 print >> sys.stderr, ('\nThe following lines/blocks in file '
2742 '<%s> reported errors:' % fname)
2817 '<%s> reported errors:' % fname)
2743
2818
2744 for badline in badblocks:
2819 for badline in badblocks:
2745 print >> sys.stderr, badline
2820 print >> sys.stderr, badline
2746 else: # regular file execution
2821 else: # regular file execution
2747 try:
2822 try:
2748 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2823 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2749 # Work around a bug in Python for Windows. The bug was
2824 # Work around a bug in Python for Windows. The bug was
2750 # fixed in in Python 2.5 r54159 and 54158, but that's still
2825 # fixed in in Python 2.5 r54159 and 54158, but that's still
2751 # SVN Python as of March/07. For details, see:
2826 # SVN Python as of March/07. For details, see:
2752 # http://projects.scipy.org/ipython/ipython/ticket/123
2827 # http://projects.scipy.org/ipython/ipython/ticket/123
2753 try:
2828 try:
2754 globs,locs = where[0:2]
2829 globs,locs = where[0:2]
2755 except:
2830 except:
2756 try:
2831 try:
2757 globs = locs = where[0]
2832 globs = locs = where[0]
2758 except:
2833 except:
2759 globs = locs = globals()
2834 globs = locs = globals()
2760 exec file(fname) in globs,locs
2835 exec file(fname) in globs,locs
2761 else:
2836 else:
2762 execfile(fname,*where)
2837 execfile(fname,*where)
2763 except SyntaxError:
2838 except SyntaxError:
2764 self.showsyntaxerror()
2839 self.showsyntaxerror()
2765 warn('Failure executing file: <%s>' % fname)
2840 warn('Failure executing file: <%s>' % fname)
2766 except SystemExit,status:
2841 except SystemExit,status:
2767 # Code that correctly sets the exit status flag to success (0)
2842 # Code that correctly sets the exit status flag to success (0)
2768 # shouldn't be bothered with a traceback. Note that a plain
2843 # shouldn't be bothered with a traceback. Note that a plain
2769 # sys.exit() does NOT set the message to 0 (it's empty) so that
2844 # sys.exit() does NOT set the message to 0 (it's empty) so that
2770 # will still get a traceback. Note that the structure of the
2845 # will still get a traceback. Note that the structure of the
2771 # SystemExit exception changed between Python 2.4 and 2.5, so
2846 # SystemExit exception changed between Python 2.4 and 2.5, so
2772 # the checks must be done in a version-dependent way.
2847 # the checks must be done in a version-dependent way.
2773 show = False
2848 show = False
2774
2849
2775 if sys.version_info[:2] > (2,5):
2850 if sys.version_info[:2] > (2,5):
2776 if status.message!=0 and not kw['exit_ignore']:
2851 if status.message!=0 and not kw['exit_ignore']:
2777 show = True
2852 show = True
2778 else:
2853 else:
2779 if status.code and not kw['exit_ignore']:
2854 if status.code and not kw['exit_ignore']:
2780 show = True
2855 show = True
2781 if show:
2856 if show:
2782 self.showtraceback()
2857 self.showtraceback()
2783 warn('Failure executing file: <%s>' % fname)
2858 warn('Failure executing file: <%s>' % fname)
2784 except:
2859 except:
2785 self.showtraceback()
2860 self.showtraceback()
2786 warn('Failure executing file: <%s>' % fname)
2861 warn('Failure executing file: <%s>' % fname)
2787
2862
2788 syspath_cleanup()
2863 syspath_cleanup()
2789
2864
2790 #************************* end of file <iplib.py> *****************************
2865 #************************* end of file <iplib.py> *****************************
@@ -1,896 +1,951 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 # -*- test-case-name: IPython.kernel.test.test_multiengineclient -*-
2 # -*- test-case-name: IPython.kernel.test.test_multiengineclient -*-
3
3
4 """General Classes for IMultiEngine clients."""
4 """General Classes for IMultiEngine clients."""
5
5
6 __docformat__ = "restructuredtext en"
6 __docformat__ = "restructuredtext en"
7
7
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
9 # Copyright (C) 2008 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14
14
15 #-------------------------------------------------------------------------------
15 #-------------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-------------------------------------------------------------------------------
17 #-------------------------------------------------------------------------------
18
18
19 import sys
19 import sys
20 import cPickle as pickle
20 import cPickle as pickle
21 from types import FunctionType
21 from types import FunctionType
22 import linecache
22 import linecache
23
23
24 from twisted.internet import reactor
24 from twisted.internet import reactor
25 from twisted.python import components, log
25 from twisted.python import components, log
26 from twisted.python.failure import Failure
26 from twisted.python.failure import Failure
27 from zope.interface import Interface, implements, Attribute
27 from zope.interface import Interface, implements, Attribute
28
28
29 from IPython.ColorANSI import TermColors
29 from IPython.ColorANSI import TermColors
30
30
31 from IPython.kernel.twistedutil import blockingCallFromThread
31 from IPython.kernel.twistedutil import blockingCallFromThread
32 from IPython.kernel import error
32 from IPython.kernel import error
33 from IPython.kernel.parallelfunction import ParallelFunction
33 from IPython.kernel.parallelfunction import ParallelFunction
34 from IPython.kernel.mapper import (
34 from IPython.kernel.mapper import (
35 MultiEngineMapper,
35 MultiEngineMapper,
36 IMultiEngineMapperFactory,
36 IMultiEngineMapperFactory,
37 IMapper
37 IMapper
38 )
38 )
39 from IPython.kernel import map as Map
39 from IPython.kernel import map as Map
40 from IPython.kernel import multiengine as me
40 from IPython.kernel import multiengine as me
41 from IPython.kernel.multiengine import (IFullMultiEngine,
41 from IPython.kernel.multiengine import (IFullMultiEngine,
42 IFullSynchronousMultiEngine)
42 IFullSynchronousMultiEngine)
43
43
44
44
45 #-------------------------------------------------------------------------------
45 #-------------------------------------------------------------------------------
46 # Pending Result things
46 # Pending Result things
47 #-------------------------------------------------------------------------------
47 #-------------------------------------------------------------------------------
48
48
49 class IPendingResult(Interface):
49 class IPendingResult(Interface):
50 """A representation of a result that is pending.
50 """A representation of a result that is pending.
51
51
52 This class is similar to Twisted's `Deferred` object, but is designed to be
52 This class is similar to Twisted's `Deferred` object, but is designed to be
53 used in a synchronous context.
53 used in a synchronous context.
54 """
54 """
55
55
56 result_id=Attribute("ID of the deferred on the other side")
56 result_id=Attribute("ID of the deferred on the other side")
57 client=Attribute("A client that I came from")
57 client=Attribute("A client that I came from")
58 r=Attribute("An attribute that is a property that calls and returns get_result")
58 r=Attribute("An attribute that is a property that calls and returns get_result")
59
59
60 def get_result(default=None, block=True):
60 def get_result(default=None, block=True):
61 """
61 """
62 Get a result that is pending.
62 Get a result that is pending.
63
63
64 :Parameters:
64 :Parameters:
65 default
65 default
66 The value to return if the result is not ready.
66 The value to return if the result is not ready.
67 block : boolean
67 block : boolean
68 Should I block for the result.
68 Should I block for the result.
69
69
70 :Returns: The actual result or the default value.
70 :Returns: The actual result or the default value.
71 """
71 """
72
72
73 def add_callback(f, *args, **kwargs):
73 def add_callback(f, *args, **kwargs):
74 """
74 """
75 Add a callback that is called with the result.
75 Add a callback that is called with the result.
76
76
77 If the original result is foo, adding a callback will cause
77 If the original result is foo, adding a callback will cause
78 f(foo, *args, **kwargs) to be returned instead. If multiple
78 f(foo, *args, **kwargs) to be returned instead. If multiple
79 callbacks are registered, they are chained together: the result of
79 callbacks are registered, they are chained together: the result of
80 one is passed to the next and so on.
80 one is passed to the next and so on.
81
81
82 Unlike Twisted's Deferred object, there is no errback chain. Thus
82 Unlike Twisted's Deferred object, there is no errback chain. Thus
83 any exception raised will not be caught and handled. User must
83 any exception raised will not be caught and handled. User must
84 catch these by hand when calling `get_result`.
84 catch these by hand when calling `get_result`.
85 """
85 """
86
86
87
87
88 class PendingResult(object):
88 class PendingResult(object):
89 """A representation of a result that is not yet ready.
89 """A representation of a result that is not yet ready.
90
90
91 A user should not create a `PendingResult` instance by hand.
91 A user should not create a `PendingResult` instance by hand.
92
92
93 Methods
93 Methods
94 =======
94 =======
95
95
96 * `get_result`
96 * `get_result`
97 * `add_callback`
97 * `add_callback`
98
98
99 Properties
99 Properties
100 ==========
100 ==========
101 * `r`
101 * `r`
102 """
102 """
103
103
104 def __init__(self, client, result_id):
104 def __init__(self, client, result_id):
105 """Create a PendingResult with a result_id and a client instance.
105 """Create a PendingResult with a result_id and a client instance.
106
106
107 The client should implement `_getPendingResult(result_id, block)`.
107 The client should implement `_getPendingResult(result_id, block)`.
108 """
108 """
109 self.client = client
109 self.client = client
110 self.result_id = result_id
110 self.result_id = result_id
111 self.called = False
111 self.called = False
112 self.raised = False
112 self.raised = False
113 self.callbacks = []
113 self.callbacks = []
114
114
115 def get_result(self, default=None, block=True):
115 def get_result(self, default=None, block=True):
116 """Get a result that is pending.
116 """Get a result that is pending.
117
117
118 This method will connect to an IMultiEngine adapted controller
118 This method will connect to an IMultiEngine adapted controller
119 and see if the result is ready. If the action triggers an exception
119 and see if the result is ready. If the action triggers an exception
120 raise it and record it. This method records the result/exception once it is
120 raise it and record it. This method records the result/exception once it is
121 retrieved. Calling `get_result` again will get this cached result or will
121 retrieved. Calling `get_result` again will get this cached result or will
122 re-raise the exception. The .r attribute is a property that calls
122 re-raise the exception. The .r attribute is a property that calls
123 `get_result` with block=True.
123 `get_result` with block=True.
124
124
125 :Parameters:
125 :Parameters:
126 default
126 default
127 The value to return if the result is not ready.
127 The value to return if the result is not ready.
128 block : boolean
128 block : boolean
129 Should I block for the result.
129 Should I block for the result.
130
130
131 :Returns: The actual result or the default value.
131 :Returns: The actual result or the default value.
132 """
132 """
133
133
134 if self.called:
134 if self.called:
135 if self.raised:
135 if self.raised:
136 raise self.result[0], self.result[1], self.result[2]
136 raise self.result[0], self.result[1], self.result[2]
137 else:
137 else:
138 return self.result
138 return self.result
139 try:
139 try:
140 result = self.client.get_pending_deferred(self.result_id, block)
140 result = self.client.get_pending_deferred(self.result_id, block)
141 except error.ResultNotCompleted:
141 except error.ResultNotCompleted:
142 return default
142 return default
143 except:
143 except:
144 # Reraise other error, but first record them so they can be reraised
144 # Reraise other error, but first record them so they can be reraised
145 # later if .r or get_result is called again.
145 # later if .r or get_result is called again.
146 self.result = sys.exc_info()
146 self.result = sys.exc_info()
147 self.called = True
147 self.called = True
148 self.raised = True
148 self.raised = True
149 raise
149 raise
150 else:
150 else:
151 for cb in self.callbacks:
151 for cb in self.callbacks:
152 result = cb[0](result, *cb[1], **cb[2])
152 result = cb[0](result, *cb[1], **cb[2])
153 self.result = result
153 self.result = result
154 self.called = True
154 self.called = True
155 return result
155 return result
156
156
157 def add_callback(self, f, *args, **kwargs):
157 def add_callback(self, f, *args, **kwargs):
158 """Add a callback that is called with the result.
158 """Add a callback that is called with the result.
159
159
160 If the original result is result, adding a callback will cause
160 If the original result is result, adding a callback will cause
161 f(result, *args, **kwargs) to be returned instead. If multiple
161 f(result, *args, **kwargs) to be returned instead. If multiple
162 callbacks are registered, they are chained together: the result of
162 callbacks are registered, they are chained together: the result of
163 one is passed to the next and so on.
163 one is passed to the next and so on.
164
164
165 Unlike Twisted's Deferred object, there is no errback chain. Thus
165 Unlike Twisted's Deferred object, there is no errback chain. Thus
166 any exception raised will not be caught and handled. User must
166 any exception raised will not be caught and handled. User must
167 catch these by hand when calling `get_result`.
167 catch these by hand when calling `get_result`.
168 """
168 """
169 assert callable(f)
169 assert callable(f)
170 self.callbacks.append((f, args, kwargs))
170 self.callbacks.append((f, args, kwargs))
171
171
172 def __cmp__(self, other):
172 def __cmp__(self, other):
173 if self.result_id < other.result_id:
173 if self.result_id < other.result_id:
174 return -1
174 return -1
175 else:
175 else:
176 return 1
176 return 1
177
177
178 def _get_r(self):
178 def _get_r(self):
179 return self.get_result(block=True)
179 return self.get_result(block=True)
180
180
181 r = property(_get_r)
181 r = property(_get_r)
182 """This property is a shortcut to a `get_result(block=True)`."""
182 """This property is a shortcut to a `get_result(block=True)`."""
183
183
184
184
185 #-------------------------------------------------------------------------------
185 #-------------------------------------------------------------------------------
186 # Pretty printing wrappers for certain lists
186 # Pretty printing wrappers for certain lists
187 #-------------------------------------------------------------------------------
187 #-------------------------------------------------------------------------------
188
188
189 class ResultList(list):
189 class ResultList(list):
190 """A subclass of list that pretty prints the output of `execute`/`get_result`."""
190 """A subclass of list that pretty prints the output of `execute`/`get_result`."""
191
191
192 def __repr__(self):
192 def __repr__(self):
193 output = []
193 output = []
194 # These colored prompts were not working on Windows
194 # These colored prompts were not working on Windows
195 if sys.platform == 'win32':
195 if sys.platform == 'win32':
196 blue = normal = red = green = ''
196 blue = normal = red = green = ''
197 else:
197 else:
198 blue = TermColors.Blue
198 blue = TermColors.Blue
199 normal = TermColors.Normal
199 normal = TermColors.Normal
200 red = TermColors.Red
200 red = TermColors.Red
201 green = TermColors.Green
201 green = TermColors.Green
202 output.append("<Results List>\n")
202 output.append("<Results List>\n")
203 for cmd in self:
203 for cmd in self:
204 if isinstance(cmd, Failure):
204 if isinstance(cmd, Failure):
205 output.append(cmd)
205 output.append(cmd)
206 else:
206 else:
207 target = cmd.get('id',None)
207 target = cmd.get('id',None)
208 cmd_num = cmd.get('number',None)
208 cmd_num = cmd.get('number',None)
209 cmd_stdin = cmd.get('input',{}).get('translated','No Input')
209 cmd_stdin = cmd.get('input',{}).get('translated','No Input')
210 cmd_stdout = cmd.get('stdout', None)
210 cmd_stdout = cmd.get('stdout', None)
211 cmd_stderr = cmd.get('stderr', None)
211 cmd_stderr = cmd.get('stderr', None)
212 output.append("%s[%i]%s In [%i]:%s %s\n" % \
212 output.append("%s[%i]%s In [%i]:%s %s\n" % \
213 (green, target,
213 (green, target,
214 blue, cmd_num, normal, cmd_stdin))
214 blue, cmd_num, normal, cmd_stdin))
215 if cmd_stdout:
215 if cmd_stdout:
216 output.append("%s[%i]%s Out[%i]:%s %s\n" % \
216 output.append("%s[%i]%s Out[%i]:%s %s\n" % \
217 (green, target,
217 (green, target,
218 red, cmd_num, normal, cmd_stdout))
218 red, cmd_num, normal, cmd_stdout))
219 if cmd_stderr:
219 if cmd_stderr:
220 output.append("%s[%i]%s Err[%i]:\n%s %s" % \
220 output.append("%s[%i]%s Err[%i]:\n%s %s" % \
221 (green, target,
221 (green, target,
222 red, cmd_num, normal, cmd_stderr))
222 red, cmd_num, normal, cmd_stderr))
223 return ''.join(output)
223 return ''.join(output)
224
224
225
225
226 def wrapResultList(result):
226 def wrapResultList(result):
227 """A function that wraps the output of `execute`/`get_result` -> `ResultList`."""
227 """A function that wraps the output of `execute`/`get_result` -> `ResultList`."""
228 if len(result) == 0:
228 if len(result) == 0:
229 result = [result]
229 result = [result]
230 return ResultList(result)
230 return ResultList(result)
231
231
232
232
233 class QueueStatusList(list):
233 class QueueStatusList(list):
234 """A subclass of list that pretty prints the output of `queue_status`."""
234 """A subclass of list that pretty prints the output of `queue_status`."""
235
235
236 def __repr__(self):
236 def __repr__(self):
237 output = []
237 output = []
238 output.append("<Queue Status List>\n")
238 output.append("<Queue Status List>\n")
239 for e in self:
239 for e in self:
240 output.append("Engine: %s\n" % repr(e[0]))
240 output.append("Engine: %s\n" % repr(e[0]))
241 output.append(" Pending: %s\n" % repr(e[1]['pending']))
241 output.append(" Pending: %s\n" % repr(e[1]['pending']))
242 for q in e[1]['queue']:
242 for q in e[1]['queue']:
243 output.append(" Command: %s\n" % repr(q))
243 output.append(" Command: %s\n" % repr(q))
244 return ''.join(output)
244 return ''.join(output)
245
245
246
246
247 #-------------------------------------------------------------------------------
247 #-------------------------------------------------------------------------------
248 # InteractiveMultiEngineClient
248 # InteractiveMultiEngineClient
249 #-------------------------------------------------------------------------------
249 #-------------------------------------------------------------------------------
250
250
251 class InteractiveMultiEngineClient(object):
251 class InteractiveMultiEngineClient(object):
252 """A mixin class that add a few methods to a multiengine client.
252 """A mixin class that add a few methods to a multiengine client.
253
253
254 The methods in this mixin class are designed for interactive usage.
254 The methods in this mixin class are designed for interactive usage.
255 """
255 """
256
256
257 def activate(self):
257 def activate(self):
258 """Make this `MultiEngineClient` active for parallel magic commands.
258 """Make this `MultiEngineClient` active for parallel magic commands.
259
259
260 IPython has a magic command syntax to work with `MultiEngineClient` objects.
260 IPython has a magic command syntax to work with `MultiEngineClient` objects.
261 In a given IPython session there is a single active one. While
261 In a given IPython session there is a single active one. While
262 there can be many `MultiEngineClient` created and used by the user,
262 there can be many `MultiEngineClient` created and used by the user,
263 there is only one active one. The active `MultiEngineClient` is used whenever
263 there is only one active one. The active `MultiEngineClient` is used whenever
264 the magic commands %px and %autopx are used.
264 the magic commands %px and %autopx are used.
265
265
266 The activate() method is called on a given `MultiEngineClient` to make it
266 The activate() method is called on a given `MultiEngineClient` to make it
267 active. Once this has been done, the magic commands can be used.
267 active. Once this has been done, the magic commands can be used.
268 """
268 """
269
269
270 try:
270 try:
271 __IPYTHON__.activeController = self
271 __IPYTHON__.activeController = self
272 except NameError:
272 except NameError:
273 print "The IPython Controller magics only work within IPython."
273 print "The IPython Controller magics only work within IPython."
274
274
275 def __setitem__(self, key, value):
275 def __setitem__(self, key, value):
276 """Add a dictionary interface for pushing/pulling.
276 """Add a dictionary interface for pushing/pulling.
277
277
278 This functions as a shorthand for `push`.
278 This functions as a shorthand for `push`.
279
279
280 :Parameters:
280 :Parameters:
281 key : str
281 key : str
282 What to call the remote object.
282 What to call the remote object.
283 value : object
283 value : object
284 The local Python object to push.
284 The local Python object to push.
285 """
285 """
286 targets, block = self._findTargetsAndBlock()
286 targets, block = self._findTargetsAndBlock()
287 return self.push({key:value}, targets=targets, block=block)
287 return self.push({key:value}, targets=targets, block=block)
288
288
289 def __getitem__(self, key):
289 def __getitem__(self, key):
290 """Add a dictionary interface for pushing/pulling.
290 """Add a dictionary interface for pushing/pulling.
291
291
292 This functions as a shorthand to `pull`.
292 This functions as a shorthand to `pull`.
293
293
294 :Parameters:
294 :Parameters:
295 - `key`: A string representing the key.
295 - `key`: A string representing the key.
296 """
296 """
297 if isinstance(key, str):
297 if isinstance(key, str):
298 targets, block = self._findTargetsAndBlock()
298 targets, block = self._findTargetsAndBlock()
299 return self.pull(key, targets=targets, block=block)
299 return self.pull(key, targets=targets, block=block)
300 else:
300 else:
301 raise TypeError("__getitem__ only takes strs")
301 raise TypeError("__getitem__ only takes strs")
302
302
303 def __len__(self):
303 def __len__(self):
304 """Return the number of available engines."""
304 """Return the number of available engines."""
305 return len(self.get_ids())
305 return len(self.get_ids())
306
306
307 #---------------------------------------------------------------------------
307 #---------------------------------------------------------------------------
308 # Make this a context manager for with
308 # Make this a context manager for with
309 #---------------------------------------------------------------------------
309 #---------------------------------------------------------------------------
310
310
311 def findsource_file(self,f):
311 def findsource_file(self,f):
312 linecache.checkcache()
312 linecache.checkcache()
313 s = findsource(f.f_code)
313 s = findsource(f.f_code)
314 lnum = f.f_lineno
314 lnum = f.f_lineno
315 wsource = s[0][f.f_lineno:]
315 wsource = s[0][f.f_lineno:]
316 return strip_whitespace(wsource)
316 return strip_whitespace(wsource)
317
317
318 def findsource_ipython(self,f):
318 def findsource_ipython(self,f):
319 from IPython import ipapi
319 from IPython import ipapi
320 self.ip = ipapi.get()
320 self.ip = ipapi.get()
321 wsource = [l+'\n' for l in
321 wsource = [l+'\n' for l in
322 self.ip.IP.input_hist_raw[-1].splitlines()[1:]]
322 self.ip.IP.input_hist_raw[-1].splitlines()[1:]]
323 return strip_whitespace(wsource)
323 return strip_whitespace(wsource)
324
324
325 def __enter__(self):
325 def __enter__(self):
326 f = sys._getframe(1)
326 f = sys._getframe(1)
327 local_ns = f.f_locals
327 local_ns = f.f_locals
328 global_ns = f.f_globals
328 global_ns = f.f_globals
329 if f.f_code.co_filename == '<ipython console>':
329 if f.f_code.co_filename == '<ipython console>':
330 s = self.findsource_ipython(f)
330 s = self.findsource_ipython(f)
331 else:
331 else:
332 s = self.findsource_file(f)
332 s = self.findsource_file(f)
333
333
334 self._with_context_result = self.execute(s)
334 self._with_context_result = self.execute(s)
335
335
336 def __exit__ (self, etype, value, tb):
336 def __exit__ (self, etype, value, tb):
337 if issubclass(etype,error.StopLocalExecution):
337 if issubclass(etype,error.StopLocalExecution):
338 return True
338 return True
339
339
340
340
341 def remote():
341 def remote():
342 m = 'Special exception to stop local execution of parallel code.'
342 m = 'Special exception to stop local execution of parallel code.'
343 raise error.StopLocalExecution(m)
343 raise error.StopLocalExecution(m)
344
344
345 def strip_whitespace(source):
345 def strip_whitespace(source):
346 # Expand tabs to avoid any confusion.
346 # Expand tabs to avoid any confusion.
347 wsource = [l.expandtabs(4) for l in source]
347 wsource = [l.expandtabs(4) for l in source]
348 # Detect the indentation level
348 # Detect the indentation level
349 done = False
349 done = False
350 for line in wsource:
350 for line in wsource:
351 if line.isspace():
351 if line.isspace():
352 continue
352 continue
353 for col,char in enumerate(line):
353 for col,char in enumerate(line):
354 if char != ' ':
354 if char != ' ':
355 done = True
355 done = True
356 break
356 break
357 if done:
357 if done:
358 break
358 break
359 # Now we know how much leading space there is in the code. Next, we
359 # Now we know how much leading space there is in the code. Next, we
360 # extract up to the first line that has less indentation.
360 # extract up to the first line that has less indentation.
361 # WARNINGS: we skip comments that may be misindented, but we do NOT yet
361 # WARNINGS: we skip comments that may be misindented, but we do NOT yet
362 # detect triple quoted strings that may have flush left text.
362 # detect triple quoted strings that may have flush left text.
363 for lno,line in enumerate(wsource):
363 for lno,line in enumerate(wsource):
364 lead = line[:col]
364 lead = line[:col]
365 if lead.isspace():
365 if lead.isspace():
366 continue
366 continue
367 else:
367 else:
368 if not lead.lstrip().startswith('#'):
368 if not lead.lstrip().startswith('#'):
369 break
369 break
370 # The real 'with' source is up to lno
370 # The real 'with' source is up to lno
371 src_lines = [l[col:] for l in wsource[:lno+1]]
371 src_lines = [l[col:] for l in wsource[:lno+1]]
372
372
373 # Finally, check that the source's first non-comment line begins with the
373 # Finally, check that the source's first non-comment line begins with the
374 # special call 'remote()'
374 # special call 'remote()'
375 for nline,line in enumerate(src_lines):
375 for nline,line in enumerate(src_lines):
376 if line.isspace() or line.startswith('#'):
376 if line.isspace() or line.startswith('#'):
377 continue
377 continue
378 if 'remote()' in line:
378 if 'remote()' in line:
379 break
379 break
380 else:
380 else:
381 raise ValueError('remote() call missing at the start of code')
381 raise ValueError('remote() call missing at the start of code')
382 src = ''.join(src_lines[nline+1:])
382 src = ''.join(src_lines[nline+1:])
383 #print 'SRC:\n<<<<<<<>>>>>>>\n%s<<<<<>>>>>>' % src # dbg
383 #print 'SRC:\n<<<<<<<>>>>>>>\n%s<<<<<>>>>>>' % src # dbg
384 return src
384 return src
385
385
386
386
387 #-------------------------------------------------------------------------------
387 #-------------------------------------------------------------------------------
388 # The top-level MultiEngine client adaptor
388 # The top-level MultiEngine client adaptor
389 #-------------------------------------------------------------------------------
389 #-------------------------------------------------------------------------------
390
390
391
391
392 class IFullBlockingMultiEngineClient(Interface):
392 class IFullBlockingMultiEngineClient(Interface):
393 pass
393 pass
394
394
395
395
396 class FullBlockingMultiEngineClient(InteractiveMultiEngineClient):
396 class FullBlockingMultiEngineClient(InteractiveMultiEngineClient):
397 """
397 """
398 A blocking client to the `IMultiEngine` controller interface.
398 A blocking client to the `IMultiEngine` controller interface.
399
399
400 This class allows users to use a set of engines for a parallel
400 This class allows users to use a set of engines for a parallel
401 computation through the `IMultiEngine` interface. In this interface,
401 computation through the `IMultiEngine` interface. In this interface,
402 each engine has a specific id (an int) that is used to refer to the
402 each engine has a specific id (an int) that is used to refer to the
403 engine, run code on it, etc.
403 engine, run code on it, etc.
404 """
404 """
405
405
406 implements(
406 implements(
407 IFullBlockingMultiEngineClient,
407 IFullBlockingMultiEngineClient,
408 IMultiEngineMapperFactory,
408 IMultiEngineMapperFactory,
409 IMapper
409 IMapper
410 )
410 )
411
411
412 def __init__(self, smultiengine):
412 def __init__(self, smultiengine):
413 self.smultiengine = smultiengine
413 self.smultiengine = smultiengine
414 self.block = True
414 self.block = True
415 self.targets = 'all'
415 self.targets = 'all'
416
416
417 def _findBlock(self, block=None):
417 def _findBlock(self, block=None):
418 if block is None:
418 if block is None:
419 return self.block
419 return self.block
420 else:
420 else:
421 if block in (True, False):
421 if block in (True, False):
422 return block
422 return block
423 else:
423 else:
424 raise ValueError("block must be True or False")
424 raise ValueError("block must be True or False")
425
425
426 def _findTargets(self, targets=None):
426 def _findTargets(self, targets=None):
427 if targets is None:
427 if targets is None:
428 return self.targets
428 return self.targets
429 else:
429 else:
430 if not isinstance(targets, (str,list,tuple,int)):
430 if not isinstance(targets, (str,list,tuple,int)):
431 raise ValueError("targets must be a str, list, tuple or int")
431 raise ValueError("targets must be a str, list, tuple or int")
432 return targets
432 return targets
433
433
434 def _findTargetsAndBlock(self, targets=None, block=None):
434 def _findTargetsAndBlock(self, targets=None, block=None):
435 return self._findTargets(targets), self._findBlock(block)
435 return self._findTargets(targets), self._findBlock(block)
436
436
437 def _blockFromThread(self, function, *args, **kwargs):
437 def _blockFromThread(self, function, *args, **kwargs):
438 block = kwargs.get('block', None)
438 block = kwargs.get('block', None)
439 if block is None:
439 if block is None:
440 raise error.MissingBlockArgument("'block' keyword argument is missing")
440 raise error.MissingBlockArgument("'block' keyword argument is missing")
441 result = blockingCallFromThread(function, *args, **kwargs)
441 result = blockingCallFromThread(function, *args, **kwargs)
442 if not block:
442 if not block:
443 result = PendingResult(self, result)
443 result = PendingResult(self, result)
444 return result
444 return result
445
445
446 def get_pending_deferred(self, deferredID, block):
446 def get_pending_deferred(self, deferredID, block):
447 return blockingCallFromThread(self.smultiengine.get_pending_deferred, deferredID, block)
447 return blockingCallFromThread(self.smultiengine.get_pending_deferred, deferredID, block)
448
448
449 def barrier(self, pendingResults):
449 def barrier(self, pendingResults):
450 """Synchronize a set of `PendingResults`.
450 """Synchronize a set of `PendingResults`.
451
451
452 This method is a synchronization primitive that waits for a set of
452 This method is a synchronization primitive that waits for a set of
453 `PendingResult` objects to complete. More specifically, barier does
453 `PendingResult` objects to complete. More specifically, barier does
454 the following.
454 the following.
455
455
456 * The `PendingResult`s are sorted by result_id.
456 * The `PendingResult`s are sorted by result_id.
457 * The `get_result` method is called for each `PendingResult` sequentially
457 * The `get_result` method is called for each `PendingResult` sequentially
458 with block=True.
458 with block=True.
459 * If a `PendingResult` gets a result that is an exception, it is
459 * If a `PendingResult` gets a result that is an exception, it is
460 trapped and can be re-raised later by calling `get_result` again.
460 trapped and can be re-raised later by calling `get_result` again.
461 * The `PendingResult`s are flushed from the controller.
461 * The `PendingResult`s are flushed from the controller.
462
462
463 After barrier has been called on a `PendingResult`, its results can
463 After barrier has been called on a `PendingResult`, its results can
464 be retrieved by calling `get_result` again or accesing the `r` attribute
464 be retrieved by calling `get_result` again or accesing the `r` attribute
465 of the instance.
465 of the instance.
466 """
466 """
467
467
468 # Convert to list for sorting and check class type
468 # Convert to list for sorting and check class type
469 prList = list(pendingResults)
469 prList = list(pendingResults)
470 for pr in prList:
470 for pr in prList:
471 if not isinstance(pr, PendingResult):
471 if not isinstance(pr, PendingResult):
472 raise error.NotAPendingResult("Objects passed to barrier must be PendingResult instances")
472 raise error.NotAPendingResult("Objects passed to barrier must be PendingResult instances")
473
473
474 # Sort the PendingResults so they are in order
474 # Sort the PendingResults so they are in order
475 prList.sort()
475 prList.sort()
476 # Block on each PendingResult object
476 # Block on each PendingResult object
477 for pr in prList:
477 for pr in prList:
478 try:
478 try:
479 result = pr.get_result(block=True)
479 result = pr.get_result(block=True)
480 except Exception:
480 except Exception:
481 pass
481 pass
482
482
483 def flush(self):
483 def flush(self):
484 """
484 """
485 Clear all pending deferreds/results from the controller.
485 Clear all pending deferreds/results from the controller.
486
486
487 For each `PendingResult` that is created by this client, the controller
487 For each `PendingResult` that is created by this client, the controller
488 holds on to the result for that `PendingResult`. This can be a problem
488 holds on to the result for that `PendingResult`. This can be a problem
489 if there are a large number of `PendingResult` objects that are created.
489 if there are a large number of `PendingResult` objects that are created.
490
490
491 Once the result of the `PendingResult` has been retrieved, the result
491 Once the result of the `PendingResult` has been retrieved, the result
492 is removed from the controller, but if a user doesn't get a result (
492 is removed from the controller, but if a user doesn't get a result (
493 they just ignore the `PendingResult`) the result is kept forever on the
493 they just ignore the `PendingResult`) the result is kept forever on the
494 controller. This method allows the user to clear out all un-retrieved
494 controller. This method allows the user to clear out all un-retrieved
495 results on the controller.
495 results on the controller.
496 """
496 """
497 r = blockingCallFromThread(self.smultiengine.clear_pending_deferreds)
497 r = blockingCallFromThread(self.smultiengine.clear_pending_deferreds)
498 return r
498 return r
499
499
500 clear_pending_results = flush
500 clear_pending_results = flush
501
501
502 #---------------------------------------------------------------------------
502 #---------------------------------------------------------------------------
503 # IEngineMultiplexer related methods
503 # IEngineMultiplexer related methods
504 #---------------------------------------------------------------------------
504 #---------------------------------------------------------------------------
505
505
506 def execute(self, lines, targets=None, block=None):
506 def execute(self, lines, targets=None, block=None):
507 """
507 """
508 Execute code on a set of engines.
508 Execute code on a set of engines.
509
509
510 :Parameters:
510 :Parameters:
511 lines : str
511 lines : str
512 The Python code to execute as a string
512 The Python code to execute as a string
513 targets : id or list of ids
513 targets : id or list of ids
514 The engine to use for the execution
514 The engine to use for the execution
515 block : boolean
515 block : boolean
516 If False, this method will return the actual result. If False,
516 If False, this method will return the actual result. If False,
517 a `PendingResult` is returned which can be used to get the result
517 a `PendingResult` is returned which can be used to get the result
518 at a later time.
518 at a later time.
519 """
519 """
520 targets, block = self._findTargetsAndBlock(targets, block)
520 targets, block = self._findTargetsAndBlock(targets, block)
521 result = blockingCallFromThread(self.smultiengine.execute, lines,
521 result = blockingCallFromThread(self.smultiengine.execute, lines,
522 targets=targets, block=block)
522 targets=targets, block=block)
523 if block:
523 if block:
524 result = ResultList(result)
524 result = ResultList(result)
525 else:
525 else:
526 result = PendingResult(self, result)
526 result = PendingResult(self, result)
527 result.add_callback(wrapResultList)
527 result.add_callback(wrapResultList)
528 return result
528 return result
529
529
530 def push(self, namespace, targets=None, block=None):
530 def push(self, namespace, targets=None, block=None):
531 """
531 """
532 Push a dictionary of keys and values to engines namespace.
532 Push a dictionary of keys and values to engines namespace.
533
533
534 Each engine has a persistent namespace. This method is used to push
534 Each engine has a persistent namespace. This method is used to push
535 Python objects into that namespace.
535 Python objects into that namespace.
536
536
537 The objects in the namespace must be pickleable.
537 The objects in the namespace must be pickleable.
538
538
539 :Parameters:
539 :Parameters:
540 namespace : dict
540 namespace : dict
541 A dict that contains Python objects to be injected into
541 A dict that contains Python objects to be injected into
542 the engine persistent namespace.
542 the engine persistent namespace.
543 targets : id or list of ids
543 targets : id or list of ids
544 The engine to use for the execution
544 The engine to use for the execution
545 block : boolean
545 block : boolean
546 If False, this method will return the actual result. If False,
546 If False, this method will return the actual result. If False,
547 a `PendingResult` is returned which can be used to get the result
547 a `PendingResult` is returned which can be used to get the result
548 at a later time.
548 at a later time.
549 """
549 """
550 targets, block = self._findTargetsAndBlock(targets, block)
550 targets, block = self._findTargetsAndBlock(targets, block)
551 return self._blockFromThread(self.smultiengine.push, namespace,
551 return self._blockFromThread(self.smultiengine.push, namespace,
552 targets=targets, block=block)
552 targets=targets, block=block)
553
553
554 def pull(self, keys, targets=None, block=None):
554 def pull(self, keys, targets=None, block=None):
555 """
555 """
556 Pull Python objects by key out of engines namespaces.
556 Pull Python objects by key out of engines namespaces.
557
557
558 :Parameters:
558 :Parameters:
559 keys : str or list of str
559 keys : str or list of str
560 The names of the variables to be pulled
560 The names of the variables to be pulled
561 targets : id or list of ids
561 targets : id or list of ids
562 The engine to use for the execution
562 The engine to use for the execution
563 block : boolean
563 block : boolean
564 If False, this method will return the actual result. If False,
564 If False, this method will return the actual result. If False,
565 a `PendingResult` is returned which can be used to get the result
565 a `PendingResult` is returned which can be used to get the result
566 at a later time.
566 at a later time.
567 """
567 """
568 targets, block = self._findTargetsAndBlock(targets, block)
568 targets, block = self._findTargetsAndBlock(targets, block)
569 return self._blockFromThread(self.smultiengine.pull, keys, targets=targets, block=block)
569 return self._blockFromThread(self.smultiengine.pull, keys, targets=targets, block=block)
570
570
571 def push_function(self, namespace, targets=None, block=None):
571 def push_function(self, namespace, targets=None, block=None):
572 """
572 """
573 Push a Python function to an engine.
573 Push a Python function to an engine.
574
574
575 This method is used to push a Python function to an engine. This
575 This method is used to push a Python function to an engine. This
576 method can then be used in code on the engines. Closures are not supported.
576 method can then be used in code on the engines. Closures are not supported.
577
577
578 :Parameters:
578 :Parameters:
579 namespace : dict
579 namespace : dict
580 A dict whose values are the functions to be pushed. The keys give
580 A dict whose values are the functions to be pushed. The keys give
581 that names that the function will appear as in the engines
581 that names that the function will appear as in the engines
582 namespace.
582 namespace.
583 targets : id or list of ids
583 targets : id or list of ids
584 The engine to use for the execution
584 The engine to use for the execution
585 block : boolean
585 block : boolean
586 If False, this method will return the actual result. If False,
586 If False, this method will return the actual result. If False,
587 a `PendingResult` is returned which can be used to get the result
587 a `PendingResult` is returned which can be used to get the result
588 at a later time.
588 at a later time.
589 """
589 """
590 targets, block = self._findTargetsAndBlock(targets, block)
590 targets, block = self._findTargetsAndBlock(targets, block)
591 return self._blockFromThread(self.smultiengine.push_function, namespace, targets=targets, block=block)
591 return self._blockFromThread(self.smultiengine.push_function, namespace, targets=targets, block=block)
592
592
593 def pull_function(self, keys, targets=None, block=None):
593 def pull_function(self, keys, targets=None, block=None):
594 """
594 """
595 Pull a Python function from an engine.
595 Pull a Python function from an engine.
596
596
597 This method is used to pull a Python function from an engine.
597 This method is used to pull a Python function from an engine.
598 Closures are not supported.
598 Closures are not supported.
599
599
600 :Parameters:
600 :Parameters:
601 keys : str or list of str
601 keys : str or list of str
602 The names of the functions to be pulled
602 The names of the functions to be pulled
603 targets : id or list of ids
603 targets : id or list of ids
604 The engine to use for the execution
604 The engine to use for the execution
605 block : boolean
605 block : boolean
606 If False, this method will return the actual result. If False,
606 If False, this method will return the actual result. If False,
607 a `PendingResult` is returned which can be used to get the result
607 a `PendingResult` is returned which can be used to get the result
608 at a later time.
608 at a later time.
609 """
609 """
610 targets, block = self._findTargetsAndBlock(targets, block)
610 targets, block = self._findTargetsAndBlock(targets, block)
611 return self._blockFromThread(self.smultiengine.pull_function, keys, targets=targets, block=block)
611 return self._blockFromThread(self.smultiengine.pull_function, keys, targets=targets, block=block)
612
612
613 def push_serialized(self, namespace, targets=None, block=None):
613 def push_serialized(self, namespace, targets=None, block=None):
614 targets, block = self._findTargetsAndBlock(targets, block)
614 targets, block = self._findTargetsAndBlock(targets, block)
615 return self._blockFromThread(self.smultiengine.push_serialized, namespace, targets=targets, block=block)
615 return self._blockFromThread(self.smultiengine.push_serialized, namespace, targets=targets, block=block)
616
616
617 def pull_serialized(self, keys, targets=None, block=None):
617 def pull_serialized(self, keys, targets=None, block=None):
618 targets, block = self._findTargetsAndBlock(targets, block)
618 targets, block = self._findTargetsAndBlock(targets, block)
619 return self._blockFromThread(self.smultiengine.pull_serialized, keys, targets=targets, block=block)
619 return self._blockFromThread(self.smultiengine.pull_serialized, keys, targets=targets, block=block)
620
620
621 def get_result(self, i=None, targets=None, block=None):
621 def get_result(self, i=None, targets=None, block=None):
622 """
622 """
623 Get a previous result.
623 Get a previous result.
624
624
625 When code is executed in an engine, a dict is created and returned. This
625 When code is executed in an engine, a dict is created and returned. This
626 method retrieves that dict for previous commands.
626 method retrieves that dict for previous commands.
627
627
628 :Parameters:
628 :Parameters:
629 i : int
629 i : int
630 The number of the result to get
630 The number of the result to get
631 targets : id or list of ids
631 targets : id or list of ids
632 The engine to use for the execution
632 The engine to use for the execution
633 block : boolean
633 block : boolean
634 If False, this method will return the actual result. If False,
634 If False, this method will return the actual result. If False,
635 a `PendingResult` is returned which can be used to get the result
635 a `PendingResult` is returned which can be used to get the result
636 at a later time.
636 at a later time.
637 """
637 """
638 targets, block = self._findTargetsAndBlock(targets, block)
638 targets, block = self._findTargetsAndBlock(targets, block)
639 result = blockingCallFromThread(self.smultiengine.get_result, i, targets=targets, block=block)
639 result = blockingCallFromThread(self.smultiengine.get_result, i, targets=targets, block=block)
640 if block:
640 if block:
641 result = ResultList(result)
641 result = ResultList(result)
642 else:
642 else:
643 result = PendingResult(self, result)
643 result = PendingResult(self, result)
644 result.add_callback(wrapResultList)
644 result.add_callback(wrapResultList)
645 return result
645 return result
646
646
647 def reset(self, targets=None, block=None):
647 def reset(self, targets=None, block=None):
648 """
648 """
649 Reset an engine.
649 Reset an engine.
650
650
651 This method clears out the namespace of an engine.
651 This method clears out the namespace of an engine.
652
652
653 :Parameters:
653 :Parameters:
654 targets : id or list of ids
654 targets : id or list of ids
655 The engine to use for the execution
655 The engine to use for the execution
656 block : boolean
656 block : boolean
657 If False, this method will return the actual result. If False,
657 If False, this method will return the actual result. If False,
658 a `PendingResult` is returned which can be used to get the result
658 a `PendingResult` is returned which can be used to get the result
659 at a later time.
659 at a later time.
660 """
660 """
661 targets, block = self._findTargetsAndBlock(targets, block)
661 targets, block = self._findTargetsAndBlock(targets, block)
662 return self._blockFromThread(self.smultiengine.reset, targets=targets, block=block)
662 return self._blockFromThread(self.smultiengine.reset, targets=targets, block=block)
663
663
664 def keys(self, targets=None, block=None):
664 def keys(self, targets=None, block=None):
665 """
665 """
666 Get a list of all the variables in an engine's namespace.
666 Get a list of all the variables in an engine's namespace.
667
667
668 :Parameters:
668 :Parameters:
669 targets : id or list of ids
669 targets : id or list of ids
670 The engine to use for the execution
670 The engine to use for the execution
671 block : boolean
671 block : boolean
672 If False, this method will return the actual result. If False,
672 If False, this method will return the actual result. If False,
673 a `PendingResult` is returned which can be used to get the result
673 a `PendingResult` is returned which can be used to get the result
674 at a later time.
674 at a later time.
675 """
675 """
676 targets, block = self._findTargetsAndBlock(targets, block)
676 targets, block = self._findTargetsAndBlock(targets, block)
677 return self._blockFromThread(self.smultiengine.keys, targets=targets, block=block)
677 return self._blockFromThread(self.smultiengine.keys, targets=targets, block=block)
678
678
679 def kill(self, controller=False, targets=None, block=None):
679 def kill(self, controller=False, targets=None, block=None):
680 """
680 """
681 Kill the engines and controller.
681 Kill the engines and controller.
682
682
683 This method is used to stop the engine and controller by calling
683 This method is used to stop the engine and controller by calling
684 `reactor.stop`.
684 `reactor.stop`.
685
685
686 :Parameters:
686 :Parameters:
687 controller : boolean
687 controller : boolean
688 If True, kill the engines and controller. If False, just the
688 If True, kill the engines and controller. If False, just the
689 engines
689 engines
690 targets : id or list of ids
690 targets : id or list of ids
691 The engine to use for the execution
691 The engine to use for the execution
692 block : boolean
692 block : boolean
693 If False, this method will return the actual result. If False,
693 If False, this method will return the actual result. If False,
694 a `PendingResult` is returned which can be used to get the result
694 a `PendingResult` is returned which can be used to get the result
695 at a later time.
695 at a later time.
696 """
696 """
697 targets, block = self._findTargetsAndBlock(targets, block)
697 targets, block = self._findTargetsAndBlock(targets, block)
698 return self._blockFromThread(self.smultiengine.kill, controller, targets=targets, block=block)
698 return self._blockFromThread(self.smultiengine.kill, controller, targets=targets, block=block)
699
699
700 def clear_queue(self, targets=None, block=None):
700 def clear_queue(self, targets=None, block=None):
701 """
701 """
702 Clear out the controller's queue for an engine.
702 Clear out the controller's queue for an engine.
703
703
704 The controller maintains a queue for each engine. This clear it out.
704 The controller maintains a queue for each engine. This clear it out.
705
705
706 :Parameters:
706 :Parameters:
707 targets : id or list of ids
707 targets : id or list of ids
708 The engine to use for the execution
708 The engine to use for the execution
709 block : boolean
709 block : boolean
710 If False, this method will return the actual result. If False,
710 If False, this method will return the actual result. If False,
711 a `PendingResult` is returned which can be used to get the result
711 a `PendingResult` is returned which can be used to get the result
712 at a later time.
712 at a later time.
713 """
713 """
714 targets, block = self._findTargetsAndBlock(targets, block)
714 targets, block = self._findTargetsAndBlock(targets, block)
715 return self._blockFromThread(self.smultiengine.clear_queue, targets=targets, block=block)
715 return self._blockFromThread(self.smultiengine.clear_queue, targets=targets, block=block)
716
716
717 def queue_status(self, targets=None, block=None):
717 def queue_status(self, targets=None, block=None):
718 """
718 """
719 Get the status of an engines queue.
719 Get the status of an engines queue.
720
720
721 :Parameters:
721 :Parameters:
722 targets : id or list of ids
722 targets : id or list of ids
723 The engine to use for the execution
723 The engine to use for the execution
724 block : boolean
724 block : boolean
725 If False, this method will return the actual result. If False,
725 If False, this method will return the actual result. If False,
726 a `PendingResult` is returned which can be used to get the result
726 a `PendingResult` is returned which can be used to get the result
727 at a later time.
727 at a later time.
728 """
728 """
729 targets, block = self._findTargetsAndBlock(targets, block)
729 targets, block = self._findTargetsAndBlock(targets, block)
730 return self._blockFromThread(self.smultiengine.queue_status, targets=targets, block=block)
730 return self._blockFromThread(self.smultiengine.queue_status, targets=targets, block=block)
731
731
732 def set_properties(self, properties, targets=None, block=None):
732 def set_properties(self, properties, targets=None, block=None):
733 targets, block = self._findTargetsAndBlock(targets, block)
733 targets, block = self._findTargetsAndBlock(targets, block)
734 return self._blockFromThread(self.smultiengine.set_properties, properties, targets=targets, block=block)
734 return self._blockFromThread(self.smultiengine.set_properties, properties, targets=targets, block=block)
735
735
736 def get_properties(self, keys=None, targets=None, block=None):
736 def get_properties(self, keys=None, targets=None, block=None):
737 targets, block = self._findTargetsAndBlock(targets, block)
737 targets, block = self._findTargetsAndBlock(targets, block)
738 return self._blockFromThread(self.smultiengine.get_properties, keys, targets=targets, block=block)
738 return self._blockFromThread(self.smultiengine.get_properties, keys, targets=targets, block=block)
739
739
740 def has_properties(self, keys, targets=None, block=None):
740 def has_properties(self, keys, targets=None, block=None):
741 targets, block = self._findTargetsAndBlock(targets, block)
741 targets, block = self._findTargetsAndBlock(targets, block)
742 return self._blockFromThread(self.smultiengine.has_properties, keys, targets=targets, block=block)
742 return self._blockFromThread(self.smultiengine.has_properties, keys, targets=targets, block=block)
743
743
744 def del_properties(self, keys, targets=None, block=None):
744 def del_properties(self, keys, targets=None, block=None):
745 targets, block = self._findTargetsAndBlock(targets, block)
745 targets, block = self._findTargetsAndBlock(targets, block)
746 return self._blockFromThread(self.smultiengine.del_properties, keys, targets=targets, block=block)
746 return self._blockFromThread(self.smultiengine.del_properties, keys, targets=targets, block=block)
747
747
748 def clear_properties(self, targets=None, block=None):
748 def clear_properties(self, targets=None, block=None):
749 targets, block = self._findTargetsAndBlock(targets, block)
749 targets, block = self._findTargetsAndBlock(targets, block)
750 return self._blockFromThread(self.smultiengine.clear_properties, targets=targets, block=block)
750 return self._blockFromThread(self.smultiengine.clear_properties, targets=targets, block=block)
751
751
752 #---------------------------------------------------------------------------
752 #---------------------------------------------------------------------------
753 # IMultiEngine related methods
753 # IMultiEngine related methods
754 #---------------------------------------------------------------------------
754 #---------------------------------------------------------------------------
755
755
756 def get_ids(self):
756 def get_ids(self):
757 """
757 """
758 Returns the ids of currently registered engines.
758 Returns the ids of currently registered engines.
759 """
759 """
760 result = blockingCallFromThread(self.smultiengine.get_ids)
760 result = blockingCallFromThread(self.smultiengine.get_ids)
761 return result
761 return result
762
762
763 #---------------------------------------------------------------------------
763 #---------------------------------------------------------------------------
764 # IMultiEngineCoordinator
764 # IMultiEngineCoordinator
765 #---------------------------------------------------------------------------
765 #---------------------------------------------------------------------------
766
766
767 def scatter(self, key, seq, dist='b', flatten=False, targets=None, block=None):
767 def scatter(self, key, seq, dist='b', flatten=False, targets=None, block=None):
768 """
768 """
769 Partition a Python sequence and send the partitions to a set of engines.
769 Partition a Python sequence and send the partitions to a set of engines.
770 """
770 """
771 targets, block = self._findTargetsAndBlock(targets, block)
771 targets, block = self._findTargetsAndBlock(targets, block)
772 return self._blockFromThread(self.smultiengine.scatter, key, seq,
772 return self._blockFromThread(self.smultiengine.scatter, key, seq,
773 dist, flatten, targets=targets, block=block)
773 dist, flatten, targets=targets, block=block)
774
774
775 def gather(self, key, dist='b', targets=None, block=None):
775 def gather(self, key, dist='b', targets=None, block=None):
776 """
776 """
777 Gather a partitioned sequence on a set of engines as a single local seq.
777 Gather a partitioned sequence on a set of engines as a single local seq.
778 """
778 """
779 targets, block = self._findTargetsAndBlock(targets, block)
779 targets, block = self._findTargetsAndBlock(targets, block)
780 return self._blockFromThread(self.smultiengine.gather, key, dist,
780 return self._blockFromThread(self.smultiengine.gather, key, dist,
781 targets=targets, block=block)
781 targets=targets, block=block)
782
782
783 def raw_map(self, func, seq, dist='b', targets=None, block=None):
783 def raw_map(self, func, seq, dist='b', targets=None, block=None):
784 """
784 """
785 A parallelized version of Python's builtin map.
785 A parallelized version of Python's builtin map.
786
786
787 This has a slightly different syntax than the builtin `map`.
787 This has a slightly different syntax than the builtin `map`.
788 This is needed because we need to have keyword arguments and thus
788 This is needed because we need to have keyword arguments and thus
789 can't use *args to capture all the sequences. Instead, they must
789 can't use *args to capture all the sequences. Instead, they must
790 be passed in a list or tuple.
790 be passed in a list or tuple.
791
791
792 raw_map(func, seqs) -> map(func, seqs[0], seqs[1], ...)
792 raw_map(func, seqs) -> map(func, seqs[0], seqs[1], ...)
793
793
794 Most users will want to use parallel functions or the `mapper`
794 Most users will want to use parallel functions or the `mapper`
795 and `map` methods for an API that follows that of the builtin
795 and `map` methods for an API that follows that of the builtin
796 `map`.
796 `map`.
797 """
797 """
798 targets, block = self._findTargetsAndBlock(targets, block)
798 targets, block = self._findTargetsAndBlock(targets, block)
799 return self._blockFromThread(self.smultiengine.raw_map, func, seq,
799 return self._blockFromThread(self.smultiengine.raw_map, func, seq,
800 dist, targets=targets, block=block)
800 dist, targets=targets, block=block)
801
801
802 def map(self, func, *sequences):
802 def map(self, func, *sequences):
803 """
803 """
804 A parallel version of Python's builtin `map` function.
804 A parallel version of Python's builtin `map` function.
805
805
806 This method applies a function to sequences of arguments. It
806 This method applies a function to sequences of arguments. It
807 follows the same syntax as the builtin `map`.
807 follows the same syntax as the builtin `map`.
808
808
809 This method creates a mapper objects by calling `self.mapper` with
809 This method creates a mapper objects by calling `self.mapper` with
810 no arguments and then uses that mapper to do the mapping. See
810 no arguments and then uses that mapper to do the mapping. See
811 the documentation of `mapper` for more details.
811 the documentation of `mapper` for more details.
812 """
812 """
813 return self.mapper().map(func, *sequences)
813 return self.mapper().map(func, *sequences)
814
814
815 def mapper(self, dist='b', targets='all', block=None):
815 def mapper(self, dist='b', targets='all', block=None):
816 """
816 """
817 Create a mapper object that has a `map` method.
817 Create a mapper object that has a `map` method.
818
818
819 This method returns an object that implements the `IMapper`
819 This method returns an object that implements the `IMapper`
820 interface. This method is a factory that is used to control how
820 interface. This method is a factory that is used to control how
821 the map happens.
821 the map happens.
822
822
823 :Parameters:
823 :Parameters:
824 dist : str
824 dist : str
825 What decomposition to use, 'b' is the only one supported
825 What decomposition to use, 'b' is the only one supported
826 currently
826 currently
827 targets : str, int, sequence of ints
827 targets : str, int, sequence of ints
828 Which engines to use for the map
828 Which engines to use for the map
829 block : boolean
829 block : boolean
830 Should calls to `map` block or not
830 Should calls to `map` block or not
831 """
831 """
832 return MultiEngineMapper(self, dist, targets, block)
832 return MultiEngineMapper(self, dist, targets, block)
833
833
834 def parallel(self, dist='b', targets=None, block=None):
834 def parallel(self, dist='b', targets=None, block=None):
835 """
835 """
836 A decorator that turns a function into a parallel function.
836 A decorator that turns a function into a parallel function.
837
837
838 This can be used as:
838 This can be used as:
839
839
840 @parallel()
840 @parallel()
841 def f(x, y)
841 def f(x, y)
842 ...
842 ...
843
843
844 f(range(10), range(10))
844 f(range(10), range(10))
845
845
846 This causes f(0,0), f(1,1), ... to be called in parallel.
846 This causes f(0,0), f(1,1), ... to be called in parallel.
847
847
848 :Parameters:
848 :Parameters:
849 dist : str
849 dist : str
850 What decomposition to use, 'b' is the only one supported
850 What decomposition to use, 'b' is the only one supported
851 currently
851 currently
852 targets : str, int, sequence of ints
852 targets : str, int, sequence of ints
853 Which engines to use for the map
853 Which engines to use for the map
854 block : boolean
854 block : boolean
855 Should calls to `map` block or not
855 Should calls to `map` block or not
856 """
856 """
857 targets, block = self._findTargetsAndBlock(targets, block)
857 targets, block = self._findTargetsAndBlock(targets, block)
858 mapper = self.mapper(dist, targets, block)
858 mapper = self.mapper(dist, targets, block)
859 pf = ParallelFunction(mapper)
859 pf = ParallelFunction(mapper)
860 return pf
860 return pf
861
861
862 #---------------------------------------------------------------------------
862 #---------------------------------------------------------------------------
863 # IMultiEngineExtras
863 # IMultiEngineExtras
864 #---------------------------------------------------------------------------
864 #---------------------------------------------------------------------------
865
865
866 def zip_pull(self, keys, targets=None, block=None):
866 def zip_pull(self, keys, targets=None, block=None):
867 targets, block = self._findTargetsAndBlock(targets, block)
867 targets, block = self._findTargetsAndBlock(targets, block)
868 return self._blockFromThread(self.smultiengine.zip_pull, keys,
868 return self._blockFromThread(self.smultiengine.zip_pull, keys,
869 targets=targets, block=block)
869 targets=targets, block=block)
870
870
871 def run(self, filename, targets=None, block=None):
871 def run(self, filename, targets=None, block=None):
872 """
872 """
873 Run a Python code in a file on the engines.
873 Run a Python code in a file on the engines.
874
874
875 :Parameters:
875 :Parameters:
876 filename : str
876 filename : str
877 The name of the local file to run
877 The name of the local file to run
878 targets : id or list of ids
878 targets : id or list of ids
879 The engine to use for the execution
879 The engine to use for the execution
880 block : boolean
880 block : boolean
881 If False, this method will return the actual result. If False,
881 If False, this method will return the actual result. If False,
882 a `PendingResult` is returned which can be used to get the result
882 a `PendingResult` is returned which can be used to get the result
883 at a later time.
883 at a later time.
884 """
884 """
885 targets, block = self._findTargetsAndBlock(targets, block)
885 targets, block = self._findTargetsAndBlock(targets, block)
886 return self._blockFromThread(self.smultiengine.run, filename,
886 return self._blockFromThread(self.smultiengine.run, filename,
887 targets=targets, block=block)
887 targets=targets, block=block)
888
889 def benchmark(self, push_size=10000):
890 """
891 Run performance benchmarks for the current IPython cluster.
892
893 This method tests both the latency of sending command and data to the
894 engines as well as the throughput of sending large objects to the
895 engines using push. The latency is measured by having one or more
896 engines execute the command 'pass'. The throughput is measure by
897 sending an NumPy array of size `push_size` to one or more engines.
898
899 These benchmarks will vary widely on different hardware and networks
900 and thus can be used to get an idea of the performance characteristics
901 of a particular configuration of an IPython controller and engines.
902
903 This function is not testable within our current testing framework.
904 """
905 import timeit, __builtin__
906 __builtin__._mec_self = self
907 benchmarks = {}
908 repeat = 3
909 count = 10
910
911 timer = timeit.Timer('_mec_self.execute("pass",0)')
912 result = 1000*min(timer.repeat(repeat,count))/count
913 benchmarks['single_engine_latency'] = (result,'msec')
914
915 timer = timeit.Timer('_mec_self.execute("pass")')
916 result = 1000*min(timer.repeat(repeat,count))/count
917 benchmarks['all_engine_latency'] = (result,'msec')
888
918
919 try:
920 import numpy as np
921 except:
922 pass
923 else:
924 timer = timeit.Timer(
925 "_mec_self.push(d)",
926 "import numpy as np; d = dict(a=np.zeros(%r,dtype='float64'))" % push_size
927 )
928 result = min(timer.repeat(repeat,count))/count
929 benchmarks['all_engine_push'] = (1e-6*push_size*8/result, 'MB/sec')
930
931 try:
932 import numpy as np
933 except:
934 pass
935 else:
936 timer = timeit.Timer(
937 "_mec_self.push(d,0)",
938 "import numpy as np; d = dict(a=np.zeros(%r,dtype='float64'))" % push_size
939 )
940 result = min(timer.repeat(repeat,count))/count
941 benchmarks['single_engine_push'] = (1e-6*push_size*8/result, 'MB/sec')
942
943 return benchmarks
889
944
890
945
891 components.registerAdapter(FullBlockingMultiEngineClient,
946 components.registerAdapter(FullBlockingMultiEngineClient,
892 IFullSynchronousMultiEngine, IFullBlockingMultiEngineClient)
947 IFullSynchronousMultiEngine, IFullBlockingMultiEngineClient)
893
948
894
949
895
950
896
951
@@ -1,723 +1,783 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3
3
4 """Start an IPython cluster = (controller + engines)."""
4 """Start an IPython cluster = (controller + engines)."""
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008 The IPython Development Team
7 # Copyright (C) 2008 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import os
17 import os
18 import re
18 import re
19 import sys
19 import sys
20 import signal
20 import signal
21 import tempfile
21 import tempfile
22 pjoin = os.path.join
22 pjoin = os.path.join
23
23
24 from twisted.internet import reactor, defer
24 from twisted.internet import reactor, defer
25 from twisted.internet.protocol import ProcessProtocol
25 from twisted.internet.protocol import ProcessProtocol
26 from twisted.internet.error import ProcessDone, ProcessTerminated
26 from twisted.internet.error import ProcessDone, ProcessTerminated
27 from twisted.internet.utils import getProcessOutput
27 from twisted.internet.utils import getProcessOutput
28 from twisted.python import failure, log
28 from twisted.python import failure, log
29
29
30 from IPython.external import argparse
30 from IPython.external import argparse
31 from IPython.external import Itpl
31 from IPython.external import Itpl
32 from IPython.genutils import get_ipython_dir, num_cpus
32 from IPython.genutils import get_ipython_dir, num_cpus
33 from IPython.kernel.fcutil import have_crypto
33 from IPython.kernel.fcutil import have_crypto
34 from IPython.kernel.error import SecurityError
34 from IPython.kernel.error import SecurityError
35 from IPython.kernel.fcutil import have_crypto
35 from IPython.kernel.fcutil import have_crypto
36 from IPython.kernel.twistedutil import gatherBoth
36 from IPython.kernel.twistedutil import gatherBoth
37 from IPython.kernel.util import printer
37 from IPython.kernel.util import printer
38
38
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # General process handling code
41 # General process handling code
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 def find_exe(cmd):
44 def find_exe(cmd):
45 try:
45 try:
46 import win32api
46 import win32api
47 except ImportError:
47 except ImportError:
48 raise ImportError('you need to have pywin32 installed for this to work')
48 raise ImportError('you need to have pywin32 installed for this to work')
49 else:
49 else:
50 try:
50 try:
51 (path, offest) = win32api.SearchPath(os.environ['PATH'],cmd + '.exe')
51 (path, offest) = win32api.SearchPath(os.environ['PATH'],cmd + '.exe')
52 except:
52 except:
53 (path, offset) = win32api.SearchPath(os.environ['PATH'],cmd + '.bat')
53 (path, offset) = win32api.SearchPath(os.environ['PATH'],cmd + '.bat')
54 return path
54 return path
55
55
56 class ProcessStateError(Exception):
56 class ProcessStateError(Exception):
57 pass
57 pass
58
58
59 class UnknownStatus(Exception):
59 class UnknownStatus(Exception):
60 pass
60 pass
61
61
62 class LauncherProcessProtocol(ProcessProtocol):
62 class LauncherProcessProtocol(ProcessProtocol):
63 """
63 """
64 A ProcessProtocol to go with the ProcessLauncher.
64 A ProcessProtocol to go with the ProcessLauncher.
65 """
65 """
66 def __init__(self, process_launcher):
66 def __init__(self, process_launcher):
67 self.process_launcher = process_launcher
67 self.process_launcher = process_launcher
68
68
69 def connectionMade(self):
69 def connectionMade(self):
70 self.process_launcher.fire_start_deferred(self.transport.pid)
70 self.process_launcher.fire_start_deferred(self.transport.pid)
71
71
72 def processEnded(self, status):
72 def processEnded(self, status):
73 value = status.value
73 value = status.value
74 if isinstance(value, ProcessDone):
74 if isinstance(value, ProcessDone):
75 self.process_launcher.fire_stop_deferred(0)
75 self.process_launcher.fire_stop_deferred(0)
76 elif isinstance(value, ProcessTerminated):
76 elif isinstance(value, ProcessTerminated):
77 self.process_launcher.fire_stop_deferred(
77 self.process_launcher.fire_stop_deferred(
78 {'exit_code':value.exitCode,
78 {'exit_code':value.exitCode,
79 'signal':value.signal,
79 'signal':value.signal,
80 'status':value.status
80 'status':value.status
81 }
81 }
82 )
82 )
83 else:
83 else:
84 raise UnknownStatus("unknown exit status, this is probably a bug in Twisted")
84 raise UnknownStatus("unknown exit status, this is probably a bug in Twisted")
85
85
86 def outReceived(self, data):
86 def outReceived(self, data):
87 log.msg(data)
87 log.msg(data)
88
88
89 def errReceived(self, data):
89 def errReceived(self, data):
90 log.err(data)
90 log.err(data)
91
91
92 class ProcessLauncher(object):
92 class ProcessLauncher(object):
93 """
93 """
94 Start and stop an external process in an asynchronous manner.
94 Start and stop an external process in an asynchronous manner.
95
95
96 Currently this uses deferreds to notify other parties of process state
96 Currently this uses deferreds to notify other parties of process state
97 changes. This is an awkward design and should be moved to using
97 changes. This is an awkward design and should be moved to using
98 a formal NotificationCenter.
98 a formal NotificationCenter.
99 """
99 """
100 def __init__(self, cmd_and_args):
100 def __init__(self, cmd_and_args):
101 self.cmd = cmd_and_args[0]
101 self.cmd = cmd_and_args[0]
102 self.args = cmd_and_args
102 self.args = cmd_and_args
103 self._reset()
103 self._reset()
104
104
105 def _reset(self):
105 def _reset(self):
106 self.process_protocol = None
106 self.process_protocol = None
107 self.pid = None
107 self.pid = None
108 self.start_deferred = None
108 self.start_deferred = None
109 self.stop_deferreds = []
109 self.stop_deferreds = []
110 self.state = 'before' # before, running, or after
110 self.state = 'before' # before, running, or after
111
111
112 @property
112 @property
113 def running(self):
113 def running(self):
114 if self.state == 'running':
114 if self.state == 'running':
115 return True
115 return True
116 else:
116 else:
117 return False
117 return False
118
118
119 def fire_start_deferred(self, pid):
119 def fire_start_deferred(self, pid):
120 self.pid = pid
120 self.pid = pid
121 self.state = 'running'
121 self.state = 'running'
122 log.msg('Process %r has started with pid=%i' % (self.args, pid))
122 log.msg('Process %r has started with pid=%i' % (self.args, pid))
123 self.start_deferred.callback(pid)
123 self.start_deferred.callback(pid)
124
124
125 def start(self):
125 def start(self):
126 if self.state == 'before':
126 if self.state == 'before':
127 self.process_protocol = LauncherProcessProtocol(self)
127 self.process_protocol = LauncherProcessProtocol(self)
128 self.start_deferred = defer.Deferred()
128 self.start_deferred = defer.Deferred()
129 self.process_transport = reactor.spawnProcess(
129 self.process_transport = reactor.spawnProcess(
130 self.process_protocol,
130 self.process_protocol,
131 self.cmd,
131 self.cmd,
132 self.args,
132 self.args,
133 env=os.environ
133 env=os.environ
134 )
134 )
135 return self.start_deferred
135 return self.start_deferred
136 else:
136 else:
137 s = 'the process has already been started and has state: %r' % \
137 s = 'the process has already been started and has state: %r' % \
138 self.state
138 self.state
139 return defer.fail(ProcessStateError(s))
139 return defer.fail(ProcessStateError(s))
140
140
141 def get_stop_deferred(self):
141 def get_stop_deferred(self):
142 if self.state == 'running' or self.state == 'before':
142 if self.state == 'running' or self.state == 'before':
143 d = defer.Deferred()
143 d = defer.Deferred()
144 self.stop_deferreds.append(d)
144 self.stop_deferreds.append(d)
145 return d
145 return d
146 else:
146 else:
147 s = 'this process is already complete'
147 s = 'this process is already complete'
148 return defer.fail(ProcessStateError(s))
148 return defer.fail(ProcessStateError(s))
149
149
150 def fire_stop_deferred(self, exit_code):
150 def fire_stop_deferred(self, exit_code):
151 log.msg('Process %r has stopped with %r' % (self.args, exit_code))
151 log.msg('Process %r has stopped with %r' % (self.args, exit_code))
152 self.state = 'after'
152 self.state = 'after'
153 for d in self.stop_deferreds:
153 for d in self.stop_deferreds:
154 d.callback(exit_code)
154 d.callback(exit_code)
155
155
156 def signal(self, sig):
156 def signal(self, sig):
157 """
157 """
158 Send a signal to the process.
158 Send a signal to the process.
159
159
160 The argument sig can be ('KILL','INT', etc.) or any signal number.
160 The argument sig can be ('KILL','INT', etc.) or any signal number.
161 """
161 """
162 if self.state == 'running':
162 if self.state == 'running':
163 self.process_transport.signalProcess(sig)
163 self.process_transport.signalProcess(sig)
164
164
165 # def __del__(self):
165 # def __del__(self):
166 # self.signal('KILL')
166 # self.signal('KILL')
167
167
168 def interrupt_then_kill(self, delay=1.0):
168 def interrupt_then_kill(self, delay=1.0):
169 self.signal('INT')
169 self.signal('INT')
170 reactor.callLater(delay, self.signal, 'KILL')
170 reactor.callLater(delay, self.signal, 'KILL')
171
171
172
172
173 #-----------------------------------------------------------------------------
173 #-----------------------------------------------------------------------------
174 # Code for launching controller and engines
174 # Code for launching controller and engines
175 #-----------------------------------------------------------------------------
175 #-----------------------------------------------------------------------------
176
176
177
177
178 class ControllerLauncher(ProcessLauncher):
178 class ControllerLauncher(ProcessLauncher):
179
179
180 def __init__(self, extra_args=None):
180 def __init__(self, extra_args=None):
181 if sys.platform == 'win32':
181 if sys.platform == 'win32':
182 # This logic is needed because the ipcontroller script doesn't
182 # This logic is needed because the ipcontroller script doesn't
183 # always get installed in the same way or in the same location.
183 # always get installed in the same way or in the same location.
184 from IPython.kernel.scripts import ipcontroller
184 from IPython.kernel.scripts import ipcontroller
185 script_location = ipcontroller.__file__.replace('.pyc', '.py')
185 script_location = ipcontroller.__file__.replace('.pyc', '.py')
186 # The -u option here turns on unbuffered output, which is required
186 # The -u option here turns on unbuffered output, which is required
187 # on Win32 to prevent wierd conflict and problems with Twisted
187 # on Win32 to prevent wierd conflict and problems with Twisted
188 args = [find_exe('python'), '-u', script_location]
188 args = [find_exe('python'), '-u', script_location]
189 else:
189 else:
190 args = ['ipcontroller']
190 args = ['ipcontroller']
191 self.extra_args = extra_args
191 self.extra_args = extra_args
192 if extra_args is not None:
192 if extra_args is not None:
193 args.extend(extra_args)
193 args.extend(extra_args)
194
194
195 ProcessLauncher.__init__(self, args)
195 ProcessLauncher.__init__(self, args)
196
196
197
197
198 class EngineLauncher(ProcessLauncher):
198 class EngineLauncher(ProcessLauncher):
199
199
200 def __init__(self, extra_args=None):
200 def __init__(self, extra_args=None):
201 if sys.platform == 'win32':
201 if sys.platform == 'win32':
202 # This logic is needed because the ipcontroller script doesn't
202 # This logic is needed because the ipcontroller script doesn't
203 # always get installed in the same way or in the same location.
203 # always get installed in the same way or in the same location.
204 from IPython.kernel.scripts import ipengine
204 from IPython.kernel.scripts import ipengine
205 script_location = ipengine.__file__.replace('.pyc', '.py')
205 script_location = ipengine.__file__.replace('.pyc', '.py')
206 # The -u option here turns on unbuffered output, which is required
206 # The -u option here turns on unbuffered output, which is required
207 # on Win32 to prevent wierd conflict and problems with Twisted
207 # on Win32 to prevent wierd conflict and problems with Twisted
208 args = [find_exe('python'), '-u', script_location]
208 args = [find_exe('python'), '-u', script_location]
209 else:
209 else:
210 args = ['ipengine']
210 args = ['ipengine']
211 self.extra_args = extra_args
211 self.extra_args = extra_args
212 if extra_args is not None:
212 if extra_args is not None:
213 args.extend(extra_args)
213 args.extend(extra_args)
214
214
215 ProcessLauncher.__init__(self, args)
215 ProcessLauncher.__init__(self, args)
216
216
217
217
218 class LocalEngineSet(object):
218 class LocalEngineSet(object):
219
219
220 def __init__(self, extra_args=None):
220 def __init__(self, extra_args=None):
221 self.extra_args = extra_args
221 self.extra_args = extra_args
222 self.launchers = []
222 self.launchers = []
223
223
224 def start(self, n):
224 def start(self, n):
225 dlist = []
225 dlist = []
226 for i in range(n):
226 for i in range(n):
227 el = EngineLauncher(extra_args=self.extra_args)
227 el = EngineLauncher(extra_args=self.extra_args)
228 d = el.start()
228 d = el.start()
229 self.launchers.append(el)
229 self.launchers.append(el)
230 dlist.append(d)
230 dlist.append(d)
231 dfinal = gatherBoth(dlist, consumeErrors=True)
231 dfinal = gatherBoth(dlist, consumeErrors=True)
232 dfinal.addCallback(self._handle_start)
232 dfinal.addCallback(self._handle_start)
233 return dfinal
233 return dfinal
234
234
235 def _handle_start(self, r):
235 def _handle_start(self, r):
236 log.msg('Engines started with pids: %r' % r)
236 log.msg('Engines started with pids: %r' % r)
237 return r
237 return r
238
238
239 def _handle_stop(self, r):
239 def _handle_stop(self, r):
240 log.msg('Engines received signal: %r' % r)
240 log.msg('Engines received signal: %r' % r)
241 return r
241 return r
242
242
243 def signal(self, sig):
243 def signal(self, sig):
244 dlist = []
244 dlist = []
245 for el in self.launchers:
245 for el in self.launchers:
246 d = el.get_stop_deferred()
246 d = el.get_stop_deferred()
247 dlist.append(d)
247 dlist.append(d)
248 el.signal(sig)
248 el.signal(sig)
249 dfinal = gatherBoth(dlist, consumeErrors=True)
249 dfinal = gatherBoth(dlist, consumeErrors=True)
250 dfinal.addCallback(self._handle_stop)
250 dfinal.addCallback(self._handle_stop)
251 return dfinal
251 return dfinal
252
252
253 def interrupt_then_kill(self, delay=1.0):
253 def interrupt_then_kill(self, delay=1.0):
254 dlist = []
254 dlist = []
255 for el in self.launchers:
255 for el in self.launchers:
256 d = el.get_stop_deferred()
256 d = el.get_stop_deferred()
257 dlist.append(d)
257 dlist.append(d)
258 el.interrupt_then_kill(delay)
258 el.interrupt_then_kill(delay)
259 dfinal = gatherBoth(dlist, consumeErrors=True)
259 dfinal = gatherBoth(dlist, consumeErrors=True)
260 dfinal.addCallback(self._handle_stop)
260 dfinal.addCallback(self._handle_stop)
261 return dfinal
261 return dfinal
262
262
263
263
264 class BatchEngineSet(object):
264 class BatchEngineSet(object):
265
265
266 # Subclasses must fill these in. See PBSEngineSet
266 # Subclasses must fill these in. See PBSEngineSet
267 submit_command = ''
267 submit_command = ''
268 delete_command = ''
268 delete_command = ''
269 job_id_regexp = ''
269 job_id_regexp = ''
270
270
271 def __init__(self, template_file, **kwargs):
271 def __init__(self, template_file, **kwargs):
272 self.template_file = template_file
272 self.template_file = template_file
273 self.context = {}
273 self.context = {}
274 self.context.update(kwargs)
274 self.context.update(kwargs)
275 self.batch_file = self.template_file+'-run'
275 self.batch_file = self.template_file+'-run'
276
276
277 def parse_job_id(self, output):
277 def parse_job_id(self, output):
278 m = re.match(self.job_id_regexp, output)
278 m = re.match(self.job_id_regexp, output)
279 if m is not None:
279 if m is not None:
280 job_id = m.group()
280 job_id = m.group()
281 else:
281 else:
282 raise Exception("job id couldn't be determined: %s" % output)
282 raise Exception("job id couldn't be determined: %s" % output)
283 self.job_id = job_id
283 self.job_id = job_id
284 log.msg('Job started with job id: %r' % job_id)
284 log.msg('Job started with job id: %r' % job_id)
285 return job_id
285 return job_id
286
286
287 def write_batch_script(self, n):
287 def write_batch_script(self, n):
288 self.context['n'] = n
288 self.context['n'] = n
289 template = open(self.template_file, 'r').read()
289 template = open(self.template_file, 'r').read()
290 log.msg('Using template for batch script: %s' % self.template_file)
290 log.msg('Using template for batch script: %s' % self.template_file)
291 script_as_string = Itpl.itplns(template, self.context)
291 script_as_string = Itpl.itplns(template, self.context)
292 log.msg('Writing instantiated batch script: %s' % self.batch_file)
292 log.msg('Writing instantiated batch script: %s' % self.batch_file)
293 f = open(self.batch_file,'w')
293 f = open(self.batch_file,'w')
294 f.write(script_as_string)
294 f.write(script_as_string)
295 f.close()
295 f.close()
296
296
297 def handle_error(self, f):
297 def handle_error(self, f):
298 f.printTraceback()
298 f.printTraceback()
299 f.raiseException()
299 f.raiseException()
300
300
301 def start(self, n):
301 def start(self, n):
302 self.write_batch_script(n)
302 self.write_batch_script(n)
303 d = getProcessOutput(self.submit_command,
303 d = getProcessOutput(self.submit_command,
304 [self.batch_file],env=os.environ)
304 [self.batch_file],env=os.environ)
305 d.addCallback(self.parse_job_id)
305 d.addCallback(self.parse_job_id)
306 d.addErrback(self.handle_error)
306 d.addErrback(self.handle_error)
307 return d
307 return d
308
308
309 def kill(self):
309 def kill(self):
310 d = getProcessOutput(self.delete_command,
310 d = getProcessOutput(self.delete_command,
311 [self.job_id],env=os.environ)
311 [self.job_id],env=os.environ)
312 return d
312 return d
313
313
314 class PBSEngineSet(BatchEngineSet):
314 class PBSEngineSet(BatchEngineSet):
315
315
316 submit_command = 'qsub'
316 submit_command = 'qsub'
317 delete_command = 'qdel'
317 delete_command = 'qdel'
318 job_id_regexp = '\d+'
318 job_id_regexp = '\d+'
319
319
320 def __init__(self, template_file, **kwargs):
320 def __init__(self, template_file, **kwargs):
321 BatchEngineSet.__init__(self, template_file, **kwargs)
321 BatchEngineSet.__init__(self, template_file, **kwargs)
322
322
323
323
324 sshx_template="""#!/bin/sh
324 sshx_template="""#!/bin/sh
325 "$@" &> /dev/null &
325 "$@" &> /dev/null &
326 echo $!
326 echo $!
327 """
327 """
328
328
329 engine_killer_template="""#!/bin/sh
329 engine_killer_template="""#!/bin/sh
330 ps -fu `whoami` | grep '[i]pengine' | awk '{print $2}' | xargs kill -TERM
330 ps -fu `whoami` | grep '[i]pengine' | awk '{print $2}' | xargs kill -TERM
331 """
331 """
332
332
333 class SSHEngineSet(object):
333 class SSHEngineSet(object):
334 sshx_template=sshx_template
334 sshx_template=sshx_template
335 engine_killer_template=engine_killer_template
335 engine_killer_template=engine_killer_template
336
336
337 def __init__(self, engine_hosts, sshx=None, ipengine="ipengine"):
337 def __init__(self, engine_hosts, sshx=None, ipengine="ipengine"):
338 """Start a controller on localhost and engines using ssh.
338 """Start a controller on localhost and engines using ssh.
339
339
340 The engine_hosts argument is a dict with hostnames as keys and
340 The engine_hosts argument is a dict with hostnames as keys and
341 the number of engine (int) as values. sshx is the name of a local
341 the number of engine (int) as values. sshx is the name of a local
342 file that will be used to run remote commands. This file is used
342 file that will be used to run remote commands. This file is used
343 to setup the environment properly.
343 to setup the environment properly.
344 """
344 """
345
345
346 self.temp_dir = tempfile.gettempdir()
346 self.temp_dir = tempfile.gettempdir()
347 if sshx is not None:
347 if sshx is not None:
348 self.sshx = sshx
348 self.sshx = sshx
349 else:
349 else:
350 # Write the sshx.sh file locally from our template.
350 # Write the sshx.sh file locally from our template.
351 self.sshx = os.path.join(
351 self.sshx = os.path.join(
352 self.temp_dir,
352 self.temp_dir,
353 '%s-main-sshx.sh' % os.environ['USER']
353 '%s-main-sshx.sh' % os.environ['USER']
354 )
354 )
355 f = open(self.sshx, 'w')
355 f = open(self.sshx, 'w')
356 f.writelines(self.sshx_template)
356 f.writelines(self.sshx_template)
357 f.close()
357 f.close()
358 self.engine_command = ipengine
358 self.engine_command = ipengine
359 self.engine_hosts = engine_hosts
359 self.engine_hosts = engine_hosts
360 # Write the engine killer script file locally from our template.
360 # Write the engine killer script file locally from our template.
361 self.engine_killer = os.path.join(
361 self.engine_killer = os.path.join(
362 self.temp_dir,
362 self.temp_dir,
363 '%s-local-engine_killer.sh' % os.environ['USER']
363 '%s-local-engine_killer.sh' % os.environ['USER']
364 )
364 )
365 f = open(self.engine_killer, 'w')
365 f = open(self.engine_killer, 'w')
366 f.writelines(self.engine_killer_template)
366 f.writelines(self.engine_killer_template)
367 f.close()
367 f.close()
368
368
369 def start(self, send_furl=False):
369 def start(self, send_furl=False):
370 dlist = []
370 dlist = []
371 for host in self.engine_hosts.keys():
371 for host in self.engine_hosts.keys():
372 count = self.engine_hosts[host]
372 count = self.engine_hosts[host]
373 d = self._start(host, count, send_furl)
373 d = self._start(host, count, send_furl)
374 dlist.append(d)
374 dlist.append(d)
375 return gatherBoth(dlist, consumeErrors=True)
375 return gatherBoth(dlist, consumeErrors=True)
376
376
377 def _start(self, hostname, count=1, send_furl=False):
377 def _start(self, hostname, count=1, send_furl=False):
378 if send_furl:
378 if send_furl:
379 d = self._scp_furl(hostname)
379 d = self._scp_furl(hostname)
380 else:
380 else:
381 d = defer.succeed(None)
381 d = defer.succeed(None)
382 d.addCallback(lambda r: self._scp_sshx(hostname))
382 d.addCallback(lambda r: self._scp_sshx(hostname))
383 d.addCallback(lambda r: self._ssh_engine(hostname, count))
383 d.addCallback(lambda r: self._ssh_engine(hostname, count))
384 return d
384 return d
385
385
386 def _scp_furl(self, hostname):
386 def _scp_furl(self, hostname):
387 scp_cmd = "scp ~/.ipython/security/ipcontroller-engine.furl %s:.ipython/security/" % (hostname)
387 scp_cmd = "scp ~/.ipython/security/ipcontroller-engine.furl %s:.ipython/security/" % (hostname)
388 cmd_list = scp_cmd.split()
388 cmd_list = scp_cmd.split()
389 cmd_list[1] = os.path.expanduser(cmd_list[1])
389 cmd_list[1] = os.path.expanduser(cmd_list[1])
390 log.msg('Copying furl file: %s' % scp_cmd)
390 log.msg('Copying furl file: %s' % scp_cmd)
391 d = getProcessOutput(cmd_list[0], cmd_list[1:], env=os.environ)
391 d = getProcessOutput(cmd_list[0], cmd_list[1:], env=os.environ)
392 return d
392 return d
393
393
394 def _scp_sshx(self, hostname):
394 def _scp_sshx(self, hostname):
395 scp_cmd = "scp %s %s:%s/%s-sshx.sh" % (
395 scp_cmd = "scp %s %s:%s/%s-sshx.sh" % (
396 self.sshx, hostname,
396 self.sshx, hostname,
397 self.temp_dir, os.environ['USER']
397 self.temp_dir, os.environ['USER']
398 )
398 )
399 print
399 print
400 log.msg("Copying sshx: %s" % scp_cmd)
400 log.msg("Copying sshx: %s" % scp_cmd)
401 sshx_scp = scp_cmd.split()
401 sshx_scp = scp_cmd.split()
402 d = getProcessOutput(sshx_scp[0], sshx_scp[1:], env=os.environ)
402 d = getProcessOutput(sshx_scp[0], sshx_scp[1:], env=os.environ)
403 return d
403 return d
404
404
405 def _ssh_engine(self, hostname, count):
405 def _ssh_engine(self, hostname, count):
406 exec_engine = "ssh %s sh %s/%s-sshx.sh %s" % (
406 exec_engine = "ssh %s sh %s/%s-sshx.sh %s" % (
407 hostname, self.temp_dir,
407 hostname, self.temp_dir,
408 os.environ['USER'], self.engine_command
408 os.environ['USER'], self.engine_command
409 )
409 )
410 cmds = exec_engine.split()
410 cmds = exec_engine.split()
411 dlist = []
411 dlist = []
412 log.msg("about to start engines...")
412 log.msg("about to start engines...")
413 for i in range(count):
413 for i in range(count):
414 log.msg('Starting engines: %s' % exec_engine)
414 log.msg('Starting engines: %s' % exec_engine)
415 d = getProcessOutput(cmds[0], cmds[1:], env=os.environ)
415 d = getProcessOutput(cmds[0], cmds[1:], env=os.environ)
416 dlist.append(d)
416 dlist.append(d)
417 return gatherBoth(dlist, consumeErrors=True)
417 return gatherBoth(dlist, consumeErrors=True)
418
418
419 def kill(self):
419 def kill(self):
420 dlist = []
420 dlist = []
421 for host in self.engine_hosts.keys():
421 for host in self.engine_hosts.keys():
422 d = self._killall(host)
422 d = self._killall(host)
423 dlist.append(d)
423 dlist.append(d)
424 return gatherBoth(dlist, consumeErrors=True)
424 return gatherBoth(dlist, consumeErrors=True)
425
425
426 def _killall(self, hostname):
426 def _killall(self, hostname):
427 d = self._scp_engine_killer(hostname)
427 d = self._scp_engine_killer(hostname)
428 d.addCallback(lambda r: self._ssh_kill(hostname))
428 d.addCallback(lambda r: self._ssh_kill(hostname))
429 # d.addErrback(self._exec_err)
429 # d.addErrback(self._exec_err)
430 return d
430 return d
431
431
432 def _scp_engine_killer(self, hostname):
432 def _scp_engine_killer(self, hostname):
433 scp_cmd = "scp %s %s:%s/%s-engine_killer.sh" % (
433 scp_cmd = "scp %s %s:%s/%s-engine_killer.sh" % (
434 self.engine_killer,
434 self.engine_killer,
435 hostname,
435 hostname,
436 self.temp_dir,
436 self.temp_dir,
437 os.environ['USER']
437 os.environ['USER']
438 )
438 )
439 cmds = scp_cmd.split()
439 cmds = scp_cmd.split()
440 log.msg('Copying engine_killer: %s' % scp_cmd)
440 log.msg('Copying engine_killer: %s' % scp_cmd)
441 d = getProcessOutput(cmds[0], cmds[1:], env=os.environ)
441 d = getProcessOutput(cmds[0], cmds[1:], env=os.environ)
442 return d
442 return d
443
443
444 def _ssh_kill(self, hostname):
444 def _ssh_kill(self, hostname):
445 kill_cmd = "ssh %s sh %s/%s-engine_killer.sh" % (
445 kill_cmd = "ssh %s sh %s/%s-engine_killer.sh" % (
446 hostname,
446 hostname,
447 self.temp_dir,
447 self.temp_dir,
448 os.environ['USER']
448 os.environ['USER']
449 )
449 )
450 log.msg('Killing engine: %s' % kill_cmd)
450 log.msg('Killing engine: %s' % kill_cmd)
451 kill_cmd = kill_cmd.split()
451 kill_cmd = kill_cmd.split()
452 d = getProcessOutput(kill_cmd[0], kill_cmd[1:], env=os.environ)
452 d = getProcessOutput(kill_cmd[0], kill_cmd[1:], env=os.environ)
453 return d
453 return d
454
454
455 def _exec_err(self, r):
455 def _exec_err(self, r):
456 log.msg(r)
456 log.msg(r)
457
457
458 #-----------------------------------------------------------------------------
458 #-----------------------------------------------------------------------------
459 # Main functions for the different types of clusters
459 # Main functions for the different types of clusters
460 #-----------------------------------------------------------------------------
460 #-----------------------------------------------------------------------------
461
461
462 # TODO:
462 # TODO:
463 # The logic in these codes should be moved into classes like LocalCluster
463 # The logic in these codes should be moved into classes like LocalCluster
464 # MpirunCluster, PBSCluster, etc. This would remove alot of the duplications.
464 # MpirunCluster, PBSCluster, etc. This would remove alot of the duplications.
465 # The main functions should then just parse the command line arguments, create
465 # The main functions should then just parse the command line arguments, create
466 # the appropriate class and call a 'start' method.
466 # the appropriate class and call a 'start' method.
467
467
468 def check_security(args, cont_args):
468 def check_security(args, cont_args):
469 if (not args.x or not args.y) and not have_crypto:
469 if (not args.x or not args.y) and not have_crypto:
470 log.err("""
470 log.err("""
471 OpenSSL/pyOpenSSL is not available, so we can't run in secure mode.
471 OpenSSL/pyOpenSSL is not available, so we can't run in secure mode.
472 Try running ipcluster with the -xy flags: ipcluster local -xy -n 4""")
472 Try running ipcluster with the -xy flags: ipcluster local -xy -n 4""")
473 reactor.stop()
473 reactor.stop()
474 return False
474 return False
475 if args.x:
475 if args.x:
476 cont_args.append('-x')
476 cont_args.append('-x')
477 if args.y:
477 if args.y:
478 cont_args.append('-y')
478 cont_args.append('-y')
479 return True
479 return True
480
480
481 def check_reuse(args, cont_args):
482 if args.r:
483 cont_args.append('-r')
484 if args.client_port == 0 or args.engine_port == 0:
485 log.err("""
486 To reuse FURL files, you must also set the client and engine ports using
487 the --client-port and --engine-port options.""")
488 reactor.stop()
489 return False
490 cont_args.append('--client-port=%i' % args.client_port)
491 cont_args.append('--engine-port=%i' % args.engine_port)
492 return True
481
493
482 def main_local(args):
494 def main_local(args):
483 cont_args = []
495 cont_args = []
484 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
496 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
485
497
486 # Check security settings before proceeding
498 # Check security settings before proceeding
487 if not check_security(args, cont_args):
499 if not check_security(args, cont_args):
488 return
500 return
489
501
502 # See if we are reusing FURL files
503 if not check_reuse(args, cont_args):
504 return
505
490 cl = ControllerLauncher(extra_args=cont_args)
506 cl = ControllerLauncher(extra_args=cont_args)
491 dstart = cl.start()
507 dstart = cl.start()
492 def start_engines(cont_pid):
508 def start_engines(cont_pid):
493 engine_args = []
509 engine_args = []
494 engine_args.append('--logfile=%s' % \
510 engine_args.append('--logfile=%s' % \
495 pjoin(args.logdir,'ipengine%s-' % cont_pid))
511 pjoin(args.logdir,'ipengine%s-' % cont_pid))
496 eset = LocalEngineSet(extra_args=engine_args)
512 eset = LocalEngineSet(extra_args=engine_args)
497 def shutdown(signum, frame):
513 def shutdown(signum, frame):
498 log.msg('Stopping local cluster')
514 log.msg('Stopping local cluster')
499 # We are still playing with the times here, but these seem
515 # We are still playing with the times here, but these seem
500 # to be reliable in allowing everything to exit cleanly.
516 # to be reliable in allowing everything to exit cleanly.
501 eset.interrupt_then_kill(0.5)
517 eset.interrupt_then_kill(0.5)
502 cl.interrupt_then_kill(0.5)
518 cl.interrupt_then_kill(0.5)
503 reactor.callLater(1.0, reactor.stop)
519 reactor.callLater(1.0, reactor.stop)
504 signal.signal(signal.SIGINT,shutdown)
520 signal.signal(signal.SIGINT,shutdown)
505 d = eset.start(args.n)
521 d = eset.start(args.n)
506 return d
522 return d
507 def delay_start(cont_pid):
523 def delay_start(cont_pid):
508 # This is needed because the controller doesn't start listening
524 # This is needed because the controller doesn't start listening
509 # right when it starts and the controller needs to write
525 # right when it starts and the controller needs to write
510 # furl files for the engine to pick up
526 # furl files for the engine to pick up
511 reactor.callLater(1.0, start_engines, cont_pid)
527 reactor.callLater(1.0, start_engines, cont_pid)
512 dstart.addCallback(delay_start)
528 dstart.addCallback(delay_start)
513 dstart.addErrback(lambda f: f.raiseException())
529 dstart.addErrback(lambda f: f.raiseException())
514
530
515
531
516 def main_mpirun(args):
532 def main_mpi(args):
517 cont_args = []
533 cont_args = []
518 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
534 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
519
535
520 # Check security settings before proceeding
536 # Check security settings before proceeding
521 if not check_security(args, cont_args):
537 if not check_security(args, cont_args):
522 return
538 return
523
539
540 # See if we are reusing FURL files
541 if not check_reuse(args, cont_args):
542 return
543
524 cl = ControllerLauncher(extra_args=cont_args)
544 cl = ControllerLauncher(extra_args=cont_args)
525 dstart = cl.start()
545 dstart = cl.start()
526 def start_engines(cont_pid):
546 def start_engines(cont_pid):
527 raw_args = ['mpirun']
547 raw_args = [args.cmd]
528 raw_args.extend(['-n',str(args.n)])
548 raw_args.extend(['-n',str(args.n)])
529 raw_args.append('ipengine')
549 raw_args.append('ipengine')
530 raw_args.append('-l')
550 raw_args.append('-l')
531 raw_args.append(pjoin(args.logdir,'ipengine%s-' % cont_pid))
551 raw_args.append(pjoin(args.logdir,'ipengine%s-' % cont_pid))
532 if args.mpi:
552 if args.mpi:
533 raw_args.append('--mpi=%s' % args.mpi)
553 raw_args.append('--mpi=%s' % args.mpi)
534 eset = ProcessLauncher(raw_args)
554 eset = ProcessLauncher(raw_args)
535 def shutdown(signum, frame):
555 def shutdown(signum, frame):
536 log.msg('Stopping local cluster')
556 log.msg('Stopping local cluster')
537 # We are still playing with the times here, but these seem
557 # We are still playing with the times here, but these seem
538 # to be reliable in allowing everything to exit cleanly.
558 # to be reliable in allowing everything to exit cleanly.
539 eset.interrupt_then_kill(1.0)
559 eset.interrupt_then_kill(1.0)
540 cl.interrupt_then_kill(1.0)
560 cl.interrupt_then_kill(1.0)
541 reactor.callLater(2.0, reactor.stop)
561 reactor.callLater(2.0, reactor.stop)
542 signal.signal(signal.SIGINT,shutdown)
562 signal.signal(signal.SIGINT,shutdown)
543 d = eset.start()
563 d = eset.start()
544 return d
564 return d
545 def delay_start(cont_pid):
565 def delay_start(cont_pid):
546 # This is needed because the controller doesn't start listening
566 # This is needed because the controller doesn't start listening
547 # right when it starts and the controller needs to write
567 # right when it starts and the controller needs to write
548 # furl files for the engine to pick up
568 # furl files for the engine to pick up
549 reactor.callLater(1.0, start_engines, cont_pid)
569 reactor.callLater(1.0, start_engines, cont_pid)
550 dstart.addCallback(delay_start)
570 dstart.addCallback(delay_start)
551 dstart.addErrback(lambda f: f.raiseException())
571 dstart.addErrback(lambda f: f.raiseException())
552
572
553
573
554 def main_pbs(args):
574 def main_pbs(args):
555 cont_args = []
575 cont_args = []
556 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
576 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
557
577
558 # Check security settings before proceeding
578 # Check security settings before proceeding
559 if not check_security(args, cont_args):
579 if not check_security(args, cont_args):
560 return
580 return
561
581
582 # See if we are reusing FURL files
583 if not check_reuse(args, cont_args):
584 return
585
562 cl = ControllerLauncher(extra_args=cont_args)
586 cl = ControllerLauncher(extra_args=cont_args)
563 dstart = cl.start()
587 dstart = cl.start()
564 def start_engines(r):
588 def start_engines(r):
565 pbs_set = PBSEngineSet(args.pbsscript)
589 pbs_set = PBSEngineSet(args.pbsscript)
566 def shutdown(signum, frame):
590 def shutdown(signum, frame):
567 log.msg('Stopping pbs cluster')
591 log.msg('Stopping pbs cluster')
568 d = pbs_set.kill()
592 d = pbs_set.kill()
569 d.addBoth(lambda _: cl.interrupt_then_kill(1.0))
593 d.addBoth(lambda _: cl.interrupt_then_kill(1.0))
570 d.addBoth(lambda _: reactor.callLater(2.0, reactor.stop))
594 d.addBoth(lambda _: reactor.callLater(2.0, reactor.stop))
571 signal.signal(signal.SIGINT,shutdown)
595 signal.signal(signal.SIGINT,shutdown)
572 d = pbs_set.start(args.n)
596 d = pbs_set.start(args.n)
573 return d
597 return d
574 dstart.addCallback(start_engines)
598 dstart.addCallback(start_engines)
575 dstart.addErrback(lambda f: f.raiseException())
599 dstart.addErrback(lambda f: f.raiseException())
576
600
577
601
578 def main_ssh(args):
602 def main_ssh(args):
579 """Start a controller on localhost and engines using ssh.
603 """Start a controller on localhost and engines using ssh.
580
604
581 Your clusterfile should look like::
605 Your clusterfile should look like::
582
606
583 send_furl = False # True, if you want
607 send_furl = False # True, if you want
584 engines = {
608 engines = {
585 'engine_host1' : engine_count,
609 'engine_host1' : engine_count,
586 'engine_host2' : engine_count2
610 'engine_host2' : engine_count2
587 }
611 }
588 """
612 """
589 clusterfile = {}
613 clusterfile = {}
590 execfile(args.clusterfile, clusterfile)
614 execfile(args.clusterfile, clusterfile)
591 if not clusterfile.has_key('send_furl'):
615 if not clusterfile.has_key('send_furl'):
592 clusterfile['send_furl'] = False
616 clusterfile['send_furl'] = False
593
617
594 cont_args = []
618 cont_args = []
595 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
619 cont_args.append('--logfile=%s' % pjoin(args.logdir,'ipcontroller'))
596
620
597 # Check security settings before proceeding
621 # Check security settings before proceeding
598 if not check_security(args, cont_args):
622 if not check_security(args, cont_args):
599 return
623 return
600
624
625 # See if we are reusing FURL files
626 if not check_reuse(args, cont_args):
627 return
628
601 cl = ControllerLauncher(extra_args=cont_args)
629 cl = ControllerLauncher(extra_args=cont_args)
602 dstart = cl.start()
630 dstart = cl.start()
603 def start_engines(cont_pid):
631 def start_engines(cont_pid):
604 ssh_set = SSHEngineSet(clusterfile['engines'], sshx=args.sshx)
632 ssh_set = SSHEngineSet(clusterfile['engines'], sshx=args.sshx)
605 def shutdown(signum, frame):
633 def shutdown(signum, frame):
606 d = ssh_set.kill()
634 d = ssh_set.kill()
607 # d.addErrback(log.err)
608 cl.interrupt_then_kill(1.0)
635 cl.interrupt_then_kill(1.0)
609 reactor.callLater(2.0, reactor.stop)
636 reactor.callLater(2.0, reactor.stop)
610 signal.signal(signal.SIGINT,shutdown)
637 signal.signal(signal.SIGINT,shutdown)
611 d = ssh_set.start(clusterfile['send_furl'])
638 d = ssh_set.start(clusterfile['send_furl'])
612 return d
639 return d
613
640
614 def delay_start(cont_pid):
641 def delay_start(cont_pid):
615 reactor.callLater(1.0, start_engines, cont_pid)
642 reactor.callLater(1.0, start_engines, cont_pid)
616
643
617 dstart.addCallback(delay_start)
644 dstart.addCallback(delay_start)
618 dstart.addErrback(lambda f: f.raiseException())
645 dstart.addErrback(lambda f: f.raiseException())
619
646
620
647
621 def get_args():
648 def get_args():
622 base_parser = argparse.ArgumentParser(add_help=False)
649 base_parser = argparse.ArgumentParser(add_help=False)
623 base_parser.add_argument(
650 base_parser.add_argument(
651 '-r',
652 action='store_true',
653 dest='r',
654 help='try to reuse FURL files. Use with --client-port and --engine-port'
655 )
656 base_parser.add_argument(
657 '--client-port',
658 type=int,
659 dest='client_port',
660 help='the port the controller will listen on for client connections',
661 default=0
662 )
663 base_parser.add_argument(
664 '--engine-port',
665 type=int,
666 dest='engine_port',
667 help='the port the controller will listen on for engine connections',
668 default=0
669 )
670 base_parser.add_argument(
624 '-x',
671 '-x',
625 action='store_true',
672 action='store_true',
626 dest='x',
673 dest='x',
627 help='turn off client security'
674 help='turn off client security'
628 )
675 )
629 base_parser.add_argument(
676 base_parser.add_argument(
630 '-y',
677 '-y',
631 action='store_true',
678 action='store_true',
632 dest='y',
679 dest='y',
633 help='turn off engine security'
680 help='turn off engine security'
634 )
681 )
635 base_parser.add_argument(
682 base_parser.add_argument(
636 "--logdir",
683 "--logdir",
637 type=str,
684 type=str,
638 dest="logdir",
685 dest="logdir",
639 help="directory to put log files (default=$IPYTHONDIR/log)",
686 help="directory to put log files (default=$IPYTHONDIR/log)",
640 default=pjoin(get_ipython_dir(),'log')
687 default=pjoin(get_ipython_dir(),'log')
641 )
688 )
642 base_parser.add_argument(
689 base_parser.add_argument(
643 "-n",
690 "-n",
644 "--num",
691 "--num",
645 type=int,
692 type=int,
646 dest="n",
693 dest="n",
647 default=2,
694 default=2,
648 help="the number of engines to start"
695 help="the number of engines to start"
649 )
696 )
650
697
651 parser = argparse.ArgumentParser(
698 parser = argparse.ArgumentParser(
652 description='IPython cluster startup. This starts a controller and\
699 description='IPython cluster startup. This starts a controller and\
653 engines using various approaches. THIS IS A TECHNOLOGY PREVIEW AND\
700 engines using various approaches. THIS IS A TECHNOLOGY PREVIEW AND\
654 THE API WILL CHANGE SIGNIFICANTLY BEFORE THE FINAL RELEASE.'
701 THE API WILL CHANGE SIGNIFICANTLY BEFORE THE FINAL RELEASE.'
655 )
702 )
656 subparsers = parser.add_subparsers(
703 subparsers = parser.add_subparsers(
657 help='available cluster types. For help, do "ipcluster TYPE --help"')
704 help='available cluster types. For help, do "ipcluster TYPE --help"')
658
705
659 parser_local = subparsers.add_parser(
706 parser_local = subparsers.add_parser(
660 'local',
707 'local',
661 help='run a local cluster',
708 help='run a local cluster',
662 parents=[base_parser]
709 parents=[base_parser]
663 )
710 )
664 parser_local.set_defaults(func=main_local)
711 parser_local.set_defaults(func=main_local)
665
712
666 parser_mpirun = subparsers.add_parser(
713 parser_mpirun = subparsers.add_parser(
667 'mpirun',
714 'mpirun',
668 help='run a cluster using mpirun',
715 help='run a cluster using mpirun (mpiexec also works)',
669 parents=[base_parser]
716 parents=[base_parser]
670 )
717 )
671 parser_mpirun.add_argument(
718 parser_mpirun.add_argument(
672 "--mpi",
719 "--mpi",
673 type=str,
720 type=str,
674 dest="mpi", # Don't put a default here to allow no MPI support
721 dest="mpi", # Don't put a default here to allow no MPI support
675 help="how to call MPI_Init (default=mpi4py)"
722 help="how to call MPI_Init (default=mpi4py)"
676 )
723 )
677 parser_mpirun.set_defaults(func=main_mpirun)
724 parser_mpirun.set_defaults(func=main_mpi, cmd='mpirun')
725
726 parser_mpiexec = subparsers.add_parser(
727 'mpiexec',
728 help='run a cluster using mpiexec (mpirun also works)',
729 parents=[base_parser]
730 )
731 parser_mpiexec.add_argument(
732 "--mpi",
733 type=str,
734 dest="mpi", # Don't put a default here to allow no MPI support
735 help="how to call MPI_Init (default=mpi4py)"
736 )
737 parser_mpiexec.set_defaults(func=main_mpi, cmd='mpiexec')
678
738
679 parser_pbs = subparsers.add_parser(
739 parser_pbs = subparsers.add_parser(
680 'pbs',
740 'pbs',
681 help='run a pbs cluster',
741 help='run a pbs cluster',
682 parents=[base_parser]
742 parents=[base_parser]
683 )
743 )
684 parser_pbs.add_argument(
744 parser_pbs.add_argument(
685 '--pbs-script',
745 '--pbs-script',
686 type=str,
746 type=str,
687 dest='pbsscript',
747 dest='pbsscript',
688 help='PBS script template',
748 help='PBS script template',
689 default='pbs.template'
749 default='pbs.template'
690 )
750 )
691 parser_pbs.set_defaults(func=main_pbs)
751 parser_pbs.set_defaults(func=main_pbs)
692
752
693 parser_ssh = subparsers.add_parser(
753 parser_ssh = subparsers.add_parser(
694 'ssh',
754 'ssh',
695 help='run a cluster using ssh, should have ssh-keys setup',
755 help='run a cluster using ssh, should have ssh-keys setup',
696 parents=[base_parser]
756 parents=[base_parser]
697 )
757 )
698 parser_ssh.add_argument(
758 parser_ssh.add_argument(
699 '--clusterfile',
759 '--clusterfile',
700 type=str,
760 type=str,
701 dest='clusterfile',
761 dest='clusterfile',
702 help='python file describing the cluster',
762 help='python file describing the cluster',
703 default='clusterfile.py',
763 default='clusterfile.py',
704 )
764 )
705 parser_ssh.add_argument(
765 parser_ssh.add_argument(
706 '--sshx',
766 '--sshx',
707 type=str,
767 type=str,
708 dest='sshx',
768 dest='sshx',
709 help='sshx launcher helper'
769 help='sshx launcher helper'
710 )
770 )
711 parser_ssh.set_defaults(func=main_ssh)
771 parser_ssh.set_defaults(func=main_ssh)
712
772
713 args = parser.parse_args()
773 args = parser.parse_args()
714 return args
774 return args
715
775
716 def main():
776 def main():
717 args = get_args()
777 args = get_args()
718 reactor.callWhenRunning(args.func, args)
778 reactor.callWhenRunning(args.func, args)
719 log.startLogging(sys.stdout)
779 log.startLogging(sys.stdout)
720 reactor.run()
780 reactor.run()
721
781
722 if __name__ == '__main__':
782 if __name__ == '__main__':
723 main()
783 main()
@@ -1,98 +1,100 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """IPython Test Suite Runner.
2 """IPython Test Suite Runner.
3
3
4 This module provides a main entry point to a user script to test IPython itself
4 This module provides a main entry point to a user script to test IPython itself
5 from the command line. The main() routine can be used in a similar manner to
5 from the command line. The main() routine can be used in a similar manner to
6 the ``nosetests`` script, and it takes similar arguments, but if no arguments
6 the ``nosetests`` script, and it takes similar arguments, but if no arguments
7 are given it defaults to testing all of IPython. This should be preferred to
7 are given it defaults to testing all of IPython. This should be preferred to
8 using plain ``nosetests`` because a number of nose plugins necessary to test
8 using plain ``nosetests`` because a number of nose plugins necessary to test
9 IPython correctly are automatically configured by this code.
9 IPython correctly are automatically configured by this code.
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Module imports
13 # Module imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 # stdlib
16 # stdlib
17 import sys
17 import sys
18 import warnings
18 import warnings
19
19
20 # third-party
20 # third-party
21 import nose.plugins.builtin
21 import nose.plugins.builtin
22 from nose.core import TestProgram
22 from nose.core import TestProgram
23
23
24 # Our own imports
24 # Our own imports
25 from IPython.testing.plugin.ipdoctest import IPythonDoctest
25 from IPython.testing.plugin.ipdoctest import IPythonDoctest
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Constants and globals
28 # Constants and globals
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 # For the IPythonDoctest plugin, we need to exclude certain patterns that cause
31 # For the IPythonDoctest plugin, we need to exclude certain patterns that cause
32 # testing problems. We should strive to minimize the number of skipped
32 # testing problems. We should strive to minimize the number of skipped
33 # modules, since this means untested code. As the testing machinery
33 # modules, since this means untested code. As the testing machinery
34 # solidifies, this list should eventually become empty.
34 # solidifies, this list should eventually become empty.
35 EXCLUDE = ['IPython/external/',
35 EXCLUDE = ['IPython/external/',
36 'IPython/platutils_win32',
36 'IPython/platutils_win32',
37 'IPython/frontend/cocoa',
37 'IPython/frontend/cocoa',
38 'IPython_doctest_plugin',
38 'IPython_doctest_plugin',
39 'IPython/Gnuplot',
39 'IPython/Gnuplot',
40 'IPython/Extensions/ipy_',
40 'IPython/Extensions/ipy_',
41 'IPython/Extensions/clearcmd',
41 'IPython/Extensions/clearcmd',
42 'IPython/Extensions/PhysicalQIn',
42 'IPython/Extensions/PhysicalQIn',
43 'IPython/Extensions/scitedirector',
43 'IPython/Extensions/scitedirector',
44 ]
44 ]
45
45
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47 # Functions and classes
47 # Functions and classes
48 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
49
49
50 def main():
50 def main():
51 """Run the IPython test suite.
51 """Run the IPython test suite.
52 """
52 """
53
53
54 warnings.filterwarnings('ignore',
54 warnings.filterwarnings('ignore',
55 'This will be removed soon. Use IPython.testing.util instead')
55 'This will be removed soon. Use IPython.testing.util instead')
56
56
57 argv = sys.argv + [
57 argv = sys.argv + [
58 # Loading ipdoctest causes problems with Twisted.
58 # Loading ipdoctest causes problems with Twisted.
59 # I am removing this as a temporary fix to get the
59 # I am removing this as a temporary fix to get the
60 # test suite back into working shape. Our nose
60 # test suite back into working shape. Our nose
61 # plugin needs to be gone through with a fine
61 # plugin needs to be gone through with a fine
62 # toothed comb to find what is causing the problem.
62 # toothed comb to find what is causing the problem.
63 # '--with-ipdoctest',
63 # '--with-ipdoctest',
64 '--doctest-tests','--doctest-extension=txt',
64 '--ipdoctest-tests','--ipdoctest-extension=txt',
65 '--detailed-errors',
65 '--detailed-errors',
66
66
67 # We add --exe because of setuptools' imbecility (it
67 # We add --exe because of setuptools' imbecility (it
68 # blindly does chmod +x on ALL files). Nose does the
68 # blindly does chmod +x on ALL files). Nose does the
69 # right thing and it tries to avoid executables,
69 # right thing and it tries to avoid executables,
70 # setuptools unfortunately forces our hand here. This
70 # setuptools unfortunately forces our hand here. This
71 # has been discussed on the distutils list and the
71 # has been discussed on the distutils list and the
72 # setuptools devs refuse to fix this problem!
72 # setuptools devs refuse to fix this problem!
73 '--exe',
73 '--exe',
74 ]
74 ]
75
75
76 # Detect if any tests were required by explicitly calling an IPython
76 # Detect if any tests were required by explicitly calling an IPython
77 # submodule or giving a specific path
77 # submodule or giving a specific path
78 has_tests = False
78 has_tests = False
79 for arg in sys.argv:
79 for arg in sys.argv:
80 if 'IPython' in arg or arg.endswith('.py') or \
80 if 'IPython' in arg or arg.endswith('.py') or \
81 (':' in arg and '.py' in arg):
81 (':' in arg and '.py' in arg):
82 has_tests = True
82 has_tests = True
83 break
83 break
84
84 # If nothing was specifically requested, test full IPython
85 # If nothing was specifically requested, test full IPython
85 if not has_tests:
86 if not has_tests:
86 argv.append('IPython')
87 argv.append('IPython')
87
88
88 # Construct list of plugins, omitting the existing doctest plugin.
89 # Construct list of plugins, omitting the existing doctest plugin, which
90 # ours replaces (and extends).
89 plugins = [IPythonDoctest(EXCLUDE)]
91 plugins = [IPythonDoctest(EXCLUDE)]
90 for p in nose.plugins.builtin.plugins:
92 for p in nose.plugins.builtin.plugins:
91 plug = p()
93 plug = p()
92 if plug.name == 'doctest':
94 if plug.name == 'doctest':
93 continue
95 continue
94
96
95 #print '*** adding plugin:',plug.name # dbg
97 #print '*** adding plugin:',plug.name # dbg
96 plugins.append(plug)
98 plugins.append(plug)
97
99
98 TestProgram(argv=argv,plugins=plugins)
100 TestProgram(argv=argv,plugins=plugins)
@@ -1,850 +1,875 b''
1 """Nose Plugin that supports IPython doctests.
1 """Nose Plugin that supports IPython doctests.
2
2
3 Limitations:
3 Limitations:
4
4
5 - When generating examples for use as doctests, make sure that you have
5 - When generating examples for use as doctests, make sure that you have
6 pretty-printing OFF. This can be done either by starting ipython with the
6 pretty-printing OFF. This can be done either by starting ipython with the
7 flag '--nopprint', by setting pprint to 0 in your ipythonrc file, or by
7 flag '--nopprint', by setting pprint to 0 in your ipythonrc file, or by
8 interactively disabling it with %Pprint. This is required so that IPython
8 interactively disabling it with %Pprint. This is required so that IPython
9 output matches that of normal Python, which is used by doctest for internal
9 output matches that of normal Python, which is used by doctest for internal
10 execution.
10 execution.
11
11
12 - Do not rely on specific prompt numbers for results (such as using
12 - Do not rely on specific prompt numbers for results (such as using
13 '_34==True', for example). For IPython tests run via an external process the
13 '_34==True', for example). For IPython tests run via an external process the
14 prompt numbers may be different, and IPython tests run as normal python code
14 prompt numbers may be different, and IPython tests run as normal python code
15 won't even have these special _NN variables set at all.
15 won't even have these special _NN variables set at all.
16 """
16 """
17
17
18
19 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
20 # Module imports
19 # Module imports
21
20
22 # From the standard library
21 # From the standard library
23 import __builtin__
22 import __builtin__
24 import commands
23 import commands
25 import doctest
24 import doctest
26 import inspect
25 import inspect
27 import logging
26 import logging
28 import os
27 import os
29 import re
28 import re
30 import sys
29 import sys
31 import traceback
30 import traceback
32 import unittest
31 import unittest
33
32
34 from inspect import getmodule
33 from inspect import getmodule
35 from StringIO import StringIO
34 from StringIO import StringIO
36
35
37 # We are overriding the default doctest runner, so we need to import a few
36 # We are overriding the default doctest runner, so we need to import a few
38 # things from doctest directly
37 # things from doctest directly
39 from doctest import (REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE,
38 from doctest import (REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE,
40 _unittest_reportflags, DocTestRunner,
39 _unittest_reportflags, DocTestRunner,
41 _extract_future_flags, pdb, _OutputRedirectingPdb,
40 _extract_future_flags, pdb, _OutputRedirectingPdb,
42 _exception_traceback,
41 _exception_traceback,
43 linecache)
42 linecache)
44
43
45 # Third-party modules
44 # Third-party modules
46 import nose.core
45 import nose.core
47
46
48 from nose.plugins import doctests, Plugin
47 from nose.plugins import doctests, Plugin
49 from nose.util import anyp, getpackage, test_address, resolve_name, tolist
48 from nose.util import anyp, getpackage, test_address, resolve_name, tolist
50
49
51 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
52 # Module globals and other constants
51 # Module globals and other constants
53
52
54 log = logging.getLogger(__name__)
53 log = logging.getLogger(__name__)
55
54
56 ###########################################################################
55 ###########################################################################
57 # *** HACK ***
56 # *** HACK ***
58 # We must start our own ipython object and heavily muck with it so that all the
57 # We must start our own ipython object and heavily muck with it so that all the
59 # modifications IPython makes to system behavior don't send the doctest
58 # modifications IPython makes to system behavior don't send the doctest
60 # machinery into a fit. This code should be considered a gross hack, but it
59 # machinery into a fit. This code should be considered a gross hack, but it
61 # gets the job done.
60 # gets the job done.
62
61
63
62
64 # Hack to modify the %run command so we can sync the user's namespace with the
63 # Hack to modify the %run command so we can sync the user's namespace with the
65 # test globals. Once we move over to a clean magic system, this will be done
64 # test globals. Once we move over to a clean magic system, this will be done
66 # with much less ugliness.
65 # with much less ugliness.
67
66
68 class py_file_finder(object):
67 class py_file_finder(object):
69 def __init__(self,test_filename):
68 def __init__(self,test_filename):
70 self.test_filename = test_filename
69 self.test_filename = test_filename
71
70
72 def __call__(self,name):
71 def __call__(self,name):
73 from IPython.genutils import get_py_filename
72 from IPython.genutils import get_py_filename
74 try:
73 try:
75 return get_py_filename(name)
74 return get_py_filename(name)
76 except IOError:
75 except IOError:
77 test_dir = os.path.dirname(self.test_filename)
76 test_dir = os.path.dirname(self.test_filename)
78 new_path = os.path.join(test_dir,name)
77 new_path = os.path.join(test_dir,name)
79 return get_py_filename(new_path)
78 return get_py_filename(new_path)
80
79
81
80
82 def _run_ns_sync(self,arg_s,runner=None):
81 def _run_ns_sync(self,arg_s,runner=None):
83 """Modified version of %run that syncs testing namespaces.
82 """Modified version of %run that syncs testing namespaces.
84
83
85 This is strictly needed for running doctests that call %run.
84 This is strictly needed for running doctests that call %run.
86 """
85 """
87
86
88 finder = py_file_finder(_run_ns_sync.test_filename)
87 finder = py_file_finder(_run_ns_sync.test_filename)
89 out = _ip.IP.magic_run_ori(arg_s,runner,finder)
88 out = _ip.IP.magic_run_ori(arg_s,runner,finder)
90 _run_ns_sync.test_globs.update(_ip.user_ns)
89 _run_ns_sync.test_globs.update(_ip.user_ns)
91 return out
90 return out
92
91
93
92
94 class ipnsdict(dict):
93 class ipnsdict(dict):
95 """A special subclass of dict for use as an IPython namespace in doctests.
94 """A special subclass of dict for use as an IPython namespace in doctests.
96
95
97 This subclass adds a simple checkpointing capability so that when testing
96 This subclass adds a simple checkpointing capability so that when testing
98 machinery clears it (we use it as the test execution context), it doesn't
97 machinery clears it (we use it as the test execution context), it doesn't
99 get completely destroyed.
98 get completely destroyed.
100 """
99 """
101
100
102 def __init__(self,*a):
101 def __init__(self,*a):
103 dict.__init__(self,*a)
102 dict.__init__(self,*a)
104 self._savedict = {}
103 self._savedict = {}
105
104
106 def clear(self):
105 def clear(self):
107 dict.clear(self)
106 dict.clear(self)
108 self.update(self._savedict)
107 self.update(self._savedict)
109
108
110 def _checkpoint(self):
109 def _checkpoint(self):
111 self._savedict.clear()
110 self._savedict.clear()
112 self._savedict.update(self)
111 self._savedict.update(self)
113
112
114 def update(self,other):
113 def update(self,other):
115 self._checkpoint()
114 self._checkpoint()
116 dict.update(self,other)
115 dict.update(self,other)
117 # If '_' is in the namespace, python won't set it when executing code,
116 # If '_' is in the namespace, python won't set it when executing code,
118 # and we have examples that test it. So we ensure that the namespace
117 # and we have examples that test it. So we ensure that the namespace
119 # is always 'clean' of it before it's used for test code execution.
118 # is always 'clean' of it before it's used for test code execution.
120 self.pop('_',None)
119 self.pop('_',None)
121
120
122
121
123 def start_ipython():
122 def start_ipython():
124 """Start a global IPython shell, which we need for IPython-specific syntax.
123 """Start a global IPython shell, which we need for IPython-specific syntax.
125 """
124 """
125
126 # This function should only ever run once!
127 if hasattr(start_ipython,'already_called'):
128 return
129 start_ipython.already_called = True
130
131 # Ok, first time we're called, go ahead
126 import new
132 import new
127
133
128 import IPython
134 import IPython
129
135
130 def xsys(cmd):
136 def xsys(cmd):
131 """Execute a command and print its output.
137 """Execute a command and print its output.
132
138
133 This is just a convenience function to replace the IPython system call
139 This is just a convenience function to replace the IPython system call
134 with one that is more doctest-friendly.
140 with one that is more doctest-friendly.
135 """
141 """
136 cmd = _ip.IP.var_expand(cmd,depth=1)
142 cmd = _ip.IP.var_expand(cmd,depth=1)
137 sys.stdout.write(commands.getoutput(cmd))
143 sys.stdout.write(commands.getoutput(cmd))
138 sys.stdout.flush()
144 sys.stdout.flush()
139
145
140 # Store certain global objects that IPython modifies
146 # Store certain global objects that IPython modifies
141 _displayhook = sys.displayhook
147 _displayhook = sys.displayhook
142 _excepthook = sys.excepthook
148 _excepthook = sys.excepthook
143 _main = sys.modules.get('__main__')
149 _main = sys.modules.get('__main__')
144
150
145 # Start IPython instance. We customize it to start with minimal frills.
151 # Start IPython instance. We customize it to start with minimal frills.
146 user_ns,global_ns = IPython.ipapi.make_user_namespaces(ipnsdict(),dict())
152 user_ns,global_ns = IPython.ipapi.make_user_namespaces(ipnsdict(),dict())
147 IPython.Shell.IPShell(['--colors=NoColor','--noterm_title'],
153 IPython.Shell.IPShell(['--colors=NoColor','--noterm_title'],
148 user_ns,global_ns)
154 user_ns,global_ns)
149
155
150 # Deactivate the various python system hooks added by ipython for
156 # Deactivate the various python system hooks added by ipython for
151 # interactive convenience so we don't confuse the doctest system
157 # interactive convenience so we don't confuse the doctest system
152 sys.modules['__main__'] = _main
158 sys.modules['__main__'] = _main
153 sys.displayhook = _displayhook
159 sys.displayhook = _displayhook
154 sys.excepthook = _excepthook
160 sys.excepthook = _excepthook
155
161
156 # So that ipython magics and aliases can be doctested (they work by making
162 # So that ipython magics and aliases can be doctested (they work by making
157 # a call into a global _ip object)
163 # a call into a global _ip object)
158 _ip = IPython.ipapi.get()
164 _ip = IPython.ipapi.get()
159 __builtin__._ip = _ip
165 __builtin__._ip = _ip
160
166
161 # Modify the IPython system call with one that uses getoutput, so that we
167 # Modify the IPython system call with one that uses getoutput, so that we
162 # can capture subcommands and print them to Python's stdout, otherwise the
168 # can capture subcommands and print them to Python's stdout, otherwise the
163 # doctest machinery would miss them.
169 # doctest machinery would miss them.
164 _ip.system = xsys
170 _ip.system = xsys
165
171
166 # Also patch our %run function in.
172 # Also patch our %run function in.
167 im = new.instancemethod(_run_ns_sync,_ip.IP, _ip.IP.__class__)
173 im = new.instancemethod(_run_ns_sync,_ip.IP, _ip.IP.__class__)
168 _ip.IP.magic_run_ori = _ip.IP.magic_run
174 _ip.IP.magic_run_ori = _ip.IP.magic_run
169 _ip.IP.magic_run = im
175 _ip.IP.magic_run = im
170
176
171 # The start call MUST be made here. I'm not sure yet why it doesn't work if
177 # The start call MUST be made here. I'm not sure yet why it doesn't work if
172 # it is made later, at plugin initialization time, but in all my tests, that's
178 # it is made later, at plugin initialization time, but in all my tests, that's
173 # the case.
179 # the case.
174 start_ipython()
180 start_ipython()
175
181
176 # *** END HACK ***
182 # *** END HACK ***
177 ###########################################################################
183 ###########################################################################
178
184
179 # Classes and functions
185 # Classes and functions
180
186
181 def is_extension_module(filename):
187 def is_extension_module(filename):
182 """Return whether the given filename is an extension module.
188 """Return whether the given filename is an extension module.
183
189
184 This simply checks that the extension is either .so or .pyd.
190 This simply checks that the extension is either .so or .pyd.
185 """
191 """
186 return os.path.splitext(filename)[1].lower() in ('.so','.pyd')
192 return os.path.splitext(filename)[1].lower() in ('.so','.pyd')
187
193
188
194
189 class DocTestSkip(object):
195 class DocTestSkip(object):
190 """Object wrapper for doctests to be skipped."""
196 """Object wrapper for doctests to be skipped."""
191
197
192 ds_skip = """Doctest to skip.
198 ds_skip = """Doctest to skip.
193 >>> 1 #doctest: +SKIP
199 >>> 1 #doctest: +SKIP
194 """
200 """
195
201
196 def __init__(self,obj):
202 def __init__(self,obj):
197 self.obj = obj
203 self.obj = obj
198
204
199 def __getattribute__(self,key):
205 def __getattribute__(self,key):
200 if key == '__doc__':
206 if key == '__doc__':
201 return DocTestSkip.ds_skip
207 return DocTestSkip.ds_skip
202 else:
208 else:
203 return getattr(object.__getattribute__(self,'obj'),key)
209 return getattr(object.__getattribute__(self,'obj'),key)
204
210
205 # Modified version of the one in the stdlib, that fixes a python bug (doctests
211 # Modified version of the one in the stdlib, that fixes a python bug (doctests
206 # not found in extension modules, http://bugs.python.org/issue3158)
212 # not found in extension modules, http://bugs.python.org/issue3158)
207 class DocTestFinder(doctest.DocTestFinder):
213 class DocTestFinder(doctest.DocTestFinder):
208
214
209 def _from_module(self, module, object):
215 def _from_module(self, module, object):
210 """
216 """
211 Return true if the given object is defined in the given
217 Return true if the given object is defined in the given
212 module.
218 module.
213 """
219 """
214 if module is None:
220 if module is None:
215 return True
221 return True
216 elif inspect.isfunction(object):
222 elif inspect.isfunction(object):
217 return module.__dict__ is object.func_globals
223 return module.__dict__ is object.func_globals
218 elif inspect.isbuiltin(object):
224 elif inspect.isbuiltin(object):
219 return module.__name__ == object.__module__
225 return module.__name__ == object.__module__
220 elif inspect.isclass(object):
226 elif inspect.isclass(object):
221 return module.__name__ == object.__module__
227 return module.__name__ == object.__module__
222 elif inspect.ismethod(object):
228 elif inspect.ismethod(object):
223 # This one may be a bug in cython that fails to correctly set the
229 # This one may be a bug in cython that fails to correctly set the
224 # __module__ attribute of methods, but since the same error is easy
230 # __module__ attribute of methods, but since the same error is easy
225 # to make by extension code writers, having this safety in place
231 # to make by extension code writers, having this safety in place
226 # isn't such a bad idea
232 # isn't such a bad idea
227 return module.__name__ == object.im_class.__module__
233 return module.__name__ == object.im_class.__module__
228 elif inspect.getmodule(object) is not None:
234 elif inspect.getmodule(object) is not None:
229 return module is inspect.getmodule(object)
235 return module is inspect.getmodule(object)
230 elif hasattr(object, '__module__'):
236 elif hasattr(object, '__module__'):
231 return module.__name__ == object.__module__
237 return module.__name__ == object.__module__
232 elif isinstance(object, property):
238 elif isinstance(object, property):
233 return True # [XX] no way not be sure.
239 return True # [XX] no way not be sure.
234 else:
240 else:
235 raise ValueError("object must be a class or function")
241 raise ValueError("object must be a class or function")
236
242
237 def _find(self, tests, obj, name, module, source_lines, globs, seen):
243 def _find(self, tests, obj, name, module, source_lines, globs, seen):
238 """
244 """
239 Find tests for the given object and any contained objects, and
245 Find tests for the given object and any contained objects, and
240 add them to `tests`.
246 add them to `tests`.
241 """
247 """
242
248
243 if hasattr(obj,"skip_doctest"):
249 if hasattr(obj,"skip_doctest"):
244 #print 'SKIPPING DOCTEST FOR:',obj # dbg
250 #print 'SKIPPING DOCTEST FOR:',obj # dbg
245 obj = DocTestSkip(obj)
251 obj = DocTestSkip(obj)
246
252
247 doctest.DocTestFinder._find(self,tests, obj, name, module,
253 doctest.DocTestFinder._find(self,tests, obj, name, module,
248 source_lines, globs, seen)
254 source_lines, globs, seen)
249
255
250 # Below we re-run pieces of the above method with manual modifications,
256 # Below we re-run pieces of the above method with manual modifications,
251 # because the original code is buggy and fails to correctly identify
257 # because the original code is buggy and fails to correctly identify
252 # doctests in extension modules.
258 # doctests in extension modules.
253
259
254 # Local shorthands
260 # Local shorthands
255 from inspect import isroutine, isclass, ismodule
261 from inspect import isroutine, isclass, ismodule
256
262
257 # Look for tests in a module's contained objects.
263 # Look for tests in a module's contained objects.
258 if inspect.ismodule(obj) and self._recurse:
264 if inspect.ismodule(obj) and self._recurse:
259 for valname, val in obj.__dict__.items():
265 for valname, val in obj.__dict__.items():
260 valname1 = '%s.%s' % (name, valname)
266 valname1 = '%s.%s' % (name, valname)
261 if ( (isroutine(val) or isclass(val))
267 if ( (isroutine(val) or isclass(val))
262 and self._from_module(module, val) ):
268 and self._from_module(module, val) ):
263
269
264 self._find(tests, val, valname1, module, source_lines,
270 self._find(tests, val, valname1, module, source_lines,
265 globs, seen)
271 globs, seen)
266
272
267 # Look for tests in a class's contained objects.
273 # Look for tests in a class's contained objects.
268 if inspect.isclass(obj) and self._recurse:
274 if inspect.isclass(obj) and self._recurse:
269 #print 'RECURSE into class:',obj # dbg
275 #print 'RECURSE into class:',obj # dbg
270 for valname, val in obj.__dict__.items():
276 for valname, val in obj.__dict__.items():
271 # Special handling for staticmethod/classmethod.
277 # Special handling for staticmethod/classmethod.
272 if isinstance(val, staticmethod):
278 if isinstance(val, staticmethod):
273 val = getattr(obj, valname)
279 val = getattr(obj, valname)
274 if isinstance(val, classmethod):
280 if isinstance(val, classmethod):
275 val = getattr(obj, valname).im_func
281 val = getattr(obj, valname).im_func
276
282
277 # Recurse to methods, properties, and nested classes.
283 # Recurse to methods, properties, and nested classes.
278 if ((inspect.isfunction(val) or inspect.isclass(val) or
284 if ((inspect.isfunction(val) or inspect.isclass(val) or
279 inspect.ismethod(val) or
285 inspect.ismethod(val) or
280 isinstance(val, property)) and
286 isinstance(val, property)) and
281 self._from_module(module, val)):
287 self._from_module(module, val)):
282 valname = '%s.%s' % (name, valname)
288 valname = '%s.%s' % (name, valname)
283 self._find(tests, val, valname, module, source_lines,
289 self._find(tests, val, valname, module, source_lines,
284 globs, seen)
290 globs, seen)
285
291
286
292
287 class IPDoctestOutputChecker(doctest.OutputChecker):
293 class IPDoctestOutputChecker(doctest.OutputChecker):
288 """Second-chance checker with support for random tests.
294 """Second-chance checker with support for random tests.
289
295
290 If the default comparison doesn't pass, this checker looks in the expected
296 If the default comparison doesn't pass, this checker looks in the expected
291 output string for flags that tell us to ignore the output.
297 output string for flags that tell us to ignore the output.
292 """
298 """
293
299
294 random_re = re.compile(r'#\s*random\s+')
300 random_re = re.compile(r'#\s*random\s+')
295
301
296 def check_output(self, want, got, optionflags):
302 def check_output(self, want, got, optionflags):
297 """Check output, accepting special markers embedded in the output.
303 """Check output, accepting special markers embedded in the output.
298
304
299 If the output didn't pass the default validation but the special string
305 If the output didn't pass the default validation but the special string
300 '#random' is included, we accept it."""
306 '#random' is included, we accept it."""
301
307
302 # Let the original tester verify first, in case people have valid tests
308 # Let the original tester verify first, in case people have valid tests
303 # that happen to have a comment saying '#random' embedded in.
309 # that happen to have a comment saying '#random' embedded in.
304 ret = doctest.OutputChecker.check_output(self, want, got,
310 ret = doctest.OutputChecker.check_output(self, want, got,
305 optionflags)
311 optionflags)
306 if not ret and self.random_re.search(want):
312 if not ret and self.random_re.search(want):
307 #print >> sys.stderr, 'RANDOM OK:',want # dbg
313 #print >> sys.stderr, 'RANDOM OK:',want # dbg
308 return True
314 return True
309
315
310 return ret
316 return ret
311
317
312
318
313 class DocTestCase(doctests.DocTestCase):
319 class DocTestCase(doctests.DocTestCase):
314 """Proxy for DocTestCase: provides an address() method that
320 """Proxy for DocTestCase: provides an address() method that
315 returns the correct address for the doctest case. Otherwise
321 returns the correct address for the doctest case. Otherwise
316 acts as a proxy to the test case. To provide hints for address(),
322 acts as a proxy to the test case. To provide hints for address(),
317 an obj may also be passed -- this will be used as the test object
323 an obj may also be passed -- this will be used as the test object
318 for purposes of determining the test address, if it is provided.
324 for purposes of determining the test address, if it is provided.
319 """
325 """
320
326
321 # Note: this method was taken from numpy's nosetester module.
327 # Note: this method was taken from numpy's nosetester module.
322
328
323 # Subclass nose.plugins.doctests.DocTestCase to work around a bug in
329 # Subclass nose.plugins.doctests.DocTestCase to work around a bug in
324 # its constructor that blocks non-default arguments from being passed
330 # its constructor that blocks non-default arguments from being passed
325 # down into doctest.DocTestCase
331 # down into doctest.DocTestCase
326
332
327 def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
333 def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
328 checker=None, obj=None, result_var='_'):
334 checker=None, obj=None, result_var='_'):
329 self._result_var = result_var
335 self._result_var = result_var
330 doctests.DocTestCase.__init__(self, test,
336 doctests.DocTestCase.__init__(self, test,
331 optionflags=optionflags,
337 optionflags=optionflags,
332 setUp=setUp, tearDown=tearDown,
338 setUp=setUp, tearDown=tearDown,
333 checker=checker)
339 checker=checker)
334 # Now we must actually copy the original constructor from the stdlib
340 # Now we must actually copy the original constructor from the stdlib
335 # doctest class, because we can't call it directly and a bug in nose
341 # doctest class, because we can't call it directly and a bug in nose
336 # means it never gets passed the right arguments.
342 # means it never gets passed the right arguments.
337
343
338 self._dt_optionflags = optionflags
344 self._dt_optionflags = optionflags
339 self._dt_checker = checker
345 self._dt_checker = checker
340 self._dt_test = test
346 self._dt_test = test
341 self._dt_setUp = setUp
347 self._dt_setUp = setUp
342 self._dt_tearDown = tearDown
348 self._dt_tearDown = tearDown
343
349
344 # XXX - store this runner once in the object!
350 # XXX - store this runner once in the object!
345 runner = IPDocTestRunner(optionflags=optionflags,
351 runner = IPDocTestRunner(optionflags=optionflags,
346 checker=checker, verbose=False)
352 checker=checker, verbose=False)
347 self._dt_runner = runner
353 self._dt_runner = runner
348
354
349
355
350 # Each doctest should remember what directory it was loaded from...
356 # Each doctest should remember what directory it was loaded from...
351 self._ori_dir = os.getcwd()
357 self._ori_dir = os.getcwd()
352
358
353 # Modified runTest from the default stdlib
359 # Modified runTest from the default stdlib
354 def runTest(self):
360 def runTest(self):
355 test = self._dt_test
361 test = self._dt_test
356 runner = self._dt_runner
362 runner = self._dt_runner
357
363
358 old = sys.stdout
364 old = sys.stdout
359 new = StringIO()
365 new = StringIO()
360 optionflags = self._dt_optionflags
366 optionflags = self._dt_optionflags
361
367
362 if not (optionflags & REPORTING_FLAGS):
368 if not (optionflags & REPORTING_FLAGS):
363 # The option flags don't include any reporting flags,
369 # The option flags don't include any reporting flags,
364 # so add the default reporting flags
370 # so add the default reporting flags
365 optionflags |= _unittest_reportflags
371 optionflags |= _unittest_reportflags
366
372
367 try:
373 try:
368 # Save our current directory and switch out to the one where the
374 # Save our current directory and switch out to the one where the
369 # test was originally created, in case another doctest did a
375 # test was originally created, in case another doctest did a
370 # directory change. We'll restore this in the finally clause.
376 # directory change. We'll restore this in the finally clause.
371 curdir = os.getcwd()
377 curdir = os.getcwd()
372 os.chdir(self._ori_dir)
378 os.chdir(self._ori_dir)
373
379
374 runner.DIVIDER = "-"*70
380 runner.DIVIDER = "-"*70
375 failures, tries = runner.run(test,out=new.write,
381 failures, tries = runner.run(test,out=new.write,
376 clear_globs=False)
382 clear_globs=False)
377 finally:
383 finally:
378 sys.stdout = old
384 sys.stdout = old
379 os.chdir(curdir)
385 os.chdir(curdir)
380
386
381 if failures:
387 if failures:
382 raise self.failureException(self.format_failure(new.getvalue()))
388 raise self.failureException(self.format_failure(new.getvalue()))
383
389
384 def setUp(self):
390 def setUp(self):
385 """Modified test setup that syncs with ipython namespace"""
391 """Modified test setup that syncs with ipython namespace"""
386
392
387 if isinstance(self._dt_test.examples[0],IPExample):
393 if isinstance(self._dt_test.examples[0],IPExample):
388 # for IPython examples *only*, we swap the globals with the ipython
394 # for IPython examples *only*, we swap the globals with the ipython
389 # namespace, after updating it with the globals (which doctest
395 # namespace, after updating it with the globals (which doctest
390 # fills with the necessary info from the module being tested).
396 # fills with the necessary info from the module being tested).
391 _ip.IP.user_ns.update(self._dt_test.globs)
397 _ip.IP.user_ns.update(self._dt_test.globs)
392 self._dt_test.globs = _ip.IP.user_ns
398 self._dt_test.globs = _ip.IP.user_ns
393
399
394 doctests.DocTestCase.setUp(self)
400 doctests.DocTestCase.setUp(self)
395
401
396
402
397 # A simple subclassing of the original with a different class name, so we can
403 # A simple subclassing of the original with a different class name, so we can
398 # distinguish and treat differently IPython examples from pure python ones.
404 # distinguish and treat differently IPython examples from pure python ones.
399 class IPExample(doctest.Example): pass
405 class IPExample(doctest.Example): pass
400
406
401
407
402 class IPExternalExample(doctest.Example):
408 class IPExternalExample(doctest.Example):
403 """Doctest examples to be run in an external process."""
409 """Doctest examples to be run in an external process."""
404
410
405 def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
411 def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
406 options=None):
412 options=None):
407 # Parent constructor
413 # Parent constructor
408 doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options)
414 doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options)
409
415
410 # An EXTRA newline is needed to prevent pexpect hangs
416 # An EXTRA newline is needed to prevent pexpect hangs
411 self.source += '\n'
417 self.source += '\n'
412
418
413
419
414 class IPDocTestParser(doctest.DocTestParser):
420 class IPDocTestParser(doctest.DocTestParser):
415 """
421 """
416 A class used to parse strings containing doctest examples.
422 A class used to parse strings containing doctest examples.
417
423
418 Note: This is a version modified to properly recognize IPython input and
424 Note: This is a version modified to properly recognize IPython input and
419 convert any IPython examples into valid Python ones.
425 convert any IPython examples into valid Python ones.
420 """
426 """
421 # This regular expression is used to find doctest examples in a
427 # This regular expression is used to find doctest examples in a
422 # string. It defines three groups: `source` is the source code
428 # string. It defines three groups: `source` is the source code
423 # (including leading indentation and prompts); `indent` is the
429 # (including leading indentation and prompts); `indent` is the
424 # indentation of the first (PS1) line of the source code; and
430 # indentation of the first (PS1) line of the source code; and
425 # `want` is the expected output (including leading indentation).
431 # `want` is the expected output (including leading indentation).
426
432
427 # Classic Python prompts or default IPython ones
433 # Classic Python prompts or default IPython ones
428 _PS1_PY = r'>>>'
434 _PS1_PY = r'>>>'
429 _PS2_PY = r'\.\.\.'
435 _PS2_PY = r'\.\.\.'
430
436
431 _PS1_IP = r'In\ \[\d+\]:'
437 _PS1_IP = r'In\ \[\d+\]:'
432 _PS2_IP = r'\ \ \ \.\.\.+:'
438 _PS2_IP = r'\ \ \ \.\.\.+:'
433
439
434 _RE_TPL = r'''
440 _RE_TPL = r'''
435 # Source consists of a PS1 line followed by zero or more PS2 lines.
441 # Source consists of a PS1 line followed by zero or more PS2 lines.
436 (?P<source>
442 (?P<source>
437 (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line
443 (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line
438 (?:\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines
444 (?:\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines
439 \n? # a newline
445 \n? # a newline
440 # Want consists of any non-blank lines that do not start with PS1.
446 # Want consists of any non-blank lines that do not start with PS1.
441 (?P<want> (?:(?![ ]*$) # Not a blank line
447 (?P<want> (?:(?![ ]*$) # Not a blank line
442 (?![ ]*%s) # Not a line starting with PS1
448 (?![ ]*%s) # Not a line starting with PS1
443 (?![ ]*%s) # Not a line starting with PS2
449 (?![ ]*%s) # Not a line starting with PS2
444 .*$\n? # But any other line
450 .*$\n? # But any other line
445 )*)
451 )*)
446 '''
452 '''
447
453
448 _EXAMPLE_RE_PY = re.compile( _RE_TPL % (_PS1_PY,_PS2_PY,_PS1_PY,_PS2_PY),
454 _EXAMPLE_RE_PY = re.compile( _RE_TPL % (_PS1_PY,_PS2_PY,_PS1_PY,_PS2_PY),
449 re.MULTILINE | re.VERBOSE)
455 re.MULTILINE | re.VERBOSE)
450
456
451 _EXAMPLE_RE_IP = re.compile( _RE_TPL % (_PS1_IP,_PS2_IP,_PS1_IP,_PS2_IP),
457 _EXAMPLE_RE_IP = re.compile( _RE_TPL % (_PS1_IP,_PS2_IP,_PS1_IP,_PS2_IP),
452 re.MULTILINE | re.VERBOSE)
458 re.MULTILINE | re.VERBOSE)
453
459
454 # Mark a test as being fully random. In this case, we simply append the
460 # Mark a test as being fully random. In this case, we simply append the
455 # random marker ('#random') to each individual example's output. This way
461 # random marker ('#random') to each individual example's output. This way
456 # we don't need to modify any other code.
462 # we don't need to modify any other code.
457 _RANDOM_TEST = re.compile(r'#\s*all-random\s+')
463 _RANDOM_TEST = re.compile(r'#\s*all-random\s+')
458
464
459 # Mark tests to be executed in an external process - currently unsupported.
465 # Mark tests to be executed in an external process - currently unsupported.
460 _EXTERNAL_IP = re.compile(r'#\s*ipdoctest:\s*EXTERNAL')
466 _EXTERNAL_IP = re.compile(r'#\s*ipdoctest:\s*EXTERNAL')
461
467
462 def ip2py(self,source):
468 def ip2py(self,source):
463 """Convert input IPython source into valid Python."""
469 """Convert input IPython source into valid Python."""
464 out = []
470 out = []
465 newline = out.append
471 newline = out.append
466 #print 'IPSRC:\n',source,'\n###' # dbg
472 #print 'IPSRC:\n',source,'\n###' # dbg
467 # The input source must be first stripped of all bracketing whitespace
473 # The input source must be first stripped of all bracketing whitespace
468 # and turned into lines, so it looks to the parser like regular user
474 # and turned into lines, so it looks to the parser like regular user
469 # input
475 # input
470 for lnum,line in enumerate(source.strip().splitlines()):
476 for lnum,line in enumerate(source.strip().splitlines()):
471 newline(_ip.IP.prefilter(line,lnum>0))
477 newline(_ip.IP.prefilter(line,lnum>0))
472 newline('') # ensure a closing newline, needed by doctest
478 newline('') # ensure a closing newline, needed by doctest
473 #print "PYSRC:", '\n'.join(out) # dbg
479 #print "PYSRC:", '\n'.join(out) # dbg
474 return '\n'.join(out)
480 return '\n'.join(out)
475
481
476 def parse(self, string, name='<string>'):
482 def parse(self, string, name='<string>'):
477 """
483 """
478 Divide the given string into examples and intervening text,
484 Divide the given string into examples and intervening text,
479 and return them as a list of alternating Examples and strings.
485 and return them as a list of alternating Examples and strings.
480 Line numbers for the Examples are 0-based. The optional
486 Line numbers for the Examples are 0-based. The optional
481 argument `name` is a name identifying this string, and is only
487 argument `name` is a name identifying this string, and is only
482 used for error messages.
488 used for error messages.
483 """
489 """
484
490
485 #print 'Parse string:\n',string # dbg
491 #print 'Parse string:\n',string # dbg
486
492
487 string = string.expandtabs()
493 string = string.expandtabs()
488 # If all lines begin with the same indentation, then strip it.
494 # If all lines begin with the same indentation, then strip it.
489 min_indent = self._min_indent(string)
495 min_indent = self._min_indent(string)
490 if min_indent > 0:
496 if min_indent > 0:
491 string = '\n'.join([l[min_indent:] for l in string.split('\n')])
497 string = '\n'.join([l[min_indent:] for l in string.split('\n')])
492
498
493 output = []
499 output = []
494 charno, lineno = 0, 0
500 charno, lineno = 0, 0
495
501
496 # We make 'all random' tests by adding the '# random' mark to every
502 # We make 'all random' tests by adding the '# random' mark to every
497 # block of output in the test.
503 # block of output in the test.
498 if self._RANDOM_TEST.search(string):
504 if self._RANDOM_TEST.search(string):
499 random_marker = '\n# random'
505 random_marker = '\n# random'
500 else:
506 else:
501 random_marker = ''
507 random_marker = ''
502
508
503 # Whether to convert the input from ipython to python syntax
509 # Whether to convert the input from ipython to python syntax
504 ip2py = False
510 ip2py = False
505 # Find all doctest examples in the string. First, try them as Python
511 # Find all doctest examples in the string. First, try them as Python
506 # examples, then as IPython ones
512 # examples, then as IPython ones
507 terms = list(self._EXAMPLE_RE_PY.finditer(string))
513 terms = list(self._EXAMPLE_RE_PY.finditer(string))
508 if terms:
514 if terms:
509 # Normal Python example
515 # Normal Python example
510 #print '-'*70 # dbg
516 #print '-'*70 # dbg
511 #print 'PyExample, Source:\n',string # dbg
517 #print 'PyExample, Source:\n',string # dbg
512 #print '-'*70 # dbg
518 #print '-'*70 # dbg
513 Example = doctest.Example
519 Example = doctest.Example
514 else:
520 else:
515 # It's an ipython example. Note that IPExamples are run
521 # It's an ipython example. Note that IPExamples are run
516 # in-process, so their syntax must be turned into valid python.
522 # in-process, so their syntax must be turned into valid python.
517 # IPExternalExamples are run out-of-process (via pexpect) so they
523 # IPExternalExamples are run out-of-process (via pexpect) so they
518 # don't need any filtering (a real ipython will be executing them).
524 # don't need any filtering (a real ipython will be executing them).
519 terms = list(self._EXAMPLE_RE_IP.finditer(string))
525 terms = list(self._EXAMPLE_RE_IP.finditer(string))
520 if self._EXTERNAL_IP.search(string):
526 if self._EXTERNAL_IP.search(string):
521 #print '-'*70 # dbg
527 #print '-'*70 # dbg
522 #print 'IPExternalExample, Source:\n',string # dbg
528 #print 'IPExternalExample, Source:\n',string # dbg
523 #print '-'*70 # dbg
529 #print '-'*70 # dbg
524 Example = IPExternalExample
530 Example = IPExternalExample
525 else:
531 else:
526 #print '-'*70 # dbg
532 #print '-'*70 # dbg
527 #print 'IPExample, Source:\n',string # dbg
533 #print 'IPExample, Source:\n',string # dbg
528 #print '-'*70 # dbg
534 #print '-'*70 # dbg
529 Example = IPExample
535 Example = IPExample
530 ip2py = True
536 ip2py = True
531
537
532 for m in terms:
538 for m in terms:
533 # Add the pre-example text to `output`.
539 # Add the pre-example text to `output`.
534 output.append(string[charno:m.start()])
540 output.append(string[charno:m.start()])
535 # Update lineno (lines before this example)
541 # Update lineno (lines before this example)
536 lineno += string.count('\n', charno, m.start())
542 lineno += string.count('\n', charno, m.start())
537 # Extract info from the regexp match.
543 # Extract info from the regexp match.
538 (source, options, want, exc_msg) = \
544 (source, options, want, exc_msg) = \
539 self._parse_example(m, name, lineno,ip2py)
545 self._parse_example(m, name, lineno,ip2py)
540
546
541 # Append the random-output marker (it defaults to empty in most
547 # Append the random-output marker (it defaults to empty in most
542 # cases, it's only non-empty for 'all-random' tests):
548 # cases, it's only non-empty for 'all-random' tests):
543 want += random_marker
549 want += random_marker
544
550
545 if Example is IPExternalExample:
551 if Example is IPExternalExample:
546 options[doctest.NORMALIZE_WHITESPACE] = True
552 options[doctest.NORMALIZE_WHITESPACE] = True
547 want += '\n'
553 want += '\n'
548
554
549 # Create an Example, and add it to the list.
555 # Create an Example, and add it to the list.
550 if not self._IS_BLANK_OR_COMMENT(source):
556 if not self._IS_BLANK_OR_COMMENT(source):
551 output.append(Example(source, want, exc_msg,
557 output.append(Example(source, want, exc_msg,
552 lineno=lineno,
558 lineno=lineno,
553 indent=min_indent+len(m.group('indent')),
559 indent=min_indent+len(m.group('indent')),
554 options=options))
560 options=options))
555 # Update lineno (lines inside this example)
561 # Update lineno (lines inside this example)
556 lineno += string.count('\n', m.start(), m.end())
562 lineno += string.count('\n', m.start(), m.end())
557 # Update charno.
563 # Update charno.
558 charno = m.end()
564 charno = m.end()
559 # Add any remaining post-example text to `output`.
565 # Add any remaining post-example text to `output`.
560 output.append(string[charno:])
566 output.append(string[charno:])
561 return output
567 return output
562
568
563 def _parse_example(self, m, name, lineno,ip2py=False):
569 def _parse_example(self, m, name, lineno,ip2py=False):
564 """
570 """
565 Given a regular expression match from `_EXAMPLE_RE` (`m`),
571 Given a regular expression match from `_EXAMPLE_RE` (`m`),
566 return a pair `(source, want)`, where `source` is the matched
572 return a pair `(source, want)`, where `source` is the matched
567 example's source code (with prompts and indentation stripped);
573 example's source code (with prompts and indentation stripped);
568 and `want` is the example's expected output (with indentation
574 and `want` is the example's expected output (with indentation
569 stripped).
575 stripped).
570
576
571 `name` is the string's name, and `lineno` is the line number
577 `name` is the string's name, and `lineno` is the line number
572 where the example starts; both are used for error messages.
578 where the example starts; both are used for error messages.
573
579
574 Optional:
580 Optional:
575 `ip2py`: if true, filter the input via IPython to convert the syntax
581 `ip2py`: if true, filter the input via IPython to convert the syntax
576 into valid python.
582 into valid python.
577 """
583 """
578
584
579 # Get the example's indentation level.
585 # Get the example's indentation level.
580 indent = len(m.group('indent'))
586 indent = len(m.group('indent'))
581
587
582 # Divide source into lines; check that they're properly
588 # Divide source into lines; check that they're properly
583 # indented; and then strip their indentation & prompts.
589 # indented; and then strip their indentation & prompts.
584 source_lines = m.group('source').split('\n')
590 source_lines = m.group('source').split('\n')
585
591
586 # We're using variable-length input prompts
592 # We're using variable-length input prompts
587 ps1 = m.group('ps1')
593 ps1 = m.group('ps1')
588 ps2 = m.group('ps2')
594 ps2 = m.group('ps2')
589 ps1_len = len(ps1)
595 ps1_len = len(ps1)
590
596
591 self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)
597 self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)
592 if ps2:
598 if ps2:
593 self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno)
599 self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno)
594
600
595 source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines])
601 source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines])
596
602
597 if ip2py:
603 if ip2py:
598 # Convert source input from IPython into valid Python syntax
604 # Convert source input from IPython into valid Python syntax
599 source = self.ip2py(source)
605 source = self.ip2py(source)
600
606
601 # Divide want into lines; check that it's properly indented; and
607 # Divide want into lines; check that it's properly indented; and
602 # then strip the indentation. Spaces before the last newline should
608 # then strip the indentation. Spaces before the last newline should
603 # be preserved, so plain rstrip() isn't good enough.
609 # be preserved, so plain rstrip() isn't good enough.
604 want = m.group('want')
610 want = m.group('want')
605 want_lines = want.split('\n')
611 want_lines = want.split('\n')
606 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
612 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
607 del want_lines[-1] # forget final newline & spaces after it
613 del want_lines[-1] # forget final newline & spaces after it
608 self._check_prefix(want_lines, ' '*indent, name,
614 self._check_prefix(want_lines, ' '*indent, name,
609 lineno + len(source_lines))
615 lineno + len(source_lines))
610
616
611 # Remove ipython output prompt that might be present in the first line
617 # Remove ipython output prompt that might be present in the first line
612 want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0])
618 want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0])
613
619
614 want = '\n'.join([wl[indent:] for wl in want_lines])
620 want = '\n'.join([wl[indent:] for wl in want_lines])
615
621
616 # If `want` contains a traceback message, then extract it.
622 # If `want` contains a traceback message, then extract it.
617 m = self._EXCEPTION_RE.match(want)
623 m = self._EXCEPTION_RE.match(want)
618 if m:
624 if m:
619 exc_msg = m.group('msg')
625 exc_msg = m.group('msg')
620 else:
626 else:
621 exc_msg = None
627 exc_msg = None
622
628
623 # Extract options from the source.
629 # Extract options from the source.
624 options = self._find_options(source, name, lineno)
630 options = self._find_options(source, name, lineno)
625
631
626 return source, options, want, exc_msg
632 return source, options, want, exc_msg
627
633
628 def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len):
634 def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len):
629 """
635 """
630 Given the lines of a source string (including prompts and
636 Given the lines of a source string (including prompts and
631 leading indentation), check to make sure that every prompt is
637 leading indentation), check to make sure that every prompt is
632 followed by a space character. If any line is not followed by
638 followed by a space character. If any line is not followed by
633 a space character, then raise ValueError.
639 a space character, then raise ValueError.
634
640
635 Note: IPython-modified version which takes the input prompt length as a
641 Note: IPython-modified version which takes the input prompt length as a
636 parameter, so that prompts of variable length can be dealt with.
642 parameter, so that prompts of variable length can be dealt with.
637 """
643 """
638 space_idx = indent+ps1_len
644 space_idx = indent+ps1_len
639 min_len = space_idx+1
645 min_len = space_idx+1
640 for i, line in enumerate(lines):
646 for i, line in enumerate(lines):
641 if len(line) >= min_len and line[space_idx] != ' ':
647 if len(line) >= min_len and line[space_idx] != ' ':
642 raise ValueError('line %r of the docstring for %s '
648 raise ValueError('line %r of the docstring for %s '
643 'lacks blank after %s: %r' %
649 'lacks blank after %s: %r' %
644 (lineno+i+1, name,
650 (lineno+i+1, name,
645 line[indent:space_idx], line))
651 line[indent:space_idx], line))
646
652
647
653
648 SKIP = doctest.register_optionflag('SKIP')
654 SKIP = doctest.register_optionflag('SKIP')
649
655
650
656
651 class IPDocTestRunner(doctest.DocTestRunner,object):
657 class IPDocTestRunner(doctest.DocTestRunner,object):
652 """Test runner that synchronizes the IPython namespace with test globals.
658 """Test runner that synchronizes the IPython namespace with test globals.
653 """
659 """
654
660
655 def run(self, test, compileflags=None, out=None, clear_globs=True):
661 def run(self, test, compileflags=None, out=None, clear_globs=True):
656
662
657 # Hack: ipython needs access to the execution context of the example,
663 # Hack: ipython needs access to the execution context of the example,
658 # so that it can propagate user variables loaded by %run into
664 # so that it can propagate user variables loaded by %run into
659 # test.globs. We put them here into our modified %run as a function
665 # test.globs. We put them here into our modified %run as a function
660 # attribute. Our new %run will then only make the namespace update
666 # attribute. Our new %run will then only make the namespace update
661 # when called (rather than unconconditionally updating test.globs here
667 # when called (rather than unconconditionally updating test.globs here
662 # for all examples, most of which won't be calling %run anyway).
668 # for all examples, most of which won't be calling %run anyway).
663 _run_ns_sync.test_globs = test.globs
669 _run_ns_sync.test_globs = test.globs
664 _run_ns_sync.test_filename = test.filename
670 _run_ns_sync.test_filename = test.filename
665
671
666 return super(IPDocTestRunner,self).run(test,
672 return super(IPDocTestRunner,self).run(test,
667 compileflags,out,clear_globs)
673 compileflags,out,clear_globs)
668
674
669
675
670 class DocFileCase(doctest.DocFileCase):
676 class DocFileCase(doctest.DocFileCase):
671 """Overrides to provide filename
677 """Overrides to provide filename
672 """
678 """
673 def address(self):
679 def address(self):
674 return (self._dt_test.filename, None, None)
680 return (self._dt_test.filename, None, None)
675
681
676
682
677 class ExtensionDoctest(doctests.Doctest):
683 class ExtensionDoctest(doctests.Doctest):
678 """Nose Plugin that supports doctests in extension modules.
684 """Nose Plugin that supports doctests in extension modules.
679 """
685 """
680 name = 'extdoctest' # call nosetests with --with-extdoctest
686 name = 'extdoctest' # call nosetests with --with-extdoctest
681 enabled = True
687 enabled = True
682
688
683 def __init__(self,exclude_patterns=None):
689 def __init__(self,exclude_patterns=None):
684 """Create a new ExtensionDoctest plugin.
690 """Create a new ExtensionDoctest plugin.
685
691
686 Parameters
692 Parameters
687 ----------
693 ----------
688
694
689 exclude_patterns : sequence of strings, optional
695 exclude_patterns : sequence of strings, optional
690 These patterns are compiled as regular expressions, subsequently used
696 These patterns are compiled as regular expressions, subsequently used
691 to exclude any filename which matches them from inclusion in the test
697 to exclude any filename which matches them from inclusion in the test
692 suite (using pattern.search(), NOT pattern.match() ).
698 suite (using pattern.search(), NOT pattern.match() ).
693 """
699 """
700
694 if exclude_patterns is None:
701 if exclude_patterns is None:
695 exclude_patterns = []
702 exclude_patterns = []
696 self.exclude_patterns = map(re.compile,exclude_patterns)
703 self.exclude_patterns = map(re.compile,exclude_patterns)
697 doctests.Doctest.__init__(self)
704 doctests.Doctest.__init__(self)
698
705
699 def options(self, parser, env=os.environ):
706 def options(self, parser, env=os.environ):
700 Plugin.options(self, parser, env)
707 Plugin.options(self, parser, env)
701 parser.add_option('--doctest-tests', action='store_true',
708 parser.add_option('--doctest-tests', action='store_true',
702 dest='doctest_tests',
709 dest='doctest_tests',
703 default=env.get('NOSE_DOCTEST_TESTS',True),
710 default=env.get('NOSE_DOCTEST_TESTS',True),
704 help="Also look for doctests in test modules. "
711 help="Also look for doctests in test modules. "
705 "Note that classes, methods and functions should "
712 "Note that classes, methods and functions should "
706 "have either doctests or non-doctest tests, "
713 "have either doctests or non-doctest tests, "
707 "not both. [NOSE_DOCTEST_TESTS]")
714 "not both. [NOSE_DOCTEST_TESTS]")
708 parser.add_option('--doctest-extension', action="append",
715 parser.add_option('--doctest-extension', action="append",
709 dest="doctestExtension",
716 dest="doctestExtension",
710 help="Also look for doctests in files with "
717 help="Also look for doctests in files with "
711 "this extension [NOSE_DOCTEST_EXTENSION]")
718 "this extension [NOSE_DOCTEST_EXTENSION]")
712 # Set the default as a list, if given in env; otherwise
719 # Set the default as a list, if given in env; otherwise
713 # an additional value set on the command line will cause
720 # an additional value set on the command line will cause
714 # an error.
721 # an error.
715 env_setting = env.get('NOSE_DOCTEST_EXTENSION')
722 env_setting = env.get('NOSE_DOCTEST_EXTENSION')
716 if env_setting is not None:
723 if env_setting is not None:
717 parser.set_defaults(doctestExtension=tolist(env_setting))
724 parser.set_defaults(doctestExtension=tolist(env_setting))
718
725
719
726
720 def configure(self, options, config):
727 def configure(self, options, config):
721 Plugin.configure(self, options, config)
728 Plugin.configure(self, options, config)
722 self.doctest_tests = options.doctest_tests
729 self.doctest_tests = options.doctest_tests
723 self.extension = tolist(options.doctestExtension)
730 self.extension = tolist(options.doctestExtension)
724
731
725 self.parser = doctest.DocTestParser()
732 self.parser = doctest.DocTestParser()
726 self.finder = DocTestFinder()
733 self.finder = DocTestFinder()
727 self.checker = IPDoctestOutputChecker()
734 self.checker = IPDoctestOutputChecker()
728 self.globs = None
735 self.globs = None
729 self.extraglobs = None
736 self.extraglobs = None
730
737
731
738
732 def loadTestsFromExtensionModule(self,filename):
739 def loadTestsFromExtensionModule(self,filename):
733 bpath,mod = os.path.split(filename)
740 bpath,mod = os.path.split(filename)
734 modname = os.path.splitext(mod)[0]
741 modname = os.path.splitext(mod)[0]
735 try:
742 try:
736 sys.path.append(bpath)
743 sys.path.append(bpath)
737 module = __import__(modname)
744 module = __import__(modname)
738 tests = list(self.loadTestsFromModule(module))
745 tests = list(self.loadTestsFromModule(module))
739 finally:
746 finally:
740 sys.path.pop()
747 sys.path.pop()
741 return tests
748 return tests
742
749
743 # NOTE: the method below is almost a copy of the original one in nose, with
750 # NOTE: the method below is almost a copy of the original one in nose, with
744 # a few modifications to control output checking.
751 # a few modifications to control output checking.
745
752
746 def loadTestsFromModule(self, module):
753 def loadTestsFromModule(self, module):
747 #print '*** ipdoctest - lTM',module # dbg
754 #print '*** ipdoctest - lTM',module # dbg
748
755
749 if not self.matches(module.__name__):
756 if not self.matches(module.__name__):
750 log.debug("Doctest doesn't want module %s", module)
757 log.debug("Doctest doesn't want module %s", module)
751 return
758 return
752
759
753 tests = self.finder.find(module,globs=self.globs,
760 tests = self.finder.find(module,globs=self.globs,
754 extraglobs=self.extraglobs)
761 extraglobs=self.extraglobs)
755 if not tests:
762 if not tests:
756 return
763 return
757
764
758 # always use whitespace and ellipsis options
765 # always use whitespace and ellipsis options
759 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
766 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
760
767
761 tests.sort()
768 tests.sort()
762 module_file = module.__file__
769 module_file = module.__file__
763 if module_file[-4:] in ('.pyc', '.pyo'):
770 if module_file[-4:] in ('.pyc', '.pyo'):
764 module_file = module_file[:-1]
771 module_file = module_file[:-1]
765 for test in tests:
772 for test in tests:
766 if not test.examples:
773 if not test.examples:
767 continue
774 continue
768 if not test.filename:
775 if not test.filename:
769 test.filename = module_file
776 test.filename = module_file
770
777
771 yield DocTestCase(test,
778 yield DocTestCase(test,
772 optionflags=optionflags,
779 optionflags=optionflags,
773 checker=self.checker)
780 checker=self.checker)
774
781
775
782
776 def loadTestsFromFile(self, filename):
783 def loadTestsFromFile(self, filename):
777 if is_extension_module(filename):
784 if is_extension_module(filename):
778 for t in self.loadTestsFromExtensionModule(filename):
785 for t in self.loadTestsFromExtensionModule(filename):
779 yield t
786 yield t
780 else:
787 else:
781 if self.extension and anyp(filename.endswith, self.extension):
788 if self.extension and anyp(filename.endswith, self.extension):
782 name = os.path.basename(filename)
789 name = os.path.basename(filename)
783 dh = open(filename)
790 dh = open(filename)
784 try:
791 try:
785 doc = dh.read()
792 doc = dh.read()
786 finally:
793 finally:
787 dh.close()
794 dh.close()
788 test = self.parser.get_doctest(
795 test = self.parser.get_doctest(
789 doc, globs={'__file__': filename}, name=name,
796 doc, globs={'__file__': filename}, name=name,
790 filename=filename, lineno=0)
797 filename=filename, lineno=0)
791 if test.examples:
798 if test.examples:
792 #print 'FileCase:',test.examples # dbg
799 #print 'FileCase:',test.examples # dbg
793 yield DocFileCase(test)
800 yield DocFileCase(test)
794 else:
801 else:
795 yield False # no tests to load
802 yield False # no tests to load
796
803
797 def wantFile(self,filename):
804 def wantFile(self,filename):
798 """Return whether the given filename should be scanned for tests.
805 """Return whether the given filename should be scanned for tests.
799
806
800 Modified version that accepts extension modules as valid containers for
807 Modified version that accepts extension modules as valid containers for
801 doctests.
808 doctests.
802 """
809 """
803 #print '*** ipdoctest- wantFile:',filename # dbg
810 #print '*** ipdoctest- wantFile:',filename # dbg
804
811
805 for pat in self.exclude_patterns:
812 for pat in self.exclude_patterns:
806 if pat.search(filename):
813 if pat.search(filename):
807 #print '###>>> SKIP:',filename # dbg
814 #print '###>>> SKIP:',filename # dbg
808 return False
815 return False
809
816
810 if is_extension_module(filename):
817 if is_extension_module(filename):
811 return True
818 return True
812 else:
819 else:
813 return doctests.Doctest.wantFile(self,filename)
820 return doctests.Doctest.wantFile(self,filename)
814
821
815
822
816 class IPythonDoctest(ExtensionDoctest):
823 class IPythonDoctest(ExtensionDoctest):
817 """Nose Plugin that supports doctests in extension modules.
824 """Nose Plugin that supports doctests in extension modules.
818 """
825 """
819 name = 'ipdoctest' # call nosetests with --with-ipdoctest
826 name = 'ipdoctest' # call nosetests with --with-ipdoctest
820 enabled = True
827 enabled = True
821
828
822 def makeTest(self, obj, parent):
829 def makeTest(self, obj, parent):
823 """Look for doctests in the given object, which will be a
830 """Look for doctests in the given object, which will be a
824 function, method or class.
831 function, method or class.
825 """
832 """
826 # always use whitespace and ellipsis options
833 # always use whitespace and ellipsis options
827 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
834 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
828
835
829 doctests = self.finder.find(obj, module=getmodule(parent))
836 doctests = self.finder.find(obj, module=getmodule(parent))
830 if doctests:
837 if doctests:
831 for test in doctests:
838 for test in doctests:
832 if len(test.examples) == 0:
839 if len(test.examples) == 0:
833 continue
840 continue
834
841
835 yield DocTestCase(test, obj=obj,
842 yield DocTestCase(test, obj=obj,
836 optionflags=optionflags,
843 optionflags=optionflags,
837 checker=self.checker)
844 checker=self.checker)
838
845
839 def configure(self, options, config):
846 def options(self, parser, env=os.environ):
847 Plugin.options(self, parser, env)
848 parser.add_option('--ipdoctest-tests', action='store_true',
849 dest='ipdoctest_tests',
850 default=env.get('NOSE_IPDOCTEST_TESTS',True),
851 help="Also look for doctests in test modules. "
852 "Note that classes, methods and functions should "
853 "have either doctests or non-doctest tests, "
854 "not both. [NOSE_IPDOCTEST_TESTS]")
855 parser.add_option('--ipdoctest-extension', action="append",
856 dest="ipdoctest_extension",
857 help="Also look for doctests in files with "
858 "this extension [NOSE_IPDOCTEST_EXTENSION]")
859 # Set the default as a list, if given in env; otherwise
860 # an additional value set on the command line will cause
861 # an error.
862 env_setting = env.get('NOSE_IPDOCTEST_EXTENSION')
863 if env_setting is not None:
864 parser.set_defaults(ipdoctest_extension=tolist(env_setting))
840
865
866 def configure(self, options, config):
841 Plugin.configure(self, options, config)
867 Plugin.configure(self, options, config)
842 self.doctest_tests = options.doctest_tests
868 self.doctest_tests = options.ipdoctest_tests
843 self.extension = tolist(options.doctestExtension)
869 self.extension = tolist(options.ipdoctest_extension)
844
870
845 self.parser = IPDocTestParser()
871 self.parser = IPDocTestParser()
846 self.finder = DocTestFinder(parser=self.parser)
872 self.finder = DocTestFinder(parser=self.parser)
847 self.checker = IPDoctestOutputChecker()
873 self.checker = IPDoctestOutputChecker()
848 self.globs = None
874 self.globs = None
849 self.extraglobs = None
875 self.extraglobs = None
850
@@ -1,17 +1,18 b''
1 """Simple script to show reference holding behavior.
1 """Simple script to show reference holding behavior.
2
2
3 This is used by a companion test case.
3 This is used by a companion test case.
4 """
4 """
5
5
6 import gc
6 import gc
7
7
8 class C(object):
8 class C(object):
9 def __del__(self):
9 def __del__(self):
10 print 'deleting object...'
10 pass
11 #print 'deleting object...' # dbg
11
12
12 c = C()
13 c = C()
13
14
14 c_refs = gc.get_referrers(c)
15 c_refs = gc.get_referrers(c)
15 ref_ids = map(id,c_refs)
16 ref_ids = map(id,c_refs)
16
17
17 print 'c referrers:',map(type,c_refs)
18 print 'c referrers:',map(type,c_refs)
@@ -1,51 +1,48 b''
1 """Some simple tests for the plugin while running scripts.
1 """Some simple tests for the plugin while running scripts.
2 """
2 """
3 # Module imports
3 # Module imports
4 # Std lib
4 # Std lib
5 import inspect
5 import inspect
6
6
7 # Our own
7 # Our own
8 from IPython.testing import decorators as dec
8 from IPython.testing import decorators as dec
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Testing functions
11 # Testing functions
12
12
13 def test_trivial():
13 def test_trivial():
14 """A trivial passing test."""
14 """A trivial passing test."""
15 pass
15 pass
16
16
17 def doctest_run():
17 def doctest_run():
18 """Test running a trivial script.
18 """Test running a trivial script.
19
19
20 In [13]: run simplevars.py
20 In [13]: run simplevars.py
21 x is: 1
21 x is: 1
22 """
22 """
23
23
24 def doctest_runvars():
24 def doctest_runvars():
25 """Test that variables defined in scripts get loaded correcly via %run.
25 """Test that variables defined in scripts get loaded correcly via %run.
26
26
27 In [13]: run simplevars.py
27 In [13]: run simplevars.py
28 x is: 1
28 x is: 1
29
29
30 In [14]: x
30 In [14]: x
31 Out[14]: 1
31 Out[14]: 1
32 """
32 """
33
33
34 def doctest_ivars():
34 def doctest_ivars():
35 """Test that variables defined interactively are picked up.
35 """Test that variables defined interactively are picked up.
36 In [5]: zz=1
36 In [5]: zz=1
37
37
38 In [6]: zz
38 In [6]: zz
39 Out[6]: 1
39 Out[6]: 1
40 """
40 """
41
41
42 @dec.skip_doctest
42 #@dec.skip_doctest
43 def doctest_refs():
43 def doctest_refs():
44 """DocTest reference holding issues when running scripts.
44 """DocTest reference holding issues when running scripts.
45
45
46 In [32]: run show_refs.py
46 In [32]: run show_refs.py
47 c referrers: [<type 'dict'>]
47 c referrers: [<type 'dict'>]
48
49 In [33]: map(type,gc.get_referrers(c))
50 Out[33]: [<type 'dict'>]
51 """
48 """
@@ -1,34 +1,34 b''
1 """Test code for https://bugs.launchpad.net/ipython/+bug/239054
1 """Test code for https://bugs.launchpad.net/ipython/+bug/239054
2
2
3 WARNING: this script exits IPython! It MUST be run in a subprocess.
3 WARNING: this script exits IPython! It MUST be run in a subprocess.
4
4
5 When you run the following script from CPython it prints:
5 When you run the following script from CPython it prints:
6 __init__ is here
6 __init__ is here
7 __del__ is here
7 __del__ is here
8
8
9 and creates the __del__.txt file
9 and creates the __del__.txt file
10
10
11 When you run it from IPython it prints:
11 When you run it from IPython it prints:
12 __init__ is here
12 __init__ is here
13
13
14 When you exit() or Exit from IPython neothing is printed and no file is created
14 When you exit() or Exit from IPython neothing is printed and no file is created
15 (the file thing is to make sure __del__ is really never called and not that
15 (the file thing is to make sure __del__ is really never called and not that
16 just the output is eaten).
16 just the output is eaten).
17
17
18 Note that if you call %reset in IPython then everything is Ok.
18 Note that if you call %reset in IPython then everything is Ok.
19
19
20 IPython should do the equivalent of %reset and release all the references it
20 IPython should do the equivalent of %reset and release all the references it
21 holds before exit. This behavior is important when working with binding objects
21 holds before exit. This behavior is important when working with binding objects
22 that rely on __del__. If the current behavior has some use case then I suggest
22 that rely on __del__. If the current behavior has some use case then I suggest
23 to add a configuration option to IPython to control it.
23 to add a configuration option to IPython to control it.
24 """
24 """
25 import sys
25 import sys
26
26
27 class A(object):
27 class A(object):
28 def __del__(self):
28 def __del__(self):
29 print 'object A deleted'
29 print 'obj_del.py: object A deleted'
30
30
31 a = A()
31 a = A()
32
32
33 # Now, we force an exit, the caller will check that the del printout was given
33 # Now, we force an exit, the caller will check that the del printout was given
34 _ip.IP.ask_exit()
34 _ip.IP.ask_exit()
@@ -1,26 +1,27 b''
1 """Simple script to instantiate a class for testing %run"""
1 """Simple script to instantiate a class for testing %run"""
2
2
3 import sys
3 import sys
4
4
5 # An external test will check that calls to f() work after %run
5 # An external test will check that calls to f() work after %run
6 class foo: pass
6 class foo: pass
7
7
8 def f():
8 def f():
9 return foo()
9 return foo()
10
10
11 # We also want to ensure that while objects remain available for immediate
11 # We also want to ensure that while objects remain available for immediate
12 # access, objects from *previous* runs of the same script get collected, to
12 # access, objects from *previous* runs of the same script get collected, to
13 # avoid accumulating massive amounts of old references.
13 # avoid accumulating massive amounts of old references.
14 class C(object):
14 class C(object):
15 def __init__(self,name):
15 def __init__(self,name):
16 self.name = name
16 self.name = name
17
17
18 def __del__(self):
18 def __del__(self):
19 print 'Deleting object:',self.name
19 print 'tclass.py: deleting object:',self.name
20
20
21 try:
21 try:
22 name = sys.argv[1]
22 name = sys.argv[1]
23 except IndexError:
23 except IndexError:
24 pass
24 pass
25 else:
25 else:
26 c = C(name)
26 if name.startswith('C'):
27 c = C(name)
@@ -1,17 +1,68 b''
1 """Tests for the key iplib module, where the main ipython class is defined.
1 """Tests for the key iplib module, where the main ipython class is defined.
2 """
2 """
3 #-----------------------------------------------------------------------------
4 # Module imports
5 #-----------------------------------------------------------------------------
3
6
7 # stdlib
8 import os
9 import shutil
10 import tempfile
11
12 # third party
4 import nose.tools as nt
13 import nose.tools as nt
5
14
15 # our own packages
16 from IPython import iplib
17
18 #-----------------------------------------------------------------------------
19 # Globals
20 #-----------------------------------------------------------------------------
21
22 # Useful global ipapi object and main IPython one. Unfortunately we have a
23 # long precedent of carrying the 'ipapi' global object which is injected into
24 # the system namespace as _ip, but that keeps a pointer to the actual IPython
25 # InteractiveShell instance, which is named IP. Since in testing we do need
26 # access to the real thing (we want to probe beyond what ipapi exposes), make
27 # here a global reference to each. In general, things that are exposed by the
28 # ipapi instance should be read from there, but we also will often need to use
29 # the actual IPython one.
30
31 ip = _ip # This is the ipapi instance
32 IP = ip.IP # This is the actual IPython shell 'raw' object.
33
34 #-----------------------------------------------------------------------------
35 # Test functions
36 #-----------------------------------------------------------------------------
6
37
7 def test_reset():
38 def test_reset():
8 """reset must clear most namespaces."""
39 """reset must clear most namespaces."""
9 ip = _ip.IP
40 IP.reset() # first, it should run without error
10 ip.reset() # first, it should run without error
11 # Then, check that most namespaces end up empty
41 # Then, check that most namespaces end up empty
12 for ns in ip.ns_refs_table:
42 for ns in IP.ns_refs_table:
13 if ns is ip.user_ns:
43 if ns is IP.user_ns:
14 # The user namespace is reset with some data, so we can't check for
44 # The user namespace is reset with some data, so we can't check for
15 # it being empty
45 # it being empty
16 continue
46 continue
17 nt.assert_equals(len(ns),0)
47 nt.assert_equals(len(ns),0)
48
49
50 # make sure that user_setup can be run re-entrantly in 'install' mode.
51 def test_user_setup():
52 # use a lambda to pass kwargs to the generator
53 user_setup = lambda a,k: iplib.user_setup(*a,**k)
54 kw = dict(mode='install', interactive=False)
55
56 # Call the user setup and verify that the directory exists
57 yield user_setup, (ip.options.ipythondir,''), kw
58 yield os.path.isdir, ip.options.ipythondir
59
60 # Now repeat the operation with a non-existent directory. Check both that
61 # the call succeeds and that the directory is created.
62 tmpdir = tempfile.mktemp(prefix='ipython-test-')
63 try:
64 yield user_setup, (tmpdir,''), kw
65 yield os.path.isdir, tmpdir
66 finally:
67 # In this case, clean up the temp dir once done
68 shutil.rmtree(tmpdir)
@@ -1,135 +1,151 b''
1 """Tests for various magic functions.
1 """Tests for various magic functions.
2
2
3 Needs to be run by nose (to make ipython session available).
3 Needs to be run by nose (to make ipython session available).
4 """
4 """
5
5
6 # Standard library imports
6 # Standard library imports
7 import os
7 import os
8 import sys
8 import sys
9
9
10 # Third-party imports
10 # Third-party imports
11 import nose.tools as nt
11 import nose.tools as nt
12
12
13 # From our own code
13 # From our own code
14 from IPython.testing import decorators as dec
14 from IPython.testing import decorators as dec
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Test functions begin
17 # Test functions begin
18
18
19 def test_rehashx():
19 def test_rehashx():
20 # clear up everything
20 # clear up everything
21 _ip.IP.alias_table.clear()
21 _ip.IP.alias_table.clear()
22 del _ip.db['syscmdlist']
22 del _ip.db['syscmdlist']
23
23
24 _ip.magic('rehashx')
24 _ip.magic('rehashx')
25 # Practically ALL ipython development systems will have more than 10 aliases
25 # Practically ALL ipython development systems will have more than 10 aliases
26
26
27 assert len(_ip.IP.alias_table) > 10
27 assert len(_ip.IP.alias_table) > 10
28 for key, val in _ip.IP.alias_table.items():
28 for key, val in _ip.IP.alias_table.items():
29 # we must strip dots from alias names
29 # we must strip dots from alias names
30 assert '.' not in key
30 assert '.' not in key
31
31
32 # rehashx must fill up syscmdlist
32 # rehashx must fill up syscmdlist
33 scoms = _ip.db['syscmdlist']
33 scoms = _ip.db['syscmdlist']
34 assert len(scoms) > 10
34 assert len(scoms) > 10
35
35
36
36
37 def doctest_run_ns():
37 def doctest_run_ns():
38 """Classes declared %run scripts must be instantiable afterwards.
38 """Classes declared %run scripts must be instantiable afterwards.
39
39
40 In [11]: run tclass
40 In [11]: run tclass foo
41
41
42 In [12]: isinstance(f(),foo)
42 In [12]: isinstance(f(),foo)
43 Out[12]: True
43 Out[12]: True
44 """
44 """
45
45
46
46
47 def doctest_run_ns2():
47 def doctest_run_ns2():
48 """Classes declared %run scripts must be instantiable afterwards.
48 """Classes declared %run scripts must be instantiable afterwards.
49
49
50 In [3]: run tclass.py
50 In [4]: run tclass C-first_pass
51
51
52 In [4]: run tclass first_pass
52 In [5]: run tclass C-second_pass
53
53 tclass.py: deleting object: C-first_pass
54 In [5]: run tclass second_pass
55 Deleting object: first_pass
56 """
54 """
57
55
58
56
59 def doctest_hist_f():
57 def doctest_hist_f():
60 """Test %hist -f with temporary filename.
58 """Test %hist -f with temporary filename.
61
59
62 In [9]: import tempfile
60 In [9]: import tempfile
63
61
64 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
62 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
65
63
66 In [11]: %history -n -f $tfile 3
64 In [11]: %history -n -f $tfile 3
67 """
65 """
68
66
69
67
70 def doctest_hist_r():
68 def doctest_hist_r():
71 """Test %hist -r
69 """Test %hist -r
72
70
73 XXX - This test is not recording the output correctly. Not sure why...
71 XXX - This test is not recording the output correctly. Not sure why...
74
72
75 In [6]: x=1
73 In [6]: x=1
76
74
77 In [7]: hist -n -r 2
75 In [7]: hist -n -r 2
78 x=1 # random
76 x=1 # random
79 hist -n -r 2 # random
77 hist -n -r 2 # random
80 """
78 """
81
79
82
80
83 def test_obj_del():
81 def test_obj_del():
84 """Test that object's __del__ methods are called on exit."""
82 """Test that object's __del__ methods are called on exit."""
85 test_dir = os.path.dirname(__file__)
83 test_dir = os.path.dirname(__file__)
86 del_file = os.path.join(test_dir,'obj_del.py')
84 del_file = os.path.join(test_dir,'obj_del.py')
87 out = _ip.IP.getoutput('ipython %s' % del_file)
85 out = _ip.IP.getoutput('ipython %s' % del_file)
88 nt.assert_equals(out,'object A deleted')
86 nt.assert_equals(out,'obj_del.py: object A deleted')
89
87
90
88
91 def test_shist():
89 def test_shist():
92 # Simple tests of ShadowHist class - test generator.
90 # Simple tests of ShadowHist class - test generator.
93 import os, shutil, tempfile
91 import os, shutil, tempfile
94
92
95 from IPython.Extensions import pickleshare
93 from IPython.Extensions import pickleshare
96 from IPython.history import ShadowHist
94 from IPython.history import ShadowHist
97
95
98 tfile = tempfile.mktemp('','tmp-ipython-')
96 tfile = tempfile.mktemp('','tmp-ipython-')
99
97
100 db = pickleshare.PickleShareDB(tfile)
98 db = pickleshare.PickleShareDB(tfile)
101 s = ShadowHist(db)
99 s = ShadowHist(db)
102 s.add('hello')
100 s.add('hello')
103 s.add('world')
101 s.add('world')
104 s.add('hello')
102 s.add('hello')
105 s.add('hello')
103 s.add('hello')
106 s.add('karhu')
104 s.add('karhu')
107
105
108 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
106 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
109
107
110 yield nt.assert_equal,s.get(2),'world'
108 yield nt.assert_equal,s.get(2),'world'
111
109
112 shutil.rmtree(tfile)
110 shutil.rmtree(tfile)
113
111
114 @dec.skipif_not_numpy
112 @dec.skipif_not_numpy
115 def test_numpy_clear_array_undec():
113 def test_numpy_clear_array_undec():
116 _ip.ex('import numpy as np')
114 _ip.ex('import numpy as np')
117 _ip.ex('a = np.empty(2)')
115 _ip.ex('a = np.empty(2)')
118
116
119 yield nt.assert_true,'a' in _ip.user_ns
117 yield nt.assert_true,'a' in _ip.user_ns
120 _ip.magic('clear array')
118 _ip.magic('clear array')
121 yield nt.assert_false,'a' in _ip.user_ns
119 yield nt.assert_false,'a' in _ip.user_ns
122
120
123
121
124 @dec.skip()
122 @dec.skip()
125 def test_fail_dec(*a,**k):
123 def test_fail_dec(*a,**k):
126 yield nt.assert_true, False
124 yield nt.assert_true, False
127
125
128 @dec.skip('This one shouldn not run')
126 @dec.skip('This one shouldn not run')
129 def test_fail_dec2(*a,**k):
127 def test_fail_dec2(*a,**k):
130 yield nt.assert_true, False
128 yield nt.assert_true, False
131
129
132 @dec.skipknownfailure
130 @dec.skipknownfailure
133 def test_fail_dec3(*a,**k):
131 def test_fail_dec3(*a,**k):
134 yield nt.assert_true, False
132 yield nt.assert_true, False
135
133
134
135 def doctest_refbug():
136 """Very nasty problem with references held by multiple runs of a script.
137 See: https://bugs.launchpad.net/ipython/+bug/269966
138
139 In [1]: _ip.IP.clear_main_mod_cache()
140
141 In [2]: run refbug
142
143 In [3]: call_f()
144 lowercased: hello
145
146 In [4]: run refbug
147
148 In [5]: call_f()
149 lowercased: hello
150 lowercased: hello
151 """
@@ -1,398 +1,401 b''
1 .. _changes:
1 .. _changes:
2
2
3 ==========
3 ==========
4 What's new
4 What's new
5 ==========
5 ==========
6
6
7 .. contents::
7 .. contents::
8 ..
8 ..
9 1 Release 0.9.1
9 1 Release 0.9.1
10 2 Release 0.9
10 2 Release 0.9
11 2.1 New features
11 2.1 New features
12 2.2 Bug fixes
12 2.2 Bug fixes
13 2.3 Backwards incompatible changes
13 2.3 Backwards incompatible changes
14 2.4 Changes merged in from IPython1
14 2.4 Changes merged in from IPython1
15 2.4.1 New features
15 2.4.1 New features
16 2.4.2 Bug fixes
16 2.4.2 Bug fixes
17 2.4.3 Backwards incompatible changes
17 2.4.3 Backwards incompatible changes
18 3 Release 0.8.4
18 3 Release 0.8.4
19 4 Release 0.8.3
19 4 Release 0.8.3
20 5 Release 0.8.2
20 5 Release 0.8.2
21 6 Older releases
21 6 Older releases
22 ..
22 ..
23
23
24 Release dev
24 Release dev
25 ===========
25 ===========
26
26
27 New features
27 New features
28 ------------
28 ------------
29
29
30 * The new ipcluster now has a fully working ssh mode that should work on
30 * The new ipcluster now has a fully working ssh mode that should work on
31 Linux, Unix and OS X. Thanks to Vishal Vatsa for implementing this!
31 Linux, Unix and OS X. Thanks to Vishal Vatsa for implementing this!
32
32
33 * The wonderful TextMate editor can now be used with %edit on OS X. Thanks
33 * The wonderful TextMate editor can now be used with %edit on OS X. Thanks
34 to Matt Foster for this patch.
34 to Matt Foster for this patch.
35
35
36 * Fully refactored :command:`ipcluster` command line program for starting
36 * Fully refactored :command:`ipcluster` command line program for starting
37 IPython clusters. This new version is a complete rewrite and 1) is fully
37 IPython clusters. This new version is a complete rewrite and 1) is fully
38 cross platform (we now use Twisted's process management), 2) has much
38 cross platform (we now use Twisted's process management), 2) has much
39 improved performance, 3) uses subcommands for different types of clusters,
39 improved performance, 3) uses subcommands for different types of clusters,
40 4) uses argparse for parsing command line options, 5) has better support
40 4) uses argparse for parsing command line options, 5) has better support
41 for starting clusters using :command:`mpirun`, 6) has experimental support
41 for starting clusters using :command:`mpirun`, 6) has experimental support
42 for starting engines using PBS. However, this new version of ipcluster
42 for starting engines using PBS. However, this new version of ipcluster
43 should be considered a technology preview. We plan on changing the API
43 should be considered a technology preview. We plan on changing the API
44 in significant ways before it is final.
44 in significant ways before it is final.
45
45
46 * The :mod:`argparse` module has been added to :mod:`IPython.external`.
46 * The :mod:`argparse` module has been added to :mod:`IPython.external`.
47
47
48 * Fully description of the security model added to the docs.
48 * Fully description of the security model added to the docs.
49
49
50 * cd completer: show bookmarks if no other completions are available.
50 * cd completer: show bookmarks if no other completions are available.
51
51
52 * sh profile: easy way to give 'title' to prompt: assign to variable
52 * sh profile: easy way to give 'title' to prompt: assign to variable
53 '_prompt_title'. It looks like this::
53 '_prompt_title'. It looks like this::
54
54
55 [~]|1> _prompt_title = 'sudo!'
55 [~]|1> _prompt_title = 'sudo!'
56 sudo![~]|2>
56 sudo![~]|2>
57
57
58 * %edit: If you do '%edit pasted_block', pasted_block
58 * %edit: If you do '%edit pasted_block', pasted_block
59 variable gets updated with new data (so repeated
59 variable gets updated with new data (so repeated
60 editing makes sense)
60 editing makes sense)
61
61
62 Bug fixes
62 Bug fixes
63 ---------
63 ---------
64
64
65 * Numerous bugs on Windows with the new ipcluster have been fixed.
65 * Numerous bugs on Windows with the new ipcluster have been fixed.
66
66
67 * The ipengine and ipcontroller scripts now handle missing furl files
67 * The ipengine and ipcontroller scripts now handle missing furl files
68 more gracefully by giving better error messages.
68 more gracefully by giving better error messages.
69
69
70 * %rehashx: Aliases no longer contain dots. python3.0 binary
70 * %rehashx: Aliases no longer contain dots. python3.0 binary
71 will create alias python30. Fixes:
71 will create alias python30. Fixes:
72 #259716 "commands with dots in them don't work"
72 #259716 "commands with dots in them don't work"
73
73
74 * %cpaste: %cpaste -r repeats the last pasted block.
74 * %cpaste: %cpaste -r repeats the last pasted block.
75 The block is assigned to pasted_block even if code
75 The block is assigned to pasted_block even if code
76 raises exception.
76 raises exception.
77
77
78 * Bug #274067 'The code in get_home_dir is broken for py2exe' was
79 fixed.
80
78 Backwards incompatible changes
81 Backwards incompatible changes
79 ------------------------------
82 ------------------------------
80
83
81 * The controller now has a ``-r`` flag that needs to be used if you want to
84 * The controller now has a ``-r`` flag that needs to be used if you want to
82 reuse existing furl files. Otherwise they are deleted (the default).
85 reuse existing furl files. Otherwise they are deleted (the default).
83
86
84 * Remove ipy_leo.py. "easy_install ipython-extension" to get it.
87 * Remove ipy_leo.py. "easy_install ipython-extension" to get it.
85 (done to decouple it from ipython release cycle)
88 (done to decouple it from ipython release cycle)
86
89
87
90
88
91
89 Release 0.9.1
92 Release 0.9.1
90 =============
93 =============
91
94
92 This release was quickly made to restore compatibility with Python 2.4, which
95 This release was quickly made to restore compatibility with Python 2.4, which
93 version 0.9 accidentally broke. No new features were introduced, other than
96 version 0.9 accidentally broke. No new features were introduced, other than
94 some additional testing support for internal use.
97 some additional testing support for internal use.
95
98
96
99
97 Release 0.9
100 Release 0.9
98 ===========
101 ===========
99
102
100 New features
103 New features
101 ------------
104 ------------
102
105
103 * All furl files and security certificates are now put in a read-only
106 * All furl files and security certificates are now put in a read-only
104 directory named ~./ipython/security.
107 directory named ~./ipython/security.
105
108
106 * A single function :func:`get_ipython_dir`, in :mod:`IPython.genutils` that
109 * A single function :func:`get_ipython_dir`, in :mod:`IPython.genutils` that
107 determines the user's IPython directory in a robust manner.
110 determines the user's IPython directory in a robust manner.
108
111
109 * Laurent's WX application has been given a top-level script called
112 * Laurent's WX application has been given a top-level script called
110 ipython-wx, and it has received numerous fixes. We expect this code to be
113 ipython-wx, and it has received numerous fixes. We expect this code to be
111 architecturally better integrated with Gael's WX 'ipython widget' over the
114 architecturally better integrated with Gael's WX 'ipython widget' over the
112 next few releases.
115 next few releases.
113
116
114 * The Editor synchronization work by Vivian De Smedt has been merged in. This
117 * The Editor synchronization work by Vivian De Smedt has been merged in. This
115 code adds a number of new editor hooks to synchronize with editors under
118 code adds a number of new editor hooks to synchronize with editors under
116 Windows.
119 Windows.
117
120
118 * A new, still experimental but highly functional, WX shell by Gael Varoquaux.
121 * A new, still experimental but highly functional, WX shell by Gael Varoquaux.
119 This work was sponsored by Enthought, and while it's still very new, it is
122 This work was sponsored by Enthought, and while it's still very new, it is
120 based on a more cleanly organized arhictecture of the various IPython
123 based on a more cleanly organized arhictecture of the various IPython
121 components. We will continue to develop this over the next few releases as a
124 components. We will continue to develop this over the next few releases as a
122 model for GUI components that use IPython.
125 model for GUI components that use IPython.
123
126
124 * Another GUI frontend, Cocoa based (Cocoa is the OSX native GUI framework),
127 * Another GUI frontend, Cocoa based (Cocoa is the OSX native GUI framework),
125 authored by Barry Wark. Currently the WX and the Cocoa ones have slightly
128 authored by Barry Wark. Currently the WX and the Cocoa ones have slightly
126 different internal organizations, but the whole team is working on finding
129 different internal organizations, but the whole team is working on finding
127 what the right abstraction points are for a unified codebase.
130 what the right abstraction points are for a unified codebase.
128
131
129 * As part of the frontend work, Barry Wark also implemented an experimental
132 * As part of the frontend work, Barry Wark also implemented an experimental
130 event notification system that various ipython components can use. In the
133 event notification system that various ipython components can use. In the
131 next release the implications and use patterns of this system regarding the
134 next release the implications and use patterns of this system regarding the
132 various GUI options will be worked out.
135 various GUI options will be worked out.
133
136
134 * IPython finally has a full test system, that can test docstrings with
137 * IPython finally has a full test system, that can test docstrings with
135 IPython-specific functionality. There are still a few pieces missing for it
138 IPython-specific functionality. There are still a few pieces missing for it
136 to be widely accessible to all users (so they can run the test suite at any
139 to be widely accessible to all users (so they can run the test suite at any
137 time and report problems), but it now works for the developers. We are
140 time and report problems), but it now works for the developers. We are
138 working hard on continuing to improve it, as this was probably IPython's
141 working hard on continuing to improve it, as this was probably IPython's
139 major Achilles heel (the lack of proper test coverage made it effectively
142 major Achilles heel (the lack of proper test coverage made it effectively
140 impossible to do large-scale refactoring). The full test suite can now
143 impossible to do large-scale refactoring). The full test suite can now
141 be run using the :command:`iptest` command line program.
144 be run using the :command:`iptest` command line program.
142
145
143 * The notion of a task has been completely reworked. An `ITask` interface has
146 * The notion of a task has been completely reworked. An `ITask` interface has
144 been created. This interface defines the methods that tasks need to
147 been created. This interface defines the methods that tasks need to
145 implement. These methods are now responsible for things like submitting
148 implement. These methods are now responsible for things like submitting
146 tasks and processing results. There are two basic task types:
149 tasks and processing results. There are two basic task types:
147 :class:`IPython.kernel.task.StringTask` (this is the old `Task` object, but
150 :class:`IPython.kernel.task.StringTask` (this is the old `Task` object, but
148 renamed) and the new :class:`IPython.kernel.task.MapTask`, which is based on
151 renamed) and the new :class:`IPython.kernel.task.MapTask`, which is based on
149 a function.
152 a function.
150
153
151 * A new interface, :class:`IPython.kernel.mapper.IMapper` has been defined to
154 * A new interface, :class:`IPython.kernel.mapper.IMapper` has been defined to
152 standardize the idea of a `map` method. This interface has a single `map`
155 standardize the idea of a `map` method. This interface has a single `map`
153 method that has the same syntax as the built-in `map`. We have also defined
156 method that has the same syntax as the built-in `map`. We have also defined
154 a `mapper` factory interface that creates objects that implement
157 a `mapper` factory interface that creates objects that implement
155 :class:`IPython.kernel.mapper.IMapper` for different controllers. Both the
158 :class:`IPython.kernel.mapper.IMapper` for different controllers. Both the
156 multiengine and task controller now have mapping capabilties.
159 multiengine and task controller now have mapping capabilties.
157
160
158 * The parallel function capabilities have been reworks. The major changes are
161 * The parallel function capabilities have been reworks. The major changes are
159 that i) there is now an `@parallel` magic that creates parallel functions,
162 that i) there is now an `@parallel` magic that creates parallel functions,
160 ii) the syntax for mulitple variable follows that of `map`, iii) both the
163 ii) the syntax for mulitple variable follows that of `map`, iii) both the
161 multiengine and task controller now have a parallel function implementation.
164 multiengine and task controller now have a parallel function implementation.
162
165
163 * All of the parallel computing capabilities from `ipython1-dev` have been
166 * All of the parallel computing capabilities from `ipython1-dev` have been
164 merged into IPython proper. This resulted in the following new subpackages:
167 merged into IPython proper. This resulted in the following new subpackages:
165 :mod:`IPython.kernel`, :mod:`IPython.kernel.core`, :mod:`IPython.config`,
168 :mod:`IPython.kernel`, :mod:`IPython.kernel.core`, :mod:`IPython.config`,
166 :mod:`IPython.tools` and :mod:`IPython.testing`.
169 :mod:`IPython.tools` and :mod:`IPython.testing`.
167
170
168 * As part of merging in the `ipython1-dev` stuff, the `setup.py` script and
171 * As part of merging in the `ipython1-dev` stuff, the `setup.py` script and
169 friends have been completely refactored. Now we are checking for
172 friends have been completely refactored. Now we are checking for
170 dependencies using the approach that matplotlib uses.
173 dependencies using the approach that matplotlib uses.
171
174
172 * The documentation has been completely reorganized to accept the
175 * The documentation has been completely reorganized to accept the
173 documentation from `ipython1-dev`.
176 documentation from `ipython1-dev`.
174
177
175 * We have switched to using Foolscap for all of our network protocols in
178 * We have switched to using Foolscap for all of our network protocols in
176 :mod:`IPython.kernel`. This gives us secure connections that are both
179 :mod:`IPython.kernel`. This gives us secure connections that are both
177 encrypted and authenticated.
180 encrypted and authenticated.
178
181
179 * We have a brand new `COPYING.txt` files that describes the IPython license
182 * We have a brand new `COPYING.txt` files that describes the IPython license
180 and copyright. The biggest change is that we are putting "The IPython
183 and copyright. The biggest change is that we are putting "The IPython
181 Development Team" as the copyright holder. We give more details about
184 Development Team" as the copyright holder. We give more details about
182 exactly what this means in this file. All developer should read this and use
185 exactly what this means in this file. All developer should read this and use
183 the new banner in all IPython source code files.
186 the new banner in all IPython source code files.
184
187
185 * sh profile: ./foo runs foo as system command, no need to do !./foo anymore
188 * sh profile: ./foo runs foo as system command, no need to do !./foo anymore
186
189
187 * String lists now support ``sort(field, nums = True)`` method (to easily sort
190 * String lists now support ``sort(field, nums = True)`` method (to easily sort
188 system command output). Try it with ``a = !ls -l ; a.sort(1, nums=1)``.
191 system command output). Try it with ``a = !ls -l ; a.sort(1, nums=1)``.
189
192
190 * '%cpaste foo' now assigns the pasted block as string list, instead of string
193 * '%cpaste foo' now assigns the pasted block as string list, instead of string
191
194
192 * The ipcluster script now run by default with no security. This is done
195 * The ipcluster script now run by default with no security. This is done
193 because the main usage of the script is for starting things on localhost.
196 because the main usage of the script is for starting things on localhost.
194 Eventually when ipcluster is able to start things on other hosts, we will put
197 Eventually when ipcluster is able to start things on other hosts, we will put
195 security back.
198 security back.
196
199
197 * 'cd --foo' searches directory history for string foo, and jumps to that dir.
200 * 'cd --foo' searches directory history for string foo, and jumps to that dir.
198 Last part of dir name is checked first. If no matches for that are found,
201 Last part of dir name is checked first. If no matches for that are found,
199 look at the whole path.
202 look at the whole path.
200
203
201
204
202 Bug fixes
205 Bug fixes
203 ---------
206 ---------
204
207
205 * The Windows installer has been fixed. Now all IPython scripts have ``.bat``
208 * The Windows installer has been fixed. Now all IPython scripts have ``.bat``
206 versions created. Also, the Start Menu shortcuts have been updated.
209 versions created. Also, the Start Menu shortcuts have been updated.
207
210
208 * The colors escapes in the multiengine client are now turned off on win32 as
211 * The colors escapes in the multiengine client are now turned off on win32 as
209 they don't print correctly.
212 they don't print correctly.
210
213
211 * The :mod:`IPython.kernel.scripts.ipengine` script was exec'ing
214 * The :mod:`IPython.kernel.scripts.ipengine` script was exec'ing
212 mpi_import_statement incorrectly, which was leading the engine to crash when
215 mpi_import_statement incorrectly, which was leading the engine to crash when
213 mpi was enabled.
216 mpi was enabled.
214
217
215 * A few subpackages had missing ``__init__.py`` files.
218 * A few subpackages had missing ``__init__.py`` files.
216
219
217 * The documentation is only created if Sphinx is found. Previously, the
220 * The documentation is only created if Sphinx is found. Previously, the
218 ``setup.py`` script would fail if it was missing.
221 ``setup.py`` script would fail if it was missing.
219
222
220 * Greedy ``cd`` completion has been disabled again (it was enabled in 0.8.4) as
223 * Greedy ``cd`` completion has been disabled again (it was enabled in 0.8.4) as
221 it caused problems on certain platforms.
224 it caused problems on certain platforms.
222
225
223
226
224 Backwards incompatible changes
227 Backwards incompatible changes
225 ------------------------------
228 ------------------------------
226
229
227 * The ``clusterfile`` options of the :command:`ipcluster` command has been
230 * The ``clusterfile`` options of the :command:`ipcluster` command has been
228 removed as it was not working and it will be replaced soon by something much
231 removed as it was not working and it will be replaced soon by something much
229 more robust.
232 more robust.
230
233
231 * The :mod:`IPython.kernel` configuration now properly find the user's
234 * The :mod:`IPython.kernel` configuration now properly find the user's
232 IPython directory.
235 IPython directory.
233
236
234 * In ipapi, the :func:`make_user_ns` function has been replaced with
237 * In ipapi, the :func:`make_user_ns` function has been replaced with
235 :func:`make_user_namespaces`, to support dict subclasses in namespace
238 :func:`make_user_namespaces`, to support dict subclasses in namespace
236 creation.
239 creation.
237
240
238 * :class:`IPython.kernel.client.Task` has been renamed
241 * :class:`IPython.kernel.client.Task` has been renamed
239 :class:`IPython.kernel.client.StringTask` to make way for new task types.
242 :class:`IPython.kernel.client.StringTask` to make way for new task types.
240
243
241 * The keyword argument `style` has been renamed `dist` in `scatter`, `gather`
244 * The keyword argument `style` has been renamed `dist` in `scatter`, `gather`
242 and `map`.
245 and `map`.
243
246
244 * Renamed the values that the rename `dist` keyword argument can have from
247 * Renamed the values that the rename `dist` keyword argument can have from
245 `'basic'` to `'b'`.
248 `'basic'` to `'b'`.
246
249
247 * IPython has a larger set of dependencies if you want all of its capabilities.
250 * IPython has a larger set of dependencies if you want all of its capabilities.
248 See the `setup.py` script for details.
251 See the `setup.py` script for details.
249
252
250 * The constructors for :class:`IPython.kernel.client.MultiEngineClient` and
253 * The constructors for :class:`IPython.kernel.client.MultiEngineClient` and
251 :class:`IPython.kernel.client.TaskClient` no longer take the (ip,port) tuple.
254 :class:`IPython.kernel.client.TaskClient` no longer take the (ip,port) tuple.
252 Instead they take the filename of a file that contains the FURL for that
255 Instead they take the filename of a file that contains the FURL for that
253 client. If the FURL file is in your IPYTHONDIR, it will be found automatically
256 client. If the FURL file is in your IPYTHONDIR, it will be found automatically
254 and the constructor can be left empty.
257 and the constructor can be left empty.
255
258
256 * The asynchronous clients in :mod:`IPython.kernel.asyncclient` are now created
259 * The asynchronous clients in :mod:`IPython.kernel.asyncclient` are now created
257 using the factory functions :func:`get_multiengine_client` and
260 using the factory functions :func:`get_multiengine_client` and
258 :func:`get_task_client`. These return a `Deferred` to the actual client.
261 :func:`get_task_client`. These return a `Deferred` to the actual client.
259
262
260 * The command line options to `ipcontroller` and `ipengine` have changed to
263 * The command line options to `ipcontroller` and `ipengine` have changed to
261 reflect the new Foolscap network protocol and the FURL files. Please see the
264 reflect the new Foolscap network protocol and the FURL files. Please see the
262 help for these scripts for details.
265 help for these scripts for details.
263
266
264 * The configuration files for the kernel have changed because of the Foolscap
267 * The configuration files for the kernel have changed because of the Foolscap
265 stuff. If you were using custom config files before, you should delete them
268 stuff. If you were using custom config files before, you should delete them
266 and regenerate new ones.
269 and regenerate new ones.
267
270
268 Changes merged in from IPython1
271 Changes merged in from IPython1
269 -------------------------------
272 -------------------------------
270
273
271 New features
274 New features
272 ............
275 ............
273
276
274 * Much improved ``setup.py`` and ``setupegg.py`` scripts. Because Twisted and
277 * Much improved ``setup.py`` and ``setupegg.py`` scripts. Because Twisted and
275 zope.interface are now easy installable, we can declare them as dependencies
278 zope.interface are now easy installable, we can declare them as dependencies
276 in our setupegg.py script.
279 in our setupegg.py script.
277
280
278 * IPython is now compatible with Twisted 2.5.0 and 8.x.
281 * IPython is now compatible with Twisted 2.5.0 and 8.x.
279
282
280 * Added a new example of how to use :mod:`ipython1.kernel.asynclient`.
283 * Added a new example of how to use :mod:`ipython1.kernel.asynclient`.
281
284
282 * Initial draft of a process daemon in :mod:`ipython1.daemon`. This has not
285 * Initial draft of a process daemon in :mod:`ipython1.daemon`. This has not
283 been merged into IPython and is still in `ipython1-dev`.
286 been merged into IPython and is still in `ipython1-dev`.
284
287
285 * The ``TaskController`` now has methods for getting the queue status.
288 * The ``TaskController`` now has methods for getting the queue status.
286
289
287 * The ``TaskResult`` objects not have information about how long the task
290 * The ``TaskResult`` objects not have information about how long the task
288 took to run.
291 took to run.
289
292
290 * We are attaching additional attributes to exceptions ``(_ipython_*)`` that
293 * We are attaching additional attributes to exceptions ``(_ipython_*)`` that
291 we use to carry additional info around.
294 we use to carry additional info around.
292
295
293 * New top-level module :mod:`asyncclient` that has asynchronous versions (that
296 * New top-level module :mod:`asyncclient` that has asynchronous versions (that
294 return deferreds) of the client classes. This is designed to users who want
297 return deferreds) of the client classes. This is designed to users who want
295 to run their own Twisted reactor.
298 to run their own Twisted reactor.
296
299
297 * All the clients in :mod:`client` are now based on Twisted. This is done by
300 * All the clients in :mod:`client` are now based on Twisted. This is done by
298 running the Twisted reactor in a separate thread and using the
301 running the Twisted reactor in a separate thread and using the
299 :func:`blockingCallFromThread` function that is in recent versions of Twisted.
302 :func:`blockingCallFromThread` function that is in recent versions of Twisted.
300
303
301 * Functions can now be pushed/pulled to/from engines using
304 * Functions can now be pushed/pulled to/from engines using
302 :meth:`MultiEngineClient.push_function` and
305 :meth:`MultiEngineClient.push_function` and
303 :meth:`MultiEngineClient.pull_function`.
306 :meth:`MultiEngineClient.pull_function`.
304
307
305 * Gather/scatter are now implemented in the client to reduce the work load
308 * Gather/scatter are now implemented in the client to reduce the work load
306 of the controller and improve performance.
309 of the controller and improve performance.
307
310
308 * Complete rewrite of the IPython docuementation. All of the documentation
311 * Complete rewrite of the IPython docuementation. All of the documentation
309 from the IPython website has been moved into docs/source as restructured
312 from the IPython website has been moved into docs/source as restructured
310 text documents. PDF and HTML documentation are being generated using
313 text documents. PDF and HTML documentation are being generated using
311 Sphinx.
314 Sphinx.
312
315
313 * New developer oriented documentation: development guidelines and roadmap.
316 * New developer oriented documentation: development guidelines and roadmap.
314
317
315 * Traditional ``ChangeLog`` has been changed to a more useful ``changes.txt``
318 * Traditional ``ChangeLog`` has been changed to a more useful ``changes.txt``
316 file that is organized by release and is meant to provide something more
319 file that is organized by release and is meant to provide something more
317 relevant for users.
320 relevant for users.
318
321
319 Bug fixes
322 Bug fixes
320 .........
323 .........
321
324
322 * Created a proper ``MANIFEST.in`` file to create source distributions.
325 * Created a proper ``MANIFEST.in`` file to create source distributions.
323
326
324 * Fixed a bug in the ``MultiEngine`` interface. Previously, multi-engine
327 * Fixed a bug in the ``MultiEngine`` interface. Previously, multi-engine
325 actions were being collected with a :class:`DeferredList` with
328 actions were being collected with a :class:`DeferredList` with
326 ``fireononeerrback=1``. This meant that methods were returning
329 ``fireononeerrback=1``. This meant that methods were returning
327 before all engines had given their results. This was causing extremely odd
330 before all engines had given their results. This was causing extremely odd
328 bugs in certain cases. To fix this problem, we have 1) set
331 bugs in certain cases. To fix this problem, we have 1) set
329 ``fireononeerrback=0`` to make sure all results (or exceptions) are in
332 ``fireononeerrback=0`` to make sure all results (or exceptions) are in
330 before returning and 2) introduced a :exc:`CompositeError` exception
333 before returning and 2) introduced a :exc:`CompositeError` exception
331 that wraps all of the engine exceptions. This is a huge change as it means
334 that wraps all of the engine exceptions. This is a huge change as it means
332 that users will have to catch :exc:`CompositeError` rather than the actual
335 that users will have to catch :exc:`CompositeError` rather than the actual
333 exception.
336 exception.
334
337
335 Backwards incompatible changes
338 Backwards incompatible changes
336 ..............................
339 ..............................
337
340
338 * All names have been renamed to conform to the lowercase_with_underscore
341 * All names have been renamed to conform to the lowercase_with_underscore
339 convention. This will require users to change references to all names like
342 convention. This will require users to change references to all names like
340 ``queueStatus`` to ``queue_status``.
343 ``queueStatus`` to ``queue_status``.
341
344
342 * Previously, methods like :meth:`MultiEngineClient.push` and
345 * Previously, methods like :meth:`MultiEngineClient.push` and
343 :meth:`MultiEngineClient.push` used ``*args`` and ``**kwargs``. This was
346 :meth:`MultiEngineClient.push` used ``*args`` and ``**kwargs``. This was
344 becoming a problem as we weren't able to introduce new keyword arguments into
347 becoming a problem as we weren't able to introduce new keyword arguments into
345 the API. Now these methods simple take a dict or sequence. This has also
348 the API. Now these methods simple take a dict or sequence. This has also
346 allowed us to get rid of the ``*All`` methods like :meth:`pushAll` and
349 allowed us to get rid of the ``*All`` methods like :meth:`pushAll` and
347 :meth:`pullAll`. These things are now handled with the ``targets`` keyword
350 :meth:`pullAll`. These things are now handled with the ``targets`` keyword
348 argument that defaults to ``'all'``.
351 argument that defaults to ``'all'``.
349
352
350 * The :attr:`MultiEngineClient.magicTargets` has been renamed to
353 * The :attr:`MultiEngineClient.magicTargets` has been renamed to
351 :attr:`MultiEngineClient.targets`.
354 :attr:`MultiEngineClient.targets`.
352
355
353 * All methods in the MultiEngine interface now accept the optional keyword
356 * All methods in the MultiEngine interface now accept the optional keyword
354 argument ``block``.
357 argument ``block``.
355
358
356 * Renamed :class:`RemoteController` to :class:`MultiEngineClient` and
359 * Renamed :class:`RemoteController` to :class:`MultiEngineClient` and
357 :class:`TaskController` to :class:`TaskClient`.
360 :class:`TaskController` to :class:`TaskClient`.
358
361
359 * Renamed the top-level module from :mod:`api` to :mod:`client`.
362 * Renamed the top-level module from :mod:`api` to :mod:`client`.
360
363
361 * Most methods in the multiengine interface now raise a :exc:`CompositeError`
364 * Most methods in the multiengine interface now raise a :exc:`CompositeError`
362 exception that wraps the user's exceptions, rather than just raising the raw
365 exception that wraps the user's exceptions, rather than just raising the raw
363 user's exception.
366 user's exception.
364
367
365 * Changed the ``setupNS`` and ``resultNames`` in the ``Task`` class to ``push``
368 * Changed the ``setupNS`` and ``resultNames`` in the ``Task`` class to ``push``
366 and ``pull``.
369 and ``pull``.
367
370
368
371
369 Release 0.8.4
372 Release 0.8.4
370 =============
373 =============
371
374
372 This was a quick release to fix an unfortunate bug that slipped into the 0.8.3
375 This was a quick release to fix an unfortunate bug that slipped into the 0.8.3
373 release. The ``--twisted`` option was disabled, as it turned out to be broken
376 release. The ``--twisted`` option was disabled, as it turned out to be broken
374 across several platforms.
377 across several platforms.
375
378
376
379
377 Release 0.8.3
380 Release 0.8.3
378 =============
381 =============
379
382
380 * pydb is now disabled by default (due to %run -d problems). You can enable
383 * pydb is now disabled by default (due to %run -d problems). You can enable
381 it by passing -pydb command line argument to IPython. Note that setting
384 it by passing -pydb command line argument to IPython. Note that setting
382 it in config file won't work.
385 it in config file won't work.
383
386
384
387
385 Release 0.8.2
388 Release 0.8.2
386 =============
389 =============
387
390
388 * %pushd/%popd behave differently; now "pushd /foo" pushes CURRENT directory
391 * %pushd/%popd behave differently; now "pushd /foo" pushes CURRENT directory
389 and jumps to /foo. The current behaviour is closer to the documented
392 and jumps to /foo. The current behaviour is closer to the documented
390 behaviour, and should not trip anyone.
393 behaviour, and should not trip anyone.
391
394
392
395
393 Older releases
396 Older releases
394 ==============
397 ==============
395
398
396 Changes in earlier releases of IPython are described in the older file
399 Changes in earlier releases of IPython are described in the older file
397 ``ChangeLog``. Please refer to this document for details.
400 ``ChangeLog``. Please refer to this document for details.
398
401
@@ -1,454 +1,485 b''
1 .. _development:
1 .. _development:
2
2
3 ==============================
3 ==============================
4 IPython development guidelines
4 IPython development guidelines
5 ==============================
5 ==============================
6
6
7
7
8 Overview
8 Overview
9 ========
9 ========
10
10
11 This document describes IPython from the perspective of developers. Most
11 This document describes IPython from the perspective of developers. Most
12 importantly, it gives information for people who want to contribute to the
12 importantly, it gives information for people who want to contribute to the
13 development of IPython. So if you want to help out, read on!
13 development of IPython. So if you want to help out, read on!
14
14
15 How to contribute to IPython
15 How to contribute to IPython
16 ============================
16 ============================
17
17
18 IPython development is done using Bazaar [Bazaar]_ and Launchpad [Launchpad]_.
18 IPython development is done using Bazaar [Bazaar]_ and Launchpad [Launchpad]_.
19 This makes it easy for people to contribute to the development of IPython.
19 This makes it easy for people to contribute to the development of IPython.
20 There are several ways in which you can join in.
20 There are several ways in which you can join in.
21
21
22 If you have a small change that you want to send to the team, you can edit your
22 If you have a small change that you want to send to the team, you can edit your
23 bazaar checkout of IPython (see below) in-place, and ask bazaar for the
23 bazaar checkout of IPython (see below) in-place, and ask bazaar for the
24 differences::
24 differences::
25
25
26 $ cd /path/to/your/copy/of/ipython
26 $ cd /path/to/your/copy/of/ipython
27 $ bzr diff > my_fixes.diff
27 $ bzr diff > my_fixes.diff
28
28
29 This produces a patch file with your fixes, which we can apply to the source
29 This produces a patch file with your fixes, which we can apply to the source
30 tree. This file should then be attached to a ticket in our `bug tracker
30 tree. This file should then be attached to a ticket in our `bug tracker
31 <https://bugs.launchpad.net/ipython>`_, indicating what it does.
31 <https://bugs.launchpad.net/ipython>`_, indicating what it does.
32
32
33 This model of creating small, self-contained patches works very well and there
33 This model of creating small, self-contained patches works very well and there
34 are open source projects that do their entire development this way. However,
34 are open source projects that do their entire development this way. However,
35 in IPython we have found that for tracking larger changes, making use of
35 in IPython we have found that for tracking larger changes, making use of
36 bazaar's full capabilities in conjunction with Launchpad's code hosting
36 bazaar's full capabilities in conjunction with Launchpad's code hosting
37 services makes for a much better experience.
37 services makes for a much better experience.
38
38
39 Making your own branch of IPython allows you to refine your changes over time,
39 Making your own branch of IPython allows you to refine your changes over time,
40 track the development of the main team, and propose your own full version of
40 track the development of the main team, and propose your own full version of
41 the code for others to use and review, with a minimum amount of fuss. The next
41 the code for others to use and review, with a minimum amount of fuss. The next
42 parts of this document will explain how to do this.
42 parts of this document will explain how to do this.
43
43
44 Install Bazaar and create a Launchpad account
44 Install Bazaar and create a Launchpad account
45 ---------------------------------------------
45 ---------------------------------------------
46
46
47 First make sure you have installed Bazaar (see their `website
47 First make sure you have installed Bazaar (see their `website
48 <http://bazaar-vcs.org/>`_). To see that Bazaar is installed and knows about
48 <http://bazaar-vcs.org/>`_). To see that Bazaar is installed and knows about
49 you, try the following::
49 you, try the following::
50
50
51 $ bzr whoami
51 $ bzr whoami
52 Joe Coder <jcoder@gmail.com>
52 Joe Coder <jcoder@gmail.com>
53
53
54 This should display your name and email. Next, you will want to create an
54 This should display your name and email. Next, you will want to create an
55 account on the `Launchpad website <http://www.launchpad.net>`_ and setup your
55 account on the `Launchpad website <http://www.launchpad.net>`_ and setup your
56 ssh keys. For more information of setting up your ssh keys, see `this link
56 ssh keys. For more information of setting up your ssh keys, see `this link
57 <https://help.launchpad.net/YourAccount/CreatingAnSSHKeyPair>`_.
57 <https://help.launchpad.net/YourAccount/CreatingAnSSHKeyPair>`_.
58
58
59 Get the main IPython branch from Launchpad
59 Get the main IPython branch from Launchpad
60 ------------------------------------------
60 ------------------------------------------
61
61
62 Now, you can get a copy of the main IPython development branch (we call this
62 Now, you can get a copy of the main IPython development branch (we call this
63 the "trunk")::
63 the "trunk")::
64
64
65 $ bzr branch lp:ipython
65 $ bzr branch lp:ipython
66
66
67 Create a working branch
67 Create a working branch
68 -----------------------
68 -----------------------
69
69
70 When working on IPython, you won't actually make edits directly to the
70 When working on IPython, you won't actually make edits directly to the
71 :file:`lp:ipython` branch. Instead, you will create a separate branch for your
71 :file:`lp:ipython` branch. Instead, you will create a separate branch for your
72 changes. For now, let's assume you want to do your work in a branch named
72 changes. For now, let's assume you want to do your work in a branch named
73 "ipython-mybranch". Create this branch by doing::
73 "ipython-mybranch". Create this branch by doing::
74
74
75 $ bzr branch ipython ipython-mybranch
75 $ bzr branch ipython ipython-mybranch
76
76
77 When you actually create a branch, you will want to give it a name that
77 When you actually create a branch, you will want to give it a name that
78 reflects the nature of the work that you will be doing in it, like
78 reflects the nature of the work that you will be doing in it, like
79 "install-docs-update".
79 "install-docs-update".
80
80
81 Make edits in your working branch
81 Make edits in your working branch
82 ---------------------------------
82 ---------------------------------
83
83
84 Now you are ready to actually make edits in your :file:`ipython-mybranch`
84 Now you are ready to actually make edits in your :file:`ipython-mybranch`
85 branch. Before doing this, it is helpful to install this branch so you can
85 branch. Before doing this, it is helpful to install this branch so you can
86 test your changes as you work. This is easiest if you have setuptools
86 test your changes as you work. This is easiest if you have setuptools
87 installed. Then, just do::
87 installed. Then, just do::
88
88
89 $ cd ipython-mybranch
89 $ cd ipython-mybranch
90 $ python setupegg.py develop
90 $ python setupegg.py develop
91
91
92 Now, make some changes. After a while, you will want to commit your changes.
92 Now, make some changes. After a while, you will want to commit your changes.
93 This let's Bazaar know that you like the changes you have made and gives you
93 This let's Bazaar know that you like the changes you have made and gives you
94 an opportunity to keep a nice record of what you have done. This looks like
94 an opportunity to keep a nice record of what you have done. This looks like
95 this::
95 this::
96
96
97 $ ...do work in ipython-mybranch...
97 $ ...do work in ipython-mybranch...
98 $ bzr commit -m "the commit message goes here"
98 $ bzr commit -m "the commit message goes here"
99
99
100 Please note that since we now don't use an old-style linear ChangeLog (that
100 Please note that since we now don't use an old-style linear ChangeLog (that
101 tends to cause problems with distributed version control systems), you should
101 tends to cause problems with distributed version control systems), you should
102 ensure that your log messages are reasonably detailed. Use a docstring-like
102 ensure that your log messages are reasonably detailed. Use a docstring-like
103 approach in the commit messages (including the second line being left
103 approach in the commit messages (including the second line being left
104 *blank*)::
104 *blank*)::
105
105
106 Single line summary of changes being committed.
106 Single line summary of changes being committed.
107
107
108 * more details when warranted ...
108 * more details when warranted ...
109 * including crediting outside contributors if they sent the
109 * including crediting outside contributors if they sent the
110 code/bug/idea!
110 code/bug/idea!
111
111
112 As you work, you will repeat this edit/commit cycle many times. If you work on
112 As you work, you will repeat this edit/commit cycle many times. If you work on
113 your branch for a long time, you will also want to get the latest changes from
113 your branch for a long time, you will also want to get the latest changes from
114 the :file:`lp:ipython` branch. This can be done with the following sequence of
114 the :file:`lp:ipython` branch. This can be done with the following sequence of
115 commands::
115 commands::
116
116
117 $ ls
117 $ ls
118 ipython
118 ipython
119 ipython-mybranch
119 ipython-mybranch
120
120
121 $ cd ipython
121 $ cd ipython
122 $ bzr pull
122 $ bzr pull
123 $ cd ../ipython-mybranch
123 $ cd ../ipython-mybranch
124 $ bzr merge ../ipython
124 $ bzr merge ../ipython
125 $ bzr commit -m "Merging changes from trunk"
125 $ bzr commit -m "Merging changes from trunk"
126
126
127 Along the way, you should also run the IPython test suite. You can do this
127 Along the way, you should also run the IPython test suite. You can do this
128 using the :command:`iptest` command (which is basically a customized version of
128 using the :command:`iptest` command (which is basically a customized version of
129 :command:`nosetests`)::
129 :command:`nosetests`)::
130
130
131 $ cd
131 $ cd
132 $ iptest
132 $ iptest
133
133
134 The :command:`iptest` command will also pick up and run any tests you have
134 The :command:`iptest` command will also pick up and run any tests you have
135 written. See :ref:`_devel_testing` for further details on the testing system.
135 written. See :ref:`_devel_testing` for further details on the testing system.
136
136
137
137
138 Post your branch and request a code review
138 Post your branch and request a code review
139 ------------------------------------------
139 ------------------------------------------
140
140
141 Once you are done with your edits, you should post your branch on Launchpad so
141 Once you are done with your edits, you should post your branch on Launchpad so
142 that other IPython developers can review the changes and help you merge your
142 that other IPython developers can review the changes and help you merge your
143 changes into the main development branch. To post your branch on Launchpad,
143 changes into the main development branch. To post your branch on Launchpad,
144 do::
144 do::
145
145
146 $ cd ipython-mybranch
146 $ cd ipython-mybranch
147 $ bzr push lp:~yourusername/ipython/ipython-mybranch
147 $ bzr push lp:~yourusername/ipython/ipython-mybranch
148
148
149 Then, go to the `IPython Launchpad site <www.launchpad.net/ipython>`_, and you
149 Then, go to the `IPython Launchpad site <www.launchpad.net/ipython>`_, and you
150 should see your branch under the "Code" tab. If you click on your branch, you
150 should see your branch under the "Code" tab. If you click on your branch, you
151 can provide a short description of the branch as well as mark its status. Most
151 can provide a short description of the branch as well as mark its status. Most
152 importantly, you should click the link that reads "Propose for merging into
152 importantly, you should click the link that reads "Propose for merging into
153 another branch". What does this do?
153 another branch". What does this do?
154
154
155 This let's the other IPython developers know that your branch is ready to be
155 This let's the other IPython developers know that your branch is ready to be
156 reviewed and merged into the main development branch. During this review
156 reviewed and merged into the main development branch. During this review
157 process, other developers will give you feedback and help you get your code
157 process, other developers will give you feedback and help you get your code
158 ready to be merged. What types of things will we be looking for:
158 ready to be merged. What types of things will we be looking for:
159
159
160 * All code is documented.
160 * All code is documented.
161 * All code has tests.
161 * All code has tests.
162 * The entire IPython test suite passes.
162 * The entire IPython test suite passes.
163
163
164 Once your changes have been reviewed and approved, someone will merge them
164 Once your changes have been reviewed and approved, someone will merge them
165 into the main development branch.
165 into the main development branch.
166
166
167 Documentation
167 Documentation
168 =============
168 =============
169
169
170 Standalone documentation
170 Standalone documentation
171 ------------------------
171 ------------------------
172
172
173 All standalone documentation should be written in plain text (``.txt``) files
173 All standalone documentation should be written in plain text (``.txt``) files
174 using reStructuredText [reStructuredText]_ for markup and formatting. All such
174 using reStructuredText [reStructuredText]_ for markup and formatting. All such
175 documentation should be placed in directory :file:`docs/source` of the IPython
175 documentation should be placed in directory :file:`docs/source` of the IPython
176 source tree. The documentation in this location will serve as the main source
176 source tree. The documentation in this location will serve as the main source
177 for IPython documentation and all existing documentation should be converted
177 for IPython documentation and all existing documentation should be converted
178 to this format.
178 to this format.
179
179
180 To build the final documentation, we use Sphinx [Sphinx]_. Once you have
180 To build the final documentation, we use Sphinx [Sphinx]_. Once you have
181 Sphinx installed, you can build the html docs yourself by doing::
181 Sphinx installed, you can build the html docs yourself by doing::
182
182
183 $ cd ipython-mybranch/docs
183 $ cd ipython-mybranch/docs
184 $ make html
184 $ make html
185
185
186 Docstring format
186 Docstring format
187 ----------------
187 ----------------
188
188
189 Good docstrings are very important. All new code should have docstrings that
189 Good docstrings are very important. All new code should have docstrings that
190 are formatted using reStructuredText for markup and formatting, since it is
190 are formatted using reStructuredText for markup and formatting, since it is
191 understood by a wide variety of tools. Details about using reStructuredText
191 understood by a wide variety of tools. Details about using reStructuredText
192 for docstrings can be found `here
192 for docstrings can be found `here
193 <http://epydoc.sourceforge.net/manual-othermarkup.html>`_.
193 <http://epydoc.sourceforge.net/manual-othermarkup.html>`_.
194
194
195 Additional PEPs of interest regarding documentation of code:
195 Additional PEPs of interest regarding documentation of code:
196
196
197 * `Docstring Conventions <http://www.python.org/peps/pep-0257.html>`_
197 * `Docstring Conventions <http://www.python.org/peps/pep-0257.html>`_
198 * `Docstring Processing System Framework <http://www.python.org/peps/pep-0256.html>`_
198 * `Docstring Processing System Framework <http://www.python.org/peps/pep-0256.html>`_
199 * `Docutils Design Specification <http://www.python.org/peps/pep-0258.html>`_
199 * `Docutils Design Specification <http://www.python.org/peps/pep-0258.html>`_
200
200
201
201
202 Coding conventions
202 Coding conventions
203 ==================
203 ==================
204
204
205 General
205 General
206 -------
206 -------
207
207
208 In general, we'll try to follow the standard Python style conventions as
208 In general, we'll try to follow the standard Python style conventions as
209 described here:
209 described here:
210
210
211 * `Style Guide for Python Code <http://www.python.org/peps/pep-0008.html>`_
211 * `Style Guide for Python Code <http://www.python.org/peps/pep-0008.html>`_
212
212
213
213
214 Other comments:
214 Other comments:
215
215
216 * In a large file, top level classes and functions should be
216 * In a large file, top level classes and functions should be
217 separated by 2-3 lines to make it easier to separate them visually.
217 separated by 2-3 lines to make it easier to separate them visually.
218 * Use 4 spaces for indentation.
218 * Use 4 spaces for indentation.
219 * Keep the ordering of methods the same in classes that have the same
219 * Keep the ordering of methods the same in classes that have the same
220 methods. This is particularly true for classes that implement an interface.
220 methods. This is particularly true for classes that implement an interface.
221
221
222 Naming conventions
222 Naming conventions
223 ------------------
223 ------------------
224
224
225 In terms of naming conventions, we'll follow the guidelines from the `Style
225 In terms of naming conventions, we'll follow the guidelines from the `Style
226 Guide for Python Code`_.
226 Guide for Python Code`_.
227
227
228 For all new IPython code (and much existing code is being refactored), we'll
228 For all new IPython code (and much existing code is being refactored), we'll
229 use:
229 use:
230
230
231 * All ``lowercase`` module names.
231 * All ``lowercase`` module names.
232
232
233 * ``CamelCase`` for class names.
233 * ``CamelCase`` for class names.
234
234
235 * ``lowercase_with_underscores`` for methods, functions, variables and
235 * ``lowercase_with_underscores`` for methods, functions, variables and
236 attributes.
236 attributes.
237
237
238 There are, however, some important exceptions to these rules. In some cases,
238 There are, however, some important exceptions to these rules. In some cases,
239 IPython code will interface with packages (Twisted, Wx, Qt) that use other
239 IPython code will interface with packages (Twisted, Wx, Qt) that use other
240 conventions. At some level this makes it impossible to adhere to our own
240 conventions. At some level this makes it impossible to adhere to our own
241 standards at all times. In particular, when subclassing classes that use other
241 standards at all times. In particular, when subclassing classes that use other
242 naming conventions, you must follow their naming conventions. To deal with
242 naming conventions, you must follow their naming conventions. To deal with
243 cases like this, we propose the following policy:
243 cases like this, we propose the following policy:
244
244
245 * If you are subclassing a class that uses different conventions, use its
245 * If you are subclassing a class that uses different conventions, use its
246 naming conventions throughout your subclass. Thus, if you are creating a
246 naming conventions throughout your subclass. Thus, if you are creating a
247 Twisted Protocol class, used Twisted's
247 Twisted Protocol class, used Twisted's
248 ``namingSchemeForMethodsAndAttributes.``
248 ``namingSchemeForMethodsAndAttributes.``
249
249
250 * All IPython's official interfaces should use our conventions. In some cases
250 * All IPython's official interfaces should use our conventions. In some cases
251 this will mean that you need to provide shadow names (first implement
251 this will mean that you need to provide shadow names (first implement
252 ``fooBar`` and then ``foo_bar = fooBar``). We want to avoid this at all
252 ``fooBar`` and then ``foo_bar = fooBar``). We want to avoid this at all
253 costs, but it will probably be necessary at times. But, please use this
253 costs, but it will probably be necessary at times. But, please use this
254 sparingly!
254 sparingly!
255
255
256 Implementation-specific *private* methods will use
256 Implementation-specific *private* methods will use
257 ``_single_underscore_prefix``. Names with a leading double underscore will
257 ``_single_underscore_prefix``. Names with a leading double underscore will
258 *only* be used in special cases, as they makes subclassing difficult (such
258 *only* be used in special cases, as they makes subclassing difficult (such
259 names are not easily seen by child classes).
259 names are not easily seen by child classes).
260
260
261 Occasionally some run-in lowercase names are used, but mostly for very short
261 Occasionally some run-in lowercase names are used, but mostly for very short
262 names or where we are implementing methods very similar to existing ones in a
262 names or where we are implementing methods very similar to existing ones in a
263 base class (like ``runlines()`` where ``runsource()`` and ``runcode()`` had
263 base class (like ``runlines()`` where ``runsource()`` and ``runcode()`` had
264 established precedent).
264 established precedent).
265
265
266 The old IPython codebase has a big mix of classes and modules prefixed with an
266 The old IPython codebase has a big mix of classes and modules prefixed with an
267 explicit ``IP``. In Python this is mostly unnecessary, redundant and frowned
267 explicit ``IP``. In Python this is mostly unnecessary, redundant and frowned
268 upon, as namespaces offer cleaner prefixing. The only case where this approach
268 upon, as namespaces offer cleaner prefixing. The only case where this approach
269 is justified is for classes which are expected to be imported into external
269 is justified is for classes which are expected to be imported into external
270 namespaces and a very generic name (like Shell) is too likely to clash with
270 namespaces and a very generic name (like Shell) is too likely to clash with
271 something else. We'll need to revisit this issue as we clean up and refactor
271 something else. We'll need to revisit this issue as we clean up and refactor
272 the code, but in general we should remove as many unnecessary ``IP``/``ip``
272 the code, but in general we should remove as many unnecessary ``IP``/``ip``
273 prefixes as possible. However, if a prefix seems absolutely necessary the more
273 prefixes as possible. However, if a prefix seems absolutely necessary the more
274 specific ``IPY`` or ``ipy`` are preferred.
274 specific ``IPY`` or ``ipy`` are preferred.
275
275
276 .. _devel_testing:
276 .. _devel_testing:
277
277
278 Testing system
278 Testing system
279 ==============
279 ==============
280
280
281 It is extremely important that all code contributed to IPython has tests.
281 It is extremely important that all code contributed to IPython has tests.
282 Tests should be written as unittests, doctests or as entities that the Nose
282 Tests should be written as unittests, doctests or as entities that the Nose
283 [Nose]_ testing package will find. Regardless of how the tests are written, we
283 [Nose]_ testing package will find. Regardless of how the tests are written, we
284 will use Nose for discovering and running the tests. Nose will be required to
284 will use Nose for discovering and running the tests. Nose will be required to
285 run the IPython test suite, but will not be required to simply use IPython.
285 run the IPython test suite, but will not be required to simply use IPython.
286
286
287 Tests of Twisted using code need to follow two additional guidelines:
287 Tests of Twisted using code need to follow two additional guidelines:
288
288
289 1. Twisted using tests should be written by subclassing the :class:`TestCase`
289 1. Twisted using tests should be written by subclassing the :class:`TestCase`
290 class that comes with :mod:`twisted.trial.unittest`.
290 class that comes with :mod:`twisted.trial.unittest`.
291
291
292 2. All :class:`Deferred` instances that are created in the test must be
292 2. All :class:`Deferred` instances that are created in the test must be
293 properly chained and the final one *must* be the return value of the test
293 properly chained and the final one *must* be the return value of the test
294 method.
294 method.
295
295
296 When these two things are done, Nose will be able to run the tests and the
296 When these two things are done, Nose will be able to run the tests and the
297 twisted reactor will be handled correctly.
297 twisted reactor will be handled correctly.
298
298
299 Each subpackage in IPython should have its own :file:`tests` directory that
299 Each subpackage in IPython should have its own :file:`tests` directory that
300 contains all of the tests for that subpackage. This allows each subpackage to
300 contains all of the tests for that subpackage. This allows each subpackage to
301 be self-contained. A good convention to follow is to have a file named
301 be self-contained. A good convention to follow is to have a file named
302 :file:`test_foo.py` for each module :file:`foo.py` in the package. This makes
302 :file:`test_foo.py` for each module :file:`foo.py` in the package. This makes
303 it easy to organize the tests, though like most conventions, it's OK to break
303 it easy to organize the tests, though like most conventions, it's OK to break
304 it if logic and common sense dictate otherwise.
304 it if logic and common sense dictate otherwise.
305
305
306 If a subpackage has any dependencies beyond the Python standard library, the
306 If a subpackage has any dependencies beyond the Python standard library, the
307 tests for that subpackage should be skipped if the dependencies are not
307 tests for that subpackage should be skipped if the dependencies are not
308 found. This is very important so users don't get tests failing simply because
308 found. This is very important so users don't get tests failing simply because
309 they don't have dependencies. We ship a set of decorators in the
309 they don't have dependencies. We ship a set of decorators in the
310 :mod:`IPython.testing` package to tag tests that may be platform-specific or
310 :mod:`IPython.testing` package to tag tests that may be platform-specific or
311 otherwise may have restrictions; if the existing ones don't fit your needs, add
311 otherwise may have restrictions; if the existing ones don't fit your needs, add
312 a new decorator in that location so other tests can reuse it.
312 a new decorator in that location so other tests can reuse it.
313
313
314 To run the IPython test suite, use the :command:`iptest` command that is
314 To run the IPython test suite, use the :command:`iptest` command that is
315 installed with IPython (if you are using IPython in-place, without installing
315 installed with IPython (if you are using IPython in-place, without installing
316 it, you can find this script in the :file:`scripts` directory)::
316 it, you can find this script in the :file:`scripts` directory)::
317
317
318 $ iptest
318 $ iptest
319
319
320 This command runs Nose with the proper options and extensions. By default,
320 This command runs Nose with the proper options and extensions. By default,
321 :command:`iptest` runs the entire IPython test suite (skipping tests that may
321 :command:`iptest` runs the entire IPython test suite (skipping tests that may
322 be platform-specific or which depend on tools you may not have). But you can
322 be platform-specific or which depend on tools you may not have). But you can
323 also use it to run only one specific test file, or a specific test function.
323 also use it to run only one specific test file, or a specific test function.
324 For example, this will run only the :file:`test_magic` file from the test
324 For example, this will run only the :file:`test_magic` file from the test
325 suite::
325 suite::
326
326
327 $ iptest IPython.tests.test_magic
327 $ iptest IPython.tests.test_magic
328 ----------------------------------------------------------------------
328 ----------------------------------------------------------------------
329 Ran 10 tests in 0.348s
329 Ran 10 tests in 0.348s
330
330
331 OK (SKIP=3)
331 OK (SKIP=3)
332 Deleting object: second_pass
332 Deleting object: second_pass
333
333
334 while the ``path:function`` syntax allows you to select a specific function in
334 while the ``path:function`` syntax allows you to select a specific function in
335 that file to run::
335 that file to run::
336
336
337 $ iptest IPython.tests.test_magic:test_obj_del
337 $ iptest IPython.tests.test_magic:test_obj_del
338 ----------------------------------------------------------------------
338 ----------------------------------------------------------------------
339 Ran 1 test in 0.204s
339 Ran 1 test in 0.204s
340
340
341 OK
341 OK
342
342
343 Since :command:`iptest` is based on nosetests, you can pass it any regular
343 Since :command:`iptest` is based on nosetests, you can pass it any regular
344 nosetests option. For example, you can use ``--pdb`` or ``--pdb-failures`` to
344 nosetests option. For example, you can use ``--pdb`` or ``--pdb-failures`` to
345 automatically activate the interactive Pdb debugger on errors or failures. See
345 automatically activate the interactive Pdb debugger on errors or failures. See
346 the nosetests documentation for further details.
346 the nosetests documentation for further details.
347
347
348 .. warning::
349
350 Note that right now we have a nasty interaction between ipdoctest and
351 twisted. Until we figure this out, please use the following instructions to
352 ensure that at least you run all the tests.
353
354 Right now, if you now run::
355
356 $ iptest [any options] [any submodules]
357
358 it will NOT load ipdoctest but won't cause any Twisted problems.
359
360 Once you're happy that you didn't break Twisted, run::
361
362 $ iptest --with-ipdoctest [any options] [any submodules]
363
364 This MAY give a Twisted AlreadyCalledError exception at the end, but it will
365 also correctly load up all of the ipython-specific tests and doctests.
366
367 The above can be made easier with a trivial shell alias::
368
369 $ alias iptest2='iptest --with-ipdoctest'
370
371 So that you can run::
372
373 $ iptest ...
374 # Twisted happy
375 # iptest2 ...
376 # ignore possible Twisted error, this checks all the rest.
377
378
348 A few tips for writing tests
379 A few tips for writing tests
349 ----------------------------
380 ----------------------------
350
381
351 You can write tests either as normal test files, using all the conventions that
382 You can write tests either as normal test files, using all the conventions that
352 Nose recognizes, or as doctests. Note that *all* IPython functions should have
383 Nose recognizes, or as doctests. Note that *all* IPython functions should have
353 at least one example that serves as a doctest, whenever technically feasible.
384 at least one example that serves as a doctest, whenever technically feasible.
354 However, example doctests should only be in the main docstring if they are *a
385 However, example doctests should only be in the main docstring if they are *a
355 good example*, i.e. if they convey useful information about the function. If
386 good example*, i.e. if they convey useful information about the function. If
356 you simply would like to write a test as a doctest, put it in a separate test
387 you simply would like to write a test as a doctest, put it in a separate test
357 file and write a no-op function whose only purpose is its docstring.
388 file and write a no-op function whose only purpose is its docstring.
358
389
359 Note, however, that in a file named :file:`test_X`, functions whose only test
390 Note, however, that in a file named :file:`test_X`, functions whose only test
360 is their docstring (as a doctest) and which have no test functionality of their
391 is their docstring (as a doctest) and which have no test functionality of their
361 own, should be called *doctest_foo* instead of *test_foo*, otherwise they get
392 own, should be called *doctest_foo* instead of *test_foo*, otherwise they get
362 double-counted (the empty function call is counted as a test, which just
393 double-counted (the empty function call is counted as a test, which just
363 inflates tests numbers artificially). This restriction does not apply to
394 inflates tests numbers artificially). This restriction does not apply to
364 functions in files with other names, due to how Nose discovers tests.
395 functions in files with other names, due to how Nose discovers tests.
365
396
366 You can use IPython examples in your docstrings. Those can make full use of
397 You can use IPython examples in your docstrings. Those can make full use of
367 IPython functionality (magics, variable substitution, etc), but be careful to
398 IPython functionality (magics, variable substitution, etc), but be careful to
368 keep them generic enough that they run identically on all Operating Systems.
399 keep them generic enough that they run identically on all Operating Systems.
369
400
370 The prompts in your doctests can be either of the plain Python ``>>>`` variety
401 The prompts in your doctests can be either of the plain Python ``>>>`` variety
371 or ``In [1]:`` IPython style. Since this is the IPython system, after all, we
402 or ``In [1]:`` IPython style. Since this is the IPython system, after all, we
372 encourage you to use IPython prompts throughout, unless you are illustrating a
403 encourage you to use IPython prompts throughout, unless you are illustrating a
373 specific aspect of the normal prompts (such as the ``%doctest_mode`` magic).
404 specific aspect of the normal prompts (such as the ``%doctest_mode`` magic).
374
405
375 If a test isn't safe to run inside the main nose process (e.g. because it loads
406 If a test isn't safe to run inside the main nose process (e.g. because it loads
376 a GUI toolkit), consider running it in a subprocess and capturing its output
407 a GUI toolkit), consider running it in a subprocess and capturing its output
377 for evaluation and test decision later. Here is an example of how to do it, by
408 for evaluation and test decision later. Here is an example of how to do it, by
378 relying on the builtin ``_ip`` object that contains the public IPython api as
409 relying on the builtin ``_ip`` object that contains the public IPython api as
379 defined in :mod:`IPython.ipapi`::
410 defined in :mod:`IPython.ipapi`::
380
411
381 def test_obj_del():
412 def test_obj_del():
382 """Test that object's __del__ methods are called on exit."""
413 """Test that object's __del__ methods are called on exit."""
383 test_dir = os.path.dirname(__file__)
414 test_dir = os.path.dirname(__file__)
384 del_file = os.path.join(test_dir,'obj_del.py')
415 del_file = os.path.join(test_dir,'obj_del.py')
385 out = _ip.IP.getoutput('ipython %s' % del_file)
416 out = _ip.IP.getoutput('ipython %s' % del_file)
386 nt.assert_equals(out,'object A deleted')
417 nt.assert_equals(out,'object A deleted')
387
418
388
419
389
420
390 If a doctest contains input whose output you don't want to verify identically
421 If a doctest contains input whose output you don't want to verify identically
391 via doctest (random output, an object id, etc), you can mark a docstring with
422 via doctest (random output, an object id, etc), you can mark a docstring with
392 ``#random``. All of these test will have their code executed but no output
423 ``#random``. All of these test will have their code executed but no output
393 checking will be done::
424 checking will be done::
394
425
395 >>> 1+3
426 >>> 1+3
396 junk goes here... # random
427 junk goes here... # random
397
428
398 >>> 1+2
429 >>> 1+2
399 again, anything goes #random
430 again, anything goes #random
400 if multiline, the random mark is only needed once.
431 if multiline, the random mark is only needed once.
401
432
402 >>> 1+2
433 >>> 1+2
403 You can also put the random marker at the end:
434 You can also put the random marker at the end:
404 # random
435 # random
405
436
406 >>> 1+2
437 >>> 1+2
407 # random
438 # random
408 .. or at the beginning.
439 .. or at the beginning.
409
440
410 In a case where you want an *entire* docstring to be executed but not verified
441 In a case where you want an *entire* docstring to be executed but not verified
411 (this only serves to check that the code runs without crashing, so it should be
442 (this only serves to check that the code runs without crashing, so it should be
412 used very sparingly), you can put ``# all-random`` in the docstring.
443 used very sparingly), you can put ``# all-random`` in the docstring.
413
444
414 .. _devel_config:
445 .. _devel_config:
415
446
416 Release checklist
447 Release checklist
417 =================
448 =================
418
449
419 Most of the release process is automated by the :file:`release` script in the
450 Most of the release process is automated by the :file:`release` script in the
420 :file:`tools` directory. This is just a handy reminder for the release manager.
451 :file:`tools` directory. This is just a handy reminder for the release manager.
421
452
422 #. Run the release script, which makes the tar.gz, eggs and Win32 .exe
453 #. Run the release script, which makes the tar.gz, eggs and Win32 .exe
423 installer. It posts them to the site and registers the release with PyPI.
454 installer. It posts them to the site and registers the release with PyPI.
424
455
425 #. Updating the website with announcements and links to the updated
456 #. Updating the website with announcements and links to the updated
426 changes.txt in html form. Remember to put a short note both on the news
457 changes.txt in html form. Remember to put a short note both on the news
427 page of the site and on Launcphad.
458 page of the site and on Launcphad.
428
459
429 #. Drafting a short release announcement with i) highlights and ii) a link to
460 #. Drafting a short release announcement with i) highlights and ii) a link to
430 the html changes.txt.
461 the html changes.txt.
431
462
432 #. Make sure that the released version of the docs is live on the site.
463 #. Make sure that the released version of the docs is live on the site.
433
464
434 #. Celebrate!
465 #. Celebrate!
435
466
436 Porting to 3.0
467 Porting to 3.0
437 ==============
468 ==============
438
469
439 There are no definite plans for porting of IPython to python 3. The major
470 There are no definite plans for porting of IPython to python 3. The major
440 issue is the dependency on twisted framework for the networking/threading
471 issue is the dependency on twisted framework for the networking/threading
441 stuff. It is possible that it the traditional IPython interactive console
472 stuff. It is possible that it the traditional IPython interactive console
442 could be ported more easily since it has no such dependency. Here are a few
473 could be ported more easily since it has no such dependency. Here are a few
443 things that will need to be considered when doing such a port especially
474 things that will need to be considered when doing such a port especially
444 if we want to have a codebase that works directly on both 2.x and 3.x.
475 if we want to have a codebase that works directly on both 2.x and 3.x.
445
476
446 1. The syntax for exceptions changed (PEP 3110). The old
477 1. The syntax for exceptions changed (PEP 3110). The old
447 `except exc, var` changed to `except exc as var`. At last
478 `except exc, var` changed to `except exc as var`. At last
448 count there was 78 occurences of this usage in the codebase
479 count there was 78 occurences of this usage in the codebase
449
480
450 .. [Bazaar] Bazaar. http://bazaar-vcs.org/
481 .. [Bazaar] Bazaar. http://bazaar-vcs.org/
451 .. [Launchpad] Launchpad. http://www.launchpad.net/ipython
482 .. [Launchpad] Launchpad. http://www.launchpad.net/ipython
452 .. [reStructuredText] reStructuredText. http://docutils.sourceforge.net/rst.html
483 .. [reStructuredText] reStructuredText. http://docutils.sourceforge.net/rst.html
453 .. [Sphinx] Sphinx. http://sphinx.pocoo.org/
484 .. [Sphinx] Sphinx. http://sphinx.pocoo.org/
454 .. [Nose] Nose: a discovery based unittest extension. http://code.google.com/p/python-nose/
485 .. [Nose] Nose: a discovery based unittest extension. http://code.google.com/p/python-nose/
@@ -1,157 +1,157 b''
1 .. _parallelmpi:
1 .. _parallelmpi:
2
2
3 =======================
3 =======================
4 Using MPI with IPython
4 Using MPI with IPython
5 =======================
5 =======================
6
6
7 Often, a parallel algorithm will require moving data between the engines. One way of accomplishing this is by doing a pull and then a push using the multiengine client. However, this will be slow as all the data has to go through the controller to the client and then back through the controller, to its final destination.
7 Often, a parallel algorithm will require moving data between the engines. One way of accomplishing this is by doing a pull and then a push using the multiengine client. However, this will be slow as all the data has to go through the controller to the client and then back through the controller, to its final destination.
8
8
9 A much better way of moving data between engines is to use a message passing library, such as the Message Passing Interface (MPI) [MPI]_. IPython's parallel computing architecture has been designed from the ground up to integrate with MPI. This document describes how to use MPI with IPython.
9 A much better way of moving data between engines is to use a message passing library, such as the Message Passing Interface (MPI) [MPI]_. IPython's parallel computing architecture has been designed from the ground up to integrate with MPI. This document describes how to use MPI with IPython.
10
10
11 Additional installation requirements
11 Additional installation requirements
12 ====================================
12 ====================================
13
13
14 If you want to use MPI with IPython, you will need to install:
14 If you want to use MPI with IPython, you will need to install:
15
15
16 * A standard MPI implementation such as OpenMPI [OpenMPI]_ or MPICH.
16 * A standard MPI implementation such as OpenMPI [OpenMPI]_ or MPICH.
17 * The mpi4py [mpi4py]_ package.
17 * The mpi4py [mpi4py]_ package.
18
18
19 .. note::
19 .. note::
20
20
21 The mpi4py package is not a strict requirement. However, you need to
21 The mpi4py package is not a strict requirement. However, you need to
22 have *some* way of calling MPI from Python. You also need some way of
22 have *some* way of calling MPI from Python. You also need some way of
23 making sure that :func:`MPI_Init` is called when the IPython engines start
23 making sure that :func:`MPI_Init` is called when the IPython engines start
24 up. There are a number of ways of doing this and a good number of
24 up. There are a number of ways of doing this and a good number of
25 associated subtleties. We highly recommend just using mpi4py as it
25 associated subtleties. We highly recommend just using mpi4py as it
26 takes care of most of these problems. If you want to do something
26 takes care of most of these problems. If you want to do something
27 different, let us know and we can help you get started.
27 different, let us know and we can help you get started.
28
28
29 Starting the engines with MPI enabled
29 Starting the engines with MPI enabled
30 =====================================
30 =====================================
31
31
32 To use code that calls MPI, there are typically two things that MPI requires.
32 To use code that calls MPI, there are typically two things that MPI requires.
33
33
34 1. The process that wants to call MPI must be started using
34 1. The process that wants to call MPI must be started using
35 :command:`mpirun` or a batch system (like PBS) that has MPI support.
35 :command:`mpiexec` or a batch system (like PBS) that has MPI support.
36 2. Once the process starts, it must call :func:`MPI_Init`.
36 2. Once the process starts, it must call :func:`MPI_Init`.
37
37
38 There are a couple of ways that you can start the IPython engines and get these things to happen.
38 There are a couple of ways that you can start the IPython engines and get these things to happen.
39
39
40 Automatic starting using :command:`mpirun` and :command:`ipcluster`
40 Automatic starting using :command:`mpiexec` and :command:`ipcluster`
41 -------------------------------------------------------------------
41 -------------------------------------------------------------------
42
42
43 The easiest approach is to use the `mpirun` mode of :command:`ipcluster`, which will first start a controller and then a set of engines using :command:`mpirun`::
43 The easiest approach is to use the `mpiexec` mode of :command:`ipcluster`, which will first start a controller and then a set of engines using :command:`mpiexec`::
44
44
45 $ ipcluster mpirun -n 4
45 $ ipcluster mpiexec -n 4
46
46
47 This approach is best as interrupting :command:`ipcluster` will automatically
47 This approach is best as interrupting :command:`ipcluster` will automatically
48 stop and clean up the controller and engines.
48 stop and clean up the controller and engines.
49
49
50 Manual starting using :command:`mpirun`
50 Manual starting using :command:`mpiexec`
51 ---------------------------------------
51 ---------------------------------------
52
52
53 If you want to start the IPython engines using the :command:`mpirun`, just do::
53 If you want to start the IPython engines using the :command:`mpiexec`, just do::
54
54
55 $ mpirun -n 4 ipengine --mpi=mpi4py
55 $ mpiexec -n 4 ipengine --mpi=mpi4py
56
56
57 This requires that you already have a controller running and that the FURL
57 This requires that you already have a controller running and that the FURL
58 files for the engines are in place. We also have built in support for
58 files for the engines are in place. We also have built in support for
59 PyTrilinos [PyTrilinos]_, which can be used (assuming is installed) by
59 PyTrilinos [PyTrilinos]_, which can be used (assuming is installed) by
60 starting the engines with::
60 starting the engines with::
61
61
62 mpirun -n 4 ipengine --mpi=pytrilinos
62 mpiexec -n 4 ipengine --mpi=pytrilinos
63
63
64 Automatic starting using PBS and :command:`ipcluster`
64 Automatic starting using PBS and :command:`ipcluster`
65 -----------------------------------------------------
65 -----------------------------------------------------
66
66
67 The :command:`ipcluster` command also has built-in integration with PBS. For more information on this approach, see our documentation on :ref:`ipcluster <parallel_process>`.
67 The :command:`ipcluster` command also has built-in integration with PBS. For more information on this approach, see our documentation on :ref:`ipcluster <parallel_process>`.
68
68
69 Actually using MPI
69 Actually using MPI
70 ==================
70 ==================
71
71
72 Once the engines are running with MPI enabled, you are ready to go. You can now call any code that uses MPI in the IPython engines. And, all of this can be done interactively. Here we show a simple example that uses mpi4py [mpi4py]_.
72 Once the engines are running with MPI enabled, you are ready to go. You can now call any code that uses MPI in the IPython engines. And, all of this can be done interactively. Here we show a simple example that uses mpi4py [mpi4py]_.
73
73
74 First, lets define a simply function that uses MPI to calculate the sum of a distributed array. Save the following text in a file called :file:`psum.py`:
74 First, lets define a simply function that uses MPI to calculate the sum of a distributed array. Save the following text in a file called :file:`psum.py`:
75
75
76 .. sourcecode:: python
76 .. sourcecode:: python
77
77
78 from mpi4py import MPI
78 from mpi4py import MPI
79 import numpy as np
79 import numpy as np
80
80
81 def psum(a):
81 def psum(a):
82 s = np.sum(a)
82 s = np.sum(a)
83 return MPI.COMM_WORLD.Allreduce(s,MPI.SUM)
83 return MPI.COMM_WORLD.Allreduce(s,MPI.SUM)
84
84
85 Now, start an IPython cluster in the same directory as :file:`psum.py`::
85 Now, start an IPython cluster in the same directory as :file:`psum.py`::
86
86
87 $ ipcluster mpirun -n 4
87 $ ipcluster mpiexec -n 4
88
88
89 Finally, connect to the cluster and use this function interactively. In this case, we create a random array on each engine and sum up all the random arrays using our :func:`psum` function:
89 Finally, connect to the cluster and use this function interactively. In this case, we create a random array on each engine and sum up all the random arrays using our :func:`psum` function:
90
90
91 .. sourcecode:: ipython
91 .. sourcecode:: ipython
92
92
93 In [1]: from IPython.kernel import client
93 In [1]: from IPython.kernel import client
94
94
95 In [2]: mec = client.MultiEngineClient()
95 In [2]: mec = client.MultiEngineClient()
96
96
97 In [3]: mec.activate()
97 In [3]: mec.activate()
98
98
99 In [4]: px import numpy as np
99 In [4]: px import numpy as np
100 Parallel execution on engines: all
100 Parallel execution on engines: all
101 Out[4]:
101 Out[4]:
102 <Results List>
102 <Results List>
103 [0] In [13]: import numpy as np
103 [0] In [13]: import numpy as np
104 [1] In [13]: import numpy as np
104 [1] In [13]: import numpy as np
105 [2] In [13]: import numpy as np
105 [2] In [13]: import numpy as np
106 [3] In [13]: import numpy as np
106 [3] In [13]: import numpy as np
107
107
108 In [6]: px a = np.random.rand(100)
108 In [6]: px a = np.random.rand(100)
109 Parallel execution on engines: all
109 Parallel execution on engines: all
110 Out[6]:
110 Out[6]:
111 <Results List>
111 <Results List>
112 [0] In [15]: a = np.random.rand(100)
112 [0] In [15]: a = np.random.rand(100)
113 [1] In [15]: a = np.random.rand(100)
113 [1] In [15]: a = np.random.rand(100)
114 [2] In [15]: a = np.random.rand(100)
114 [2] In [15]: a = np.random.rand(100)
115 [3] In [15]: a = np.random.rand(100)
115 [3] In [15]: a = np.random.rand(100)
116
116
117 In [7]: px from psum import psum
117 In [7]: px from psum import psum
118 Parallel execution on engines: all
118 Parallel execution on engines: all
119 Out[7]:
119 Out[7]:
120 <Results List>
120 <Results List>
121 [0] In [16]: from psum import psum
121 [0] In [16]: from psum import psum
122 [1] In [16]: from psum import psum
122 [1] In [16]: from psum import psum
123 [2] In [16]: from psum import psum
123 [2] In [16]: from psum import psum
124 [3] In [16]: from psum import psum
124 [3] In [16]: from psum import psum
125
125
126 In [8]: px s = psum(a)
126 In [8]: px s = psum(a)
127 Parallel execution on engines: all
127 Parallel execution on engines: all
128 Out[8]:
128 Out[8]:
129 <Results List>
129 <Results List>
130 [0] In [17]: s = psum(a)
130 [0] In [17]: s = psum(a)
131 [1] In [17]: s = psum(a)
131 [1] In [17]: s = psum(a)
132 [2] In [17]: s = psum(a)
132 [2] In [17]: s = psum(a)
133 [3] In [17]: s = psum(a)
133 [3] In [17]: s = psum(a)
134
134
135 In [9]: px print s
135 In [9]: px print s
136 Parallel execution on engines: all
136 Parallel execution on engines: all
137 Out[9]:
137 Out[9]:
138 <Results List>
138 <Results List>
139 [0] In [18]: print s
139 [0] In [18]: print s
140 [0] Out[18]: 187.451545803
140 [0] Out[18]: 187.451545803
141
141
142 [1] In [18]: print s
142 [1] In [18]: print s
143 [1] Out[18]: 187.451545803
143 [1] Out[18]: 187.451545803
144
144
145 [2] In [18]: print s
145 [2] In [18]: print s
146 [2] Out[18]: 187.451545803
146 [2] Out[18]: 187.451545803
147
147
148 [3] In [18]: print s
148 [3] In [18]: print s
149 [3] Out[18]: 187.451545803
149 [3] Out[18]: 187.451545803
150
150
151 Any Python code that makes calls to MPI can be used in this manner, including
151 Any Python code that makes calls to MPI can be used in this manner, including
152 compiled C, C++ and Fortran libraries that have been exposed to Python.
152 compiled C, C++ and Fortran libraries that have been exposed to Python.
153
153
154 .. [MPI] Message Passing Interface. http://www-unix.mcs.anl.gov/mpi/
154 .. [MPI] Message Passing Interface. http://www-unix.mcs.anl.gov/mpi/
155 .. [mpi4py] MPI for Python. mpi4py: http://mpi4py.scipy.org/
155 .. [mpi4py] MPI for Python. mpi4py: http://mpi4py.scipy.org/
156 .. [OpenMPI] Open MPI. http://www.open-mpi.org/
156 .. [OpenMPI] Open MPI. http://www.open-mpi.org/
157 .. [PyTrilinos] PyTrilinos. http://trilinos.sandia.gov/packages/pytrilinos/ No newline at end of file
157 .. [PyTrilinos] PyTrilinos. http://trilinos.sandia.gov/packages/pytrilinos/
@@ -1,324 +1,336 b''
1 .. _parallel_process:
1 .. _parallel_process:
2
2
3 ===========================================
3 ===========================================
4 Starting the IPython controller and engines
4 Starting the IPython controller and engines
5 ===========================================
5 ===========================================
6
6
7 To use IPython for parallel computing, you need to start one instance of
7 To use IPython for parallel computing, you need to start one instance of
8 the controller and one or more instances of the engine. The controller
8 the controller and one or more instances of the engine. The controller
9 and each engine can run on different machines or on the same machine.
9 and each engine can run on different machines or on the same machine.
10 Because of this, there are many different possibilities.
10 Because of this, there are many different possibilities.
11
11
12 Broadly speaking, there are two ways of going about starting a controller and engines:
12 Broadly speaking, there are two ways of going about starting a controller and engines:
13
13
14 * In an automated manner using the :command:`ipcluster` command.
14 * In an automated manner using the :command:`ipcluster` command.
15 * In a more manual way using the :command:`ipcontroller` and
15 * In a more manual way using the :command:`ipcontroller` and
16 :command:`ipengine` commands.
16 :command:`ipengine` commands.
17
17
18 This document describes both of these methods. We recommend that new users start with the :command:`ipcluster` command as it simplifies many common usage cases.
18 This document describes both of these methods. We recommend that new users start with the :command:`ipcluster` command as it simplifies many common usage cases.
19
19
20 General considerations
20 General considerations
21 ======================
21 ======================
22
22
23 Before delving into the details about how you can start a controller and engines using the various methods, we outline some of the general issues that come up when starting the controller and engines. These things come up no matter which method you use to start your IPython cluster.
23 Before delving into the details about how you can start a controller and engines using the various methods, we outline some of the general issues that come up when starting the controller and engines. These things come up no matter which method you use to start your IPython cluster.
24
24
25 Let's say that you want to start the controller on ``host0`` and engines on hosts ``host1``-``hostn``. The following steps are then required:
25 Let's say that you want to start the controller on ``host0`` and engines on hosts ``host1``-``hostn``. The following steps are then required:
26
26
27 1. Start the controller on ``host0`` by running :command:`ipcontroller` on
27 1. Start the controller on ``host0`` by running :command:`ipcontroller` on
28 ``host0``.
28 ``host0``.
29 2. Move the FURL file (:file:`ipcontroller-engine.furl`) created by the
29 2. Move the FURL file (:file:`ipcontroller-engine.furl`) created by the
30 controller from ``host0`` to hosts ``host1``-``hostn``.
30 controller from ``host0`` to hosts ``host1``-``hostn``.
31 3. Start the engines on hosts ``host1``-``hostn`` by running
31 3. Start the engines on hosts ``host1``-``hostn`` by running
32 :command:`ipengine`. This command has to be told where the FURL file
32 :command:`ipengine`. This command has to be told where the FURL file
33 (:file:`ipcontroller-engine.furl`) is located.
33 (:file:`ipcontroller-engine.furl`) is located.
34
34
35 At this point, the controller and engines will be connected. By default, the
35 At this point, the controller and engines will be connected. By default, the
36 FURL files created by the controller are put into the
36 FURL files created by the controller are put into the
37 :file:`~/.ipython/security` directory. If the engines share a filesystem with
37 :file:`~/.ipython/security` directory. If the engines share a filesystem with
38 the controller, step 2 can be skipped as the engines will automatically look
38 the controller, step 2 can be skipped as the engines will automatically look
39 at that location.
39 at that location.
40
40
41 The final step required required to actually use the running controller from a
41 The final step required required to actually use the running controller from a
42 client is to move the FURL files :file:`ipcontroller-mec.furl` and
42 client is to move the FURL files :file:`ipcontroller-mec.furl` and
43 :file:`ipcontroller-tc.furl` from ``host0`` to the host where the clients will
43 :file:`ipcontroller-tc.furl` from ``host0`` to the host where the clients will
44 be run. If these file are put into the :file:`~/.ipython/security` directory of the client's host, they will be found automatically. Otherwise, the full path to them has to be passed to the client's constructor.
44 be run. If these file are put into the :file:`~/.ipython/security` directory of the client's host, they will be found automatically. Otherwise, the full path to them has to be passed to the client's constructor.
45
45
46 Using :command:`ipcluster`
46 Using :command:`ipcluster`
47 ==========================
47 ==========================
48
48
49 The :command:`ipcluster` command provides a simple way of starting a controller and engines in the following situations:
49 The :command:`ipcluster` command provides a simple way of starting a controller and engines in the following situations:
50
50
51 1. When the controller and engines are all run on localhost. This is useful
51 1. When the controller and engines are all run on localhost. This is useful
52 for testing or running on a multicore computer.
52 for testing or running on a multicore computer.
53 2. When engines are started using the :command:`mpirun` command that comes
53 2. When engines are started using the :command:`mpirun` command that comes
54 with most MPI [MPI]_ implementations
54 with most MPI [MPI]_ implementations
55 3. When engines are started using the PBS [PBS]_ batch system.
55 3. When engines are started using the PBS [PBS]_ batch system.
56 4. When the controller is started on localhost and the engines are started on
56 4. When the controller is started on localhost and the engines are started on
57 remote nodes using :command:`ssh`.
57 remote nodes using :command:`ssh`.
58
58
59 .. note::
59 .. note::
60
60
61 It is also possible for advanced users to add support to
61 It is also possible for advanced users to add support to
62 :command:`ipcluster` for starting controllers and engines using other
62 :command:`ipcluster` for starting controllers and engines using other
63 methods (like Sun's Grid Engine for example).
63 methods (like Sun's Grid Engine for example).
64
64
65 .. note::
65 .. note::
66
66
67 Currently :command:`ipcluster` requires that the
67 Currently :command:`ipcluster` requires that the
68 :file:`~/.ipython/security` directory live on a shared filesystem that is
68 :file:`~/.ipython/security` directory live on a shared filesystem that is
69 seen by both the controller and engines. If you don't have a shared file
69 seen by both the controller and engines. If you don't have a shared file
70 system you will need to use :command:`ipcontroller` and
70 system you will need to use :command:`ipcontroller` and
71 :command:`ipengine` directly. This constraint can be relaxed if you are
71 :command:`ipengine` directly. This constraint can be relaxed if you are
72 using the :command:`ssh` method to start the cluster.
72 using the :command:`ssh` method to start the cluster.
73
73
74 Underneath the hood, :command:`ipcluster` just uses :command:`ipcontroller`
74 Underneath the hood, :command:`ipcluster` just uses :command:`ipcontroller`
75 and :command:`ipengine` to perform the steps described above.
75 and :command:`ipengine` to perform the steps described above.
76
76
77 Using :command:`ipcluster` in local mode
77 Using :command:`ipcluster` in local mode
78 ----------------------------------------
78 ----------------------------------------
79
79
80 To start one controller and 4 engines on localhost, just do::
80 To start one controller and 4 engines on localhost, just do::
81
81
82 $ ipcluster local -n 4
82 $ ipcluster local -n 4
83
83
84 To see other command line options for the local mode, do::
84 To see other command line options for the local mode, do::
85
85
86 $ ipcluster local -h
86 $ ipcluster local -h
87
87
88 Using :command:`ipcluster` in mpirun mode
88 Using :command:`ipcluster` in mpiexec/mpirun mode
89 -----------------------------------------
89 -------------------------------------------------
90
90
91 The mpirun mode is useful if you:
91 The mpiexec/mpirun mode is useful if you:
92
92
93 1. Have MPI installed.
93 1. Have MPI installed.
94 2. Your systems are configured to use the :command:`mpirun` command to start
94 2. Your systems are configured to use the :command:`mpiexec` or
95 processes.
95 :command:`mpirun` commands to start MPI processes.
96
97 .. note::
98
99 The preferred command to use is :command:`mpiexec`. However, we also
100 support :command:`mpirun` for backwards compatibility. The underlying
101 logic used is exactly the same, the only difference being the name of the
102 command line program that is called.
96
103
97 If these are satisfied, you can start an IPython cluster using::
104 If these are satisfied, you can start an IPython cluster using::
98
105
99 $ ipcluster mpirun -n 4
106 $ ipcluster mpiexec -n 4
100
107
101 This does the following:
108 This does the following:
102
109
103 1. Starts the IPython controller on current host.
110 1. Starts the IPython controller on current host.
104 2. Uses :command:`mpirun` to start 4 engines.
111 2. Uses :command:`mpiexec` to start 4 engines.
105
112
106 On newer MPI implementations (such as OpenMPI), this will work even if you don't make any calls to MPI or call :func:`MPI_Init`. However, older MPI implementations actually require each process to call :func:`MPI_Init` upon starting. The easiest way of having this done is to install the mpi4py [mpi4py]_ package and then call ipcluster with the ``--mpi`` option::
113 On newer MPI implementations (such as OpenMPI), this will work even if you don't make any calls to MPI or call :func:`MPI_Init`. However, older MPI implementations actually require each process to call :func:`MPI_Init` upon starting. The easiest way of having this done is to install the mpi4py [mpi4py]_ package and then call ipcluster with the ``--mpi`` option::
107
114
108 $ ipcluster mpirun -n 4 --mpi=mpi4py
115 $ ipcluster mpiexec -n 4 --mpi=mpi4py
109
116
110 Unfortunately, even this won't work for some MPI implementations. If you are having problems with this, you will likely have to use a custom Python executable that itself calls :func:`MPI_Init` at the appropriate time. Fortunately, mpi4py comes with such a custom Python executable that is easy to install and use. However, this custom Python executable approach will not work with :command:`ipcluster` currently.
117 Unfortunately, even this won't work for some MPI implementations. If you are having problems with this, you will likely have to use a custom Python executable that itself calls :func:`MPI_Init` at the appropriate time. Fortunately, mpi4py comes with such a custom Python executable that is easy to install and use. However, this custom Python executable approach will not work with :command:`ipcluster` currently.
111
118
112 Additional command line options for this mode can be found by doing::
119 Additional command line options for this mode can be found by doing::
113
120
114 $ ipcluster mpirun -h
121 $ ipcluster mpiexec -h
115
122
116 More details on using MPI with IPython can be found :ref:`here <parallelmpi>`.
123 More details on using MPI with IPython can be found :ref:`here <parallelmpi>`.
117
124
118
125
119 Using :command:`ipcluster` in PBS mode
126 Using :command:`ipcluster` in PBS mode
120 --------------------------------------
127 --------------------------------------
121
128
122 The PBS mode uses the Portable Batch System [PBS]_ to start the engines. To use this mode, you first need to create a PBS script template that will be used to start the engines. Here is a sample PBS script template:
129 The PBS mode uses the Portable Batch System [PBS]_ to start the engines. To use this mode, you first need to create a PBS script template that will be used to start the engines. Here is a sample PBS script template:
123
130
124 .. sourcecode:: bash
131 .. sourcecode:: bash
125
132
126 #PBS -N ipython
133 #PBS -N ipython
127 #PBS -j oe
134 #PBS -j oe
128 #PBS -l walltime=00:10:00
135 #PBS -l walltime=00:10:00
129 #PBS -l nodes=${n/4}:ppn=4
136 #PBS -l nodes=${n/4}:ppn=4
130 #PBS -q parallel
137 #PBS -q parallel
131
138
132 cd $$PBS_O_WORKDIR
139 cd $$PBS_O_WORKDIR
133 export PATH=$$HOME/usr/local/bin
140 export PATH=$$HOME/usr/local/bin
134 export PYTHONPATH=$$HOME/usr/local/lib/python2.4/site-packages
141 export PYTHONPATH=$$HOME/usr/local/lib/python2.4/site-packages
135 /usr/local/bin/mpiexec -n ${n} ipengine --logfile=$$PBS_O_WORKDIR/ipengine
142 /usr/local/bin/mpiexec -n ${n} ipengine --logfile=$$PBS_O_WORKDIR/ipengine
136
143
137 There are a few important points about this template:
144 There are a few important points about this template:
138
145
139 1. This template will be rendered at runtime using IPython's :mod:`Itpl`
146 1. This template will be rendered at runtime using IPython's :mod:`Itpl`
140 template engine.
147 template engine.
141
148
142 2. Instead of putting in the actual number of engines, use the notation
149 2. Instead of putting in the actual number of engines, use the notation
143 ``${n}`` to indicate the number of engines to be started. You can also uses
150 ``${n}`` to indicate the number of engines to be started. You can also uses
144 expressions like ``${n/4}`` in the template to indicate the number of
151 expressions like ``${n/4}`` in the template to indicate the number of
145 nodes.
152 nodes.
146
153
147 3. Because ``$`` is a special character used by the template engine, you must
154 3. Because ``$`` is a special character used by the template engine, you must
148 escape any ``$`` by using ``$$``. This is important when referring to
155 escape any ``$`` by using ``$$``. This is important when referring to
149 environment variables in the template.
156 environment variables in the template.
150
157
151 4. Any options to :command:`ipengine` should be given in the batch script
158 4. Any options to :command:`ipengine` should be given in the batch script
152 template.
159 template.
153
160
154 5. Depending on the configuration of you system, you may have to set
161 5. Depending on the configuration of you system, you may have to set
155 environment variables in the script template.
162 environment variables in the script template.
156
163
157 Once you have created such a script, save it with a name like :file:`pbs.template`. Now you are ready to start your job::
164 Once you have created such a script, save it with a name like :file:`pbs.template`. Now you are ready to start your job::
158
165
159 $ ipcluster pbs -n 128 --pbs-script=pbs.template
166 $ ipcluster pbs -n 128 --pbs-script=pbs.template
160
167
161 Additional command line options for this mode can be found by doing::
168 Additional command line options for this mode can be found by doing::
162
169
163 $ ipcluster pbs -h
170 $ ipcluster pbs -h
164
171
165 Using :command:`ipcluster` in SSH mode
172 Using :command:`ipcluster` in SSH mode
166 --------------------------------------
173 --------------------------------------
167
174
168 The SSH mode uses :command:`ssh` to execute :command:`ipengine` on remote
175 The SSH mode uses :command:`ssh` to execute :command:`ipengine` on remote
169 nodes and the :command:`ipcontroller` on localhost.
176 nodes and the :command:`ipcontroller` on localhost.
170
177
171 When using using this mode it highly recommended that you have set up SSH keys and are using ssh-agent [SSH]_ for password-less logins.
178 When using using this mode it highly recommended that you have set up SSH keys and are using ssh-agent [SSH]_ for password-less logins.
172
179
173 To use this mode you need a python file describing the cluster, here is an example of such a "clusterfile":
180 To use this mode you need a python file describing the cluster, here is an example of such a "clusterfile":
174
181
175 .. sourcecode:: python
182 .. sourcecode:: python
176
183
177 send_furl = True
184 send_furl = True
178 engines = { 'host1.example.com' : 2,
185 engines = { 'host1.example.com' : 2,
179 'host2.example.com' : 5,
186 'host2.example.com' : 5,
180 'host3.example.com' : 1,
187 'host3.example.com' : 1,
181 'host4.example.com' : 8 }
188 'host4.example.com' : 8 }
182
189
183 Since this is a regular python file usual python syntax applies. Things to note:
190 Since this is a regular python file usual python syntax applies. Things to note:
184
191
185 * The `engines` dict, where the keys is the host we want to run engines on and
192 * The `engines` dict, where the keys is the host we want to run engines on and
186 the value is the number of engines to run on that host.
193 the value is the number of engines to run on that host.
187 * send_furl can either be `True` or `False`, if `True` it will copy over the
194 * send_furl can either be `True` or `False`, if `True` it will copy over the
188 furl needed for :command:`ipengine` to each host.
195 furl needed for :command:`ipengine` to each host.
189
196
190 The ``--clusterfile`` command line option lets you specify the file to use for
197 The ``--clusterfile`` command line option lets you specify the file to use for
191 the cluster definition. Once you have your cluster file and you can
198 the cluster definition. Once you have your cluster file and you can
192 :command:`ssh` into the remote hosts with out an password you are ready to
199 :command:`ssh` into the remote hosts with out an password you are ready to
193 start your cluster like so:
200 start your cluster like so:
194
201
195 .. sourcecode:: bash
202 .. sourcecode:: bash
196
203
197 $ ipcluster ssh --clusterfile /path/to/my/clusterfile.py
204 $ ipcluster ssh --clusterfile /path/to/my/clusterfile.py
198
205
199
206
200 Two helper shell scripts are used to start and stop :command:`ipengine` on remote hosts:
207 Two helper shell scripts are used to start and stop :command:`ipengine` on remote hosts:
201
208
202 * sshx.sh
209 * sshx.sh
203 * engine_killer.sh
210 * engine_killer.sh
204
211
205 Defaults for both of these are contained in the source code for :command:`ipcluster`. The default scripts are written to a local file in a tmep directory and then copied to a temp directory on the remote host and executed from there. On most Unix, Linux and OS X systems this is /tmp.
212 Defaults for both of these are contained in the source code for :command:`ipcluster`. The default scripts are written to a local file in a tmep directory and then copied to a temp directory on the remote host and executed from there. On most Unix, Linux and OS X systems this is /tmp.
206
213
207 The default sshx.sh is the following:
214 The default sshx.sh is the following:
208
215
209 .. sourcecode:: bash
216 .. sourcecode:: bash
210
217
211 #!/bin/sh
218 #!/bin/sh
212 "$@" &> /dev/null &
219 "$@" &> /dev/null &
213 echo $!
220 echo $!
214
221
215 If you want to use a custom sshx.sh script you need to use the ``--sshx``
222 If you want to use a custom sshx.sh script you need to use the ``--sshx``
216 option and specify the file to use. Using a custom sshx.sh file could be
223 option and specify the file to use. Using a custom sshx.sh file could be
217 helpful when you need to setup the environment on the remote host before
224 helpful when you need to setup the environment on the remote host before
218 executing :command:`ipengine`.
225 executing :command:`ipengine`.
219
226
220 For a detailed options list:
227 For a detailed options list:
221
228
222 .. sourcecode:: bash
229 .. sourcecode:: bash
223
230
224 $ ipcluster ssh -h
231 $ ipcluster ssh -h
225
232
226 Current limitations of the SSH mode of :command:`ipcluster` are:
233 Current limitations of the SSH mode of :command:`ipcluster` are:
227
234
228 * Untested on Windows. Would require a working :command:`ssh` on Windows.
235 * Untested on Windows. Would require a working :command:`ssh` on Windows.
229 Also, we are using shell scripts to setup and execute commands on remote
236 Also, we are using shell scripts to setup and execute commands on remote
230 hosts.
237 hosts.
231 * :command:`ipcontroller` is started on localhost, with no option to start it
238 * :command:`ipcontroller` is started on localhost, with no option to start it
232 on a remote node.
239 on a remote node.
233
240
234 Using the :command:`ipcontroller` and :command:`ipengine` commands
241 Using the :command:`ipcontroller` and :command:`ipengine` commands
235 ==================================================================
242 ==================================================================
236
243
237 It is also possible to use the :command:`ipcontroller` and :command:`ipengine` commands to start your controller and engines. This approach gives you full control over all aspects of the startup process.
244 It is also possible to use the :command:`ipcontroller` and :command:`ipengine` commands to start your controller and engines. This approach gives you full control over all aspects of the startup process.
238
245
239 Starting the controller and engine on your local machine
246 Starting the controller and engine on your local machine
240 --------------------------------------------------------
247 --------------------------------------------------------
241
248
242 To use :command:`ipcontroller` and :command:`ipengine` to start things on your
249 To use :command:`ipcontroller` and :command:`ipengine` to start things on your
243 local machine, do the following.
250 local machine, do the following.
244
251
245 First start the controller::
252 First start the controller::
246
253
247 $ ipcontroller
254 $ ipcontroller
248
255
249 Next, start however many instances of the engine you want using (repeatedly) the command::
256 Next, start however many instances of the engine you want using (repeatedly) the command::
250
257
251 $ ipengine
258 $ ipengine
252
259
253 The engines should start and automatically connect to the controller using the FURL files in :file:`~./ipython/security`. You are now ready to use the controller and engines from IPython.
260 The engines should start and automatically connect to the controller using the FURL files in :file:`~./ipython/security`. You are now ready to use the controller and engines from IPython.
254
261
255 .. warning::
262 .. warning::
256
263
257 The order of the above operations is very important. You *must*
264 The order of the above operations is very important. You *must*
258 start the controller before the engines, since the engines connect
265 start the controller before the engines, since the engines connect
259 to the controller as they get started.
266 to the controller as they get started.
260
267
261 .. note::
268 .. note::
262
269
263 On some platforms (OS X), to put the controller and engine into the
270 On some platforms (OS X), to put the controller and engine into the
264 background you may need to give these commands in the form ``(ipcontroller
271 background you may need to give these commands in the form ``(ipcontroller
265 &)`` and ``(ipengine &)`` (with the parentheses) for them to work
272 &)`` and ``(ipengine &)`` (with the parentheses) for them to work
266 properly.
273 properly.
267
274
268 Starting the controller and engines on different hosts
275 Starting the controller and engines on different hosts
269 ------------------------------------------------------
276 ------------------------------------------------------
270
277
271 When the controller and engines are running on different hosts, things are
278 When the controller and engines are running on different hosts, things are
272 slightly more complicated, but the underlying ideas are the same:
279 slightly more complicated, but the underlying ideas are the same:
273
280
274 1. Start the controller on a host using :command:`ipcontroller`.
281 1. Start the controller on a host using :command:`ipcontroller`.
275 2. Copy :file:`ipcontroller-engine.furl` from :file:`~./ipython/security` on the controller's host to the host where the engines will run.
282 2. Copy :file:`ipcontroller-engine.furl` from :file:`~./ipython/security` on the controller's host to the host where the engines will run.
276 3. Use :command:`ipengine` on the engine's hosts to start the engines.
283 3. Use :command:`ipengine` on the engine's hosts to start the engines.
277
284
278 The only thing you have to be careful of is to tell :command:`ipengine` where the :file:`ipcontroller-engine.furl` file is located. There are two ways you can do this:
285 The only thing you have to be careful of is to tell :command:`ipengine` where the :file:`ipcontroller-engine.furl` file is located. There are two ways you can do this:
279
286
280 * Put :file:`ipcontroller-engine.furl` in the :file:`~./ipython/security`
287 * Put :file:`ipcontroller-engine.furl` in the :file:`~./ipython/security`
281 directory on the engine's host, where it will be found automatically.
288 directory on the engine's host, where it will be found automatically.
282 * Call :command:`ipengine` with the ``--furl-file=full_path_to_the_file``
289 * Call :command:`ipengine` with the ``--furl-file=full_path_to_the_file``
283 flag.
290 flag.
284
291
285 The ``--furl-file`` flag works like this::
292 The ``--furl-file`` flag works like this::
286
293
287 $ ipengine --furl-file=/path/to/my/ipcontroller-engine.furl
294 $ ipengine --furl-file=/path/to/my/ipcontroller-engine.furl
288
295
289 .. note::
296 .. note::
290
297
291 If the controller's and engine's hosts all have a shared file system
298 If the controller's and engine's hosts all have a shared file system
292 (:file:`~./ipython/security` is the same on all of them), then things
299 (:file:`~./ipython/security` is the same on all of them), then things
293 will just work!
300 will just work!
294
301
295 Make FURL files persistent
302 Make FURL files persistent
296 ---------------------------
303 ---------------------------
297
304
298 At fist glance it may seem that that managing the FURL files is a bit annoying. Going back to the house and key analogy, copying the FURL around each time you start the controller is like having to make a new key every time you want to unlock the door and enter your house. As with your house, you want to be able to create the key (or FURL file) once, and then simply use it at any point in the future.
305 At fist glance it may seem that that managing the FURL files is a bit annoying. Going back to the house and key analogy, copying the FURL around each time you start the controller is like having to make a new key every time you want to unlock the door and enter your house. As with your house, you want to be able to create the key (or FURL file) once, and then simply use it at any point in the future.
299
306
300 This is possible. The only thing you have to do is decide what ports the controller will listen on for the engines and clients. This is done as follows::
307 This is possible. The only thing you have to do is decide what ports the controller will listen on for the engines and clients. This is done as follows::
301
308
302 $ ipcontroller -r --client-port=10101 --engine-port=10102
309 $ ipcontroller -r --client-port=10101 --engine-port=10102
303
310
311 These options also work with all of the various modes of
312 :command:`ipcluster`::
313
314 $ ipcluster local -n 2 -r --client-port=10101 --engine-port=10102
315
304 Then, just copy the furl files over the first time and you are set. You can start and stop the controller and engines any many times as you want in the future, just make sure to tell the controller to use the *same* ports.
316 Then, just copy the furl files over the first time and you are set. You can start and stop the controller and engines any many times as you want in the future, just make sure to tell the controller to use the *same* ports.
305
317
306 .. note::
318 .. note::
307
319
308 You may ask the question: what ports does the controller listen on if you
320 You may ask the question: what ports does the controller listen on if you
309 don't tell is to use specific ones? The default is to use high random port
321 don't tell is to use specific ones? The default is to use high random port
310 numbers. We do this for two reasons: i) to increase security through
322 numbers. We do this for two reasons: i) to increase security through
311 obscurity and ii) to multiple controllers on a given host to start and
323 obscurity and ii) to multiple controllers on a given host to start and
312 automatically use different ports.
324 automatically use different ports.
313
325
314 Log files
326 Log files
315 ---------
327 ---------
316
328
317 All of the components of IPython have log files associated with them.
329 All of the components of IPython have log files associated with them.
318 These log files can be extremely useful in debugging problems with
330 These log files can be extremely useful in debugging problems with
319 IPython and can be found in the directory :file:`~/.ipython/log`. Sending
331 IPython and can be found in the directory :file:`~/.ipython/log`. Sending
320 the log files to us will often help us to debug any problems.
332 the log files to us will often help us to debug any problems.
321
333
322
334
323 .. [PBS] Portable Batch System. http://www.openpbs.org/
335 .. [PBS] Portable Batch System. http://www.openpbs.org/
324 .. [SSH] SSH-Agent http://en.wikipedia.org/wiki/Ssh-agent
336 .. [SSH] SSH-Agent http://en.wikipedia.org/wiki/Ssh-agent
@@ -1,87 +1,93 b''
1 #
1 #
2 # A pair of directives for inserting content that will only appear in
2 # A pair of directives for inserting content that will only appear in
3 # either html or latex.
3 # either html or latex.
4 #
4 #
5
5
6 from docutils.nodes import Body, Element
6 from docutils.nodes import Body, Element
7 from docutils.writers.html4css1 import HTMLTranslator
7 from docutils.writers.html4css1 import HTMLTranslator
8 from sphinx.latexwriter import LaTeXTranslator
9 from docutils.parsers.rst import directives
8 from docutils.parsers.rst import directives
10
9
10 # The sphinx API has changed, so we try both the old and new import forms
11 try:
12 from sphinx.latexwriter import LaTeXTranslator
13 except ImportError:
14 from sphinx.writers.latex import LaTeXTranslator
15
16
11 class html_only(Body, Element):
17 class html_only(Body, Element):
12 pass
18 pass
13
19
14 class latex_only(Body, Element):
20 class latex_only(Body, Element):
15 pass
21 pass
16
22
17 def run(content, node_class, state, content_offset):
23 def run(content, node_class, state, content_offset):
18 text = '\n'.join(content)
24 text = '\n'.join(content)
19 node = node_class(text)
25 node = node_class(text)
20 state.nested_parse(content, content_offset, node)
26 state.nested_parse(content, content_offset, node)
21 return [node]
27 return [node]
22
28
23 try:
29 try:
24 from docutils.parsers.rst import Directive
30 from docutils.parsers.rst import Directive
25 except ImportError:
31 except ImportError:
26 from docutils.parsers.rst.directives import _directives
32 from docutils.parsers.rst.directives import _directives
27
33
28 def html_only_directive(name, arguments, options, content, lineno,
34 def html_only_directive(name, arguments, options, content, lineno,
29 content_offset, block_text, state, state_machine):
35 content_offset, block_text, state, state_machine):
30 return run(content, html_only, state, content_offset)
36 return run(content, html_only, state, content_offset)
31
37
32 def latex_only_directive(name, arguments, options, content, lineno,
38 def latex_only_directive(name, arguments, options, content, lineno,
33 content_offset, block_text, state, state_machine):
39 content_offset, block_text, state, state_machine):
34 return run(content, latex_only, state, content_offset)
40 return run(content, latex_only, state, content_offset)
35
41
36 for func in (html_only_directive, latex_only_directive):
42 for func in (html_only_directive, latex_only_directive):
37 func.content = 1
43 func.content = 1
38 func.options = {}
44 func.options = {}
39 func.arguments = None
45 func.arguments = None
40
46
41 _directives['htmlonly'] = html_only_directive
47 _directives['htmlonly'] = html_only_directive
42 _directives['latexonly'] = latex_only_directive
48 _directives['latexonly'] = latex_only_directive
43 else:
49 else:
44 class OnlyDirective(Directive):
50 class OnlyDirective(Directive):
45 has_content = True
51 has_content = True
46 required_arguments = 0
52 required_arguments = 0
47 optional_arguments = 0
53 optional_arguments = 0
48 final_argument_whitespace = True
54 final_argument_whitespace = True
49 option_spec = {}
55 option_spec = {}
50
56
51 def run(self):
57 def run(self):
52 self.assert_has_content()
58 self.assert_has_content()
53 return run(self.content, self.node_class,
59 return run(self.content, self.node_class,
54 self.state, self.content_offset)
60 self.state, self.content_offset)
55
61
56 class HtmlOnlyDirective(OnlyDirective):
62 class HtmlOnlyDirective(OnlyDirective):
57 node_class = html_only
63 node_class = html_only
58
64
59 class LatexOnlyDirective(OnlyDirective):
65 class LatexOnlyDirective(OnlyDirective):
60 node_class = latex_only
66 node_class = latex_only
61
67
62 directives.register_directive('htmlonly', HtmlOnlyDirective)
68 directives.register_directive('htmlonly', HtmlOnlyDirective)
63 directives.register_directive('latexonly', LatexOnlyDirective)
69 directives.register_directive('latexonly', LatexOnlyDirective)
64
70
65 def setup(app):
71 def setup(app):
66 app.add_node(html_only)
72 app.add_node(html_only)
67 app.add_node(latex_only)
73 app.add_node(latex_only)
68
74
69 # Add visit/depart methods to HTML-Translator:
75 # Add visit/depart methods to HTML-Translator:
70 def visit_perform(self, node):
76 def visit_perform(self, node):
71 pass
77 pass
72 def depart_perform(self, node):
78 def depart_perform(self, node):
73 pass
79 pass
74 def visit_ignore(self, node):
80 def visit_ignore(self, node):
75 node.children = []
81 node.children = []
76 def depart_ignore(self, node):
82 def depart_ignore(self, node):
77 node.children = []
83 node.children = []
78
84
79 HTMLTranslator.visit_html_only = visit_perform
85 HTMLTranslator.visit_html_only = visit_perform
80 HTMLTranslator.depart_html_only = depart_perform
86 HTMLTranslator.depart_html_only = depart_perform
81 HTMLTranslator.visit_latex_only = visit_ignore
87 HTMLTranslator.visit_latex_only = visit_ignore
82 HTMLTranslator.depart_latex_only = depart_ignore
88 HTMLTranslator.depart_latex_only = depart_ignore
83
89
84 LaTeXTranslator.visit_html_only = visit_ignore
90 LaTeXTranslator.visit_html_only = visit_ignore
85 LaTeXTranslator.depart_html_only = depart_ignore
91 LaTeXTranslator.depart_html_only = depart_ignore
86 LaTeXTranslator.visit_latex_only = visit_perform
92 LaTeXTranslator.visit_latex_only = visit_perform
87 LaTeXTranslator.depart_latex_only = depart_perform
93 LaTeXTranslator.depart_latex_only = depart_perform
General Comments 0
You need to be logged in to leave comments. Login now