Show More
@@ -1,525 +1,525 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Pdb debugger class. |
|
4 | 4 | |
|
5 | 5 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
6 | 6 | the command line completion of other programs which include this isn't |
|
7 | 7 | damaged. |
|
8 | 8 | |
|
9 | 9 | In the future, this class will be expanded with improvements over the standard |
|
10 | 10 | pdb. |
|
11 | 11 | |
|
12 | 12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor |
|
13 | 13 | changes. Licensing should therefore be under the standard Python terms. For |
|
14 | 14 | details on the PSF (Python Software Foundation) standard license, see: |
|
15 | 15 | |
|
16 | 16 | http://www.python.org/2.2.3/license.html""" |
|
17 | 17 | |
|
18 | 18 | #***************************************************************************** |
|
19 | 19 | # |
|
20 | 20 | # This file is licensed under the PSF license. |
|
21 | 21 | # |
|
22 | 22 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
23 | 23 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> |
|
24 | 24 | # |
|
25 | 25 | # |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | import bdb |
|
29 | 29 | import linecache |
|
30 | 30 | import sys |
|
31 | 31 | |
|
32 | 32 | from IPython.utils import PyColorize |
|
33 | 33 | from IPython.core import ipapi |
|
34 | 34 | from IPython.utils import coloransi, io |
|
35 | 35 | from IPython.core.excolors import exception_colors |
|
36 | 36 | |
|
37 | 37 | # See if we can use pydb. |
|
38 | 38 | has_pydb = False |
|
39 | 39 | prompt = 'ipdb> ' |
|
40 | 40 | #We have to check this directly from sys.argv, config struct not yet available |
|
41 | 41 | if '--pydb' in sys.argv: |
|
42 | 42 | try: |
|
43 | 43 | import pydb |
|
44 | 44 | if hasattr(pydb.pydb, "runl") and pydb.version>'1.17': |
|
45 | 45 | # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we |
|
46 | 46 | # better protect against it. |
|
47 | 47 | has_pydb = True |
|
48 | 48 | except ImportError: |
|
49 | 49 | print "Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available" |
|
50 | 50 | |
|
51 | 51 | if has_pydb: |
|
52 | 52 | from pydb import Pdb as OldPdb |
|
53 | 53 | #print "Using pydb for %run -d and post-mortem" #dbg |
|
54 | 54 | prompt = 'ipydb> ' |
|
55 | 55 | else: |
|
56 | 56 | from pdb import Pdb as OldPdb |
|
57 | 57 | |
|
58 | 58 | # Allow the set_trace code to operate outside of an ipython instance, even if |
|
59 | 59 | # it does so with some limitations. The rest of this support is implemented in |
|
60 | 60 | # the Tracer constructor. |
|
61 | 61 | def BdbQuit_excepthook(et,ev,tb): |
|
62 | 62 | if et==bdb.BdbQuit: |
|
63 | 63 | print 'Exiting Debugger.' |
|
64 | 64 | else: |
|
65 | 65 | BdbQuit_excepthook.excepthook_ori(et,ev,tb) |
|
66 | 66 | |
|
67 | 67 | def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None): |
|
68 | 68 | print 'Exiting Debugger.' |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | class Tracer(object): |
|
72 | 72 | """Class for local debugging, similar to pdb.set_trace. |
|
73 | 73 | |
|
74 | 74 | Instances of this class, when called, behave like pdb.set_trace, but |
|
75 | 75 | providing IPython's enhanced capabilities. |
|
76 | 76 | |
|
77 | 77 | This is implemented as a class which must be initialized in your own code |
|
78 | 78 | and not as a standalone function because we need to detect at runtime |
|
79 | 79 | whether IPython is already active or not. That detection is done in the |
|
80 | 80 | constructor, ensuring that this code plays nicely with a running IPython, |
|
81 | 81 | while functioning acceptably (though with limitations) if outside of it. |
|
82 | 82 | """ |
|
83 | 83 | |
|
84 | 84 | def __init__(self,colors=None): |
|
85 | 85 | """Create a local debugger instance. |
|
86 | 86 | |
|
87 | 87 | :Parameters: |
|
88 | 88 | |
|
89 | 89 | - `colors` (None): a string containing the name of the color scheme to |
|
90 | 90 | use, it must be one of IPython's valid color schemes. If not given, the |
|
91 | 91 | function will default to the current IPython scheme when running inside |
|
92 | 92 | IPython, and to 'NoColor' otherwise. |
|
93 | 93 | |
|
94 | 94 | Usage example: |
|
95 | 95 | |
|
96 | 96 | from IPython.core.debugger import Tracer; debug_here = Tracer() |
|
97 | 97 | |
|
98 | 98 | ... later in your code |
|
99 | 99 | debug_here() # -> will open up the debugger at that point. |
|
100 | 100 | |
|
101 | 101 | Once the debugger activates, you can use all of its regular commands to |
|
102 | 102 | step through code, set breakpoints, etc. See the pdb documentation |
|
103 | 103 | from the Python standard library for usage details. |
|
104 | 104 | """ |
|
105 | 105 | |
|
106 | 106 | try: |
|
107 | 107 | ip = get_ipython() |
|
108 | 108 | except NameError: |
|
109 | 109 | # Outside of ipython, we set our own exception hook manually |
|
110 | 110 | BdbQuit_excepthook.excepthook_ori = sys.excepthook |
|
111 | 111 | sys.excepthook = BdbQuit_excepthook |
|
112 | 112 | def_colors = 'NoColor' |
|
113 | 113 | try: |
|
114 | 114 | # Limited tab completion support |
|
115 | 115 | import readline |
|
116 | 116 | readline.parse_and_bind('tab: complete') |
|
117 | 117 | except ImportError: |
|
118 | 118 | pass |
|
119 | 119 | else: |
|
120 | 120 | # In ipython, we use its custom exception handler mechanism |
|
121 | 121 | def_colors = ip.colors |
|
122 | 122 | ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook) |
|
123 | 123 | |
|
124 | 124 | if colors is None: |
|
125 | 125 | colors = def_colors |
|
126 | 126 | |
|
127 | 127 | # The stdlib debugger internally uses a modified repr from the `repr` |
|
128 | 128 | # module, that limits the length of printed strings to a hardcoded |
|
129 | 129 | # limit of 30 characters. That much trimming is too aggressive, let's |
|
130 | 130 | # at least raise that limit to 80 chars, which should be enough for |
|
131 | 131 | # most interactive uses. |
|
132 | 132 | try: |
|
133 | 133 | from repr import aRepr |
|
134 | 134 | aRepr.maxstring = 80 |
|
135 | 135 | except: |
|
136 | 136 | # This is only a user-facing convenience, so any error we encounter |
|
137 | 137 | # here can be warned about but can be otherwise ignored. These |
|
138 | 138 | # printouts will tell us about problems if this API changes |
|
139 | 139 | import traceback |
|
140 | 140 | traceback.print_exc() |
|
141 | 141 | |
|
142 | 142 | self.debugger = Pdb(colors) |
|
143 | 143 | |
|
144 | 144 | def __call__(self): |
|
145 | 145 | """Starts an interactive debugger at the point where called. |
|
146 | 146 | |
|
147 | 147 | This is similar to the pdb.set_trace() function from the std lib, but |
|
148 | 148 | using IPython's enhanced debugger.""" |
|
149 | 149 | |
|
150 | 150 | self.debugger.set_trace(sys._getframe().f_back) |
|
151 | 151 | |
|
152 | 152 | |
|
153 | 153 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): |
|
154 | 154 | """Make new_fn have old_fn's doc string. This is particularly useful |
|
155 | 155 | for the do_... commands that hook into the help system. |
|
156 | 156 | Adapted from from a comp.lang.python posting |
|
157 | 157 | by Duncan Booth.""" |
|
158 | 158 | def wrapper(*args, **kw): |
|
159 | 159 | return new_fn(*args, **kw) |
|
160 | 160 | if old_fn.__doc__: |
|
161 | 161 | wrapper.__doc__ = old_fn.__doc__ + additional_text |
|
162 | 162 | return wrapper |
|
163 | 163 | |
|
164 | 164 | |
|
165 | 165 | def _file_lines(fname): |
|
166 | 166 | """Return the contents of a named file as a list of lines. |
|
167 | 167 | |
|
168 | 168 | This function never raises an IOError exception: if the file can't be |
|
169 | 169 | read, it simply returns an empty list.""" |
|
170 | 170 | |
|
171 | 171 | try: |
|
172 | 172 | outfile = open(fname) |
|
173 | 173 | except IOError: |
|
174 | 174 | return [] |
|
175 | 175 | else: |
|
176 | 176 | out = outfile.readlines() |
|
177 | 177 | outfile.close() |
|
178 | 178 | return out |
|
179 | 179 | |
|
180 | 180 | |
|
181 | 181 | class Pdb(OldPdb): |
|
182 | 182 | """Modified Pdb class, does not load readline.""" |
|
183 | 183 | |
|
184 | 184 | def __init__(self,color_scheme='NoColor',completekey=None, |
|
185 | 185 | stdin=None, stdout=None): |
|
186 | 186 | |
|
187 | 187 | # Parent constructor: |
|
188 | 188 | if has_pydb and completekey is None: |
|
189 | 189 | OldPdb.__init__(self,stdin=stdin,stdout=io.stdout) |
|
190 | 190 | else: |
|
191 | 191 | OldPdb.__init__(self,completekey,stdin,stdout) |
|
192 | 192 | |
|
193 | 193 | self.prompt = prompt # The default prompt is '(Pdb)' |
|
194 | 194 | |
|
195 | 195 | # IPython changes... |
|
196 | 196 | self.is_pydb = has_pydb |
|
197 | 197 | |
|
198 | 198 | self.shell = ipapi.get() |
|
199 | 199 | |
|
200 | 200 | if self.is_pydb: |
|
201 | 201 | |
|
202 | 202 | # interactiveshell.py's ipalias seems to want pdb's checkline |
|
203 | 203 | # which located in pydb.fn |
|
204 | 204 | import pydb.fns |
|
205 | 205 | self.checkline = lambda filename, lineno: \ |
|
206 | 206 | pydb.fns.checkline(self, filename, lineno) |
|
207 | 207 | |
|
208 | 208 | self.curframe = None |
|
209 | 209 | self.do_restart = self.new_do_restart |
|
210 | 210 | |
|
211 | 211 | self.old_all_completions = self.shell.Completer.all_completions |
|
212 | 212 | self.shell.Completer.all_completions=self.all_completions |
|
213 | 213 | |
|
214 | 214 | self.do_list = decorate_fn_with_doc(self.list_command_pydb, |
|
215 | 215 | OldPdb.do_list) |
|
216 | 216 | self.do_l = self.do_list |
|
217 | 217 | self.do_frame = decorate_fn_with_doc(self.new_do_frame, |
|
218 | 218 | OldPdb.do_frame) |
|
219 | 219 | |
|
220 | 220 | self.aliases = {} |
|
221 | 221 | |
|
222 | 222 | # Create color table: we copy the default one from the traceback |
|
223 | 223 | # module and add a few attributes needed for debugging |
|
224 | 224 | self.color_scheme_table = exception_colors() |
|
225 | 225 | |
|
226 | 226 | # shorthands |
|
227 | 227 | C = coloransi.TermColors |
|
228 | 228 | cst = self.color_scheme_table |
|
229 | 229 | |
|
230 | 230 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor |
|
231 | 231 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor |
|
232 | 232 | |
|
233 | 233 | cst['Linux'].colors.breakpoint_enabled = C.LightRed |
|
234 | 234 | cst['Linux'].colors.breakpoint_disabled = C.Red |
|
235 | 235 | |
|
236 | 236 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed |
|
237 | 237 | cst['LightBG'].colors.breakpoint_disabled = C.Red |
|
238 | 238 | |
|
239 | 239 | self.set_colors(color_scheme) |
|
240 | 240 | |
|
241 | 241 | # Add a python parser so we can syntax highlight source while |
|
242 | 242 | # debugging. |
|
243 | 243 | self.parser = PyColorize.Parser() |
|
244 | 244 | |
|
245 | 245 | def set_colors(self, scheme): |
|
246 | 246 | """Shorthand access to the color table scheme selector method.""" |
|
247 | 247 | self.color_scheme_table.set_active_scheme(scheme) |
|
248 | 248 | |
|
249 | 249 | def interaction(self, frame, traceback): |
|
250 | 250 | self.shell.set_completer_frame(frame) |
|
251 | 251 | OldPdb.interaction(self, frame, traceback) |
|
252 | 252 | |
|
253 | 253 | def new_do_up(self, arg): |
|
254 | 254 | OldPdb.do_up(self, arg) |
|
255 | 255 | self.shell.set_completer_frame(self.curframe) |
|
256 | 256 | do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up) |
|
257 | 257 | |
|
258 | 258 | def new_do_down(self, arg): |
|
259 | 259 | OldPdb.do_down(self, arg) |
|
260 | 260 | self.shell.set_completer_frame(self.curframe) |
|
261 | 261 | |
|
262 | 262 | do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down) |
|
263 | 263 | |
|
264 | 264 | def new_do_frame(self, arg): |
|
265 | 265 | OldPdb.do_frame(self, arg) |
|
266 | 266 | self.shell.set_completer_frame(self.curframe) |
|
267 | 267 | |
|
268 | 268 | def new_do_quit(self, arg): |
|
269 | 269 | |
|
270 | 270 | if hasattr(self, 'old_all_completions'): |
|
271 | 271 | self.shell.Completer.all_completions=self.old_all_completions |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | return OldPdb.do_quit(self, arg) |
|
275 | 275 | |
|
276 | 276 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) |
|
277 | 277 | |
|
278 | 278 | def new_do_restart(self, arg): |
|
279 | 279 | """Restart command. In the context of ipython this is exactly the same |
|
280 | 280 | thing as 'quit'.""" |
|
281 | 281 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") |
|
282 | 282 | return self.do_quit(arg) |
|
283 | 283 | |
|
284 | 284 | def postloop(self): |
|
285 | 285 | self.shell.set_completer_frame(None) |
|
286 | 286 | |
|
287 | 287 | def print_stack_trace(self): |
|
288 | 288 | try: |
|
289 | 289 | for frame_lineno in self.stack: |
|
290 | 290 | self.print_stack_entry(frame_lineno, context = 5) |
|
291 | 291 | except KeyboardInterrupt: |
|
292 | 292 | pass |
|
293 | 293 | |
|
294 | 294 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', |
|
295 | 295 | context = 3): |
|
296 | 296 | #frame, lineno = frame_lineno |
|
297 | 297 | print >>io.stdout, self.format_stack_entry(frame_lineno, '', context) |
|
298 | 298 | |
|
299 | 299 | # vds: >> |
|
300 | 300 | frame, lineno = frame_lineno |
|
301 | 301 | filename = frame.f_code.co_filename |
|
302 | 302 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
303 | 303 | # vds: << |
|
304 | 304 | |
|
305 | 305 | def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): |
|
306 | 306 | import linecache, repr |
|
307 | 307 | |
|
308 | 308 | ret = [] |
|
309 | 309 | |
|
310 | 310 | Colors = self.color_scheme_table.active_colors |
|
311 | 311 | ColorsNormal = Colors.Normal |
|
312 | 312 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
313 | 313 | tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) |
|
314 | 314 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
315 | 315 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, |
|
316 | 316 | ColorsNormal) |
|
317 | 317 | |
|
318 | 318 | frame, lineno = frame_lineno |
|
319 | 319 | |
|
320 | 320 | return_value = '' |
|
321 | 321 | if '__return__' in frame.f_locals: |
|
322 | 322 | rv = frame.f_locals['__return__'] |
|
323 | 323 | #return_value += '->' |
|
324 | 324 | return_value += repr.repr(rv) + '\n' |
|
325 | 325 | ret.append(return_value) |
|
326 | 326 | |
|
327 | 327 | #s = filename + '(' + `lineno` + ')' |
|
328 | 328 | filename = self.canonic(frame.f_code.co_filename) |
|
329 | 329 | link = tpl_link % filename |
|
330 | 330 | |
|
331 | 331 | if frame.f_code.co_name: |
|
332 | 332 | func = frame.f_code.co_name |
|
333 | 333 | else: |
|
334 | 334 | func = "<lambda>" |
|
335 | 335 | |
|
336 | 336 | call = '' |
|
337 | 337 | if func != '?': |
|
338 | 338 | if '__args__' in frame.f_locals: |
|
339 | 339 | args = repr.repr(frame.f_locals['__args__']) |
|
340 | 340 | else: |
|
341 | 341 | args = '()' |
|
342 | 342 | call = tpl_call % (func, args) |
|
343 | 343 | |
|
344 | 344 | # The level info should be generated in the same format pdb uses, to |
|
345 | 345 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. |
|
346 | 346 | if frame is self.curframe: |
|
347 | 347 | ret.append('> ') |
|
348 | 348 | else: |
|
349 | 349 | ret.append(' ') |
|
350 | 350 | ret.append('%s(%s)%s\n' % (link,lineno,call)) |
|
351 | 351 | |
|
352 | 352 | start = lineno - 1 - context//2 |
|
353 | 353 | lines = linecache.getlines(filename) |
|
354 | 354 | start = max(start, 0) |
|
355 | 355 | start = min(start, len(lines) - context) |
|
356 | 356 | lines = lines[start : start + context] |
|
357 | 357 | |
|
358 | 358 | for i,line in enumerate(lines): |
|
359 | 359 | show_arrow = (start + 1 + i == lineno) |
|
360 | 360 | linetpl = (frame is self.curframe or show_arrow) \ |
|
361 | 361 | and tpl_line_em \ |
|
362 | 362 | or tpl_line |
|
363 | 363 | ret.append(self.__format_line(linetpl, filename, |
|
364 | 364 | start + 1 + i, line, |
|
365 | 365 | arrow = show_arrow) ) |
|
366 | 366 | |
|
367 | 367 | return ''.join(ret) |
|
368 | 368 | |
|
369 | 369 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): |
|
370 | 370 | bp_mark = "" |
|
371 | 371 | bp_mark_color = "" |
|
372 | 372 | |
|
373 | 373 | scheme = self.color_scheme_table.active_scheme_name |
|
374 | 374 | new_line, err = self.parser.format2(line, 'str', scheme) |
|
375 | 375 | if not err: line = new_line |
|
376 | 376 | |
|
377 | 377 | bp = None |
|
378 | 378 | if lineno in self.get_file_breaks(filename): |
|
379 | 379 | bps = self.get_breaks(filename, lineno) |
|
380 | 380 | bp = bps[-1] |
|
381 | 381 | |
|
382 | 382 | if bp: |
|
383 | 383 | Colors = self.color_scheme_table.active_colors |
|
384 | 384 | bp_mark = str(bp.number) |
|
385 | 385 | bp_mark_color = Colors.breakpoint_enabled |
|
386 | 386 | if not bp.enabled: |
|
387 | 387 | bp_mark_color = Colors.breakpoint_disabled |
|
388 | 388 | |
|
389 | 389 | numbers_width = 7 |
|
390 | 390 | if arrow: |
|
391 | 391 | # This is the line with the error |
|
392 | 392 | pad = numbers_width - len(str(lineno)) - len(bp_mark) |
|
393 | 393 | if pad >= 3: |
|
394 | 394 | marker = '-'*(pad-3) + '-> ' |
|
395 | 395 | elif pad == 2: |
|
396 | 396 | marker = '> ' |
|
397 | 397 | elif pad == 1: |
|
398 | 398 | marker = '>' |
|
399 | 399 | else: |
|
400 | 400 | marker = '' |
|
401 | 401 | num = '%s%s' % (marker, str(lineno)) |
|
402 | 402 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
403 | 403 | else: |
|
404 | 404 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) |
|
405 | 405 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
406 | 406 | |
|
407 | 407 | return line |
|
408 | 408 | |
|
409 | 409 | def list_command_pydb(self, arg): |
|
410 | 410 | """List command to use if we have a newer pydb installed""" |
|
411 | 411 | filename, first, last = OldPdb.parse_list_cmd(self, arg) |
|
412 | 412 | if filename is not None: |
|
413 | 413 | self.print_list_lines(filename, first, last) |
|
414 | 414 | |
|
415 | 415 | def print_list_lines(self, filename, first, last): |
|
416 | 416 | """The printing (as opposed to the parsing part of a 'list' |
|
417 | 417 | command.""" |
|
418 | 418 | try: |
|
419 | 419 | Colors = self.color_scheme_table.active_colors |
|
420 | 420 | ColorsNormal = Colors.Normal |
|
421 | 421 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
422 | 422 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
423 | 423 | src = [] |
|
424 | 424 | for lineno in range(first, last+1): |
|
425 | 425 | line = linecache.getline(filename, lineno) |
|
426 | 426 | if not line: |
|
427 | 427 | break |
|
428 | 428 | |
|
429 | 429 | if lineno == self.curframe.f_lineno: |
|
430 | 430 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) |
|
431 | 431 | else: |
|
432 | 432 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) |
|
433 | 433 | |
|
434 | 434 | src.append(line) |
|
435 | 435 | self.lineno = lineno |
|
436 | 436 | |
|
437 | 437 | print >>io.stdout, ''.join(src) |
|
438 | 438 | |
|
439 | 439 | except KeyboardInterrupt: |
|
440 | 440 | pass |
|
441 | 441 | |
|
442 | 442 | def do_list(self, arg): |
|
443 | 443 | self.lastcmd = 'list' |
|
444 | 444 | last = None |
|
445 | 445 | if arg: |
|
446 | 446 | try: |
|
447 | 447 | x = eval(arg, {}, {}) |
|
448 | 448 | if type(x) == type(()): |
|
449 | 449 | first, last = x |
|
450 | 450 | first = int(first) |
|
451 | 451 | last = int(last) |
|
452 | 452 | if last < first: |
|
453 | 453 | # Assume it's a count |
|
454 | 454 | last = first + last |
|
455 | 455 | else: |
|
456 | 456 | first = max(1, int(x) - 5) |
|
457 | 457 | except: |
|
458 |
print '*** Error in argument:', |
|
|
458 | print '*** Error in argument:', repr(arg) | |
|
459 | 459 | return |
|
460 | 460 | elif self.lineno is None: |
|
461 | 461 | first = max(1, self.curframe.f_lineno - 5) |
|
462 | 462 | else: |
|
463 | 463 | first = self.lineno + 1 |
|
464 | 464 | if last is None: |
|
465 | 465 | last = first + 10 |
|
466 | 466 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) |
|
467 | 467 | |
|
468 | 468 | # vds: >> |
|
469 | 469 | lineno = first |
|
470 | 470 | filename = self.curframe.f_code.co_filename |
|
471 | 471 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
472 | 472 | # vds: << |
|
473 | 473 | |
|
474 | 474 | do_l = do_list |
|
475 | 475 | |
|
476 | 476 | def do_pdef(self, arg): |
|
477 | 477 | """The debugger interface to magic_pdef""" |
|
478 | 478 | namespaces = [('Locals', self.curframe.f_locals), |
|
479 | 479 | ('Globals', self.curframe.f_globals)] |
|
480 | 480 | self.shell.magic_pdef(arg, namespaces=namespaces) |
|
481 | 481 | |
|
482 | 482 | def do_pdoc(self, arg): |
|
483 | 483 | """The debugger interface to magic_pdoc""" |
|
484 | 484 | namespaces = [('Locals', self.curframe.f_locals), |
|
485 | 485 | ('Globals', self.curframe.f_globals)] |
|
486 | 486 | self.shell.magic_pdoc(arg, namespaces=namespaces) |
|
487 | 487 | |
|
488 | 488 | def do_pinfo(self, arg): |
|
489 | 489 | """The debugger equivalant of ?obj""" |
|
490 | 490 | namespaces = [('Locals', self.curframe.f_locals), |
|
491 | 491 | ('Globals', self.curframe.f_globals)] |
|
492 | 492 | self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces) |
|
493 | 493 | |
|
494 | 494 | def checkline(self, filename, lineno): |
|
495 | 495 | """Check whether specified line seems to be executable. |
|
496 | 496 | |
|
497 | 497 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank |
|
498 | 498 | line or EOF). Warning: testing is not comprehensive. |
|
499 | 499 | """ |
|
500 | 500 | ####################################################################### |
|
501 | 501 | # XXX Hack! Use python-2.5 compatible code for this call, because with |
|
502 | 502 | # all of our changes, we've drifted from the pdb api in 2.6. For now, |
|
503 | 503 | # changing: |
|
504 | 504 | # |
|
505 | 505 | #line = linecache.getline(filename, lineno, self.curframe.f_globals) |
|
506 | 506 | # to: |
|
507 | 507 | # |
|
508 | 508 | line = linecache.getline(filename, lineno) |
|
509 | 509 | # |
|
510 | 510 | # does the trick. But in reality, we need to fix this by reconciling |
|
511 | 511 | # our updates with the new Pdb APIs in Python 2.6. |
|
512 | 512 | # |
|
513 | 513 | # End hack. The rest of this method is copied verbatim from 2.6 pdb.py |
|
514 | 514 | ####################################################################### |
|
515 | 515 | |
|
516 | 516 | if not line: |
|
517 | 517 | print >>self.stdout, 'End of file' |
|
518 | 518 | return 0 |
|
519 | 519 | line = line.strip() |
|
520 | 520 | # Don't allow setting breakpoint at a blank line |
|
521 | 521 | if (not line or (line[0] == '#') or |
|
522 | 522 | (line[:3] == '"""') or line[:3] == "'''"): |
|
523 | 523 | print >>self.stdout, '*** Blank or comment' |
|
524 | 524 | return 0 |
|
525 | 525 | return lineno |
@@ -1,269 +1,269 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Displayhook for IPython. |
|
3 | 3 | |
|
4 | 4 | This defines a callable class that IPython uses for `sys.displayhook`. |
|
5 | 5 | |
|
6 | 6 | Authors: |
|
7 | 7 | |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | * Brian Granger |
|
10 | 10 | * Robert Kern |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
15 | 15 | # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu> |
|
16 | 16 | # |
|
17 | 17 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | 18 | # the file COPYING, distributed as part of this software. |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Imports |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | import __builtin__ |
|
26 | 26 | |
|
27 | 27 | from IPython.config.configurable import Configurable |
|
28 | 28 | from IPython.utils import io |
|
29 | 29 | from IPython.utils.traitlets import Instance, List |
|
30 | 30 | from IPython.utils.warn import warn |
|
31 | 31 | |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | # Main displayhook class |
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | 35 | |
|
36 | 36 | # TODO: Move the various attributes (cache_size, [others now moved]). Some |
|
37 | 37 | # of these are also attributes of InteractiveShell. They should be on ONE object |
|
38 | 38 | # only and the other objects should ask that one object for their values. |
|
39 | 39 | |
|
40 | 40 | class DisplayHook(Configurable): |
|
41 | 41 | """The custom IPython displayhook to replace sys.displayhook. |
|
42 | 42 | |
|
43 | 43 | This class does many things, but the basic idea is that it is a callable |
|
44 | 44 | that gets called anytime user code returns a value. |
|
45 | 45 | """ |
|
46 | 46 | |
|
47 | 47 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
48 | 48 | |
|
49 | 49 | def __init__(self, shell=None, cache_size=1000, config=None): |
|
50 | 50 | super(DisplayHook, self).__init__(shell=shell, config=config) |
|
51 | 51 | |
|
52 | 52 | cache_size_min = 3 |
|
53 | 53 | if cache_size <= 0: |
|
54 | 54 | self.do_full_cache = 0 |
|
55 | 55 | cache_size = 0 |
|
56 | 56 | elif cache_size < cache_size_min: |
|
57 | 57 | self.do_full_cache = 0 |
|
58 | 58 | cache_size = 0 |
|
59 | 59 | warn('caching was disabled (min value for cache size is %s).' % |
|
60 | 60 | cache_size_min,level=3) |
|
61 | 61 | else: |
|
62 | 62 | self.do_full_cache = 1 |
|
63 | 63 | |
|
64 | 64 | self.cache_size = cache_size |
|
65 | 65 | |
|
66 | 66 | # we need a reference to the user-level namespace |
|
67 | 67 | self.shell = shell |
|
68 | 68 | |
|
69 | 69 | self._,self.__,self.___ = '','','' |
|
70 | 70 | |
|
71 | 71 | # these are deliberately global: |
|
72 | 72 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
73 | 73 | self.shell.user_ns.update(to_user_ns) |
|
74 | 74 | |
|
75 | 75 | @property |
|
76 | 76 | def prompt_count(self): |
|
77 | 77 | return self.shell.execution_count |
|
78 | 78 | |
|
79 | 79 | #------------------------------------------------------------------------- |
|
80 | 80 | # Methods used in __call__. Override these methods to modify the behavior |
|
81 | 81 | # of the displayhook. |
|
82 | 82 | #------------------------------------------------------------------------- |
|
83 | 83 | |
|
84 | 84 | def check_for_underscore(self): |
|
85 | 85 | """Check if the user has set the '_' variable by hand.""" |
|
86 | 86 | # If something injected a '_' variable in __builtin__, delete |
|
87 | 87 | # ipython's automatic one so we don't clobber that. gettext() in |
|
88 | 88 | # particular uses _, so we need to stay away from it. |
|
89 | 89 | if '_' in __builtin__.__dict__: |
|
90 | 90 | try: |
|
91 | 91 | del self.shell.user_ns['_'] |
|
92 | 92 | except KeyError: |
|
93 | 93 | pass |
|
94 | 94 | |
|
95 | 95 | def quiet(self): |
|
96 | 96 | """Should we silence the display hook because of ';'?""" |
|
97 | 97 | # do not print output if input ends in ';' |
|
98 | 98 | try: |
|
99 | 99 | cell = self.shell.history_manager.input_hist_parsed[self.prompt_count] |
|
100 | 100 | if cell.rstrip().endswith(';'): |
|
101 | 101 | return True |
|
102 | 102 | except IndexError: |
|
103 | 103 | # some uses of ipshellembed may fail here |
|
104 | 104 | pass |
|
105 | 105 | return False |
|
106 | 106 | |
|
107 | 107 | def start_displayhook(self): |
|
108 | 108 | """Start the displayhook, initializing resources.""" |
|
109 | 109 | pass |
|
110 | 110 | |
|
111 | 111 | def write_output_prompt(self): |
|
112 | 112 | """Write the output prompt. |
|
113 | 113 | |
|
114 | 114 | The default implementation simply writes the prompt to |
|
115 | 115 | ``io.stdout``. |
|
116 | 116 | """ |
|
117 | 117 | # Use write, not print which adds an extra space. |
|
118 | 118 | io.stdout.write(self.shell.separate_out) |
|
119 | 119 | outprompt = self.shell.prompt_manager.render('out') |
|
120 | 120 | if self.do_full_cache: |
|
121 | 121 | io.stdout.write(outprompt) |
|
122 | 122 | |
|
123 | 123 | def compute_format_data(self, result): |
|
124 | 124 | """Compute format data of the object to be displayed. |
|
125 | 125 | |
|
126 | 126 | The format data is a generalization of the :func:`repr` of an object. |
|
127 | 127 | In the default implementation the format data is a :class:`dict` of |
|
128 | 128 | key value pair where the keys are valid MIME types and the values |
|
129 | 129 | are JSON'able data structure containing the raw data for that MIME |
|
130 | 130 | type. It is up to frontends to determine pick a MIME to to use and |
|
131 | 131 | display that data in an appropriate manner. |
|
132 | 132 | |
|
133 | 133 | This method only computes the format data for the object and should |
|
134 | 134 | NOT actually print or write that to a stream. |
|
135 | 135 | |
|
136 | 136 | Parameters |
|
137 | 137 | ---------- |
|
138 | 138 | result : object |
|
139 | 139 | The Python object passed to the display hook, whose format will be |
|
140 | 140 | computed. |
|
141 | 141 | |
|
142 | 142 | Returns |
|
143 | 143 | ------- |
|
144 | 144 | format_data : dict |
|
145 | 145 | A :class:`dict` whose keys are valid MIME types and values are |
|
146 | 146 | JSON'able raw data for that MIME type. It is recommended that |
|
147 | 147 | all return values of this should always include the "text/plain" |
|
148 | 148 | MIME type representation of the object. |
|
149 | 149 | """ |
|
150 | 150 | return self.shell.display_formatter.format(result) |
|
151 | 151 | |
|
152 | 152 | def write_format_data(self, format_dict): |
|
153 | 153 | """Write the format data dict to the frontend. |
|
154 | 154 | |
|
155 | 155 | This default version of this method simply writes the plain text |
|
156 | 156 | representation of the object to ``io.stdout``. Subclasses should |
|
157 | 157 | override this method to send the entire `format_dict` to the |
|
158 | 158 | frontends. |
|
159 | 159 | |
|
160 | 160 | Parameters |
|
161 | 161 | ---------- |
|
162 | 162 | format_dict : dict |
|
163 | 163 | The format dict for the object passed to `sys.displayhook`. |
|
164 | 164 | """ |
|
165 | 165 | # We want to print because we want to always make sure we have a |
|
166 | 166 | # newline, even if all the prompt separators are ''. This is the |
|
167 | 167 | # standard IPython behavior. |
|
168 | 168 | result_repr = format_dict['text/plain'] |
|
169 | 169 | if '\n' in result_repr: |
|
170 | 170 | # So that multi-line strings line up with the left column of |
|
171 | 171 | # the screen, instead of having the output prompt mess up |
|
172 | 172 | # their first line. |
|
173 | 173 | # We use the prompt template instead of the expanded prompt |
|
174 | 174 | # because the expansion may add ANSI escapes that will interfere |
|
175 | 175 | # with our ability to determine whether or not we should add |
|
176 | 176 | # a newline. |
|
177 | 177 | prompt_template = self.shell.prompt_manager.out_template |
|
178 | 178 | if prompt_template and not prompt_template.endswith('\n'): |
|
179 | 179 | # But avoid extraneous empty lines. |
|
180 | 180 | result_repr = '\n' + result_repr |
|
181 | 181 | |
|
182 | 182 | print >>io.stdout, result_repr |
|
183 | 183 | |
|
184 | 184 | def update_user_ns(self, result): |
|
185 | 185 | """Update user_ns with various things like _, __, _1, etc.""" |
|
186 | 186 | |
|
187 | 187 | # Avoid recursive reference when displaying _oh/Out |
|
188 | 188 | if result is not self.shell.user_ns['_oh']: |
|
189 | 189 | if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache: |
|
190 | 190 | warn('Output cache limit (currently '+ |
|
191 |
|
|
|
191 | repr(self.cache_size)+' entries) hit.\n' | |
|
192 | 192 | 'Flushing cache and resetting history counter...\n' |
|
193 | 193 | 'The only history variables available will be _,__,___ and _1\n' |
|
194 | 194 | 'with the current result.') |
|
195 | 195 | |
|
196 | 196 | self.flush() |
|
197 | 197 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
198 | 198 | # we cause buggy behavior for things like gettext). |
|
199 | 199 | |
|
200 | 200 | if '_' not in __builtin__.__dict__: |
|
201 | 201 | self.___ = self.__ |
|
202 | 202 | self.__ = self._ |
|
203 | 203 | self._ = result |
|
204 | 204 | self.shell.push({'_':self._, |
|
205 | 205 | '__':self.__, |
|
206 | 206 | '___':self.___}, interactive=False) |
|
207 | 207 | |
|
208 | 208 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
209 | 209 | to_main = {} |
|
210 | 210 | if self.do_full_cache: |
|
211 |
new_result = '_'+ |
|
|
211 | new_result = '_'+repr(self.prompt_count) | |
|
212 | 212 | to_main[new_result] = result |
|
213 | 213 | self.shell.push(to_main, interactive=False) |
|
214 | 214 | self.shell.user_ns['_oh'][self.prompt_count] = result |
|
215 | 215 | |
|
216 | 216 | def log_output(self, format_dict): |
|
217 | 217 | """Log the output.""" |
|
218 | 218 | if self.shell.logger.log_output: |
|
219 | 219 | self.shell.logger.log_write(format_dict['text/plain'], 'output') |
|
220 | 220 | self.shell.history_manager.output_hist_reprs[self.prompt_count] = \ |
|
221 | 221 | format_dict['text/plain'] |
|
222 | 222 | |
|
223 | 223 | def finish_displayhook(self): |
|
224 | 224 | """Finish up all displayhook activities.""" |
|
225 | 225 | io.stdout.write(self.shell.separate_out2) |
|
226 | 226 | io.stdout.flush() |
|
227 | 227 | |
|
228 | 228 | def __call__(self, result=None): |
|
229 | 229 | """Printing with history cache management. |
|
230 | 230 | |
|
231 | 231 | This is invoked everytime the interpreter needs to print, and is |
|
232 | 232 | activated by setting the variable sys.displayhook to it. |
|
233 | 233 | """ |
|
234 | 234 | self.check_for_underscore() |
|
235 | 235 | if result is not None and not self.quiet(): |
|
236 | 236 | self.start_displayhook() |
|
237 | 237 | self.write_output_prompt() |
|
238 | 238 | format_dict = self.compute_format_data(result) |
|
239 | 239 | self.write_format_data(format_dict) |
|
240 | 240 | self.update_user_ns(result) |
|
241 | 241 | self.log_output(format_dict) |
|
242 | 242 | self.finish_displayhook() |
|
243 | 243 | |
|
244 | 244 | def flush(self): |
|
245 | 245 | if not self.do_full_cache: |
|
246 | 246 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
247 | 247 | "if full caching is not enabled!" |
|
248 | 248 | # delete auto-generated vars from global namespace |
|
249 | 249 | |
|
250 | 250 | for n in range(1,self.prompt_count + 1): |
|
251 |
key = '_'+ |
|
|
251 | key = '_'+repr(n) | |
|
252 | 252 | try: |
|
253 | 253 | del self.shell.user_ns[key] |
|
254 | 254 | except: pass |
|
255 | 255 | # In some embedded circumstances, the user_ns doesn't have the |
|
256 | 256 | # '_oh' key set up. |
|
257 | 257 | oh = self.shell.user_ns.get('_oh', None) |
|
258 | 258 | if oh is not None: |
|
259 | 259 | oh.clear() |
|
260 | 260 | |
|
261 | 261 | # Release our own references to objects: |
|
262 | 262 | self._, self.__, self.___ = '', '', '' |
|
263 | 263 | |
|
264 | 264 | if '_' not in __builtin__.__dict__: |
|
265 | 265 | self.shell.user_ns.update({'_':None,'__':None, '___':None}) |
|
266 | 266 | import gc |
|
267 | 267 | # TODO: Is this really needed? |
|
268 | 268 | gc.collect() |
|
269 | 269 |
@@ -1,220 +1,220 b'' | |||
|
1 | 1 | """Logger class for IPython's logging facilities. |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | #***************************************************************************** |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
6 | 6 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | |
|
12 | 12 | #**************************************************************************** |
|
13 | 13 | # Modules and globals |
|
14 | 14 | |
|
15 | 15 | # Python standard modules |
|
16 | 16 | import glob |
|
17 | 17 | import io |
|
18 | 18 | import os |
|
19 | 19 | import time |
|
20 | 20 | |
|
21 | 21 | from IPython.utils.py3compat import str_to_unicode |
|
22 | 22 | |
|
23 | 23 | #**************************************************************************** |
|
24 | 24 | # FIXME: This class isn't a mixin anymore, but it still needs attributes from |
|
25 | 25 | # ipython and does input cache management. Finish cleanup later... |
|
26 | 26 | |
|
27 | 27 | class Logger(object): |
|
28 | 28 | """A Logfile class with different policies for file creation""" |
|
29 | 29 | |
|
30 | 30 | def __init__(self, home_dir, logfname='Logger.log', loghead=u'', |
|
31 | 31 | logmode='over'): |
|
32 | 32 | |
|
33 | 33 | # this is the full ipython instance, we need some attributes from it |
|
34 | 34 | # which won't exist until later. What a mess, clean up later... |
|
35 | 35 | self.home_dir = home_dir |
|
36 | 36 | |
|
37 | 37 | self.logfname = logfname |
|
38 | 38 | self.loghead = loghead |
|
39 | 39 | self.logmode = logmode |
|
40 | 40 | self.logfile = None |
|
41 | 41 | |
|
42 | 42 | # Whether to log raw or processed input |
|
43 | 43 | self.log_raw_input = False |
|
44 | 44 | |
|
45 | 45 | # whether to also log output |
|
46 | 46 | self.log_output = False |
|
47 | 47 | |
|
48 | 48 | # whether to put timestamps before each log entry |
|
49 | 49 | self.timestamp = False |
|
50 | 50 | |
|
51 | 51 | # activity control flags |
|
52 | 52 | self.log_active = False |
|
53 | 53 | |
|
54 | 54 | # logmode is a validated property |
|
55 | 55 | def _set_mode(self,mode): |
|
56 | 56 | if mode not in ['append','backup','global','over','rotate']: |
|
57 | 57 | raise ValueError,'invalid log mode %s given' % mode |
|
58 | 58 | self._logmode = mode |
|
59 | 59 | |
|
60 | 60 | def _get_mode(self): |
|
61 | 61 | return self._logmode |
|
62 | 62 | |
|
63 | 63 | logmode = property(_get_mode,_set_mode) |
|
64 | 64 | |
|
65 | 65 | def logstart(self, logfname=None, loghead=None, logmode=None, |
|
66 | 66 | log_output=False, timestamp=False, log_raw_input=False): |
|
67 | 67 | """Generate a new log-file with a default header. |
|
68 | 68 | |
|
69 | 69 | Raises RuntimeError if the log has already been started""" |
|
70 | 70 | |
|
71 | 71 | if self.logfile is not None: |
|
72 | 72 | raise RuntimeError('Log file is already active: %s' % |
|
73 | 73 | self.logfname) |
|
74 | 74 | |
|
75 | 75 | # The parameters can override constructor defaults |
|
76 | 76 | if logfname is not None: self.logfname = logfname |
|
77 | 77 | if loghead is not None: self.loghead = loghead |
|
78 | 78 | if logmode is not None: self.logmode = logmode |
|
79 | 79 | |
|
80 | 80 | # Parameters not part of the constructor |
|
81 | 81 | self.timestamp = timestamp |
|
82 | 82 | self.log_output = log_output |
|
83 | 83 | self.log_raw_input = log_raw_input |
|
84 | 84 | |
|
85 | 85 | # init depending on the log mode requested |
|
86 | 86 | isfile = os.path.isfile |
|
87 | 87 | logmode = self.logmode |
|
88 | 88 | |
|
89 | 89 | if logmode == 'append': |
|
90 | 90 | self.logfile = io.open(self.logfname, 'a', encoding='utf-8') |
|
91 | 91 | |
|
92 | 92 | elif logmode == 'backup': |
|
93 | 93 | if isfile(self.logfname): |
|
94 | 94 | backup_logname = self.logfname+'~' |
|
95 | 95 | # Manually remove any old backup, since os.rename may fail |
|
96 | 96 | # under Windows. |
|
97 | 97 | if isfile(backup_logname): |
|
98 | 98 | os.remove(backup_logname) |
|
99 | 99 | os.rename(self.logfname,backup_logname) |
|
100 | 100 | self.logfile = io.open(self.logfname, 'w', encoding='utf-8') |
|
101 | 101 | |
|
102 | 102 | elif logmode == 'global': |
|
103 | 103 | self.logfname = os.path.join(self.home_dir,self.logfname) |
|
104 | 104 | self.logfile = io.open(self.logfname, 'a', encoding='utf-8') |
|
105 | 105 | |
|
106 | 106 | elif logmode == 'over': |
|
107 | 107 | if isfile(self.logfname): |
|
108 | 108 | os.remove(self.logfname) |
|
109 | 109 | self.logfile = io.open(self.logfname,'w', encoding='utf-8') |
|
110 | 110 | |
|
111 | 111 | elif logmode == 'rotate': |
|
112 | 112 | if isfile(self.logfname): |
|
113 | 113 | if isfile(self.logfname+'.001~'): |
|
114 | 114 | old = glob.glob(self.logfname+'.*~') |
|
115 | 115 | old.sort() |
|
116 | 116 | old.reverse() |
|
117 | 117 | for f in old: |
|
118 | 118 | root, ext = os.path.splitext(f) |
|
119 | 119 | num = int(ext[1:-1])+1 |
|
120 |
os.rename(f, root+'.'+ |
|
|
120 | os.rename(f, root+'.'+repr(num).zfill(3)+'~') | |
|
121 | 121 | os.rename(self.logfname, self.logfname+'.001~') |
|
122 | 122 | self.logfile = io.open(self.logfname, 'w', encoding='utf-8') |
|
123 | 123 | |
|
124 | 124 | if logmode != 'append': |
|
125 | 125 | self.logfile.write(self.loghead) |
|
126 | 126 | |
|
127 | 127 | self.logfile.flush() |
|
128 | 128 | self.log_active = True |
|
129 | 129 | |
|
130 | 130 | def switch_log(self,val): |
|
131 | 131 | """Switch logging on/off. val should be ONLY a boolean.""" |
|
132 | 132 | |
|
133 | 133 | if val not in [False,True,0,1]: |
|
134 | 134 | raise ValueError, \ |
|
135 | 135 | 'Call switch_log ONLY with a boolean argument, not with:',val |
|
136 | 136 | |
|
137 | 137 | label = {0:'OFF',1:'ON',False:'OFF',True:'ON'} |
|
138 | 138 | |
|
139 | 139 | if self.logfile is None: |
|
140 | 140 | print """ |
|
141 | 141 | Logging hasn't been started yet (use logstart for that). |
|
142 | 142 | |
|
143 | 143 | %logon/%logoff are for temporarily starting and stopping logging for a logfile |
|
144 | 144 | which already exists. But you must first start the logging process with |
|
145 | 145 | %logstart (optionally giving a logfile name).""" |
|
146 | 146 | |
|
147 | 147 | else: |
|
148 | 148 | if self.log_active == val: |
|
149 | 149 | print 'Logging is already',label[val] |
|
150 | 150 | else: |
|
151 | 151 | print 'Switching logging',label[val] |
|
152 | 152 | self.log_active = not self.log_active |
|
153 | 153 | self.log_active_out = self.log_active |
|
154 | 154 | |
|
155 | 155 | def logstate(self): |
|
156 | 156 | """Print a status message about the logger.""" |
|
157 | 157 | if self.logfile is None: |
|
158 | 158 | print 'Logging has not been activated.' |
|
159 | 159 | else: |
|
160 | 160 | state = self.log_active and 'active' or 'temporarily suspended' |
|
161 | 161 | print 'Filename :',self.logfname |
|
162 | 162 | print 'Mode :',self.logmode |
|
163 | 163 | print 'Output logging :',self.log_output |
|
164 | 164 | print 'Raw input log :',self.log_raw_input |
|
165 | 165 | print 'Timestamping :',self.timestamp |
|
166 | 166 | print 'State :',state |
|
167 | 167 | |
|
168 | 168 | def log(self, line_mod, line_ori): |
|
169 | 169 | """Write the sources to a log. |
|
170 | 170 | |
|
171 | 171 | Inputs: |
|
172 | 172 | |
|
173 | 173 | - line_mod: possibly modified input, such as the transformations made |
|
174 | 174 | by input prefilters or input handlers of various kinds. This should |
|
175 | 175 | always be valid Python. |
|
176 | 176 | |
|
177 | 177 | - line_ori: unmodified input line from the user. This is not |
|
178 | 178 | necessarily valid Python. |
|
179 | 179 | """ |
|
180 | 180 | |
|
181 | 181 | # Write the log line, but decide which one according to the |
|
182 | 182 | # log_raw_input flag, set when the log is started. |
|
183 | 183 | if self.log_raw_input: |
|
184 | 184 | self.log_write(line_ori) |
|
185 | 185 | else: |
|
186 | 186 | self.log_write(line_mod) |
|
187 | 187 | |
|
188 | 188 | def log_write(self, data, kind='input'): |
|
189 | 189 | """Write data to the log file, if active""" |
|
190 | 190 | |
|
191 | 191 | #print 'data: %r' % data # dbg |
|
192 | 192 | if self.log_active and data: |
|
193 | 193 | write = self.logfile.write |
|
194 | 194 | if kind=='input': |
|
195 | 195 | if self.timestamp: |
|
196 | 196 | write(str_to_unicode(time.strftime('# %a, %d %b %Y %H:%M:%S\n', |
|
197 | 197 | time.localtime()))) |
|
198 | 198 | write(data) |
|
199 | 199 | elif kind=='output' and self.log_output: |
|
200 | 200 | odata = u'\n'.join([u'#[Out]# %s' % s |
|
201 | 201 | for s in data.splitlines()]) |
|
202 | 202 | write(u'%s\n' % odata) |
|
203 | 203 | self.logfile.flush() |
|
204 | 204 | |
|
205 | 205 | def logstop(self): |
|
206 | 206 | """Fully stop logging and close log file. |
|
207 | 207 | |
|
208 | 208 | In order to start logging again, a new logstart() call needs to be |
|
209 | 209 | made, possibly (though not necessarily) with a new filename, mode and |
|
210 | 210 | other options.""" |
|
211 | 211 | |
|
212 | 212 | if self.logfile is not None: |
|
213 | 213 | self.logfile.close() |
|
214 | 214 | self.logfile = None |
|
215 | 215 | else: |
|
216 | 216 | print "Logging hadn't been started." |
|
217 | 217 | self.log_active = False |
|
218 | 218 | |
|
219 | 219 | # For backwards compatibility, in case anyone was using this. |
|
220 | 220 | close_log = logstop |
@@ -1,1022 +1,1022 b'' | |||
|
1 | 1 | """Implementation of execution-related magic functions. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # Stdlib |
|
16 | 16 | import __builtin__ as builtin_mod |
|
17 | 17 | import bdb |
|
18 | 18 | import os |
|
19 | 19 | import sys |
|
20 | 20 | import time |
|
21 | 21 | from StringIO import StringIO |
|
22 | 22 | |
|
23 | 23 | # cProfile was added in Python2.5 |
|
24 | 24 | try: |
|
25 | 25 | import cProfile as profile |
|
26 | 26 | import pstats |
|
27 | 27 | except ImportError: |
|
28 | 28 | # profile isn't bundled by default in Debian for license reasons |
|
29 | 29 | try: |
|
30 | 30 | import profile, pstats |
|
31 | 31 | except ImportError: |
|
32 | 32 | profile = pstats = None |
|
33 | 33 | |
|
34 | 34 | # Our own packages |
|
35 | 35 | from IPython.core import debugger, oinspect |
|
36 | 36 | from IPython.core import magic_arguments |
|
37 | 37 | from IPython.core import page |
|
38 | 38 | from IPython.core.error import UsageError |
|
39 | 39 | from IPython.core.macro import Macro |
|
40 | 40 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, |
|
41 | 41 | line_cell_magic, on_off, needs_local_scope) |
|
42 | 42 | from IPython.testing.skipdoctest import skip_doctest |
|
43 | 43 | from IPython.utils import py3compat |
|
44 | 44 | from IPython.utils.io import capture_output |
|
45 | 45 | from IPython.utils.ipstruct import Struct |
|
46 | 46 | from IPython.utils.module_paths import find_mod |
|
47 | 47 | from IPython.utils.path import get_py_filename, unquote_filename |
|
48 | 48 | from IPython.utils.timing import clock, clock2 |
|
49 | 49 | from IPython.utils.warn import warn, error |
|
50 | 50 | |
|
51 | 51 | #----------------------------------------------------------------------------- |
|
52 | 52 | # Magic implementation classes |
|
53 | 53 | #----------------------------------------------------------------------------- |
|
54 | 54 | |
|
55 | 55 | @magics_class |
|
56 | 56 | class ExecutionMagics(Magics): |
|
57 | 57 | """Magics related to code execution, debugging, profiling, etc. |
|
58 | 58 | |
|
59 | 59 | """ |
|
60 | 60 | |
|
61 | 61 | def __init__(self, shell): |
|
62 | 62 | super(ExecutionMagics, self).__init__(shell) |
|
63 | 63 | if profile is None: |
|
64 | 64 | self.prun = self.profile_missing_notice |
|
65 | 65 | # Default execution function used to actually run user code. |
|
66 | 66 | self.default_runner = None |
|
67 | 67 | |
|
68 | 68 | def profile_missing_notice(self, *args, **kwargs): |
|
69 | 69 | error("""\ |
|
70 | 70 | The profile module could not be found. It has been removed from the standard |
|
71 | 71 | python packages because of its non-free license. To use profiling, install the |
|
72 | 72 | python-profiler package from non-free.""") |
|
73 | 73 | |
|
74 | 74 | @skip_doctest |
|
75 | 75 | @line_cell_magic |
|
76 | 76 | def prun(self, parameter_s='', cell=None, user_mode=True, |
|
77 | 77 | opts=None,arg_lst=None,prog_ns=None): |
|
78 | 78 | |
|
79 | 79 | """Run a statement through the python code profiler. |
|
80 | 80 | |
|
81 | 81 | Usage, in line mode: |
|
82 | 82 | %prun [options] statement |
|
83 | 83 | |
|
84 | 84 | Usage, in cell mode: |
|
85 | 85 | %%prun [options] [statement] |
|
86 | 86 | code... |
|
87 | 87 | code... |
|
88 | 88 | |
|
89 | 89 | In cell mode, the additional code lines are appended to the (possibly |
|
90 | 90 | empty) statement in the first line. Cell mode allows you to easily |
|
91 | 91 | profile multiline blocks without having to put them in a separate |
|
92 | 92 | function. |
|
93 | 93 | |
|
94 | 94 | The given statement (which doesn't require quote marks) is run via the |
|
95 | 95 | python profiler in a manner similar to the profile.run() function. |
|
96 | 96 | Namespaces are internally managed to work correctly; profile.run |
|
97 | 97 | cannot be used in IPython because it makes certain assumptions about |
|
98 | 98 | namespaces which do not hold under IPython. |
|
99 | 99 | |
|
100 | 100 | Options: |
|
101 | 101 | |
|
102 | 102 | -l <limit>: you can place restrictions on what or how much of the |
|
103 | 103 | profile gets printed. The limit value can be: |
|
104 | 104 | |
|
105 | 105 | * A string: only information for function names containing this string |
|
106 | 106 | is printed. |
|
107 | 107 | |
|
108 | 108 | * An integer: only these many lines are printed. |
|
109 | 109 | |
|
110 | 110 | * A float (between 0 and 1): this fraction of the report is printed |
|
111 | 111 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
112 | 112 | |
|
113 | 113 | You can combine several limits with repeated use of the option. For |
|
114 | 114 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
115 | 115 | information about class constructors. |
|
116 | 116 | |
|
117 | 117 | -r: return the pstats.Stats object generated by the profiling. This |
|
118 | 118 | object has all the information about the profile in it, and you can |
|
119 | 119 | later use it for further analysis or in other functions. |
|
120 | 120 | |
|
121 | 121 | -s <key>: sort profile by given key. You can provide more than one key |
|
122 | 122 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
123 | 123 | default sorting key is 'time'. |
|
124 | 124 | |
|
125 | 125 | The following is copied verbatim from the profile documentation |
|
126 | 126 | referenced below: |
|
127 | 127 | |
|
128 | 128 | When more than one key is provided, additional keys are used as |
|
129 | 129 | secondary criteria when the there is equality in all keys selected |
|
130 | 130 | before them. |
|
131 | 131 | |
|
132 | 132 | Abbreviations can be used for any key names, as long as the |
|
133 | 133 | abbreviation is unambiguous. The following are the keys currently |
|
134 | 134 | defined: |
|
135 | 135 | |
|
136 | 136 | Valid Arg Meaning |
|
137 | 137 | "calls" call count |
|
138 | 138 | "cumulative" cumulative time |
|
139 | 139 | "file" file name |
|
140 | 140 | "module" file name |
|
141 | 141 | "pcalls" primitive call count |
|
142 | 142 | "line" line number |
|
143 | 143 | "name" function name |
|
144 | 144 | "nfl" name/file/line |
|
145 | 145 | "stdname" standard name |
|
146 | 146 | "time" internal time |
|
147 | 147 | |
|
148 | 148 | Note that all sorts on statistics are in descending order (placing |
|
149 | 149 | most time consuming items first), where as name, file, and line number |
|
150 | 150 | searches are in ascending order (i.e., alphabetical). The subtle |
|
151 | 151 | distinction between "nfl" and "stdname" is that the standard name is a |
|
152 | 152 | sort of the name as printed, which means that the embedded line |
|
153 | 153 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
154 | 154 | would (if the file names were the same) appear in the string order |
|
155 | 155 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
156 | 156 | line numbers. In fact, sort_stats("nfl") is the same as |
|
157 | 157 | sort_stats("name", "file", "line"). |
|
158 | 158 | |
|
159 | 159 | -T <filename>: save profile results as shown on screen to a text |
|
160 | 160 | file. The profile is still shown on screen. |
|
161 | 161 | |
|
162 | 162 | -D <filename>: save (via dump_stats) profile statistics to given |
|
163 | 163 | filename. This data is in a format understood by the pstats module, and |
|
164 | 164 | is generated by a call to the dump_stats() method of profile |
|
165 | 165 | objects. The profile is still shown on screen. |
|
166 | 166 | |
|
167 | 167 | -q: suppress output to the pager. Best used with -T and/or -D above. |
|
168 | 168 | |
|
169 | 169 | If you want to run complete programs under the profiler's control, use |
|
170 | 170 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
171 | 171 | contains profiler specific options as described here. |
|
172 | 172 | |
|
173 | 173 | You can read the complete documentation for the profile module with:: |
|
174 | 174 | |
|
175 | 175 | In [1]: import profile; profile.help() |
|
176 | 176 | """ |
|
177 | 177 | |
|
178 | 178 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
179 | 179 | |
|
180 | 180 | if user_mode: # regular user call |
|
181 | 181 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q', |
|
182 | 182 | list_all=True, posix=False) |
|
183 | 183 | namespace = self.shell.user_ns |
|
184 | 184 | if cell is not None: |
|
185 | 185 | arg_str += '\n' + cell |
|
186 | 186 | else: # called to run a program by %run -p |
|
187 | 187 | try: |
|
188 | 188 | filename = get_py_filename(arg_lst[0]) |
|
189 | 189 | except IOError as e: |
|
190 | 190 | try: |
|
191 | 191 | msg = str(e) |
|
192 | 192 | except UnicodeError: |
|
193 | 193 | msg = e.message |
|
194 | 194 | error(msg) |
|
195 | 195 | return |
|
196 | 196 | |
|
197 | 197 | arg_str = 'execfile(filename,prog_ns)' |
|
198 | 198 | namespace = { |
|
199 | 199 | 'execfile': self.shell.safe_execfile, |
|
200 | 200 | 'prog_ns': prog_ns, |
|
201 | 201 | 'filename': filename |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | opts.merge(opts_def) |
|
205 | 205 | |
|
206 | 206 | prof = profile.Profile() |
|
207 | 207 | try: |
|
208 | 208 | prof = prof.runctx(arg_str,namespace,namespace) |
|
209 | 209 | sys_exit = '' |
|
210 | 210 | except SystemExit: |
|
211 | 211 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
212 | 212 | |
|
213 | 213 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
214 | 214 | |
|
215 | 215 | lims = opts.l |
|
216 | 216 | if lims: |
|
217 | 217 | lims = [] # rebuild lims with ints/floats/strings |
|
218 | 218 | for lim in opts.l: |
|
219 | 219 | try: |
|
220 | 220 | lims.append(int(lim)) |
|
221 | 221 | except ValueError: |
|
222 | 222 | try: |
|
223 | 223 | lims.append(float(lim)) |
|
224 | 224 | except ValueError: |
|
225 | 225 | lims.append(lim) |
|
226 | 226 | |
|
227 | 227 | # Trap output. |
|
228 | 228 | stdout_trap = StringIO() |
|
229 | 229 | |
|
230 | 230 | if hasattr(stats,'stream'): |
|
231 | 231 | # In newer versions of python, the stats object has a 'stream' |
|
232 | 232 | # attribute to write into. |
|
233 | 233 | stats.stream = stdout_trap |
|
234 | 234 | stats.print_stats(*lims) |
|
235 | 235 | else: |
|
236 | 236 | # For older versions, we manually redirect stdout during printing |
|
237 | 237 | sys_stdout = sys.stdout |
|
238 | 238 | try: |
|
239 | 239 | sys.stdout = stdout_trap |
|
240 | 240 | stats.print_stats(*lims) |
|
241 | 241 | finally: |
|
242 | 242 | sys.stdout = sys_stdout |
|
243 | 243 | |
|
244 | 244 | output = stdout_trap.getvalue() |
|
245 | 245 | output = output.rstrip() |
|
246 | 246 | |
|
247 | 247 | if 'q' not in opts: |
|
248 | 248 | page.page(output) |
|
249 | 249 | print sys_exit, |
|
250 | 250 | |
|
251 | 251 | dump_file = opts.D[0] |
|
252 | 252 | text_file = opts.T[0] |
|
253 | 253 | if dump_file: |
|
254 | 254 | dump_file = unquote_filename(dump_file) |
|
255 | 255 | prof.dump_stats(dump_file) |
|
256 | 256 | print '\n*** Profile stats marshalled to file',\ |
|
257 |
|
|
|
257 | repr(dump_file)+'.',sys_exit | |
|
258 | 258 | if text_file: |
|
259 | 259 | text_file = unquote_filename(text_file) |
|
260 | 260 | pfile = open(text_file,'w') |
|
261 | 261 | pfile.write(output) |
|
262 | 262 | pfile.close() |
|
263 | 263 | print '\n*** Profile printout saved to text file',\ |
|
264 |
|
|
|
264 | repr(text_file)+'.',sys_exit | |
|
265 | 265 | |
|
266 | 266 | if opts.has_key('r'): |
|
267 | 267 | return stats |
|
268 | 268 | else: |
|
269 | 269 | return None |
|
270 | 270 | |
|
271 | 271 | @line_magic |
|
272 | 272 | def pdb(self, parameter_s=''): |
|
273 | 273 | """Control the automatic calling of the pdb interactive debugger. |
|
274 | 274 | |
|
275 | 275 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
276 | 276 | argument it works as a toggle. |
|
277 | 277 | |
|
278 | 278 | When an exception is triggered, IPython can optionally call the |
|
279 | 279 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
280 | 280 | this feature on and off. |
|
281 | 281 | |
|
282 | 282 | The initial state of this feature is set in your configuration |
|
283 | 283 | file (the option is ``InteractiveShell.pdb``). |
|
284 | 284 | |
|
285 | 285 | If you want to just activate the debugger AFTER an exception has fired, |
|
286 | 286 | without having to type '%pdb on' and rerunning your code, you can use |
|
287 | 287 | the %debug magic.""" |
|
288 | 288 | |
|
289 | 289 | par = parameter_s.strip().lower() |
|
290 | 290 | |
|
291 | 291 | if par: |
|
292 | 292 | try: |
|
293 | 293 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
294 | 294 | except KeyError: |
|
295 | 295 | print ('Incorrect argument. Use on/1, off/0, ' |
|
296 | 296 | 'or nothing for a toggle.') |
|
297 | 297 | return |
|
298 | 298 | else: |
|
299 | 299 | # toggle |
|
300 | 300 | new_pdb = not self.shell.call_pdb |
|
301 | 301 | |
|
302 | 302 | # set on the shell |
|
303 | 303 | self.shell.call_pdb = new_pdb |
|
304 | 304 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
305 | 305 | |
|
306 | 306 | @line_magic |
|
307 | 307 | def debug(self, parameter_s=''): |
|
308 | 308 | """Activate the interactive debugger in post-mortem mode. |
|
309 | 309 | |
|
310 | 310 | If an exception has just occurred, this lets you inspect its stack |
|
311 | 311 | frames interactively. Note that this will always work only on the last |
|
312 | 312 | traceback that occurred, so you must call this quickly after an |
|
313 | 313 | exception that you wish to inspect has fired, because if another one |
|
314 | 314 | occurs, it clobbers the previous one. |
|
315 | 315 | |
|
316 | 316 | If you want IPython to automatically do this on every exception, see |
|
317 | 317 | the %pdb magic for more details. |
|
318 | 318 | """ |
|
319 | 319 | self.shell.debugger(force=True) |
|
320 | 320 | |
|
321 | 321 | @line_magic |
|
322 | 322 | def tb(self, s): |
|
323 | 323 | """Print the last traceback with the currently active exception mode. |
|
324 | 324 | |
|
325 | 325 | See %xmode for changing exception reporting modes.""" |
|
326 | 326 | self.shell.showtraceback() |
|
327 | 327 | |
|
328 | 328 | @skip_doctest |
|
329 | 329 | @line_magic |
|
330 | 330 | def run(self, parameter_s='', runner=None, |
|
331 | 331 | file_finder=get_py_filename): |
|
332 | 332 | """Run the named file inside IPython as a program. |
|
333 | 333 | |
|
334 | 334 | Usage:\\ |
|
335 | 335 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
336 | 336 | |
|
337 | 337 | Parameters after the filename are passed as command-line arguments to |
|
338 | 338 | the program (put in sys.argv). Then, control returns to IPython's |
|
339 | 339 | prompt. |
|
340 | 340 | |
|
341 | 341 | This is similar to running at a system prompt:\\ |
|
342 | 342 | $ python file args\\ |
|
343 | 343 | but with the advantage of giving you IPython's tracebacks, and of |
|
344 | 344 | loading all variables into your interactive namespace for further use |
|
345 | 345 | (unless -p is used, see below). |
|
346 | 346 | |
|
347 | 347 | The file is executed in a namespace initially consisting only of |
|
348 | 348 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
349 | 349 | sees its environment as if it were being run as a stand-alone program |
|
350 | 350 | (except for sharing global objects such as previously imported |
|
351 | 351 | modules). But after execution, the IPython interactive namespace gets |
|
352 | 352 | updated with all variables defined in the program (except for __name__ |
|
353 | 353 | and sys.argv). This allows for very convenient loading of code for |
|
354 | 354 | interactive work, while giving each program a 'clean sheet' to run in. |
|
355 | 355 | |
|
356 | 356 | Options: |
|
357 | 357 | |
|
358 | 358 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
359 | 359 | without extension (as python does under import). This allows running |
|
360 | 360 | scripts and reloading the definitions in them without calling code |
|
361 | 361 | protected by an ' if __name__ == "__main__" ' clause. |
|
362 | 362 | |
|
363 | 363 | -i: run the file in IPython's namespace instead of an empty one. This |
|
364 | 364 | is useful if you are experimenting with code written in a text editor |
|
365 | 365 | which depends on variables defined interactively. |
|
366 | 366 | |
|
367 | 367 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
368 | 368 | being run. This is particularly useful if IPython is being used to |
|
369 | 369 | run unittests, which always exit with a sys.exit() call. In such |
|
370 | 370 | cases you are interested in the output of the test results, not in |
|
371 | 371 | seeing a traceback of the unittest module. |
|
372 | 372 | |
|
373 | 373 | -t: print timing information at the end of the run. IPython will give |
|
374 | 374 | you an estimated CPU time consumption for your script, which under |
|
375 | 375 | Unix uses the resource module to avoid the wraparound problems of |
|
376 | 376 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
377 | 377 | is also given (for Windows platforms this is reported as 0.0). |
|
378 | 378 | |
|
379 | 379 | If -t is given, an additional -N<N> option can be given, where <N> |
|
380 | 380 | must be an integer indicating how many times you want the script to |
|
381 | 381 | run. The final timing report will include total and per run results. |
|
382 | 382 | |
|
383 | 383 | For example (testing the script uniq_stable.py):: |
|
384 | 384 | |
|
385 | 385 | In [1]: run -t uniq_stable |
|
386 | 386 | |
|
387 | 387 | IPython CPU timings (estimated):\\ |
|
388 | 388 | User : 0.19597 s.\\ |
|
389 | 389 | System: 0.0 s.\\ |
|
390 | 390 | |
|
391 | 391 | In [2]: run -t -N5 uniq_stable |
|
392 | 392 | |
|
393 | 393 | IPython CPU timings (estimated):\\ |
|
394 | 394 | Total runs performed: 5\\ |
|
395 | 395 | Times : Total Per run\\ |
|
396 | 396 | User : 0.910862 s, 0.1821724 s.\\ |
|
397 | 397 | System: 0.0 s, 0.0 s. |
|
398 | 398 | |
|
399 | 399 | -d: run your program under the control of pdb, the Python debugger. |
|
400 | 400 | This allows you to execute your program step by step, watch variables, |
|
401 | 401 | etc. Internally, what IPython does is similar to calling: |
|
402 | 402 | |
|
403 | 403 | pdb.run('execfile("YOURFILENAME")') |
|
404 | 404 | |
|
405 | 405 | with a breakpoint set on line 1 of your file. You can change the line |
|
406 | 406 | number for this automatic breakpoint to be <N> by using the -bN option |
|
407 | 407 | (where N must be an integer). For example:: |
|
408 | 408 | |
|
409 | 409 | %run -d -b40 myscript |
|
410 | 410 | |
|
411 | 411 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
412 | 412 | the first breakpoint must be set on a line which actually does |
|
413 | 413 | something (not a comment or docstring) for it to stop execution. |
|
414 | 414 | |
|
415 | 415 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
416 | 416 | first enter 'c' (without quotes) to start execution up to the first |
|
417 | 417 | breakpoint. |
|
418 | 418 | |
|
419 | 419 | Entering 'help' gives information about the use of the debugger. You |
|
420 | 420 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
421 | 421 | at a prompt. |
|
422 | 422 | |
|
423 | 423 | -p: run program under the control of the Python profiler module (which |
|
424 | 424 | prints a detailed report of execution times, function calls, etc). |
|
425 | 425 | |
|
426 | 426 | You can pass other options after -p which affect the behavior of the |
|
427 | 427 | profiler itself. See the docs for %prun for details. |
|
428 | 428 | |
|
429 | 429 | In this mode, the program's variables do NOT propagate back to the |
|
430 | 430 | IPython interactive namespace (because they remain in the namespace |
|
431 | 431 | where the profiler executes them). |
|
432 | 432 | |
|
433 | 433 | Internally this triggers a call to %prun, see its documentation for |
|
434 | 434 | details on the options available specifically for profiling. |
|
435 | 435 | |
|
436 | 436 | There is one special usage for which the text above doesn't apply: |
|
437 | 437 | if the filename ends with .ipy, the file is run as ipython script, |
|
438 | 438 | just as if the commands were written on IPython prompt. |
|
439 | 439 | |
|
440 | 440 | -m: specify module name to load instead of script path. Similar to |
|
441 | 441 | the -m option for the python interpreter. Use this option last if you |
|
442 | 442 | want to combine with other %run options. Unlike the python interpreter |
|
443 | 443 | only source modules are allowed no .pyc or .pyo files. |
|
444 | 444 | For example:: |
|
445 | 445 | |
|
446 | 446 | %run -m example |
|
447 | 447 | |
|
448 | 448 | will run the example module. |
|
449 | 449 | |
|
450 | 450 | """ |
|
451 | 451 | |
|
452 | 452 | # get arguments and set sys.argv for program to be run. |
|
453 | 453 | opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:', |
|
454 | 454 | mode='list', list_all=1) |
|
455 | 455 | if "m" in opts: |
|
456 | 456 | modulename = opts["m"][0] |
|
457 | 457 | modpath = find_mod(modulename) |
|
458 | 458 | if modpath is None: |
|
459 | 459 | warn('%r is not a valid modulename on sys.path'%modulename) |
|
460 | 460 | return |
|
461 | 461 | arg_lst = [modpath] + arg_lst |
|
462 | 462 | try: |
|
463 | 463 | filename = file_finder(arg_lst[0]) |
|
464 | 464 | except IndexError: |
|
465 | 465 | warn('you must provide at least a filename.') |
|
466 | 466 | print '\n%run:\n', oinspect.getdoc(self.run) |
|
467 | 467 | return |
|
468 | 468 | except IOError as e: |
|
469 | 469 | try: |
|
470 | 470 | msg = str(e) |
|
471 | 471 | except UnicodeError: |
|
472 | 472 | msg = e.message |
|
473 | 473 | error(msg) |
|
474 | 474 | return |
|
475 | 475 | |
|
476 | 476 | if filename.lower().endswith('.ipy'): |
|
477 | 477 | self.shell.safe_execfile_ipy(filename) |
|
478 | 478 | return |
|
479 | 479 | |
|
480 | 480 | # Control the response to exit() calls made by the script being run |
|
481 | 481 | exit_ignore = 'e' in opts |
|
482 | 482 | |
|
483 | 483 | # Make sure that the running script gets a proper sys.argv as if it |
|
484 | 484 | # were run from a system shell. |
|
485 | 485 | save_argv = sys.argv # save it for later restoring |
|
486 | 486 | |
|
487 | 487 | # simulate shell expansion on arguments, at least tilde expansion |
|
488 | 488 | args = [ os.path.expanduser(a) for a in arg_lst[1:] ] |
|
489 | 489 | |
|
490 | 490 | sys.argv = [filename] + args # put in the proper filename |
|
491 | 491 | # protect sys.argv from potential unicode strings on Python 2: |
|
492 | 492 | if not py3compat.PY3: |
|
493 | 493 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
494 | 494 | |
|
495 | 495 | if 'i' in opts: |
|
496 | 496 | # Run in user's interactive namespace |
|
497 | 497 | prog_ns = self.shell.user_ns |
|
498 | 498 | __name__save = self.shell.user_ns['__name__'] |
|
499 | 499 | prog_ns['__name__'] = '__main__' |
|
500 | 500 | main_mod = self.shell.new_main_mod(prog_ns) |
|
501 | 501 | else: |
|
502 | 502 | # Run in a fresh, empty namespace |
|
503 | 503 | if 'n' in opts: |
|
504 | 504 | name = os.path.splitext(os.path.basename(filename))[0] |
|
505 | 505 | else: |
|
506 | 506 | name = '__main__' |
|
507 | 507 | |
|
508 | 508 | main_mod = self.shell.new_main_mod() |
|
509 | 509 | prog_ns = main_mod.__dict__ |
|
510 | 510 | prog_ns['__name__'] = name |
|
511 | 511 | |
|
512 | 512 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
513 | 513 | # set the __file__ global in the script's namespace |
|
514 | 514 | prog_ns['__file__'] = filename |
|
515 | 515 | |
|
516 | 516 | # pickle fix. See interactiveshell for an explanation. But we need to |
|
517 | 517 | # make sure that, if we overwrite __main__, we replace it at the end |
|
518 | 518 | main_mod_name = prog_ns['__name__'] |
|
519 | 519 | |
|
520 | 520 | if main_mod_name == '__main__': |
|
521 | 521 | restore_main = sys.modules['__main__'] |
|
522 | 522 | else: |
|
523 | 523 | restore_main = False |
|
524 | 524 | |
|
525 | 525 | # This needs to be undone at the end to prevent holding references to |
|
526 | 526 | # every single object ever created. |
|
527 | 527 | sys.modules[main_mod_name] = main_mod |
|
528 | 528 | |
|
529 | 529 | try: |
|
530 | 530 | stats = None |
|
531 | 531 | with self.shell.readline_no_record: |
|
532 | 532 | if 'p' in opts: |
|
533 | 533 | stats = self.prun('', None, False, opts, arg_lst, prog_ns) |
|
534 | 534 | else: |
|
535 | 535 | if 'd' in opts: |
|
536 | 536 | deb = debugger.Pdb(self.shell.colors) |
|
537 | 537 | # reset Breakpoint state, which is moronically kept |
|
538 | 538 | # in a class |
|
539 | 539 | bdb.Breakpoint.next = 1 |
|
540 | 540 | bdb.Breakpoint.bplist = {} |
|
541 | 541 | bdb.Breakpoint.bpbynumber = [None] |
|
542 | 542 | # Set an initial breakpoint to stop execution |
|
543 | 543 | maxtries = 10 |
|
544 | 544 | bp = int(opts.get('b', [1])[0]) |
|
545 | 545 | checkline = deb.checkline(filename, bp) |
|
546 | 546 | if not checkline: |
|
547 | 547 | for bp in range(bp + 1, bp + maxtries + 1): |
|
548 | 548 | if deb.checkline(filename, bp): |
|
549 | 549 | break |
|
550 | 550 | else: |
|
551 | 551 | msg = ("\nI failed to find a valid line to set " |
|
552 | 552 | "a breakpoint\n" |
|
553 | 553 | "after trying up to line: %s.\n" |
|
554 | 554 | "Please set a valid breakpoint manually " |
|
555 | 555 | "with the -b option." % bp) |
|
556 | 556 | error(msg) |
|
557 | 557 | return |
|
558 | 558 | # if we find a good linenumber, set the breakpoint |
|
559 | 559 | deb.do_break('%s:%s' % (filename, bp)) |
|
560 | 560 | # Start file run |
|
561 | 561 | print "NOTE: Enter 'c' at the", |
|
562 | 562 | print "%s prompt to start your script." % deb.prompt |
|
563 | 563 | ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns} |
|
564 | 564 | try: |
|
565 | 565 | deb.run('execfile("%s", prog_ns)' % filename, ns) |
|
566 | 566 | |
|
567 | 567 | except: |
|
568 | 568 | etype, value, tb = sys.exc_info() |
|
569 | 569 | # Skip three frames in the traceback: the %run one, |
|
570 | 570 | # one inside bdb.py, and the command-line typed by the |
|
571 | 571 | # user (run by exec in pdb itself). |
|
572 | 572 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) |
|
573 | 573 | else: |
|
574 | 574 | if runner is None: |
|
575 | 575 | runner = self.default_runner |
|
576 | 576 | if runner is None: |
|
577 | 577 | runner = self.shell.safe_execfile |
|
578 | 578 | if 't' in opts: |
|
579 | 579 | # timed execution |
|
580 | 580 | try: |
|
581 | 581 | nruns = int(opts['N'][0]) |
|
582 | 582 | if nruns < 1: |
|
583 | 583 | error('Number of runs must be >=1') |
|
584 | 584 | return |
|
585 | 585 | except (KeyError): |
|
586 | 586 | nruns = 1 |
|
587 | 587 | twall0 = time.time() |
|
588 | 588 | if nruns == 1: |
|
589 | 589 | t0 = clock2() |
|
590 | 590 | runner(filename, prog_ns, prog_ns, |
|
591 | 591 | exit_ignore=exit_ignore) |
|
592 | 592 | t1 = clock2() |
|
593 | 593 | t_usr = t1[0] - t0[0] |
|
594 | 594 | t_sys = t1[1] - t0[1] |
|
595 | 595 | print "\nIPython CPU timings (estimated):" |
|
596 | 596 | print " User : %10.2f s." % t_usr |
|
597 | 597 | print " System : %10.2f s." % t_sys |
|
598 | 598 | else: |
|
599 | 599 | runs = range(nruns) |
|
600 | 600 | t0 = clock2() |
|
601 | 601 | for nr in runs: |
|
602 | 602 | runner(filename, prog_ns, prog_ns, |
|
603 | 603 | exit_ignore=exit_ignore) |
|
604 | 604 | t1 = clock2() |
|
605 | 605 | t_usr = t1[0] - t0[0] |
|
606 | 606 | t_sys = t1[1] - t0[1] |
|
607 | 607 | print "\nIPython CPU timings (estimated):" |
|
608 | 608 | print "Total runs performed:", nruns |
|
609 | 609 | print " Times : %10.2f %10.2f" % ('Total', 'Per run') |
|
610 | 610 | print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns) |
|
611 | 611 | print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns) |
|
612 | 612 | twall1 = time.time() |
|
613 | 613 | print "Wall time: %10.2f s." % (twall1 - twall0) |
|
614 | 614 | |
|
615 | 615 | else: |
|
616 | 616 | # regular execution |
|
617 | 617 | runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore) |
|
618 | 618 | |
|
619 | 619 | if 'i' in opts: |
|
620 | 620 | self.shell.user_ns['__name__'] = __name__save |
|
621 | 621 | else: |
|
622 | 622 | # The shell MUST hold a reference to prog_ns so after %run |
|
623 | 623 | # exits, the python deletion mechanism doesn't zero it out |
|
624 | 624 | # (leaving dangling references). |
|
625 | 625 | self.shell.cache_main_mod(prog_ns, filename) |
|
626 | 626 | # update IPython interactive namespace |
|
627 | 627 | |
|
628 | 628 | # Some forms of read errors on the file may mean the |
|
629 | 629 | # __name__ key was never set; using pop we don't have to |
|
630 | 630 | # worry about a possible KeyError. |
|
631 | 631 | prog_ns.pop('__name__', None) |
|
632 | 632 | |
|
633 | 633 | self.shell.user_ns.update(prog_ns) |
|
634 | 634 | finally: |
|
635 | 635 | # It's a bit of a mystery why, but __builtins__ can change from |
|
636 | 636 | # being a module to becoming a dict missing some key data after |
|
637 | 637 | # %run. As best I can see, this is NOT something IPython is doing |
|
638 | 638 | # at all, and similar problems have been reported before: |
|
639 | 639 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html |
|
640 | 640 | # Since this seems to be done by the interpreter itself, the best |
|
641 | 641 | # we can do is to at least restore __builtins__ for the user on |
|
642 | 642 | # exit. |
|
643 | 643 | self.shell.user_ns['__builtins__'] = builtin_mod |
|
644 | 644 | |
|
645 | 645 | # Ensure key global structures are restored |
|
646 | 646 | sys.argv = save_argv |
|
647 | 647 | if restore_main: |
|
648 | 648 | sys.modules['__main__'] = restore_main |
|
649 | 649 | else: |
|
650 | 650 | # Remove from sys.modules the reference to main_mod we'd |
|
651 | 651 | # added. Otherwise it will trap references to objects |
|
652 | 652 | # contained therein. |
|
653 | 653 | del sys.modules[main_mod_name] |
|
654 | 654 | |
|
655 | 655 | return stats |
|
656 | 656 | |
|
657 | 657 | @skip_doctest |
|
658 | 658 | @line_cell_magic |
|
659 | 659 | def timeit(self, line='', cell=None): |
|
660 | 660 | """Time execution of a Python statement or expression |
|
661 | 661 | |
|
662 | 662 | Usage, in line mode: |
|
663 | 663 | %timeit [-n<N> -r<R> [-t|-c]] statement |
|
664 | 664 | or in cell mode: |
|
665 | 665 | %%timeit [-n<N> -r<R> [-t|-c]] setup_code |
|
666 | 666 | code |
|
667 | 667 | code... |
|
668 | 668 | |
|
669 | 669 | Time execution of a Python statement or expression using the timeit |
|
670 | 670 | module. This function can be used both as a line and cell magic: |
|
671 | 671 | |
|
672 | 672 | - In line mode you can time a single-line statement (though multiple |
|
673 | 673 | ones can be chained with using semicolons). |
|
674 | 674 | |
|
675 | 675 | - In cell mode, the statement in the first line is used as setup code |
|
676 | 676 | (executed but not timed) and the body of the cell is timed. The cell |
|
677 | 677 | body has access to any variables created in the setup code. |
|
678 | 678 | |
|
679 | 679 | Options: |
|
680 | 680 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
681 | 681 | is not given, a fitting value is chosen. |
|
682 | 682 | |
|
683 | 683 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
684 | 684 | Default: 3 |
|
685 | 685 | |
|
686 | 686 | -t: use time.time to measure the time, which is the default on Unix. |
|
687 | 687 | This function measures wall time. |
|
688 | 688 | |
|
689 | 689 | -c: use time.clock to measure the time, which is the default on |
|
690 | 690 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
691 | 691 | instead and returns the CPU user time. |
|
692 | 692 | |
|
693 | 693 | -p<P>: use a precision of <P> digits to display the timing result. |
|
694 | 694 | Default: 3 |
|
695 | 695 | |
|
696 | 696 | |
|
697 | 697 | Examples |
|
698 | 698 | -------- |
|
699 | 699 | :: |
|
700 | 700 | |
|
701 | 701 | In [1]: %timeit pass |
|
702 | 702 | 10000000 loops, best of 3: 53.3 ns per loop |
|
703 | 703 | |
|
704 | 704 | In [2]: u = None |
|
705 | 705 | |
|
706 | 706 | In [3]: %timeit u is None |
|
707 | 707 | 10000000 loops, best of 3: 184 ns per loop |
|
708 | 708 | |
|
709 | 709 | In [4]: %timeit -r 4 u == None |
|
710 | 710 | 1000000 loops, best of 4: 242 ns per loop |
|
711 | 711 | |
|
712 | 712 | In [5]: import time |
|
713 | 713 | |
|
714 | 714 | In [6]: %timeit -n1 time.sleep(2) |
|
715 | 715 | 1 loops, best of 3: 2 s per loop |
|
716 | 716 | |
|
717 | 717 | |
|
718 | 718 | The times reported by %timeit will be slightly higher than those |
|
719 | 719 | reported by the timeit.py script when variables are accessed. This is |
|
720 | 720 | due to the fact that %timeit executes the statement in the namespace |
|
721 | 721 | of the shell, compared with timeit.py, which uses a single setup |
|
722 | 722 | statement to import function or create variables. Generally, the bias |
|
723 | 723 | does not matter as long as results from timeit.py are not mixed with |
|
724 | 724 | those from %timeit.""" |
|
725 | 725 | |
|
726 | 726 | import timeit |
|
727 | 727 | import math |
|
728 | 728 | |
|
729 | 729 | # XXX: Unfortunately the unicode 'micro' symbol can cause problems in |
|
730 | 730 | # certain terminals. Until we figure out a robust way of |
|
731 | 731 | # auto-detecting if the terminal can deal with it, use plain 'us' for |
|
732 | 732 | # microseconds. I am really NOT happy about disabling the proper |
|
733 | 733 | # 'micro' prefix, but crashing is worse... If anyone knows what the |
|
734 | 734 | # right solution for this is, I'm all ears... |
|
735 | 735 | # |
|
736 | 736 | # Note: using |
|
737 | 737 | # |
|
738 | 738 | # s = u'\xb5' |
|
739 | 739 | # s.encode(sys.getdefaultencoding()) |
|
740 | 740 | # |
|
741 | 741 | # is not sufficient, as I've seen terminals where that fails but |
|
742 | 742 | # print s |
|
743 | 743 | # |
|
744 | 744 | # succeeds |
|
745 | 745 | # |
|
746 | 746 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 |
|
747 | 747 | |
|
748 | 748 | #units = [u"s", u"ms",u'\xb5',"ns"] |
|
749 | 749 | units = [u"s", u"ms",u'us',"ns"] |
|
750 | 750 | |
|
751 | 751 | scaling = [1, 1e3, 1e6, 1e9] |
|
752 | 752 | |
|
753 | 753 | opts, stmt = self.parse_options(line,'n:r:tcp:', |
|
754 | 754 | posix=False, strict=False) |
|
755 | 755 | if stmt == "" and cell is None: |
|
756 | 756 | return |
|
757 | 757 | timefunc = timeit.default_timer |
|
758 | 758 | number = int(getattr(opts, "n", 0)) |
|
759 | 759 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
760 | 760 | precision = int(getattr(opts, "p", 3)) |
|
761 | 761 | if hasattr(opts, "t"): |
|
762 | 762 | timefunc = time.time |
|
763 | 763 | if hasattr(opts, "c"): |
|
764 | 764 | timefunc = clock |
|
765 | 765 | |
|
766 | 766 | timer = timeit.Timer(timer=timefunc) |
|
767 | 767 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
768 | 768 | # but is there a better way to achieve that the code stmt has access |
|
769 | 769 | # to the shell namespace? |
|
770 | 770 | transform = self.shell.input_splitter.transform_cell |
|
771 | 771 | if cell is None: |
|
772 | 772 | # called as line magic |
|
773 | 773 | setup = 'pass' |
|
774 | 774 | stmt = timeit.reindent(transform(stmt), 8) |
|
775 | 775 | else: |
|
776 | 776 | setup = timeit.reindent(transform(stmt), 4) |
|
777 | 777 | stmt = timeit.reindent(transform(cell), 8) |
|
778 | 778 | |
|
779 | 779 | # From Python 3.3, this template uses new-style string formatting. |
|
780 | 780 | if sys.version_info >= (3, 3): |
|
781 | 781 | src = timeit.template.format(stmt=stmt, setup=setup) |
|
782 | 782 | else: |
|
783 | 783 | src = timeit.template % dict(stmt=stmt, setup=setup) |
|
784 | 784 | |
|
785 | 785 | # Track compilation time so it can be reported if too long |
|
786 | 786 | # Minimum time above which compilation time will be reported |
|
787 | 787 | tc_min = 0.1 |
|
788 | 788 | |
|
789 | 789 | t0 = clock() |
|
790 | 790 | code = compile(src, "<magic-timeit>", "exec") |
|
791 | 791 | tc = clock()-t0 |
|
792 | 792 | |
|
793 | 793 | ns = {} |
|
794 | 794 | exec code in self.shell.user_ns, ns |
|
795 | 795 | timer.inner = ns["inner"] |
|
796 | 796 | |
|
797 | 797 | if number == 0: |
|
798 | 798 | # determine number so that 0.2 <= total time < 2.0 |
|
799 | 799 | number = 1 |
|
800 | 800 | for i in range(1, 10): |
|
801 | 801 | if timer.timeit(number) >= 0.2: |
|
802 | 802 | break |
|
803 | 803 | number *= 10 |
|
804 | 804 | |
|
805 | 805 | best = min(timer.repeat(repeat, number)) / number |
|
806 | 806 | |
|
807 | 807 | if best > 0.0 and best < 1000.0: |
|
808 | 808 | order = min(-int(math.floor(math.log10(best)) // 3), 3) |
|
809 | 809 | elif best >= 1000.0: |
|
810 | 810 | order = 0 |
|
811 | 811 | else: |
|
812 | 812 | order = 3 |
|
813 | 813 | print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat, |
|
814 | 814 | precision, |
|
815 | 815 | best * scaling[order], |
|
816 | 816 | units[order]) |
|
817 | 817 | if tc > tc_min: |
|
818 | 818 | print "Compiler time: %.2f s" % tc |
|
819 | 819 | |
|
820 | 820 | @skip_doctest |
|
821 | 821 | @needs_local_scope |
|
822 | 822 | @line_magic |
|
823 | 823 | def time(self,parameter_s, user_locals): |
|
824 | 824 | """Time execution of a Python statement or expression. |
|
825 | 825 | |
|
826 | 826 | The CPU and wall clock times are printed, and the value of the |
|
827 | 827 | expression (if any) is returned. Note that under Win32, system time |
|
828 | 828 | is always reported as 0, since it can not be measured. |
|
829 | 829 | |
|
830 | 830 | This function provides very basic timing functionality. In Python |
|
831 | 831 | 2.3, the timeit module offers more control and sophistication, so this |
|
832 | 832 | could be rewritten to use it (patches welcome). |
|
833 | 833 | |
|
834 | 834 | Examples |
|
835 | 835 | -------- |
|
836 | 836 | :: |
|
837 | 837 | |
|
838 | 838 | In [1]: time 2**128 |
|
839 | 839 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
840 | 840 | Wall time: 0.00 |
|
841 | 841 | Out[1]: 340282366920938463463374607431768211456L |
|
842 | 842 | |
|
843 | 843 | In [2]: n = 1000000 |
|
844 | 844 | |
|
845 | 845 | In [3]: time sum(range(n)) |
|
846 | 846 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
847 | 847 | Wall time: 1.37 |
|
848 | 848 | Out[3]: 499999500000L |
|
849 | 849 | |
|
850 | 850 | In [4]: time print 'hello world' |
|
851 | 851 | hello world |
|
852 | 852 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
853 | 853 | Wall time: 0.00 |
|
854 | 854 | |
|
855 | 855 | Note that the time needed by Python to compile the given expression |
|
856 | 856 | will be reported if it is more than 0.1s. In this example, the |
|
857 | 857 | actual exponentiation is done by Python at compilation time, so while |
|
858 | 858 | the expression can take a noticeable amount of time to compute, that |
|
859 | 859 | time is purely due to the compilation: |
|
860 | 860 | |
|
861 | 861 | In [5]: time 3**9999; |
|
862 | 862 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
863 | 863 | Wall time: 0.00 s |
|
864 | 864 | |
|
865 | 865 | In [6]: time 3**999999; |
|
866 | 866 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
867 | 867 | Wall time: 0.00 s |
|
868 | 868 | Compiler : 0.78 s |
|
869 | 869 | """ |
|
870 | 870 | |
|
871 | 871 | # fail immediately if the given expression can't be compiled |
|
872 | 872 | |
|
873 | 873 | expr = self.shell.prefilter(parameter_s,False) |
|
874 | 874 | |
|
875 | 875 | # Minimum time above which compilation time will be reported |
|
876 | 876 | tc_min = 0.1 |
|
877 | 877 | |
|
878 | 878 | try: |
|
879 | 879 | mode = 'eval' |
|
880 | 880 | t0 = clock() |
|
881 | 881 | code = compile(expr,'<timed eval>',mode) |
|
882 | 882 | tc = clock()-t0 |
|
883 | 883 | except SyntaxError: |
|
884 | 884 | mode = 'exec' |
|
885 | 885 | t0 = clock() |
|
886 | 886 | code = compile(expr,'<timed exec>',mode) |
|
887 | 887 | tc = clock()-t0 |
|
888 | 888 | # skew measurement as little as possible |
|
889 | 889 | glob = self.shell.user_ns |
|
890 | 890 | wtime = time.time |
|
891 | 891 | # time execution |
|
892 | 892 | wall_st = wtime() |
|
893 | 893 | if mode=='eval': |
|
894 | 894 | st = clock2() |
|
895 | 895 | out = eval(code, glob, user_locals) |
|
896 | 896 | end = clock2() |
|
897 | 897 | else: |
|
898 | 898 | st = clock2() |
|
899 | 899 | exec code in glob, user_locals |
|
900 | 900 | end = clock2() |
|
901 | 901 | out = None |
|
902 | 902 | wall_end = wtime() |
|
903 | 903 | # Compute actual times and report |
|
904 | 904 | wall_time = wall_end-wall_st |
|
905 | 905 | cpu_user = end[0]-st[0] |
|
906 | 906 | cpu_sys = end[1]-st[1] |
|
907 | 907 | cpu_tot = cpu_user+cpu_sys |
|
908 | 908 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
909 | 909 | (cpu_user,cpu_sys,cpu_tot) |
|
910 | 910 | print "Wall time: %.2f s" % wall_time |
|
911 | 911 | if tc > tc_min: |
|
912 | 912 | print "Compiler : %.2f s" % tc |
|
913 | 913 | return out |
|
914 | 914 | |
|
915 | 915 | @skip_doctest |
|
916 | 916 | @line_magic |
|
917 | 917 | def macro(self, parameter_s=''): |
|
918 | 918 | """Define a macro for future re-execution. It accepts ranges of history, |
|
919 | 919 | filenames or string objects. |
|
920 | 920 | |
|
921 | 921 | Usage:\\ |
|
922 | 922 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
923 | 923 | |
|
924 | 924 | Options: |
|
925 | 925 | |
|
926 | 926 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
927 | 927 | so that magics are loaded in their transformed version to valid |
|
928 | 928 | Python. If this option is given, the raw input as typed as the |
|
929 | 929 | command line is used instead. |
|
930 | 930 | |
|
931 | 931 | This will define a global variable called `name` which is a string |
|
932 | 932 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
933 | 933 | above) from your input history into a single string. This variable |
|
934 | 934 | acts like an automatic function which re-executes those lines as if |
|
935 | 935 | you had typed them. You just type 'name' at the prompt and the code |
|
936 | 936 | executes. |
|
937 | 937 | |
|
938 | 938 | The syntax for indicating input ranges is described in %history. |
|
939 | 939 | |
|
940 | 940 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
941 | 941 | notation, where N:M means numbers N through M-1. |
|
942 | 942 | |
|
943 | 943 | For example, if your history contains (%hist prints it):: |
|
944 | 944 | |
|
945 | 945 | 44: x=1 |
|
946 | 946 | 45: y=3 |
|
947 | 947 | 46: z=x+y |
|
948 | 948 | 47: print x |
|
949 | 949 | 48: a=5 |
|
950 | 950 | 49: print 'x',x,'y',y |
|
951 | 951 | |
|
952 | 952 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
953 | 953 | called my_macro with:: |
|
954 | 954 | |
|
955 | 955 | In [55]: %macro my_macro 44-47 49 |
|
956 | 956 | |
|
957 | 957 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
958 | 958 | in one pass. |
|
959 | 959 | |
|
960 | 960 | You don't need to give the line-numbers in order, and any given line |
|
961 | 961 | number can appear multiple times. You can assemble macros with any |
|
962 | 962 | lines from your input history in any order. |
|
963 | 963 | |
|
964 | 964 | The macro is a simple object which holds its value in an attribute, |
|
965 | 965 | but IPython's display system checks for macros and executes them as |
|
966 | 966 | code instead of printing them when you type their name. |
|
967 | 967 | |
|
968 | 968 | You can view a macro's contents by explicitly printing it with:: |
|
969 | 969 | |
|
970 | 970 | print macro_name |
|
971 | 971 | |
|
972 | 972 | """ |
|
973 | 973 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
974 | 974 | if not args: # List existing macros |
|
975 | 975 | return sorted(k for k,v in self.shell.user_ns.iteritems() if\ |
|
976 | 976 | isinstance(v, Macro)) |
|
977 | 977 | if len(args) == 1: |
|
978 | 978 | raise UsageError( |
|
979 | 979 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") |
|
980 | 980 | name, codefrom = args[0], " ".join(args[1:]) |
|
981 | 981 | |
|
982 | 982 | #print 'rng',ranges # dbg |
|
983 | 983 | try: |
|
984 | 984 | lines = self.shell.find_user_code(codefrom, 'r' in opts) |
|
985 | 985 | except (ValueError, TypeError) as e: |
|
986 | 986 | print e.args[0] |
|
987 | 987 | return |
|
988 | 988 | macro = Macro(lines) |
|
989 | 989 | self.shell.define_macro(name, macro) |
|
990 | 990 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
991 | 991 | print '=== Macro contents: ===' |
|
992 | 992 | print macro, |
|
993 | 993 | |
|
994 | 994 | @magic_arguments.magic_arguments() |
|
995 | 995 | @magic_arguments.argument('output', type=str, default='', nargs='?', |
|
996 | 996 | help="""The name of the variable in which to store output. |
|
997 | 997 | This is a utils.io.CapturedIO object with stdout/err attributes |
|
998 | 998 | for the text of the captured output. |
|
999 | 999 | |
|
1000 | 1000 | CapturedOutput also has a show() method for displaying the output, |
|
1001 | 1001 | and __call__ as well, so you can use that to quickly display the |
|
1002 | 1002 | output. |
|
1003 | 1003 | |
|
1004 | 1004 | If unspecified, captured output is discarded. |
|
1005 | 1005 | """ |
|
1006 | 1006 | ) |
|
1007 | 1007 | @magic_arguments.argument('--no-stderr', action="store_true", |
|
1008 | 1008 | help="""Don't capture stderr.""" |
|
1009 | 1009 | ) |
|
1010 | 1010 | @magic_arguments.argument('--no-stdout', action="store_true", |
|
1011 | 1011 | help="""Don't capture stdout.""" |
|
1012 | 1012 | ) |
|
1013 | 1013 | @cell_magic |
|
1014 | 1014 | def capture(self, line, cell): |
|
1015 | 1015 | """run the cell, capturing stdout/err""" |
|
1016 | 1016 | args = magic_arguments.parse_argstring(self.capture, line) |
|
1017 | 1017 | out = not args.no_stdout |
|
1018 | 1018 | err = not args.no_stderr |
|
1019 | 1019 | with capture_output(out, err) as io: |
|
1020 | 1020 | self.shell.run_cell(cell) |
|
1021 | 1021 | if args.output: |
|
1022 | 1022 | self.shell.user_ns[args.output] = io |
@@ -1,340 +1,340 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Paging capabilities for IPython.core |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | |
|
10 | 10 | Notes |
|
11 | 11 | ----- |
|
12 | 12 | |
|
13 | 13 | For now this uses ipapi, so it can't be in IPython.utils. If we can get |
|
14 | 14 | rid of that dependency, we could move it there. |
|
15 | 15 | ----- |
|
16 | 16 | """ |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Copyright (C) 2008-2011 The IPython Development Team |
|
20 | 20 | # |
|
21 | 21 | # Distributed under the terms of the BSD License. The full license is in |
|
22 | 22 | # the file COPYING, distributed as part of this software. |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Imports |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | import os |
|
30 | 30 | import re |
|
31 | 31 | import sys |
|
32 | 32 | import tempfile |
|
33 | 33 | |
|
34 | 34 | from io import UnsupportedOperation |
|
35 | 35 | |
|
36 | 36 | from IPython.core import ipapi |
|
37 | 37 | from IPython.core.error import TryNext |
|
38 | 38 | from IPython.utils.cursesimport import use_curses |
|
39 | 39 | from IPython.utils.data import chop |
|
40 | 40 | from IPython.utils import io |
|
41 | 41 | from IPython.utils.process import system |
|
42 | 42 | from IPython.utils.terminal import get_terminal_size |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | # Classes and functions |
|
47 | 47 | #----------------------------------------------------------------------------- |
|
48 | 48 | |
|
49 | 49 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
50 | 50 | |
|
51 | 51 | def page_dumb(strng, start=0, screen_lines=25): |
|
52 | 52 | """Very dumb 'pager' in Python, for when nothing else works. |
|
53 | 53 | |
|
54 | 54 | Only moves forward, same interface as page(), except for pager_cmd and |
|
55 | 55 | mode.""" |
|
56 | 56 | |
|
57 | 57 | out_ln = strng.splitlines()[start:] |
|
58 | 58 | screens = chop(out_ln,screen_lines-1) |
|
59 | 59 | if len(screens) == 1: |
|
60 | 60 | print >>io.stdout, os.linesep.join(screens[0]) |
|
61 | 61 | else: |
|
62 | 62 | last_escape = "" |
|
63 | 63 | for scr in screens[0:-1]: |
|
64 | 64 | hunk = os.linesep.join(scr) |
|
65 | 65 | print >>io.stdout, last_escape + hunk |
|
66 | 66 | if not page_more(): |
|
67 | 67 | return |
|
68 | 68 | esc_list = esc_re.findall(hunk) |
|
69 | 69 | if len(esc_list) > 0: |
|
70 | 70 | last_escape = esc_list[-1] |
|
71 | 71 | print >>io.stdout, last_escape + os.linesep.join(screens[-1]) |
|
72 | 72 | |
|
73 | 73 | def _detect_screen_size(use_curses, screen_lines_def): |
|
74 | 74 | """Attempt to work out the number of lines on the screen. |
|
75 | 75 | |
|
76 | 76 | This is called by page(). It can raise an error (e.g. when run in the |
|
77 | 77 | test suite), so it's separated out so it can easily be called in a try block. |
|
78 | 78 | """ |
|
79 | 79 | TERM = os.environ.get('TERM',None) |
|
80 | 80 | if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5': |
|
81 | 81 | local_use_curses = use_curses |
|
82 | 82 | else: |
|
83 | 83 | # curses causes problems on many terminals other than xterm, and |
|
84 | 84 | # some termios calls lock up on Sun OS5. |
|
85 | 85 | local_use_curses = False |
|
86 | 86 | if local_use_curses: |
|
87 | 87 | import termios |
|
88 | 88 | import curses |
|
89 | 89 | # There is a bug in curses, where *sometimes* it fails to properly |
|
90 | 90 | # initialize, and then after the endwin() call is made, the |
|
91 | 91 | # terminal is left in an unusable state. Rather than trying to |
|
92 | 92 | # check everytime for this (by requesting and comparing termios |
|
93 | 93 | # flags each time), we just save the initial terminal state and |
|
94 | 94 | # unconditionally reset it every time. It's cheaper than making |
|
95 | 95 | # the checks. |
|
96 | 96 | term_flags = termios.tcgetattr(sys.stdout) |
|
97 | 97 | |
|
98 | 98 | # Curses modifies the stdout buffer size by default, which messes |
|
99 | 99 | # up Python's normal stdout buffering. This would manifest itself |
|
100 | 100 | # to IPython users as delayed printing on stdout after having used |
|
101 | 101 | # the pager. |
|
102 | 102 | # |
|
103 | 103 | # We can prevent this by manually setting the NCURSES_NO_SETBUF |
|
104 | 104 | # environment variable. For more details, see: |
|
105 | 105 | # http://bugs.python.org/issue10144 |
|
106 | 106 | NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None) |
|
107 | 107 | os.environ['NCURSES_NO_SETBUF'] = '' |
|
108 | 108 | |
|
109 | 109 | # Proceed with curses initialization |
|
110 | 110 | scr = curses.initscr() |
|
111 | 111 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
112 | 112 | curses.endwin() |
|
113 | 113 | |
|
114 | 114 | # Restore environment |
|
115 | 115 | if NCURSES_NO_SETBUF is None: |
|
116 | 116 | del os.environ['NCURSES_NO_SETBUF'] |
|
117 | 117 | else: |
|
118 | 118 | os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF |
|
119 | 119 | |
|
120 | 120 | # Restore terminal state in case endwin() didn't. |
|
121 | 121 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
|
122 | 122 | # Now we have what we needed: the screen size in rows/columns |
|
123 | 123 | return screen_lines_real |
|
124 | 124 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
125 | 125 | #screen_cols,'columns.' # dbg |
|
126 | 126 | else: |
|
127 | 127 | return screen_lines_def |
|
128 | 128 | |
|
129 | 129 | def page(strng, start=0, screen_lines=0, pager_cmd=None): |
|
130 | 130 | """Print a string, piping through a pager after a certain length. |
|
131 | 131 | |
|
132 | 132 | The screen_lines parameter specifies the number of *usable* lines of your |
|
133 | 133 | terminal screen (total lines minus lines you need to reserve to show other |
|
134 | 134 | information). |
|
135 | 135 | |
|
136 | 136 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
137 | 137 | your screen size and will only use up to (screen_size+screen_lines) for |
|
138 | 138 | printing, paging after that. That is, if you want auto-detection but need |
|
139 | 139 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
140 | 140 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
141 | 141 | |
|
142 | 142 | If a string won't fit in the allowed lines, it is sent through the |
|
143 | 143 | specified pager command. If none given, look for PAGER in the environment, |
|
144 | 144 | and ultimately default to less. |
|
145 | 145 | |
|
146 | 146 | If no system pager works, the string is sent through a 'dumb pager' |
|
147 | 147 | written in python, very simplistic. |
|
148 | 148 | """ |
|
149 | 149 | |
|
150 | 150 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
151 | 151 | # negative value. Offset to 0 for robustness. |
|
152 | 152 | start = max(0, start) |
|
153 | 153 | |
|
154 | 154 | # first, try the hook |
|
155 | 155 | ip = ipapi.get() |
|
156 | 156 | if ip: |
|
157 | 157 | try: |
|
158 | 158 | ip.hooks.show_in_pager(strng) |
|
159 | 159 | return |
|
160 | 160 | except TryNext: |
|
161 | 161 | pass |
|
162 | 162 | |
|
163 | 163 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
164 | 164 | TERM = os.environ.get('TERM','dumb') |
|
165 | 165 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
166 | 166 | print strng |
|
167 | 167 | return |
|
168 | 168 | # chop off the topmost part of the string we don't want to see |
|
169 | 169 | str_lines = strng.splitlines()[start:] |
|
170 | 170 | str_toprint = os.linesep.join(str_lines) |
|
171 | 171 | num_newlines = len(str_lines) |
|
172 | 172 | len_str = len(str_toprint) |
|
173 | 173 | |
|
174 | 174 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
175 | 175 | # takes. Very basic, but good enough for docstrings in reasonable |
|
176 | 176 | # terminals. If someone later feels like refining it, it's not hard. |
|
177 | 177 | numlines = max(num_newlines,int(len_str/80)+1) |
|
178 | 178 | |
|
179 | 179 | screen_lines_def = get_terminal_size()[1] |
|
180 | 180 | |
|
181 | 181 | # auto-determine screen size |
|
182 | 182 | if screen_lines <= 0: |
|
183 | 183 | try: |
|
184 | 184 | screen_lines += _detect_screen_size(use_curses, screen_lines_def) |
|
185 | 185 | except (TypeError, UnsupportedOperation): |
|
186 | 186 | print >>io.stdout, str_toprint |
|
187 | 187 | return |
|
188 | 188 | |
|
189 | 189 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
190 | 190 | if numlines <= screen_lines : |
|
191 | 191 | #print '*** normal print' # dbg |
|
192 | 192 | print >>io.stdout, str_toprint |
|
193 | 193 | else: |
|
194 | 194 | # Try to open pager and default to internal one if that fails. |
|
195 | 195 | # All failure modes are tagged as 'retval=1', to match the return |
|
196 | 196 | # value of a failed system command. If any intermediate attempt |
|
197 | 197 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
198 | 198 | pager_cmd = get_pager_cmd(pager_cmd) |
|
199 | 199 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
200 | 200 | if os.name == 'nt': |
|
201 | 201 | if pager_cmd.startswith('type'): |
|
202 | 202 | # The default WinXP 'type' command is failing on complex strings. |
|
203 | 203 | retval = 1 |
|
204 | 204 | else: |
|
205 | 205 | tmpname = tempfile.mktemp('.txt') |
|
206 | 206 | tmpfile = open(tmpname,'wt') |
|
207 | 207 | tmpfile.write(strng) |
|
208 | 208 | tmpfile.close() |
|
209 | 209 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
210 | 210 | if os.system(cmd): |
|
211 | 211 | retval = 1 |
|
212 | 212 | else: |
|
213 | 213 | retval = None |
|
214 | 214 | os.remove(tmpname) |
|
215 | 215 | else: |
|
216 | 216 | try: |
|
217 | 217 | retval = None |
|
218 | 218 | # if I use popen4, things hang. No idea why. |
|
219 | 219 | #pager,shell_out = os.popen4(pager_cmd) |
|
220 | 220 | pager = os.popen(pager_cmd,'w') |
|
221 | 221 | pager.write(strng) |
|
222 | 222 | pager.close() |
|
223 | 223 | retval = pager.close() # success returns None |
|
224 | 224 | except IOError as msg: # broken pipe when user quits |
|
225 | 225 | if msg.args == (32,'Broken pipe'): |
|
226 | 226 | retval = None |
|
227 | 227 | else: |
|
228 | 228 | retval = 1 |
|
229 | 229 | except OSError: |
|
230 | 230 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
231 | 231 | retval = 1 |
|
232 | 232 | if retval is not None: |
|
233 | 233 | page_dumb(strng,screen_lines=screen_lines) |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | def page_file(fname, start=0, pager_cmd=None): |
|
237 | 237 | """Page a file, using an optional pager command and starting line. |
|
238 | 238 | """ |
|
239 | 239 | |
|
240 | 240 | pager_cmd = get_pager_cmd(pager_cmd) |
|
241 | 241 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
242 | 242 | |
|
243 | 243 | try: |
|
244 | 244 | if os.environ['TERM'] in ['emacs','dumb']: |
|
245 | 245 | raise EnvironmentError |
|
246 | 246 | system(pager_cmd + ' ' + fname) |
|
247 | 247 | except: |
|
248 | 248 | try: |
|
249 | 249 | if start > 0: |
|
250 | 250 | start -= 1 |
|
251 | 251 | page(open(fname).read(),start) |
|
252 | 252 | except: |
|
253 |
print 'Unable to show file', |
|
|
253 | print 'Unable to show file',repr(fname) | |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | def get_pager_cmd(pager_cmd=None): |
|
257 | 257 | """Return a pager command. |
|
258 | 258 | |
|
259 | 259 | Makes some attempts at finding an OS-correct one. |
|
260 | 260 | """ |
|
261 | 261 | if os.name == 'posix': |
|
262 | 262 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
263 | 263 | elif os.name in ['nt','dos']: |
|
264 | 264 | default_pager_cmd = 'type' |
|
265 | 265 | |
|
266 | 266 | if pager_cmd is None: |
|
267 | 267 | try: |
|
268 | 268 | pager_cmd = os.environ['PAGER'] |
|
269 | 269 | except: |
|
270 | 270 | pager_cmd = default_pager_cmd |
|
271 | 271 | return pager_cmd |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | def get_pager_start(pager, start): |
|
275 | 275 | """Return the string for paging files with an offset. |
|
276 | 276 | |
|
277 | 277 | This is the '+N' argument which less and more (under Unix) accept. |
|
278 | 278 | """ |
|
279 | 279 | |
|
280 | 280 | if pager in ['less','more']: |
|
281 | 281 | if start: |
|
282 | 282 | start_string = '+' + str(start) |
|
283 | 283 | else: |
|
284 | 284 | start_string = '' |
|
285 | 285 | else: |
|
286 | 286 | start_string = '' |
|
287 | 287 | return start_string |
|
288 | 288 | |
|
289 | 289 | |
|
290 | 290 | # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch() |
|
291 | 291 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
292 | 292 | import msvcrt |
|
293 | 293 | def page_more(): |
|
294 | 294 | """ Smart pausing between pages |
|
295 | 295 | |
|
296 | 296 | @return: True if need print more lines, False if quit |
|
297 | 297 | """ |
|
298 | 298 | io.stdout.write('---Return to continue, q to quit--- ') |
|
299 | 299 | ans = msvcrt.getch() |
|
300 | 300 | if ans in ("q", "Q"): |
|
301 | 301 | result = False |
|
302 | 302 | else: |
|
303 | 303 | result = True |
|
304 | 304 | io.stdout.write("\b"*37 + " "*37 + "\b"*37) |
|
305 | 305 | return result |
|
306 | 306 | else: |
|
307 | 307 | def page_more(): |
|
308 | 308 | ans = raw_input('---Return to continue, q to quit--- ') |
|
309 | 309 | if ans.lower().startswith('q'): |
|
310 | 310 | return False |
|
311 | 311 | else: |
|
312 | 312 | return True |
|
313 | 313 | |
|
314 | 314 | |
|
315 | 315 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
316 | 316 | """Print a string snipping the midsection to fit in width. |
|
317 | 317 | |
|
318 | 318 | print_full: mode control: |
|
319 | 319 | - 0: only snip long strings |
|
320 | 320 | - 1: send to page() directly. |
|
321 | 321 | - 2: snip long strings and ask for full length viewing with page() |
|
322 | 322 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
323 | 323 | |
|
324 | 324 | if print_full == 1: |
|
325 | 325 | page(header+str) |
|
326 | 326 | return 0 |
|
327 | 327 | |
|
328 | 328 | print header, |
|
329 | 329 | if len(str) < width: |
|
330 | 330 | print str |
|
331 | 331 | snip = 0 |
|
332 | 332 | else: |
|
333 | 333 | whalf = int((width -5)/2) |
|
334 | 334 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
335 | 335 | snip = 1 |
|
336 | 336 | if snip and print_full == 2: |
|
337 | 337 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
338 | 338 | page(str) |
|
339 | 339 | return snip |
|
340 | 340 |
General Comments 0
You need to be logged in to leave comments.
Login now