##// END OF EJS Templates
Merging -r 1177 from lp:ipython with fixes and resolutions....
Brian Granger -
r2124:4a54d9d3 merge
parent child Browse files
Show More
@@ -1,249 +1,254 b''
1 1 """Tests for various magic functions.
2 2
3 3 Needs to be run by nose (to make ipython session available).
4 4 """
5 5
6 6 import os
7 7 import sys
8 8 import tempfile
9 9 import types
10 10
11 11 import nose.tools as nt
12 12
13 13 from IPython.utils.platutils import find_cmd, get_long_path_name
14 14 from IPython.testing import decorators as dec
15 15 from IPython.testing import tools as tt
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Test functions begin
19 19
20 20 def test_rehashx():
21 21 # clear up everything
22 22 _ip.IP.alias_table.clear()
23 23 del _ip.db['syscmdlist']
24 24
25 25 _ip.magic('rehashx')
26 26 # Practically ALL ipython development systems will have more than 10 aliases
27 27
28 assert len(_ip.IP.alias_table) > 10
28 yield (nt.assert_true, len(_ip.IP.alias_table) > 10)
29 29 for key, val in _ip.IP.alias_table.items():
30 30 # we must strip dots from alias names
31 assert '.' not in key
31 nt.assert_true('.' not in key)
32 32
33 33 # rehashx must fill up syscmdlist
34 34 scoms = _ip.db['syscmdlist']
35 assert len(scoms) > 10
35 yield (nt.assert_true, len(scoms) > 10)
36 36
37 37
38 38 def doctest_hist_f():
39 39 """Test %hist -f with temporary filename.
40 40
41 41 In [9]: import tempfile
42 42
43 43 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
44 44
45 In [11]: %history -n -f $tfile 3
45 In [11]: %hist -n -f $tfile 3
46
46 47 """
47 48
48 49
49 50 def doctest_hist_r():
50 51 """Test %hist -r
51 52
52 53 XXX - This test is not recording the output correctly. Not sure why...
53 54
55 In [20]: 'hist' in _ip.IP.lsmagic()
56 Out[20]: True
57
54 58 In [6]: x=1
55 59
56 In [7]: hist -n -r 2
60 In [7]: %hist -n -r 2
57 61 x=1 # random
58 62 hist -n -r 2 # random
59 63 """
60 64
61 65 # This test is known to fail on win32.
62 66 # See ticket https://bugs.launchpad.net/bugs/366334
63 67 def test_obj_del():
64 68 """Test that object's __del__ methods are called on exit."""
65 69 test_dir = os.path.dirname(__file__)
66 70 del_file = os.path.join(test_dir,'obj_del.py')
67 71 ipython_cmd = find_cmd('ipython')
68 72 out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file))
69 73 nt.assert_equals(out,'obj_del.py: object A deleted')
70 74
71 75
72 76 def test_shist():
73 77 # Simple tests of ShadowHist class - test generator.
74 78 import os, shutil, tempfile
75 79
76 80 from IPython.extensions import pickleshare
77 81 from IPython.core.history import ShadowHist
78 82
79 83 tfile = tempfile.mktemp('','tmp-ipython-')
80 84
81 85 db = pickleshare.PickleShareDB(tfile)
82 86 s = ShadowHist(db)
83 87 s.add('hello')
84 88 s.add('world')
85 89 s.add('hello')
86 90 s.add('hello')
87 91 s.add('karhu')
88 92
89 93 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
90 94
91 95 yield nt.assert_equal,s.get(2),'world'
92 96
93 97 shutil.rmtree(tfile)
94 98
95 99 @dec.skipif_not_numpy
96 100 def test_numpy_clear_array_undec():
101 from IPython.extensions import clearcmd
102
97 103 _ip.ex('import numpy as np')
98 104 _ip.ex('a = np.empty(2)')
99
100 yield nt.assert_true,'a' in _ip.user_ns
105 yield (nt.assert_true, 'a' in _ip.user_ns)
101 106 _ip.magic('clear array')
102 yield nt.assert_false,'a' in _ip.user_ns
107 yield (nt.assert_false, 'a' in _ip.user_ns)
103 108
104 109
105 110 @dec.skip()
106 111 def test_fail_dec(*a,**k):
107 112 yield nt.assert_true, False
108 113
109 114 @dec.skip('This one shouldn not run')
110 115 def test_fail_dec2(*a,**k):
111 116 yield nt.assert_true, False
112 117
113 118 @dec.skipknownfailure
114 119 def test_fail_dec3(*a,**k):
115 120 yield nt.assert_true, False
116 121
117 122
118 123 def doctest_refbug():
119 124 """Very nasty problem with references held by multiple runs of a script.
120 125 See: https://bugs.launchpad.net/ipython/+bug/269966
121 126
122 127 In [1]: _ip.IP.clear_main_mod_cache()
123 128
124 129 In [2]: run refbug
125 130
126 131 In [3]: call_f()
127 132 lowercased: hello
128 133
129 134 In [4]: run refbug
130 135
131 136 In [5]: call_f()
132 137 lowercased: hello
133 138 lowercased: hello
134 139 """
135 140
136 141 #-----------------------------------------------------------------------------
137 142 # Tests for %run
138 143 #-----------------------------------------------------------------------------
139 144
140 145 # %run is critical enough that it's a good idea to have a solid collection of
141 146 # tests for it, some as doctests and some as normal tests.
142 147
143 148 def doctest_run_ns():
144 149 """Classes declared %run scripts must be instantiable afterwards.
145 150
146 151 In [11]: run tclass foo
147 152
148 153 In [12]: isinstance(f(),foo)
149 154 Out[12]: True
150 155 """
151 156
152 157
153 158 def doctest_run_ns2():
154 159 """Classes declared %run scripts must be instantiable afterwards.
155 160
156 161 In [4]: run tclass C-first_pass
157 162
158 163 In [5]: run tclass C-second_pass
159 164 tclass.py: deleting object: C-first_pass
160 165 """
161 166
162 167 @dec.skip_win32
163 168 def doctest_run_builtins():
164 169 """Check that %run doesn't damage __builtins__ via a doctest.
165 170
166 171 This is similar to the test_run_builtins, but I want *both* forms of the
167 172 test to catch any possible glitches in our testing machinery, since that
168 173 modifies %run somewhat. So for this, we have both a normal test (below)
169 174 and a doctest (this one).
170 175
171 176 In [1]: import tempfile
172 177
173 178 In [2]: bid1 = id(__builtins__)
174 179
175 180 In [3]: f = tempfile.NamedTemporaryFile()
176 181
177 182 In [4]: f.write('pass\\n')
178 183
179 184 In [5]: f.flush()
180 185
181 186 In [6]: print 'B1:',type(__builtins__)
182 187 B1: <type 'module'>
183 188
184 189 In [7]: %run $f.name
185 190
186 191 In [8]: bid2 = id(__builtins__)
187 192
188 193 In [9]: print 'B2:',type(__builtins__)
189 194 B2: <type 'module'>
190 195
191 196 In [10]: bid1 == bid2
192 197 Out[10]: True
193 198 """
194 199
195 200 # For some tests, it will be handy to organize them in a class with a common
196 201 # setup that makes a temp file
197 202
198 203 class TestMagicRun(object):
199 204
200 205 def setup(self):
201 206 """Make a valid python temp file."""
202 207 f = tempfile.NamedTemporaryFile()
203 208 f.write('pass\n')
204 209 f.flush()
205 210 self.tmpfile = f
206 211
207 212 def run_tmpfile(self):
208 213 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
209 214 # See below and ticket https://bugs.launchpad.net/bugs/366353
210 215 _ip.magic('run %s' % self.tmpfile.name)
211 216
212 217 # See https://bugs.launchpad.net/bugs/366353
213 218 @dec.skip_if_not_win32
214 219 def test_run_tempfile_path(self):
215 220 tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.")
216 221
217 222 # See https://bugs.launchpad.net/bugs/366353
218 223 @dec.skip_win32
219 224 def test_builtins_id(self):
220 225 """Check that %run doesn't damage __builtins__ """
221 226
222 227 # Test that the id of __builtins__ is not modified by %run
223 228 bid1 = id(_ip.user_ns['__builtins__'])
224 229 self.run_tmpfile()
225 230 bid2 = id(_ip.user_ns['__builtins__'])
226 231 tt.assert_equals(bid1, bid2)
227 232
228 233 # See https://bugs.launchpad.net/bugs/366353
229 234 @dec.skip_win32
230 235 def test_builtins_type(self):
231 236 """Check that the type of __builtins__ doesn't change with %run.
232 237
233 238 However, the above could pass if __builtins__ was already modified to
234 239 be a dict (it should be a module) by a previous use of %run. So we
235 240 also check explicitly that it really is a module:
236 241 """
237 242 self.run_tmpfile()
238 243 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
239 244
240 245 # See https://bugs.launchpad.net/bugs/366353
241 246 @dec.skip_win32
242 247 def test_prompts(self):
243 248 """Test that prompts correctly generate after %run"""
244 249 self.run_tmpfile()
245 250 p2 = str(_ip.IP.outputcache.prompt2).strip()
246 251 nt.assert_equals(p2[:3], '...')
247 252
248 253 def teardown(self):
249 254 self.tmpfile.close()
@@ -1,1057 +1,1063 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 ultratb.py -- Spice up your tracebacks!
4 4
5 5 * ColorTB
6 6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 7 ColorTB class is a solution to that problem. It colors the different parts of a
8 8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 9 text editor.
10 10
11 11 Installation instructions for ColorTB:
12 12 import sys,ultratb
13 13 sys.excepthook = ultratb.ColorTB()
14 14
15 15 * VerboseTB
16 16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 18 and intended it for CGI programmers, but why should they have all the fun? I
19 19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 20 but kind of neat, and maybe useful for long-running programs that you believe
21 21 are bug-free. If a crash *does* occur in that type of program you want details.
22 22 Give it a shot--you'll love it or you'll hate it.
23 23
24 24 Note:
25 25
26 26 The Verbose mode prints the variables currently visible where the exception
27 27 happened (shortening their strings if too long). This can potentially be
28 28 very slow, if you happen to have a huge data structure whose string
29 29 representation is complex to compute. Your computer may appear to freeze for
30 30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 31 with Ctrl-C (maybe hitting it more than once).
32 32
33 33 If you encounter this kind of situation often, you may want to use the
34 34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 35 variables (but otherwise includes the information and context given by
36 36 Verbose).
37 37
38 38
39 39 Installation instructions for ColorTB:
40 40 import sys,ultratb
41 41 sys.excepthook = ultratb.VerboseTB()
42 42
43 43 Note: Much of the code in this module was lifted verbatim from the standard
44 44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45 45
46 46 * Color schemes
47 47 The colors are defined in the class TBTools through the use of the
48 48 ColorSchemeTable class. Currently the following exist:
49 49
50 50 - NoColor: allows all of this module to be used in any terminal (the color
51 51 escapes are just dummy blank strings).
52 52
53 53 - Linux: is meant to look good in a terminal like the Linux console (black
54 54 or very dark background).
55 55
56 56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 57 in light background terminals.
58 58
59 59 You can implement other color schemes easily, the syntax is fairly
60 60 self-explanatory. Please send back new schemes you develop to the author for
61 61 possible inclusion in future releases.
62 62 """
63 63
64 64 #*****************************************************************************
65 65 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 66 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
67 67 #
68 68 # Distributed under the terms of the BSD License. The full license is in
69 69 # the file COPYING, distributed as part of this software.
70 70 #*****************************************************************************
71 71
72 72 # Required modules
73 73 import inspect
74 74 import keyword
75 75 import linecache
76 76 import os
77 77 import pydoc
78 78 import re
79 79 import string
80 80 import sys
81 81 import time
82 82 import tokenize
83 83 import traceback
84 84 import types
85 85
86 86 # For purposes of monkeypatching inspect to fix a bug in it.
87 87 from inspect import getsourcefile, getfile, getmodule,\
88 88 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
89 89
90 90
91 91 # IPython's own modules
92 92 # Modified pdb which doesn't damage IPython's readline handling
93 93 from IPython.utils import PyColorize
94 from IPython.core import debugger
94 from IPython.core import debugger, ipapi
95 95 from IPython.utils.ipstruct import Struct
96 96 from IPython.core.excolors import exception_colors
97 97 from IPython.utils.genutils import Term,uniq_stable,error,info
98 98
99 99 # Globals
100 100 # amount of space to put line numbers before verbose tracebacks
101 101 INDENT_SIZE = 8
102 102
103 103 # Default color scheme. This is used, for example, by the traceback
104 104 # formatter. When running in an actual IPython instance, the user's rc.colors
105 105 # value is used, but havinga module global makes this functionality available
106 106 # to users of ultratb who are NOT running inside ipython.
107 107 DEFAULT_SCHEME = 'NoColor'
108 108
109 109 #---------------------------------------------------------------------------
110 110 # Code begins
111 111
112 112 # Utility functions
113 113 def inspect_error():
114 114 """Print a message about internal inspect errors.
115 115
116 116 These are unfortunately quite common."""
117 117
118 118 error('Internal Python error in the inspect module.\n'
119 119 'Below is the traceback from this internal error.\n')
120 120
121 121
122 122 def findsource(object):
123 123 """Return the entire source file and starting line number for an object.
124 124
125 125 The argument may be a module, class, method, function, traceback, frame,
126 126 or code object. The source code is returned as a list of all the lines
127 127 in the file and the line number indexes a line in that list. An IOError
128 128 is raised if the source code cannot be retrieved.
129 129
130 130 FIXED version with which we monkeypatch the stdlib to work around a bug."""
131 131
132 132 file = getsourcefile(object) or getfile(object)
133 133 # If the object is a frame, then trying to get the globals dict from its
134 134 # module won't work. Instead, the frame object itself has the globals
135 135 # dictionary.
136 136 globals_dict = None
137 137 if inspect.isframe(object):
138 138 # XXX: can this ever be false?
139 139 globals_dict = object.f_globals
140 140 else:
141 141 module = getmodule(object, file)
142 142 if module:
143 143 globals_dict = module.__dict__
144 144 lines = linecache.getlines(file, globals_dict)
145 145 if not lines:
146 146 raise IOError('could not get source code')
147 147
148 148 if ismodule(object):
149 149 return lines, 0
150 150
151 151 if isclass(object):
152 152 name = object.__name__
153 153 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
154 154 # make some effort to find the best matching class definition:
155 155 # use the one with the least indentation, which is the one
156 156 # that's most probably not inside a function definition.
157 157 candidates = []
158 158 for i in range(len(lines)):
159 159 match = pat.match(lines[i])
160 160 if match:
161 161 # if it's at toplevel, it's already the best one
162 162 if lines[i][0] == 'c':
163 163 return lines, i
164 164 # else add whitespace to candidate list
165 165 candidates.append((match.group(1), i))
166 166 if candidates:
167 167 # this will sort by whitespace, and by line number,
168 168 # less whitespace first
169 169 candidates.sort()
170 170 return lines, candidates[0][1]
171 171 else:
172 172 raise IOError('could not find class definition')
173 173
174 174 if ismethod(object):
175 175 object = object.im_func
176 176 if isfunction(object):
177 177 object = object.func_code
178 178 if istraceback(object):
179 179 object = object.tb_frame
180 180 if isframe(object):
181 181 object = object.f_code
182 182 if iscode(object):
183 183 if not hasattr(object, 'co_firstlineno'):
184 184 raise IOError('could not find function definition')
185 185 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
186 186 pmatch = pat.match
187 187 # fperez - fix: sometimes, co_firstlineno can give a number larger than
188 188 # the length of lines, which causes an error. Safeguard against that.
189 189 lnum = min(object.co_firstlineno,len(lines))-1
190 190 while lnum > 0:
191 191 if pmatch(lines[lnum]): break
192 192 lnum -= 1
193 193
194 194 return lines, lnum
195 195 raise IOError('could not find code object')
196 196
197 197 # Monkeypatch inspect to apply our bugfix. This code only works with py25
198 198 if sys.version_info[:2] >= (2,5):
199 199 inspect.findsource = findsource
200 200
201 201 def fix_frame_records_filenames(records):
202 202 """Try to fix the filenames in each record from inspect.getinnerframes().
203 203
204 204 Particularly, modules loaded from within zip files have useless filenames
205 205 attached to their code object, and inspect.getinnerframes() just uses it.
206 206 """
207 207 fixed_records = []
208 208 for frame, filename, line_no, func_name, lines, index in records:
209 209 # Look inside the frame's globals dictionary for __file__, which should
210 210 # be better.
211 211 better_fn = frame.f_globals.get('__file__', None)
212 212 if isinstance(better_fn, str):
213 213 # Check the type just in case someone did something weird with
214 214 # __file__. It might also be None if the error occurred during
215 215 # import.
216 216 filename = better_fn
217 217 fixed_records.append((frame, filename, line_no, func_name, lines, index))
218 218 return fixed_records
219 219
220 220
221 221 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
222 222 import linecache
223 223 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
224 224
225 225 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
226 226
227 227 # If the error is at the console, don't build any context, since it would
228 228 # otherwise produce 5 blank lines printed out (there is no file at the
229 229 # console)
230 230 rec_check = records[tb_offset:]
231 231 try:
232 232 rname = rec_check[0][1]
233 233 if rname == '<ipython console>' or rname.endswith('<string>'):
234 234 return rec_check
235 235 except IndexError:
236 236 pass
237 237
238 238 aux = traceback.extract_tb(etb)
239 239 assert len(records) == len(aux)
240 240 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
241 241 maybeStart = lnum-1 - context//2
242 242 start = max(maybeStart, 0)
243 243 end = start + context
244 244 lines = linecache.getlines(file)[start:end]
245 245 # pad with empty lines if necessary
246 246 if maybeStart < 0:
247 247 lines = (['\n'] * -maybeStart) + lines
248 248 if len(lines) < context:
249 249 lines += ['\n'] * (context - len(lines))
250 250 buf = list(records[i])
251 251 buf[LNUM_POS] = lnum
252 252 buf[INDEX_POS] = lnum - 1 - start
253 253 buf[LINES_POS] = lines
254 254 records[i] = tuple(buf)
255 255 return records[tb_offset:]
256 256
257 257 # Helper function -- largely belongs to VerboseTB, but we need the same
258 258 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
259 259 # can be recognized properly by ipython.el's py-traceback-line-re
260 260 # (SyntaxErrors have to be treated specially because they have no traceback)
261 261
262 262 _parser = PyColorize.Parser()
263 263
264 264 def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None):
265 265 numbers_width = INDENT_SIZE - 1
266 266 res = []
267 267 i = lnum - index
268 268
269 269 # This lets us get fully syntax-highlighted tracebacks.
270 270 if scheme is None:
271 try:
272 scheme = __IPYTHON__.rc.colors
273 except:
271 ipinst = ipapi.get()
272 if ipinst is not None:
273 scheme = ipinst.IP.rc.colors
274 else:
274 275 scheme = DEFAULT_SCHEME
276
275 277 _line_format = _parser.format2
276 278
277 279 for line in lines:
278 280 new_line, err = _line_format(line,'str',scheme)
279 281 if not err: line = new_line
280 282
281 283 if i == lnum:
282 284 # This is the line with the error
283 285 pad = numbers_width - len(str(i))
284 286 if pad >= 3:
285 287 marker = '-'*(pad-3) + '-> '
286 288 elif pad == 2:
287 289 marker = '> '
288 290 elif pad == 1:
289 291 marker = '>'
290 292 else:
291 293 marker = ''
292 294 num = marker + str(i)
293 295 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
294 296 Colors.line, line, Colors.Normal)
295 297 else:
296 298 num = '%*s' % (numbers_width,i)
297 299 line = '%s%s%s %s' %(Colors.lineno, num,
298 300 Colors.Normal, line)
299 301
300 302 res.append(line)
301 303 if lvals and i == lnum:
302 304 res.append(lvals + '\n')
303 305 i = i + 1
304 306 return res
305 307
306 308
307 309 #---------------------------------------------------------------------------
308 310 # Module classes
309 311 class TBTools:
310 312 """Basic tools used by all traceback printer classes."""
311 313
312 314 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
313 315 # Whether to call the interactive pdb debugger after printing
314 316 # tracebacks or not
315 317 self.call_pdb = call_pdb
316 318
317 319 # Create color table
318 320 self.color_scheme_table = exception_colors()
319 321
320 322 self.set_colors(color_scheme)
321 323 self.old_scheme = color_scheme # save initial value for toggles
322 324
323 325 if call_pdb:
324 326 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
325 327 else:
326 328 self.pdb = None
327 329
328 330 def set_colors(self,*args,**kw):
329 331 """Shorthand access to the color table scheme selector method."""
330 332
331 333 # Set own color table
332 334 self.color_scheme_table.set_active_scheme(*args,**kw)
333 335 # for convenience, set Colors to the active scheme
334 336 self.Colors = self.color_scheme_table.active_colors
335 337 # Also set colors of debugger
336 338 if hasattr(self,'pdb') and self.pdb is not None:
337 339 self.pdb.set_colors(*args,**kw)
338 340
339 341 def color_toggle(self):
340 342 """Toggle between the currently active color scheme and NoColor."""
341 343
342 344 if self.color_scheme_table.active_scheme_name == 'NoColor':
343 345 self.color_scheme_table.set_active_scheme(self.old_scheme)
344 346 self.Colors = self.color_scheme_table.active_colors
345 347 else:
346 348 self.old_scheme = self.color_scheme_table.active_scheme_name
347 349 self.color_scheme_table.set_active_scheme('NoColor')
348 350 self.Colors = self.color_scheme_table.active_colors
349 351
350 352 #---------------------------------------------------------------------------
351 353 class ListTB(TBTools):
352 354 """Print traceback information from a traceback list, with optional color.
353 355
354 356 Calling: requires 3 arguments:
355 357 (etype, evalue, elist)
356 358 as would be obtained by:
357 359 etype, evalue, tb = sys.exc_info()
358 360 if tb:
359 361 elist = traceback.extract_tb(tb)
360 362 else:
361 363 elist = None
362 364
363 365 It can thus be used by programs which need to process the traceback before
364 366 printing (such as console replacements based on the code module from the
365 367 standard library).
366 368
367 369 Because they are meant to be called without a full traceback (only a
368 370 list), instances of this class can't call the interactive pdb debugger."""
369 371
370 372 def __init__(self,color_scheme = 'NoColor'):
371 373 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
372 374
373 375 def __call__(self, etype, value, elist):
374 376 Term.cout.flush()
375 377 print >> Term.cerr, self.text(etype,value,elist)
376 378 Term.cerr.flush()
377 379
378 380 def text(self,etype, value, elist,context=5):
379 381 """Return a color formatted string with the traceback info."""
380 382
381 383 Colors = self.Colors
382 384 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
383 385 if elist:
384 386 out_string.append('Traceback %s(most recent call last)%s:' % \
385 387 (Colors.normalEm, Colors.Normal) + '\n')
386 388 out_string.extend(self._format_list(elist))
387 389 lines = self._format_exception_only(etype, value)
388 390 for line in lines[:-1]:
389 391 out_string.append(" "+line)
390 392 out_string.append(lines[-1])
391 393 return ''.join(out_string)
392 394
393 395 def _format_list(self, extracted_list):
394 396 """Format a list of traceback entry tuples for printing.
395 397
396 398 Given a list of tuples as returned by extract_tb() or
397 399 extract_stack(), return a list of strings ready for printing.
398 400 Each string in the resulting list corresponds to the item with the
399 401 same index in the argument list. Each string ends in a newline;
400 402 the strings may contain internal newlines as well, for those items
401 403 whose source text line is not None.
402 404
403 405 Lifted almost verbatim from traceback.py
404 406 """
405 407
406 408 Colors = self.Colors
407 409 list = []
408 410 for filename, lineno, name, line in extracted_list[:-1]:
409 411 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
410 412 (Colors.filename, filename, Colors.Normal,
411 413 Colors.lineno, lineno, Colors.Normal,
412 414 Colors.name, name, Colors.Normal)
413 415 if line:
414 416 item = item + ' %s\n' % line.strip()
415 417 list.append(item)
416 418 # Emphasize the last entry
417 419 filename, lineno, name, line = extracted_list[-1]
418 420 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
419 421 (Colors.normalEm,
420 422 Colors.filenameEm, filename, Colors.normalEm,
421 423 Colors.linenoEm, lineno, Colors.normalEm,
422 424 Colors.nameEm, name, Colors.normalEm,
423 425 Colors.Normal)
424 426 if line:
425 427 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
426 428 Colors.Normal)
427 429 list.append(item)
428 430 return list
429 431
430 432 def _format_exception_only(self, etype, value):
431 433 """Format the exception part of a traceback.
432 434
433 435 The arguments are the exception type and value such as given by
434 436 sys.exc_info()[:2]. The return value is a list of strings, each ending
435 437 in a newline. Normally, the list contains a single string; however,
436 438 for SyntaxError exceptions, it contains several lines that (when
437 439 printed) display detailed information about where the syntax error
438 440 occurred. The message indicating which exception occurred is the
439 441 always last string in the list.
440 442
441 443 Also lifted nearly verbatim from traceback.py
442 444 """
443 445
444 446 have_filedata = False
445 447 Colors = self.Colors
446 448 list = []
447 449 try:
448 450 stype = Colors.excName + etype.__name__ + Colors.Normal
449 451 except AttributeError:
450 452 stype = etype # String exceptions don't get special coloring
451 453 if value is None:
452 454 list.append( str(stype) + '\n')
453 455 else:
454 456 if etype is SyntaxError:
455 457 try:
456 458 msg, (filename, lineno, offset, line) = value
457 459 except:
458 460 have_filedata = False
459 461 else:
460 462 have_filedata = True
461 463 #print 'filename is',filename # dbg
462 464 if not filename: filename = "<string>"
463 465 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
464 466 (Colors.normalEm,
465 467 Colors.filenameEm, filename, Colors.normalEm,
466 468 Colors.linenoEm, lineno, Colors.Normal ))
467 469 if line is not None:
468 470 i = 0
469 471 while i < len(line) and line[i].isspace():
470 472 i = i+1
471 473 list.append('%s %s%s\n' % (Colors.line,
472 474 line.strip(),
473 475 Colors.Normal))
474 476 if offset is not None:
475 477 s = ' '
476 478 for c in line[i:offset-1]:
477 479 if c.isspace():
478 480 s = s + c
479 481 else:
480 482 s = s + ' '
481 483 list.append('%s%s^%s\n' % (Colors.caret, s,
482 484 Colors.Normal) )
483 485 value = msg
484 486 s = self._some_str(value)
485 487 if s:
486 488 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
487 489 Colors.Normal, s))
488 490 else:
489 491 list.append('%s\n' % str(stype))
490 492
491 493 # vds:>>
492 494 if have_filedata:
493 __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0)
495 ipinst = ipapi.get()
496 if ipinst is not None:
497 ipinst.IP.hooks.synchronize_with_editor(filename, lineno, 0)
494 498 # vds:<<
495 499
496 500 return list
497 501
498 502 def _some_str(self, value):
499 503 # Lifted from traceback.py
500 504 try:
501 505 return str(value)
502 506 except:
503 507 return '<unprintable %s object>' % type(value).__name__
504 508
505 509 #----------------------------------------------------------------------------
506 510 class VerboseTB(TBTools):
507 511 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
508 512 of HTML. Requires inspect and pydoc. Crazy, man.
509 513
510 514 Modified version which optionally strips the topmost entries from the
511 515 traceback, to be used with alternate interpreters (because their own code
512 516 would appear in the traceback)."""
513 517
514 518 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
515 519 call_pdb = 0, include_vars=1):
516 520 """Specify traceback offset, headers and color scheme.
517 521
518 522 Define how many frames to drop from the tracebacks. Calling it with
519 523 tb_offset=1 allows use of this handler in interpreters which will have
520 524 their own code at the top of the traceback (VerboseTB will first
521 525 remove that frame before printing the traceback info)."""
522 526 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
523 527 self.tb_offset = tb_offset
524 528 self.long_header = long_header
525 529 self.include_vars = include_vars
526 530
527 531 def text(self, etype, evalue, etb, context=5):
528 532 """Return a nice text document describing the traceback."""
529 533
530 534 # some locals
531 535 try:
532 536 etype = etype.__name__
533 537 except AttributeError:
534 538 pass
535 539 Colors = self.Colors # just a shorthand + quicker name lookup
536 540 ColorsNormal = Colors.Normal # used a lot
537 541 col_scheme = self.color_scheme_table.active_scheme_name
538 542 indent = ' '*INDENT_SIZE
539 543 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
540 544 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
541 545 exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal)
542 546
543 547 # some internal-use functions
544 548 def text_repr(value):
545 549 """Hopefully pretty robust repr equivalent."""
546 550 # this is pretty horrible but should always return *something*
547 551 try:
548 552 return pydoc.text.repr(value)
549 553 except KeyboardInterrupt:
550 554 raise
551 555 except:
552 556 try:
553 557 return repr(value)
554 558 except KeyboardInterrupt:
555 559 raise
556 560 except:
557 561 try:
558 562 # all still in an except block so we catch
559 563 # getattr raising
560 564 name = getattr(value, '__name__', None)
561 565 if name:
562 566 # ick, recursion
563 567 return text_repr(name)
564 568 klass = getattr(value, '__class__', None)
565 569 if klass:
566 570 return '%s instance' % text_repr(klass)
567 571 except KeyboardInterrupt:
568 572 raise
569 573 except:
570 574 return 'UNRECOVERABLE REPR FAILURE'
571 575 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
572 576 def nullrepr(value, repr=text_repr): return ''
573 577
574 578 # meat of the code begins
575 579 try:
576 580 etype = etype.__name__
577 581 except AttributeError:
578 582 pass
579 583
580 584 if self.long_header:
581 585 # Header with the exception type, python version, and date
582 586 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
583 587 date = time.ctime(time.time())
584 588
585 589 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
586 590 exc, ' '*(75-len(str(etype))-len(pyver)),
587 591 pyver, string.rjust(date, 75) )
588 592 head += "\nA problem occured executing Python code. Here is the sequence of function"\
589 593 "\ncalls leading up to the error, with the most recent (innermost) call last."
590 594 else:
591 595 # Simplified header
592 596 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
593 597 string.rjust('Traceback (most recent call last)',
594 598 75 - len(str(etype)) ) )
595 599 frames = []
596 600 # Flush cache before calling inspect. This helps alleviate some of the
597 601 # problems with python 2.3's inspect.py.
598 602 linecache.checkcache()
599 603 # Drop topmost frames if requested
600 604 try:
601 605 # Try the default getinnerframes and Alex's: Alex's fixes some
602 606 # problems, but it generates empty tracebacks for console errors
603 607 # (5 blanks lines) where none should be returned.
604 608 #records = inspect.getinnerframes(etb, context)[self.tb_offset:]
605 609 #print 'python records:', records # dbg
606 610 records = _fixed_getinnerframes(etb, context,self.tb_offset)
607 611 #print 'alex records:', records # dbg
608 612 except:
609 613
610 614 # FIXME: I've been getting many crash reports from python 2.3
611 615 # users, traceable to inspect.py. If I can find a small test-case
612 616 # to reproduce this, I should either write a better workaround or
613 617 # file a bug report against inspect (if that's the real problem).
614 618 # So far, I haven't been able to find an isolated example to
615 619 # reproduce the problem.
616 620 inspect_error()
617 621 traceback.print_exc(file=Term.cerr)
618 622 info('\nUnfortunately, your original traceback can not be constructed.\n')
619 623 return ''
620 624
621 625 # build some color string templates outside these nested loops
622 626 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
623 627 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
624 628 ColorsNormal)
625 629 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
626 630 (Colors.vName, Colors.valEm, ColorsNormal)
627 631 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
628 632 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
629 633 Colors.vName, ColorsNormal)
630 634 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
631 635 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
632 636 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
633 637 ColorsNormal)
634 638
635 639 # now, loop over all records printing context and info
636 640 abspath = os.path.abspath
637 641 for frame, file, lnum, func, lines, index in records:
638 642 #print '*** record:',file,lnum,func,lines,index # dbg
639 643 try:
640 644 file = file and abspath(file) or '?'
641 645 except OSError:
642 646 # if file is '<console>' or something not in the filesystem,
643 647 # the abspath call will throw an OSError. Just ignore it and
644 648 # keep the original file string.
645 649 pass
646 650 link = tpl_link % file
647 651 try:
648 652 args, varargs, varkw, locals = inspect.getargvalues(frame)
649 653 except:
650 654 # This can happen due to a bug in python2.3. We should be
651 655 # able to remove this try/except when 2.4 becomes a
652 656 # requirement. Bug details at http://python.org/sf/1005466
653 657 inspect_error()
654 658 traceback.print_exc(file=Term.cerr)
655 659 info("\nIPython's exception reporting continues...\n")
656 660
657 661 if func == '?':
658 662 call = ''
659 663 else:
660 664 # Decide whether to include variable details or not
661 665 var_repr = self.include_vars and eqrepr or nullrepr
662 666 try:
663 667 call = tpl_call % (func,inspect.formatargvalues(args,
664 668 varargs, varkw,
665 669 locals,formatvalue=var_repr))
666 670 except KeyError:
667 671 # Very odd crash from inspect.formatargvalues(). The
668 672 # scenario under which it appeared was a call to
669 673 # view(array,scale) in NumTut.view.view(), where scale had
670 674 # been defined as a scalar (it should be a tuple). Somehow
671 675 # inspect messes up resolving the argument list of view()
672 676 # and barfs out. At some point I should dig into this one
673 677 # and file a bug report about it.
674 678 inspect_error()
675 679 traceback.print_exc(file=Term.cerr)
676 680 info("\nIPython's exception reporting continues...\n")
677 681 call = tpl_call_fail % func
678 682
679 683 # Initialize a list of names on the current line, which the
680 684 # tokenizer below will populate.
681 685 names = []
682 686
683 687 def tokeneater(token_type, token, start, end, line):
684 688 """Stateful tokeneater which builds dotted names.
685 689
686 690 The list of names it appends to (from the enclosing scope) can
687 691 contain repeated composite names. This is unavoidable, since
688 692 there is no way to disambguate partial dotted structures until
689 693 the full list is known. The caller is responsible for pruning
690 694 the final list of duplicates before using it."""
691 695
692 696 # build composite names
693 697 if token == '.':
694 698 try:
695 699 names[-1] += '.'
696 700 # store state so the next token is added for x.y.z names
697 701 tokeneater.name_cont = True
698 702 return
699 703 except IndexError:
700 704 pass
701 705 if token_type == tokenize.NAME and token not in keyword.kwlist:
702 706 if tokeneater.name_cont:
703 707 # Dotted names
704 708 names[-1] += token
705 709 tokeneater.name_cont = False
706 710 else:
707 711 # Regular new names. We append everything, the caller
708 712 # will be responsible for pruning the list later. It's
709 713 # very tricky to try to prune as we go, b/c composite
710 714 # names can fool us. The pruning at the end is easy
711 715 # to do (or the caller can print a list with repeated
712 716 # names if so desired.
713 717 names.append(token)
714 718 elif token_type == tokenize.NEWLINE:
715 719 raise IndexError
716 720 # we need to store a bit of state in the tokenizer to build
717 721 # dotted names
718 722 tokeneater.name_cont = False
719 723
720 724 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
721 725 line = getline(file, lnum[0])
722 726 lnum[0] += 1
723 727 return line
724 728
725 729 # Build the list of names on this line of code where the exception
726 730 # occurred.
727 731 try:
728 732 # This builds the names list in-place by capturing it from the
729 733 # enclosing scope.
730 734 tokenize.tokenize(linereader, tokeneater)
731 735 except IndexError:
732 736 # signals exit of tokenizer
733 737 pass
734 738 except tokenize.TokenError,msg:
735 739 _m = ("An unexpected error occurred while tokenizing input\n"
736 740 "The following traceback may be corrupted or invalid\n"
737 741 "The error message is: %s\n" % msg)
738 742 error(_m)
739 743
740 744 # prune names list of duplicates, but keep the right order
741 745 unique_names = uniq_stable(names)
742 746
743 747 # Start loop over vars
744 748 lvals = []
745 749 if self.include_vars:
746 750 for name_full in unique_names:
747 751 name_base = name_full.split('.',1)[0]
748 752 if name_base in frame.f_code.co_varnames:
749 753 if locals.has_key(name_base):
750 754 try:
751 755 value = repr(eval(name_full,locals))
752 756 except:
753 757 value = undefined
754 758 else:
755 759 value = undefined
756 760 name = tpl_local_var % name_full
757 761 else:
758 762 if frame.f_globals.has_key(name_base):
759 763 try:
760 764 value = repr(eval(name_full,frame.f_globals))
761 765 except:
762 766 value = undefined
763 767 else:
764 768 value = undefined
765 769 name = tpl_global_var % name_full
766 770 lvals.append(tpl_name_val % (name,value))
767 771 if lvals:
768 772 lvals = '%s%s' % (indent,em_normal.join(lvals))
769 773 else:
770 774 lvals = ''
771 775
772 776 level = '%s %s\n' % (link,call)
773 777
774 778 if index is None:
775 779 frames.append(level)
776 780 else:
777 781 frames.append('%s%s' % (level,''.join(
778 782 _formatTracebackLines(lnum,index,lines,Colors,lvals,
779 783 col_scheme))))
780 784
781 785 # Get (safely) a string form of the exception info
782 786 try:
783 787 etype_str,evalue_str = map(str,(etype,evalue))
784 788 except:
785 789 # User exception is improperly defined.
786 790 etype,evalue = str,sys.exc_info()[:2]
787 791 etype_str,evalue_str = map(str,(etype,evalue))
788 792 # ... and format it
789 793 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
790 794 ColorsNormal, evalue_str)]
791 795 if type(evalue) is types.InstanceType:
792 796 try:
793 797 names = [w for w in dir(evalue) if isinstance(w, basestring)]
794 798 except:
795 799 # Every now and then, an object with funny inernals blows up
796 800 # when dir() is called on it. We do the best we can to report
797 801 # the problem and continue
798 802 _m = '%sException reporting error (object with broken dir())%s:'
799 803 exception.append(_m % (Colors.excName,ColorsNormal))
800 804 etype_str,evalue_str = map(str,sys.exc_info()[:2])
801 805 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
802 806 ColorsNormal, evalue_str))
803 807 names = []
804 808 for name in names:
805 809 value = text_repr(getattr(evalue, name))
806 810 exception.append('\n%s%s = %s' % (indent, name, value))
807 811
808 812 # vds: >>
809 813 if records:
810 814 filepath, lnum = records[-1][1:3]
811 815 #print "file:", str(file), "linenb", str(lnum) # dbg
812 816 filepath = os.path.abspath(filepath)
813 __IPYTHON__.hooks.synchronize_with_editor(filepath, lnum, 0)
817 ipinst = ipapi.get()
818 if ipinst is not None:
819 ipinst.IP.hooks.synchronize_with_editor(filepath, lnum, 0)
814 820 # vds: <<
815 821
816 822 # return all our info assembled as a single string
817 823 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
818 824
819 825 def debugger(self,force=False):
820 826 """Call up the pdb debugger if desired, always clean up the tb
821 827 reference.
822 828
823 829 Keywords:
824 830
825 831 - force(False): by default, this routine checks the instance call_pdb
826 832 flag and does not actually invoke the debugger if the flag is false.
827 833 The 'force' option forces the debugger to activate even if the flag
828 834 is false.
829 835
830 836 If the call_pdb flag is set, the pdb interactive debugger is
831 837 invoked. In all cases, the self.tb reference to the current traceback
832 838 is deleted to prevent lingering references which hamper memory
833 839 management.
834 840
835 841 Note that each call to pdb() does an 'import readline', so if your app
836 842 requires a special setup for the readline completers, you'll have to
837 843 fix that by hand after invoking the exception handler."""
838 844
839 845 if force or self.call_pdb:
840 846 if self.pdb is None:
841 847 self.pdb = debugger.Pdb(
842 848 self.color_scheme_table.active_scheme_name)
843 849 # the system displayhook may have changed, restore the original
844 850 # for pdb
845 851 dhook = sys.displayhook
846 852 sys.displayhook = sys.__displayhook__
847 853 self.pdb.reset()
848 854 # Find the right frame so we don't pop up inside ipython itself
849 855 if hasattr(self,'tb'):
850 856 etb = self.tb
851 857 else:
852 858 etb = self.tb = sys.last_traceback
853 859 while self.tb.tb_next is not None:
854 860 self.tb = self.tb.tb_next
855 861 try:
856 862 if etb and etb.tb_next:
857 863 etb = etb.tb_next
858 864 self.pdb.botframe = etb.tb_frame
859 865 self.pdb.interaction(self.tb.tb_frame, self.tb)
860 866 finally:
861 867 sys.displayhook = dhook
862 868
863 869 if hasattr(self,'tb'):
864 870 del self.tb
865 871
866 872 def handler(self, info=None):
867 873 (etype, evalue, etb) = info or sys.exc_info()
868 874 self.tb = etb
869 875 Term.cout.flush()
870 876 print >> Term.cerr, self.text(etype, evalue, etb)
871 877 Term.cerr.flush()
872 878
873 879 # Changed so an instance can just be called as VerboseTB_inst() and print
874 880 # out the right info on its own.
875 881 def __call__(self, etype=None, evalue=None, etb=None):
876 882 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
877 883 if etb is None:
878 884 self.handler()
879 885 else:
880 886 self.handler((etype, evalue, etb))
881 887 try:
882 888 self.debugger()
883 889 except KeyboardInterrupt:
884 890 print "\nKeyboardInterrupt"
885 891
886 892 #----------------------------------------------------------------------------
887 893 class FormattedTB(VerboseTB,ListTB):
888 894 """Subclass ListTB but allow calling with a traceback.
889 895
890 896 It can thus be used as a sys.excepthook for Python > 2.1.
891 897
892 898 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
893 899
894 900 Allows a tb_offset to be specified. This is useful for situations where
895 901 one needs to remove a number of topmost frames from the traceback (such as
896 902 occurs with python programs that themselves execute other python code,
897 903 like Python shells). """
898 904
899 905 def __init__(self, mode = 'Plain', color_scheme='Linux',
900 906 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
901 907
902 908 # NEVER change the order of this list. Put new modes at the end:
903 909 self.valid_modes = ['Plain','Context','Verbose']
904 910 self.verbose_modes = self.valid_modes[1:3]
905 911
906 912 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
907 913 call_pdb=call_pdb,include_vars=include_vars)
908 914 self.set_mode(mode)
909 915
910 916 def _extract_tb(self,tb):
911 917 if tb:
912 918 return traceback.extract_tb(tb)
913 919 else:
914 920 return None
915 921
916 922 def text(self, etype, value, tb,context=5,mode=None):
917 923 """Return formatted traceback.
918 924
919 925 If the optional mode parameter is given, it overrides the current
920 926 mode."""
921 927
922 928 if mode is None:
923 929 mode = self.mode
924 930 if mode in self.verbose_modes:
925 931 # verbose modes need a full traceback
926 932 return VerboseTB.text(self,etype, value, tb,context=5)
927 933 else:
928 934 # We must check the source cache because otherwise we can print
929 935 # out-of-date source code.
930 936 linecache.checkcache()
931 937 # Now we can extract and format the exception
932 938 elist = self._extract_tb(tb)
933 939 if len(elist) > self.tb_offset:
934 940 del elist[:self.tb_offset]
935 941 return ListTB.text(self,etype,value,elist)
936 942
937 943 def set_mode(self,mode=None):
938 944 """Switch to the desired mode.
939 945
940 946 If mode is not specified, cycles through the available modes."""
941 947
942 948 if not mode:
943 949 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
944 950 len(self.valid_modes)
945 951 self.mode = self.valid_modes[new_idx]
946 952 elif mode not in self.valid_modes:
947 953 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
948 954 'Valid modes: '+str(self.valid_modes)
949 955 else:
950 956 self.mode = mode
951 957 # include variable details only in 'Verbose' mode
952 958 self.include_vars = (self.mode == self.valid_modes[2])
953 959
954 960 # some convenient shorcuts
955 961 def plain(self):
956 962 self.set_mode(self.valid_modes[0])
957 963
958 964 def context(self):
959 965 self.set_mode(self.valid_modes[1])
960 966
961 967 def verbose(self):
962 968 self.set_mode(self.valid_modes[2])
963 969
964 970 #----------------------------------------------------------------------------
965 971 class AutoFormattedTB(FormattedTB):
966 972 """A traceback printer which can be called on the fly.
967 973
968 974 It will find out about exceptions by itself.
969 975
970 976 A brief example:
971 977
972 978 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
973 979 try:
974 980 ...
975 981 except:
976 982 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
977 983 """
978 984 def __call__(self,etype=None,evalue=None,etb=None,
979 985 out=None,tb_offset=None):
980 986 """Print out a formatted exception traceback.
981 987
982 988 Optional arguments:
983 989 - out: an open file-like object to direct output to.
984 990
985 991 - tb_offset: the number of frames to skip over in the stack, on a
986 992 per-call basis (this overrides temporarily the instance's tb_offset
987 993 given at initialization time. """
988 994
989 995 if out is None:
990 996 out = Term.cerr
991 997 Term.cout.flush()
992 998 if tb_offset is not None:
993 999 tb_offset, self.tb_offset = self.tb_offset, tb_offset
994 1000 print >> out, self.text(etype, evalue, etb)
995 1001 self.tb_offset = tb_offset
996 1002 else:
997 1003 print >> out, self.text(etype, evalue, etb)
998 1004 out.flush()
999 1005 try:
1000 1006 self.debugger()
1001 1007 except KeyboardInterrupt:
1002 1008 print "\nKeyboardInterrupt"
1003 1009
1004 1010 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
1005 1011 if etype is None:
1006 1012 etype,value,tb = sys.exc_info()
1007 1013 self.tb = tb
1008 1014 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
1009 1015
1010 1016 #---------------------------------------------------------------------------
1011 1017 # A simple class to preserve Nathan's original functionality.
1012 1018 class ColorTB(FormattedTB):
1013 1019 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1014 1020 def __init__(self,color_scheme='Linux',call_pdb=0):
1015 1021 FormattedTB.__init__(self,color_scheme=color_scheme,
1016 1022 call_pdb=call_pdb)
1017 1023
1018 1024 #----------------------------------------------------------------------------
1019 1025 # module testing (minimal)
1020 1026 if __name__ == "__main__":
1021 1027 def spam(c, (d, e)):
1022 1028 x = c + d
1023 1029 y = c * d
1024 1030 foo(x, y)
1025 1031
1026 1032 def foo(a, b, bar=1):
1027 1033 eggs(a, b + bar)
1028 1034
1029 1035 def eggs(f, g, z=globals()):
1030 1036 h = f + g
1031 1037 i = f - g
1032 1038 return h / i
1033 1039
1034 1040 print ''
1035 1041 print '*** Before ***'
1036 1042 try:
1037 1043 print spam(1, (2, 3))
1038 1044 except:
1039 1045 traceback.print_exc()
1040 1046 print ''
1041 1047
1042 1048 handler = ColorTB()
1043 1049 print '*** ColorTB ***'
1044 1050 try:
1045 1051 print spam(1, (2, 3))
1046 1052 except:
1047 1053 apply(handler, sys.exc_info() )
1048 1054 print ''
1049 1055
1050 1056 handler = VerboseTB()
1051 1057 print '*** VerboseTB ***'
1052 1058 try:
1053 1059 print spam(1, (2, 3))
1054 1060 except:
1055 1061 apply(handler, sys.exc_info() )
1056 1062 print ''
1057 1063
@@ -1,252 +1,266 b''
1 1 # encoding: utf-8
2 2 """
3 3 Test process execution and IO redirection.
4 4 """
5 5
6 6 __docformat__ = "restructuredtext en"
7 7
8 8 #-------------------------------------------------------------------------------
9 9 # Copyright (C) 2008 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is
12 12 # in the file COPYING, distributed as part of this software.
13 13 #-------------------------------------------------------------------------------
14 14
15 15 from copy import copy, deepcopy
16 16 from cStringIO import StringIO
17 17 import string
18 import sys
18 19
19 20 from nose.tools import assert_equal
20 21
21 22 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
22 23 from IPython.core.ipapi import get as get_ipython0
23 24 from IPython.testing.plugin.ipdoctest import default_argv
24 25
25 26
26 def safe_deepcopy(d):
27 """ Deep copy every key of the given dict, when possible. Elsewhere
28 do a copy.
29 """
30 copied_d = dict()
31 for key, value in d.iteritems():
32 try:
33 copied_d[key] = deepcopy(value)
34 except:
35 try:
36 copied_d[key] = copy(value)
37 except:
38 copied_d[key] = value
39 return copied_d
40
41
42 27 class TestPrefilterFrontEnd(PrefilterFrontEnd):
43 28
44 29 input_prompt_template = string.Template('')
45 30 output_prompt_template = string.Template('')
46 31 banner = ''
47 32
48 33 def __init__(self):
49 34 self.out = StringIO()
50 35 PrefilterFrontEnd.__init__(self,argv=default_argv())
51 36 # Some more code for isolation (yeah, crazy)
52 37 self._on_enter()
53 38 self.out.flush()
54 39 self.out.reset()
55 40 self.out.truncate()
56 41
57 42 def write(self, string, *args, **kwargs):
58 43 self.out.write(string)
59 44
60 45 def _on_enter(self):
61 46 self.input_buffer += '\n'
62 47 PrefilterFrontEnd._on_enter(self)
63 48
64 49
65 50 def isolate_ipython0(func):
66 51 """ Decorator to isolate execution that involves an iptyhon0.
67 52
68 53 Notes
69 54 -----
70 55
71 56 Apply only to functions with no arguments. Nose skips functions
72 57 with arguments.
73 58 """
74 59 def my_func():
75 iplib = get_ipython0()
76 if iplib is None:
60 ip0 = get_ipython0()
61 if ip0 is None:
77 62 return func()
78 ipython0 = iplib.IP
79 global_ns = safe_deepcopy(ipython0.user_global_ns)
80 user_ns = safe_deepcopy(ipython0.user_ns)
63 # We have a real ipython running...
64 user_ns = ip0.IP.user_ns
65 user_global_ns = ip0.IP.user_global_ns
66
67 # Previously the isolation was attempted with a deep copy of the user
68 # dicts, but we found cases where this didn't work correctly. I'm not
69 # quite sure why, but basically it did damage the user namespace, such
70 # that later tests stopped working correctly. Instead we use a simpler
71 # approach, just computing the list of added keys to the namespace and
72 # eliminating those afterwards. Existing keys that may have been
73 # modified remain modified. So far this has proven to be robust.
74
75 # Compute set of old local/global keys
76 old_locals = set(user_ns.keys())
77 old_globals = set(user_global_ns.keys())
81 78 try:
82 79 out = func()
83 80 finally:
84 ipython0.user_ns = user_ns
85 ipython0.user_global_ns = global_ns
81 # Find new keys, and if any, remove them
82 new_locals = set(user_ns.keys()) - old_locals
83 new_globals = set(user_global_ns.keys()) - old_globals
84 for k in new_locals:
85 del user_ns[k]
86 for k in new_globals:
87 del user_global_ns[k]
86 88 # Undo the hack at creation of PrefilterFrontEnd
87 89 from IPython.core import iplib
88 90 iplib.InteractiveShell.isthreaded = False
89 91 return out
90 92
91 93 my_func.__name__ = func.__name__
92 94 return my_func
93 95
94 96
95 97 @isolate_ipython0
96 98 def test_execution():
97 99 """ Test execution of a command.
98 100 """
99 101 f = TestPrefilterFrontEnd()
100 f.input_buffer = 'print 1'
102 f.input_buffer = 'print(1)'
101 103 f._on_enter()
102 104 out_value = f.out.getvalue()
103 105 assert_equal(out_value, '1\n')
104 106
105 107
106 108 @isolate_ipython0
107 109 def test_multiline():
108 110 """ Test execution of a multiline command.
109 111 """
110 112 f = TestPrefilterFrontEnd()
111 113 f.input_buffer = 'if True:'
112 114 f._on_enter()
113 115 f.input_buffer += 'print 1'
114 116 f._on_enter()
115 117 out_value = f.out.getvalue()
116 118 yield assert_equal, out_value, ''
117 119 f._on_enter()
118 120 out_value = f.out.getvalue()
119 121 yield assert_equal, out_value, '1\n'
120 122 f = TestPrefilterFrontEnd()
121 123 f.input_buffer='(1 +'
122 124 f._on_enter()
123 125 f.input_buffer += '0)'
124 126 f._on_enter()
125 127 out_value = f.out.getvalue()
126 128 yield assert_equal, out_value, ''
127 129 f._on_enter()
128 130 out_value = f.out.getvalue()
129 131 yield assert_equal, out_value, '1\n'
130 132
131 133
132 134 @isolate_ipython0
133 135 def test_capture():
134 136 """ Test the capture of output in different channels.
135 137 """
136 138 # Test on the OS-level stdout, stderr.
137 139 f = TestPrefilterFrontEnd()
138 140 f.input_buffer = \
139 141 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()'
140 142 f._on_enter()
141 143 out_value = f.out.getvalue()
142 144 yield assert_equal, out_value, '1'
143 145 f = TestPrefilterFrontEnd()
144 146 f.input_buffer = \
145 147 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()'
146 148 f._on_enter()
147 149 out_value = f.out.getvalue()
148 150 yield assert_equal, out_value, '1'
149 151
150 152
151 153 @isolate_ipython0
152 154 def test_magic():
153 155 """ Test the magic expansion and history.
154 156
155 157 This test is fairly fragile and will break when magics change.
156 158 """
157 159 f = TestPrefilterFrontEnd()
158 160 # Before checking the interactive namespace, make sure it's clear (it can
159 161 # otherwise pick up things stored in the user's local db)
160 162 f.input_buffer += '%reset -f'
161 163 f._on_enter()
162 164 f.complete_current_input()
163 165 # Now, run the %who magic and check output
164 166 f.input_buffer += '%who'
165 167 f._on_enter()
166 168 out_value = f.out.getvalue()
167 169 assert_equal(out_value, 'Interactive namespace is empty.\n')
168 170
169 171
170 172 @isolate_ipython0
171 173 def test_help():
172 174 """ Test object inspection.
173 175 """
174 176 f = TestPrefilterFrontEnd()
175 177 f.input_buffer += "def f():"
176 178 f._on_enter()
177 179 f.input_buffer += "'foobar'"
178 180 f._on_enter()
179 181 f.input_buffer += "pass"
180 182 f._on_enter()
181 183 f._on_enter()
182 184 f.input_buffer += "f?"
183 185 f._on_enter()
184 186 assert 'traceback' not in f.last_result
185 187 ## XXX: ipython doctest magic breaks this. I have no clue why
186 188 #out_value = f.out.getvalue()
187 189 #assert out_value.split()[-1] == 'foobar'
188 190
189 191
190 192 @isolate_ipython0
191 193 def test_completion_simple():
192 194 """ Test command-line completion on trivial examples.
193 195 """
194 196 f = TestPrefilterFrontEnd()
195 197 f.input_buffer = 'zzza = 1'
196 198 f._on_enter()
197 199 f.input_buffer = 'zzzb = 2'
198 200 f._on_enter()
199 201 f.input_buffer = 'zz'
200 202 f.complete_current_input()
201 203 out_value = f.out.getvalue()
202 204 yield assert_equal, out_value, '\nzzza zzzb '
203 205 yield assert_equal, f.input_buffer, 'zzz'
204 206
205 207
206 208 @isolate_ipython0
207 209 def test_completion_parenthesis():
208 210 """ Test command-line completion when a parenthesis is open.
209 211 """
210 212 f = TestPrefilterFrontEnd()
211 213 f.input_buffer = 'zzza = 1'
212 214 f._on_enter()
213 215 f.input_buffer = 'zzzb = 2'
214 216 f._on_enter()
215 217 f.input_buffer = 'map(zz'
216 218 f.complete_current_input()
217 219 out_value = f.out.getvalue()
218 220 yield assert_equal, out_value, '\nzzza zzzb '
219 221 yield assert_equal, f.input_buffer, 'map(zzz'
220 222
221 223
222 224 @isolate_ipython0
223 225 def test_completion_indexing():
224 226 """ Test command-line completion when indexing on objects.
225 227 """
226 228 f = TestPrefilterFrontEnd()
227 229 f.input_buffer = 'a = [0]'
228 230 f._on_enter()
229 231 f.input_buffer = 'a[0].'
230 232 f.complete_current_input()
231 assert_equal(f.input_buffer, 'a[0].__')
233
234 if sys.version_info[:2] >= (2,6):
235 # In Python 2.6, ints picked up a few non __ methods, so now there are
236 # no completions.
237 assert_equal(f.input_buffer, 'a[0].')
238 else:
239 # Right answer for 2.4/2.5
240 assert_equal(f.input_buffer, 'a[0].__')
232 241
233 242
234 243 @isolate_ipython0
235 244 def test_completion_equal():
236 245 """ Test command-line completion when the delimiter is "=", not " ".
237 246 """
238 247 f = TestPrefilterFrontEnd()
239 248 f.input_buffer = 'a=1.'
240 249 f.complete_current_input()
241 assert_equal(f.input_buffer, 'a=1.__')
242
250 if sys.version_info[:2] >= (2,6):
251 # In Python 2.6, ints picked up a few non __ methods, so now there are
252 # no completions.
253 assert_equal(f.input_buffer, 'a=1.')
254 else:
255 # Right answer for 2.4/2.5
256 assert_equal(f.input_buffer, 'a=1.__')
243 257
244 258
245 259 if __name__ == '__main__':
246 260 test_magic()
247 261 test_help()
248 262 test_execution()
249 263 test_multiline()
250 264 test_capture()
251 265 test_completion_simple()
252 266 test_completion_complex()
@@ -1,141 +1,141 b''
1 1 # encoding: utf-8
2 2 # -*- test-case-name: IPython.kernel.test.test_contexts -*-
3 3 """Context managers for IPython.
4 4
5 5 Python 2.5 introduced the `with` statement, which is based on the context
6 6 manager protocol. This module offers a few context managers for common cases,
7 7 which can also be useful as templates for writing new, application-specific
8 8 managers.
9 9 """
10 10
11 11 __docformat__ = "restructuredtext en"
12 12
13 13 #-------------------------------------------------------------------------------
14 14 # Copyright (C) 2008 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-------------------------------------------------------------------------------
19 19
20 20 #-------------------------------------------------------------------------------
21 21 # Imports
22 22 #-------------------------------------------------------------------------------
23 23
24 24 import linecache
25 25 import sys
26 26
27 27 from twisted.internet.error import ConnectionRefusedError
28 28
29 from IPython.ultraTB import _fixed_getinnerframes, findsource
29 from IPython.core.ultratb import _fixed_getinnerframes, findsource
30 30 from IPython.core import ipapi
31 31
32 32 from IPython.kernel import error
33 33
34 34 #---------------------------------------------------------------------------
35 35 # Utility functions needed by all context managers.
36 36 #---------------------------------------------------------------------------
37 37
38 38 def remote():
39 39 """Raises a special exception meant to be caught by context managers.
40 40 """
41 41 m = 'Special exception to stop local execution of parallel code.'
42 42 raise error.StopLocalExecution(m)
43 43
44 44
45 45 def strip_whitespace(source,require_remote=True):
46 46 """strip leading whitespace from input source.
47 47
48 48 :Parameters:
49 49
50 50 """
51 51 remote_mark = 'remote()'
52 52 # Expand tabs to avoid any confusion.
53 53 wsource = [l.expandtabs(4) for l in source]
54 54 # Detect the indentation level
55 55 done = False
56 56 for line in wsource:
57 57 if line.isspace():
58 58 continue
59 59 for col,char in enumerate(line):
60 60 if char != ' ':
61 61 done = True
62 62 break
63 63 if done:
64 64 break
65 65 # Now we know how much leading space there is in the code. Next, we
66 66 # extract up to the first line that has less indentation.
67 67 # WARNINGS: we skip comments that may be misindented, but we do NOT yet
68 68 # detect triple quoted strings that may have flush left text.
69 69 for lno,line in enumerate(wsource):
70 70 lead = line[:col]
71 71 if lead.isspace():
72 72 continue
73 73 else:
74 74 if not lead.lstrip().startswith('#'):
75 75 break
76 76 # The real 'with' source is up to lno
77 77 src_lines = [l[col:] for l in wsource[:lno+1]]
78 78
79 79 # Finally, check that the source's first non-comment line begins with the
80 80 # special call 'remote()'
81 81 if require_remote:
82 82 for nline,line in enumerate(src_lines):
83 83 if line.isspace() or line.startswith('#'):
84 84 continue
85 85 if line.startswith(remote_mark):
86 86 break
87 87 else:
88 88 raise ValueError('%s call missing at the start of code' %
89 89 remote_mark)
90 90 out_lines = src_lines[nline+1:]
91 91 else:
92 92 # If the user specified that the remote() call wasn't mandatory
93 93 out_lines = src_lines
94 94
95 95 # src = ''.join(out_lines) # dbg
96 96 #print 'SRC:\n<<<<<<<>>>>>>>\n%s<<<<<>>>>>>' % src # dbg
97 97 return ''.join(out_lines)
98 98
99 99 class RemoteContextBase(object):
100 100 def __init__(self):
101 101 self.ip = ipapi.get()
102 102
103 103 def _findsource_file(self,f):
104 104 linecache.checkcache()
105 105 s = findsource(f.f_code)
106 106 lnum = f.f_lineno
107 107 wsource = s[0][f.f_lineno:]
108 108 return strip_whitespace(wsource)
109 109
110 110 def _findsource_ipython(self,f):
111 111 from IPython.core import ipapi
112 112 self.ip = ipapi.get()
113 113 buf = self.ip.IP.input_hist_raw[-1].splitlines()[1:]
114 114 wsource = [l+'\n' for l in buf ]
115 115
116 116 return strip_whitespace(wsource)
117 117
118 118 def findsource(self,frame):
119 119 local_ns = frame.f_locals
120 120 global_ns = frame.f_globals
121 121 if frame.f_code.co_filename == '<ipython console>':
122 122 src = self._findsource_ipython(frame)
123 123 else:
124 124 src = self._findsource_file(frame)
125 125 return src
126 126
127 127 def __enter__(self):
128 128 raise NotImplementedError
129 129
130 130 def __exit__ (self, etype, value, tb):
131 131 if issubclass(etype,error.StopLocalExecution):
132 132 return True
133 133
134 134 class RemoteMultiEngine(RemoteContextBase):
135 135 def __init__(self,mec):
136 136 self.mec = mec
137 137 RemoteContextBase.__init__(self)
138 138
139 139 def __enter__(self):
140 140 src = self.findsource(sys._getframe(1))
141 141 return self.mec.execute(src)
@@ -1,761 +1,761 b''
1 1 # encoding: utf-8
2 2
3 3 """Central interpreter object for an IPython engine.
4 4
5 5 The interpreter is the object whose job is to process lines of user input and
6 6 actually execute them in the user's namespace.
7 7 """
8 8
9 9 __docformat__ = "restructuredtext en"
10 10
11 11 #-------------------------------------------------------------------------------
12 12 # Copyright (C) 2008 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-------------------------------------------------------------------------------
17 17
18 18 #-------------------------------------------------------------------------------
19 19 # Imports
20 20 #-------------------------------------------------------------------------------
21 21
22 22 # Standard library imports.
23 23 from types import FunctionType
24 24
25 25 import __builtin__
26 26 import codeop
27 27 import compiler
28 28 import sys
29 29 import traceback
30 30
31 31 # Local imports.
32 from IPython.kernel.core import ultraTB
32 from IPython.core import ultratb
33 33 from IPython.kernel.core.display_trap import DisplayTrap
34 34 from IPython.kernel.core.macro import Macro
35 35 from IPython.kernel.core.prompts import CachedOutput
36 36 from IPython.kernel.core.traceback_trap import TracebackTrap
37 37 from IPython.kernel.core.util import Bunch, system_shell
38 38 from IPython.external.Itpl import ItplNS
39 39
40 40 # Global constants
41 41 COMPILER_ERROR = 'error'
42 42 INCOMPLETE_INPUT = 'incomplete'
43 43 COMPLETE_INPUT = 'complete'
44 44
45 45 ##############################################################################
46 46 # TEMPORARY!!! fake configuration, while we decide whether to use tconfig or
47 47 # not
48 48
49 49 rc = Bunch()
50 50 rc.cache_size = 100
51 51 rc.pprint = True
52 52 rc.separate_in = '\n'
53 53 rc.separate_out = '\n'
54 54 rc.separate_out2 = ''
55 55 rc.prompt_in1 = r'In [\#]: '
56 56 rc.prompt_in2 = r' .\\D.: '
57 57 rc.prompt_out = ''
58 58 rc.prompts_pad_left = False
59 59
60 60 ##############################################################################
61 61
62 62 # Top-level utilities
63 63 def default_display_formatters():
64 64 """ Return a list of default display formatters.
65 65 """
66 66
67 67 from display_formatter import PPrintDisplayFormatter, ReprDisplayFormatter
68 68 return [PPrintDisplayFormatter(), ReprDisplayFormatter()]
69 69
70 70 def default_traceback_formatters():
71 71 """ Return a list of default traceback formatters.
72 72 """
73 73
74 74 from traceback_formatter import PlainTracebackFormatter
75 75 return [PlainTracebackFormatter()]
76 76
77 77 # Top-level classes
78 78 class NotDefined(object): pass
79 79
80 80 class Interpreter(object):
81 81 """ An interpreter object.
82 82
83 83 fixme: needs to negotiate available formatters with frontends.
84 84
85 85 Important: the interpeter should be built so that it exposes a method
86 86 for each attribute/method of its sub-object. This way it can be
87 87 replaced by a network adapter.
88 88 """
89 89
90 90 def __init__(self, user_ns=None, global_ns=None,translator=None,
91 91 magic=None, display_formatters=None,
92 92 traceback_formatters=None, output_trap=None, history=None,
93 93 message_cache=None, filename='<string>', config=None):
94 94
95 95 # The local/global namespaces for code execution
96 96 local_ns = user_ns # compatibility name
97 97 if local_ns is None:
98 98 local_ns = {}
99 99 self.user_ns = local_ns
100 100 # The local namespace
101 101 if global_ns is None:
102 102 global_ns = {}
103 103 self.user_global_ns = global_ns
104 104
105 105 # An object that will translate commands into executable Python.
106 106 # The current translator does not work properly so for now we are going
107 107 # without!
108 108 # if translator is None:
109 109 # from IPython.kernel.core.translator import IPythonTranslator
110 110 # translator = IPythonTranslator()
111 111 self.translator = translator
112 112
113 113 # An object that maintains magic commands.
114 114 if magic is None:
115 115 from IPython.kernel.core.magic import Magic
116 116 magic = Magic(self)
117 117 self.magic = magic
118 118
119 119 # A list of formatters for the displayhook.
120 120 if display_formatters is None:
121 121 display_formatters = default_display_formatters()
122 122 self.display_formatters = display_formatters
123 123
124 124 # A list of formatters for tracebacks.
125 125 if traceback_formatters is None:
126 126 traceback_formatters = default_traceback_formatters()
127 127 self.traceback_formatters = traceback_formatters
128 128
129 129 # The object trapping stdout/stderr.
130 130 if output_trap is None:
131 131 from IPython.kernel.core.output_trap import OutputTrap
132 132 output_trap = OutputTrap()
133 133 self.output_trap = output_trap
134 134
135 135 # An object that manages the history.
136 136 if history is None:
137 137 from IPython.kernel.core.history import InterpreterHistory
138 138 history = InterpreterHistory()
139 139 self.history = history
140 140 self.get_history_item = history.get_history_item
141 141 self.get_history_input_cache = history.get_input_cache
142 142 self.get_history_input_after = history.get_input_after
143 143
144 144 # An object that caches all of the return messages.
145 145 if message_cache is None:
146 146 from IPython.kernel.core.message_cache import SimpleMessageCache
147 147 message_cache = SimpleMessageCache()
148 148 self.message_cache = message_cache
149 149
150 150 # The "filename" of the code that is executed in this interpreter.
151 151 self.filename = filename
152 152
153 153 # An object that contains much configuration information.
154 154 if config is None:
155 155 # fixme: Move this constant elsewhere!
156 156 config = Bunch(ESC_MAGIC='%')
157 157 self.config = config
158 158
159 159 # Hook managers.
160 160 # fixme: make the display callbacks configurable. In the meantime,
161 161 # enable macros.
162 162 self.display_trap = DisplayTrap(
163 163 formatters=self.display_formatters,
164 164 callbacks=[self._possible_macro],
165 165 )
166 166 self.traceback_trap = TracebackTrap(
167 167 formatters=self.traceback_formatters)
168 168
169 169 # This is used temporarily for reformating exceptions in certain
170 # cases. It will go away once the ultraTB stuff is ported
170 # cases. It will go away once the ultratb stuff is ported
171 171 # to ipython1
172 self.tbHandler = ultraTB.FormattedTB(color_scheme='NoColor',
172 self.tbHandler = ultratb.FormattedTB(color_scheme='NoColor',
173 173 mode='Context',
174 174 tb_offset=2)
175 175
176 176 # An object that can compile commands and remember __future__
177 177 # statements.
178 178 self.command_compiler = codeop.CommandCompiler()
179 179
180 180 # A replacement for the raw_input() and input() builtins. Change these
181 181 # attributes later to configure them.
182 182 self.raw_input_builtin = raw_input
183 183 self.input_builtin = input
184 184
185 185 # The number of the current cell.
186 186 self.current_cell_number = 1
187 187
188 188 # Initialize cache, set in/out prompts and printing system
189 189 self.outputcache = CachedOutput(self,
190 190 rc.cache_size,
191 191 rc.pprint,
192 192 input_sep = rc.separate_in,
193 193 output_sep = rc.separate_out,
194 194 output_sep2 = rc.separate_out2,
195 195 ps1 = rc.prompt_in1,
196 196 ps2 = rc.prompt_in2,
197 197 ps_out = rc.prompt_out,
198 198 pad_left = rc.prompts_pad_left)
199 199
200 200 # Need to decide later if this is the right approach, but clients
201 201 # commonly use sys.ps1/2, so it may be best to just set them here
202 202 sys.ps1 = self.outputcache.prompt1.p_str
203 203 sys.ps2 = self.outputcache.prompt2.p_str
204 204
205 205 # This is the message dictionary assigned temporarily when running the
206 206 # code.
207 207 self.message = None
208 208
209 209 self.setup_namespace()
210 210
211 211
212 212 #### Public 'Interpreter' interface ########################################
213 213
214 214 def formatTraceback(self, et, ev, tb, message=''):
215 215 """Put a formatted version of the traceback into value and reraise.
216 216
217 217 When exceptions have to be sent over the network, the traceback
218 218 needs to be put into the value of the exception in a nicely
219 219 formatted way. The method takes the type, value and tb of an
220 220 exception and puts a string representation of the tb into the
221 221 value of the exception and reraises it.
222 222
223 223 Currently this method uses the ultraTb formatter from IPython trunk.
224 224 Eventually it should simply use the traceback formatters in core
225 225 that are loaded into self.tracback_trap.formatters.
226 226 """
227 227 tbinfo = self.tbHandler.text(et,ev,tb)
228 228 ev._ipython_traceback_text = tbinfo
229 229 return et, ev, tb
230 230
231 231 def execute(self, commands, raiseException=True):
232 232 """ Execute some IPython commands.
233 233
234 234 1. Translate them into Python.
235 235 2. Run them.
236 236 3. Trap stdout/stderr.
237 237 4. Trap sys.displayhook().
238 238 5. Trap exceptions.
239 239 6. Return a message object.
240 240
241 241 Parameters
242 242 ----------
243 243 commands : str
244 244 The raw commands that the user typed into the prompt.
245 245
246 246 Returns
247 247 -------
248 248 message : dict
249 249 The dictionary of responses. See the README.txt in this directory
250 250 for an explanation of the format.
251 251 """
252 252
253 253 # Create a message dictionary with all of the information we will be
254 254 # returning to the frontend and other listeners.
255 255 message = self.setup_message()
256 256
257 257 # Massage the input and store the raw and translated commands into
258 258 # a dict.
259 259 user_input = dict(raw=commands)
260 260 if self.translator is not None:
261 261 python = self.translator(commands, message)
262 262 if python is None:
263 263 # Something went wrong with the translation. The translator
264 264 # should have added an appropriate entry to the message object.
265 265 return message
266 266 else:
267 267 python = commands
268 268 user_input['translated'] = python
269 269 message['input'] = user_input
270 270
271 271 # Set the message object so that any magics executed in the code have
272 272 # access.
273 273 self.message = message
274 274
275 275 # Set all of the output/exception traps.
276 276 self.set_traps()
277 277
278 278 # Actually execute the Python code.
279 279 status = self.execute_python(python)
280 280
281 281 # Unset all of the traps.
282 282 self.unset_traps()
283 283
284 284 # Unset the message object.
285 285 self.message = None
286 286
287 287 # Update the history variables in the namespace.
288 288 # E.g. In, Out, _, __, ___
289 289 if self.history is not None:
290 290 self.history.update_history(self, python)
291 291
292 292 # Let all of the traps contribute to the message and then clear their
293 293 # stored information.
294 294 self.output_trap.add_to_message(message)
295 295 self.output_trap.clear()
296 296 self.display_trap.add_to_message(message)
297 297 self.display_trap.clear()
298 298 self.traceback_trap.add_to_message(message)
299 299 # Pull out the type, value and tb of the current exception
300 300 # before clearing it.
301 301 einfo = self.traceback_trap.args
302 302 self.traceback_trap.clear()
303 303
304 304 # Cache the message.
305 305 self.message_cache.add_message(self.current_cell_number, message)
306 306
307 307 # Bump the number.
308 308 self.current_cell_number += 1
309 309
310 310 # This conditional lets the execute method either raise any
311 311 # exception that has occured in user code OR return the message
312 312 # dict containing the traceback and other useful info.
313 313 if raiseException and einfo:
314 314 raise einfo[0],einfo[1],einfo[2]
315 315 else:
316 316 return message
317 317
318 318 def generate_prompt(self, is_continuation):
319 319 """Calculate and return a string with the prompt to display.
320 320
321 321 :Parameters:
322 322 is_continuation : bool
323 323 Whether the input line is continuing multiline input or not, so
324 324 that a proper continuation prompt can be computed."""
325 325
326 326 if is_continuation:
327 327 return str(self.outputcache.prompt2)
328 328 else:
329 329 return str(self.outputcache.prompt1)
330 330
331 331 def execute_python(self, python):
332 332 """ Actually run the Python code in the namespace.
333 333
334 334 :Parameters:
335 335
336 336 python : str
337 337 Pure, exec'able Python code. Special IPython commands should have
338 338 already been translated into pure Python.
339 339 """
340 340
341 341 # We use a CommandCompiler instance to compile the code so as to keep
342 342 # track of __future__ imports.
343 343 try:
344 344 commands = self.split_commands(python)
345 345 except (SyntaxError, IndentationError), e:
346 346 # Save the exc_info so compilation related exceptions can be
347 347 # reraised
348 348 self.traceback_trap.args = sys.exc_info()
349 349 self.pack_exception(self.message,e)
350 350 return None
351 351
352 352 for cmd in commands:
353 353 try:
354 354 code = self.command_compiler(cmd, self.filename, 'single')
355 355 except (SyntaxError, OverflowError, ValueError), e:
356 356 self.traceback_trap.args = sys.exc_info()
357 357 self.pack_exception(self.message,e)
358 358 # No point in continuing if one block raised
359 359 return None
360 360 else:
361 361 self.execute_block(code)
362 362
363 363 def execute_block(self,code):
364 364 """Execute a single block of code in the user namespace.
365 365
366 366 Return value: a flag indicating whether the code to be run completed
367 367 successfully:
368 368
369 369 - 0: successful execution.
370 370 - 1: an error occurred.
371 371 """
372 372
373 373 outflag = 1 # start by assuming error, success will reset it
374 374 try:
375 375 exec code in self.user_ns
376 376 outflag = 0
377 377 except SystemExit:
378 378 self.resetbuffer()
379 379 self.traceback_trap.args = sys.exc_info()
380 380 except:
381 381 self.traceback_trap.args = sys.exc_info()
382 382
383 383 return outflag
384 384
385 385 def execute_macro(self, macro):
386 386 """ Execute the value of a macro.
387 387
388 388 Parameters
389 389 ----------
390 390 macro : Macro
391 391 """
392 392
393 393 python = macro.value
394 394 if self.translator is not None:
395 395 python = self.translator(python)
396 396 self.execute_python(python)
397 397
398 398 def getCommand(self, i=None):
399 399 """Gets the ith message in the message_cache.
400 400
401 401 This is implemented here for compatibility with the old ipython1 shell
402 402 I am not sure we need this though. I even seem to remember that we
403 403 were going to get rid of it.
404 404 """
405 405 return self.message_cache.get_message(i)
406 406
407 407 def reset(self):
408 408 """Reset the interpreter.
409 409
410 410 Currently this only resets the users variables in the namespace.
411 411 In the future we might want to also reset the other stateful
412 412 things like that the Interpreter has, like In, Out, etc.
413 413 """
414 414 self.user_ns.clear()
415 415 self.setup_namespace()
416 416
417 417 def complete(self,line,text=None, pos=None):
418 418 """Complete the given text.
419 419
420 420 :Parameters:
421 421
422 422 text : str
423 423 Text fragment to be completed on. Typically this is
424 424 """
425 425 # fixme: implement
426 426 raise NotImplementedError
427 427
428 428 def push(self, ns):
429 429 """ Put value into the namespace with name key.
430 430
431 431 Parameters
432 432 ----------
433 433 **kwds
434 434 """
435 435
436 436 self.user_ns.update(ns)
437 437
438 438 def push_function(self, ns):
439 439 # First set the func_globals for all functions to self.user_ns
440 440 new_kwds = {}
441 441 for k, v in ns.iteritems():
442 442 if not isinstance(v, FunctionType):
443 443 raise TypeError("function object expected")
444 444 new_kwds[k] = FunctionType(v.func_code, self.user_ns)
445 445 self.user_ns.update(new_kwds)
446 446
447 447 def pack_exception(self,message,exc):
448 448 message['exception'] = exc.__class__
449 449 message['exception_value'] = \
450 450 traceback.format_exception_only(exc.__class__, exc)
451 451
452 452 def feed_block(self, source, filename='<input>', symbol='single'):
453 453 """Compile some source in the interpreter.
454 454
455 455 One several things can happen:
456 456
457 457 1) The input is incorrect; compile_command() raised an
458 458 exception (SyntaxError or OverflowError).
459 459
460 460 2) The input is incomplete, and more input is required;
461 461 compile_command() returned None. Nothing happens.
462 462
463 463 3) The input is complete; compile_command() returned a code
464 464 object. The code is executed by calling self.runcode() (which
465 465 also handles run-time exceptions, except for SystemExit).
466 466
467 467 The return value is:
468 468
469 469 - True in case 2
470 470
471 471 - False in the other cases, unless an exception is raised, where
472 472 None is returned instead. This can be used by external callers to
473 473 know whether to continue feeding input or not.
474 474
475 475 The return value can be used to decide whether to use sys.ps1 or
476 476 sys.ps2 to prompt the next line."""
477 477
478 478 self.message = self.setup_message()
479 479
480 480 try:
481 481 code = self.command_compiler(source,filename,symbol)
482 482 except (OverflowError, SyntaxError, IndentationError, ValueError ), e:
483 483 # Case 1
484 484 self.traceback_trap.args = sys.exc_info()
485 485 self.pack_exception(self.message,e)
486 486 return COMPILER_ERROR,False
487 487
488 488 if code is None:
489 489 # Case 2: incomplete input. This means that the input can span
490 490 # multiple lines. But we still need to decide when to actually
491 491 # stop taking user input. Later we'll add auto-indentation support
492 492 # somehow. In the meantime, we'll just stop if there are two lines
493 493 # of pure whitespace at the end.
494 494 last_two = source.rsplit('\n',2)[-2:]
495 495 print 'last two:',last_two # dbg
496 496 if len(last_two)==2 and all(s.isspace() for s in last_two):
497 497 return COMPLETE_INPUT,False
498 498 else:
499 499 return INCOMPLETE_INPUT, True
500 500 else:
501 501 # Case 3
502 502 return COMPLETE_INPUT, False
503 503
504 504 def pull(self, keys):
505 505 """ Get an item out of the namespace by key.
506 506
507 507 Parameters
508 508 ----------
509 509 key : str
510 510
511 511 Returns
512 512 -------
513 513 value : object
514 514
515 515 Raises
516 516 ------
517 517 TypeError if the key is not a string.
518 518 NameError if the object doesn't exist.
519 519 """
520 520
521 521 if isinstance(keys, str):
522 522 result = self.user_ns.get(keys, NotDefined())
523 523 if isinstance(result, NotDefined):
524 524 raise NameError('name %s is not defined' % keys)
525 525 elif isinstance(keys, (list, tuple)):
526 526 result = []
527 527 for key in keys:
528 528 if not isinstance(key, str):
529 529 raise TypeError("objects must be keyed by strings.")
530 530 else:
531 531 r = self.user_ns.get(key, NotDefined())
532 532 if isinstance(r, NotDefined):
533 533 raise NameError('name %s is not defined' % key)
534 534 else:
535 535 result.append(r)
536 536 if len(keys)==1:
537 537 result = result[0]
538 538 else:
539 539 raise TypeError("keys must be a strong or a list/tuple of strings")
540 540 return result
541 541
542 542 def pull_function(self, keys):
543 543 return self.pull(keys)
544 544
545 545 #### Interactive user API ##################################################
546 546
547 547 def ipsystem(self, command):
548 548 """ Execute a command in a system shell while expanding variables in the
549 549 current namespace.
550 550
551 551 Parameters
552 552 ----------
553 553 command : str
554 554 """
555 555
556 556 # Expand $variables.
557 557 command = self.var_expand(command)
558 558
559 559 system_shell(command,
560 560 header='IPython system call: ',
561 561 verbose=self.rc.system_verbose,
562 562 )
563 563
564 564 def ipmagic(self, arg_string):
565 565 """ Call a magic function by name.
566 566
567 567 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
568 568 prompt:
569 569
570 570 In[1]: %name -opt foo bar
571 571
572 572 To call a magic without arguments, simply use ipmagic('name').
573 573
574 574 This provides a proper Python function to call IPython's magics in any
575 575 valid Python code you can type at the interpreter, including loops and
576 576 compound statements. It is added by IPython to the Python builtin
577 577 namespace upon initialization.
578 578
579 579 Parameters
580 580 ----------
581 581 arg_string : str
582 582 A string containing the name of the magic function to call and any
583 583 additional arguments to be passed to the magic.
584 584
585 585 Returns
586 586 -------
587 587 something : object
588 588 The return value of the actual object.
589 589 """
590 590
591 591 # Taken from IPython.
592 592 raise NotImplementedError('Not ported yet')
593 593
594 594 args = arg_string.split(' ', 1)
595 595 magic_name = args[0]
596 596 magic_name = magic_name.lstrip(self.config.ESC_MAGIC)
597 597
598 598 try:
599 599 magic_args = args[1]
600 600 except IndexError:
601 601 magic_args = ''
602 602 fn = getattr(self.magic, 'magic_'+magic_name, None)
603 603 if fn is None:
604 604 self.error("Magic function `%s` not found." % magic_name)
605 605 else:
606 606 magic_args = self.var_expand(magic_args)
607 607 return fn(magic_args)
608 608
609 609
610 610 #### Private 'Interpreter' interface #######################################
611 611
612 612 def setup_message(self):
613 613 """Return a message object.
614 614
615 615 This method prepares and returns a message dictionary. This dict
616 616 contains the various fields that are used to transfer information about
617 617 execution, results, tracebacks, etc, to clients (either in or out of
618 618 process ones). Because of the need to work with possibly out of
619 619 process clients, this dict MUST contain strictly pickle-safe values.
620 620 """
621 621
622 622 return dict(number=self.current_cell_number)
623 623
624 624 def setup_namespace(self):
625 625 """ Add things to the namespace.
626 626 """
627 627
628 628 self.user_ns.setdefault('__name__', '__main__')
629 629 self.user_ns.setdefault('__builtins__', __builtin__)
630 630 self.user_ns['__IP'] = self
631 631 if self.raw_input_builtin is not None:
632 632 self.user_ns['raw_input'] = self.raw_input_builtin
633 633 if self.input_builtin is not None:
634 634 self.user_ns['input'] = self.input_builtin
635 635
636 636 builtin_additions = dict(
637 637 ipmagic=self.ipmagic,
638 638 )
639 639 __builtin__.__dict__.update(builtin_additions)
640 640
641 641 if self.history is not None:
642 642 self.history.setup_namespace(self.user_ns)
643 643
644 644 def set_traps(self):
645 645 """ Set all of the output, display, and traceback traps.
646 646 """
647 647
648 648 self.output_trap.set()
649 649 self.display_trap.set()
650 650 self.traceback_trap.set()
651 651
652 652 def unset_traps(self):
653 653 """ Unset all of the output, display, and traceback traps.
654 654 """
655 655
656 656 self.output_trap.unset()
657 657 self.display_trap.unset()
658 658 self.traceback_trap.unset()
659 659
660 660 def split_commands(self, python):
661 661 """ Split multiple lines of code into discrete commands that can be
662 662 executed singly.
663 663
664 664 Parameters
665 665 ----------
666 666 python : str
667 667 Pure, exec'able Python code.
668 668
669 669 Returns
670 670 -------
671 671 commands : list of str
672 672 Separate commands that can be exec'ed independently.
673 673 """
674 674
675 675 # compiler.parse treats trailing spaces after a newline as a
676 676 # SyntaxError. This is different than codeop.CommandCompiler, which
677 677 # will compile the trailng spaces just fine. We simply strip any
678 678 # trailing whitespace off. Passing a string with trailing whitespace
679 679 # to exec will fail however. There seems to be some inconsistency in
680 680 # how trailing whitespace is handled, but this seems to work.
681 681 python = python.strip()
682 682
683 683 # The compiler module does not like unicode. We need to convert
684 684 # it encode it:
685 685 if isinstance(python, unicode):
686 686 # Use the utf-8-sig BOM so the compiler detects this a UTF-8
687 687 # encode string.
688 688 python = '\xef\xbb\xbf' + python.encode('utf-8')
689 689
690 690 # The compiler module will parse the code into an abstract syntax tree.
691 691 # This has a bug with str("a\nb"), but not str("""a\nb""")!!!
692 692 ast = compiler.parse(python)
693 693
694 694 # Uncomment to help debug the ast tree
695 695 # for n in ast.node:
696 696 # print n.lineno,'->',n
697 697
698 698 # Each separate command is available by iterating over ast.node. The
699 699 # lineno attribute is the line number (1-indexed) beginning the commands
700 700 # suite.
701 701 # lines ending with ";" yield a Discard Node that doesn't have a lineno
702 702 # attribute. These nodes can and should be discarded. But there are
703 703 # other situations that cause Discard nodes that shouldn't be discarded.
704 704 # We might eventually discover other cases where lineno is None and have
705 705 # to put in a more sophisticated test.
706 706 linenos = [x.lineno-1 for x in ast.node if x.lineno is not None]
707 707
708 708 # When we finally get the slices, we will need to slice all the way to
709 709 # the end even though we don't have a line number for it. Fortunately,
710 710 # None does the job nicely.
711 711 linenos.append(None)
712 712
713 713 # Same problem at the other end: sometimes the ast tree has its
714 714 # first complete statement not starting on line 0. In this case
715 715 # we might miss part of it. This fixes ticket 266993. Thanks Gael!
716 716 linenos[0] = 0
717 717
718 718 lines = python.splitlines()
719 719
720 720 # Create a list of atomic commands.
721 721 cmds = []
722 722 for i, j in zip(linenos[:-1], linenos[1:]):
723 723 cmd = lines[i:j]
724 724 if cmd:
725 725 cmds.append('\n'.join(cmd)+'\n')
726 726
727 727 return cmds
728 728
729 729 def error(self, text):
730 730 """ Pass an error message back to the shell.
731 731
732 732 Preconditions
733 733 -------------
734 734 This should only be called when self.message is set. In other words,
735 735 when code is being executed.
736 736
737 737 Parameters
738 738 ----------
739 739 text : str
740 740 """
741 741
742 742 errors = self.message.get('IPYTHON_ERROR', [])
743 743 errors.append(text)
744 744
745 745 def var_expand(self, template):
746 746 """ Expand $variables in the current namespace using Itpl.
747 747
748 748 Parameters
749 749 ----------
750 750 template : str
751 751 """
752 752
753 753 return str(ItplNS(template, self.user_ns))
754 754
755 755 def _possible_macro(self, obj):
756 756 """ If the object is a macro, execute it.
757 757 """
758 758
759 759 if isinstance(obj, Macro):
760 760 self.execute_macro(obj)
761 761
@@ -1,279 +1,285 b''
1 1 # -*- coding: utf-8 -*-
2 2 """IPython Test Suite Runner.
3 3
4 4 This module provides a main entry point to a user script to test IPython
5 5 itself from the command line. There are two ways of running this script:
6 6
7 7 1. With the syntax `iptest all`. This runs our entire test suite by
8 8 calling this script (with different arguments) or trial recursively. This
9 9 causes modules and package to be tested in different processes, using nose
10 10 or trial where appropriate.
11 11 2. With the regular nose syntax, like `iptest -vvs IPython`. In this form
12 12 the script simply calls nose, but with special command line flags and
13 13 plugins loaded.
14 14
15 15 For now, this script requires that both nose and twisted are installed. This
16 16 will change in the future.
17 17 """
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Module imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import os
24 24 import os.path as path
25 25 import sys
26 26 import subprocess
27 27 import time
28 28 import warnings
29 29
30 30 import nose.plugins.builtin
31 31 from nose.core import TestProgram
32 32
33 33 from IPython.utils.platutils import find_cmd
34 34 from IPython.testing.plugin.ipdoctest import IPythonDoctest
35 35
36 36 pjoin = path.join
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Logic for skipping doctests
40 40 #-----------------------------------------------------------------------------
41 41
42 42 def test_for(mod):
43 43 """Test to see if mod is importable."""
44 44 try:
45 45 __import__(mod)
46 46 except ImportError:
47 47 return False
48 48 else:
49 49 return True
50 50
51 51 have_curses = test_for('_curses')
52 52 have_wx = test_for('wx')
53 have_wx_aui = test_for('wx.aui')
53 54 have_zi = test_for('zope.interface')
54 55 have_twisted = test_for('twisted')
55 56 have_foolscap = test_for('foolscap')
56 57 have_objc = test_for('objc')
57 58 have_pexpect = test_for('pexpect')
58 59
59 60
60 61 def make_exclude():
61 62
62 63 # For the IPythonDoctest plugin, we need to exclude certain patterns that cause
63 64 # testing problems. We should strive to minimize the number of skipped
64 65 # modules, since this means untested code. As the testing machinery
65 66 # solidifies, this list should eventually become empty.
66 67 EXCLUDE = [pjoin('IPython', 'external'),
67 68 pjoin('IPython', 'frontend', 'process', 'winprocess.py'),
68 69 pjoin('IPython_doctest_plugin'),
69 70 pjoin('IPython', 'extensions', 'ipy_'),
70 pjoin('IPython', 'extensions', 'clearcmd'),
71 pjoin('IPython', 'extensions', 'PhysicalQInput'),
71 72 pjoin('IPython', 'extensions', 'PhysicalQInteractive'),
73 pjoin('IPython', 'extensions', 'InterpreterPasteInput'),
72 74 pjoin('IPython', 'extensions', 'scitedirector'),
73 75 pjoin('IPython', 'extensions', 'numeric_formats'),
74 76 pjoin('IPython', 'testing', 'attic'),
75 77 pjoin('IPython', 'testing', 'tools'),
76 78 pjoin('IPython', 'testing', 'mkdoctests')
77 79 ]
78 80
79 81 if not have_wx:
80 82 EXCLUDE.append(pjoin('IPython', 'extensions', 'igrid'))
81 83 EXCLUDE.append(pjoin('IPython', 'gui'))
82 84 EXCLUDE.append(pjoin('IPython', 'frontend', 'wx'))
83 85
86 if not have_wx_aui:
87 EXCLUDE.append(pjoin('IPython', 'gui', 'wx', 'wxIPython'))
88
84 89 if not have_objc:
85 90 EXCLUDE.append(pjoin('IPython', 'frontend', 'cocoa'))
86 91
87 92 if not have_curses:
88 93 EXCLUDE.append(pjoin('IPython', 'extensions', 'ibrowse'))
89 94
90 95 if not sys.platform == 'win32':
91 96 EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_win32'))
92 97
93 98 # These have to be skipped on win32 because the use echo, rm, cd, etc.
94 99 # See ticket https://bugs.launchpad.net/bugs/366982
95 100 if sys.platform == 'win32':
96 101 EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'test_exampleip'))
97 102 EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'dtexample'))
98 103
99 104 if not os.name == 'posix':
100 105 EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_posix'))
101 106
102 107 if not have_pexpect:
103 108 EXCLUDE.append(pjoin('IPython', 'scripts', 'irunner'))
104 109
105 110 # Skip shell always because of a bug in FakeModule.
106 111 EXCLUDE.append(pjoin('IPython', 'core', 'shell'))
107 112
108 113 # This is needed for the reg-exp to match on win32 in the ipdoctest plugin.
109 114 if sys.platform == 'win32':
110 115 EXCLUDE = [s.replace('\\','\\\\') for s in EXCLUDE]
111 116
112 117 return EXCLUDE
113 118
119
114 120 #-----------------------------------------------------------------------------
115 121 # Functions and classes
116 122 #-----------------------------------------------------------------------------
117 123
118 124 def run_iptest():
119 125 """Run the IPython test suite using nose.
120 126
121 127 This function is called when this script is **not** called with the form
122 128 `iptest all`. It simply calls nose with appropriate command line flags
123 129 and accepts all of the standard nose arguments.
124 130 """
125 131
126 132 warnings.filterwarnings('ignore',
127 133 'This will be removed soon. Use IPython.testing.util instead')
128 134
129 135 argv = sys.argv + [
130 136 # Loading ipdoctest causes problems with Twisted.
131 137 # I am removing this as a temporary fix to get the
132 138 # test suite back into working shape. Our nose
133 139 # plugin needs to be gone through with a fine
134 140 # toothed comb to find what is causing the problem.
135 141 '--with-ipdoctest',
136 142 '--ipdoctest-tests','--ipdoctest-extension=txt',
137 143 '--detailed-errors',
138 144
139 145 # We add --exe because of setuptools' imbecility (it
140 146 # blindly does chmod +x on ALL files). Nose does the
141 147 # right thing and it tries to avoid executables,
142 148 # setuptools unfortunately forces our hand here. This
143 149 # has been discussed on the distutils list and the
144 150 # setuptools devs refuse to fix this problem!
145 151 '--exe',
146 152 ]
147 153
148 154 # Detect if any tests were required by explicitly calling an IPython
149 155 # submodule or giving a specific path
150 156 has_tests = False
151 157 for arg in sys.argv:
152 158 if 'IPython' in arg or arg.endswith('.py') or \
153 159 (':' in arg and '.py' in arg):
154 160 has_tests = True
155 161 break
156 162
157 163 # If nothing was specifically requested, test full IPython
158 164 if not has_tests:
159 165 argv.append('IPython')
160 166
161 167 # Construct list of plugins, omitting the existing doctest plugin, which
162 168 # ours replaces (and extends).
163 169 EXCLUDE = make_exclude()
164 170 plugins = [IPythonDoctest(EXCLUDE)]
165 171 for p in nose.plugins.builtin.plugins:
166 172 plug = p()
167 173 if plug.name == 'doctest':
168 174 continue
169 175 plugins.append(plug)
170 176
171 177 TestProgram(argv=argv,plugins=plugins)
172 178
173 179
174 180 class IPTester(object):
175 181 """Call that calls iptest or trial in a subprocess.
176 182 """
177 183 def __init__(self,runner='iptest',params=None):
178 184 """ """
179 185 if runner == 'iptest':
180 186 self.runner = ['iptest','-v']
181 187 else:
182 188 self.runner = [find_cmd('trial')]
183 189 if params is None:
184 190 params = []
185 191 if isinstance(params,str):
186 192 params = [params]
187 193 self.params = params
188 194
189 195 # Assemble call
190 196 self.call_args = self.runner+self.params
191 197
192 198 def run(self):
193 199 """Run the stored commands"""
194 200 return subprocess.call(self.call_args)
195 201
196 202
197 203 def make_runners():
198 204 """Define the top-level packages that need to be tested.
199 205 """
200 206
201 207 nose_packages = ['config', 'core', 'extensions',
202 208 'frontend', 'lib', 'quarantine',
203 209 'scripts', 'testing', 'utils']
204 210 trial_packages = ['kernel']
205 211
206 212 if have_wx:
207 213 nose_packages.append('gui')
208 214
209 215 nose_packages = ['IPython.%s' % m for m in nose_packages ]
210 216 trial_packages = ['IPython.%s' % m for m in trial_packages ]
211 217
212 218 # Make runners
213 219 runners = dict()
214 220
215 221 nose_runners = dict(zip(nose_packages, [IPTester(params=v) for v in nose_packages]))
216 222 if have_zi and have_twisted and have_foolscap:
217 223 trial_runners = dict(zip(trial_packages, [IPTester('trial',params=v) for v in trial_packages]))
218 224 runners.update(nose_runners)
219 225 runners.update(trial_runners)
220 226
221 227 return runners
222 228
223 229
224 230 def run_iptestall():
225 231 """Run the entire IPython test suite by calling nose and trial.
226 232
227 233 This function constructs :class:`IPTester` instances for all IPython
228 234 modules and package and then runs each of them. This causes the modules
229 235 and packages of IPython to be tested each in their own subprocess using
230 236 nose or twisted.trial appropriately.
231 237 """
232 238
233 239 runners = make_runners()
234 240
235 241 # Run all test runners, tracking execution time
236 242 failed = {}
237 243 t_start = time.time()
238 244 for name,runner in runners.iteritems():
239 245 print '*'*77
240 print 'IPython test set:', name
246 print 'IPython test group:',name
241 247 res = runner.run()
242 248 if res:
243 249 failed[name] = res
244 250 t_end = time.time()
245 251 t_tests = t_end - t_start
246 252 nrunners = len(runners)
247 253 nfail = len(failed)
248 254 # summarize results
249 255 print
250 256 print '*'*77
251 print 'Ran %s test sets in %.3fs' % (nrunners, t_tests)
257 print 'Ran %s test groups in %.3fs' % (nrunners, t_tests)
252 258 print
253 259 if not failed:
254 260 print 'OK'
255 261 else:
256 262 # If anything went wrong, point out what command to rerun manually to
257 263 # see the actual errors and individual summary
258 print 'ERROR - %s out of %s test sets failed.' % (nfail, nrunners)
264 print 'ERROR - %s out of %s test groups failed.' % (nfail, nrunners)
259 265 for name in failed:
260 266 failed_runner = runners[name]
261 267 print '-'*40
262 268 print 'Runner failed:',name
263 269 print 'You may wish to rerun this one individually, with:'
264 270 print ' '.join(failed_runner.call_args)
265 271 print
266 272
267 273
268 274 def main():
269 275 if len(sys.argv) == 1:
270 276 run_iptestall()
271 277 else:
272 278 if sys.argv[1] == 'all':
273 279 run_iptestall()
274 280 else:
275 281 run_iptest()
276 282
277 283
278 284 if __name__ == '__main__':
279 main() No newline at end of file
285 main()
@@ -1,909 +1,918 b''
1 1 """Nose Plugin that supports IPython doctests.
2 2
3 3 Limitations:
4 4
5 5 - When generating examples for use as doctests, make sure that you have
6 6 pretty-printing OFF. This can be done either by starting ipython with the
7 7 flag '--nopprint', by setting pprint to 0 in your ipythonrc file, or by
8 8 interactively disabling it with %Pprint. This is required so that IPython
9 9 output matches that of normal Python, which is used by doctest for internal
10 10 execution.
11 11
12 12 - Do not rely on specific prompt numbers for results (such as using
13 13 '_34==True', for example). For IPython tests run via an external process the
14 14 prompt numbers may be different, and IPython tests run as normal python code
15 15 won't even have these special _NN variables set at all.
16 16 """
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Module imports
20 20
21 21 # From the standard library
22 22 import __builtin__
23 23 import commands
24 24 import doctest
25 25 import inspect
26 26 import logging
27 27 import os
28 28 import re
29 29 import sys
30 30 import traceback
31 31 import unittest
32 32
33 33 from inspect import getmodule
34 34 from StringIO import StringIO
35 35
36 36 # We are overriding the default doctest runner, so we need to import a few
37 37 # things from doctest directly
38 38 from doctest import (REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE,
39 39 _unittest_reportflags, DocTestRunner,
40 40 _extract_future_flags, pdb, _OutputRedirectingPdb,
41 41 _exception_traceback,
42 42 linecache)
43 43
44 44 # Third-party modules
45 45 import nose.core
46 46
47 47 from nose.plugins import doctests, Plugin
48 48 from nose.util import anyp, getpackage, test_address, resolve_name, tolist
49 49
50 50 #-----------------------------------------------------------------------------
51 51 # Module globals and other constants
52 52
53 53 log = logging.getLogger(__name__)
54 54
55 55 ###########################################################################
56 56 # *** HACK ***
57 57 # We must start our own ipython object and heavily muck with it so that all the
58 58 # modifications IPython makes to system behavior don't send the doctest
59 59 # machinery into a fit. This code should be considered a gross hack, but it
60 60 # gets the job done.
61 61
62 62 def default_argv():
63 63 """Return a valid default argv for creating testing instances of ipython"""
64 64
65 65 # Get the install directory for the user configuration and tell ipython to
66 66 # use the default profile from there.
67 67 from IPython.config import userconfig
68 68 ipcdir = os.path.dirname(userconfig.__file__)
69 69 #ipconf = os.path.join(ipcdir,'ipy_user_conf.py')
70 70 ipconf = os.path.join(ipcdir,'ipythonrc')
71 71 #print 'conf:',ipconf # dbg
72 72
73 73 return ['--colors=NoColor','--noterm_title','-rcfile=%s' % ipconf]
74 74
75 75
76 76 # Hack to modify the %run command so we can sync the user's namespace with the
77 77 # test globals. Once we move over to a clean magic system, this will be done
78 78 # with much less ugliness.
79 79
80 80 class py_file_finder(object):
81 81 def __init__(self,test_filename):
82 82 self.test_filename = test_filename
83 83
84 84 def __call__(self,name):
85 85 from IPython.utils.genutils import get_py_filename
86 86 try:
87 87 return get_py_filename(name)
88 88 except IOError:
89 89 test_dir = os.path.dirname(self.test_filename)
90 90 new_path = os.path.join(test_dir,name)
91 91 return get_py_filename(new_path)
92 92
93 93
94 94 def _run_ns_sync(self,arg_s,runner=None):
95 95 """Modified version of %run that syncs testing namespaces.
96 96
97 97 This is strictly needed for running doctests that call %run.
98 98 """
99 99
100 100 # When tests call %run directly (not via doctest) these function attributes
101 101 # are not set
102 102 try:
103 103 fname = _run_ns_sync.test_filename
104 104 except AttributeError:
105 105 fname = arg_s
106 106
107 107 finder = py_file_finder(fname)
108 108 out = _ip.IP.magic_run_ori(arg_s,runner,finder)
109 109
110 110 # Simliarly, there is no test_globs when a test is NOT a doctest
111 111 if hasattr(_run_ns_sync,'test_globs'):
112 112 _run_ns_sync.test_globs.update(_ip.user_ns)
113 113 return out
114 114
115 115
116 116 class ipnsdict(dict):
117 117 """A special subclass of dict for use as an IPython namespace in doctests.
118 118
119 119 This subclass adds a simple checkpointing capability so that when testing
120 120 machinery clears it (we use it as the test execution context), it doesn't
121 121 get completely destroyed.
122 122 """
123 123
124 124 def __init__(self,*a):
125 125 dict.__init__(self,*a)
126 126 self._savedict = {}
127 127
128 128 def clear(self):
129 129 dict.clear(self)
130 130 self.update(self._savedict)
131 131
132 132 def _checkpoint(self):
133 133 self._savedict.clear()
134 134 self._savedict.update(self)
135 135
136 136 def update(self,other):
137 137 self._checkpoint()
138 138 dict.update(self,other)
139 139
140 140 # If '_' is in the namespace, python won't set it when executing code,
141 141 # and we have examples that test it. So we ensure that the namespace
142 142 # is always 'clean' of it before it's used for test code execution.
143 143 self.pop('_',None)
144 144
145 145 # The builtins namespace must *always* be the real __builtin__ module,
146 146 # else weird stuff happens. The main ipython code does have provisions
147 147 # to ensure this after %run, but since in this class we do some
148 148 # aggressive low-level cleaning of the execution namespace, we need to
149 149 # correct for that ourselves, to ensure consitency with the 'real'
150 150 # ipython.
151 151 self['__builtins__'] = __builtin__
152 152
153 153
154 154 def start_ipython():
155 155 """Start a global IPython shell, which we need for IPython-specific syntax.
156 156 """
157 157
158 158 # This function should only ever run once!
159 159 if hasattr(start_ipython,'already_called'):
160 160 return
161 161 start_ipython.already_called = True
162 162
163 163 # Ok, first time we're called, go ahead
164 164 import new
165 165
166 166 import IPython
167 167 from IPython.core import ipapi
168 168
169 169 def xsys(cmd):
170 170 """Execute a command and print its output.
171 171
172 172 This is just a convenience function to replace the IPython system call
173 173 with one that is more doctest-friendly.
174 174 """
175 175 cmd = _ip.IP.var_expand(cmd,depth=1)
176 176 sys.stdout.write(commands.getoutput(cmd))
177 177 sys.stdout.flush()
178 178
179 179 # Store certain global objects that IPython modifies
180 180 _displayhook = sys.displayhook
181 181 _excepthook = sys.excepthook
182 182 _main = sys.modules.get('__main__')
183 183
184 184 argv = default_argv()
185 185
186 186 # Start IPython instance. We customize it to start with minimal frills.
187 187 user_ns,global_ns = ipapi.make_user_namespaces(ipnsdict(),dict())
188 188 IPython.shell.IPShell(argv,user_ns,global_ns)
189 189
190 190 # Deactivate the various python system hooks added by ipython for
191 191 # interactive convenience so we don't confuse the doctest system
192 192 sys.modules['__main__'] = _main
193 193 sys.displayhook = _displayhook
194 194 sys.excepthook = _excepthook
195 195
196 196 # So that ipython magics and aliases can be doctested (they work by making
197 197 # a call into a global _ip object)
198 198 _ip = ipapi.get()
199 199 __builtin__._ip = _ip
200 200
201 201 # Modify the IPython system call with one that uses getoutput, so that we
202 202 # can capture subcommands and print them to Python's stdout, otherwise the
203 203 # doctest machinery would miss them.
204 204 _ip.system = xsys
205 205
206 206 # Also patch our %run function in.
207 207 im = new.instancemethod(_run_ns_sync,_ip.IP, _ip.IP.__class__)
208 208 _ip.IP.magic_run_ori = _ip.IP.magic_run
209 209 _ip.IP.magic_run = im
210 210
211 # XXX - For some very bizarre reason, the loading of %history by default is
212 # failing. This needs to be fixed later, but for now at least this ensures
213 # that tests that use %hist run to completion.
214 from IPython.core import history
215 history.init_ipython(_ip)
216 if not hasattr(_ip.IP,'magic_history'):
217 raise RuntimeError("Can't load magics, aborting")
218
219
211 220 # The start call MUST be made here. I'm not sure yet why it doesn't work if
212 221 # it is made later, at plugin initialization time, but in all my tests, that's
213 222 # the case.
214 223 start_ipython()
215 224
216 225 # *** END HACK ***
217 226 ###########################################################################
218 227
219 228 # Classes and functions
220 229
221 230 def is_extension_module(filename):
222 231 """Return whether the given filename is an extension module.
223 232
224 233 This simply checks that the extension is either .so or .pyd.
225 234 """
226 235 return os.path.splitext(filename)[1].lower() in ('.so','.pyd')
227 236
228 237
229 238 class DocTestSkip(object):
230 239 """Object wrapper for doctests to be skipped."""
231 240
232 241 ds_skip = """Doctest to skip.
233 242 >>> 1 #doctest: +SKIP
234 243 """
235 244
236 245 def __init__(self,obj):
237 246 self.obj = obj
238 247
239 248 def __getattribute__(self,key):
240 249 if key == '__doc__':
241 250 return DocTestSkip.ds_skip
242 251 else:
243 252 return getattr(object.__getattribute__(self,'obj'),key)
244 253
245 254 # Modified version of the one in the stdlib, that fixes a python bug (doctests
246 255 # not found in extension modules, http://bugs.python.org/issue3158)
247 256 class DocTestFinder(doctest.DocTestFinder):
248 257
249 258 def _from_module(self, module, object):
250 259 """
251 260 Return true if the given object is defined in the given
252 261 module.
253 262 """
254 263 if module is None:
255 264 return True
256 265 elif inspect.isfunction(object):
257 266 return module.__dict__ is object.func_globals
258 267 elif inspect.isbuiltin(object):
259 268 return module.__name__ == object.__module__
260 269 elif inspect.isclass(object):
261 270 return module.__name__ == object.__module__
262 271 elif inspect.ismethod(object):
263 272 # This one may be a bug in cython that fails to correctly set the
264 273 # __module__ attribute of methods, but since the same error is easy
265 274 # to make by extension code writers, having this safety in place
266 275 # isn't such a bad idea
267 276 return module.__name__ == object.im_class.__module__
268 277 elif inspect.getmodule(object) is not None:
269 278 return module is inspect.getmodule(object)
270 279 elif hasattr(object, '__module__'):
271 280 return module.__name__ == object.__module__
272 281 elif isinstance(object, property):
273 282 return True # [XX] no way not be sure.
274 283 else:
275 284 raise ValueError("object must be a class or function")
276 285
277 286 def _find(self, tests, obj, name, module, source_lines, globs, seen):
278 287 """
279 288 Find tests for the given object and any contained objects, and
280 289 add them to `tests`.
281 290 """
282 291
283 292 if hasattr(obj,"skip_doctest"):
284 293 #print 'SKIPPING DOCTEST FOR:',obj # dbg
285 294 obj = DocTestSkip(obj)
286 295
287 296 doctest.DocTestFinder._find(self,tests, obj, name, module,
288 297 source_lines, globs, seen)
289 298
290 299 # Below we re-run pieces of the above method with manual modifications,
291 300 # because the original code is buggy and fails to correctly identify
292 301 # doctests in extension modules.
293 302
294 303 # Local shorthands
295 304 from inspect import isroutine, isclass, ismodule
296 305
297 306 # Look for tests in a module's contained objects.
298 307 if inspect.ismodule(obj) and self._recurse:
299 308 for valname, val in obj.__dict__.items():
300 309 valname1 = '%s.%s' % (name, valname)
301 310 if ( (isroutine(val) or isclass(val))
302 311 and self._from_module(module, val) ):
303 312
304 313 self._find(tests, val, valname1, module, source_lines,
305 314 globs, seen)
306 315
307 316 # Look for tests in a class's contained objects.
308 317 if inspect.isclass(obj) and self._recurse:
309 318 #print 'RECURSE into class:',obj # dbg
310 319 for valname, val in obj.__dict__.items():
311 320 # Special handling for staticmethod/classmethod.
312 321 if isinstance(val, staticmethod):
313 322 val = getattr(obj, valname)
314 323 if isinstance(val, classmethod):
315 324 val = getattr(obj, valname).im_func
316 325
317 326 # Recurse to methods, properties, and nested classes.
318 327 if ((inspect.isfunction(val) or inspect.isclass(val) or
319 328 inspect.ismethod(val) or
320 329 isinstance(val, property)) and
321 330 self._from_module(module, val)):
322 331 valname = '%s.%s' % (name, valname)
323 332 self._find(tests, val, valname, module, source_lines,
324 333 globs, seen)
325 334
326 335
327 336 class IPDoctestOutputChecker(doctest.OutputChecker):
328 337 """Second-chance checker with support for random tests.
329 338
330 339 If the default comparison doesn't pass, this checker looks in the expected
331 340 output string for flags that tell us to ignore the output.
332 341 """
333 342
334 343 random_re = re.compile(r'#\s*random\s+')
335 344
336 345 def check_output(self, want, got, optionflags):
337 346 """Check output, accepting special markers embedded in the output.
338 347
339 348 If the output didn't pass the default validation but the special string
340 349 '#random' is included, we accept it."""
341 350
342 351 # Let the original tester verify first, in case people have valid tests
343 352 # that happen to have a comment saying '#random' embedded in.
344 353 ret = doctest.OutputChecker.check_output(self, want, got,
345 354 optionflags)
346 355 if not ret and self.random_re.search(want):
347 356 #print >> sys.stderr, 'RANDOM OK:',want # dbg
348 357 return True
349 358
350 359 return ret
351 360
352 361
353 362 class DocTestCase(doctests.DocTestCase):
354 363 """Proxy for DocTestCase: provides an address() method that
355 364 returns the correct address for the doctest case. Otherwise
356 365 acts as a proxy to the test case. To provide hints for address(),
357 366 an obj may also be passed -- this will be used as the test object
358 367 for purposes of determining the test address, if it is provided.
359 368 """
360 369
361 370 # Note: this method was taken from numpy's nosetester module.
362 371
363 372 # Subclass nose.plugins.doctests.DocTestCase to work around a bug in
364 373 # its constructor that blocks non-default arguments from being passed
365 374 # down into doctest.DocTestCase
366 375
367 376 def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
368 377 checker=None, obj=None, result_var='_'):
369 378 self._result_var = result_var
370 379 doctests.DocTestCase.__init__(self, test,
371 380 optionflags=optionflags,
372 381 setUp=setUp, tearDown=tearDown,
373 382 checker=checker)
374 383 # Now we must actually copy the original constructor from the stdlib
375 384 # doctest class, because we can't call it directly and a bug in nose
376 385 # means it never gets passed the right arguments.
377 386
378 387 self._dt_optionflags = optionflags
379 388 self._dt_checker = checker
380 389 self._dt_test = test
381 390 self._dt_setUp = setUp
382 391 self._dt_tearDown = tearDown
383 392
384 393 # XXX - store this runner once in the object!
385 394 runner = IPDocTestRunner(optionflags=optionflags,
386 395 checker=checker, verbose=False)
387 396 self._dt_runner = runner
388 397
389 398
390 399 # Each doctest should remember what directory it was loaded from...
391 400 self._ori_dir = os.getcwd()
392 401
393 402 # Modified runTest from the default stdlib
394 403 def runTest(self):
395 404 test = self._dt_test
396 405 runner = self._dt_runner
397 406
398 407 old = sys.stdout
399 408 new = StringIO()
400 409 optionflags = self._dt_optionflags
401 410
402 411 if not (optionflags & REPORTING_FLAGS):
403 412 # The option flags don't include any reporting flags,
404 413 # so add the default reporting flags
405 414 optionflags |= _unittest_reportflags
406 415
407 416 try:
408 417 # Save our current directory and switch out to the one where the
409 418 # test was originally created, in case another doctest did a
410 419 # directory change. We'll restore this in the finally clause.
411 420 curdir = os.getcwd()
412 421 os.chdir(self._ori_dir)
413 422
414 423 runner.DIVIDER = "-"*70
415 424 failures, tries = runner.run(test,out=new.write,
416 425 clear_globs=False)
417 426 finally:
418 427 sys.stdout = old
419 428 os.chdir(curdir)
420 429
421 430 if failures:
422 431 raise self.failureException(self.format_failure(new.getvalue()))
423 432
424 433 def setUp(self):
425 434 """Modified test setup that syncs with ipython namespace"""
426 435
427 436 if isinstance(self._dt_test.examples[0],IPExample):
428 437 # for IPython examples *only*, we swap the globals with the ipython
429 438 # namespace, after updating it with the globals (which doctest
430 439 # fills with the necessary info from the module being tested).
431 440 _ip.IP.user_ns.update(self._dt_test.globs)
432 441 self._dt_test.globs = _ip.IP.user_ns
433 442
434 443 doctests.DocTestCase.setUp(self)
435 444
436 445
437 446 # A simple subclassing of the original with a different class name, so we can
438 447 # distinguish and treat differently IPython examples from pure python ones.
439 448 class IPExample(doctest.Example): pass
440 449
441 450
442 451 class IPExternalExample(doctest.Example):
443 452 """Doctest examples to be run in an external process."""
444 453
445 454 def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
446 455 options=None):
447 456 # Parent constructor
448 457 doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options)
449 458
450 459 # An EXTRA newline is needed to prevent pexpect hangs
451 460 self.source += '\n'
452 461
453 462
454 463 class IPDocTestParser(doctest.DocTestParser):
455 464 """
456 465 A class used to parse strings containing doctest examples.
457 466
458 467 Note: This is a version modified to properly recognize IPython input and
459 468 convert any IPython examples into valid Python ones.
460 469 """
461 470 # This regular expression is used to find doctest examples in a
462 471 # string. It defines three groups: `source` is the source code
463 472 # (including leading indentation and prompts); `indent` is the
464 473 # indentation of the first (PS1) line of the source code; and
465 474 # `want` is the expected output (including leading indentation).
466 475
467 476 # Classic Python prompts or default IPython ones
468 477 _PS1_PY = r'>>>'
469 478 _PS2_PY = r'\.\.\.'
470 479
471 480 _PS1_IP = r'In\ \[\d+\]:'
472 481 _PS2_IP = r'\ \ \ \.\.\.+:'
473 482
474 483 _RE_TPL = r'''
475 484 # Source consists of a PS1 line followed by zero or more PS2 lines.
476 485 (?P<source>
477 486 (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line
478 487 (?:\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines
479 488 \n? # a newline
480 489 # Want consists of any non-blank lines that do not start with PS1.
481 490 (?P<want> (?:(?![ ]*$) # Not a blank line
482 491 (?![ ]*%s) # Not a line starting with PS1
483 492 (?![ ]*%s) # Not a line starting with PS2
484 493 .*$\n? # But any other line
485 494 )*)
486 495 '''
487 496
488 497 _EXAMPLE_RE_PY = re.compile( _RE_TPL % (_PS1_PY,_PS2_PY,_PS1_PY,_PS2_PY),
489 498 re.MULTILINE | re.VERBOSE)
490 499
491 500 _EXAMPLE_RE_IP = re.compile( _RE_TPL % (_PS1_IP,_PS2_IP,_PS1_IP,_PS2_IP),
492 501 re.MULTILINE | re.VERBOSE)
493 502
494 503 # Mark a test as being fully random. In this case, we simply append the
495 504 # random marker ('#random') to each individual example's output. This way
496 505 # we don't need to modify any other code.
497 506 _RANDOM_TEST = re.compile(r'#\s*all-random\s+')
498 507
499 508 # Mark tests to be executed in an external process - currently unsupported.
500 509 _EXTERNAL_IP = re.compile(r'#\s*ipdoctest:\s*EXTERNAL')
501 510
502 511 def ip2py(self,source):
503 512 """Convert input IPython source into valid Python."""
504 513 out = []
505 514 newline = out.append
506 515 #print 'IPSRC:\n',source,'\n###' # dbg
507 516 # The input source must be first stripped of all bracketing whitespace
508 517 # and turned into lines, so it looks to the parser like regular user
509 518 # input
510 519 for lnum,line in enumerate(source.strip().splitlines()):
511 520 newline(_ip.IP.prefilter(line,lnum>0))
512 521 newline('') # ensure a closing newline, needed by doctest
513 522 #print "PYSRC:", '\n'.join(out) # dbg
514 523 return '\n'.join(out)
515 524
516 525 def parse(self, string, name='<string>'):
517 526 """
518 527 Divide the given string into examples and intervening text,
519 528 and return them as a list of alternating Examples and strings.
520 529 Line numbers for the Examples are 0-based. The optional
521 530 argument `name` is a name identifying this string, and is only
522 531 used for error messages.
523 532 """
524 533
525 534 #print 'Parse string:\n',string # dbg
526 535
527 536 string = string.expandtabs()
528 537 # If all lines begin with the same indentation, then strip it.
529 538 min_indent = self._min_indent(string)
530 539 if min_indent > 0:
531 540 string = '\n'.join([l[min_indent:] for l in string.split('\n')])
532 541
533 542 output = []
534 543 charno, lineno = 0, 0
535 544
536 545 # We make 'all random' tests by adding the '# random' mark to every
537 546 # block of output in the test.
538 547 if self._RANDOM_TEST.search(string):
539 548 random_marker = '\n# random'
540 549 else:
541 550 random_marker = ''
542 551
543 552 # Whether to convert the input from ipython to python syntax
544 553 ip2py = False
545 554 # Find all doctest examples in the string. First, try them as Python
546 555 # examples, then as IPython ones
547 556 terms = list(self._EXAMPLE_RE_PY.finditer(string))
548 557 if terms:
549 558 # Normal Python example
550 559 #print '-'*70 # dbg
551 560 #print 'PyExample, Source:\n',string # dbg
552 561 #print '-'*70 # dbg
553 562 Example = doctest.Example
554 563 else:
555 564 # It's an ipython example. Note that IPExamples are run
556 565 # in-process, so their syntax must be turned into valid python.
557 566 # IPExternalExamples are run out-of-process (via pexpect) so they
558 567 # don't need any filtering (a real ipython will be executing them).
559 568 terms = list(self._EXAMPLE_RE_IP.finditer(string))
560 569 if self._EXTERNAL_IP.search(string):
561 570 #print '-'*70 # dbg
562 571 #print 'IPExternalExample, Source:\n',string # dbg
563 572 #print '-'*70 # dbg
564 573 Example = IPExternalExample
565 574 else:
566 575 #print '-'*70 # dbg
567 576 #print 'IPExample, Source:\n',string # dbg
568 577 #print '-'*70 # dbg
569 578 Example = IPExample
570 579 ip2py = True
571 580
572 581 for m in terms:
573 582 # Add the pre-example text to `output`.
574 583 output.append(string[charno:m.start()])
575 584 # Update lineno (lines before this example)
576 585 lineno += string.count('\n', charno, m.start())
577 586 # Extract info from the regexp match.
578 587 (source, options, want, exc_msg) = \
579 588 self._parse_example(m, name, lineno,ip2py)
580 589
581 590 # Append the random-output marker (it defaults to empty in most
582 591 # cases, it's only non-empty for 'all-random' tests):
583 592 want += random_marker
584 593
585 594 if Example is IPExternalExample:
586 595 options[doctest.NORMALIZE_WHITESPACE] = True
587 596 want += '\n'
588 597
589 598 # Create an Example, and add it to the list.
590 599 if not self._IS_BLANK_OR_COMMENT(source):
591 600 output.append(Example(source, want, exc_msg,
592 601 lineno=lineno,
593 602 indent=min_indent+len(m.group('indent')),
594 603 options=options))
595 604 # Update lineno (lines inside this example)
596 605 lineno += string.count('\n', m.start(), m.end())
597 606 # Update charno.
598 607 charno = m.end()
599 608 # Add any remaining post-example text to `output`.
600 609 output.append(string[charno:])
601 610 return output
602 611
603 612 def _parse_example(self, m, name, lineno,ip2py=False):
604 613 """
605 614 Given a regular expression match from `_EXAMPLE_RE` (`m`),
606 615 return a pair `(source, want)`, where `source` is the matched
607 616 example's source code (with prompts and indentation stripped);
608 617 and `want` is the example's expected output (with indentation
609 618 stripped).
610 619
611 620 `name` is the string's name, and `lineno` is the line number
612 621 where the example starts; both are used for error messages.
613 622
614 623 Optional:
615 624 `ip2py`: if true, filter the input via IPython to convert the syntax
616 625 into valid python.
617 626 """
618 627
619 628 # Get the example's indentation level.
620 629 indent = len(m.group('indent'))
621 630
622 631 # Divide source into lines; check that they're properly
623 632 # indented; and then strip their indentation & prompts.
624 633 source_lines = m.group('source').split('\n')
625 634
626 635 # We're using variable-length input prompts
627 636 ps1 = m.group('ps1')
628 637 ps2 = m.group('ps2')
629 638 ps1_len = len(ps1)
630 639
631 640 self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len)
632 641 if ps2:
633 642 self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno)
634 643
635 644 source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines])
636 645
637 646 if ip2py:
638 647 # Convert source input from IPython into valid Python syntax
639 648 source = self.ip2py(source)
640 649
641 650 # Divide want into lines; check that it's properly indented; and
642 651 # then strip the indentation. Spaces before the last newline should
643 652 # be preserved, so plain rstrip() isn't good enough.
644 653 want = m.group('want')
645 654 want_lines = want.split('\n')
646 655 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
647 656 del want_lines[-1] # forget final newline & spaces after it
648 657 self._check_prefix(want_lines, ' '*indent, name,
649 658 lineno + len(source_lines))
650 659
651 660 # Remove ipython output prompt that might be present in the first line
652 661 want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0])
653 662
654 663 want = '\n'.join([wl[indent:] for wl in want_lines])
655 664
656 665 # If `want` contains a traceback message, then extract it.
657 666 m = self._EXCEPTION_RE.match(want)
658 667 if m:
659 668 exc_msg = m.group('msg')
660 669 else:
661 670 exc_msg = None
662 671
663 672 # Extract options from the source.
664 673 options = self._find_options(source, name, lineno)
665 674
666 675 return source, options, want, exc_msg
667 676
668 677 def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len):
669 678 """
670 679 Given the lines of a source string (including prompts and
671 680 leading indentation), check to make sure that every prompt is
672 681 followed by a space character. If any line is not followed by
673 682 a space character, then raise ValueError.
674 683
675 684 Note: IPython-modified version which takes the input prompt length as a
676 685 parameter, so that prompts of variable length can be dealt with.
677 686 """
678 687 space_idx = indent+ps1_len
679 688 min_len = space_idx+1
680 689 for i, line in enumerate(lines):
681 690 if len(line) >= min_len and line[space_idx] != ' ':
682 691 raise ValueError('line %r of the docstring for %s '
683 692 'lacks blank after %s: %r' %
684 693 (lineno+i+1, name,
685 694 line[indent:space_idx], line))
686 695
687 696
688 697 SKIP = doctest.register_optionflag('SKIP')
689 698
690 699
691 700 class IPDocTestRunner(doctest.DocTestRunner,object):
692 701 """Test runner that synchronizes the IPython namespace with test globals.
693 702 """
694 703
695 704 def run(self, test, compileflags=None, out=None, clear_globs=True):
696 705
697 706 # Hack: ipython needs access to the execution context of the example,
698 707 # so that it can propagate user variables loaded by %run into
699 708 # test.globs. We put them here into our modified %run as a function
700 709 # attribute. Our new %run will then only make the namespace update
701 710 # when called (rather than unconconditionally updating test.globs here
702 711 # for all examples, most of which won't be calling %run anyway).
703 712 _run_ns_sync.test_globs = test.globs
704 713 _run_ns_sync.test_filename = test.filename
705 714
706 715 return super(IPDocTestRunner,self).run(test,
707 716 compileflags,out,clear_globs)
708 717
709 718
710 719 class DocFileCase(doctest.DocFileCase):
711 720 """Overrides to provide filename
712 721 """
713 722 def address(self):
714 723 return (self._dt_test.filename, None, None)
715 724
716 725
717 726 class ExtensionDoctest(doctests.Doctest):
718 727 """Nose Plugin that supports doctests in extension modules.
719 728 """
720 729 name = 'extdoctest' # call nosetests with --with-extdoctest
721 730 enabled = True
722 731
723 732 def __init__(self,exclude_patterns=None):
724 733 """Create a new ExtensionDoctest plugin.
725 734
726 735 Parameters
727 736 ----------
728 737
729 738 exclude_patterns : sequence of strings, optional
730 739 These patterns are compiled as regular expressions, subsequently used
731 740 to exclude any filename which matches them from inclusion in the test
732 741 suite (using pattern.search(), NOT pattern.match() ).
733 742 """
734 743
735 744 if exclude_patterns is None:
736 745 exclude_patterns = []
737 746 self.exclude_patterns = map(re.compile,exclude_patterns)
738 747 doctests.Doctest.__init__(self)
739 748
740 749 def options(self, parser, env=os.environ):
741 750 Plugin.options(self, parser, env)
742 751 parser.add_option('--doctest-tests', action='store_true',
743 752 dest='doctest_tests',
744 753 default=env.get('NOSE_DOCTEST_TESTS',True),
745 754 help="Also look for doctests in test modules. "
746 755 "Note that classes, methods and functions should "
747 756 "have either doctests or non-doctest tests, "
748 757 "not both. [NOSE_DOCTEST_TESTS]")
749 758 parser.add_option('--doctest-extension', action="append",
750 759 dest="doctestExtension",
751 760 help="Also look for doctests in files with "
752 761 "this extension [NOSE_DOCTEST_EXTENSION]")
753 762 # Set the default as a list, if given in env; otherwise
754 763 # an additional value set on the command line will cause
755 764 # an error.
756 765 env_setting = env.get('NOSE_DOCTEST_EXTENSION')
757 766 if env_setting is not None:
758 767 parser.set_defaults(doctestExtension=tolist(env_setting))
759 768
760 769
761 770 def configure(self, options, config):
762 771 Plugin.configure(self, options, config)
763 772 self.doctest_tests = options.doctest_tests
764 773 self.extension = tolist(options.doctestExtension)
765 774
766 775 self.parser = doctest.DocTestParser()
767 776 self.finder = DocTestFinder()
768 777 self.checker = IPDoctestOutputChecker()
769 778 self.globs = None
770 779 self.extraglobs = None
771 780
772 781
773 782 def loadTestsFromExtensionModule(self,filename):
774 783 bpath,mod = os.path.split(filename)
775 784 modname = os.path.splitext(mod)[0]
776 785 try:
777 786 sys.path.append(bpath)
778 787 module = __import__(modname)
779 788 tests = list(self.loadTestsFromModule(module))
780 789 finally:
781 790 sys.path.pop()
782 791 return tests
783 792
784 793 # NOTE: the method below is almost a copy of the original one in nose, with
785 794 # a few modifications to control output checking.
786 795
787 796 def loadTestsFromModule(self, module):
788 797 #print '*** ipdoctest - lTM',module # dbg
789 798
790 799 if not self.matches(module.__name__):
791 800 log.debug("Doctest doesn't want module %s", module)
792 801 return
793 802
794 803 tests = self.finder.find(module,globs=self.globs,
795 804 extraglobs=self.extraglobs)
796 805 if not tests:
797 806 return
798 807
799 808 # always use whitespace and ellipsis options
800 809 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
801 810
802 811 tests.sort()
803 812 module_file = module.__file__
804 813 if module_file[-4:] in ('.pyc', '.pyo'):
805 814 module_file = module_file[:-1]
806 815 for test in tests:
807 816 if not test.examples:
808 817 continue
809 818 if not test.filename:
810 819 test.filename = module_file
811 820
812 821 yield DocTestCase(test,
813 822 optionflags=optionflags,
814 823 checker=self.checker)
815 824
816 825
817 826 def loadTestsFromFile(self, filename):
818 827 if is_extension_module(filename):
819 828 for t in self.loadTestsFromExtensionModule(filename):
820 829 yield t
821 830 else:
822 831 if self.extension and anyp(filename.endswith, self.extension):
823 832 name = os.path.basename(filename)
824 833 dh = open(filename)
825 834 try:
826 835 doc = dh.read()
827 836 finally:
828 837 dh.close()
829 838 test = self.parser.get_doctest(
830 839 doc, globs={'__file__': filename}, name=name,
831 840 filename=filename, lineno=0)
832 841 if test.examples:
833 842 #print 'FileCase:',test.examples # dbg
834 843 yield DocFileCase(test)
835 844 else:
836 845 yield False # no tests to load
837 846
838 847 def wantFile(self,filename):
839 848 """Return whether the given filename should be scanned for tests.
840 849
841 850 Modified version that accepts extension modules as valid containers for
842 851 doctests.
843 852 """
844 853 # print '*** ipdoctest- wantFile:',filename # dbg
845 854
846 855 for pat in self.exclude_patterns:
847 856 if pat.search(filename):
848 857 # print '###>>> SKIP:',filename # dbg
849 858 return False
850 859
851 860 if is_extension_module(filename):
852 861 return True
853 862 else:
854 863 return doctests.Doctest.wantFile(self,filename)
855 864
856 865
857 866 class IPythonDoctest(ExtensionDoctest):
858 867 """Nose Plugin that supports doctests in extension modules.
859 868 """
860 869 name = 'ipdoctest' # call nosetests with --with-ipdoctest
861 870 enabled = True
862 871
863 872 def makeTest(self, obj, parent):
864 873 """Look for doctests in the given object, which will be a
865 874 function, method or class.
866 875 """
867 876 # always use whitespace and ellipsis options
868 877 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
869 878
870 879 doctests = self.finder.find(obj, module=getmodule(parent))
871 880 if doctests:
872 881 for test in doctests:
873 882 if len(test.examples) == 0:
874 883 continue
875 884
876 885 yield DocTestCase(test, obj=obj,
877 886 optionflags=optionflags,
878 887 checker=self.checker)
879 888
880 889 def options(self, parser, env=os.environ):
881 890 Plugin.options(self, parser, env)
882 891 parser.add_option('--ipdoctest-tests', action='store_true',
883 892 dest='ipdoctest_tests',
884 893 default=env.get('NOSE_IPDOCTEST_TESTS',True),
885 894 help="Also look for doctests in test modules. "
886 895 "Note that classes, methods and functions should "
887 896 "have either doctests or non-doctest tests, "
888 897 "not both. [NOSE_IPDOCTEST_TESTS]")
889 898 parser.add_option('--ipdoctest-extension', action="append",
890 899 dest="ipdoctest_extension",
891 900 help="Also look for doctests in files with "
892 901 "this extension [NOSE_IPDOCTEST_EXTENSION]")
893 902 # Set the default as a list, if given in env; otherwise
894 903 # an additional value set on the command line will cause
895 904 # an error.
896 905 env_setting = env.get('NOSE_IPDOCTEST_EXTENSION')
897 906 if env_setting is not None:
898 907 parser.set_defaults(ipdoctest_extension=tolist(env_setting))
899 908
900 909 def configure(self, options, config):
901 910 Plugin.configure(self, options, config)
902 911 self.doctest_tests = options.ipdoctest_tests
903 912 self.extension = tolist(options.ipdoctest_extension)
904 913
905 914 self.parser = IPDocTestParser()
906 915 self.finder = DocTestFinder(parser=self.parser)
907 916 self.checker = IPDoctestOutputChecker()
908 917 self.globs = None
909 918 self.extraglobs = None
@@ -1,2261 +1,2261 b''
1 1 # -*- coding: utf-8 -*-
2 2 """General purpose utilities.
3 3
4 4 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 5 these things are also convenient when working at the command line.
6 6 """
7 7
8 8 #*****************************************************************************
9 9 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #*****************************************************************************
14 14
15 15 #****************************************************************************
16 16 # required modules from the Python standard library
17 17 import __main__
18 18 import commands
19 19 try:
20 20 import doctest
21 21 except ImportError:
22 22 pass
23 23 import os
24 24 import platform
25 25 import re
26 26 import shlex
27 27 import shutil
28 28 import subprocess
29 29 import sys
30 30 import tempfile
31 31 import time
32 32 import types
33 33 import warnings
34 34
35 35 # Curses and termios are Unix-only modules
36 36 try:
37 37 import curses
38 38 # We need termios as well, so if its import happens to raise, we bail on
39 39 # using curses altogether.
40 40 import termios
41 41 except ImportError:
42 42 USE_CURSES = False
43 43 else:
44 44 # Curses on Solaris may not be complete, so we can't use it there
45 45 USE_CURSES = hasattr(curses,'initscr')
46 46
47 47 # Other IPython utilities
48 48 import IPython
49 49 from IPython.external.Itpl import Itpl,itpl,printpl
50 50 from IPython.utils import platutils
51 51 from IPython.utils import DPyGetOpt
52 52 from IPython.utils.generics import result_display
53 53 from IPython.core import ipapi
54 54 from IPython.external.path import path
55 55 if os.name == "nt":
56 56 from IPython.utils.winconsole import get_console_size
57 57
58 58 try:
59 59 set
60 60 except:
61 61 from sets import Set as set
62 62
63 63
64 64 #****************************************************************************
65 65 # Exceptions
66 66 class Error(Exception):
67 67 """Base class for exceptions in this module."""
68 68 pass
69 69
70 70 #----------------------------------------------------------------------------
71 71 class IOStream:
72 72 def __init__(self,stream,fallback):
73 73 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
74 74 stream = fallback
75 75 self.stream = stream
76 76 self._swrite = stream.write
77 77 self.flush = stream.flush
78 78
79 79 def write(self,data):
80 80 try:
81 81 self._swrite(data)
82 82 except:
83 83 try:
84 84 # print handles some unicode issues which may trip a plain
85 85 # write() call. Attempt to emulate write() by using a
86 86 # trailing comma
87 87 print >> self.stream, data,
88 88 except:
89 89 # if we get here, something is seriously broken.
90 90 print >> sys.stderr, \
91 91 'ERROR - failed to write data to stream:', self.stream
92 92
93 93 def close(self):
94 94 pass
95 95
96 96
97 97 class IOTerm:
98 98 """ Term holds the file or file-like objects for handling I/O operations.
99 99
100 100 These are normally just sys.stdin, sys.stdout and sys.stderr but for
101 101 Windows they can can replaced to allow editing the strings before they are
102 102 displayed."""
103 103
104 104 # In the future, having IPython channel all its I/O operations through
105 105 # this class will make it easier to embed it into other environments which
106 106 # are not a normal terminal (such as a GUI-based shell)
107 107 def __init__(self,cin=None,cout=None,cerr=None):
108 108 self.cin = IOStream(cin,sys.stdin)
109 109 self.cout = IOStream(cout,sys.stdout)
110 110 self.cerr = IOStream(cerr,sys.stderr)
111 111
112 112 # Global variable to be used for all I/O
113 113 Term = IOTerm()
114 114
115 115 import IPython.utils.rlineimpl as readline
116 116 # Remake Term to use the readline i/o facilities
117 117 if sys.platform == 'win32' and readline.have_readline:
118 118
119 119 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
120 120
121 121
122 122 #****************************************************************************
123 123 # Generic warning/error printer, used by everything else
124 124 def warn(msg,level=2,exit_val=1):
125 125 """Standard warning printer. Gives formatting consistency.
126 126
127 127 Output is sent to Term.cerr (sys.stderr by default).
128 128
129 129 Options:
130 130
131 131 -level(2): allows finer control:
132 132 0 -> Do nothing, dummy function.
133 133 1 -> Print message.
134 134 2 -> Print 'WARNING:' + message. (Default level).
135 135 3 -> Print 'ERROR:' + message.
136 136 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
137 137
138 138 -exit_val (1): exit value returned by sys.exit() for a level 4
139 139 warning. Ignored for all other levels."""
140 140
141 141 if level>0:
142 142 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
143 143 print >> Term.cerr, '%s%s' % (header[level],msg)
144 144 if level == 4:
145 145 print >> Term.cerr,'Exiting.\n'
146 146 sys.exit(exit_val)
147 147
148 148 def info(msg):
149 149 """Equivalent to warn(msg,level=1)."""
150 150
151 151 warn(msg,level=1)
152 152
153 153 def error(msg):
154 154 """Equivalent to warn(msg,level=3)."""
155 155
156 156 warn(msg,level=3)
157 157
158 158 def fatal(msg,exit_val=1):
159 159 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
160 160
161 161 warn(msg,exit_val=exit_val,level=4)
162 162
163 163 #---------------------------------------------------------------------------
164 164 # Debugging routines
165 165 #
166 166 def debugx(expr,pre_msg=''):
167 167 """Print the value of an expression from the caller's frame.
168 168
169 169 Takes an expression, evaluates it in the caller's frame and prints both
170 170 the given expression and the resulting value (as well as a debug mark
171 171 indicating the name of the calling function. The input must be of a form
172 172 suitable for eval().
173 173
174 174 An optional message can be passed, which will be prepended to the printed
175 175 expr->value pair."""
176 176
177 177 cf = sys._getframe(1)
178 178 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
179 179 eval(expr,cf.f_globals,cf.f_locals))
180 180
181 181 # deactivate it by uncommenting the following line, which makes it a no-op
182 182 #def debugx(expr,pre_msg=''): pass
183 183
184 184 #----------------------------------------------------------------------------
185 185 StringTypes = types.StringTypes
186 186
187 187 # Basic timing functionality
188 188
189 189 # If possible (Unix), use the resource module instead of time.clock()
190 190 try:
191 191 import resource
192 192 def clocku():
193 193 """clocku() -> floating point number
194 194
195 195 Return the *USER* CPU time in seconds since the start of the process.
196 196 This is done via a call to resource.getrusage, so it avoids the
197 197 wraparound problems in time.clock()."""
198 198
199 199 return resource.getrusage(resource.RUSAGE_SELF)[0]
200 200
201 201 def clocks():
202 202 """clocks() -> floating point number
203 203
204 204 Return the *SYSTEM* CPU time in seconds since the start of the process.
205 205 This is done via a call to resource.getrusage, so it avoids the
206 206 wraparound problems in time.clock()."""
207 207
208 208 return resource.getrusage(resource.RUSAGE_SELF)[1]
209 209
210 210 def clock():
211 211 """clock() -> floating point number
212 212
213 213 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
214 214 the process. This is done via a call to resource.getrusage, so it
215 215 avoids the wraparound problems in time.clock()."""
216 216
217 217 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
218 218 return u+s
219 219
220 220 def clock2():
221 221 """clock2() -> (t_user,t_system)
222 222
223 223 Similar to clock(), but return a tuple of user/system times."""
224 224 return resource.getrusage(resource.RUSAGE_SELF)[:2]
225 225
226 226 except ImportError:
227 227 # There is no distinction of user/system time under windows, so we just use
228 228 # time.clock() for everything...
229 229 clocku = clocks = clock = time.clock
230 230 def clock2():
231 231 """Under windows, system CPU time can't be measured.
232 232
233 233 This just returns clock() and zero."""
234 234 return time.clock(),0.0
235 235
236 236 def timings_out(reps,func,*args,**kw):
237 237 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
238 238
239 239 Execute a function reps times, return a tuple with the elapsed total
240 240 CPU time in seconds, the time per call and the function's output.
241 241
242 242 Under Unix, the return value is the sum of user+system time consumed by
243 243 the process, computed via the resource module. This prevents problems
244 244 related to the wraparound effect which the time.clock() function has.
245 245
246 246 Under Windows the return value is in wall clock seconds. See the
247 247 documentation for the time module for more details."""
248 248
249 249 reps = int(reps)
250 250 assert reps >=1, 'reps must be >= 1'
251 251 if reps==1:
252 252 start = clock()
253 253 out = func(*args,**kw)
254 254 tot_time = clock()-start
255 255 else:
256 256 rng = xrange(reps-1) # the last time is executed separately to store output
257 257 start = clock()
258 258 for dummy in rng: func(*args,**kw)
259 259 out = func(*args,**kw) # one last time
260 260 tot_time = clock()-start
261 261 av_time = tot_time / reps
262 262 return tot_time,av_time,out
263 263
264 264 def timings(reps,func,*args,**kw):
265 265 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
266 266
267 267 Execute a function reps times, return a tuple with the elapsed total CPU
268 268 time in seconds and the time per call. These are just the first two values
269 269 in timings_out()."""
270 270
271 271 return timings_out(reps,func,*args,**kw)[0:2]
272 272
273 273 def timing(func,*args,**kw):
274 274 """timing(func,*args,**kw) -> t_total
275 275
276 276 Execute a function once, return the elapsed total CPU time in
277 277 seconds. This is just the first value in timings_out()."""
278 278
279 279 return timings_out(1,func,*args,**kw)[0]
280 280
281 281 #****************************************************************************
282 282 # file and system
283 283
284 284 def arg_split(s,posix=False):
285 285 """Split a command line's arguments in a shell-like manner.
286 286
287 287 This is a modified version of the standard library's shlex.split()
288 288 function, but with a default of posix=False for splitting, so that quotes
289 289 in inputs are respected."""
290 290
291 291 # XXX - there may be unicode-related problems here!!! I'm not sure that
292 292 # shlex is truly unicode-safe, so it might be necessary to do
293 293 #
294 294 # s = s.encode(sys.stdin.encoding)
295 295 #
296 296 # first, to ensure that shlex gets a normal string. Input from anyone who
297 297 # knows more about unicode and shlex than I would be good to have here...
298 298 lex = shlex.shlex(s, posix=posix)
299 299 lex.whitespace_split = True
300 300 return list(lex)
301 301
302 302 def system(cmd,verbose=0,debug=0,header=''):
303 303 """Execute a system command, return its exit status.
304 304
305 305 Options:
306 306
307 307 - verbose (0): print the command to be executed.
308 308
309 309 - debug (0): only print, do not actually execute.
310 310
311 311 - header (''): Header to print on screen prior to the executed command (it
312 312 is only prepended to the command, no newlines are added).
313 313
314 314 Note: a stateful version of this function is available through the
315 315 SystemExec class."""
316 316
317 317 stat = 0
318 318 if verbose or debug: print header+cmd
319 319 sys.stdout.flush()
320 320 if not debug: stat = os.system(cmd)
321 321 return stat
322 322
323 323 def abbrev_cwd():
324 324 """ Return abbreviated version of cwd, e.g. d:mydir """
325 325 cwd = os.getcwd().replace('\\','/')
326 326 drivepart = ''
327 327 tail = cwd
328 328 if sys.platform == 'win32':
329 329 if len(cwd) < 4:
330 330 return cwd
331 331 drivepart,tail = os.path.splitdrive(cwd)
332 332
333 333
334 334 parts = tail.split('/')
335 335 if len(parts) > 2:
336 336 tail = '/'.join(parts[-2:])
337 337
338 338 return (drivepart + (
339 339 cwd == '/' and '/' or tail))
340 340
341 341
342 342 # This function is used by ipython in a lot of places to make system calls.
343 343 # We need it to be slightly different under win32, due to the vagaries of
344 344 # 'network shares'. A win32 override is below.
345 345
346 346 def shell(cmd,verbose=0,debug=0,header=''):
347 347 """Execute a command in the system shell, always return None.
348 348
349 349 Options:
350 350
351 351 - verbose (0): print the command to be executed.
352 352
353 353 - debug (0): only print, do not actually execute.
354 354
355 355 - header (''): Header to print on screen prior to the executed command (it
356 356 is only prepended to the command, no newlines are added).
357 357
358 358 Note: this is similar to genutils.system(), but it returns None so it can
359 359 be conveniently used in interactive loops without getting the return value
360 360 (typically 0) printed many times."""
361 361
362 362 stat = 0
363 363 if verbose or debug: print header+cmd
364 364 # flush stdout so we don't mangle python's buffering
365 365 sys.stdout.flush()
366 366
367 367 if not debug:
368 368 platutils.set_term_title("IPy " + cmd)
369 369 os.system(cmd)
370 370 platutils.set_term_title("IPy " + abbrev_cwd())
371 371
372 372 # override shell() for win32 to deal with network shares
373 373 if os.name in ('nt','dos'):
374 374
375 375 shell_ori = shell
376 376
377 377 def shell(cmd,verbose=0,debug=0,header=''):
378 378 if os.getcwd().startswith(r"\\"):
379 379 path = os.getcwd()
380 380 # change to c drive (cannot be on UNC-share when issuing os.system,
381 381 # as cmd.exe cannot handle UNC addresses)
382 382 os.chdir("c:")
383 383 # issue pushd to the UNC-share and then run the command
384 384 try:
385 385 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
386 386 finally:
387 387 os.chdir(path)
388 388 else:
389 389 shell_ori(cmd,verbose,debug,header)
390 390
391 391 shell.__doc__ = shell_ori.__doc__
392 392
393 393 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
394 394 """Dummy substitute for perl's backquotes.
395 395
396 396 Executes a command and returns the output.
397 397
398 398 Accepts the same arguments as system(), plus:
399 399
400 400 - split(0): if true, the output is returned as a list split on newlines.
401 401
402 402 Note: a stateful version of this function is available through the
403 403 SystemExec class.
404 404
405 405 This is pretty much deprecated and rarely used,
406 406 genutils.getoutputerror may be what you need.
407 407
408 408 """
409 409
410 410 if verbose or debug: print header+cmd
411 411 if not debug:
412 412 output = os.popen(cmd).read()
413 413 # stipping last \n is here for backwards compat.
414 414 if output.endswith('\n'):
415 415 output = output[:-1]
416 416 if split:
417 417 return output.split('\n')
418 418 else:
419 419 return output
420 420
421 421 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
422 422 """Return (standard output,standard error) of executing cmd in a shell.
423 423
424 424 Accepts the same arguments as system(), plus:
425 425
426 426 - split(0): if true, each of stdout/err is returned as a list split on
427 427 newlines.
428 428
429 429 Note: a stateful version of this function is available through the
430 430 SystemExec class."""
431 431
432 432 if verbose or debug: print header+cmd
433 433 if not cmd:
434 434 if split:
435 435 return [],[]
436 436 else:
437 437 return '',''
438 438 if not debug:
439 439 pin,pout,perr = os.popen3(cmd)
440 440 tout = pout.read().rstrip()
441 441 terr = perr.read().rstrip()
442 442 pin.close()
443 443 pout.close()
444 444 perr.close()
445 445 if split:
446 446 return tout.split('\n'),terr.split('\n')
447 447 else:
448 448 return tout,terr
449 449
450 450 # for compatibility with older naming conventions
451 451 xsys = system
452 452 bq = getoutput
453 453
454 454 class SystemExec:
455 455 """Access the system and getoutput functions through a stateful interface.
456 456
457 457 Note: here we refer to the system and getoutput functions from this
458 458 library, not the ones from the standard python library.
459 459
460 460 This class offers the system and getoutput functions as methods, but the
461 461 verbose, debug and header parameters can be set for the instance (at
462 462 creation time or later) so that they don't need to be specified on each
463 463 call.
464 464
465 465 For efficiency reasons, there's no way to override the parameters on a
466 466 per-call basis other than by setting instance attributes. If you need
467 467 local overrides, it's best to directly call system() or getoutput().
468 468
469 469 The following names are provided as alternate options:
470 470 - xsys: alias to system
471 471 - bq: alias to getoutput
472 472
473 473 An instance can then be created as:
474 474 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
475 475 """
476 476
477 477 def __init__(self,verbose=0,debug=0,header='',split=0):
478 478 """Specify the instance's values for verbose, debug and header."""
479 479 setattr_list(self,'verbose debug header split')
480 480
481 481 def system(self,cmd):
482 482 """Stateful interface to system(), with the same keyword parameters."""
483 483
484 484 system(cmd,self.verbose,self.debug,self.header)
485 485
486 486 def shell(self,cmd):
487 487 """Stateful interface to shell(), with the same keyword parameters."""
488 488
489 489 shell(cmd,self.verbose,self.debug,self.header)
490 490
491 491 xsys = system # alias
492 492
493 493 def getoutput(self,cmd):
494 494 """Stateful interface to getoutput()."""
495 495
496 496 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
497 497
498 498 def getoutputerror(self,cmd):
499 499 """Stateful interface to getoutputerror()."""
500 500
501 501 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
502 502
503 503 bq = getoutput # alias
504 504
505 505 #-----------------------------------------------------------------------------
506 506 def mutex_opts(dict,ex_op):
507 507 """Check for presence of mutually exclusive keys in a dict.
508 508
509 509 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
510 510 for op1,op2 in ex_op:
511 511 if op1 in dict and op2 in dict:
512 512 raise ValueError,'\n*** ERROR in Arguments *** '\
513 513 'Options '+op1+' and '+op2+' are mutually exclusive.'
514 514
515 515 #-----------------------------------------------------------------------------
516 516 def get_py_filename(name):
517 517 """Return a valid python filename in the current directory.
518 518
519 519 If the given name is not a file, it adds '.py' and searches again.
520 520 Raises IOError with an informative message if the file isn't found."""
521 521
522 522 name = os.path.expanduser(name)
523 523 if not os.path.isfile(name) and not name.endswith('.py'):
524 524 name += '.py'
525 525 if os.path.isfile(name):
526 526 return name
527 527 else:
528 528 raise IOError,'File `%s` not found.' % name
529 529
530 530 #-----------------------------------------------------------------------------
531 531 def filefind(fname,alt_dirs = None):
532 532 """Return the given filename either in the current directory, if it
533 533 exists, or in a specified list of directories.
534 534
535 535 ~ expansion is done on all file and directory names.
536 536
537 537 Upon an unsuccessful search, raise an IOError exception."""
538 538
539 539 if alt_dirs is None:
540 540 try:
541 541 alt_dirs = get_home_dir()
542 542 except HomeDirError:
543 543 alt_dirs = os.getcwd()
544 544 search = [fname] + list_strings(alt_dirs)
545 545 search = map(os.path.expanduser,search)
546 546 #print 'search list for',fname,'list:',search # dbg
547 547 fname = search[0]
548 548 if os.path.isfile(fname):
549 549 return fname
550 550 for direc in search[1:]:
551 551 testname = os.path.join(direc,fname)
552 552 #print 'testname',testname # dbg
553 553 if os.path.isfile(testname):
554 554 return testname
555 555 raise IOError,'File' + `fname` + \
556 556 ' not found in current or supplied directories:' + `alt_dirs`
557 557
558 558 #----------------------------------------------------------------------------
559 559 def file_read(filename):
560 560 """Read a file and close it. Returns the file source."""
561 561 fobj = open(filename,'r');
562 562 source = fobj.read();
563 563 fobj.close()
564 564 return source
565 565
566 566 def file_readlines(filename):
567 567 """Read a file and close it. Returns the file source using readlines()."""
568 568 fobj = open(filename,'r');
569 569 lines = fobj.readlines();
570 570 fobj.close()
571 571 return lines
572 572
573 573 #----------------------------------------------------------------------------
574 574 def target_outdated(target,deps):
575 575 """Determine whether a target is out of date.
576 576
577 577 target_outdated(target,deps) -> 1/0
578 578
579 579 deps: list of filenames which MUST exist.
580 580 target: single filename which may or may not exist.
581 581
582 582 If target doesn't exist or is older than any file listed in deps, return
583 583 true, otherwise return false.
584 584 """
585 585 try:
586 586 target_time = os.path.getmtime(target)
587 587 except os.error:
588 588 return 1
589 589 for dep in deps:
590 590 dep_time = os.path.getmtime(dep)
591 591 if dep_time > target_time:
592 592 #print "For target",target,"Dep failed:",dep # dbg
593 593 #print "times (dep,tar):",dep_time,target_time # dbg
594 594 return 1
595 595 return 0
596 596
597 597 #-----------------------------------------------------------------------------
598 598 def target_update(target,deps,cmd):
599 599 """Update a target with a given command given a list of dependencies.
600 600
601 601 target_update(target,deps,cmd) -> runs cmd if target is outdated.
602 602
603 603 This is just a wrapper around target_outdated() which calls the given
604 604 command if target is outdated."""
605 605
606 606 if target_outdated(target,deps):
607 607 xsys(cmd)
608 608
609 609 #----------------------------------------------------------------------------
610 610 def unquote_ends(istr):
611 611 """Remove a single pair of quotes from the endpoints of a string."""
612 612
613 613 if not istr:
614 614 return istr
615 615 if (istr[0]=="'" and istr[-1]=="'") or \
616 616 (istr[0]=='"' and istr[-1]=='"'):
617 617 return istr[1:-1]
618 618 else:
619 619 return istr
620 620
621 621 #----------------------------------------------------------------------------
622 622 def process_cmdline(argv,names=[],defaults={},usage=''):
623 623 """ Process command-line options and arguments.
624 624
625 625 Arguments:
626 626
627 627 - argv: list of arguments, typically sys.argv.
628 628
629 629 - names: list of option names. See DPyGetOpt docs for details on options
630 630 syntax.
631 631
632 632 - defaults: dict of default values.
633 633
634 634 - usage: optional usage notice to print if a wrong argument is passed.
635 635
636 636 Return a dict of options and a list of free arguments."""
637 637
638 638 getopt = DPyGetOpt.DPyGetOpt()
639 639 getopt.setIgnoreCase(0)
640 640 getopt.parseConfiguration(names)
641 641
642 642 try:
643 643 getopt.processArguments(argv)
644 644 except DPyGetOpt.ArgumentError, exc:
645 645 print usage
646 646 warn('"%s"' % exc,level=4)
647 647
648 648 defaults.update(getopt.optionValues)
649 649 args = getopt.freeValues
650 650
651 651 return defaults,args
652 652
653 653 #----------------------------------------------------------------------------
654 654 def optstr2types(ostr):
655 655 """Convert a string of option names to a dict of type mappings.
656 656
657 657 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
658 658
659 659 This is used to get the types of all the options in a string formatted
660 660 with the conventions of DPyGetOpt. The 'type' None is used for options
661 661 which are strings (they need no further conversion). This function's main
662 662 use is to get a typemap for use with read_dict().
663 663 """
664 664
665 665 typeconv = {None:'',int:'',float:''}
666 666 typemap = {'s':None,'i':int,'f':float}
667 667 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
668 668
669 669 for w in ostr.split():
670 670 oname,alias,otype = opt_re.match(w).groups()
671 671 if otype == '' or alias == '!': # simple switches are integers too
672 672 otype = 'i'
673 673 typeconv[typemap[otype]] += oname + ' '
674 674 return typeconv
675 675
676 676 #----------------------------------------------------------------------------
677 677 def read_dict(filename,type_conv=None,**opt):
678 678 r"""Read a dictionary of key=value pairs from an input file, optionally
679 679 performing conversions on the resulting values.
680 680
681 681 read_dict(filename,type_conv,**opt) -> dict
682 682
683 683 Only one value per line is accepted, the format should be
684 684 # optional comments are ignored
685 685 key value\n
686 686
687 687 Args:
688 688
689 689 - type_conv: A dictionary specifying which keys need to be converted to
690 690 which types. By default all keys are read as strings. This dictionary
691 691 should have as its keys valid conversion functions for strings
692 692 (int,long,float,complex, or your own). The value for each key
693 693 (converter) should be a whitespace separated string containing the names
694 694 of all the entries in the file to be converted using that function. For
695 695 keys to be left alone, use None as the conversion function (only needed
696 696 with purge=1, see below).
697 697
698 698 - opt: dictionary with extra options as below (default in parens)
699 699
700 700 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
701 701 of the dictionary to be returned. If purge is going to be used, the
702 702 set of keys to be left as strings also has to be explicitly specified
703 703 using the (non-existent) conversion function None.
704 704
705 705 fs(None): field separator. This is the key/value separator to be used
706 706 when parsing the file. The None default means any whitespace [behavior
707 707 of string.split()].
708 708
709 709 strip(0): if 1, strip string values of leading/trailinig whitespace.
710 710
711 711 warn(1): warning level if requested keys are not found in file.
712 712 - 0: silently ignore.
713 713 - 1: inform but proceed.
714 714 - 2: raise KeyError exception.
715 715
716 716 no_empty(0): if 1, remove keys with whitespace strings as a value.
717 717
718 718 unique([]): list of keys (or space separated string) which can't be
719 719 repeated. If one such key is found in the file, each new instance
720 720 overwrites the previous one. For keys not listed here, the behavior is
721 721 to make a list of all appearances.
722 722
723 723 Example:
724 724
725 725 If the input file test.ini contains (we put it in a string to keep the test
726 726 self-contained):
727 727
728 728 >>> test_ini = '''\
729 729 ... i 3
730 730 ... x 4.5
731 731 ... y 5.5
732 732 ... s hi ho'''
733 733
734 734 Then we can use it as follows:
735 735 >>> type_conv={int:'i',float:'x',None:'s'}
736 736
737 737 >>> d = read_dict(test_ini)
738 738
739 739 >>> sorted(d.items())
740 740 [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')]
741 741
742 742 >>> d = read_dict(test_ini,type_conv)
743 743
744 744 >>> sorted(d.items())
745 745 [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')]
746 746
747 747 >>> d = read_dict(test_ini,type_conv,purge=True)
748 748
749 749 >>> sorted(d.items())
750 750 [('i', 3), ('s', 'hi ho'), ('x', 4.5)]
751 751 """
752 752
753 753 # starting config
754 754 opt.setdefault('purge',0)
755 755 opt.setdefault('fs',None) # field sep defaults to any whitespace
756 756 opt.setdefault('strip',0)
757 757 opt.setdefault('warn',1)
758 758 opt.setdefault('no_empty',0)
759 759 opt.setdefault('unique','')
760 760 if type(opt['unique']) in StringTypes:
761 761 unique_keys = qw(opt['unique'])
762 762 elif type(opt['unique']) in (types.TupleType,types.ListType):
763 763 unique_keys = opt['unique']
764 764 else:
765 765 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
766 766
767 767 dict = {}
768 768
769 769 # first read in table of values as strings
770 770 if '\n' in filename:
771 771 lines = filename.splitlines()
772 772 file = None
773 773 else:
774 774 file = open(filename,'r')
775 775 lines = file.readlines()
776 776 for line in lines:
777 777 line = line.strip()
778 778 if len(line) and line[0]=='#': continue
779 779 if len(line)>0:
780 780 lsplit = line.split(opt['fs'],1)
781 781 try:
782 782 key,val = lsplit
783 783 except ValueError:
784 784 key,val = lsplit[0],''
785 785 key = key.strip()
786 786 if opt['strip']: val = val.strip()
787 787 if val == "''" or val == '""': val = ''
788 788 if opt['no_empty'] and (val=='' or val.isspace()):
789 789 continue
790 790 # if a key is found more than once in the file, build a list
791 791 # unless it's in the 'unique' list. In that case, last found in file
792 792 # takes precedence. User beware.
793 793 try:
794 794 if dict[key] and key in unique_keys:
795 795 dict[key] = val
796 796 elif type(dict[key]) is types.ListType:
797 797 dict[key].append(val)
798 798 else:
799 799 dict[key] = [dict[key],val]
800 800 except KeyError:
801 801 dict[key] = val
802 802 # purge if requested
803 803 if opt['purge']:
804 804 accepted_keys = qwflat(type_conv.values())
805 805 for key in dict.keys():
806 806 if key in accepted_keys: continue
807 807 del(dict[key])
808 808 # now convert if requested
809 809 if type_conv==None: return dict
810 810 conversions = type_conv.keys()
811 811 try: conversions.remove(None)
812 812 except: pass
813 813 for convert in conversions:
814 814 for val in qw(type_conv[convert]):
815 815 try:
816 816 dict[val] = convert(dict[val])
817 817 except KeyError,e:
818 818 if opt['warn'] == 0:
819 819 pass
820 820 elif opt['warn'] == 1:
821 821 print >>sys.stderr, 'Warning: key',val,\
822 822 'not found in file',filename
823 823 elif opt['warn'] == 2:
824 824 raise KeyError,e
825 825 else:
826 826 raise ValueError,'Warning level must be 0,1 or 2'
827 827
828 828 return dict
829 829
830 830 #----------------------------------------------------------------------------
831 831 def flag_calls(func):
832 832 """Wrap a function to detect and flag when it gets called.
833 833
834 834 This is a decorator which takes a function and wraps it in a function with
835 835 a 'called' attribute. wrapper.called is initialized to False.
836 836
837 837 The wrapper.called attribute is set to False right before each call to the
838 838 wrapped function, so if the call fails it remains False. After the call
839 839 completes, wrapper.called is set to True and the output is returned.
840 840
841 841 Testing for truth in wrapper.called allows you to determine if a call to
842 842 func() was attempted and succeeded."""
843 843
844 844 def wrapper(*args,**kw):
845 845 wrapper.called = False
846 846 out = func(*args,**kw)
847 847 wrapper.called = True
848 848 return out
849 849
850 850 wrapper.called = False
851 851 wrapper.__doc__ = func.__doc__
852 852 return wrapper
853 853
854 854 #----------------------------------------------------------------------------
855 855 def dhook_wrap(func,*a,**k):
856 856 """Wrap a function call in a sys.displayhook controller.
857 857
858 858 Returns a wrapper around func which calls func, with all its arguments and
859 859 keywords unmodified, using the default sys.displayhook. Since IPython
860 860 modifies sys.displayhook, it breaks the behavior of certain systems that
861 861 rely on the default behavior, notably doctest.
862 862 """
863 863
864 864 def f(*a,**k):
865 865
866 866 dhook_s = sys.displayhook
867 867 sys.displayhook = sys.__displayhook__
868 868 try:
869 869 out = func(*a,**k)
870 870 finally:
871 871 sys.displayhook = dhook_s
872 872
873 873 return out
874 874
875 875 f.__doc__ = func.__doc__
876 876 return f
877 877
878 878 #----------------------------------------------------------------------------
879 879 def doctest_reload():
880 880 """Properly reload doctest to reuse it interactively.
881 881
882 882 This routine:
883 883
884 - reloads doctest
884 - imports doctest but does NOT reload it (see below).
885 885
886 886 - resets its global 'master' attribute to None, so that multiple uses of
887 887 the module interactively don't produce cumulative reports.
888 888
889 889 - Monkeypatches its core test runner method to protect it from IPython's
890 890 modified displayhook. Doctest expects the default displayhook behavior
891 891 deep down, so our modification breaks it completely. For this reason, a
892 892 hard monkeypatch seems like a reasonable solution rather than asking
893 users to manually use a different doctest runner when under IPython."""
893 users to manually use a different doctest runner when under IPython.
894 894
895 import doctest
896 reload(doctest)
897 doctest.master=None
895 Note
896 ----
898 897
899 try:
900 doctest.DocTestRunner
901 except AttributeError:
902 # This is only for python 2.3 compatibility, remove once we move to
903 # 2.4 only.
904 pass
905 else:
906 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
898 This function *used to* reload doctest, but this has been disabled because
899 reloading doctest unconditionally can cause massive breakage of other
900 doctest-dependent modules already in memory, such as those for IPython's
901 own testing system. The name wasn't changed to avoid breaking people's
902 code, but the reload call isn't actually made anymore."""
903
904 import doctest
905 doctest.master = None
906 doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run)
907 907
908 908 #----------------------------------------------------------------------------
909 909 class HomeDirError(Error):
910 910 pass
911 911
912 912 def get_home_dir():
913 913 """Return the closest possible equivalent to a 'home' directory.
914 914
915 915 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
916 916
917 917 Currently only Posix and NT are implemented, a HomeDirError exception is
918 918 raised for all other OSes. """
919 919
920 920 isdir = os.path.isdir
921 921 env = os.environ
922 922
923 923 # first, check py2exe distribution root directory for _ipython.
924 924 # This overrides all. Normally does not exist.
925 925
926 926 if hasattr(sys, "frozen"): #Is frozen by py2exe
927 927 if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
928 928 root, rest = IPython.__file__.lower().split('library.zip')
929 929 else:
930 930 root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
931 931 root=os.path.abspath(root).rstrip('\\')
932 932 if isdir(os.path.join(root, '_ipython')):
933 933 os.environ["IPYKITROOT"] = root
934 934 return root
935 935 try:
936 936 homedir = env['HOME']
937 937 if not isdir(homedir):
938 938 # in case a user stuck some string which does NOT resolve to a
939 939 # valid path, it's as good as if we hadn't foud it
940 940 raise KeyError
941 941 return homedir
942 942 except KeyError:
943 943 if os.name == 'posix':
944 944 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
945 945 elif os.name == 'nt':
946 946 # For some strange reason, win9x returns 'nt' for os.name.
947 947 try:
948 948 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
949 949 if not isdir(homedir):
950 950 homedir = os.path.join(env['USERPROFILE'])
951 951 if not isdir(homedir):
952 952 raise HomeDirError
953 953 return homedir
954 954 except KeyError:
955 955 try:
956 956 # Use the registry to get the 'My Documents' folder.
957 957 import _winreg as wreg
958 958 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
959 959 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
960 960 homedir = wreg.QueryValueEx(key,'Personal')[0]
961 961 key.Close()
962 962 if not isdir(homedir):
963 963 e = ('Invalid "Personal" folder registry key '
964 964 'typically "My Documents".\n'
965 965 'Value: %s\n'
966 966 'This is not a valid directory on your system.' %
967 967 homedir)
968 968 raise HomeDirError(e)
969 969 return homedir
970 970 except HomeDirError:
971 971 raise
972 972 except:
973 973 return 'C:\\'
974 974 elif os.name == 'dos':
975 975 # Desperate, may do absurd things in classic MacOS. May work under DOS.
976 976 return 'C:\\'
977 977 else:
978 978 raise HomeDirError,'support for your operating system not implemented.'
979 979
980 980
981 981 def get_ipython_dir():
982 982 """Get the IPython directory for this platform and user.
983 983
984 984 This uses the logic in `get_home_dir` to find the home directory
985 985 and the adds either .ipython or _ipython to the end of the path.
986 986 """
987 987 if os.name == 'posix':
988 988 ipdir_def = '.ipython'
989 989 else:
990 990 ipdir_def = '_ipython'
991 991 home_dir = get_home_dir()
992 992 ipdir = os.path.abspath(os.environ.get('IPYTHONDIR',
993 993 os.path.join(home_dir, ipdir_def)))
994 994 return ipdir.decode(sys.getfilesystemencoding())
995 995
996 996 def get_security_dir():
997 997 """Get the IPython security directory.
998 998
999 999 This directory is the default location for all security related files,
1000 1000 including SSL/TLS certificates and FURL files.
1001 1001
1002 1002 If the directory does not exist, it is created with 0700 permissions.
1003 1003 If it exists, permissions are set to 0700.
1004 1004 """
1005 1005 security_dir = os.path.join(get_ipython_dir(), 'security')
1006 1006 if not os.path.isdir(security_dir):
1007 1007 os.mkdir(security_dir, 0700)
1008 1008 else:
1009 1009 os.chmod(security_dir, 0700)
1010 1010 return security_dir
1011 1011
1012 1012 def get_log_dir():
1013 1013 """Get the IPython log directory.
1014 1014
1015 1015 If the log directory does not exist, it is created.
1016 1016 """
1017 1017 log_dir = os.path.join(get_ipython_dir(), 'log')
1018 1018 if not os.path.isdir(log_dir):
1019 1019 os.mkdir(log_dir, 0777)
1020 1020 return log_dir
1021 1021
1022 1022 #****************************************************************************
1023 1023 # strings and text
1024 1024
1025 1025 class LSString(str):
1026 1026 """String derivative with a special access attributes.
1027 1027
1028 1028 These are normal strings, but with the special attributes:
1029 1029
1030 1030 .l (or .list) : value as list (split on newlines).
1031 1031 .n (or .nlstr): original value (the string itself).
1032 1032 .s (or .spstr): value as whitespace-separated string.
1033 1033 .p (or .paths): list of path objects
1034 1034
1035 1035 Any values which require transformations are computed only once and
1036 1036 cached.
1037 1037
1038 1038 Such strings are very useful to efficiently interact with the shell, which
1039 1039 typically only understands whitespace-separated options for commands."""
1040 1040
1041 1041 def get_list(self):
1042 1042 try:
1043 1043 return self.__list
1044 1044 except AttributeError:
1045 1045 self.__list = self.split('\n')
1046 1046 return self.__list
1047 1047
1048 1048 l = list = property(get_list)
1049 1049
1050 1050 def get_spstr(self):
1051 1051 try:
1052 1052 return self.__spstr
1053 1053 except AttributeError:
1054 1054 self.__spstr = self.replace('\n',' ')
1055 1055 return self.__spstr
1056 1056
1057 1057 s = spstr = property(get_spstr)
1058 1058
1059 1059 def get_nlstr(self):
1060 1060 return self
1061 1061
1062 1062 n = nlstr = property(get_nlstr)
1063 1063
1064 1064 def get_paths(self):
1065 1065 try:
1066 1066 return self.__paths
1067 1067 except AttributeError:
1068 1068 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
1069 1069 return self.__paths
1070 1070
1071 1071 p = paths = property(get_paths)
1072 1072
1073 1073 def print_lsstring(arg):
1074 1074 """ Prettier (non-repr-like) and more informative printer for LSString """
1075 1075 print "LSString (.p, .n, .l, .s available). Value:"
1076 1076 print arg
1077 1077
1078 1078 print_lsstring = result_display.when_type(LSString)(print_lsstring)
1079 1079
1080 1080 #----------------------------------------------------------------------------
1081 1081 class SList(list):
1082 1082 """List derivative with a special access attributes.
1083 1083
1084 1084 These are normal lists, but with the special attributes:
1085 1085
1086 1086 .l (or .list) : value as list (the list itself).
1087 1087 .n (or .nlstr): value as a string, joined on newlines.
1088 1088 .s (or .spstr): value as a string, joined on spaces.
1089 1089 .p (or .paths): list of path objects
1090 1090
1091 1091 Any values which require transformations are computed only once and
1092 1092 cached."""
1093 1093
1094 1094 def get_list(self):
1095 1095 return self
1096 1096
1097 1097 l = list = property(get_list)
1098 1098
1099 1099 def get_spstr(self):
1100 1100 try:
1101 1101 return self.__spstr
1102 1102 except AttributeError:
1103 1103 self.__spstr = ' '.join(self)
1104 1104 return self.__spstr
1105 1105
1106 1106 s = spstr = property(get_spstr)
1107 1107
1108 1108 def get_nlstr(self):
1109 1109 try:
1110 1110 return self.__nlstr
1111 1111 except AttributeError:
1112 1112 self.__nlstr = '\n'.join(self)
1113 1113 return self.__nlstr
1114 1114
1115 1115 n = nlstr = property(get_nlstr)
1116 1116
1117 1117 def get_paths(self):
1118 1118 try:
1119 1119 return self.__paths
1120 1120 except AttributeError:
1121 1121 self.__paths = [path(p) for p in self if os.path.exists(p)]
1122 1122 return self.__paths
1123 1123
1124 1124 p = paths = property(get_paths)
1125 1125
1126 1126 def grep(self, pattern, prune = False, field = None):
1127 1127 """ Return all strings matching 'pattern' (a regex or callable)
1128 1128
1129 1129 This is case-insensitive. If prune is true, return all items
1130 1130 NOT matching the pattern.
1131 1131
1132 1132 If field is specified, the match must occur in the specified
1133 1133 whitespace-separated field.
1134 1134
1135 1135 Examples::
1136 1136
1137 1137 a.grep( lambda x: x.startswith('C') )
1138 1138 a.grep('Cha.*log', prune=1)
1139 1139 a.grep('chm', field=-1)
1140 1140 """
1141 1141
1142 1142 def match_target(s):
1143 1143 if field is None:
1144 1144 return s
1145 1145 parts = s.split()
1146 1146 try:
1147 1147 tgt = parts[field]
1148 1148 return tgt
1149 1149 except IndexError:
1150 1150 return ""
1151 1151
1152 1152 if isinstance(pattern, basestring):
1153 1153 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
1154 1154 else:
1155 1155 pred = pattern
1156 1156 if not prune:
1157 1157 return SList([el for el in self if pred(match_target(el))])
1158 1158 else:
1159 1159 return SList([el for el in self if not pred(match_target(el))])
1160 1160 def fields(self, *fields):
1161 1161 """ Collect whitespace-separated fields from string list
1162 1162
1163 1163 Allows quick awk-like usage of string lists.
1164 1164
1165 1165 Example data (in var a, created by 'a = !ls -l')::
1166 1166 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
1167 1167 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
1168 1168
1169 1169 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
1170 1170 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
1171 1171 (note the joining by space).
1172 1172 a.fields(-1) is ['ChangeLog', 'IPython']
1173 1173
1174 1174 IndexErrors are ignored.
1175 1175
1176 1176 Without args, fields() just split()'s the strings.
1177 1177 """
1178 1178 if len(fields) == 0:
1179 1179 return [el.split() for el in self]
1180 1180
1181 1181 res = SList()
1182 1182 for el in [f.split() for f in self]:
1183 1183 lineparts = []
1184 1184
1185 1185 for fd in fields:
1186 1186 try:
1187 1187 lineparts.append(el[fd])
1188 1188 except IndexError:
1189 1189 pass
1190 1190 if lineparts:
1191 1191 res.append(" ".join(lineparts))
1192 1192
1193 1193 return res
1194 1194 def sort(self,field= None, nums = False):
1195 1195 """ sort by specified fields (see fields())
1196 1196
1197 1197 Example::
1198 1198 a.sort(1, nums = True)
1199 1199
1200 1200 Sorts a by second field, in numerical order (so that 21 > 3)
1201 1201
1202 1202 """
1203 1203
1204 1204 #decorate, sort, undecorate
1205 1205 if field is not None:
1206 1206 dsu = [[SList([line]).fields(field), line] for line in self]
1207 1207 else:
1208 1208 dsu = [[line, line] for line in self]
1209 1209 if nums:
1210 1210 for i in range(len(dsu)):
1211 1211 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
1212 1212 try:
1213 1213 n = int(numstr)
1214 1214 except ValueError:
1215 1215 n = 0;
1216 1216 dsu[i][0] = n
1217 1217
1218 1218
1219 1219 dsu.sort()
1220 1220 return SList([t[1] for t in dsu])
1221 1221
1222 1222 def print_slist(arg):
1223 1223 """ Prettier (non-repr-like) and more informative printer for SList """
1224 1224 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
1225 1225 if hasattr(arg, 'hideonce') and arg.hideonce:
1226 1226 arg.hideonce = False
1227 1227 return
1228 1228
1229 1229 nlprint(arg)
1230 1230
1231 1231 print_slist = result_display.when_type(SList)(print_slist)
1232 1232
1233 1233
1234 1234
1235 1235 #----------------------------------------------------------------------------
1236 1236 def esc_quotes(strng):
1237 1237 """Return the input string with single and double quotes escaped out"""
1238 1238
1239 1239 return strng.replace('"','\\"').replace("'","\\'")
1240 1240
1241 1241 #----------------------------------------------------------------------------
1242 1242 def make_quoted_expr(s):
1243 1243 """Return string s in appropriate quotes, using raw string if possible.
1244 1244
1245 1245 XXX - example removed because it caused encoding errors in documentation
1246 1246 generation. We need a new example that doesn't contain invalid chars.
1247 1247
1248 1248 Note the use of raw string and padding at the end to allow trailing
1249 1249 backslash.
1250 1250 """
1251 1251
1252 1252 tail = ''
1253 1253 tailpadding = ''
1254 1254 raw = ''
1255 1255 if "\\" in s:
1256 1256 raw = 'r'
1257 1257 if s.endswith('\\'):
1258 1258 tail = '[:-1]'
1259 1259 tailpadding = '_'
1260 1260 if '"' not in s:
1261 1261 quote = '"'
1262 1262 elif "'" not in s:
1263 1263 quote = "'"
1264 1264 elif '"""' not in s and not s.endswith('"'):
1265 1265 quote = '"""'
1266 1266 elif "'''" not in s and not s.endswith("'"):
1267 1267 quote = "'''"
1268 1268 else:
1269 1269 # give up, backslash-escaped string will do
1270 1270 return '"%s"' % esc_quotes(s)
1271 1271 res = raw + quote + s + tailpadding + quote + tail
1272 1272 return res
1273 1273
1274 1274
1275 1275 #----------------------------------------------------------------------------
1276 1276 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1277 1277 """Take multiple lines of input.
1278 1278
1279 1279 A list with each line of input as a separate element is returned when a
1280 1280 termination string is entered (defaults to a single '.'). Input can also
1281 1281 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1282 1282
1283 1283 Lines of input which end in \\ are joined into single entries (and a
1284 1284 secondary continuation prompt is issued as long as the user terminates
1285 1285 lines with \\). This allows entering very long strings which are still
1286 1286 meant to be treated as single entities.
1287 1287 """
1288 1288
1289 1289 try:
1290 1290 if header:
1291 1291 header += '\n'
1292 1292 lines = [raw_input(header + ps1)]
1293 1293 except EOFError:
1294 1294 return []
1295 1295 terminate = [terminate_str]
1296 1296 try:
1297 1297 while lines[-1:] != terminate:
1298 1298 new_line = raw_input(ps1)
1299 1299 while new_line.endswith('\\'):
1300 1300 new_line = new_line[:-1] + raw_input(ps2)
1301 1301 lines.append(new_line)
1302 1302
1303 1303 return lines[:-1] # don't return the termination command
1304 1304 except EOFError:
1305 1305 print
1306 1306 return lines
1307 1307
1308 1308 #----------------------------------------------------------------------------
1309 1309 def raw_input_ext(prompt='', ps2='... '):
1310 1310 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1311 1311
1312 1312 line = raw_input(prompt)
1313 1313 while line.endswith('\\'):
1314 1314 line = line[:-1] + raw_input(ps2)
1315 1315 return line
1316 1316
1317 1317 #----------------------------------------------------------------------------
1318 1318 def ask_yes_no(prompt,default=None):
1319 1319 """Asks a question and returns a boolean (y/n) answer.
1320 1320
1321 1321 If default is given (one of 'y','n'), it is used if the user input is
1322 1322 empty. Otherwise the question is repeated until an answer is given.
1323 1323
1324 1324 An EOF is treated as the default answer. If there is no default, an
1325 1325 exception is raised to prevent infinite loops.
1326 1326
1327 1327 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1328 1328
1329 1329 answers = {'y':True,'n':False,'yes':True,'no':False}
1330 1330 ans = None
1331 1331 while ans not in answers.keys():
1332 1332 try:
1333 1333 ans = raw_input(prompt+' ').lower()
1334 1334 if not ans: # response was an empty string
1335 1335 ans = default
1336 1336 except KeyboardInterrupt:
1337 1337 pass
1338 1338 except EOFError:
1339 1339 if default in answers.keys():
1340 1340 ans = default
1341 1341 print
1342 1342 else:
1343 1343 raise
1344 1344
1345 1345 return answers[ans]
1346 1346
1347 1347 #----------------------------------------------------------------------------
1348 1348 def marquee(txt='',width=78,mark='*'):
1349 1349 """Return the input string centered in a 'marquee'."""
1350 1350 if not txt:
1351 1351 return (mark*width)[:width]
1352 1352 nmark = (width-len(txt)-2)/len(mark)/2
1353 1353 if nmark < 0: nmark =0
1354 1354 marks = mark*nmark
1355 1355 return '%s %s %s' % (marks,txt,marks)
1356 1356
1357 1357 #----------------------------------------------------------------------------
1358 1358 class EvalDict:
1359 1359 """
1360 1360 Emulate a dict which evaluates its contents in the caller's frame.
1361 1361
1362 1362 Usage:
1363 1363 >>> number = 19
1364 1364
1365 1365 >>> text = "python"
1366 1366
1367 1367 >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1368 1368 Python 2.1 rules!
1369 1369 """
1370 1370
1371 1371 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1372 1372 # modified (shorter) version of:
1373 1373 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1374 1374 # Skip Montanaro (skip@pobox.com).
1375 1375
1376 1376 def __getitem__(self, name):
1377 1377 frame = sys._getframe(1)
1378 1378 return eval(name, frame.f_globals, frame.f_locals)
1379 1379
1380 1380 EvalString = EvalDict # for backwards compatibility
1381 1381 #----------------------------------------------------------------------------
1382 1382 def qw(words,flat=0,sep=None,maxsplit=-1):
1383 1383 """Similar to Perl's qw() operator, but with some more options.
1384 1384
1385 1385 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1386 1386
1387 1387 words can also be a list itself, and with flat=1, the output will be
1388 1388 recursively flattened.
1389 1389
1390 1390 Examples:
1391 1391
1392 1392 >>> qw('1 2')
1393 1393 ['1', '2']
1394 1394
1395 1395 >>> qw(['a b','1 2',['m n','p q']])
1396 1396 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1397 1397
1398 1398 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1399 1399 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
1400 1400 """
1401 1401
1402 1402 if type(words) in StringTypes:
1403 1403 return [word.strip() for word in words.split(sep,maxsplit)
1404 1404 if word and not word.isspace() ]
1405 1405 if flat:
1406 1406 return flatten(map(qw,words,[1]*len(words)))
1407 1407 return map(qw,words)
1408 1408
1409 1409 #----------------------------------------------------------------------------
1410 1410 def qwflat(words,sep=None,maxsplit=-1):
1411 1411 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1412 1412 return qw(words,1,sep,maxsplit)
1413 1413
1414 1414 #----------------------------------------------------------------------------
1415 1415 def qw_lol(indata):
1416 1416 """qw_lol('a b') -> [['a','b']],
1417 1417 otherwise it's just a call to qw().
1418 1418
1419 1419 We need this to make sure the modules_some keys *always* end up as a
1420 1420 list of lists."""
1421 1421
1422 1422 if type(indata) in StringTypes:
1423 1423 return [qw(indata)]
1424 1424 else:
1425 1425 return qw(indata)
1426 1426
1427 1427 #----------------------------------------------------------------------------
1428 1428 def grep(pat,list,case=1):
1429 1429 """Simple minded grep-like function.
1430 1430 grep(pat,list) returns occurrences of pat in list, None on failure.
1431 1431
1432 1432 It only does simple string matching, with no support for regexps. Use the
1433 1433 option case=0 for case-insensitive matching."""
1434 1434
1435 1435 # This is pretty crude. At least it should implement copying only references
1436 1436 # to the original data in case it's big. Now it copies the data for output.
1437 1437 out=[]
1438 1438 if case:
1439 1439 for term in list:
1440 1440 if term.find(pat)>-1: out.append(term)
1441 1441 else:
1442 1442 lpat=pat.lower()
1443 1443 for term in list:
1444 1444 if term.lower().find(lpat)>-1: out.append(term)
1445 1445
1446 1446 if len(out): return out
1447 1447 else: return None
1448 1448
1449 1449 #----------------------------------------------------------------------------
1450 1450 def dgrep(pat,*opts):
1451 1451 """Return grep() on dir()+dir(__builtins__).
1452 1452
1453 1453 A very common use of grep() when working interactively."""
1454 1454
1455 1455 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1456 1456
1457 1457 #----------------------------------------------------------------------------
1458 1458 def idgrep(pat):
1459 1459 """Case-insensitive dgrep()"""
1460 1460
1461 1461 return dgrep(pat,0)
1462 1462
1463 1463 #----------------------------------------------------------------------------
1464 1464 def igrep(pat,list):
1465 1465 """Synonym for case-insensitive grep."""
1466 1466
1467 1467 return grep(pat,list,case=0)
1468 1468
1469 1469 #----------------------------------------------------------------------------
1470 1470 def indent(str,nspaces=4,ntabs=0):
1471 1471 """Indent a string a given number of spaces or tabstops.
1472 1472
1473 1473 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1474 1474 """
1475 1475 if str is None:
1476 1476 return
1477 1477 ind = '\t'*ntabs+' '*nspaces
1478 1478 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1479 1479 if outstr.endswith(os.linesep+ind):
1480 1480 return outstr[:-len(ind)]
1481 1481 else:
1482 1482 return outstr
1483 1483
1484 1484 #-----------------------------------------------------------------------------
1485 1485 def native_line_ends(filename,backup=1):
1486 1486 """Convert (in-place) a file to line-ends native to the current OS.
1487 1487
1488 1488 If the optional backup argument is given as false, no backup of the
1489 1489 original file is left. """
1490 1490
1491 1491 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1492 1492
1493 1493 bak_filename = filename + backup_suffixes[os.name]
1494 1494
1495 1495 original = open(filename).read()
1496 1496 shutil.copy2(filename,bak_filename)
1497 1497 try:
1498 1498 new = open(filename,'wb')
1499 1499 new.write(os.linesep.join(original.splitlines()))
1500 1500 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1501 1501 new.close()
1502 1502 except:
1503 1503 os.rename(bak_filename,filename)
1504 1504 if not backup:
1505 1505 try:
1506 1506 os.remove(bak_filename)
1507 1507 except:
1508 1508 pass
1509 1509
1510 1510 #----------------------------------------------------------------------------
1511 1511 def get_pager_cmd(pager_cmd = None):
1512 1512 """Return a pager command.
1513 1513
1514 1514 Makes some attempts at finding an OS-correct one."""
1515 1515
1516 1516 if os.name == 'posix':
1517 1517 default_pager_cmd = 'less -r' # -r for color control sequences
1518 1518 elif os.name in ['nt','dos']:
1519 1519 default_pager_cmd = 'type'
1520 1520
1521 1521 if pager_cmd is None:
1522 1522 try:
1523 1523 pager_cmd = os.environ['PAGER']
1524 1524 except:
1525 1525 pager_cmd = default_pager_cmd
1526 1526 return pager_cmd
1527 1527
1528 1528 #-----------------------------------------------------------------------------
1529 1529 def get_pager_start(pager,start):
1530 1530 """Return the string for paging files with an offset.
1531 1531
1532 1532 This is the '+N' argument which less and more (under Unix) accept.
1533 1533 """
1534 1534
1535 1535 if pager in ['less','more']:
1536 1536 if start:
1537 1537 start_string = '+' + str(start)
1538 1538 else:
1539 1539 start_string = ''
1540 1540 else:
1541 1541 start_string = ''
1542 1542 return start_string
1543 1543
1544 1544 #----------------------------------------------------------------------------
1545 1545 # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch()
1546 1546 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
1547 1547 import msvcrt
1548 1548 def page_more():
1549 1549 """ Smart pausing between pages
1550 1550
1551 1551 @return: True if need print more lines, False if quit
1552 1552 """
1553 1553 Term.cout.write('---Return to continue, q to quit--- ')
1554 1554 ans = msvcrt.getch()
1555 1555 if ans in ("q", "Q"):
1556 1556 result = False
1557 1557 else:
1558 1558 result = True
1559 1559 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1560 1560 return result
1561 1561 else:
1562 1562 def page_more():
1563 1563 ans = raw_input('---Return to continue, q to quit--- ')
1564 1564 if ans.lower().startswith('q'):
1565 1565 return False
1566 1566 else:
1567 1567 return True
1568 1568
1569 1569 esc_re = re.compile(r"(\x1b[^m]+m)")
1570 1570
1571 1571 def page_dumb(strng,start=0,screen_lines=25):
1572 1572 """Very dumb 'pager' in Python, for when nothing else works.
1573 1573
1574 1574 Only moves forward, same interface as page(), except for pager_cmd and
1575 1575 mode."""
1576 1576
1577 1577 out_ln = strng.splitlines()[start:]
1578 1578 screens = chop(out_ln,screen_lines-1)
1579 1579 if len(screens) == 1:
1580 1580 print >>Term.cout, os.linesep.join(screens[0])
1581 1581 else:
1582 1582 last_escape = ""
1583 1583 for scr in screens[0:-1]:
1584 1584 hunk = os.linesep.join(scr)
1585 1585 print >>Term.cout, last_escape + hunk
1586 1586 if not page_more():
1587 1587 return
1588 1588 esc_list = esc_re.findall(hunk)
1589 1589 if len(esc_list) > 0:
1590 1590 last_escape = esc_list[-1]
1591 1591 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1592 1592
1593 1593 #----------------------------------------------------------------------------
1594 1594 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1595 1595 """Print a string, piping through a pager after a certain length.
1596 1596
1597 1597 The screen_lines parameter specifies the number of *usable* lines of your
1598 1598 terminal screen (total lines minus lines you need to reserve to show other
1599 1599 information).
1600 1600
1601 1601 If you set screen_lines to a number <=0, page() will try to auto-determine
1602 1602 your screen size and will only use up to (screen_size+screen_lines) for
1603 1603 printing, paging after that. That is, if you want auto-detection but need
1604 1604 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1605 1605 auto-detection without any lines reserved simply use screen_lines = 0.
1606 1606
1607 1607 If a string won't fit in the allowed lines, it is sent through the
1608 1608 specified pager command. If none given, look for PAGER in the environment,
1609 1609 and ultimately default to less.
1610 1610
1611 1611 If no system pager works, the string is sent through a 'dumb pager'
1612 1612 written in python, very simplistic.
1613 1613 """
1614 1614
1615 1615 # Some routines may auto-compute start offsets incorrectly and pass a
1616 1616 # negative value. Offset to 0 for robustness.
1617 1617 start = max(0,start)
1618 1618
1619 1619 # first, try the hook
1620 1620 ip = ipapi.get()
1621 1621 if ip:
1622 1622 try:
1623 1623 ip.IP.hooks.show_in_pager(strng)
1624 1624 return
1625 1625 except ipapi.TryNext:
1626 1626 pass
1627 1627
1628 1628 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1629 1629 TERM = os.environ.get('TERM','dumb')
1630 1630 if TERM in ['dumb','emacs'] and os.name != 'nt':
1631 1631 print strng
1632 1632 return
1633 1633 # chop off the topmost part of the string we don't want to see
1634 1634 str_lines = strng.split(os.linesep)[start:]
1635 1635 str_toprint = os.linesep.join(str_lines)
1636 1636 num_newlines = len(str_lines)
1637 1637 len_str = len(str_toprint)
1638 1638
1639 1639 # Dumb heuristics to guesstimate number of on-screen lines the string
1640 1640 # takes. Very basic, but good enough for docstrings in reasonable
1641 1641 # terminals. If someone later feels like refining it, it's not hard.
1642 1642 numlines = max(num_newlines,int(len_str/80)+1)
1643 1643
1644 1644 if os.name == "nt":
1645 1645 screen_lines_def = get_console_size(defaulty=25)[1]
1646 1646 else:
1647 1647 screen_lines_def = 25 # default value if we can't auto-determine
1648 1648
1649 1649 # auto-determine screen size
1650 1650 if screen_lines <= 0:
1651 1651 if TERM=='xterm':
1652 1652 use_curses = USE_CURSES
1653 1653 else:
1654 1654 # curses causes problems on many terminals other than xterm.
1655 1655 use_curses = False
1656 1656 if use_curses:
1657 1657 # There is a bug in curses, where *sometimes* it fails to properly
1658 1658 # initialize, and then after the endwin() call is made, the
1659 1659 # terminal is left in an unusable state. Rather than trying to
1660 1660 # check everytime for this (by requesting and comparing termios
1661 1661 # flags each time), we just save the initial terminal state and
1662 1662 # unconditionally reset it every time. It's cheaper than making
1663 1663 # the checks.
1664 1664 term_flags = termios.tcgetattr(sys.stdout)
1665 1665 scr = curses.initscr()
1666 1666 screen_lines_real,screen_cols = scr.getmaxyx()
1667 1667 curses.endwin()
1668 1668 # Restore terminal state in case endwin() didn't.
1669 1669 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
1670 1670 # Now we have what we needed: the screen size in rows/columns
1671 1671 screen_lines += screen_lines_real
1672 1672 #print '***Screen size:',screen_lines_real,'lines x',\
1673 1673 #screen_cols,'columns.' # dbg
1674 1674 else:
1675 1675 screen_lines += screen_lines_def
1676 1676
1677 1677 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1678 1678 if numlines <= screen_lines :
1679 1679 #print '*** normal print' # dbg
1680 1680 print >>Term.cout, str_toprint
1681 1681 else:
1682 1682 # Try to open pager and default to internal one if that fails.
1683 1683 # All failure modes are tagged as 'retval=1', to match the return
1684 1684 # value of a failed system command. If any intermediate attempt
1685 1685 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1686 1686 pager_cmd = get_pager_cmd(pager_cmd)
1687 1687 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1688 1688 if os.name == 'nt':
1689 1689 if pager_cmd.startswith('type'):
1690 1690 # The default WinXP 'type' command is failing on complex strings.
1691 1691 retval = 1
1692 1692 else:
1693 1693 tmpname = tempfile.mktemp('.txt')
1694 1694 tmpfile = file(tmpname,'wt')
1695 1695 tmpfile.write(strng)
1696 1696 tmpfile.close()
1697 1697 cmd = "%s < %s" % (pager_cmd,tmpname)
1698 1698 if os.system(cmd):
1699 1699 retval = 1
1700 1700 else:
1701 1701 retval = None
1702 1702 os.remove(tmpname)
1703 1703 else:
1704 1704 try:
1705 1705 retval = None
1706 1706 # if I use popen4, things hang. No idea why.
1707 1707 #pager,shell_out = os.popen4(pager_cmd)
1708 1708 pager = os.popen(pager_cmd,'w')
1709 1709 pager.write(strng)
1710 1710 pager.close()
1711 1711 retval = pager.close() # success returns None
1712 1712 except IOError,msg: # broken pipe when user quits
1713 1713 if msg.args == (32,'Broken pipe'):
1714 1714 retval = None
1715 1715 else:
1716 1716 retval = 1
1717 1717 except OSError:
1718 1718 # Other strange problems, sometimes seen in Win2k/cygwin
1719 1719 retval = 1
1720 1720 if retval is not None:
1721 1721 page_dumb(strng,screen_lines=screen_lines)
1722 1722
1723 1723 #----------------------------------------------------------------------------
1724 1724 def page_file(fname,start = 0, pager_cmd = None):
1725 1725 """Page a file, using an optional pager command and starting line.
1726 1726 """
1727 1727
1728 1728 pager_cmd = get_pager_cmd(pager_cmd)
1729 1729 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1730 1730
1731 1731 try:
1732 1732 if os.environ['TERM'] in ['emacs','dumb']:
1733 1733 raise EnvironmentError
1734 1734 xsys(pager_cmd + ' ' + fname)
1735 1735 except:
1736 1736 try:
1737 1737 if start > 0:
1738 1738 start -= 1
1739 1739 page(open(fname).read(),start)
1740 1740 except:
1741 1741 print 'Unable to show file',`fname`
1742 1742
1743 1743
1744 1744 #----------------------------------------------------------------------------
1745 1745 def snip_print(str,width = 75,print_full = 0,header = ''):
1746 1746 """Print a string snipping the midsection to fit in width.
1747 1747
1748 1748 print_full: mode control:
1749 1749 - 0: only snip long strings
1750 1750 - 1: send to page() directly.
1751 1751 - 2: snip long strings and ask for full length viewing with page()
1752 1752 Return 1 if snipping was necessary, 0 otherwise."""
1753 1753
1754 1754 if print_full == 1:
1755 1755 page(header+str)
1756 1756 return 0
1757 1757
1758 1758 print header,
1759 1759 if len(str) < width:
1760 1760 print str
1761 1761 snip = 0
1762 1762 else:
1763 1763 whalf = int((width -5)/2)
1764 1764 print str[:whalf] + ' <...> ' + str[-whalf:]
1765 1765 snip = 1
1766 1766 if snip and print_full == 2:
1767 1767 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1768 1768 page(str)
1769 1769 return snip
1770 1770
1771 1771 #****************************************************************************
1772 1772 # lists, dicts and structures
1773 1773
1774 1774 def belong(candidates,checklist):
1775 1775 """Check whether a list of items appear in a given list of options.
1776 1776
1777 1777 Returns a list of 1 and 0, one for each candidate given."""
1778 1778
1779 1779 return [x in checklist for x in candidates]
1780 1780
1781 1781 #----------------------------------------------------------------------------
1782 1782 def uniq_stable(elems):
1783 1783 """uniq_stable(elems) -> list
1784 1784
1785 1785 Return from an iterable, a list of all the unique elements in the input,
1786 1786 but maintaining the order in which they first appear.
1787 1787
1788 1788 A naive solution to this problem which just makes a dictionary with the
1789 1789 elements as keys fails to respect the stability condition, since
1790 1790 dictionaries are unsorted by nature.
1791 1791
1792 1792 Note: All elements in the input must be valid dictionary keys for this
1793 1793 routine to work, as it internally uses a dictionary for efficiency
1794 1794 reasons."""
1795 1795
1796 1796 unique = []
1797 1797 unique_dict = {}
1798 1798 for nn in elems:
1799 1799 if nn not in unique_dict:
1800 1800 unique.append(nn)
1801 1801 unique_dict[nn] = None
1802 1802 return unique
1803 1803
1804 1804 #----------------------------------------------------------------------------
1805 1805 class NLprinter:
1806 1806 """Print an arbitrarily nested list, indicating index numbers.
1807 1807
1808 1808 An instance of this class called nlprint is available and callable as a
1809 1809 function.
1810 1810
1811 1811 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1812 1812 and using 'sep' to separate the index from the value. """
1813 1813
1814 1814 def __init__(self):
1815 1815 self.depth = 0
1816 1816
1817 1817 def __call__(self,lst,pos='',**kw):
1818 1818 """Prints the nested list numbering levels."""
1819 1819 kw.setdefault('indent',' ')
1820 1820 kw.setdefault('sep',': ')
1821 1821 kw.setdefault('start',0)
1822 1822 kw.setdefault('stop',len(lst))
1823 1823 # we need to remove start and stop from kw so they don't propagate
1824 1824 # into a recursive call for a nested list.
1825 1825 start = kw['start']; del kw['start']
1826 1826 stop = kw['stop']; del kw['stop']
1827 1827 if self.depth == 0 and 'header' in kw.keys():
1828 1828 print kw['header']
1829 1829
1830 1830 for idx in range(start,stop):
1831 1831 elem = lst[idx]
1832 1832 if type(elem)==type([]):
1833 1833 self.depth += 1
1834 1834 self.__call__(elem,itpl('$pos$idx,'),**kw)
1835 1835 self.depth -= 1
1836 1836 else:
1837 1837 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1838 1838
1839 1839 nlprint = NLprinter()
1840 1840 #----------------------------------------------------------------------------
1841 1841 def all_belong(candidates,checklist):
1842 1842 """Check whether a list of items ALL appear in a given list of options.
1843 1843
1844 1844 Returns a single 1 or 0 value."""
1845 1845
1846 1846 return 1-(0 in [x in checklist for x in candidates])
1847 1847
1848 1848 #----------------------------------------------------------------------------
1849 1849 def sort_compare(lst1,lst2,inplace = 1):
1850 1850 """Sort and compare two lists.
1851 1851
1852 1852 By default it does it in place, thus modifying the lists. Use inplace = 0
1853 1853 to avoid that (at the cost of temporary copy creation)."""
1854 1854 if not inplace:
1855 1855 lst1 = lst1[:]
1856 1856 lst2 = lst2[:]
1857 1857 lst1.sort(); lst2.sort()
1858 1858 return lst1 == lst2
1859 1859
1860 1860 #----------------------------------------------------------------------------
1861 1861 def list2dict(lst):
1862 1862 """Takes a list of (key,value) pairs and turns it into a dict."""
1863 1863
1864 1864 dic = {}
1865 1865 for k,v in lst: dic[k] = v
1866 1866 return dic
1867 1867
1868 1868 #----------------------------------------------------------------------------
1869 1869 def list2dict2(lst,default=''):
1870 1870 """Takes a list and turns it into a dict.
1871 1871 Much slower than list2dict, but more versatile. This version can take
1872 1872 lists with sublists of arbitrary length (including sclars)."""
1873 1873
1874 1874 dic = {}
1875 1875 for elem in lst:
1876 1876 if type(elem) in (types.ListType,types.TupleType):
1877 1877 size = len(elem)
1878 1878 if size == 0:
1879 1879 pass
1880 1880 elif size == 1:
1881 1881 dic[elem] = default
1882 1882 else:
1883 1883 k,v = elem[0], elem[1:]
1884 1884 if len(v) == 1: v = v[0]
1885 1885 dic[k] = v
1886 1886 else:
1887 1887 dic[elem] = default
1888 1888 return dic
1889 1889
1890 1890 #----------------------------------------------------------------------------
1891 1891 def flatten(seq):
1892 1892 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1893 1893
1894 1894 return [x for subseq in seq for x in subseq]
1895 1895
1896 1896 #----------------------------------------------------------------------------
1897 1897 def get_slice(seq,start=0,stop=None,step=1):
1898 1898 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1899 1899 if stop == None:
1900 1900 stop = len(seq)
1901 1901 item = lambda i: seq[i]
1902 1902 return map(item,xrange(start,stop,step))
1903 1903
1904 1904 #----------------------------------------------------------------------------
1905 1905 def chop(seq,size):
1906 1906 """Chop a sequence into chunks of the given size."""
1907 1907 chunk = lambda i: seq[i:i+size]
1908 1908 return map(chunk,xrange(0,len(seq),size))
1909 1909
1910 1910 #----------------------------------------------------------------------------
1911 1911 # with is a keyword as of python 2.5, so this function is renamed to withobj
1912 1912 # from its old 'with' name.
1913 1913 def with_obj(object, **args):
1914 1914 """Set multiple attributes for an object, similar to Pascal's with.
1915 1915
1916 1916 Example:
1917 1917 with_obj(jim,
1918 1918 born = 1960,
1919 1919 haircolour = 'Brown',
1920 1920 eyecolour = 'Green')
1921 1921
1922 1922 Credit: Greg Ewing, in
1923 1923 http://mail.python.org/pipermail/python-list/2001-May/040703.html.
1924 1924
1925 1925 NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
1926 1926 has become a keyword for Python 2.5, so we had to rename it."""
1927 1927
1928 1928 object.__dict__.update(args)
1929 1929
1930 1930 #----------------------------------------------------------------------------
1931 1931 def setattr_list(obj,alist,nspace = None):
1932 1932 """Set a list of attributes for an object taken from a namespace.
1933 1933
1934 1934 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1935 1935 alist with their values taken from nspace, which must be a dict (something
1936 1936 like locals() will often do) If nspace isn't given, locals() of the
1937 1937 *caller* is used, so in most cases you can omit it.
1938 1938
1939 1939 Note that alist can be given as a string, which will be automatically
1940 1940 split into a list on whitespace. If given as a list, it must be a list of
1941 1941 *strings* (the variable names themselves), not of variables."""
1942 1942
1943 1943 # this grabs the local variables from the *previous* call frame -- that is
1944 1944 # the locals from the function that called setattr_list().
1945 1945 # - snipped from weave.inline()
1946 1946 if nspace is None:
1947 1947 call_frame = sys._getframe().f_back
1948 1948 nspace = call_frame.f_locals
1949 1949
1950 1950 if type(alist) in StringTypes:
1951 1951 alist = alist.split()
1952 1952 for attr in alist:
1953 1953 val = eval(attr,nspace)
1954 1954 setattr(obj,attr,val)
1955 1955
1956 1956 #----------------------------------------------------------------------------
1957 1957 def getattr_list(obj,alist,*args):
1958 1958 """getattr_list(obj,alist[, default]) -> attribute list.
1959 1959
1960 1960 Get a list of named attributes for an object. When a default argument is
1961 1961 given, it is returned when the attribute doesn't exist; without it, an
1962 1962 exception is raised in that case.
1963 1963
1964 1964 Note that alist can be given as a string, which will be automatically
1965 1965 split into a list on whitespace. If given as a list, it must be a list of
1966 1966 *strings* (the variable names themselves), not of variables."""
1967 1967
1968 1968 if type(alist) in StringTypes:
1969 1969 alist = alist.split()
1970 1970 if args:
1971 1971 if len(args)==1:
1972 1972 default = args[0]
1973 1973 return map(lambda attr: getattr(obj,attr,default),alist)
1974 1974 else:
1975 1975 raise ValueError,'getattr_list() takes only one optional argument'
1976 1976 else:
1977 1977 return map(lambda attr: getattr(obj,attr),alist)
1978 1978
1979 1979 #----------------------------------------------------------------------------
1980 1980 def map_method(method,object_list,*argseq,**kw):
1981 1981 """map_method(method,object_list,*args,**kw) -> list
1982 1982
1983 1983 Return a list of the results of applying the methods to the items of the
1984 1984 argument sequence(s). If more than one sequence is given, the method is
1985 1985 called with an argument list consisting of the corresponding item of each
1986 1986 sequence. All sequences must be of the same length.
1987 1987
1988 1988 Keyword arguments are passed verbatim to all objects called.
1989 1989
1990 1990 This is Python code, so it's not nearly as fast as the builtin map()."""
1991 1991
1992 1992 out_list = []
1993 1993 idx = 0
1994 1994 for object in object_list:
1995 1995 try:
1996 1996 handler = getattr(object, method)
1997 1997 except AttributeError:
1998 1998 out_list.append(None)
1999 1999 else:
2000 2000 if argseq:
2001 2001 args = map(lambda lst:lst[idx],argseq)
2002 2002 #print 'ob',object,'hand',handler,'ar',args # dbg
2003 2003 out_list.append(handler(args,**kw))
2004 2004 else:
2005 2005 out_list.append(handler(**kw))
2006 2006 idx += 1
2007 2007 return out_list
2008 2008
2009 2009 #----------------------------------------------------------------------------
2010 2010 def get_class_members(cls):
2011 2011 ret = dir(cls)
2012 2012 if hasattr(cls,'__bases__'):
2013 2013 for base in cls.__bases__:
2014 2014 ret.extend(get_class_members(base))
2015 2015 return ret
2016 2016
2017 2017 #----------------------------------------------------------------------------
2018 2018 def dir2(obj):
2019 2019 """dir2(obj) -> list of strings
2020 2020
2021 2021 Extended version of the Python builtin dir(), which does a few extra
2022 2022 checks, and supports common objects with unusual internals that confuse
2023 2023 dir(), such as Traits and PyCrust.
2024 2024
2025 2025 This version is guaranteed to return only a list of true strings, whereas
2026 2026 dir() returns anything that objects inject into themselves, even if they
2027 2027 are later not really valid for attribute access (many extension libraries
2028 2028 have such bugs).
2029 2029 """
2030 2030
2031 2031 # Start building the attribute list via dir(), and then complete it
2032 2032 # with a few extra special-purpose calls.
2033 2033 words = dir(obj)
2034 2034
2035 2035 if hasattr(obj,'__class__'):
2036 2036 words.append('__class__')
2037 2037 words.extend(get_class_members(obj.__class__))
2038 2038 #if '__base__' in words: 1/0
2039 2039
2040 2040 # Some libraries (such as traits) may introduce duplicates, we want to
2041 2041 # track and clean this up if it happens
2042 2042 may_have_dupes = False
2043 2043
2044 2044 # this is the 'dir' function for objects with Enthought's traits
2045 2045 if hasattr(obj, 'trait_names'):
2046 2046 try:
2047 2047 words.extend(obj.trait_names())
2048 2048 may_have_dupes = True
2049 2049 except TypeError:
2050 2050 # This will happen if `obj` is a class and not an instance.
2051 2051 pass
2052 2052
2053 2053 # Support for PyCrust-style _getAttributeNames magic method.
2054 2054 if hasattr(obj, '_getAttributeNames'):
2055 2055 try:
2056 2056 words.extend(obj._getAttributeNames())
2057 2057 may_have_dupes = True
2058 2058 except TypeError:
2059 2059 # `obj` is a class and not an instance. Ignore
2060 2060 # this error.
2061 2061 pass
2062 2062
2063 2063 if may_have_dupes:
2064 2064 # eliminate possible duplicates, as some traits may also
2065 2065 # appear as normal attributes in the dir() call.
2066 2066 words = list(set(words))
2067 2067 words.sort()
2068 2068
2069 2069 # filter out non-string attributes which may be stuffed by dir() calls
2070 2070 # and poor coding in third-party modules
2071 2071 return [w for w in words if isinstance(w, basestring)]
2072 2072
2073 2073 #----------------------------------------------------------------------------
2074 2074 def import_fail_info(mod_name,fns=None):
2075 2075 """Inform load failure for a module."""
2076 2076
2077 2077 if fns == None:
2078 2078 warn("Loading of %s failed.\n" % (mod_name,))
2079 2079 else:
2080 2080 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
2081 2081
2082 2082 #----------------------------------------------------------------------------
2083 2083 # Proposed popitem() extension, written as a method
2084 2084
2085 2085
2086 2086 class NotGiven: pass
2087 2087
2088 2088 def popkey(dct,key,default=NotGiven):
2089 2089 """Return dct[key] and delete dct[key].
2090 2090
2091 2091 If default is given, return it if dct[key] doesn't exist, otherwise raise
2092 2092 KeyError. """
2093 2093
2094 2094 try:
2095 2095 val = dct[key]
2096 2096 except KeyError:
2097 2097 if default is NotGiven:
2098 2098 raise
2099 2099 else:
2100 2100 return default
2101 2101 else:
2102 2102 del dct[key]
2103 2103 return val
2104 2104
2105 2105 def wrap_deprecated(func, suggest = '<nothing>'):
2106 2106 def newFunc(*args, **kwargs):
2107 2107 warnings.warn("Call to deprecated function %s, use %s instead" %
2108 2108 ( func.__name__, suggest),
2109 2109 category=DeprecationWarning,
2110 2110 stacklevel = 2)
2111 2111 return func(*args, **kwargs)
2112 2112 return newFunc
2113 2113
2114 2114
2115 2115 def _num_cpus_unix():
2116 2116 """Return the number of active CPUs on a Unix system."""
2117 2117 return os.sysconf("SC_NPROCESSORS_ONLN")
2118 2118
2119 2119
2120 2120 def _num_cpus_darwin():
2121 2121 """Return the number of active CPUs on a Darwin system."""
2122 2122 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
2123 2123 return p.stdout.read()
2124 2124
2125 2125
2126 2126 def _num_cpus_windows():
2127 2127 """Return the number of active CPUs on a Windows system."""
2128 2128 return os.environ.get("NUMBER_OF_PROCESSORS")
2129 2129
2130 2130
2131 2131 def num_cpus():
2132 2132 """Return the effective number of CPUs in the system as an integer.
2133 2133
2134 2134 This cross-platform function makes an attempt at finding the total number of
2135 2135 available CPUs in the system, as returned by various underlying system and
2136 2136 python calls.
2137 2137
2138 2138 If it can't find a sensible answer, it returns 1 (though an error *may* make
2139 2139 it return a large positive number that's actually incorrect).
2140 2140 """
2141 2141
2142 2142 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
2143 2143 # for the names of the keys we needed to look up for this function. This
2144 2144 # code was inspired by their equivalent function.
2145 2145
2146 2146 ncpufuncs = {'Linux':_num_cpus_unix,
2147 2147 'Darwin':_num_cpus_darwin,
2148 2148 'Windows':_num_cpus_windows,
2149 2149 # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
2150 2150 # See http://bugs.python.org/issue1082 for details.
2151 2151 'Microsoft':_num_cpus_windows,
2152 2152 }
2153 2153
2154 2154 ncpufunc = ncpufuncs.get(platform.system(),
2155 2155 # default to unix version (Solaris, AIX, etc)
2156 2156 _num_cpus_unix)
2157 2157
2158 2158 try:
2159 2159 ncpus = max(1,int(ncpufunc()))
2160 2160 except:
2161 2161 ncpus = 1
2162 2162 return ncpus
2163 2163
2164 2164 def extract_vars(*names,**kw):
2165 2165 """Extract a set of variables by name from another frame.
2166 2166
2167 2167 :Parameters:
2168 2168 - `*names`: strings
2169 2169 One or more variable names which will be extracted from the caller's
2170 2170 frame.
2171 2171
2172 2172 :Keywords:
2173 2173 - `depth`: integer (0)
2174 2174 How many frames in the stack to walk when looking for your variables.
2175 2175
2176 2176
2177 2177 Examples:
2178 2178
2179 2179 In [2]: def func(x):
2180 2180 ...: y = 1
2181 2181 ...: print extract_vars('x','y')
2182 2182 ...:
2183 2183
2184 2184 In [3]: func('hello')
2185 2185 {'y': 1, 'x': 'hello'}
2186 2186 """
2187 2187
2188 2188 depth = kw.get('depth',0)
2189 2189
2190 2190 callerNS = sys._getframe(depth+1).f_locals
2191 2191 return dict((k,callerNS[k]) for k in names)
2192 2192
2193 2193
2194 2194 def extract_vars_above(*names):
2195 2195 """Extract a set of variables by name from another frame.
2196 2196
2197 2197 Similar to extractVars(), but with a specified depth of 1, so that names
2198 2198 are exctracted exactly from above the caller.
2199 2199
2200 2200 This is simply a convenience function so that the very common case (for us)
2201 2201 of skipping exactly 1 frame doesn't have to construct a special dict for
2202 2202 keyword passing."""
2203 2203
2204 2204 callerNS = sys._getframe(2).f_locals
2205 2205 return dict((k,callerNS[k]) for k in names)
2206 2206
2207 2207 def shexp(s):
2208 2208 """Expand $VARS and ~names in a string, like a shell
2209 2209
2210 2210 :Examples:
2211 2211
2212 2212 In [2]: os.environ['FOO']='test'
2213 2213
2214 2214 In [3]: shexp('variable FOO is $FOO')
2215 2215 Out[3]: 'variable FOO is test'
2216 2216 """
2217 2217 return os.path.expandvars(os.path.expanduser(s))
2218 2218
2219 2219
2220 2220 def list_strings(arg):
2221 2221 """Always return a list of strings, given a string or list of strings
2222 2222 as input.
2223 2223
2224 2224 :Examples:
2225 2225
2226 2226 In [7]: list_strings('A single string')
2227 2227 Out[7]: ['A single string']
2228 2228
2229 2229 In [8]: list_strings(['A single string in a list'])
2230 2230 Out[8]: ['A single string in a list']
2231 2231
2232 2232 In [9]: list_strings(['A','list','of','strings'])
2233 2233 Out[9]: ['A', 'list', 'of', 'strings']
2234 2234 """
2235 2235
2236 2236 if isinstance(arg,basestring): return [arg]
2237 2237 else: return arg
2238 2238
2239 2239 def marquee(txt='',width=78,mark='*'):
2240 2240 """Return the input string centered in a 'marquee'.
2241 2241
2242 2242 :Examples:
2243 2243
2244 2244 In [16]: marquee('A test',40)
2245 2245 Out[16]: '**************** A test ****************'
2246 2246
2247 2247 In [17]: marquee('A test',40,'-')
2248 2248 Out[17]: '---------------- A test ----------------'
2249 2249
2250 2250 In [18]: marquee('A test',40,' ')
2251 2251 Out[18]: ' A test '
2252 2252
2253 2253 """
2254 2254 if not txt:
2255 2255 return (mark*width)[:width]
2256 2256 nmark = (width-len(txt)-2)/len(mark)/2
2257 2257 if nmark < 0: nmark =0
2258 2258 marks = mark*nmark
2259 2259 return '%s %s %s' % (marks,txt,marks)
2260 2260
2261 2261 #*************************** end of file <genutils.py> **********************
@@ -1,44 +1,50 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3
4 4 from IPython.kernel import client
5 5 import time
6 import sys
7 flush = sys.stdout.flush
6 8
7 9 tc = client.TaskClient()
8 10 mec = client.MultiEngineClient()
9 11
10 12 mec.execute('import time')
11 13
12 14 for i in range(24):
13 15 tc.run(client.StringTask('time.sleep(1)'))
14 16
15 17 for i in range(6):
16 18 time.sleep(1.0)
17 19 print "Queue status (vebose=False)"
18 20 print tc.queue_status()
21 flush()
19 22
20 23 for i in range(24):
21 24 tc.run(client.StringTask('time.sleep(1)'))
22 25
23 26 for i in range(6):
24 27 time.sleep(1.0)
25 28 print "Queue status (vebose=True)"
26 29 print tc.queue_status(True)
30 flush()
27 31
28 32 for i in range(12):
29 33 tc.run(client.StringTask('time.sleep(2)'))
30 34
31 35 print "Queue status (vebose=True)"
32 36 print tc.queue_status(True)
37 flush()
33 38
34 39 qs = tc.queue_status(True)
35 40 sched = qs['scheduled']
36 41
37 42 for tid in sched[-4:]:
38 43 tc.abort(tid)
39 44
40 45 for i in range(6):
41 46 time.sleep(1.0)
42 47 print "Queue status (vebose=True)"
43 48 print tc.queue_status(True)
49 flush()
44 50
@@ -1,1631 +1,1632 b''
1 1 =================
2 2 IPython reference
3 3 =================
4 4
5 5 .. _command_line_options:
6 6
7 7 Command-line usage
8 8 ==================
9 9
10 10 You start IPython with the command::
11 11
12 12 $ ipython [options] files
13 13
14 14 If invoked with no options, it executes all the files listed in sequence
15 15 and drops you into the interpreter while still acknowledging any options
16 16 you may have set in your ipythonrc file. This behavior is different from
17 17 standard Python, which when called as python -i will only execute one
18 18 file and ignore your configuration setup.
19 19
20 20 Please note that some of the configuration options are not available at
21 21 the command line, simply because they are not practical here. Look into
22 22 your ipythonrc configuration file for details on those. This file
23 23 typically installed in the $HOME/.ipython directory. For Windows users,
24 24 $HOME resolves to C:\\Documents and Settings\\YourUserName in most
25 25 instances. In the rest of this text, we will refer to this directory as
26 26 IPYTHONDIR.
27 27
28 28 .. _Threading options:
29 29
30 30
31 31 Special Threading Options
32 32 -------------------------
33 33
34 34 The following special options are ONLY valid at the beginning of the
35 35 command line, and not later. This is because they control the initial-
36 36 ization of ipython itself, before the normal option-handling mechanism
37 37 is active.
38 38
39 39 -gthread, -qthread, -q4thread, -wthread, -pylab:
40 40 Only one of these can be given, and it can only be given as
41 41 the first option passed to IPython (it will have no effect in
42 42 any other position). They provide threading support for the
43 43 GTK, Qt (versions 3 and 4) and WXPython toolkits, and for the
44 44 matplotlib library.
45 45
46 46 With any of the first four options, IPython starts running a
47 47 separate thread for the graphical toolkit's operation, so that
48 48 you can open and control graphical elements from within an
49 49 IPython command line, without blocking. All four provide
50 50 essentially the same functionality, respectively for GTK, Qt3,
51 51 Qt4 and WXWidgets (via their Python interfaces).
52 52
53 53 Note that with -wthread, you can additionally use the
54 54 -wxversion option to request a specific version of wx to be
55 55 used. This requires that you have the wxversion Python module
56 56 installed, which is part of recent wxPython distributions.
57 57
58 58 If -pylab is given, IPython loads special support for the mat
59 59 plotlib library (http://matplotlib.sourceforge.net), allowing
60 60 interactive usage of any of its backends as defined in the
61 61 user's ~/.matplotlib/matplotlibrc file. It automatically
62 62 activates GTK, Qt or WX threading for IPyhton if the choice of
63 63 matplotlib backend requires it. It also modifies the %run
64 64 command to correctly execute (without blocking) any
65 65 matplotlib-based script which calls show() at the end.
66 66
67 67 -tk
68 68 The -g/q/q4/wthread options, and -pylab (if matplotlib is
69 69 configured to use GTK, Qt3, Qt4 or WX), will normally block Tk
70 70 graphical interfaces. This means that when either GTK, Qt or WX
71 71 threading is active, any attempt to open a Tk GUI will result in a
72 72 dead window, and possibly cause the Python interpreter to crash.
73 73 An extra option, -tk, is available to address this issue. It can
74 74 only be given as a second option after any of the above (-gthread,
75 75 -wthread or -pylab).
76 76
77 77 If -tk is given, IPython will try to coordinate Tk threading
78 78 with GTK, Qt or WX. This is however potentially unreliable, and
79 79 you will have to test on your platform and Python configuration to
80 80 determine whether it works for you. Debian users have reported
81 81 success, apparently due to the fact that Debian builds all of Tcl,
82 82 Tk, Tkinter and Python with pthreads support. Under other Linux
83 83 environments (such as Fedora Core 2/3), this option has caused
84 84 random crashes and lockups of the Python interpreter. Under other
85 85 operating systems (Mac OSX and Windows), you'll need to try it to
86 86 find out, since currently no user reports are available.
87 87
88 88 There is unfortunately no way for IPython to determine at run time
89 89 whether -tk will work reliably or not, so you will need to do some
90 90 experiments before relying on it for regular work.
91 91
92 92
93 93
94 94 Regular Options
95 95 ---------------
96 96
97 97 After the above threading options have been given, regular options can
98 98 follow in any order. All options can be abbreviated to their shortest
99 99 non-ambiguous form and are case-sensitive. One or two dashes can be
100 100 used. Some options have an alternate short form, indicated after a ``|``.
101 101
102 102 Most options can also be set from your ipythonrc configuration file. See
103 103 the provided example for more details on what the options do. Options
104 104 given at the command line override the values set in the ipythonrc file.
105 105
106 106 All options with a [no] prepended can be specified in negated form
107 107 (-nooption instead of -option) to turn the feature off.
108 108
109 109 -help print a help message and exit.
110 110
111 111 -pylab
112 112 this can only be given as the first option passed to IPython
113 113 (it will have no effect in any other position). It adds
114 114 special support for the matplotlib library
115 115 (http://matplotlib.sourceforge.ne), allowing interactive usage
116 116 of any of its backends as defined in the user's .matplotlibrc
117 117 file. It automatically activates GTK or WX threading for
118 118 IPyhton if the choice of matplotlib backend requires it. It
119 119 also modifies the %run command to correctly execute (without
120 120 blocking) any matplotlib-based script which calls show() at
121 121 the end. See `Matplotlib support`_ for more details.
122 122
123 123 -autocall <val>
124 124 Make IPython automatically call any callable object even if you
125 125 didn't type explicit parentheses. For example, 'str 43' becomes
126 126 'str(43)' automatically. The value can be '0' to disable the feature,
127 127 '1' for smart autocall, where it is not applied if there are no more
128 128 arguments on the line, and '2' for full autocall, where all callable
129 129 objects are automatically called (even if no arguments are
130 130 present). The default is '1'.
131 131
132 132 -[no]autoindent
133 133 Turn automatic indentation on/off.
134 134
135 135 -[no]automagic
136 136 make magic commands automatic (without needing their first character
137 137 to be %). Type %magic at the IPython prompt for more information.
138 138
139 139 -[no]autoedit_syntax
140 140 When a syntax error occurs after editing a file, automatically
141 141 open the file to the trouble causing line for convenient
142 142 fixing.
143 143
144 144 -[no]banner Print the initial information banner (default on).
145 145
146 146 -c <command>
147 147 execute the given command string. This is similar to the -c
148 148 option in the normal Python interpreter.
149 149
150 150 -cache_size, cs <n>
151 151 size of the output cache (maximum number of entries to hold in
152 152 memory). The default is 1000, you can change it permanently in your
153 153 config file. Setting it to 0 completely disables the caching system,
154 154 and the minimum value accepted is 20 (if you provide a value less than
155 155 20, it is reset to 0 and a warning is issued) This limit is defined
156 156 because otherwise you'll spend more time re-flushing a too small cache
157 157 than working.
158 158
159 159 -classic, cl
160 160 Gives IPython a similar feel to the classic Python
161 161 prompt.
162 162
163 163 -colors <scheme>
164 164 Color scheme for prompts and exception reporting. Currently
165 165 implemented: NoColor, Linux and LightBG.
166 166
167 167 -[no]color_info
168 168 IPython can display information about objects via a set of functions,
169 169 and optionally can use colors for this, syntax highlighting source
170 170 code and various other elements. However, because this information is
171 171 passed through a pager (like 'less') and many pagers get confused with
172 172 color codes, this option is off by default. You can test it and turn
173 173 it on permanently in your ipythonrc file if it works for you. As a
174 174 reference, the 'less' pager supplied with Mandrake 8.2 works ok, but
175 175 that in RedHat 7.2 doesn't.
176 176
177 177 Test it and turn it on permanently if it works with your
178 178 system. The magic function %color_info allows you to toggle this
179 179 interactively for testing.
180 180
181 181 -[no]debug
182 182 Show information about the loading process. Very useful to pin down
183 183 problems with your configuration files or to get details about
184 184 session restores.
185 185
186 186 -[no]deep_reload:
187 187 IPython can use the deep_reload module which reloads changes in
188 188 modules recursively (it replaces the reload() function, so you don't
189 189 need to change anything to use it). deep_reload() forces a full
190 190 reload of modules whose code may have changed, which the default
191 191 reload() function does not.
192 192
193 193 When deep_reload is off, IPython will use the normal reload(),
194 194 but deep_reload will still be available as dreload(). This
195 195 feature is off by default [which means that you have both
196 196 normal reload() and dreload()].
197 197
198 198 -editor <name>
199 199 Which editor to use with the %edit command. By default,
200 200 IPython will honor your EDITOR environment variable (if not
201 201 set, vi is the Unix default and notepad the Windows one).
202 202 Since this editor is invoked on the fly by IPython and is
203 203 meant for editing small code snippets, you may want to use a
204 204 small, lightweight editor here (in case your default EDITOR is
205 205 something like Emacs).
206 206
207 207 -ipythondir <name>
208 208 name of your IPython configuration directory IPYTHONDIR. This
209 209 can also be specified through the environment variable
210 210 IPYTHONDIR.
211 211
212 212 -log, l
213 213 generate a log file of all input. The file is named
214 214 ipython_log.py in your current directory (which prevents logs
215 215 from multiple IPython sessions from trampling each other). You
216 216 can use this to later restore a session by loading your
217 217 logfile as a file to be executed with option -logplay (see
218 218 below).
219 219
220 220 -logfile, lf <name> specify the name of your logfile.
221 221
222 222 -logplay, lp <name>
223 223
224 224 you can replay a previous log. For restoring a session as close as
225 225 possible to the state you left it in, use this option (don't just run
226 226 the logfile). With -logplay, IPython will try to reconstruct the
227 227 previous working environment in full, not just execute the commands in
228 228 the logfile.
229 229
230 230 When a session is restored, logging is automatically turned on
231 231 again with the name of the logfile it was invoked with (it is
232 232 read from the log header). So once you've turned logging on for
233 233 a session, you can quit IPython and reload it as many times as
234 234 you want and it will continue to log its history and restore
235 235 from the beginning every time.
236 236
237 237 Caveats: there are limitations in this option. The history
238 238 variables _i*,_* and _dh don't get restored properly. In the
239 239 future we will try to implement full session saving by writing
240 240 and retrieving a 'snapshot' of the memory state of IPython. But
241 241 our first attempts failed because of inherent limitations of
242 242 Python's Pickle module, so this may have to wait.
243 243
244 244 -[no]messages
245 245 Print messages which IPython collects about its startup
246 246 process (default on).
247 247
248 248 -[no]pdb
249 249 Automatically call the pdb debugger after every uncaught
250 250 exception. If you are used to debugging using pdb, this puts
251 251 you automatically inside of it after any call (either in
252 252 IPython or in code called by it) which triggers an exception
253 253 which goes uncaught.
254 254
255 255 -pydb
256 256 Makes IPython use the third party "pydb" package as debugger,
257 257 instead of pdb. Requires that pydb is installed.
258 258
259 259 -[no]pprint
260 260 ipython can optionally use the pprint (pretty printer) module
261 261 for displaying results. pprint tends to give a nicer display
262 262 of nested data structures. If you like it, you can turn it on
263 263 permanently in your config file (default off).
264 264
265 265 -profile, p <name>
266 266
267 267 assume that your config file is ipythonrc-<name> or
268 268 ipy_profile_<name>.py (looks in current dir first, then in
269 269 IPYTHONDIR). This is a quick way to keep and load multiple
270 270 config files for different tasks, especially if you use the
271 271 include option of config files. You can keep a basic
272 272 IPYTHONDIR/ipythonrc file and then have other 'profiles' which
273 273 include this one and load extra things for particular
274 274 tasks. For example:
275 275
276 276 1. $HOME/.ipython/ipythonrc : load basic things you always want.
277 277 2. $HOME/.ipython/ipythonrc-math : load (1) and basic math-related modules.
278 278 3. $HOME/.ipython/ipythonrc-numeric : load (1) and Numeric and plotting modules.
279 279
280 280 Since it is possible to create an endless loop by having
281 281 circular file inclusions, IPython will stop if it reaches 15
282 282 recursive inclusions.
283 283
284 284 -prompt_in1, pi1 <string>
285 285
286 286 Specify the string used for input prompts. Note that if you are using
287 287 numbered prompts, the number is represented with a '\#' in the
288 288 string. Don't forget to quote strings with spaces embedded in
289 289 them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>`
290 290 discusses in detail all the available escapes to customize your
291 291 prompts.
292 292
293 293 -prompt_in2, pi2 <string>
294 294 Similar to the previous option, but used for the continuation
295 295 prompts. The special sequence '\D' is similar to '\#', but
296 296 with all digits replaced dots (so you can have your
297 297 continuation prompt aligned with your input prompt). Default:
298 298 ' .\D.:' (note three spaces at the start for alignment with
299 299 'In [\#]').
300 300
301 301 -prompt_out,po <string>
302 302 String used for output prompts, also uses numbers like
303 303 prompt_in1. Default: 'Out[\#]:'
304 304
305 305 -quick start in bare bones mode (no config file loaded).
306 306
307 307 -rcfile <name>
308 308 name of your IPython resource configuration file. Normally
309 309 IPython loads ipythonrc (from current directory) or
310 310 IPYTHONDIR/ipythonrc.
311 311
312 312 If the loading of your config file fails, IPython starts with
313 313 a bare bones configuration (no modules loaded at all).
314 314
315 315 -[no]readline
316 316 use the readline library, which is needed to support name
317 317 completion and command history, among other things. It is
318 318 enabled by default, but may cause problems for users of
319 319 X/Emacs in Python comint or shell buffers.
320 320
321 321 Note that X/Emacs 'eterm' buffers (opened with M-x term) support
322 322 IPython's readline and syntax coloring fine, only 'emacs' (M-x
323 323 shell and C-c !) buffers do not.
324 324
325 325 -screen_length, sl <n>
326 326 number of lines of your screen. This is used to control
327 327 printing of very long strings. Strings longer than this number
328 328 of lines will be sent through a pager instead of directly
329 329 printed.
330 330
331 331 The default value for this is 0, which means IPython will
332 332 auto-detect your screen size every time it needs to print certain
333 333 potentially long strings (this doesn't change the behavior of the
334 334 'print' keyword, it's only triggered internally). If for some
335 335 reason this isn't working well (it needs curses support), specify
336 336 it yourself. Otherwise don't change the default.
337 337
338 338 -separate_in, si <string>
339 339
340 340 separator before input prompts.
341 341 Default: '\n'
342 342
343 343 -separate_out, so <string>
344 344 separator before output prompts.
345 345 Default: nothing.
346 346
347 347 -separate_out2, so2
348 348 separator after output prompts.
349 349 Default: nothing.
350 350 For these three options, use the value 0 to specify no separator.
351 351
352 352 -nosep
353 353 shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2
354 354 0'. Simply removes all input/output separators.
355 355
356 356 -upgrade
357 357 allows you to upgrade your IPYTHONDIR configuration when you
358 358 install a new version of IPython. Since new versions may
359 359 include new command line options or example files, this copies
360 360 updated ipythonrc-type files. However, it backs up (with a
361 361 .old extension) all files which it overwrites so that you can
362 362 merge back any customizations you might have in your personal
363 363 files. Note that you should probably use %upgrade instead,
364 364 it's a safer alternative.
365 365
366 366
367 367 -Version print version information and exit.
368 368
369 369 -wxversion <string>
370 370 Select a specific version of wxPython (used in conjunction
371 371 with -wthread). Requires the wxversion module, part of recent
372 372 wxPython distributions
373 373
374 374 -xmode <modename>
375 375
376 376 Mode for exception reporting.
377 377
378 378 Valid modes: Plain, Context and Verbose.
379 379
380 380 * Plain: similar to python's normal traceback printing.
381 381 * Context: prints 5 lines of context source code around each
382 382 line in the traceback.
383 383 * Verbose: similar to Context, but additionally prints the
384 384 variables currently visible where the exception happened
385 385 (shortening their strings if too long). This can potentially be
386 386 very slow, if you happen to have a huge data structure whose
387 387 string representation is complex to compute. Your computer may
388 388 appear to freeze for a while with cpu usage at 100%. If this
389 389 occurs, you can cancel the traceback with Ctrl-C (maybe hitting it
390 390 more than once).
391 391
392 392 Interactive use
393 393 ===============
394 394
395 395 Warning: IPython relies on the existence of a global variable called
396 396 _ip which controls the shell itself. If you redefine _ip to anything,
397 397 bizarre behavior will quickly occur.
398 398
399 399 Other than the above warning, IPython is meant to work as a drop-in
400 400 replacement for the standard interactive interpreter. As such, any code
401 401 which is valid python should execute normally under IPython (cases where
402 402 this is not true should be reported as bugs). It does, however, offer
403 403 many features which are not available at a standard python prompt. What
404 404 follows is a list of these.
405 405
406 406
407 407 Caution for Windows users
408 408 -------------------------
409 409
410 410 Windows, unfortunately, uses the '\' character as a path
411 411 separator. This is a terrible choice, because '\' also represents the
412 412 escape character in most modern programming languages, including
413 413 Python. For this reason, using '/' character is recommended if you
414 414 have problems with ``\``. However, in Windows commands '/' flags
415 415 options, so you can not use it for the root directory. This means that
416 416 paths beginning at the root must be typed in a contrived manner like:
417 417 ``%copy \opt/foo/bar.txt \tmp``
418 418
419 419 .. _magic:
420 420
421 421 Magic command system
422 422 --------------------
423 423
424 424 IPython will treat any line whose first character is a % as a special
425 425 call to a 'magic' function. These allow you to control the behavior of
426 426 IPython itself, plus a lot of system-type features. They are all
427 427 prefixed with a % character, but parameters are given without
428 428 parentheses or quotes.
429 429
430 430 Example: typing '%cd mydir' (without the quotes) changes you working
431 431 directory to 'mydir', if it exists.
432 432
433 433 If you have 'automagic' enabled (in your ipythonrc file, via the command
434 434 line option -automagic or with the %automagic function), you don't need
435 435 to type in the % explicitly. IPython will scan its internal list of
436 436 magic functions and call one if it exists. With automagic on you can
437 437 then just type 'cd mydir' to go to directory 'mydir'. The automagic
438 438 system has the lowest possible precedence in name searches, so defining
439 439 an identifier with the same name as an existing magic function will
440 440 shadow it for automagic use. You can still access the shadowed magic
441 441 function by explicitly using the % character at the beginning of the line.
442 442
443 443 An example (with automagic on) should clarify all this::
444 444
445 445 In [1]: cd ipython # %cd is called by automagic
446 446
447 447 /home/fperez/ipython
448 448
449 449 In [2]: cd=1 # now cd is just a variable
450 450
451 451 In [3]: cd .. # and doesn't work as a function anymore
452 452
453 453 ------------------------------
454 454
455 455 File "<console>", line 1
456 456
457 457 cd ..
458 458
459 459 ^
460 460
461 461 SyntaxError: invalid syntax
462 462
463 463 In [4]: %cd .. # but %cd always works
464 464
465 465 /home/fperez
466 466
467 467 In [5]: del cd # if you remove the cd variable
468 468
469 469 In [6]: cd ipython # automagic can work again
470 470
471 471 /home/fperez/ipython
472 472
473 473 You can define your own magic functions to extend the system. The
474 474 following example defines a new magic command, %impall::
475 475
476 476 import IPython.ipapi
477 477
478 478 ip = IPython.ipapi.get()
479 479
480 480 def doimp(self, arg):
481 481
482 482 ip = self.api
483 483
484 484 ip.ex("import %s; reload(%s); from %s import *" % (
485 485
486 486 arg,arg,arg)
487 487
488 488 )
489 489
490 490 ip.expose_magic('impall', doimp)
491 491
492 492 You can also define your own aliased names for magic functions. In your
493 493 ipythonrc file, placing a line like::
494 494
495 495 execute __IP.magic_cl = __IP.magic_clear
496 496
497 497 will define %cl as a new name for %clear.
498 498
499 499 Type %magic for more information, including a list of all available
500 500 magic functions at any time and their docstrings. You can also type
501 501 %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for
502 502 information on the '?' system) to get information about any particular
503 503 magic function you are interested in.
504 504
505 505 The API documentation for the :mod:`IPython.Magic` module contains the full
506 506 docstrings of all currently available magic commands.
507 507
508 508
509 509 Access to the standard Python help
510 510 ----------------------------------
511 511
512 512 As of Python 2.1, a help system is available with access to object docstrings
513 513 and the Python manuals. Simply type 'help' (no quotes) to access it. You can
514 514 also type help(object) to obtain information about a given object, and
515 515 help('keyword') for information on a keyword. As noted :ref:`here
516 516 <accessing_help>`, you need to properly configure your environment variable
517 517 PYTHONDOCS for this feature to work correctly.
518 518
519 519 .. _dynamic_object_info:
520 520
521 521 Dynamic object information
522 522 --------------------------
523 523
524 524 Typing ?word or word? prints detailed information about an object. If
525 525 certain strings in the object are too long (docstrings, code, etc.) they
526 526 get snipped in the center for brevity. This system gives access variable
527 527 types and values, full source code for any object (if available),
528 528 function prototypes and other useful information.
529 529
530 530 Typing ??word or word?? gives access to the full information without
531 531 snipping long strings. Long strings are sent to the screen through the
532 532 less pager if longer than the screen and printed otherwise. On systems
533 533 lacking the less command, IPython uses a very basic internal pager.
534 534
535 535 The following magic functions are particularly useful for gathering
536 536 information about your working environment. You can get more details by
537 537 typing %magic or querying them individually (use %function_name? with or
538 538 without the %), this is just a summary:
539 539
540 540 * **%pdoc <object>**: Print (or run through a pager if too long) the
541 541 docstring for an object. If the given object is a class, it will
542 542 print both the class and the constructor docstrings.
543 543 * **%pdef <object>**: Print the definition header for any callable
544 544 object. If the object is a class, print the constructor information.
545 545 * **%psource <object>**: Print (or run through a pager if too long)
546 546 the source code for an object.
547 547 * **%pfile <object>**: Show the entire source file where an object was
548 548 defined via a pager, opening it at the line where the object
549 549 definition begins.
550 550 * **%who/%whos**: These functions give information about identifiers
551 551 you have defined interactively (not things you loaded or defined
552 552 in your configuration files). %who just prints a list of
553 553 identifiers and %whos prints a table with some basic details about
554 554 each identifier.
555 555
556 556 Note that the dynamic object information functions (?/??, %pdoc, %pfile,
557 557 %pdef, %psource) give you access to documentation even on things which
558 558 are not really defined as separate identifiers. Try for example typing
559 559 {}.get? or after doing import os, type os.path.abspath??.
560 560
561 561
562 562 .. _readline:
563 563
564 564 Readline-based features
565 565 -----------------------
566 566
567 567 These features require the GNU readline library, so they won't work if
568 568 your Python installation lacks readline support. We will first describe
569 569 the default behavior IPython uses, and then how to change it to suit
570 570 your preferences.
571 571
572 572
573 573 Command line completion
574 574 +++++++++++++++++++++++
575 575
576 576 At any time, hitting TAB will complete any available python commands or
577 577 variable names, and show you a list of the possible completions if
578 578 there's no unambiguous one. It will also complete filenames in the
579 579 current directory if no python names match what you've typed so far.
580 580
581 581
582 582 Search command history
583 583 ++++++++++++++++++++++
584 584
585 585 IPython provides two ways for searching through previous input and thus
586 586 reduce the need for repetitive typing:
587 587
588 588 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n
589 589 (next,down) to search through only the history items that match
590 590 what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank
591 591 prompt, they just behave like normal arrow keys.
592 592 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system
593 593 searches your history for lines that contain what you've typed so
594 594 far, completing as much as it can.
595 595
596 596
597 597 Persistent command history across sessions
598 598 ++++++++++++++++++++++++++++++++++++++++++
599 599
600 600 IPython will save your input history when it leaves and reload it next
601 601 time you restart it. By default, the history file is named
602 602 $IPYTHONDIR/history, but if you've loaded a named profile,
603 603 '-PROFILE_NAME' is appended to the name. This allows you to keep
604 604 separate histories related to various tasks: commands related to
605 605 numerical work will not be clobbered by a system shell history, for
606 606 example.
607 607
608 608
609 609 Autoindent
610 610 ++++++++++
611 611
612 612 IPython can recognize lines ending in ':' and indent the next line,
613 613 while also un-indenting automatically after 'raise' or 'return'.
614 614
615 615 This feature uses the readline library, so it will honor your ~/.inputrc
616 616 configuration (or whatever file your INPUTRC variable points to). Adding
617 617 the following lines to your .inputrc file can make indenting/unindenting
618 618 more convenient (M-i indents, M-u unindents)::
619 619
620 620 $if Python
621 621 "\M-i": " "
622 622 "\M-u": "\d\d\d\d"
623 623 $endif
624 624
625 625 Note that there are 4 spaces between the quote marks after "M-i" above.
626 626
627 627 Warning: this feature is ON by default, but it can cause problems with
628 628 the pasting of multi-line indented code (the pasted code gets
629 629 re-indented on each line). A magic function %autoindent allows you to
630 630 toggle it on/off at runtime. You can also disable it permanently on in
631 631 your ipythonrc file (set autoindent 0).
632 632
633 633
634 634 Customizing readline behavior
635 635 +++++++++++++++++++++++++++++
636 636
637 637 All these features are based on the GNU readline library, which has an
638 638 extremely customizable interface. Normally, readline is configured via a
639 639 file which defines the behavior of the library; the details of the
640 640 syntax for this can be found in the readline documentation available
641 641 with your system or on the Internet. IPython doesn't read this file (if
642 642 it exists) directly, but it does support passing to readline valid
643 643 options via a simple interface. In brief, you can customize readline by
644 644 setting the following options in your ipythonrc configuration file (note
645 645 that these options can not be specified at the command line):
646 646
647 647 * **readline_parse_and_bind**: this option can appear as many times as
648 648 you want, each time defining a string to be executed via a
649 649 readline.parse_and_bind() command. The syntax for valid commands
650 650 of this kind can be found by reading the documentation for the GNU
651 651 readline library, as these commands are of the kind which readline
652 652 accepts in its configuration file.
653 653 * **readline_remove_delims**: a string of characters to be removed
654 654 from the default word-delimiters list used by readline, so that
655 655 completions may be performed on strings which contain them. Do not
656 656 change the default value unless you know what you're doing.
657 657 * **readline_omit__names**: when tab-completion is enabled, hitting
658 658 <tab> after a '.' in a name will complete all attributes of an
659 659 object, including all the special methods whose names include
660 660 double underscores (like __getitem__ or __class__). If you'd
661 661 rather not see these names by default, you can set this option to
662 662 1. Note that even when this option is set, you can still see those
663 663 names by explicitly typing a _ after the period and hitting <tab>:
664 664 'name._<tab>' will always complete attribute names starting with '_'.
665 665
666 666 This option is off by default so that new users see all
667 667 attributes of any objects they are dealing with.
668 668
669 669 You will find the default values along with a corresponding detailed
670 670 explanation in your ipythonrc file.
671 671
672 672
673 673 Session logging and restoring
674 674 -----------------------------
675 675
676 676 You can log all input from a session either by starting IPython with the
677 677 command line switches -log or -logfile (see :ref:`here <command_line_options>`)
678 678 or by activating the logging at any moment with the magic function %logstart.
679 679
680 680 Log files can later be reloaded with the -logplay option and IPython
681 681 will attempt to 'replay' the log by executing all the lines in it, thus
682 682 restoring the state of a previous session. This feature is not quite
683 683 perfect, but can still be useful in many cases.
684 684
685 685 The log files can also be used as a way to have a permanent record of
686 686 any code you wrote while experimenting. Log files are regular text files
687 687 which you can later open in your favorite text editor to extract code or
688 688 to 'clean them up' before using them to replay a session.
689 689
690 690 The %logstart function for activating logging in mid-session is used as
691 691 follows:
692 692
693 693 %logstart [log_name [log_mode]]
694 694
695 695 If no name is given, it defaults to a file named 'log' in your
696 696 IPYTHONDIR directory, in 'rotate' mode (see below).
697 697
698 698 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
699 699 history up to that point and then continues logging.
700 700
701 701 %logstart takes a second optional parameter: logging mode. This can be
702 702 one of (note that the modes are given unquoted):
703 703
704 704 * [over:] overwrite existing log_name.
705 705 * [backup:] rename (if exists) to log_name~ and start log_name.
706 706 * [append:] well, that says it.
707 707 * [rotate:] create rotating logs log_name.1~, log_name.2~, etc.
708 708
709 709 The %logoff and %logon functions allow you to temporarily stop and
710 710 resume logging to a file which had previously been started with
711 711 %logstart. They will fail (with an explanation) if you try to use them
712 712 before logging has been started.
713 713
714 714 .. _system_shell_access:
715 715
716 716 System shell access
717 717 -------------------
718 718
719 719 Any input line beginning with a ! character is passed verbatim (minus
720 720 the !, of course) to the underlying operating system. For example,
721 721 typing !ls will run 'ls' in the current directory.
722 722
723 723 Manual capture of command output
724 724 --------------------------------
725 725
726 726 If the input line begins with two exclamation marks, !!, the command is
727 727 executed but its output is captured and returned as a python list, split
728 728 on newlines. Any output sent by the subprocess to standard error is
729 729 printed separately, so that the resulting list only captures standard
730 730 output. The !! syntax is a shorthand for the %sx magic command.
731 731
732 732 Finally, the %sc magic (short for 'shell capture') is similar to %sx,
733 733 but allowing more fine-grained control of the capture details, and
734 734 storing the result directly into a named variable. The direct use of
735 735 %sc is now deprecated, and you should ise the ``var = !cmd`` syntax
736 736 instead.
737 737
738 738 IPython also allows you to expand the value of python variables when
739 739 making system calls. Any python variable or expression which you prepend
740 740 with $ will get expanded before the system call is made::
741 741
742 742 In [1]: pyvar='Hello world'
743 743 In [2]: !echo "A python variable: $pyvar"
744 744 A python variable: Hello world
745 745
746 746 If you want the shell to actually see a literal $, you need to type it
747 747 twice::
748 748
749 749 In [3]: !echo "A system variable: $$HOME"
750 750 A system variable: /home/fperez
751 751
752 752 You can pass arbitrary expressions, though you'll need to delimit them
753 753 with {} if there is ambiguity as to the extent of the expression::
754 754
755 755 In [5]: x=10
756 756 In [6]: y=20
757 757 In [13]: !echo $x+y
758 758 10+y
759 759 In [7]: !echo ${x+y}
760 760 30
761 761
762 762 Even object attributes can be expanded::
763 763
764 764 In [12]: !echo $sys.argv
765 765 [/home/fperez/usr/bin/ipython]
766 766
767 767
768 768 System command aliases
769 769 ----------------------
770 770
771 771 The %alias magic function and the alias option in the ipythonrc
772 772 configuration file allow you to define magic functions which are in fact
773 773 system shell commands. These aliases can have parameters.
774 774
775 775 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
776 776
777 777 Then, typing '%alias_name params' will execute the system command 'cmd
778 778 params' (from your underlying operating system).
779 779
780 780 You can also define aliases with parameters using %s specifiers (one per
781 781 parameter). The following example defines the %parts function as an
782 782 alias to the command 'echo first %s second %s' where each %s will be
783 783 replaced by a positional parameter to the call to %parts::
784 784
785 785 In [1]: alias parts echo first %s second %s
786 786 In [2]: %parts A B
787 787 first A second B
788 788 In [3]: %parts A
789 789 Incorrect number of arguments: 2 expected.
790 790 parts is an alias to: 'echo first %s second %s'
791 791
792 792 If called with no parameters, %alias prints the table of currently
793 793 defined aliases.
794 794
795 795 The %rehash/rehashx magics allow you to load your entire $PATH as
796 796 ipython aliases. See their respective docstrings (or sec. 6.2
797 797 <#sec:magic> for further details).
798 798
799 799
800 800 .. _dreload:
801 801
802 802 Recursive reload
803 803 ----------------
804 804
805 805 The dreload function does a recursive reload of a module: changes made
806 806 to the module since you imported will actually be available without
807 807 having to exit.
808 808
809 809
810 810 Verbose and colored exception traceback printouts
811 811 -------------------------------------------------
812 812
813 813 IPython provides the option to see very detailed exception tracebacks,
814 814 which can be especially useful when debugging large programs. You can
815 815 run any Python file with the %run function to benefit from these
816 816 detailed tracebacks. Furthermore, both normal and verbose tracebacks can
817 817 be colored (if your terminal supports it) which makes them much easier
818 818 to parse visually.
819 819
820 820 See the magic xmode and colors functions for details (just type %magic).
821 821
822 822 These features are basically a terminal version of Ka-Ping Yee's cgitb
823 823 module, now part of the standard Python library.
824 824
825 825
826 826 .. _input_caching:
827 827
828 828 Input caching system
829 829 --------------------
830 830
831 831 IPython offers numbered prompts (In/Out) with input and output caching
832 832 (also referred to as 'input history'). All input is saved and can be
833 833 retrieved as variables (besides the usual arrow key recall), in
834 834 addition to the %rep magic command that brings a history entry
835 835 up for editing on the next command line.
836 836
837 837 The following GLOBAL variables always exist (so don't overwrite them!):
838 838 _i: stores previous input. _ii: next previous. _iii: next-next previous.
839 839 _ih : a list of all input _ih[n] is the input from line n and this list
840 840 is aliased to the global variable In. If you overwrite In with a
841 841 variable of your own, you can remake the assignment to the internal list
842 842 with a simple 'In=_ih'.
843 843
844 844 Additionally, global variables named _i<n> are dynamically created (<n>
845 845 being the prompt counter), such that
846 846 _i<n> == _ih[<n>] == In[<n>].
847 847
848 848 For example, what you typed at prompt 14 is available as _i14, _ih[14]
849 849 and In[14].
850 850
851 851 This allows you to easily cut and paste multi line interactive prompts
852 852 by printing them out: they print like a clean string, without prompt
853 853 characters. You can also manipulate them like regular variables (they
854 854 are strings), modify or exec them (typing 'exec _i9' will re-execute the
855 855 contents of input prompt 9, 'exec In[9:14]+In[18]' will re-execute lines
856 856 9 through 13 and line 18).
857 857
858 858 You can also re-execute multiple lines of input easily by using the
859 859 magic %macro function (which automates the process and allows
860 860 re-execution without having to type 'exec' every time). The macro system
861 861 also allows you to re-execute previous lines which include magic
862 862 function calls (which require special processing). Type %macro? or see
863 863 sec. 6.2 <#sec:magic> for more details on the macro system.
864 864
865 865 A history function %hist allows you to see any part of your input
866 866 history by printing a range of the _i variables.
867 867
868 868 You can also search ('grep') through your history by typing
869 869 '%hist -g somestring'. This also searches through the so called *shadow history*,
870 870 which remembers all the commands (apart from multiline code blocks)
871 871 you have ever entered. Handy for searching for svn/bzr URL's, IP adrresses
872 872 etc. You can bring shadow history entries listed by '%hist -g' up for editing
873 873 (or re-execution by just pressing ENTER) with %rep command. Shadow history
874 874 entries are not available as _iNUMBER variables, and they are identified by
875 875 the '0' prefix in %hist -g output. That is, history entry 12 is a normal
876 876 history entry, but 0231 is a shadow history entry.
877 877
878 878 Shadow history was added because the readline history is inherently very
879 879 unsafe - if you have multiple IPython sessions open, the last session
880 880 to close will overwrite the history of previountly closed session. Likewise,
881 881 if a crash occurs, history is never saved, whereas shadow history entries
882 882 are added after entering every command (so a command executed
883 883 in another IPython session is immediately available in other IPython
884 884 sessions that are open).
885 885
886 886 To conserve space, a command can exist in shadow history only once - it doesn't
887 887 make sense to store a common line like "cd .." a thousand times. The idea is
888 888 mainly to provide a reliable place where valuable, hard-to-remember commands can
889 889 always be retrieved, as opposed to providing an exact sequence of commands
890 890 you have entered in actual order.
891 891
892 892 Because shadow history has all the commands you have ever executed,
893 893 time taken by %hist -g will increase oven time. If it ever starts to take
894 894 too long (or it ends up containing sensitive information like passwords),
895 895 clear the shadow history by `%clear shadow_nuke`.
896 896
897 897 Time taken to add entries to shadow history should be negligible, but
898 898 in any case, if you start noticing performance degradation after using
899 899 IPython for a long time (or running a script that floods the shadow history!),
900 900 you can 'compress' the shadow history by executing
901 901 `%clear shadow_compress`. In practice, this should never be necessary
902 902 in normal use.
903 903
904 904 .. _output_caching:
905 905
906 906 Output caching system
907 907 ---------------------
908 908
909 909 For output that is returned from actions, a system similar to the input
910 910 cache exists but using _ instead of _i. Only actions that produce a
911 911 result (NOT assignments, for example) are cached. If you are familiar
912 912 with Mathematica, IPython's _ variables behave exactly like
913 913 Mathematica's % variables.
914 914
915 915 The following GLOBAL variables always exist (so don't overwrite them!):
916 916
917 917 * [_] (a single underscore) : stores previous output, like Python's
918 918 default interpreter.
919 919 * [__] (two underscores): next previous.
920 920 * [___] (three underscores): next-next previous.
921 921
922 922 Additionally, global variables named _<n> are dynamically created (<n>
923 923 being the prompt counter), such that the result of output <n> is always
924 924 available as _<n> (don't use the angle brackets, just the number, e.g.
925 925 _21).
926 926
927 927 These global variables are all stored in a global dictionary (not a
928 928 list, since it only has entries for lines which returned a result)
929 929 available under the names _oh and Out (similar to _ih and In). So the
930 930 output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you
931 931 accidentally overwrite the Out variable you can recover it by typing
932 932 'Out=_oh' at the prompt.
933 933
934 934 This system obviously can potentially put heavy memory demands on your
935 935 system, since it prevents Python's garbage collector from removing any
936 936 previously computed results. You can control how many results are kept
937 937 in memory with the option (at the command line or in your ipythonrc
938 938 file) cache_size. If you set it to 0, the whole system is completely
939 939 disabled and the prompts revert to the classic '>>>' of normal Python.
940 940
941 941
942 942 Directory history
943 943 -----------------
944 944
945 945 Your history of visited directories is kept in the global list _dh, and
946 946 the magic %cd command can be used to go to any entry in that list. The
947 947 %dhist command allows you to view this history. Do ``cd -<TAB`` to
948 948 conventiently view the directory history.
949 949
950 950
951 951 Automatic parentheses and quotes
952 952 --------------------------------
953 953
954 954 These features were adapted from Nathan Gray's LazyPython. They are
955 955 meant to allow less typing for common situations.
956 956
957 957
958 958 Automatic parentheses
959 959 ---------------------
960 960
961 961 Callable objects (i.e. functions, methods, etc) can be invoked like this
962 962 (notice the commas between the arguments)::
963 963
964 964 >>> callable_ob arg1, arg2, arg3
965 965
966 966 and the input will be translated to this::
967 967
968 968 -> callable_ob(arg1, arg2, arg3)
969 969
970 970 You can force automatic parentheses by using '/' as the first character
971 971 of a line. For example::
972 972
973 973 >>> /globals # becomes 'globals()'
974 974
975 975 Note that the '/' MUST be the first character on the line! This won't work::
976 976
977 977 >>> print /globals # syntax error
978 978
979 979 In most cases the automatic algorithm should work, so you should rarely
980 980 need to explicitly invoke /. One notable exception is if you are trying
981 981 to call a function with a list of tuples as arguments (the parenthesis
982 982 will confuse IPython)::
983 983
984 984 In [1]: zip (1,2,3),(4,5,6) # won't work
985 985
986 986 but this will work::
987 987
988 988 In [2]: /zip (1,2,3),(4,5,6)
989 989 ---> zip ((1,2,3),(4,5,6))
990 990 Out[2]= [(1, 4), (2, 5), (3, 6)]
991 991
992 992 IPython tells you that it has altered your command line by displaying
993 993 the new command line preceded by ->. e.g.::
994 994
995 995 In [18]: callable list
996 996 ----> callable (list)
997 997
998 998
999 999 Automatic quoting
1000 1000 -----------------
1001 1001
1002 1002 You can force automatic quoting of a function's arguments by using ','
1003 1003 or ';' as the first character of a line. For example::
1004 1004
1005 1005 >>> ,my_function /home/me # becomes my_function("/home/me")
1006 1006
1007 1007 If you use ';' instead, the whole argument is quoted as a single string
1008 1008 (while ',' splits on whitespace)::
1009 1009
1010 1010 >>> ,my_function a b c # becomes my_function("a","b","c")
1011 1011
1012 1012 >>> ;my_function a b c # becomes my_function("a b c")
1013 1013
1014 1014 Note that the ',' or ';' MUST be the first character on the line! This
1015 1015 won't work::
1016 1016
1017 1017 >>> x = ,my_function /home/me # syntax error
1018 1018
1019 1019 IPython as your default Python environment
1020 1020 ==========================================
1021 1021
1022 1022 Python honors the environment variable PYTHONSTARTUP and will execute at
1023 1023 startup the file referenced by this variable. If you put at the end of
1024 1024 this file the following two lines of code::
1025 1025
1026 1026 import IPython
1027 1027 IPython.Shell.IPShell().mainloop(sys_exit=1)
1028 1028
1029 1029 then IPython will be your working environment anytime you start Python.
1030 1030 The sys_exit=1 is needed to have IPython issue a call to sys.exit() when
1031 1031 it finishes, otherwise you'll be back at the normal Python '>>>'
1032 1032 prompt.
1033 1033
1034 1034 This is probably useful to developers who manage multiple Python
1035 1035 versions and don't want to have correspondingly multiple IPython
1036 1036 versions. Note that in this mode, there is no way to pass IPython any
1037 1037 command-line options, as those are trapped first by Python itself.
1038 1038
1039 1039 .. _Embedding:
1040 1040
1041 1041 Embedding IPython
1042 1042 =================
1043 1043
1044 1044 It is possible to start an IPython instance inside your own Python
1045 1045 programs. This allows you to evaluate dynamically the state of your
1046 1046 code, operate with your variables, analyze them, etc. Note however that
1047 1047 any changes you make to values while in the shell do not propagate back
1048 1048 to the running code, so it is safe to modify your values because you
1049 1049 won't break your code in bizarre ways by doing so.
1050 1050
1051 1051 This feature allows you to easily have a fully functional python
1052 1052 environment for doing object introspection anywhere in your code with a
1053 1053 simple function call. In some cases a simple print statement is enough,
1054 1054 but if you need to do more detailed analysis of a code fragment this
1055 1055 feature can be very valuable.
1056 1056
1057 1057 It can also be useful in scientific computing situations where it is
1058 1058 common to need to do some automatic, computationally intensive part and
1059 1059 then stop to look at data, plots, etc.
1060 1060 Opening an IPython instance will give you full access to your data and
1061 1061 functions, and you can resume program execution once you are done with
1062 1062 the interactive part (perhaps to stop again later, as many times as
1063 1063 needed).
1064 1064
1065 1065 The following code snippet is the bare minimum you need to include in
1066 1066 your Python programs for this to work (detailed examples follow later)::
1067 1067
1068 1068 from IPython.Shell import IPShellEmbed
1069 1069
1070 1070 ipshell = IPShellEmbed()
1071 1071
1072 1072 ipshell() # this call anywhere in your program will start IPython
1073 1073
1074 1074 You can run embedded instances even in code which is itself being run at
1075 1075 the IPython interactive prompt with '%run <filename>'. Since it's easy
1076 1076 to get lost as to where you are (in your top-level IPython or in your
1077 1077 embedded one), it's a good idea in such cases to set the in/out prompts
1078 1078 to something different for the embedded instances. The code examples
1079 1079 below illustrate this.
1080 1080
1081 1081 You can also have multiple IPython instances in your program and open
1082 1082 them separately, for example with different options for data
1083 1083 presentation. If you close and open the same instance multiple times,
1084 1084 its prompt counters simply continue from each execution to the next.
1085 1085
1086 1086 Please look at the docstrings in the Shell.py module for more details on
1087 1087 the use of this system.
1088 1088
1089 1089 The following sample file illustrating how to use the embedding
1090 1090 functionality is provided in the examples directory as example-embed.py.
1091 1091 It should be fairly self-explanatory::
1092 1092
1093 1093
1094 1094 #!/usr/bin/env python
1095 1095
1096 1096 """An example of how to embed an IPython shell into a running program.
1097 1097
1098 1098 Please see the documentation in the IPython.Shell module for more details.
1099 1099
1100 1100 The accompanying file example-embed-short.py has quick code fragments for
1101 1101 embedding which you can cut and paste in your code once you understand how
1102 1102 things work.
1103 1103
1104 1104 The code in this file is deliberately extra-verbose, meant for learning."""
1105 1105
1106 1106 # The basics to get you going:
1107 1107
1108 1108 # IPython sets the __IPYTHON__ variable so you can know if you have nested
1109 1109 # copies running.
1110 1110
1111 1111 # Try running this code both at the command line and from inside IPython (with
1112 1112 # %run example-embed.py)
1113 1113 try:
1114 1114 __IPYTHON__
1115 1115 except NameError:
1116 1116 nested = 0
1117 1117 args = ['']
1118 1118 else:
1119 1119 print "Running nested copies of IPython."
1120 1120 print "The prompts for the nested copy have been modified"
1121 1121 nested = 1
1122 1122 # what the embedded instance will see as sys.argv:
1123 1123 args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ',
1124 1124 '-po','Out<\\#>: ','-nosep']
1125 1125
1126 1126 # First import the embeddable shell class
1127 1127 from IPython.Shell import IPShellEmbed
1128 1128
1129 1129 # Now create an instance of the embeddable shell. The first argument is a
1130 1130 # string with options exactly as you would type them if you were starting
1131 1131 # IPython at the system command line. Any parameters you want to define for
1132 1132 # configuration can thus be specified here.
1133 1133 ipshell = IPShellEmbed(args,
1134 1134 banner = 'Dropping into IPython',
1135 1135 exit_msg = 'Leaving Interpreter, back to program.')
1136 1136
1137 1137 # Make a second instance, you can have as many as you want.
1138 1138 if nested:
1139 1139 args[1] = 'In2<\\#>'
1140 1140 else:
1141 1141 args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ',
1142 1142 '-po','Out<\\#>: ','-nosep']
1143 1143 ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.')
1144 1144
1145 1145 print '\nHello. This is printed from the main controller program.\n'
1146 1146
1147 1147 # You can then call ipshell() anywhere you need it (with an optional
1148 1148 # message):
1149 1149 ipshell('***Called from top level. '
1150 1150 'Hit Ctrl-D to exit interpreter and continue program.\n'
1151 1151 'Note that if you use %kill_embedded, you can fully deactivate\n'
1152 1152 'This embedded instance so it will never turn on again')
1153 1153
1154 1154 print '\nBack in caller program, moving along...\n'
1155 1155
1156 1156 #---------------------------------------------------------------------------
1157 1157 # More details:
1158 1158
1159 1159 # IPShellEmbed instances don't print the standard system banner and
1160 1160 # messages. The IPython banner (which actually may contain initialization
1161 1161 # messages) is available as <instance>.IP.BANNER in case you want it.
1162 1162
1163 1163 # IPShellEmbed instances print the following information everytime they
1164 1164 # start:
1165 1165
1166 1166 # - A global startup banner.
1167 1167
1168 1168 # - A call-specific header string, which you can use to indicate where in the
1169 1169 # execution flow the shell is starting.
1170 1170
1171 1171 # They also print an exit message every time they exit.
1172 1172
1173 1173 # Both the startup banner and the exit message default to None, and can be set
1174 1174 # either at the instance constructor or at any other time with the
1175 1175 # set_banner() and set_exit_msg() methods.
1176 1176
1177 1177 # The shell instance can be also put in 'dummy' mode globally or on a per-call
1178 1178 # basis. This gives you fine control for debugging without having to change
1179 1179 # code all over the place.
1180 1180
1181 1181 # The code below illustrates all this.
1182 1182
1183 1183
1184 1184 # This is how the global banner and exit_msg can be reset at any point
1185 1185 ipshell.set_banner('Entering interpreter - New Banner')
1186 1186 ipshell.set_exit_msg('Leaving interpreter - New exit_msg')
1187 1187
1188 1188 def foo(m):
1189 1189 s = 'spam'
1190 1190 ipshell('***In foo(). Try @whos, or print s or m:')
1191 1191 print 'foo says m = ',m
1192 1192
1193 1193 def bar(n):
1194 1194 s = 'eggs'
1195 1195 ipshell('***In bar(). Try @whos, or print s or n:')
1196 1196 print 'bar says n = ',n
1197 1197
1198 1198 # Some calls to the above functions which will trigger IPython:
1199 1199 print 'Main program calling foo("eggs")\n'
1200 1200 foo('eggs')
1201 1201
1202 1202 # The shell can be put in 'dummy' mode where calls to it silently return. This
1203 1203 # allows you, for example, to globally turn off debugging for a program with a
1204 1204 # single call.
1205 1205 ipshell.set_dummy_mode(1)
1206 1206 print '\nTrying to call IPython which is now "dummy":'
1207 1207 ipshell()
1208 1208 print 'Nothing happened...'
1209 1209 # The global 'dummy' mode can still be overridden for a single call
1210 1210 print '\nOverriding dummy mode manually:'
1211 1211 ipshell(dummy=0)
1212 1212
1213 1213 # Reactivate the IPython shell
1214 1214 ipshell.set_dummy_mode(0)
1215 1215
1216 1216 print 'You can even have multiple embedded instances:'
1217 1217 ipshell2()
1218 1218
1219 1219 print '\nMain program calling bar("spam")\n'
1220 1220 bar('spam')
1221 1221
1222 1222 print 'Main program finished. Bye!'
1223 1223
1224 1224 #********************** End of file <example-embed.py> ***********************
1225 1225
1226 1226 Once you understand how the system functions, you can use the following
1227 1227 code fragments in your programs which are ready for cut and paste::
1228 1228
1229 1229
1230 1230 """Quick code snippets for embedding IPython into other programs.
1231 1231
1232 1232 See example-embed.py for full details, this file has the bare minimum code for
1233 1233 cut and paste use once you understand how to use the system."""
1234 1234
1235 1235 #---------------------------------------------------------------------------
1236 1236 # This code loads IPython but modifies a few things if it detects it's running
1237 1237 # embedded in another IPython session (helps avoid confusion)
1238 1238
1239 1239 try:
1240 1240 __IPYTHON__
1241 1241 except NameError:
1242 1242 argv = ['']
1243 1243 banner = exit_msg = ''
1244 1244 else:
1245 1245 # Command-line options for IPython (a list like sys.argv)
1246 1246 argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:']
1247 1247 banner = '*** Nested interpreter ***'
1248 1248 exit_msg = '*** Back in main IPython ***'
1249 1249
1250 1250 # First import the embeddable shell class
1251 1251 from IPython.Shell import IPShellEmbed
1252 1252 # Now create the IPython shell instance. Put ipshell() anywhere in your code
1253 1253 # where you want it to open.
1254 1254 ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg)
1255 1255
1256 1256 #---------------------------------------------------------------------------
1257 1257 # This code will load an embeddable IPython shell always with no changes for
1258 1258 # nested embededings.
1259 1259
1260 1260 from IPython.Shell import IPShellEmbed
1261 1261 ipshell = IPShellEmbed()
1262 1262 # Now ipshell() will open IPython anywhere in the code.
1263 1263
1264 1264 #---------------------------------------------------------------------------
1265 1265 # This code loads an embeddable shell only if NOT running inside
1266 1266 # IPython. Inside IPython, the embeddable shell variable ipshell is just a
1267 1267 # dummy function.
1268 1268
1269 1269 try:
1270 1270 __IPYTHON__
1271 1271 except NameError:
1272 1272 from IPython.Shell import IPShellEmbed
1273 1273 ipshell = IPShellEmbed()
1274 1274 # Now ipshell() will open IPython anywhere in the code
1275 1275 else:
1276 1276 # Define a dummy ipshell() so the same code doesn't crash inside an
1277 1277 # interactive IPython
1278 1278 def ipshell(): pass
1279 1279
1280 1280 #******************* End of file <example-embed-short.py> ********************
1281 1281
1282 1282 Using the Python debugger (pdb)
1283 1283 ===============================
1284 1284
1285 1285 Running entire programs via pdb
1286 1286 -------------------------------
1287 1287
1288 1288 pdb, the Python debugger, is a powerful interactive debugger which
1289 1289 allows you to step through code, set breakpoints, watch variables,
1290 1290 etc. IPython makes it very easy to start any script under the control
1291 1291 of pdb, regardless of whether you have wrapped it into a 'main()'
1292 1292 function or not. For this, simply type '%run -d myscript' at an
1293 1293 IPython prompt. See the %run command's documentation (via '%run?' or
1294 1294 in Sec. magic_ for more details, including how to control where pdb
1295 1295 will stop execution first.
1296 1296
1297 1297 For more information on the use of the pdb debugger, read the included
1298 1298 pdb.doc file (part of the standard Python distribution). On a stock
1299 1299 Linux system it is located at /usr/lib/python2.3/pdb.doc, but the
1300 1300 easiest way to read it is by using the help() function of the pdb module
1301 1301 as follows (in an IPython prompt):
1302 1302
1303 1303 In [1]: import pdb
1304 1304 In [2]: pdb.help()
1305 1305
1306 1306 This will load the pdb.doc document in a file viewer for you automatically.
1307 1307
1308 1308
1309 1309 Automatic invocation of pdb on exceptions
1310 1310 -----------------------------------------
1311 1311
1312 1312 IPython, if started with the -pdb option (or if the option is set in
1313 1313 your rc file) can call the Python pdb debugger every time your code
1314 1314 triggers an uncaught exception. This feature
1315 1315 can also be toggled at any time with the %pdb magic command. This can be
1316 1316 extremely useful in order to find the origin of subtle bugs, because pdb
1317 1317 opens up at the point in your code which triggered the exception, and
1318 1318 while your program is at this point 'dead', all the data is still
1319 1319 available and you can walk up and down the stack frame and understand
1320 1320 the origin of the problem.
1321 1321
1322 1322 Furthermore, you can use these debugging facilities both with the
1323 1323 embedded IPython mode and without IPython at all. For an embedded shell
1324 1324 (see sec. Embedding_), simply call the constructor with
1325 1325 '-pdb' in the argument string and automatically pdb will be called if an
1326 1326 uncaught exception is triggered by your code.
1327 1327
1328 1328 For stand-alone use of the feature in your programs which do not use
1329 1329 IPython at all, put the following lines toward the top of your 'main'
1330 1330 routine::
1331 1331
1332 import sys,IPython.ultraTB
1333 sys.excepthook = IPython.ultraTB.FormattedTB(mode='Verbose',
1332 import sys
1333 from IPython.core import ultratb
1334 sys.excepthook = ultratb.FormattedTB(mode='Verbose',
1334 1335 color_scheme='Linux', call_pdb=1)
1335 1336
1336 1337 The mode keyword can be either 'Verbose' or 'Plain', giving either very
1337 1338 detailed or normal tracebacks respectively. The color_scheme keyword can
1338 1339 be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same
1339 1340 options which can be set in IPython with -colors and -xmode.
1340 1341
1341 1342 This will give any of your programs detailed, colored tracebacks with
1342 1343 automatic invocation of pdb.
1343 1344
1344 1345
1345 1346 Extensions for syntax processing
1346 1347 ================================
1347 1348
1348 1349 This isn't for the faint of heart, because the potential for breaking
1349 1350 things is quite high. But it can be a very powerful and useful feature.
1350 1351 In a nutshell, you can redefine the way IPython processes the user input
1351 1352 line to accept new, special extensions to the syntax without needing to
1352 1353 change any of IPython's own code.
1353 1354
1354 1355 In the IPython/extensions directory you will find some examples
1355 1356 supplied, which we will briefly describe now. These can be used 'as is'
1356 1357 (and both provide very useful functionality), or you can use them as a
1357 1358 starting point for writing your own extensions.
1358 1359
1359 1360
1360 1361 Pasting of code starting with '>>> ' or '... '
1361 1362 ----------------------------------------------
1362 1363
1363 1364 In the python tutorial it is common to find code examples which have
1364 1365 been taken from real python sessions. The problem with those is that all
1365 1366 the lines begin with either '>>> ' or '... ', which makes it impossible
1366 1367 to paste them all at once. One must instead do a line by line manual
1367 1368 copying, carefully removing the leading extraneous characters.
1368 1369
1369 1370 This extension identifies those starting characters and removes them
1370 1371 from the input automatically, so that one can paste multi-line examples
1371 1372 directly into IPython, saving a lot of time. Please look at the file
1372 1373 InterpreterPasteInput.py in the IPython/extensions directory for details
1373 1374 on how this is done.
1374 1375
1375 1376 IPython comes with a special profile enabling this feature, called
1376 1377 tutorial. Simply start IPython via 'ipython -p tutorial' and the feature
1377 1378 will be available. In a normal IPython session you can activate the
1378 1379 feature by importing the corresponding module with:
1379 1380 In [1]: import IPython.extensions.InterpreterPasteInput
1380 1381
1381 1382 The following is a 'screenshot' of how things work when this extension
1382 1383 is on, copying an example from the standard tutorial::
1383 1384
1384 1385 IPython profile: tutorial
1385 1386
1386 1387 *** Pasting of code with ">>>" or "..." has been enabled.
1387 1388
1388 1389 In [1]: >>> def fib2(n): # return Fibonacci series up to n
1389 1390 ...: ... """Return a list containing the Fibonacci series up to
1390 1391 n."""
1391 1392 ...: ... result = []
1392 1393 ...: ... a, b = 0, 1
1393 1394 ...: ... while b < n:
1394 1395 ...: ... result.append(b) # see below
1395 1396 ...: ... a, b = b, a+b
1396 1397 ...: ... return result
1397 1398 ...:
1398 1399
1399 1400 In [2]: fib2(10)
1400 1401 Out[2]: [1, 1, 2, 3, 5, 8]
1401 1402
1402 1403 Note that as currently written, this extension does not recognize
1403 1404 IPython's prompts for pasting. Those are more complicated, since the
1404 1405 user can change them very easily, they involve numbers and can vary in
1405 1406 length. One could however extract all the relevant information from the
1406 1407 IPython instance and build an appropriate regular expression. This is
1407 1408 left as an exercise for the reader.
1408 1409
1409 1410
1410 1411 Input of physical quantities with units
1411 1412 ---------------------------------------
1412 1413
1413 1414 The module PhysicalQInput allows a simplified form of input for physical
1414 1415 quantities with units. This file is meant to be used in conjunction with
1415 1416 the PhysicalQInteractive module (in the same directory) and
1416 1417 Physics.PhysicalQuantities from Konrad Hinsen's ScientificPython
1417 1418 (http://dirac.cnrs-orleans.fr/ScientificPython/).
1418 1419
1419 1420 The Physics.PhysicalQuantities module defines PhysicalQuantity objects,
1420 1421 but these must be declared as instances of a class. For example, to
1421 1422 define v as a velocity of 3 m/s, normally you would write::
1422 1423
1423 1424 In [1]: v = PhysicalQuantity(3,'m/s')
1424 1425
1425 1426 Using the PhysicalQ_Input extension this can be input instead as:
1426 1427 In [1]: v = 3 m/s
1427 1428 which is much more convenient for interactive use (even though it is
1428 1429 blatantly invalid Python syntax).
1429 1430
1430 1431 The physics profile supplied with IPython (enabled via 'ipython -p
1431 1432 physics') uses these extensions, which you can also activate with:
1432 1433
1433 1434 from math import * # math MUST be imported BEFORE PhysicalQInteractive
1434 1435 from IPython.extensions.PhysicalQInteractive import *
1435 1436 import IPython.extensions.PhysicalQInput
1436 1437
1437 1438
1438 1439 Threading support
1439 1440 =================
1440 1441
1441 1442 WARNING: The threading support is still somewhat experimental, and it
1442 1443 has only seen reasonable testing under Linux. Threaded code is
1443 1444 particularly tricky to debug, and it tends to show extremely
1444 1445 platform-dependent behavior. Since I only have access to Linux machines,
1445 1446 I will have to rely on user's experiences and assistance for this area
1446 1447 of IPython to improve under other platforms.
1447 1448
1448 1449 IPython, via the -gthread , -qthread, -q4thread and -wthread options
1449 1450 (described in Sec. `Threading options`_), can run in
1450 1451 multithreaded mode to support pyGTK, Qt3, Qt4 and WXPython applications
1451 1452 respectively. These GUI toolkits need to control the python main loop of
1452 1453 execution, so under a normal Python interpreter, starting a pyGTK, Qt3,
1453 1454 Qt4 or WXPython application will immediately freeze the shell.
1454 1455
1455 1456 IPython, with one of these options (you can only use one at a time),
1456 1457 separates the graphical loop and IPython's code execution run into
1457 1458 different threads. This allows you to test interactively (with %run, for
1458 1459 example) your GUI code without blocking.
1459 1460
1460 1461 A nice mini-tutorial on using IPython along with the Qt Designer
1461 1462 application is available at the SciPy wiki:
1462 1463 http://www.scipy.org/Cookbook/Matplotlib/Qt_with_IPython_and_Designer.
1463 1464
1464 1465
1465 1466 Tk issues
1466 1467 ---------
1467 1468
1468 1469 As indicated in Sec. `Threading options`_, a special -tk option is
1469 1470 provided to try and allow Tk graphical applications to coexist
1470 1471 interactively with WX, Qt or GTK ones. Whether this works at all,
1471 1472 however, is very platform and configuration dependent. Please
1472 1473 experiment with simple test cases before committing to using this
1473 1474 combination of Tk and GTK/Qt/WX threading in a production environment.
1474 1475
1475 1476
1476 1477 I/O pitfalls
1477 1478 ------------
1478 1479
1479 1480 Be mindful that the Python interpreter switches between threads every
1480 1481 $N$ bytecodes, where the default value as of Python 2.3 is $N=100.$ This
1481 1482 value can be read by using the sys.getcheckinterval() function, and it
1482 1483 can be reset via sys.setcheckinterval(N). This switching of threads can
1483 1484 cause subtly confusing effects if one of your threads is doing file I/O.
1484 1485 In text mode, most systems only flush file buffers when they encounter a
1485 1486 '\n'. An instruction as simple as::
1486 1487
1487 1488 print >> filehandle, ''hello world''
1488 1489
1489 1490 actually consists of several bytecodes, so it is possible that the
1490 1491 newline does not reach your file before the next thread switch.
1491 1492 Similarly, if you are writing to a file in binary mode, the file won't
1492 1493 be flushed until the buffer fills, and your other thread may see
1493 1494 apparently truncated files.
1494 1495
1495 1496 For this reason, if you are using IPython's thread support and have (for
1496 1497 example) a GUI application which will read data generated by files
1497 1498 written to from the IPython thread, the safest approach is to open all
1498 1499 of your files in unbuffered mode (the third argument to the file/open
1499 1500 function is the buffering value)::
1500 1501
1501 1502 filehandle = open(filename,mode,0)
1502 1503
1503 1504 This is obviously a brute force way of avoiding race conditions with the
1504 1505 file buffering. If you want to do it cleanly, and you have a resource
1505 1506 which is being shared by the interactive IPython loop and your GUI
1506 1507 thread, you should really handle it with thread locking and
1507 1508 syncrhonization properties. The Python documentation discusses these.
1508 1509
1509 1510 .. _interactive_demos:
1510 1511
1511 1512 Interactive demos with IPython
1512 1513 ==============================
1513 1514
1514 1515 IPython ships with a basic system for running scripts interactively in
1515 1516 sections, useful when presenting code to audiences. A few tags embedded
1516 1517 in comments (so that the script remains valid Python code) divide a file
1517 1518 into separate blocks, and the demo can be run one block at a time, with
1518 1519 IPython printing (with syntax highlighting) the block before executing
1519 1520 it, and returning to the interactive prompt after each block. The
1520 1521 interactive namespace is updated after each block is run with the
1521 1522 contents of the demo's namespace.
1522 1523
1523 1524 This allows you to show a piece of code, run it and then execute
1524 1525 interactively commands based on the variables just created. Once you
1525 1526 want to continue, you simply execute the next block of the demo. The
1526 1527 following listing shows the markup necessary for dividing a script into
1527 1528 sections for execution as a demo::
1528 1529
1529 1530
1530 1531 """A simple interactive demo to illustrate the use of IPython's Demo class.
1531 1532
1532 1533 Any python script can be run as a demo, but that does little more than showing
1533 1534 it on-screen, syntax-highlighted in one shot. If you add a little simple
1534 1535 markup, you can stop at specified intervals and return to the ipython prompt,
1535 1536 resuming execution later.
1536 1537 """
1537 1538
1538 1539 print 'Hello, welcome to an interactive IPython demo.'
1539 1540 print 'Executing this block should require confirmation before proceeding,'
1540 1541 print 'unless auto_all has been set to true in the demo object'
1541 1542
1542 1543 # The mark below defines a block boundary, which is a point where IPython will
1543 1544 # stop execution and return to the interactive prompt.
1544 1545 # Note that in actual interactive execution,
1545 1546 # <demo> --- stop ---
1546 1547
1547 1548 x = 1
1548 1549 y = 2
1549 1550
1550 1551 # <demo> --- stop ---
1551 1552
1552 1553 # the mark below makes this block as silent
1553 1554 # <demo> silent
1554 1555
1555 1556 print 'This is a silent block, which gets executed but not printed.'
1556 1557
1557 1558 # <demo> --- stop ---
1558 1559 # <demo> auto
1559 1560 print 'This is an automatic block.'
1560 1561 print 'It is executed without asking for confirmation, but printed.'
1561 1562 z = x+y
1562 1563
1563 1564 print 'z=',x
1564 1565
1565 1566 # <demo> --- stop ---
1566 1567 # This is just another normal block.
1567 1568 print 'z is now:', z
1568 1569
1569 1570 print 'bye!'
1570 1571
1571 1572 In order to run a file as a demo, you must first make a Demo object out
1572 1573 of it. If the file is named myscript.py, the following code will make a
1573 1574 demo::
1574 1575
1575 1576 from IPython.demo import Demo
1576 1577
1577 1578 mydemo = Demo('myscript.py')
1578 1579
1579 1580 This creates the mydemo object, whose blocks you run one at a time by
1580 1581 simply calling the object with no arguments. If you have autocall active
1581 1582 in IPython (the default), all you need to do is type::
1582 1583
1583 1584 mydemo
1584 1585
1585 1586 and IPython will call it, executing each block. Demo objects can be
1586 1587 restarted, you can move forward or back skipping blocks, re-execute the
1587 1588 last block, etc. Simply use the Tab key on a demo object to see its
1588 1589 methods, and call '?' on them to see their docstrings for more usage
1589 1590 details. In addition, the demo module itself contains a comprehensive
1590 1591 docstring, which you can access via::
1591 1592
1592 1593 from IPython import demo
1593 1594
1594 1595 demo?
1595 1596
1596 1597 Limitations: It is important to note that these demos are limited to
1597 1598 fairly simple uses. In particular, you can not put division marks in
1598 1599 indented code (loops, if statements, function definitions, etc.)
1599 1600 Supporting something like this would basically require tracking the
1600 1601 internal execution state of the Python interpreter, so only top-level
1601 1602 divisions are allowed. If you want to be able to open an IPython
1602 1603 instance at an arbitrary point in a program, you can use IPython's
1603 1604 embedding facilities, described in detail in Sec. 9
1604 1605
1605 1606
1606 1607 .. _Matplotlib support:
1607 1608
1608 1609 Plotting with matplotlib
1609 1610 ========================
1610 1611
1611 1612 The matplotlib library (http://matplotlib.sourceforge.net
1612 1613 http://matplotlib.sourceforge.net) provides high quality 2D plotting for
1613 1614 Python. Matplotlib can produce plots on screen using a variety of GUI
1614 1615 toolkits, including Tk, GTK and WXPython. It also provides a number of
1615 1616 commands useful for scientific computing, all with a syntax compatible
1616 1617 with that of the popular Matlab program.
1617 1618
1618 1619 IPython accepts the special option -pylab (see :ref:`here
1619 1620 <command_line_options>`). This configures it to support matplotlib, honoring
1620 1621 the settings in the .matplotlibrc file. IPython will detect the user's choice
1621 1622 of matplotlib GUI backend, and automatically select the proper threading model
1622 1623 to prevent blocking. It also sets matplotlib in interactive mode and modifies
1623 1624 %run slightly, so that any matplotlib-based script can be executed using %run
1624 1625 and the final show() command does not block the interactive shell.
1625 1626
1626 1627 The -pylab option must be given first in order for IPython to configure its
1627 1628 threading mode. However, you can still issue other options afterwards. This
1628 1629 allows you to have a matplotlib-based environment customized with additional
1629 1630 modules using the standard IPython profile mechanism (see :ref:`here
1630 1631 <profiles>`): ``ipython -pylab -p myprofile`` will load the profile defined in
1631 1632 ipythonrc-myprofile after configuring matplotlib.
1 NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (1067 lines changed) Show them Hide them
General Comments 0
You need to be logged in to leave comments. Login now