##// END OF EJS Templates
fix for list pdb listcommand, broken for %run -d...
Jörgen Stenarson -
Show More
@@ -1,551 +1,557
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 from __future__ import print_function
28 28
29 29 import bdb
30 30 import linecache
31 31 import sys
32 32
33 33 from IPython.utils import PyColorize, py3compat
34 34 from IPython.core import ipapi
35 35 from IPython.utils import coloransi, io, openpy
36 36 from IPython.core.excolors import exception_colors
37 37
38 38 # See if we can use pydb.
39 39 has_pydb = False
40 40 prompt = 'ipdb> '
41 41 #We have to check this directly from sys.argv, config struct not yet available
42 42 if '--pydb' in sys.argv:
43 43 try:
44 44 import pydb
45 45 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
46 46 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
47 47 # better protect against it.
48 48 has_pydb = True
49 49 except ImportError:
50 50 print("Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available")
51 51
52 52 if has_pydb:
53 53 from pydb import Pdb as OldPdb
54 54 #print "Using pydb for %run -d and post-mortem" #dbg
55 55 prompt = 'ipydb> '
56 56 else:
57 57 from pdb import Pdb as OldPdb
58 58
59 59 # Allow the set_trace code to operate outside of an ipython instance, even if
60 60 # it does so with some limitations. The rest of this support is implemented in
61 61 # the Tracer constructor.
62 62 def BdbQuit_excepthook(et,ev,tb):
63 63 if et==bdb.BdbQuit:
64 64 print('Exiting Debugger.')
65 65 else:
66 66 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
67 67
68 68 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
69 69 print('Exiting Debugger.')
70 70
71 71
72 72 class Tracer(object):
73 73 """Class for local debugging, similar to pdb.set_trace.
74 74
75 75 Instances of this class, when called, behave like pdb.set_trace, but
76 76 providing IPython's enhanced capabilities.
77 77
78 78 This is implemented as a class which must be initialized in your own code
79 79 and not as a standalone function because we need to detect at runtime
80 80 whether IPython is already active or not. That detection is done in the
81 81 constructor, ensuring that this code plays nicely with a running IPython,
82 82 while functioning acceptably (though with limitations) if outside of it.
83 83 """
84 84
85 85 def __init__(self,colors=None):
86 86 """Create a local debugger instance.
87 87
88 88 :Parameters:
89 89
90 90 - `colors` (None): a string containing the name of the color scheme to
91 91 use, it must be one of IPython's valid color schemes. If not given, the
92 92 function will default to the current IPython scheme when running inside
93 93 IPython, and to 'NoColor' otherwise.
94 94
95 95 Usage example:
96 96
97 97 from IPython.core.debugger import Tracer; debug_here = Tracer()
98 98
99 99 ... later in your code
100 100 debug_here() # -> will open up the debugger at that point.
101 101
102 102 Once the debugger activates, you can use all of its regular commands to
103 103 step through code, set breakpoints, etc. See the pdb documentation
104 104 from the Python standard library for usage details.
105 105 """
106 106
107 107 try:
108 108 ip = get_ipython()
109 109 except NameError:
110 110 # Outside of ipython, we set our own exception hook manually
111 111 BdbQuit_excepthook.excepthook_ori = sys.excepthook
112 112 sys.excepthook = BdbQuit_excepthook
113 113 def_colors = 'NoColor'
114 114 try:
115 115 # Limited tab completion support
116 116 import readline
117 117 readline.parse_and_bind('tab: complete')
118 118 except ImportError:
119 119 pass
120 120 else:
121 121 # In ipython, we use its custom exception handler mechanism
122 122 def_colors = ip.colors
123 123 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
124 124
125 125 if colors is None:
126 126 colors = def_colors
127 127
128 128 # The stdlib debugger internally uses a modified repr from the `repr`
129 129 # module, that limits the length of printed strings to a hardcoded
130 130 # limit of 30 characters. That much trimming is too aggressive, let's
131 131 # at least raise that limit to 80 chars, which should be enough for
132 132 # most interactive uses.
133 133 try:
134 134 from repr import aRepr
135 135 aRepr.maxstring = 80
136 136 except:
137 137 # This is only a user-facing convenience, so any error we encounter
138 138 # here can be warned about but can be otherwise ignored. These
139 139 # printouts will tell us about problems if this API changes
140 140 import traceback
141 141 traceback.print_exc()
142 142
143 143 self.debugger = Pdb(colors)
144 144
145 145 def __call__(self):
146 146 """Starts an interactive debugger at the point where called.
147 147
148 148 This is similar to the pdb.set_trace() function from the std lib, but
149 149 using IPython's enhanced debugger."""
150 150
151 151 self.debugger.set_trace(sys._getframe().f_back)
152 152
153 153
154 154 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
155 155 """Make new_fn have old_fn's doc string. This is particularly useful
156 156 for the do_... commands that hook into the help system.
157 157 Adapted from from a comp.lang.python posting
158 158 by Duncan Booth."""
159 159 def wrapper(*args, **kw):
160 160 return new_fn(*args, **kw)
161 161 if old_fn.__doc__:
162 162 wrapper.__doc__ = old_fn.__doc__ + additional_text
163 163 return wrapper
164 164
165 165
166 166 def _file_lines(fname):
167 167 """Return the contents of a named file as a list of lines.
168 168
169 169 This function never raises an IOError exception: if the file can't be
170 170 read, it simply returns an empty list."""
171 171
172 172 try:
173 173 outfile = open(fname)
174 174 except IOError:
175 175 return []
176 176 else:
177 177 out = outfile.readlines()
178 178 outfile.close()
179 179 return out
180 180
181 181
182 182 def _readline(x):
183 183 """helper to pop elements off list of string
184 184
185 185 call with list of strings, return readline function that will pop
186 186 one line off the beginning of a copy of the list with each call.
187 187 raise StopIteration when empty or on third call
188 188 """
189 189 x = x[:2]
190 190 def readline():
191 191 if x:
192 192 return x.pop(0)
193 193 else:
194 194 raise StopIteration
195 195 return readline
196 196
197 197
198 198 class Pdb(OldPdb):
199 199 """Modified Pdb class, does not load readline."""
200 200
201 201 def __init__(self,color_scheme='NoColor',completekey=None,
202 202 stdin=None, stdout=None):
203 203
204 204 # Parent constructor:
205 205 if has_pydb and completekey is None:
206 206 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
207 207 else:
208 208 OldPdb.__init__(self,completekey,stdin,stdout)
209 209
210 210 self.prompt = prompt # The default prompt is '(Pdb)'
211 211
212 212 # IPython changes...
213 213 self.is_pydb = has_pydb
214 214
215 215 self.shell = ipapi.get()
216 216
217 217 if self.is_pydb:
218 218
219 219 # interactiveshell.py's ipalias seems to want pdb's checkline
220 220 # which located in pydb.fn
221 221 import pydb.fns
222 222 self.checkline = lambda filename, lineno: \
223 223 pydb.fns.checkline(self, filename, lineno)
224 224
225 225 self.curframe = None
226 226 self.do_restart = self.new_do_restart
227 227
228 228 self.old_all_completions = self.shell.Completer.all_completions
229 229 self.shell.Completer.all_completions=self.all_completions
230 230
231 231 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
232 232 OldPdb.do_list)
233 233 self.do_l = self.do_list
234 234 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
235 235 OldPdb.do_frame)
236 236
237 237 self.aliases = {}
238 238
239 239 # Create color table: we copy the default one from the traceback
240 240 # module and add a few attributes needed for debugging
241 241 self.color_scheme_table = exception_colors()
242 242
243 243 # shorthands
244 244 C = coloransi.TermColors
245 245 cst = self.color_scheme_table
246 246
247 247 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
248 248 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
249 249
250 250 cst['Linux'].colors.breakpoint_enabled = C.LightRed
251 251 cst['Linux'].colors.breakpoint_disabled = C.Red
252 252
253 253 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
254 254 cst['LightBG'].colors.breakpoint_disabled = C.Red
255 255
256 256 self.set_colors(color_scheme)
257 257
258 258 # Add a python parser so we can syntax highlight source while
259 259 # debugging.
260 260 self.parser = PyColorize.Parser()
261 261
262 262 def set_colors(self, scheme):
263 263 """Shorthand access to the color table scheme selector method."""
264 264 self.color_scheme_table.set_active_scheme(scheme)
265 265
266 266 def interaction(self, frame, traceback):
267 267 self.shell.set_completer_frame(frame)
268 268 OldPdb.interaction(self, frame, traceback)
269 269
270 270 def new_do_up(self, arg):
271 271 OldPdb.do_up(self, arg)
272 272 self.shell.set_completer_frame(self.curframe)
273 273 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
274 274
275 275 def new_do_down(self, arg):
276 276 OldPdb.do_down(self, arg)
277 277 self.shell.set_completer_frame(self.curframe)
278 278
279 279 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
280 280
281 281 def new_do_frame(self, arg):
282 282 OldPdb.do_frame(self, arg)
283 283 self.shell.set_completer_frame(self.curframe)
284 284
285 285 def new_do_quit(self, arg):
286 286
287 287 if hasattr(self, 'old_all_completions'):
288 288 self.shell.Completer.all_completions=self.old_all_completions
289 289
290 290
291 291 return OldPdb.do_quit(self, arg)
292 292
293 293 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
294 294
295 295 def new_do_restart(self, arg):
296 296 """Restart command. In the context of ipython this is exactly the same
297 297 thing as 'quit'."""
298 298 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
299 299 return self.do_quit(arg)
300 300
301 301 def postloop(self):
302 302 self.shell.set_completer_frame(None)
303 303
304 304 def print_stack_trace(self):
305 305 try:
306 306 for frame_lineno in self.stack:
307 307 self.print_stack_entry(frame_lineno, context = 5)
308 308 except KeyboardInterrupt:
309 309 pass
310 310
311 311 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
312 312 context = 3):
313 313 #frame, lineno = frame_lineno
314 314 print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout)
315 315
316 316 # vds: >>
317 317 frame, lineno = frame_lineno
318 318 filename = frame.f_code.co_filename
319 319 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
320 320 # vds: <<
321 321
322 322 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
323 323 import linecache, repr
324 324
325 325 ret = []
326 326
327 327 Colors = self.color_scheme_table.active_colors
328 328 ColorsNormal = Colors.Normal
329 329 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
330 330 tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
331 331 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
332 332 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
333 333 ColorsNormal)
334 334
335 335 frame, lineno = frame_lineno
336 336
337 337 return_value = ''
338 338 if '__return__' in frame.f_locals:
339 339 rv = frame.f_locals['__return__']
340 340 #return_value += '->'
341 341 return_value += repr.repr(rv) + '\n'
342 342 ret.append(return_value)
343 343
344 344 #s = filename + '(' + `lineno` + ')'
345 345 filename = self.canonic(frame.f_code.co_filename)
346 346 link = tpl_link % filename
347 347
348 348 if frame.f_code.co_name:
349 349 func = frame.f_code.co_name
350 350 else:
351 351 func = "<lambda>"
352 352
353 353 call = ''
354 354 if func != '?':
355 355 if '__args__' in frame.f_locals:
356 356 args = repr.repr(frame.f_locals['__args__'])
357 357 else:
358 358 args = '()'
359 359 call = tpl_call % (func, args)
360 360
361 361 # The level info should be generated in the same format pdb uses, to
362 362 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
363 363 if frame is self.curframe:
364 364 ret.append('> ')
365 365 else:
366 366 ret.append(' ')
367 367 ret.append('%s(%s)%s\n' % (link,lineno,call))
368 368
369 369 start = lineno - 1 - context//2
370 370 lines = linecache.getlines(filename)
371 371 try:
372 372 encoding, _ = openpy.detect_encoding(_readline(lines))
373 373 except SyntaxError:
374 374 encoding = "ascii"
375 375 start = max(start, 0)
376 376 start = min(start, len(lines) - context)
377 377 lines = lines[start : start + context]
378 378
379 379 for i,line in enumerate(lines):
380 380 show_arrow = (start + 1 + i == lineno)
381 381 linetpl = (frame is self.curframe or show_arrow) \
382 382 and tpl_line_em \
383 383 or tpl_line
384 384 ret.append(self.__format_line(linetpl, filename,
385 385 start + 1 + i, py3compat.cast_unicode(line),
386 386 arrow = show_arrow) )
387 387 return ''.join(ret)
388 388
389 389 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
390 390 bp_mark = ""
391 391 bp_mark_color = ""
392 392
393 393 scheme = self.color_scheme_table.active_scheme_name
394 394 new_line, err = self.parser.format2(line, 'str', scheme)
395 395 if not err: line = new_line
396 396
397 397 bp = None
398 398 if lineno in self.get_file_breaks(filename):
399 399 bps = self.get_breaks(filename, lineno)
400 400 bp = bps[-1]
401 401
402 402 if bp:
403 403 Colors = self.color_scheme_table.active_colors
404 404 bp_mark = str(bp.number)
405 405 bp_mark_color = Colors.breakpoint_enabled
406 406 if not bp.enabled:
407 407 bp_mark_color = Colors.breakpoint_disabled
408 408
409 409 numbers_width = 7
410 410 if arrow:
411 411 # This is the line with the error
412 412 pad = numbers_width - len(str(lineno)) - len(bp_mark)
413 413 if pad >= 3:
414 414 marker = '-'*(pad-3) + '-> '
415 415 elif pad == 2:
416 416 marker = '> '
417 417 elif pad == 1:
418 418 marker = '>'
419 419 else:
420 420 marker = ''
421 421 num = '%s%s' % (marker, str(lineno))
422 422 line = tpl_line % (bp_mark_color + bp_mark, num, line)
423 423 else:
424 424 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
425 425 line = tpl_line % (bp_mark_color + bp_mark, num, line)
426 426
427 427 return line
428 428
429 429 def list_command_pydb(self, arg):
430 430 """List command to use if we have a newer pydb installed"""
431 431 filename, first, last = OldPdb.parse_list_cmd(self, arg)
432 432 if filename is not None:
433 433 self.print_list_lines(filename, first, last)
434 434
435 435 def print_list_lines(self, filename, first, last):
436 436 """The printing (as opposed to the parsing part of a 'list'
437 437 command."""
438 438 try:
439 439 Colors = self.color_scheme_table.active_colors
440 440 ColorsNormal = Colors.Normal
441 441 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
442 442 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
443 443 src = []
444 if filename == "<string>" and hasattr(self, "_exec_filename"):
445 lines = list(open(self._exec_filename))
446 else:
444 447 lines = linecache.getlines(filename)
445 448 try:
446 449 encoding, _ = openpy.detect_encoding(_readline(lines))
447 450 except SyntaxError:
448 451 encoding = "ascii"
452 if not lines:
453 print >>io.stdout, "No src could be located using filename: %r"%filename
454 return #Bailing out, there is nothing to see here
449 455 for lineno in range(first, last+1):
450 456 line = py3compat.cast_unicode(lines[lineno])
451 457 if not line:
452 458 break
453 459
454 460 if lineno == self.curframe.f_lineno:
455 461 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
456 462 else:
457 463 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
458 464
459 465 src.append(line)
460 466 self.lineno = lineno
461 467
462 468 print(''.join(src), file=io.stdout)
463 469
464 470 except KeyboardInterrupt:
465 471 pass
466 472
467 473 def do_list(self, arg):
468 474 self.lastcmd = 'list'
469 475 last = None
470 476 if arg:
471 477 try:
472 478 x = eval(arg, {}, {})
473 479 if type(x) == type(()):
474 480 first, last = x
475 481 first = int(first)
476 482 last = int(last)
477 483 if last < first:
478 484 # Assume it's a count
479 485 last = first + last
480 486 else:
481 487 first = max(1, int(x) - 5)
482 488 except:
483 489 print('*** Error in argument:', repr(arg))
484 490 return
485 491 elif self.lineno is None:
486 492 first = max(1, self.curframe.f_lineno - 5)
487 493 else:
488 494 first = self.lineno + 1
489 495 if last is None:
490 496 last = first + 10
491 497 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
492 498
493 499 # vds: >>
494 500 lineno = first
495 501 filename = self.curframe.f_code.co_filename
496 502 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
497 503 # vds: <<
498 504
499 505 do_l = do_list
500 506
501 507 def do_pdef(self, arg):
502 508 """The debugger interface to magic_pdef"""
503 509 namespaces = [('Locals', self.curframe.f_locals),
504 510 ('Globals', self.curframe.f_globals)]
505 511 self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
506 512
507 513 def do_pdoc(self, arg):
508 514 """The debugger interface to magic_pdoc"""
509 515 namespaces = [('Locals', self.curframe.f_locals),
510 516 ('Globals', self.curframe.f_globals)]
511 517 self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
512 518
513 519 def do_pinfo(self, arg):
514 520 """The debugger equivalant of ?obj"""
515 521 namespaces = [('Locals', self.curframe.f_locals),
516 522 ('Globals', self.curframe.f_globals)]
517 523 self.shell.find_line_magic('pinfo')("pinfo %s" % arg,
518 524 namespaces=namespaces)
519 525
520 526 def checkline(self, filename, lineno):
521 527 """Check whether specified line seems to be executable.
522 528
523 529 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
524 530 line or EOF). Warning: testing is not comprehensive.
525 531 """
526 532 #######################################################################
527 533 # XXX Hack! Use python-2.5 compatible code for this call, because with
528 534 # all of our changes, we've drifted from the pdb api in 2.6. For now,
529 535 # changing:
530 536 #
531 537 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
532 538 # to:
533 539 #
534 540 line = linecache.getline(filename, lineno)
535 541 #
536 542 # does the trick. But in reality, we need to fix this by reconciling
537 543 # our updates with the new Pdb APIs in Python 2.6.
538 544 #
539 545 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
540 546 #######################################################################
541 547
542 548 if not line:
543 549 print('End of file', file=self.stdout)
544 550 return 0
545 551 line = line.strip()
546 552 # Don't allow setting breakpoint at a blank line
547 553 if (not line or (line[0] == '#') or
548 554 (line[:3] == '"""') or line[:3] == "'''"):
549 555 print('*** Blank or comment', file=self.stdout)
550 556 return 0
551 557 return lineno
@@ -1,1014 +1,1016
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 stats_stream = stats.stream
230 230 try:
231 231 stats.stream = stdout_trap
232 232 stats.print_stats(*lims)
233 233 finally:
234 234 stats.stream = stats_stream
235 235
236 236 output = stdout_trap.getvalue()
237 237 output = output.rstrip()
238 238
239 239 if 'q' not in opts:
240 240 page.page(output)
241 241 print sys_exit,
242 242
243 243 dump_file = opts.D[0]
244 244 text_file = opts.T[0]
245 245 if dump_file:
246 246 dump_file = unquote_filename(dump_file)
247 247 prof.dump_stats(dump_file)
248 248 print '\n*** Profile stats marshalled to file',\
249 249 repr(dump_file)+'.',sys_exit
250 250 if text_file:
251 251 text_file = unquote_filename(text_file)
252 252 pfile = open(text_file,'w')
253 253 pfile.write(output)
254 254 pfile.close()
255 255 print '\n*** Profile printout saved to text file',\
256 256 repr(text_file)+'.',sys_exit
257 257
258 258 if 'r' in opts:
259 259 return stats
260 260 else:
261 261 return None
262 262
263 263 @line_magic
264 264 def pdb(self, parameter_s=''):
265 265 """Control the automatic calling of the pdb interactive debugger.
266 266
267 267 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
268 268 argument it works as a toggle.
269 269
270 270 When an exception is triggered, IPython can optionally call the
271 271 interactive pdb debugger after the traceback printout. %pdb toggles
272 272 this feature on and off.
273 273
274 274 The initial state of this feature is set in your configuration
275 275 file (the option is ``InteractiveShell.pdb``).
276 276
277 277 If you want to just activate the debugger AFTER an exception has fired,
278 278 without having to type '%pdb on' and rerunning your code, you can use
279 279 the %debug magic."""
280 280
281 281 par = parameter_s.strip().lower()
282 282
283 283 if par:
284 284 try:
285 285 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
286 286 except KeyError:
287 287 print ('Incorrect argument. Use on/1, off/0, '
288 288 'or nothing for a toggle.')
289 289 return
290 290 else:
291 291 # toggle
292 292 new_pdb = not self.shell.call_pdb
293 293
294 294 # set on the shell
295 295 self.shell.call_pdb = new_pdb
296 296 print 'Automatic pdb calling has been turned',on_off(new_pdb)
297 297
298 298 @line_magic
299 299 def debug(self, parameter_s=''):
300 300 """Activate the interactive debugger in post-mortem mode.
301 301
302 302 If an exception has just occurred, this lets you inspect its stack
303 303 frames interactively. Note that this will always work only on the last
304 304 traceback that occurred, so you must call this quickly after an
305 305 exception that you wish to inspect has fired, because if another one
306 306 occurs, it clobbers the previous one.
307 307
308 308 If you want IPython to automatically do this on every exception, see
309 309 the %pdb magic for more details.
310 310 """
311 311 self.shell.debugger(force=True)
312 312
313 313 @line_magic
314 314 def tb(self, s):
315 315 """Print the last traceback with the currently active exception mode.
316 316
317 317 See %xmode for changing exception reporting modes."""
318 318 self.shell.showtraceback()
319 319
320 320 @skip_doctest
321 321 @line_magic
322 322 def run(self, parameter_s='', runner=None,
323 323 file_finder=get_py_filename):
324 324 """Run the named file inside IPython as a program.
325 325
326 326 Usage:\\
327 327 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
328 328
329 329 Parameters after the filename are passed as command-line arguments to
330 330 the program (put in sys.argv). Then, control returns to IPython's
331 331 prompt.
332 332
333 333 This is similar to running at a system prompt:\\
334 334 $ python file args\\
335 335 but with the advantage of giving you IPython's tracebacks, and of
336 336 loading all variables into your interactive namespace for further use
337 337 (unless -p is used, see below).
338 338
339 339 The file is executed in a namespace initially consisting only of
340 340 __name__=='__main__' and sys.argv constructed as indicated. It thus
341 341 sees its environment as if it were being run as a stand-alone program
342 342 (except for sharing global objects such as previously imported
343 343 modules). But after execution, the IPython interactive namespace gets
344 344 updated with all variables defined in the program (except for __name__
345 345 and sys.argv). This allows for very convenient loading of code for
346 346 interactive work, while giving each program a 'clean sheet' to run in.
347 347
348 348 Options:
349 349
350 350 -n: __name__ is NOT set to '__main__', but to the running file's name
351 351 without extension (as python does under import). This allows running
352 352 scripts and reloading the definitions in them without calling code
353 353 protected by an ' if __name__ == "__main__" ' clause.
354 354
355 355 -i: run the file in IPython's namespace instead of an empty one. This
356 356 is useful if you are experimenting with code written in a text editor
357 357 which depends on variables defined interactively.
358 358
359 359 -e: ignore sys.exit() calls or SystemExit exceptions in the script
360 360 being run. This is particularly useful if IPython is being used to
361 361 run unittests, which always exit with a sys.exit() call. In such
362 362 cases you are interested in the output of the test results, not in
363 363 seeing a traceback of the unittest module.
364 364
365 365 -t: print timing information at the end of the run. IPython will give
366 366 you an estimated CPU time consumption for your script, which under
367 367 Unix uses the resource module to avoid the wraparound problems of
368 368 time.clock(). Under Unix, an estimate of time spent on system tasks
369 369 is also given (for Windows platforms this is reported as 0.0).
370 370
371 371 If -t is given, an additional -N<N> option can be given, where <N>
372 372 must be an integer indicating how many times you want the script to
373 373 run. The final timing report will include total and per run results.
374 374
375 375 For example (testing the script uniq_stable.py)::
376 376
377 377 In [1]: run -t uniq_stable
378 378
379 379 IPython CPU timings (estimated):\\
380 380 User : 0.19597 s.\\
381 381 System: 0.0 s.\\
382 382
383 383 In [2]: run -t -N5 uniq_stable
384 384
385 385 IPython CPU timings (estimated):\\
386 386 Total runs performed: 5\\
387 387 Times : Total Per run\\
388 388 User : 0.910862 s, 0.1821724 s.\\
389 389 System: 0.0 s, 0.0 s.
390 390
391 391 -d: run your program under the control of pdb, the Python debugger.
392 392 This allows you to execute your program step by step, watch variables,
393 393 etc. Internally, what IPython does is similar to calling:
394 394
395 395 pdb.run('execfile("YOURFILENAME")')
396 396
397 397 with a breakpoint set on line 1 of your file. You can change the line
398 398 number for this automatic breakpoint to be <N> by using the -bN option
399 399 (where N must be an integer). For example::
400 400
401 401 %run -d -b40 myscript
402 402
403 403 will set the first breakpoint at line 40 in myscript.py. Note that
404 404 the first breakpoint must be set on a line which actually does
405 405 something (not a comment or docstring) for it to stop execution.
406 406
407 407 When the pdb debugger starts, you will see a (Pdb) prompt. You must
408 408 first enter 'c' (without quotes) to start execution up to the first
409 409 breakpoint.
410 410
411 411 Entering 'help' gives information about the use of the debugger. You
412 412 can easily see pdb's full documentation with "import pdb;pdb.help()"
413 413 at a prompt.
414 414
415 415 -p: run program under the control of the Python profiler module (which
416 416 prints a detailed report of execution times, function calls, etc).
417 417
418 418 You can pass other options after -p which affect the behavior of the
419 419 profiler itself. See the docs for %prun for details.
420 420
421 421 In this mode, the program's variables do NOT propagate back to the
422 422 IPython interactive namespace (because they remain in the namespace
423 423 where the profiler executes them).
424 424
425 425 Internally this triggers a call to %prun, see its documentation for
426 426 details on the options available specifically for profiling.
427 427
428 428 There is one special usage for which the text above doesn't apply:
429 429 if the filename ends with .ipy, the file is run as ipython script,
430 430 just as if the commands were written on IPython prompt.
431 431
432 432 -m: specify module name to load instead of script path. Similar to
433 433 the -m option for the python interpreter. Use this option last if you
434 434 want to combine with other %run options. Unlike the python interpreter
435 435 only source modules are allowed no .pyc or .pyo files.
436 436 For example::
437 437
438 438 %run -m example
439 439
440 440 will run the example module.
441 441
442 442 """
443 443
444 444 # get arguments and set sys.argv for program to be run.
445 445 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
446 446 mode='list', list_all=1)
447 447 if "m" in opts:
448 448 modulename = opts["m"][0]
449 449 modpath = find_mod(modulename)
450 450 if modpath is None:
451 451 warn('%r is not a valid modulename on sys.path'%modulename)
452 452 return
453 453 arg_lst = [modpath] + arg_lst
454 454 try:
455 455 filename = file_finder(arg_lst[0])
456 456 except IndexError:
457 457 warn('you must provide at least a filename.')
458 458 print '\n%run:\n', oinspect.getdoc(self.run)
459 459 return
460 460 except IOError as e:
461 461 try:
462 462 msg = str(e)
463 463 except UnicodeError:
464 464 msg = e.message
465 465 error(msg)
466 466 return
467 467
468 468 if filename.lower().endswith('.ipy'):
469 469 self.shell.safe_execfile_ipy(filename)
470 470 return
471 471
472 472 # Control the response to exit() calls made by the script being run
473 473 exit_ignore = 'e' in opts
474 474
475 475 # Make sure that the running script gets a proper sys.argv as if it
476 476 # were run from a system shell.
477 477 save_argv = sys.argv # save it for later restoring
478 478
479 479 # simulate shell expansion on arguments, at least tilde expansion
480 480 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
481 481
482 482 sys.argv = [filename] + args # put in the proper filename
483 483 # protect sys.argv from potential unicode strings on Python 2:
484 484 if not py3compat.PY3:
485 485 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
486 486
487 487 if 'i' in opts:
488 488 # Run in user's interactive namespace
489 489 prog_ns = self.shell.user_ns
490 490 __name__save = self.shell.user_ns['__name__']
491 491 prog_ns['__name__'] = '__main__'
492 492 main_mod = self.shell.new_main_mod(prog_ns)
493 493 else:
494 494 # Run in a fresh, empty namespace
495 495 if 'n' in opts:
496 496 name = os.path.splitext(os.path.basename(filename))[0]
497 497 else:
498 498 name = '__main__'
499 499
500 500 main_mod = self.shell.new_main_mod()
501 501 prog_ns = main_mod.__dict__
502 502 prog_ns['__name__'] = name
503 503
504 504 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
505 505 # set the __file__ global in the script's namespace
506 506 prog_ns['__file__'] = filename
507 507
508 508 # pickle fix. See interactiveshell for an explanation. But we need to
509 509 # make sure that, if we overwrite __main__, we replace it at the end
510 510 main_mod_name = prog_ns['__name__']
511 511
512 512 if main_mod_name == '__main__':
513 513 restore_main = sys.modules['__main__']
514 514 else:
515 515 restore_main = False
516 516
517 517 # This needs to be undone at the end to prevent holding references to
518 518 # every single object ever created.
519 519 sys.modules[main_mod_name] = main_mod
520 520
521 521 try:
522 522 stats = None
523 523 with self.shell.readline_no_record:
524 524 if 'p' in opts:
525 525 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
526 526 else:
527 527 if 'd' in opts:
528 528 deb = debugger.Pdb(self.shell.colors)
529 529 # reset Breakpoint state, which is moronically kept
530 530 # in a class
531 531 bdb.Breakpoint.next = 1
532 532 bdb.Breakpoint.bplist = {}
533 533 bdb.Breakpoint.bpbynumber = [None]
534 534 # Set an initial breakpoint to stop execution
535 535 maxtries = 10
536 536 bp = int(opts.get('b', [1])[0])
537 537 checkline = deb.checkline(filename, bp)
538 538 if not checkline:
539 539 for bp in range(bp + 1, bp + maxtries + 1):
540 540 if deb.checkline(filename, bp):
541 541 break
542 542 else:
543 543 msg = ("\nI failed to find a valid line to set "
544 544 "a breakpoint\n"
545 545 "after trying up to line: %s.\n"
546 546 "Please set a valid breakpoint manually "
547 547 "with the -b option." % bp)
548 548 error(msg)
549 549 return
550 550 # if we find a good linenumber, set the breakpoint
551 551 deb.do_break('%s:%s' % (filename, bp))
552 552 # Start file run
553 553 print "NOTE: Enter 'c' at the",
554 554 print "%s prompt to start your script." % deb.prompt
555 555 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
556 556 try:
557 #save filename so it can be used by methods on the deb object
558 deb._exec_filename = filename
557 559 deb.run('execfile("%s", prog_ns)' % filename, ns)
558 560
559 561 except:
560 562 etype, value, tb = sys.exc_info()
561 563 # Skip three frames in the traceback: the %run one,
562 564 # one inside bdb.py, and the command-line typed by the
563 565 # user (run by exec in pdb itself).
564 566 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
565 567 else:
566 568 if runner is None:
567 569 runner = self.default_runner
568 570 if runner is None:
569 571 runner = self.shell.safe_execfile
570 572 if 't' in opts:
571 573 # timed execution
572 574 try:
573 575 nruns = int(opts['N'][0])
574 576 if nruns < 1:
575 577 error('Number of runs must be >=1')
576 578 return
577 579 except (KeyError):
578 580 nruns = 1
579 581 twall0 = time.time()
580 582 if nruns == 1:
581 583 t0 = clock2()
582 584 runner(filename, prog_ns, prog_ns,
583 585 exit_ignore=exit_ignore)
584 586 t1 = clock2()
585 587 t_usr = t1[0] - t0[0]
586 588 t_sys = t1[1] - t0[1]
587 589 print "\nIPython CPU timings (estimated):"
588 590 print " User : %10.2f s." % t_usr
589 591 print " System : %10.2f s." % t_sys
590 592 else:
591 593 runs = range(nruns)
592 594 t0 = clock2()
593 595 for nr in runs:
594 596 runner(filename, prog_ns, prog_ns,
595 597 exit_ignore=exit_ignore)
596 598 t1 = clock2()
597 599 t_usr = t1[0] - t0[0]
598 600 t_sys = t1[1] - t0[1]
599 601 print "\nIPython CPU timings (estimated):"
600 602 print "Total runs performed:", nruns
601 603 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
602 604 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
603 605 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
604 606 twall1 = time.time()
605 607 print "Wall time: %10.2f s." % (twall1 - twall0)
606 608
607 609 else:
608 610 # regular execution
609 611 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
610 612
611 613 if 'i' in opts:
612 614 self.shell.user_ns['__name__'] = __name__save
613 615 else:
614 616 # The shell MUST hold a reference to prog_ns so after %run
615 617 # exits, the python deletion mechanism doesn't zero it out
616 618 # (leaving dangling references).
617 619 self.shell.cache_main_mod(prog_ns, filename)
618 620 # update IPython interactive namespace
619 621
620 622 # Some forms of read errors on the file may mean the
621 623 # __name__ key was never set; using pop we don't have to
622 624 # worry about a possible KeyError.
623 625 prog_ns.pop('__name__', None)
624 626
625 627 self.shell.user_ns.update(prog_ns)
626 628 finally:
627 629 # It's a bit of a mystery why, but __builtins__ can change from
628 630 # being a module to becoming a dict missing some key data after
629 631 # %run. As best I can see, this is NOT something IPython is doing
630 632 # at all, and similar problems have been reported before:
631 633 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
632 634 # Since this seems to be done by the interpreter itself, the best
633 635 # we can do is to at least restore __builtins__ for the user on
634 636 # exit.
635 637 self.shell.user_ns['__builtins__'] = builtin_mod
636 638
637 639 # Ensure key global structures are restored
638 640 sys.argv = save_argv
639 641 if restore_main:
640 642 sys.modules['__main__'] = restore_main
641 643 else:
642 644 # Remove from sys.modules the reference to main_mod we'd
643 645 # added. Otherwise it will trap references to objects
644 646 # contained therein.
645 647 del sys.modules[main_mod_name]
646 648
647 649 return stats
648 650
649 651 @skip_doctest
650 652 @line_cell_magic
651 653 def timeit(self, line='', cell=None):
652 654 """Time execution of a Python statement or expression
653 655
654 656 Usage, in line mode:
655 657 %timeit [-n<N> -r<R> [-t|-c]] statement
656 658 or in cell mode:
657 659 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
658 660 code
659 661 code...
660 662
661 663 Time execution of a Python statement or expression using the timeit
662 664 module. This function can be used both as a line and cell magic:
663 665
664 666 - In line mode you can time a single-line statement (though multiple
665 667 ones can be chained with using semicolons).
666 668
667 669 - In cell mode, the statement in the first line is used as setup code
668 670 (executed but not timed) and the body of the cell is timed. The cell
669 671 body has access to any variables created in the setup code.
670 672
671 673 Options:
672 674 -n<N>: execute the given statement <N> times in a loop. If this value
673 675 is not given, a fitting value is chosen.
674 676
675 677 -r<R>: repeat the loop iteration <R> times and take the best result.
676 678 Default: 3
677 679
678 680 -t: use time.time to measure the time, which is the default on Unix.
679 681 This function measures wall time.
680 682
681 683 -c: use time.clock to measure the time, which is the default on
682 684 Windows and measures wall time. On Unix, resource.getrusage is used
683 685 instead and returns the CPU user time.
684 686
685 687 -p<P>: use a precision of <P> digits to display the timing result.
686 688 Default: 3
687 689
688 690
689 691 Examples
690 692 --------
691 693 ::
692 694
693 695 In [1]: %timeit pass
694 696 10000000 loops, best of 3: 53.3 ns per loop
695 697
696 698 In [2]: u = None
697 699
698 700 In [3]: %timeit u is None
699 701 10000000 loops, best of 3: 184 ns per loop
700 702
701 703 In [4]: %timeit -r 4 u == None
702 704 1000000 loops, best of 4: 242 ns per loop
703 705
704 706 In [5]: import time
705 707
706 708 In [6]: %timeit -n1 time.sleep(2)
707 709 1 loops, best of 3: 2 s per loop
708 710
709 711
710 712 The times reported by %timeit will be slightly higher than those
711 713 reported by the timeit.py script when variables are accessed. This is
712 714 due to the fact that %timeit executes the statement in the namespace
713 715 of the shell, compared with timeit.py, which uses a single setup
714 716 statement to import function or create variables. Generally, the bias
715 717 does not matter as long as results from timeit.py are not mixed with
716 718 those from %timeit."""
717 719
718 720 import timeit
719 721 import math
720 722
721 723 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
722 724 # certain terminals. Until we figure out a robust way of
723 725 # auto-detecting if the terminal can deal with it, use plain 'us' for
724 726 # microseconds. I am really NOT happy about disabling the proper
725 727 # 'micro' prefix, but crashing is worse... If anyone knows what the
726 728 # right solution for this is, I'm all ears...
727 729 #
728 730 # Note: using
729 731 #
730 732 # s = u'\xb5'
731 733 # s.encode(sys.getdefaultencoding())
732 734 #
733 735 # is not sufficient, as I've seen terminals where that fails but
734 736 # print s
735 737 #
736 738 # succeeds
737 739 #
738 740 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
739 741
740 742 #units = [u"s", u"ms",u'\xb5',"ns"]
741 743 units = [u"s", u"ms",u'us',"ns"]
742 744
743 745 scaling = [1, 1e3, 1e6, 1e9]
744 746
745 747 opts, stmt = self.parse_options(line,'n:r:tcp:',
746 748 posix=False, strict=False)
747 749 if stmt == "" and cell is None:
748 750 return
749 751 timefunc = timeit.default_timer
750 752 number = int(getattr(opts, "n", 0))
751 753 repeat = int(getattr(opts, "r", timeit.default_repeat))
752 754 precision = int(getattr(opts, "p", 3))
753 755 if hasattr(opts, "t"):
754 756 timefunc = time.time
755 757 if hasattr(opts, "c"):
756 758 timefunc = clock
757 759
758 760 timer = timeit.Timer(timer=timefunc)
759 761 # this code has tight coupling to the inner workings of timeit.Timer,
760 762 # but is there a better way to achieve that the code stmt has access
761 763 # to the shell namespace?
762 764 transform = self.shell.input_splitter.transform_cell
763 765 if cell is None:
764 766 # called as line magic
765 767 setup = 'pass'
766 768 stmt = timeit.reindent(transform(stmt), 8)
767 769 else:
768 770 setup = timeit.reindent(transform(stmt), 4)
769 771 stmt = timeit.reindent(transform(cell), 8)
770 772
771 773 # From Python 3.3, this template uses new-style string formatting.
772 774 if sys.version_info >= (3, 3):
773 775 src = timeit.template.format(stmt=stmt, setup=setup)
774 776 else:
775 777 src = timeit.template % dict(stmt=stmt, setup=setup)
776 778
777 779 # Track compilation time so it can be reported if too long
778 780 # Minimum time above which compilation time will be reported
779 781 tc_min = 0.1
780 782
781 783 t0 = clock()
782 784 code = compile(src, "<magic-timeit>", "exec")
783 785 tc = clock()-t0
784 786
785 787 ns = {}
786 788 exec code in self.shell.user_ns, ns
787 789 timer.inner = ns["inner"]
788 790
789 791 if number == 0:
790 792 # determine number so that 0.2 <= total time < 2.0
791 793 number = 1
792 794 for i in range(1, 10):
793 795 if timer.timeit(number) >= 0.2:
794 796 break
795 797 number *= 10
796 798
797 799 best = min(timer.repeat(repeat, number)) / number
798 800
799 801 if best > 0.0 and best < 1000.0:
800 802 order = min(-int(math.floor(math.log10(best)) // 3), 3)
801 803 elif best >= 1000.0:
802 804 order = 0
803 805 else:
804 806 order = 3
805 807 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
806 808 precision,
807 809 best * scaling[order],
808 810 units[order])
809 811 if tc > tc_min:
810 812 print "Compiler time: %.2f s" % tc
811 813
812 814 @skip_doctest
813 815 @needs_local_scope
814 816 @line_magic
815 817 def time(self,parameter_s, local_ns=None):
816 818 """Time execution of a Python statement or expression.
817 819
818 820 The CPU and wall clock times are printed, and the value of the
819 821 expression (if any) is returned. Note that under Win32, system time
820 822 is always reported as 0, since it can not be measured.
821 823
822 824 This function provides very basic timing functionality. In Python
823 825 2.3, the timeit module offers more control and sophistication, so this
824 826 could be rewritten to use it (patches welcome).
825 827
826 828 Examples
827 829 --------
828 830 ::
829 831
830 832 In [1]: time 2**128
831 833 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
832 834 Wall time: 0.00
833 835 Out[1]: 340282366920938463463374607431768211456L
834 836
835 837 In [2]: n = 1000000
836 838
837 839 In [3]: time sum(range(n))
838 840 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
839 841 Wall time: 1.37
840 842 Out[3]: 499999500000L
841 843
842 844 In [4]: time print 'hello world'
843 845 hello world
844 846 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
845 847 Wall time: 0.00
846 848
847 849 Note that the time needed by Python to compile the given expression
848 850 will be reported if it is more than 0.1s. In this example, the
849 851 actual exponentiation is done by Python at compilation time, so while
850 852 the expression can take a noticeable amount of time to compute, that
851 853 time is purely due to the compilation:
852 854
853 855 In [5]: time 3**9999;
854 856 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
855 857 Wall time: 0.00 s
856 858
857 859 In [6]: time 3**999999;
858 860 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
859 861 Wall time: 0.00 s
860 862 Compiler : 0.78 s
861 863 """
862 864
863 865 # fail immediately if the given expression can't be compiled
864 866
865 867 expr = self.shell.prefilter(parameter_s,False)
866 868
867 869 # Minimum time above which compilation time will be reported
868 870 tc_min = 0.1
869 871
870 872 try:
871 873 mode = 'eval'
872 874 t0 = clock()
873 875 code = compile(expr,'<timed eval>',mode)
874 876 tc = clock()-t0
875 877 except SyntaxError:
876 878 mode = 'exec'
877 879 t0 = clock()
878 880 code = compile(expr,'<timed exec>',mode)
879 881 tc = clock()-t0
880 882 # skew measurement as little as possible
881 883 glob = self.shell.user_ns
882 884 wtime = time.time
883 885 # time execution
884 886 wall_st = wtime()
885 887 if mode=='eval':
886 888 st = clock2()
887 889 out = eval(code, glob, local_ns)
888 890 end = clock2()
889 891 else:
890 892 st = clock2()
891 893 exec code in glob, local_ns
892 894 end = clock2()
893 895 out = None
894 896 wall_end = wtime()
895 897 # Compute actual times and report
896 898 wall_time = wall_end-wall_st
897 899 cpu_user = end[0]-st[0]
898 900 cpu_sys = end[1]-st[1]
899 901 cpu_tot = cpu_user+cpu_sys
900 902 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
901 903 (cpu_user,cpu_sys,cpu_tot)
902 904 print "Wall time: %.2f s" % wall_time
903 905 if tc > tc_min:
904 906 print "Compiler : %.2f s" % tc
905 907 return out
906 908
907 909 @skip_doctest
908 910 @line_magic
909 911 def macro(self, parameter_s=''):
910 912 """Define a macro for future re-execution. It accepts ranges of history,
911 913 filenames or string objects.
912 914
913 915 Usage:\\
914 916 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
915 917
916 918 Options:
917 919
918 920 -r: use 'raw' input. By default, the 'processed' history is used,
919 921 so that magics are loaded in their transformed version to valid
920 922 Python. If this option is given, the raw input as typed as the
921 923 command line is used instead.
922 924
923 925 This will define a global variable called `name` which is a string
924 926 made of joining the slices and lines you specify (n1,n2,... numbers
925 927 above) from your input history into a single string. This variable
926 928 acts like an automatic function which re-executes those lines as if
927 929 you had typed them. You just type 'name' at the prompt and the code
928 930 executes.
929 931
930 932 The syntax for indicating input ranges is described in %history.
931 933
932 934 Note: as a 'hidden' feature, you can also use traditional python slice
933 935 notation, where N:M means numbers N through M-1.
934 936
935 937 For example, if your history contains (%hist prints it)::
936 938
937 939 44: x=1
938 940 45: y=3
939 941 46: z=x+y
940 942 47: print x
941 943 48: a=5
942 944 49: print 'x',x,'y',y
943 945
944 946 you can create a macro with lines 44 through 47 (included) and line 49
945 947 called my_macro with::
946 948
947 949 In [55]: %macro my_macro 44-47 49
948 950
949 951 Now, typing `my_macro` (without quotes) will re-execute all this code
950 952 in one pass.
951 953
952 954 You don't need to give the line-numbers in order, and any given line
953 955 number can appear multiple times. You can assemble macros with any
954 956 lines from your input history in any order.
955 957
956 958 The macro is a simple object which holds its value in an attribute,
957 959 but IPython's display system checks for macros and executes them as
958 960 code instead of printing them when you type their name.
959 961
960 962 You can view a macro's contents by explicitly printing it with::
961 963
962 964 print macro_name
963 965
964 966 """
965 967 opts,args = self.parse_options(parameter_s,'r',mode='list')
966 968 if not args: # List existing macros
967 969 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
968 970 isinstance(v, Macro))
969 971 if len(args) == 1:
970 972 raise UsageError(
971 973 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
972 974 name, codefrom = args[0], " ".join(args[1:])
973 975
974 976 #print 'rng',ranges # dbg
975 977 try:
976 978 lines = self.shell.find_user_code(codefrom, 'r' in opts)
977 979 except (ValueError, TypeError) as e:
978 980 print e.args[0]
979 981 return
980 982 macro = Macro(lines)
981 983 self.shell.define_macro(name, macro)
982 984 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
983 985 print '=== Macro contents: ==='
984 986 print macro,
985 987
986 988 @magic_arguments.magic_arguments()
987 989 @magic_arguments.argument('output', type=str, default='', nargs='?',
988 990 help="""The name of the variable in which to store output.
989 991 This is a utils.io.CapturedIO object with stdout/err attributes
990 992 for the text of the captured output.
991 993
992 994 CapturedOutput also has a show() method for displaying the output,
993 995 and __call__ as well, so you can use that to quickly display the
994 996 output.
995 997
996 998 If unspecified, captured output is discarded.
997 999 """
998 1000 )
999 1001 @magic_arguments.argument('--no-stderr', action="store_true",
1000 1002 help="""Don't capture stderr."""
1001 1003 )
1002 1004 @magic_arguments.argument('--no-stdout', action="store_true",
1003 1005 help="""Don't capture stdout."""
1004 1006 )
1005 1007 @cell_magic
1006 1008 def capture(self, line, cell):
1007 1009 """run the cell, capturing stdout/err"""
1008 1010 args = magic_arguments.parse_argstring(self.capture, line)
1009 1011 out = not args.no_stdout
1010 1012 err = not args.no_stderr
1011 1013 with capture_output(out, err) as io:
1012 1014 self.shell.run_cell(cell)
1013 1015 if args.output:
1014 1016 self.shell.user_ns[args.output] = io
General Comments 0
You need to be logged in to leave comments. Login now